summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--framework/mail/CMakeLists.txt2
-rw-r--r--framework/mail/filehtmlwriter.cpp111
-rw-r--r--framework/mail/maillistmodel.cpp14
-rw-r--r--framework/mail/stringhtmlwriter.cpp134
-rw-r--r--framework/mail/stringhtmlwriter.h (renamed from framework/mail/filehtmlwriter.h)32
5 files changed, 156 insertions, 137 deletions
diff --git a/framework/mail/CMakeLists.txt b/framework/mail/CMakeLists.txt
index e7286763..0abc6880 100644
--- a/framework/mail/CMakeLists.txt
+++ b/framework/mail/CMakeLists.txt
@@ -5,8 +5,8 @@ set(mailplugin_SRCS
5 singlemailcontroller.cpp 5 singlemailcontroller.cpp
6 folderlistmodel.cpp 6 folderlistmodel.cpp
7 folderlistcontroller.cpp 7 folderlistcontroller.cpp
8 filehtmlwriter.cpp
9 objecttreesource.cpp 8 objecttreesource.cpp
9 stringhtmlwriter.cpp
10) 10)
11 11
12add_library(mailplugin SHARED ${mailplugin_SRCS}) 12add_library(mailplugin SHARED ${mailplugin_SRCS})
diff --git a/framework/mail/filehtmlwriter.cpp b/framework/mail/filehtmlwriter.cpp
deleted file mode 100644
index e435e1be..00000000
--- a/framework/mail/filehtmlwriter.cpp
+++ /dev/null
@@ -1,111 +0,0 @@
1/* -*- c++ -*-
2 filehtmlwriter.cpp
3
4 This file is part of KMail, the KDE mail client.
5 Copyright (c) 2003 Marc Mutz <mutz@kde.org>
6
7 KMail is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License, version 2, as
9 published by the Free Software Foundation.
10
11 KMail is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
20 In addition, as a special exception, the copyright holders give
21 permission to link the code of this program with any edition of
22 the Qt library by Trolltech AS, Norway (or with modified versions
23 of Qt that use the same license as Qt), and distribute linked
24 combinations including the two. You must obey the GNU General
25 Public License in all respects for all of the code used other than
26 Qt. If you modify this file, you may extend this exception to
27 your version of the file, but you are not obligated to do so. If
28 you do not wish to do so, delete this exception statement from
29 your version.
30*/
31
32#include "filehtmlwriter.h"
33
34#include <QTextStream>
35
36FileHtmlWriter::FileHtmlWriter(const QString &filename)
37 : MessageViewer::HtmlWriter(),
38 mFile(filename.isEmpty() ? QStringLiteral("filehtmlwriter.out") : filename)
39{
40 mStream.setCodec("UTF-8");
41}
42
43FileHtmlWriter::~FileHtmlWriter()
44{
45 if (mFile.isOpen()) {
46 mStream.setDevice(0);
47 mFile.close();
48 }
49}
50
51void FileHtmlWriter::begin(const QString &css)
52{
53 openOrWarn();
54 if (!css.isEmpty()) {
55 write(QLatin1String("<!-- CSS Definitions \n") + css + QLatin1String("-->\n"));
56 }
57}
58
59void FileHtmlWriter::end()
60{
61 flush();
62 mStream.setDevice(0);
63 mFile.close();
64}
65
66void FileHtmlWriter::reset()
67{
68 if (mFile.isOpen()) {
69 mStream.setDevice(0);
70 mFile.close();
71 }
72}
73
74void FileHtmlWriter::write(const QString &str)
75{
76 mStream << str.toUtf8();
77 flush();
78}
79
80void FileHtmlWriter::queue(const QString &str)
81{
82 write(str);
83}
84
85void FileHtmlWriter::flush()
86{
87 mStream.flush();
88 mFile.flush();
89}
90
91void FileHtmlWriter::openOrWarn()
92{
93 if (mFile.isOpen()) {
94 mStream.setDevice(0);
95 mFile.close();
96 }
97 if (!mFile.open(QIODevice::WriteOnly)) {
98 } else {
99 mStream.setDevice(&mFile);
100 }
101}
102
103void FileHtmlWriter::embedPart(const QByteArray &contentId, const QString &url)
104{
105 mStream << "<!-- embedPart(contentID=" << contentId << ", url=" << url << ") -->" << endl;
106 flush();
107}
108void FileHtmlWriter::extraHead(const QString &)
109{
110
111} \ No newline at end of file
diff --git a/framework/mail/maillistmodel.cpp b/framework/mail/maillistmodel.cpp
index c37e2be4..d3b60187 100644
--- a/framework/mail/maillistmodel.cpp
+++ b/framework/mail/maillistmodel.cpp
@@ -1,6 +1,6 @@
1#include "maillistmodel.h" 1#include "maillistmodel.h"
2 2
3#include "filehtmlwriter.h" 3#include "stringhtmlwriter.h"
4#include "objecttreesource.h" 4#include "objecttreesource.h"
5 5
6#include <QFile> 6#include <QFile>
@@ -78,8 +78,7 @@ QVariant MailListModel::data(const QModelIndex &idx, int role) const
78 msg->parse(); 78 msg->parse();
79 79
80 // render the mail 80 // render the mail
81 const QString fname("/tmp/test.html"); 81 StringHtmlWriter htmlWriter;
82 FileHtmlWriter htmlWriter(fname);
83 QImage paintDevice; 82 QImage paintDevice;
84 MessageViewer::CSSHelper cssHelper(&paintDevice); 83 MessageViewer::CSSHelper cssHelper(&paintDevice);
85 MessageViewer::NodeHelper nodeHelper; 84 MessageViewer::NodeHelper nodeHelper;
@@ -92,16 +91,9 @@ QVariant MailListModel::data(const QModelIndex &idx, int role) const
92 otp.parseObjectTree(msg.data()); 91 otp.parseObjectTree(msg.data());
93 92
94 htmlWriter.queue(QStringLiteral("</body></html>")); 93 htmlWriter.queue(QStringLiteral("</body></html>"));
95 htmlWriter.flush();
96 htmlWriter.end(); 94 htmlWriter.end();
97 95
98 QFile file(fname); 96 return htmlWriter.html();
99 if (file.open(QFile::ReadOnly)) {
100 const auto content = file.readAll();
101 return content;
102 } else {
103 qWarning() << "Failed to open the file";
104 }
105 } else { 97 } else {
106 qWarning() << "Failed to open the file"; 98 qWarning() << "Failed to open the file";
107 } 99 }
diff --git a/framework/mail/stringhtmlwriter.cpp b/framework/mail/stringhtmlwriter.cpp
new file mode 100644
index 00000000..2c84dc6f
--- /dev/null
+++ b/framework/mail/stringhtmlwriter.cpp
@@ -0,0 +1,134 @@
1/* -*- c++ -*-
2 filehtmlwriter.cpp
3
4 This file is part of KMail, the KDE mail client.
5 Copyright (c) 2003 Marc Mutz <mutz@kde.org>
6
7 KMail is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License, version 2, as
9 published by the Free Software Foundation.
10
11 KMail is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
20 In addition, as a special exception, the copyright holders give
21 permission to link the code of this program with any edition of
22 the Qt library by Trolltech AS, Norway (or with modified versions
23 of Qt that use the same license as Qt), and distribute linked
24 combinations including the two. You must obey the GNU General
25 Public License in all respects for all of the code used other than
26 Qt. If you modify this file, you may extend this exception to
27 your version of the file, but you are not obligated to do so. If
28 you do not wish to do so, delete this exception statement from
29 your version.
30*/
31
32#include "stringhtmlwriter.h"
33
34#include <QDebug>
35#include <QTextStream>
36
37StringHtmlWriter::StringHtmlWriter()
38 : MessageViewer::HtmlWriter()
39 , mState(Ended)
40{
41}
42
43StringHtmlWriter::~StringHtmlWriter()
44{
45}
46
47void StringHtmlWriter::begin(const QString &css)
48{
49 if (mState != Ended) {
50 qWarning() << "begin() called on non-ended session!";
51 reset();
52 }
53
54 mState = Begun;
55 mExtraHead.clear();
56 mHtml.clear();
57
58 if (!css.isEmpty()) {
59 write(QLatin1String("<!-- CSS Definitions \n") + css + QLatin1String("-->\n"));
60 }
61}
62
63void StringHtmlWriter::end()
64{
65 if (mState != Begun) {
66 qWarning() << "Called on non-begun or queued session!";
67 }
68
69 if (!mExtraHead.isEmpty()) {
70 insertExtraHead();
71 mExtraHead.clear();
72 }
73 mState = Ended;
74}
75
76void StringHtmlWriter::reset()
77{
78 if (mState != Ended) {
79 mHtml.clear();
80 mExtraHead.clear();
81 mState = Begun; // don't run into end()'s warning
82 end();
83 mState = Ended;
84 }
85}
86
87void StringHtmlWriter::write(const QString &str)
88{
89 if (mState != Begun) {
90 qWarning() << "Called in Ended or Queued state!";
91 }
92 mHtml.append(str);
93}
94
95void StringHtmlWriter::queue(const QString &str)
96{
97 write(str);
98}
99
100void StringHtmlWriter::flush()
101{
102 mState = Begun; // don't run into end()'s warning
103 end();
104}
105
106void StringHtmlWriter::embedPart(const QByteArray &contentId, const QString &url)
107{
108 write("<!-- embedPart(contentID=" + contentId + ", url=" + url + ") -->\n");
109}
110void StringHtmlWriter::extraHead(const QString &extraHead)
111{
112 if (mState != Ended) {
113 qWarning() << "Called on non-started session!";
114 }
115 mExtraHead.append(extraHead);
116}
117
118
119void StringHtmlWriter::insertExtraHead()
120{
121 const QString headTag(QStringLiteral("<head>"));
122 const int index = mHtml.indexOf(headTag);
123 if (index != -1) {
124 mHtml.insert(index + headTag.length(), mExtraHead);
125 }
126}
127
128QString StringHtmlWriter::html() const
129{
130 if (mState != Ended) {
131 qWarning() << "Called on non-ended session!";
132 }
133 return mHtml;
134}
diff --git a/framework/mail/filehtmlwriter.h b/framework/mail/stringhtmlwriter.h
index 8bda39f8..0805d027 100644
--- a/framework/mail/filehtmlwriter.h
+++ b/framework/mail/stringhtmlwriter.h
@@ -1,14 +1,12 @@
1/* -*- c++ -*- 1/* -*- c++ -*-
2 filehtmlwriter.h
3 2
4 This file is part of KMail, the KDE mail client. 3 Copyright (c) 2016 Sandro Knauß <sknauss@kde.org>
5 Copyright (c) 2003 Marc Mutz <mutz@kde.org>
6 4
7 KMail is free software; you can redistribute it and/or modify it 5 Kube is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License, version 2, as 6 under the terms of the GNU General Public License, version 2, as
9 published by the Free Software Foundation. 7 published by the Free Software Foundation.
10 8
11 KMail is distributed in the hope that it will be useful, but 9 Kube is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of 10 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details. 12 General Public License for more details.
@@ -29,8 +27,8 @@
29 your version. 27 your version.
30*/ 28*/
31 29
32#ifndef __MESSAGEVIEWER_FILEHTMLWRITER_H__ 30#ifndef __KUBE_FRAMEWORK_MAIL_STRINGHTMLWRITER_H__
33#define __MESSAGEVIEWER_FILEHTMLWRITER_H__ 31#define __KUBE_FRAMEWORK_MAIL_STRINGHTMLWRITER_H__
34 32
35#include <MessageViewer/HtmlWriter> 33#include <MessageViewer/HtmlWriter>
36 34
@@ -39,11 +37,11 @@
39 37
40class QString; 38class QString;
41 39
42class FileHtmlWriter : public MessageViewer::HtmlWriter 40class StringHtmlWriter : public MessageViewer::HtmlWriter
43{ 41{
44public: 42public:
45 explicit FileHtmlWriter(const QString &filename); 43 explicit StringHtmlWriter();
46 virtual ~FileHtmlWriter(); 44 virtual ~StringHtmlWriter();
47 45
48 void begin(const QString &cssDefs) Q_DECL_OVERRIDE; 46 void begin(const QString &cssDefs) Q_DECL_OVERRIDE;
49 void end() Q_DECL_OVERRIDE; 47 void end() Q_DECL_OVERRIDE;
@@ -53,12 +51,18 @@ public:
53 void flush() Q_DECL_OVERRIDE; 51 void flush() Q_DECL_OVERRIDE;
54 void embedPart(const QByteArray &contentId, const QString &url) Q_DECL_OVERRIDE; 52 void embedPart(const QByteArray &contentId, const QString &url) Q_DECL_OVERRIDE;
55 void extraHead(const QString &str) Q_DECL_OVERRIDE; 53 void extraHead(const QString &str) Q_DECL_OVERRIDE;
56private:
57 void openOrWarn();
58 54
55 QString html() const;
59private: 56private:
60 QFile mFile; 57 void insertExtraHead();
61 QTextStream mStream; 58
59 QString mHtml;
60 QString mExtraHead;
61 enum State {
62 Begun,
63 Queued,
64 Ended
65 } mState;
62}; 66};
63 67
64#endif // __MESSAGEVIEWER_FILEHTMLWRITER_H__ 68#endif // __MESSAGEVIEWER_FILEHTMLWRITER_H__