summaryrefslogtreecommitdiffstats
path: root/framework/src/domain/mimetreeparser/otp/multipartalternative.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'framework/src/domain/mimetreeparser/otp/multipartalternative.cpp')
-rw-r--r--framework/src/domain/mimetreeparser/otp/multipartalternative.cpp94
1 files changed, 94 insertions, 0 deletions
diff --git a/framework/src/domain/mimetreeparser/otp/multipartalternative.cpp b/framework/src/domain/mimetreeparser/otp/multipartalternative.cpp
new file mode 100644
index 00000000..42c70e28
--- /dev/null
+++ b/framework/src/domain/mimetreeparser/otp/multipartalternative.cpp
@@ -0,0 +1,94 @@
1/*
2 Copyright (c) 2016 Sandro Knauß <sknauss@kde.org>
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
20#include "multipartalternative.h"
21
22#include "utils.h"
23
24#include "objecttreeparser.h"
25#include "messagepart.h"
26
27#include <KMime/Content>
28
29#include "mimetreeparser_debug.h"
30
31using namespace MimeTreeParser;
32
33const MultiPartAlternativeBodyPartFormatter *MultiPartAlternativeBodyPartFormatter::self;
34
35const Interface::BodyPartFormatter *MultiPartAlternativeBodyPartFormatter::create()
36{
37 if (!self) {
38 self = new MultiPartAlternativeBodyPartFormatter();
39 }
40 return self;
41}
42Interface::BodyPartFormatter::Result MultiPartAlternativeBodyPartFormatter::format(Interface::BodyPart *part, HtmlWriter *writer) const
43{
44 Q_UNUSED(writer)
45 const auto p = process(*part);
46 const auto mp = static_cast<MessagePart *>(p.data());
47 if (mp) {
48 mp->html(false);
49 return Ok;
50 }
51 return Failed;
52}
53
54Interface::MessagePart::Ptr MultiPartAlternativeBodyPartFormatter::process(Interface::BodyPart &part) const
55{
56 KMime::Content *node = part.content();
57 if (node->contents().isEmpty()) {
58 return MessagePart::Ptr();
59 }
60
61 auto preferredMode = part.source()->preferredMode();
62 AlternativeMessagePart::Ptr mp(new AlternativeMessagePart(part.objectTreeParser(), node, preferredMode));
63 if (mp->mChildNodes.isEmpty()) {
64 MimeMessagePart::Ptr _mp(new MimeMessagePart(part.objectTreeParser(), node->contents().at(0), false));
65 return _mp;
66 }
67
68 KMime::Content *dataIcal = mp->mChildNodes.contains(Util::MultipartIcal) ? mp->mChildNodes[Util::MultipartIcal] : nullptr;
69 KMime::Content *dataHtml = mp->mChildNodes.contains(Util::MultipartHtml) ? mp->mChildNodes[Util::MultipartHtml] : nullptr;
70 KMime::Content *dataPlain = mp->mChildNodes.contains(Util::MultipartPlain) ? mp->mChildNodes[Util::MultipartPlain] : nullptr;
71
72 // Make sure that in default ical is prefered over html and plain text
73 if (dataIcal && ((preferredMode != Util::MultipartHtml && preferredMode != Util::MultipartPlain))) {
74 if (dataHtml) {
75 part.nodeHelper()->setNodeProcessed(dataHtml, false);
76 }
77 if (dataPlain) {
78 part.nodeHelper()->setNodeProcessed(dataPlain, false);
79 }
80 preferredMode = Util::MultipartIcal;
81 } else if ((dataHtml && (preferredMode == Util::MultipartHtml || preferredMode == Util::Html)) ||
82 (dataHtml && dataPlain && dataPlain->body().isEmpty())) {
83 if (dataPlain) {
84 part.nodeHelper()->setNodeProcessed(dataPlain, false);
85 }
86 preferredMode = Util::MultipartHtml;
87 } else if (!(preferredMode == Util::MultipartHtml) && dataPlain) {
88 part.nodeHelper()->setNodeProcessed(dataHtml, false);
89 preferredMode = Util::MultipartPlain;
90 }
91 part.source()->setHtmlMode(preferredMode, mp->availableModes());
92 mp->mPreferredMode = preferredMode;
93 return mp;
94}