1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
/*
Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsys.com>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "messageparser.h"
#include "stringhtmlwriter.h"
#include "objecttreesource.h"
#include "csshelper.h"
#include <QFile>
#include <QImage>
#include <QDebug>
#include <QTime>
#include <QUrl>
#include <MimeTreeParser/ObjectTreeParser>
#include <MimeTreeParser/MessagePart>
PartModel::PartModel(QSharedPointer<MimeTreeParser::MessagePart> partTree, QMap<QByteArray, QUrl> embeddedPartMap) : mPartTree(partTree), mEmbeddedPartMap(embeddedPartMap)
{
}
QHash<int, QByteArray> PartModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[Text] = "text";
roles[IsHtml] = "isHtml";
roles[IsHidden] = "isHidden";
roles[IsEncrypted] = "isEncrypted";
roles[IsAttachment] = "isAttachment";
roles[HasContent] = "hasContent";
roles[Type] = "type";
roles[IsHidden] = "isHidden";
return roles;
}
QModelIndex PartModel::index(int row, int column, const QModelIndex &parent) const
{
// qDebug() << "index " << parent << row << column << mPartTree->subParts().size();
if (!parent.isValid()) {
if (row < mPartTree->subParts().size()) {
auto part = mPartTree->subParts().at(row);
return createIndex(row, column, part.data());
}
} else {
auto part = static_cast<MimeTreeParser::MessagePart*>(parent.internalPointer());
auto subPart = part->subParts().at(row);
return createIndex(row, column, subPart.data());
}
return QModelIndex();
}
QVariant PartModel::data(const QModelIndex &index, int role) const
{
// qDebug() << "Getting data for index";
if (index.isValid()) {
auto part = static_cast<MimeTreeParser::MessagePart*>(index.internalPointer());
switch (role) {
case Text: {
// qDebug() << "Getting text: " << part->property("text").toString();
// FIXME: we should have a list per part, and not one for all parts.
auto text = part->property("htmlContent").toString();
for (const auto &cid : mEmbeddedPartMap.keys()) {
text.replace(QString("src=\"cid:%1\"").arg(QString(cid)), QString("src=\"%1\"").arg(mEmbeddedPartMap.value(cid).toString()));
}
return text;
}
case IsAttachment:
return part->property("attachment").toBool();
case IsEncrypted:
return part->property("isEncrypted").toBool();
case IsHtml:
return part->property("isHtml").toBool();
case HasContent:
return !part->property("htmlContent").toString().isEmpty();
case Type:
return part->metaObject()->className();
case IsHidden:
return part->property("isHidden").toBool();
}
}
return QVariant();
}
QModelIndex PartModel::parent(const QModelIndex &index) const
{
// qDebug() << "parent " << index;
if (index.isValid()) {
auto part = static_cast<MimeTreeParser::MessagePart*>(index.internalPointer());
auto parentPart = static_cast<MimeTreeParser::MessagePart*>(part->parentPart());
auto row = 0;//get the parents parent to find the index
if (!parentPart) {
parentPart = mPartTree.data();
}
int i = 0;
for (const auto &p : parentPart->subParts()) {
if (p.data() == part) {
row = i;
break;
}
i++;
}
return createIndex(row, index.column(), parentPart);
}
return QModelIndex();
}
int PartModel::rowCount(const QModelIndex &parent) const
{
// qDebug() << "Row count " << parent;
if (!parent.isValid()) {
// qDebug() << "Row count " << mPartTree->subParts().size();
return mPartTree->subParts().size();
} else {
auto part = static_cast<MimeTreeParser::MessagePart*>(parent.internalPointer());
if (part) {
return part->subParts().size();
}
}
return 0;
}
int PartModel::columnCount(const QModelIndex &parent) const
{
// qDebug() << "Column count " << parent;
return 1;
}
MessageParser::MessageParser(QObject *parent)
: QObject(parent)
{
}
QString MessageParser::html() const
{
return mHtml;
}
QVariant MessageParser::message() const
{
return QVariant();
}
void MessageParser::setMessage(const QVariant &message)
{
QTime time;
time.start();
const auto mailData = KMime::CRLFtoLF(message.toByteArray());
KMime::Message::Ptr msg(new KMime::Message);
msg->setContent(mailData);
msg->parse();
qWarning() << "parsed: " << time.elapsed();
// render the mail
StringHtmlWriter htmlWriter;
QImage paintDevice;
CSSHelper cssHelper(&paintDevice);
//temporary files only have the lifetime of the nodehelper, so we keep it around until the mail changes.
mNodeHelper = std::make_shared<MimeTreeParser::NodeHelper>();
ObjectTreeSource source(&htmlWriter, &cssHelper);
MimeTreeParser::ObjectTreeParser otp(&source, mNodeHelper.get());
otp.parseObjectTree(msg.data());
mPartTree = otp.parsedPart().dynamicCast<MimeTreeParser::MessagePart>();
mEmbeddedPartMap = htmlWriter.embeddedParts();
mHtml = htmlWriter.html();
emit htmlChanged();
}
QAbstractItemModel *MessageParser::partTree() const
{
qDebug() << "Getting partTree";
qDebug() << "Row count " << mPartTree->subParts().size();
return new PartModel(mPartTree, mEmbeddedPartMap);
}
|