Messagelib

multipartalternative.cpp
1/*
2 SPDX-FileCopyrightText: 2016 Sandro Knauß <sknauss@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "multipartalternative.h"
8
9#include "messagepart.h"
10#include "objecttreeparser.h"
11
12#include <KMime/Content>
13
14using namespace MimeTreeParser;
15
16const MultiPartAlternativeBodyPartFormatter *MultiPartAlternativeBodyPartFormatter::self;
17
18const Interface::BodyPartFormatter *MultiPartAlternativeBodyPartFormatter::create()
19{
20 if (!self) {
21 self = new MultiPartAlternativeBodyPartFormatter();
22 }
23 return self;
24}
25
26MessagePart::Ptr MultiPartAlternativeBodyPartFormatter::process(Interface::BodyPart &part) const
27{
28 KMime::Content *node = part.content();
29 if (node->contents().isEmpty()) {
30 return {};
31 }
32
33 auto preferredMode = part.source()->preferredMode();
34 AlternativeMessagePart::Ptr mp(new AlternativeMessagePart(part.objectTreeParser(), node, preferredMode));
35 if (mp->childParts().isEmpty()) {
36 MimeMessagePart::Ptr _mp(new MimeMessagePart(part.objectTreeParser(), node->contents().at(0), false));
37 return _mp;
38 }
39
40 KMime::Content *dataIcal = mp->childParts().contains(Util::MultipartIcal) ? mp->childParts()[Util::MultipartIcal]->content() : nullptr;
41 KMime::Content *dataHtml = mp->childParts().contains(Util::MultipartHtml) ? mp->childParts()[Util::MultipartHtml]->content() : nullptr;
42 KMime::Content *dataPlain = mp->childParts().contains(Util::MultipartPlain) ? mp->childParts()[Util::MultipartPlain]->content() : nullptr;
43
44 // Make sure that in default ical is preferred over html and plain text
45 if (dataIcal && ((preferredMode != Util::MultipartHtml && preferredMode != Util::MultipartPlain))) {
46 if (dataHtml) {
47 part.nodeHelper()->setNodeProcessed(dataHtml, false);
48 }
49 if (dataPlain) {
50 part.nodeHelper()->setNodeProcessed(dataPlain, false);
51 }
52 preferredMode = Util::MultipartIcal;
53 } else if ((dataHtml && (preferredMode == Util::MultipartHtml || preferredMode == Util::Html)) || (dataHtml && dataPlain && dataPlain->body().isEmpty())) {
54 if (dataPlain) {
55 part.nodeHelper()->setNodeProcessed(dataPlain, false);
56 }
57 preferredMode = Util::MultipartHtml;
58 } else if (!(preferredMode == Util::MultipartHtml) && dataPlain) {
59 part.nodeHelper()->setNodeProcessed(dataHtml, false);
60 preferredMode = Util::MultipartPlain;
61 }
62
63 // qDebug() << " MessagePart::Ptr MultiPartAlternativeBodyPartFormatter::process(Interface::BodyPart &part) const";
64 // for (int i = 0; i < mp->availableModes().count(); ++i) {
65 // qDebug() << "MultiPartAlternativeBodyPartFormatter::processed Modes " << MimeTreeParser::Util::htmlModeToString(mp->availableModes().at(i));
66 // }
67 // qDebug() << "MultiPartAlternativeBodyPartFormatter::process preferred " << MimeTreeParser::Util::htmlModeToString(preferredMode);
68
69 part.source()->setHtmlMode(preferredMode, mp->availableModes());
70 mp->setPreferredMode(preferredMode);
71 return mp;
72}
Content * content(const ContentIndex &index) const
QByteArray body() const
QList< Content * > contents()
interface of message body parts.
Definition bodypart.h:45
virtual MimeTreeParser::ObjectTreeParser * objectTreeParser() const =0
For making it easier to refactor, add objectTreeParser.
virtual MimeTreeParser::NodeHelper * nodeHelper() const =0
Ok, this is ugly, exposing the node helper here, but there is too much useful stuff in there for real...
virtual KMime::Content * content() const =0
Returns the KMime::Content node represented here.
virtual MimeTreeParser::Util::HtmlMode preferredMode() const =0
Return the mode that is the preferred to display.
virtual void setHtmlMode(MimeTreeParser::Util::HtmlMode mode, const QList< MimeTreeParser::Util::HtmlMode > &availableModes)=0
Sets the type of mail that is currently displayed.
@ Html
A HTML message, non-multipart.
@ MultipartPlain
A multipart/alternative message, the plain text part is currently displayed.
@ MultipartIcal
A multipart/alternative message, the ICal part is currently displayed.
@ MultipartHtml
A multipart/alternative message, the HTML part is currently displayed.
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:33:26 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.