diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-02-22 20:21:26 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-02-22 20:21:26 +0100 |
commit | 88bbb10880d65af417689c7ad3455acf39a33215 (patch) | |
tree | a08c72761d46c628ad148096b960927a8b215b6e | |
parent | adc2201b961c31ffe0206efd7dd7d309bf5a8082 (diff) | |
download | kube-88bbb10880d65af417689c7ad3455acf39a33215.tar.gz kube-88bbb10880d65af417689c7ad3455acf39a33215.zip |
Moved the mail parsing into it's own little corner
-rw-r--r-- | applications/kube-mail/package/contents/ui/MailViewer.qml | 9 | ||||
-rw-r--r-- | applications/kube-mail/package/contents/ui/SingleMailView.qml | 2 | ||||
-rw-r--r-- | framework/mail/CMakeLists.txt | 1 | ||||
-rw-r--r-- | framework/mail/maillistmodel.cpp | 38 | ||||
-rw-r--r-- | framework/mail/maillistmodel.h | 3 | ||||
-rw-r--r-- | framework/mail/mailplugin.cpp | 2 | ||||
-rw-r--r-- | framework/mail/messageparser.cpp | 76 | ||||
-rw-r--r-- | framework/mail/messageparser.h | 45 |
8 files changed, 135 insertions, 41 deletions
diff --git a/applications/kube-mail/package/contents/ui/MailViewer.qml b/applications/kube-mail/package/contents/ui/MailViewer.qml index 5929bea0..1d305297 100644 --- a/applications/kube-mail/package/contents/ui/MailViewer.qml +++ b/applications/kube-mail/package/contents/ui/MailViewer.qml | |||
@@ -3,8 +3,11 @@ import QtQuick.Controls 1.3 | |||
3 | import QtQuick.Layouts 1.1 | 3 | import QtQuick.Layouts 1.1 |
4 | import QtWebKit 3.0 | 4 | import QtWebKit 3.0 |
5 | 5 | ||
6 | import org.kde.kube.mail 1.0 as Mail | ||
7 | |||
6 | Item { | 8 | Item { |
7 | id: root | 9 | id: root |
10 | property variant message; | ||
8 | property string html; | 11 | property string html; |
9 | 12 | ||
10 | WebView { | 13 | WebView { |
@@ -31,4 +34,10 @@ Item { | |||
31 | // The file:/// argument is necessary so local icons are found | 34 | // The file:/// argument is necessary so local icons are found |
32 | webview.loadHtml(html, "file:///"); | 35 | webview.loadHtml(html, "file:///"); |
33 | } | 36 | } |
37 | |||
38 | Mail.MessageParser { | ||
39 | id: messageParser | ||
40 | message: root.message | ||
41 | } | ||
42 | html: messageParser.html | ||
34 | } | 43 | } |
diff --git a/applications/kube-mail/package/contents/ui/SingleMailView.qml b/applications/kube-mail/package/contents/ui/SingleMailView.qml index e9563f1e..d6ae4bfa 100644 --- a/applications/kube-mail/package/contents/ui/SingleMailView.qml +++ b/applications/kube-mail/package/contents/ui/SingleMailView.qml | |||
@@ -63,7 +63,7 @@ Item { | |||
63 | } | 63 | } |
64 | 64 | ||
65 | MailViewer { | 65 | MailViewer { |
66 | html: model.renderedMessage | 66 | message: model.mimeMessage |
67 | Layout.fillHeight: true | 67 | Layout.fillHeight: true |
68 | Layout.fillWidth: true | 68 | Layout.fillWidth: true |
69 | } | 69 | } |
diff --git a/framework/mail/CMakeLists.txt b/framework/mail/CMakeLists.txt index f83a3a9e..13788cfa 100644 --- a/framework/mail/CMakeLists.txt +++ b/framework/mail/CMakeLists.txt | |||
@@ -10,6 +10,7 @@ set(mailplugin_SRCS | |||
10 | stringhtmlwriter.cpp | 10 | stringhtmlwriter.cpp |
11 | csshelper.cpp | 11 | csshelper.cpp |
12 | composer.cpp | 12 | composer.cpp |
13 | messageparser.cpp | ||
13 | ) | 14 | ) |
14 | add_definitions(-DMAIL_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/data") | 15 | add_definitions(-DMAIL_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/data") |
15 | 16 | ||
diff --git a/framework/mail/maillistmodel.cpp b/framework/mail/maillistmodel.cpp index 6bd26759..0bed1305 100644 --- a/framework/mail/maillistmodel.cpp +++ b/framework/mail/maillistmodel.cpp | |||
@@ -20,13 +20,7 @@ | |||
20 | 20 | ||
21 | #include "maillistmodel.h" | 21 | #include "maillistmodel.h" |
22 | 22 | ||
23 | #include "stringhtmlwriter.h" | ||
24 | #include "objecttreesource.h" | ||
25 | #include "csshelper.h" | ||
26 | |||
27 | #include <QFile> | 23 | #include <QFile> |
28 | #include <QImage> | ||
29 | #include <MessageViewer/ObjectTreeParser> | ||
30 | 24 | ||
31 | 25 | ||
32 | MailListModel::MailListModel(QObject *parent) | 26 | MailListModel::MailListModel(QObject *parent) |
@@ -53,7 +47,6 @@ QHash< int, QByteArray > MailListModel::roleNames() const | |||
53 | roles[Important] = "important"; | 47 | roles[Important] = "important"; |
54 | roles[Id] = "id"; | 48 | roles[Id] = "id"; |
55 | roles[MimeMessage] = "mimeMessage"; | 49 | roles[MimeMessage] = "mimeMessage"; |
56 | roles[RenderedMessage] = "renderedMessage"; | ||
57 | roles[DomainObject] = "domainObject"; | 50 | roles[DomainObject] = "domainObject"; |
58 | 51 | ||
59 | return roles; | 52 | return roles; |
@@ -90,37 +83,6 @@ QVariant MailListModel::data(const QModelIndex &idx, int role) const | |||
90 | } | 83 | } |
91 | return "Failed to read mail."; | 84 | return "Failed to read mail."; |
92 | } | 85 | } |
93 | case RenderedMessage: { | ||
94 | auto filename = srcIdx.sibling(srcIdx.row(), 6).data(Qt::DisplayRole).toString(); | ||
95 | QFile file(filename); | ||
96 | if (file.open(QFile::ReadOnly)) { | ||
97 | const auto mailData = KMime::CRLFtoLF(file.readAll()); | ||
98 | KMime::Message::Ptr msg(new KMime::Message); | ||
99 | msg->setContent(mailData); | ||
100 | msg->parse(); | ||
101 | |||
102 | // render the mail | ||
103 | StringHtmlWriter htmlWriter; | ||
104 | QImage paintDevice; | ||
105 | CSSHelper cssHelper(&paintDevice); | ||
106 | MessageViewer::NodeHelper nodeHelper; | ||
107 | ObjectTreeSource source(&htmlWriter, &cssHelper); | ||
108 | MessageViewer::ObjectTreeParser otp(&source, &nodeHelper); | ||
109 | |||
110 | htmlWriter.begin(QString()); | ||
111 | htmlWriter.queue(cssHelper.htmlHead(false)); | ||
112 | |||
113 | otp.parseObjectTree(msg.data()); | ||
114 | |||
115 | htmlWriter.queue(QStringLiteral("</body></html>")); | ||
116 | htmlWriter.end(); | ||
117 | |||
118 | return htmlWriter.html(); | ||
119 | } else { | ||
120 | qWarning() << "Failed to open the file"; | ||
121 | } | ||
122 | return "Failed to read mail."; | ||
123 | } | ||
124 | } | 86 | } |
125 | return QSortFilterProxyModel::data(idx, role); | 87 | return QSortFilterProxyModel::data(idx, role); |
126 | } | 88 | } |
diff --git a/framework/mail/maillistmodel.h b/framework/mail/maillistmodel.h index d179eb02..47a2a091 100644 --- a/framework/mail/maillistmodel.h +++ b/framework/mail/maillistmodel.h | |||
@@ -49,8 +49,7 @@ public: | |||
49 | Important, | 49 | Important, |
50 | Id, | 50 | Id, |
51 | MimeMessage, | 51 | MimeMessage, |
52 | DomainObject, | 52 | DomainObject |
53 | RenderedMessage | ||
54 | }; | 53 | }; |
55 | 54 | ||
56 | QHash<int, QByteArray> roleNames() const; | 55 | QHash<int, QByteArray> roleNames() const; |
diff --git a/framework/mail/mailplugin.cpp b/framework/mail/mailplugin.cpp index 691d4a15..750c6f8e 100644 --- a/framework/mail/mailplugin.cpp +++ b/framework/mail/mailplugin.cpp | |||
@@ -23,6 +23,7 @@ | |||
23 | #include "maillistmodel.h" | 23 | #include "maillistmodel.h" |
24 | #include "folderlistmodel.h" | 24 | #include "folderlistmodel.h" |
25 | #include "composer.h" | 25 | #include "composer.h" |
26 | #include "messageparser.h" | ||
26 | 27 | ||
27 | #include <QtQml> | 28 | #include <QtQml> |
28 | 29 | ||
@@ -33,4 +34,5 @@ void MailPlugin::registerTypes (const char *uri) | |||
33 | qmlRegisterType<FolderListModel>(uri, 1, 0, "FolderListModel"); | 34 | qmlRegisterType<FolderListModel>(uri, 1, 0, "FolderListModel"); |
34 | qmlRegisterType<MailListModel>(uri, 1, 0, "MailListModel"); | 35 | qmlRegisterType<MailListModel>(uri, 1, 0, "MailListModel"); |
35 | qmlRegisterType<Composer>(uri, 1, 0, "Composer"); | 36 | qmlRegisterType<Composer>(uri, 1, 0, "Composer"); |
37 | qmlRegisterType<MessageParser>(uri, 1, 0, "MessageParser"); | ||
36 | } | 38 | } |
diff --git a/framework/mail/messageparser.cpp b/framework/mail/messageparser.cpp new file mode 100644 index 00000000..2529a677 --- /dev/null +++ b/framework/mail/messageparser.cpp | |||
@@ -0,0 +1,76 @@ | |||
1 | /* | ||
2 | Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsys.com> | ||
3 | |||
4 | This library is free software; you can redistribute it and/or modify it | ||
5 | under the terms of the GNU Library General Public License as published by | ||
6 | the Free Software Foundation; either version 2 of the License, or (at your | ||
7 | option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, but WITHOUT | ||
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||
12 | License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this library; see the file COPYING.LIB. If not, write to the | ||
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
17 | 02110-1301, USA. | ||
18 | */ | ||
19 | #include "messageparser.h" | ||
20 | |||
21 | #include "stringhtmlwriter.h" | ||
22 | #include "objecttreesource.h" | ||
23 | #include "csshelper.h" | ||
24 | |||
25 | #include <QFile> | ||
26 | #include <QImage> | ||
27 | #include <QDebug> | ||
28 | #include <QTime> | ||
29 | #include <MessageViewer/ObjectTreeParser> | ||
30 | |||
31 | MessageParser::MessageParser(QObject *parent) | ||
32 | : QObject(parent) | ||
33 | { | ||
34 | |||
35 | } | ||
36 | |||
37 | QString MessageParser::html() const | ||
38 | { | ||
39 | return mHtml; | ||
40 | } | ||
41 | |||
42 | QVariant MessageParser::message() const | ||
43 | { | ||
44 | return QVariant(); | ||
45 | } | ||
46 | |||
47 | void MessageParser::setMessage(const QVariant &message) | ||
48 | { | ||
49 | QTime time; | ||
50 | time.start(); | ||
51 | const auto mailData = KMime::CRLFtoLF(message.toByteArray()); | ||
52 | KMime::Message::Ptr msg(new KMime::Message); | ||
53 | msg->setContent(mailData); | ||
54 | msg->parse(); | ||
55 | qWarning() << "parsed: " << time.elapsed(); | ||
56 | qWarning() << "parsed: " << message.toByteArray(); | ||
57 | |||
58 | // render the mail | ||
59 | StringHtmlWriter htmlWriter; | ||
60 | QImage paintDevice; | ||
61 | CSSHelper cssHelper(&paintDevice); | ||
62 | MessageViewer::NodeHelper nodeHelper; | ||
63 | ObjectTreeSource source(&htmlWriter, &cssHelper); | ||
64 | MessageViewer::ObjectTreeParser otp(&source, &nodeHelper); | ||
65 | |||
66 | htmlWriter.begin(QString()); | ||
67 | htmlWriter.queue(cssHelper.htmlHead(false)); | ||
68 | |||
69 | otp.parseObjectTree(msg.data()); | ||
70 | |||
71 | htmlWriter.queue(QStringLiteral("</body></html>")); | ||
72 | htmlWriter.end(); | ||
73 | |||
74 | mHtml = htmlWriter.html(); | ||
75 | emit htmlChanged(); | ||
76 | } | ||
diff --git a/framework/mail/messageparser.h b/framework/mail/messageparser.h new file mode 100644 index 00000000..754ac2bd --- /dev/null +++ b/framework/mail/messageparser.h | |||
@@ -0,0 +1,45 @@ | |||
1 | /* | ||
2 | Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsys.com> | ||
3 | |||
4 | This library is free software; you can redistribute it and/or modify it | ||
5 | under the terms of the GNU Library General Public License as published by | ||
6 | the Free Software Foundation; either version 2 of the License, or (at your | ||
7 | option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, but WITHOUT | ||
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||
12 | License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this library; see the file COPYING.LIB. If not, write to the | ||
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
17 | 02110-1301, USA. | ||
18 | */ | ||
19 | |||
20 | #pragma once | ||
21 | |||
22 | #include <QObject> | ||
23 | #include <QString> | ||
24 | #include <QStringList> | ||
25 | |||
26 | class MessageParser : public QObject | ||
27 | { | ||
28 | Q_OBJECT | ||
29 | Q_PROPERTY (QVariant message READ message WRITE setMessage) | ||
30 | Q_PROPERTY (QString html READ html NOTIFY htmlChanged) | ||
31 | |||
32 | public: | ||
33 | explicit MessageParser(QObject *parent = Q_NULLPTR); | ||
34 | |||
35 | QString html() const; | ||
36 | |||
37 | QVariant message() const; | ||
38 | void setMessage(const QVariant &to); | ||
39 | |||
40 | signals: | ||
41 | void htmlChanged(); | ||
42 | |||
43 | private: | ||
44 | QString mHtml; | ||
45 | }; | ||