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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
|
/*
objecttreeparser.cpp
This file is part of KMail, the KDE mail client.
Copyright (c) 2003 Marc Mutz <mutz@kde.org>
Copyright (C) 2002-2004 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.net
Copyright (c) 2009 Andras Mantia <andras@kdab.net>
Copyright (c) 2015 Sandro Knauß <sknauss@kde.org>
KMail is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
KMail 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
In addition, as a special exception, the copyright holders give
permission to link the code of this program with any edition of
the Qt library by Trolltech AS, Norway (or with modified versions
of Qt that use the same license as Qt), and distribute linked
combinations including the two. You must obey the GNU General
Public License in all respects for all of the code used other than
Qt. If you modify this file, you may extend this exception to
your version of the file, but you are not obligated to do so. If
you do not wish to do so, delete this exception statement from
your version.
*/
// MessageViewer includes
#include "objecttreeparser.h"
#include "attachmentstrategy.h"
#include "bodypartformatterbasefactory.h"
#include "nodehelper.h"
#include "messagepart.h"
#include "partnodebodypart.h"
#include "mimetreeparser_debug.h"
#include "utils.h"
#include "bodypartformatter.h"
#include "htmlwriter.h"
#include "messagepartrenderer.h"
#include "util.h"
#include <KMime/Headers>
#include <KMime/Message>
// KDE includes
// Qt includes
#include <QByteArray>
#include <QTextCodec>
#include <QUrl>
using namespace MimeTreeParser;
ObjectTreeParser::ObjectTreeParser(const ObjectTreeParser *topLevelParser,
bool showOnlyOneMimePart,
const AttachmentStrategy *strategy)
: mSource(topLevelParser->mSource),
mNodeHelper(topLevelParser->mNodeHelper),
mHtmlWriter(topLevelParser->mHtmlWriter),
mTopLevelContent(topLevelParser->mTopLevelContent),
mShowOnlyOneMimePart(showOnlyOneMimePart),
mHasPendingAsyncJobs(false),
mAllowAsync(topLevelParser->mAllowAsync),
mAttachmentStrategy(strategy)
{
init();
}
ObjectTreeParser::ObjectTreeParser(Interface::ObjectTreeSource *source,
MimeTreeParser::NodeHelper *nodeHelper,
bool showOnlyOneMimePart,
const AttachmentStrategy *strategy)
: mSource(source),
mNodeHelper(nodeHelper),
mHtmlWriter(nullptr),
mTopLevelContent(nullptr),
mShowOnlyOneMimePart(showOnlyOneMimePart),
mHasPendingAsyncJobs(false),
mAllowAsync(false),
mAttachmentStrategy(strategy)
{
init();
}
void ObjectTreeParser::init()
{
Q_ASSERT(mSource);
if (!attachmentStrategy()) {
mAttachmentStrategy = mSource->attachmentStrategy();
}
if (!mNodeHelper) {
mNodeHelper = new NodeHelper();
mDeleteNodeHelper = true;
} else {
mDeleteNodeHelper = false;
}
}
ObjectTreeParser::ObjectTreeParser(const ObjectTreeParser &other)
: mSource(other.mSource),
mNodeHelper(other.nodeHelper()), //TODO(Andras) hm, review what happens if mDeleteNodeHelper was true in the source
mHtmlWriter(other.mHtmlWriter),
mTopLevelContent(other.mTopLevelContent),
mShowOnlyOneMimePart(other.showOnlyOneMimePart()),
mHasPendingAsyncJobs(other.hasPendingAsyncJobs()),
mAllowAsync(other.allowAsync()),
mAttachmentStrategy(other.attachmentStrategy()),
mDeleteNodeHelper(false)
{
}
ObjectTreeParser::~ObjectTreeParser()
{
if (mDeleteNodeHelper) {
delete mNodeHelper;
mNodeHelper = nullptr;
}
}
void ObjectTreeParser::setAllowAsync(bool allow)
{
Q_ASSERT(!mHasPendingAsyncJobs);
mAllowAsync = allow;
}
bool ObjectTreeParser::allowAsync() const
{
return mAllowAsync;
}
bool ObjectTreeParser::hasPendingAsyncJobs() const
{
return mHasPendingAsyncJobs;
}
QString ObjectTreeParser::plainTextContent() const
{
return mPlainTextContent;
}
QString ObjectTreeParser::htmlContent() const
{
return mHtmlContent;
}
void ObjectTreeParser::copyContentFrom(const ObjectTreeParser *other)
{
mPlainTextContent += other->plainTextContent();
mHtmlContent += other->htmlContent();
if (!other->plainTextContentCharset().isEmpty()) {
mPlainTextContentCharset = other->plainTextContentCharset();
}
if (!other->htmlContentCharset().isEmpty()) {
mHtmlContentCharset = other->htmlContentCharset();
}
}
//-----------------------------------------------------------------------------
void ObjectTreeParser::parseObjectTree(KMime::Content *node)
{
mTopLevelContent = node;
mParsedPart = parseObjectTreeInternal(node, showOnlyOneMimePart());
if (mParsedPart) {
mParsedPart->fix();
mParsedPart->copyContentFrom();
if (auto mp = toplevelTextNode(mParsedPart)) {
if (auto _mp = mp.dynamicCast<TextMessagePart>()) {
extractNodeInfos(_mp->mNode, true);
} else if (auto _mp = mp.dynamicCast<AlternativeMessagePart>()) {
if (_mp->mChildNodes.contains(Util::MultipartPlain)) {
extractNodeInfos(_mp->mChildNodes[Util::MultipartPlain], true);
}
}
setPlainTextContent(mp->text());
}
if (htmlWriter()) {
const auto renderer = mSource->messagePartTheme(mParsedPart);
if (renderer) {
mHtmlWriter->queue(renderer->html());
}
}
}
}
MessagePartPtr ObjectTreeParser::parsedPart() const
{
return mParsedPart;
}
bool ObjectTreeParser::processType(KMime::Content *node, ProcessResult &processResult, const QByteArray &mediaType, const QByteArray &subType, Interface::MessagePartPtr &mpRet, bool onlyOneMimePart)
{
bool bRendered = false;
const auto sub = mSource->bodyPartFormatterFactory()->subtypeRegistry(mediaType.constData());
auto range = sub.equal_range(subType.constData());
for (auto it = range.first; it != range.second; ++it) {
const auto formatter = (*it).second;
if (!formatter) {
continue;
}
PartNodeBodyPart part(this, &processResult, mTopLevelContent, node, mNodeHelper);
// Set the default display strategy for this body part relying on the
// identity of Interface::BodyPart::Display and AttachmentStrategy::Display
part.setDefaultDisplay((Interface::BodyPart::Display) attachmentStrategy()->defaultDisplay(node));
mNodeHelper->setNodeDisplayedEmbedded(node, true);
const Interface::MessagePart::Ptr result = formatter->process(part);
if (!result) {
continue;
}
if (const auto mp = result.dynamicCast<MessagePart>()) {
mp->setAttachmentFlag(node);
mpRet = result;
bRendered = true;
break;
} else if (dynamic_cast<MimeTreeParser::Interface::MessagePart *>(result.data())) {
QObject *asyncResultObserver = allowAsync() ? mSource->sourceObject() : nullptr;
const auto r = formatter->format(&part, result->htmlWriter(), asyncResultObserver);
if (r == Interface::BodyPartFormatter::AsIcon) {
processResult.setNeverDisplayInline(true);
formatter->adaptProcessResult(processResult);
mNodeHelper->setNodeDisplayedEmbedded(node, false);
const Interface::MessagePart::Ptr mp = defaultHandling(node, processResult, onlyOneMimePart);
if (mp) {
if (auto _mp = mp.dynamicCast<MessagePart>()) {
_mp->setAttachmentFlag(node);
}
mpRet = mp;
}
bRendered = true;
break;
} else if (r == Interface::BodyPartFormatter::Ok) {
processResult.setNeverDisplayInline(true);
formatter->adaptProcessResult(processResult);
mpRet = result;
bRendered = true;
break;
}
continue;
} else {
continue;
}
}
return bRendered;
}
MessagePart::Ptr ObjectTreeParser::parseObjectTreeInternal(KMime::Content *node, bool onlyOneMimePart)
{
if (!node) {
return MessagePart::Ptr();
}
// reset pending async jobs state (we'll rediscover pending jobs as we go)
mHasPendingAsyncJobs = false;
// reset "processed" flags for...
if (onlyOneMimePart) {
// ... this node and all descendants
mNodeHelper->setNodeUnprocessed(node, false);
if (!node->contents().isEmpty()) {
mNodeHelper->setNodeUnprocessed(node, true);
}
} else if (!node->parent()) {
// ...this node and all it's siblings and descendants
mNodeHelper->setNodeUnprocessed(node, true);
}
const bool isRoot = node->isTopLevel();
auto parsedPart = MessagePart::Ptr(new MessagePartList(this));
parsedPart->setIsRoot(isRoot);
KMime::Content *parent = node->parent();
auto contents = parent ? parent->contents() : KMime::Content::List();
if (contents.isEmpty()) {
contents.append(node);
}
int i = contents.indexOf(const_cast<KMime::Content *>(node));
for (; i < contents.size(); ++i) {
node = contents.at(i);
if (mNodeHelper->nodeProcessed(node)) {
continue;
}
ProcessResult processResult(mNodeHelper);
QByteArray mediaType("text");
QByteArray subType("plain");
if (node->contentType(false) && !node->contentType()->mediaType().isEmpty() &&
!node->contentType()->subType().isEmpty()) {
mediaType = node->contentType()->mediaType();
subType = node->contentType()->subType();
}
Interface::MessagePartPtr mp;
if (processType(node, processResult, mediaType, subType, mp, onlyOneMimePart)) {
if (mp) {
parsedPart->appendSubPart(mp);
}
} else if (processType(node, processResult, mediaType, "*", mp, onlyOneMimePart)) {
if (mp) {
parsedPart->appendSubPart(mp);
}
} else {
qCWarning(MIMETREEPARSER_LOG) << "THIS SHOULD NO LONGER HAPPEN:" << mediaType << '/' << subType;
const auto mp = defaultHandling(node, processResult, onlyOneMimePart);
if (mp) {
if (auto _mp = mp.dynamicCast<MessagePart>()) {
_mp->setAttachmentFlag(node);
}
parsedPart->appendSubPart(mp);
}
}
mNodeHelper->setNodeProcessed(node, false);
// adjust signed/encrypted flags if inline PGP was found
processResult.adjustCryptoStatesOfNode(node);
if (onlyOneMimePart) {
break;
}
}
return parsedPart;
}
Interface::MessagePart::Ptr ObjectTreeParser::defaultHandling(KMime::Content *node, ProcessResult &result, bool onlyOneMimePart)
{
Interface::MessagePart::Ptr mp;
ProcessResult processResult(mNodeHelper);
if (node->contentType()->mimeType() == QByteArrayLiteral("application/octet-stream") &&
(node->contentType()->name().endsWith(QLatin1String("p7m")) ||
node->contentType()->name().endsWith(QLatin1String("p7s")) ||
node->contentType()->name().endsWith(QLatin1String("p7c"))
) &&
processType(node, processResult, "application", "pkcs7-mime", mp, onlyOneMimePart)) {
return mp;
}
const auto _mp = AttachmentMessagePart::Ptr(new AttachmentMessagePart(this, node, false, true, mSource->decryptMessage()));
result.setInlineSignatureState(_mp->signatureState());
result.setInlineEncryptionState(_mp->encryptionState());
_mp->setNeverDisplayInline(result.neverDisplayInline());
_mp->setIsImage(result.isImage());
mp = _mp;
// always show images in multipart/related when showing in html, not with an additional icon
auto preferredMode = mSource->preferredMode();
bool isHtmlPreferred = (preferredMode == Util::Html) || (preferredMode == Util::MultipartHtml);
if (result.isImage() && node->parent() &&
node->parent()->contentType()->subType() == "related" && isHtmlPreferred && !onlyOneMimePart) {
QString fileName = mNodeHelper->writeNodeToTempFile(node);
QString href = QUrl::fromLocalFile(fileName).url();
QByteArray cid = node->contentID()->identifier();
if (htmlWriter()) {
htmlWriter()->embedPart(cid, href);
}
nodeHelper()->setNodeDisplayedEmbedded(node, true);
mNodeHelper->setNodeDisplayedHidden(node, true);
return mp;
}
// Show it inline if showOnlyOneMimePart(), which means the user clicked the image
// in the message structure viewer manually, and therefore wants to see the full image
if (result.isImage() && onlyOneMimePart && !result.neverDisplayInline()) {
mNodeHelper->setNodeDisplayedEmbedded(node, true);
}
return mp;
}
KMMsgSignatureState ProcessResult::inlineSignatureState() const
{
return mInlineSignatureState;
}
void ProcessResult::setInlineSignatureState(KMMsgSignatureState state)
{
mInlineSignatureState = state;
}
KMMsgEncryptionState ProcessResult::inlineEncryptionState() const
{
return mInlineEncryptionState;
}
void ProcessResult::setInlineEncryptionState(KMMsgEncryptionState state)
{
mInlineEncryptionState = state;
}
bool ProcessResult::neverDisplayInline() const
{
return mNeverDisplayInline;
}
void ProcessResult::setNeverDisplayInline(bool display)
{
mNeverDisplayInline = display;
}
bool ProcessResult::isImage() const
{
return mIsImage;
}
void ProcessResult::setIsImage(bool image)
{
mIsImage = image;
}
void ProcessResult::adjustCryptoStatesOfNode(const KMime::Content *node) const
{
if ((inlineSignatureState() != KMMsgNotSigned) ||
(inlineEncryptionState() != KMMsgNotEncrypted)) {
mNodeHelper->setSignatureState(node, inlineSignatureState());
mNodeHelper->setEncryptionState(node, inlineEncryptionState());
}
}
void ObjectTreeParser::extractNodeInfos(KMime::Content *curNode, bool isFirstTextPart)
{
if (isFirstTextPart) {
mPlainTextContent += curNode->decodedText();
mPlainTextContentCharset += NodeHelper::charset(curNode);
}
}
void ObjectTreeParser::setPlainTextContent(const QString &plainTextContent)
{
mPlainTextContent = plainTextContent;
}
const QTextCodec *ObjectTreeParser::codecFor(KMime::Content *node) const
{
Q_ASSERT(node);
if (mSource->overrideCodec()) {
return mSource->overrideCodec();
}
return mNodeHelper->codec(node);
}
QByteArray ObjectTreeParser::plainTextContentCharset() const
{
return mPlainTextContentCharset;
}
QByteArray ObjectTreeParser::htmlContentCharset() const
{
return mHtmlContentCharset;
}
bool ObjectTreeParser::showOnlyOneMimePart() const
{
return mShowOnlyOneMimePart;
}
void ObjectTreeParser::setShowOnlyOneMimePart(bool show)
{
mShowOnlyOneMimePart = show;
}
const AttachmentStrategy *ObjectTreeParser::attachmentStrategy() const
{
return mAttachmentStrategy;
}
HtmlWriter *ObjectTreeParser::htmlWriter() const
{
if (mHtmlWriter) {
return mHtmlWriter;
}
return mSource->htmlWriter();
}
MimeTreeParser::NodeHelper *ObjectTreeParser::nodeHelper() const
{
return mNodeHelper;
}
|