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
|
/*
Copyright (c) 2016 Sandro Knauß <knauss@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 "partmodel.h"
#include <mimetreeparser/objecttreeparser.h>
#include "htmlutils.h"
#include <QDebug>
#include <QMimeDatabase>
#include <QTextDocument>
class PartModelPrivate
{
public:
PartModelPrivate(PartModel *q_ptr, const std::shared_ptr<MimeTreeParser::ObjectTreeParser> &parser);
~PartModelPrivate();
void createTree();
PartModel *q;
QVector<MimeTreeParser::MessagePartPtr> mParts;
std::shared_ptr<MimeTreeParser::ObjectTreeParser> mParser;
};
PartModelPrivate::PartModelPrivate(PartModel *q_ptr, const std::shared_ptr<MimeTreeParser::ObjectTreeParser> &parser)
: q(q_ptr)
, mParser(parser)
{
mParts = mParser->collectContentParts();
qWarning() << "Collected content parts: " << mParts.size();
}
PartModelPrivate::~PartModelPrivate()
{
}
PartModel::PartModel(std::shared_ptr<MimeTreeParser::ObjectTreeParser> parser)
: d(std::unique_ptr<PartModelPrivate>(new PartModelPrivate(this, parser)))
{
}
PartModel::~PartModel()
{
}
QHash<int, QByteArray> PartModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[TypeRole] = "type";
roles[ContentRole] = "content";
roles[IsEmbededRole] = "embeded";
roles[IsEncryptedRole] = "encrypted";
roles[IsSignedRole] = "signed";
roles[SecurityLevelRole] = "securityLevel";
roles[EncryptionErrorType] = "errorType";
roles[EncryptionErrorString] = "errorString";
roles[IsErrorRole] = "error";
return roles;
}
QModelIndex PartModel::index(int row, int column, const QModelIndex &parent) const
{
if (row < 0 || column != 0) {
return QModelIndex();
}
if (row < d->mParts.size()) {
return createIndex(row, column, d->mParts.at(row).data());
}
return QModelIndex();
}
QVariant PartModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) {
switch (role) {
case Qt::DisplayRole:
return QString("root");
case IsEmbededRole:
return false;
}
return QVariant();
}
if (index.internalPointer()) {
const auto messagePart = static_cast<MimeTreeParser::MessagePart*>(index.internalPointer());
qWarning() << "Found message part " << messagePart->metaObject()->className() << messagePart->partMetaData()->status << messagePart->error();
Q_ASSERT(messagePart);
switch(role) {
case Qt::DisplayRole:
return QStringLiteral("Content%1");
case TypeRole: {
if (messagePart->error()) {
return "error";
}
//For simple html we don't need a browser
auto complexHtml = [&] {
if (messagePart->isHtml()) {
const auto text = messagePart->text();
if (text.contains("<!DOCTYPE html PUBLIC")) {
return true;
}
//Media queries are too advanced
if (text.contains("@media")) {
return true;
}
if (text.contains("<style")) {
return true;
}
return false;
} else {
return false;
}
}();
if (complexHtml) {
return "html";
}
return "plain";
}
case IsEmbededRole:
return false;
case IsErrorRole:
return messagePart->error();
case ContentRole: {
auto errorText = messagePart->partMetaData()->errorText;
if (!errorText.isEmpty()) {
return errorText;
}
const auto text = messagePart->isHtml() ? messagePart->htmlContent() : messagePart->text();
if (messagePart->isHtml()) {
return d->mParser->resolveCidLinks(text);
} else { //We assume plain
//We alwas do richtext (so we get highlighted links and stuff).
return HtmlUtils::linkify(Qt::convertFromPlainText(text));
}
return text;
}
case IsEncryptedRole:
return messagePart->encryptionState() != MimeTreeParser::KMMsgNotEncrypted;
case IsSignedRole:
return messagePart->signatureState() != MimeTreeParser::KMMsgNotSigned;
case EncryptionErrorType:
return messagePart->error();
case EncryptionErrorString:
return messagePart->errorString();
}
}
return QVariant();
}
QModelIndex PartModel::parent(const QModelIndex &index) const
{
return QModelIndex();
}
int PartModel::rowCount(const QModelIndex &parent) const
{
return d->mParts.count();
}
int PartModel::columnCount(const QModelIndex &parent) const
{
return 1;
}
|