Messagelib

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

KDE's Doxygen guidelines are available online.