diff options
Diffstat (limited to 'framework/src/domain/mime/mimetreeparser/multipartalternative.cpp')
-rw-r--r-- | framework/src/domain/mime/mimetreeparser/multipartalternative.cpp | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/framework/src/domain/mime/mimetreeparser/multipartalternative.cpp b/framework/src/domain/mime/mimetreeparser/multipartalternative.cpp new file mode 100644 index 00000000..c2a6f270 --- /dev/null +++ b/framework/src/domain/mime/mimetreeparser/multipartalternative.cpp | |||
@@ -0,0 +1,82 @@ | |||
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 | |||
31 | using namespace MimeTreeParser; | ||
32 | |||
33 | const MultiPartAlternativeBodyPartFormatter *MultiPartAlternativeBodyPartFormatter::self; | ||
34 | |||
35 | const Interface::BodyPartFormatter *MultiPartAlternativeBodyPartFormatter::create() | ||
36 | { | ||
37 | if (!self) { | ||
38 | self = new MultiPartAlternativeBodyPartFormatter(); | ||
39 | } | ||
40 | return self; | ||
41 | } | ||
42 | |||
43 | MessagePart::Ptr MultiPartAlternativeBodyPartFormatter::process(Interface::BodyPart &part) const | ||
44 | { | ||
45 | KMime::Content *node = part.content(); | ||
46 | if (node->contents().isEmpty()) { | ||
47 | return MessagePart::Ptr(); | ||
48 | } | ||
49 | |||
50 | //Hardcoded after removing the source | ||
51 | auto preferredMode = MimeTreeParser::Util::Html; | ||
52 | AlternativeMessagePart::Ptr mp(new AlternativeMessagePart(part.objectTreeParser(), node, preferredMode)); | ||
53 | if (mp->mChildNodes.isEmpty()) { | ||
54 | return MimeMessagePart::Ptr(new MimeMessagePart(part.objectTreeParser(), node->contents().at(0), false)); | ||
55 | } | ||
56 | |||
57 | KMime::Content *dataIcal = mp->mChildNodes.contains(Util::MultipartIcal) ? mp->mChildNodes[Util::MultipartIcal] : nullptr; | ||
58 | KMime::Content *dataHtml = mp->mChildNodes.contains(Util::MultipartHtml) ? mp->mChildNodes[Util::MultipartHtml] : nullptr; | ||
59 | KMime::Content *dataPlain = mp->mChildNodes.contains(Util::MultipartPlain) ? mp->mChildNodes[Util::MultipartPlain] : nullptr; | ||
60 | |||
61 | // Make sure that in default ical is prefered over html and plain text | ||
62 | if (dataIcal && ((preferredMode != Util::MultipartHtml && preferredMode != Util::MultipartPlain))) { | ||
63 | if (dataHtml) { | ||
64 | part.nodeHelper()->setNodeProcessed(dataHtml, false); | ||
65 | } | ||
66 | if (dataPlain) { | ||
67 | part.nodeHelper()->setNodeProcessed(dataPlain, false); | ||
68 | } | ||
69 | preferredMode = Util::MultipartIcal; | ||
70 | } else if ((dataHtml && (preferredMode == Util::MultipartHtml || preferredMode == Util::Html)) || | ||
71 | (dataHtml && dataPlain && dataPlain->body().isEmpty())) { | ||
72 | if (dataPlain) { | ||
73 | part.nodeHelper()->setNodeProcessed(dataPlain, false); | ||
74 | } | ||
75 | preferredMode = Util::MultipartHtml; | ||
76 | } else if (!(preferredMode == Util::MultipartHtml) && dataPlain) { | ||
77 | part.nodeHelper()->setNodeProcessed(dataHtml, false); | ||
78 | preferredMode = Util::MultipartPlain; | ||
79 | } | ||
80 | mp->mPreferredMode = preferredMode; | ||
81 | return mp; | ||
82 | } | ||