diff options
Diffstat (limited to 'framework/domain/messageparser.cpp')
-rw-r--r-- | framework/domain/messageparser.cpp | 147 |
1 files changed, 32 insertions, 115 deletions
diff --git a/framework/domain/messageparser.cpp b/framework/domain/messageparser.cpp index ff763aec..ec79d50b 100644 --- a/framework/domain/messageparser.cpp +++ b/framework/domain/messageparser.cpp | |||
@@ -18,9 +18,14 @@ | |||
18 | */ | 18 | */ |
19 | #include "messageparser.h" | 19 | #include "messageparser.h" |
20 | 20 | ||
21 | #include "modeltest.h" | ||
21 | #include "stringhtmlwriter.h" | 22 | #include "stringhtmlwriter.h" |
22 | #include "objecttreesource.h" | 23 | #include "objecttreesource.h" |
23 | 24 | ||
25 | #include "mimetreeparser/interface.h" | ||
26 | |||
27 | #include <QRegExp> | ||
28 | |||
24 | #include <QFile> | 29 | #include <QFile> |
25 | #include <QImage> | 30 | #include <QImage> |
26 | #include <QDebug> | 31 | #include <QDebug> |
@@ -29,128 +34,32 @@ | |||
29 | #include <MimeTreeParser/ObjectTreeParser> | 34 | #include <MimeTreeParser/ObjectTreeParser> |
30 | #include <MimeTreeParser/MessagePart> | 35 | #include <MimeTreeParser/MessagePart> |
31 | 36 | ||
32 | PartModel::PartModel(QSharedPointer<MimeTreeParser::MessagePart> partTree, QMap<QByteArray, QUrl> embeddedPartMap) : mPartTree(partTree), mEmbeddedPartMap(embeddedPartMap) | ||
33 | { | ||
34 | } | ||
35 | 37 | ||
36 | QHash<int, QByteArray> PartModel::roleNames() const | 38 | class MessagePartPrivate |
37 | { | 39 | { |
38 | QHash<int, QByteArray> roles; | 40 | public: |
39 | roles[Text] = "text"; | 41 | QSharedPointer<MimeTreeParser::MessagePart> mPartTree; |
40 | roles[IsHtml] = "isHtml"; | 42 | QString mHtml; |
41 | roles[IsHidden] = "isHidden"; | 43 | QMap<QByteArray, QUrl> mEmbeddedPartMap; |
42 | roles[IsEncrypted] = "isEncrypted"; | 44 | std::shared_ptr<MimeTreeParser::NodeHelper> mNodeHelper; |
43 | roles[IsAttachment] = "isAttachment"; | 45 | std::shared_ptr<Parser> mParser; |
44 | roles[HasContent] = "hasContent"; | 46 | }; |
45 | roles[Type] = "type"; | ||
46 | roles[IsHidden] = "isHidden"; | ||
47 | return roles; | ||
48 | } | ||
49 | 47 | ||
50 | QModelIndex PartModel::index(int row, int column, const QModelIndex &parent) const | 48 | MessageParser::MessageParser(QObject *parent) |
51 | { | 49 | : QObject(parent) |
52 | // qDebug() << "index " << parent << row << column << mPartTree->subParts().size(); | 50 | , d(std::unique_ptr<MessagePartPrivate>(new MessagePartPrivate)) |
53 | if (!parent.isValid()) { | ||
54 | if (row < mPartTree->subParts().size()) { | ||
55 | auto part = mPartTree->subParts().at(row); | ||
56 | return createIndex(row, column, part.data()); | ||
57 | } | ||
58 | } else { | ||
59 | auto part = static_cast<MimeTreeParser::MessagePart*>(parent.internalPointer()); | ||
60 | auto subPart = part->subParts().at(row); | ||
61 | return createIndex(row, column, subPart.data()); | ||
62 | } | ||
63 | return QModelIndex(); | ||
64 | } | ||
65 | |||
66 | QVariant PartModel::data(const QModelIndex &index, int role) const | ||
67 | { | ||
68 | // qDebug() << "Getting data for index"; | ||
69 | if (index.isValid()) { | ||
70 | auto part = static_cast<MimeTreeParser::MessagePart*>(index.internalPointer()); | ||
71 | switch (role) { | ||
72 | case Text: { | ||
73 | // qDebug() << "Getting text: " << part->property("text").toString(); | ||
74 | // FIXME: we should have a list per part, and not one for all parts. | ||
75 | auto text = part->property("htmlContent").toString(); | ||
76 | for (const auto &cid : mEmbeddedPartMap.keys()) { | ||
77 | text.replace(QString("src=\"cid:%1\"").arg(QString(cid)), QString("src=\"%1\"").arg(mEmbeddedPartMap.value(cid).toString())); | ||
78 | } | ||
79 | return text; | ||
80 | } | ||
81 | case IsAttachment: | ||
82 | return part->property("attachment").toBool(); | ||
83 | case IsEncrypted: | ||
84 | return part->property("isEncrypted").toBool(); | ||
85 | case IsHtml: | ||
86 | return part->property("isHtml").toBool(); | ||
87 | case HasContent: | ||
88 | return !part->property("htmlContent").toString().isEmpty(); | ||
89 | case Type: | ||
90 | return part->metaObject()->className(); | ||
91 | case IsHidden: | ||
92 | return false; | ||
93 | //return part->property("isHidden").toBool(); | ||
94 | |||
95 | } | ||
96 | } | ||
97 | return QVariant(); | ||
98 | } | ||
99 | |||
100 | QModelIndex PartModel::parent(const QModelIndex &index) const | ||
101 | { | ||
102 | // qDebug() << "parent " << index; | ||
103 | if (index.isValid()) { | ||
104 | auto part = static_cast<MimeTreeParser::MessagePart*>(index.internalPointer()); | ||
105 | auto parentPart = static_cast<MimeTreeParser::MessagePart*>(part->parentPart()); | ||
106 | auto row = 0;//get the parents parent to find the index | ||
107 | if (!parentPart) { | ||
108 | parentPart = mPartTree.data(); | ||
109 | } | ||
110 | int i = 0; | ||
111 | for (const auto &p : parentPart->subParts()) { | ||
112 | if (p.data() == part) { | ||
113 | row = i; | ||
114 | break; | ||
115 | } | ||
116 | i++; | ||
117 | } | ||
118 | return createIndex(row, index.column(), parentPart); | ||
119 | } | ||
120 | return QModelIndex(); | ||
121 | } | ||
122 | |||
123 | int PartModel::rowCount(const QModelIndex &parent) const | ||
124 | { | 51 | { |
125 | // qDebug() << "Row count " << parent; | ||
126 | if (!parent.isValid()) { | ||
127 | // qDebug() << "Row count " << mPartTree->subParts().size(); | ||
128 | return mPartTree->subParts().size(); | ||
129 | } else { | ||
130 | auto part = static_cast<MimeTreeParser::MessagePart*>(parent.internalPointer()); | ||
131 | if (part) { | ||
132 | return part->subParts().size(); | ||
133 | } | ||
134 | } | ||
135 | return 0; | ||
136 | } | ||
137 | 52 | ||
138 | int PartModel::columnCount(const QModelIndex &parent) const | ||
139 | { | ||
140 | // qDebug() << "Column count " << parent; | ||
141 | return 1; | ||
142 | } | 53 | } |
143 | 54 | ||
144 | 55 | MessageParser::~MessageParser() | |
145 | MessageParser::MessageParser(QObject *parent) | ||
146 | : QObject(parent) | ||
147 | { | 56 | { |
148 | 57 | ||
149 | } | 58 | } |
150 | 59 | ||
151 | QString MessageParser::html() const | 60 | QString MessageParser::html() const |
152 | { | 61 | { |
153 | return mHtml; | 62 | return d->mHtml; |
154 | } | 63 | } |
155 | 64 | ||
156 | QVariant MessageParser::message() const | 65 | QVariant MessageParser::message() const |
@@ -162,6 +71,8 @@ void MessageParser::setMessage(const QVariant &message) | |||
162 | { | 71 | { |
163 | QTime time; | 72 | QTime time; |
164 | time.start(); | 73 | time.start(); |
74 | d->mParser = std::shared_ptr<Parser>(new Parser(message.toByteArray())); | ||
75 | |||
165 | const auto mailData = KMime::CRLFtoLF(message.toByteArray()); | 76 | const auto mailData = KMime::CRLFtoLF(message.toByteArray()); |
166 | KMime::Message::Ptr msg(new KMime::Message); | 77 | KMime::Message::Ptr msg(new KMime::Message); |
167 | msg->setContent(mailData); | 78 | msg->setContent(mailData); |
@@ -171,20 +82,26 @@ void MessageParser::setMessage(const QVariant &message) | |||
171 | // render the mail | 82 | // render the mail |
172 | StringHtmlWriter htmlWriter; | 83 | StringHtmlWriter htmlWriter; |
173 | //temporary files only have the lifetime of the nodehelper, so we keep it around until the mail changes. | 84 | //temporary files only have the lifetime of the nodehelper, so we keep it around until the mail changes. |
174 | mNodeHelper = std::make_shared<MimeTreeParser::NodeHelper>(); | 85 | d->mNodeHelper = std::make_shared<MimeTreeParser::NodeHelper>(); |
175 | ObjectTreeSource source(&htmlWriter); | 86 | ObjectTreeSource source(&htmlWriter); |
176 | MimeTreeParser::ObjectTreeParser otp(&source, mNodeHelper.get()); | 87 | MimeTreeParser::ObjectTreeParser otp(&source, d->mNodeHelper.get()); |
177 | 88 | ||
178 | otp.parseObjectTree(msg.data()); | 89 | otp.parseObjectTree(msg.data()); |
179 | mPartTree = otp.parsedPart().dynamicCast<MimeTreeParser::MessagePart>(); | 90 | d->mPartTree = otp.parsedPart().dynamicCast<MimeTreeParser::MessagePart>(); |
180 | 91 | ||
181 | mEmbeddedPartMap = htmlWriter.embeddedParts(); | 92 | d->mEmbeddedPartMap = htmlWriter.embeddedParts(); |
182 | mHtml = htmlWriter.html(); | 93 | d->mHtml = htmlWriter.html(); |
183 | emit htmlChanged(); | 94 | emit htmlChanged(); |
184 | } | 95 | } |
185 | 96 | ||
186 | QAbstractItemModel *MessageParser::partTree() const | 97 | QAbstractItemModel *MessageParser::partTree() const |
187 | { | 98 | { |
188 | return new PartModel(mPartTree, mEmbeddedPartMap); | 99 | return new PartModel(d->mPartTree, d->mParser); |
189 | } | 100 | } |
190 | 101 | ||
102 | QAbstractItemModel *MessageParser::newTree() const | ||
103 | { | ||
104 | const auto model = new NewModel(d->mParser); | ||
105 | new ModelTest(model, model); | ||
106 | return model; | ||
107 | } | ||