diff options
Diffstat (limited to 'framework/src/domain/messageparser.cpp')
-rw-r--r-- | framework/src/domain/messageparser.cpp | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/framework/src/domain/messageparser.cpp b/framework/src/domain/messageparser.cpp new file mode 100644 index 00000000..ea2ecbf1 --- /dev/null +++ b/framework/src/domain/messageparser.cpp | |||
@@ -0,0 +1,114 @@ | |||
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 "modeltest.h" | ||
22 | #include "stringhtmlwriter.h" | ||
23 | #include "objecttreesource.h" | ||
24 | |||
25 | #include "mimetreeparser/interface.h" | ||
26 | |||
27 | #include <QRegExp> | ||
28 | |||
29 | #include <QFile> | ||
30 | #include <QImage> | ||
31 | #include <QDebug> | ||
32 | #include <QTime> | ||
33 | #include <QUrl> | ||
34 | #include <MimeTreeParser/ObjectTreeParser> | ||
35 | #include <MimeTreeParser/MessagePart> | ||
36 | |||
37 | |||
38 | class MessagePartPrivate | ||
39 | { | ||
40 | public: | ||
41 | QSharedPointer<MimeTreeParser::MessagePart> mPartTree; | ||
42 | QString mHtml; | ||
43 | QMap<QByteArray, QUrl> mEmbeddedPartMap; | ||
44 | std::shared_ptr<MimeTreeParser::NodeHelper> mNodeHelper; | ||
45 | std::shared_ptr<Parser> mParser; | ||
46 | }; | ||
47 | |||
48 | MessageParser::MessageParser(QObject *parent) | ||
49 | : QObject(parent) | ||
50 | , d(std::unique_ptr<MessagePartPrivate>(new MessagePartPrivate)) | ||
51 | { | ||
52 | |||
53 | } | ||
54 | |||
55 | MessageParser::~MessageParser() | ||
56 | { | ||
57 | |||
58 | } | ||
59 | |||
60 | QString MessageParser::html() const | ||
61 | { | ||
62 | return d->mHtml; | ||
63 | } | ||
64 | |||
65 | QVariant MessageParser::message() const | ||
66 | { | ||
67 | return QVariant(); | ||
68 | } | ||
69 | |||
70 | void MessageParser::setMessage(const QVariant &message) | ||
71 | { | ||
72 | // QTime time; | ||
73 | // time.start(); | ||
74 | d->mParser = std::shared_ptr<Parser>(new Parser(message.toByteArray())); | ||
75 | |||
76 | const auto mailData = KMime::CRLFtoLF(message.toByteArray()); | ||
77 | KMime::Message::Ptr msg(new KMime::Message); | ||
78 | msg->setContent(mailData); | ||
79 | msg->parse(); | ||
80 | // qWarning() << "parsed: " << time.elapsed(); | ||
81 | |||
82 | // render the mail | ||
83 | StringHtmlWriter htmlWriter; | ||
84 | //temporary files only have the lifetime of the nodehelper, so we keep it around until the mail changes. | ||
85 | d->mNodeHelper = std::make_shared<MimeTreeParser::NodeHelper>(); | ||
86 | ObjectTreeSource source(&htmlWriter); | ||
87 | MimeTreeParser::ObjectTreeParser otp(&source, d->mNodeHelper.get()); | ||
88 | |||
89 | otp.parseObjectTree(msg.data()); | ||
90 | d->mPartTree = otp.parsedPart().dynamicCast<MimeTreeParser::MessagePart>(); | ||
91 | |||
92 | d->mEmbeddedPartMap = htmlWriter.embeddedParts(); | ||
93 | d->mHtml = htmlWriter.html(); | ||
94 | emit htmlChanged(); | ||
95 | } | ||
96 | |||
97 | QAbstractItemModel *MessageParser::partTree() const | ||
98 | { | ||
99 | return new PartModel(d->mPartTree, d->mParser); | ||
100 | } | ||
101 | |||
102 | QAbstractItemModel *MessageParser::newTree() const | ||
103 | { | ||
104 | const auto model = new NewModel(d->mParser); | ||
105 | // new ModelTest(model, model); | ||
106 | return model; | ||
107 | } | ||
108 | |||
109 | QAbstractItemModel *MessageParser::attachments() const | ||
110 | { | ||
111 | const auto model = new AttachmentModel(d->mParser); | ||
112 | // new ModelTest(model, model); | ||
113 | return model; | ||
114 | } | ||