Messagelib

bodypartformatter.cpp
1 /* -*- c++ -*-
2  bodypartformatter.cpp
3 
4  This file is part of KMail, the KDE mail client.
5  SPDX-FileCopyrightText: 2003 Marc Mutz <[email protected]>
6 
7  SPDX-License-Identifier: GPL-2.0-or-later
8 */
9 
10 #include "mimetreeparser_debug.h"
11 
12 #include "bodyformatter/applicationpgpencrypted.h"
13 #include "bodyformatter/applicationpkcs7mime.h"
14 #include "bodyformatter/encrypted.h"
15 #include "bodyformatter/mailman.h"
16 #include "bodyformatter/multipartalternative.h"
17 #include "bodyformatter/multipartencrypted.h"
18 #include "bodyformatter/multipartmixed.h"
19 #include "bodyformatter/multipartsigned.h"
20 #include "bodyformatter/texthtml.h"
21 #include "bodyformatter/textplain.h"
22 
23 #include "interfaces/bodypart.h"
24 #include "interfaces/bodypartformatter.h"
25 
26 #include "bodypartformatterfactory.h"
27 #include "bodypartformatterfactory_p.h"
28 
29 #include "messagepart.h"
30 #include "objecttreeparser.h"
31 
32 #include <KMime/Content>
33 
34 using namespace MimeTreeParser;
35 
36 namespace
37 {
38 class AnyTypeBodyPartFormatter : public MimeTreeParser::Interface::BodyPartFormatter
39 {
40  static const AnyTypeBodyPartFormatter *self;
41 
42 public:
43  MessagePart::Ptr process(Interface::BodyPart &part) const override
44  {
45  KMime::Content *node = part.content();
46  const auto mp = AttachmentMessagePart::Ptr(new AttachmentMessagePart(part.objectTreeParser(), node, part.source()->decryptMessage()));
47  part.processResult()->setInlineSignatureState(mp->signatureState());
48  part.processResult()->setInlineEncryptionState(mp->encryptionState());
49  part.processResult()->setNeverDisplayInline(true);
50  mp->setNeverDisplayInline(true);
51  mp->setIsImage(false);
52  return mp;
53  }
54 
56  {
57  if (!self) {
58  self = new AnyTypeBodyPartFormatter();
59  }
60  return self;
61  }
62 };
63 
64 const AnyTypeBodyPartFormatter *AnyTypeBodyPartFormatter::self = nullptr;
65 
66 class ImageTypeBodyPartFormatter : public MimeTreeParser::Interface::BodyPartFormatter
67 {
68  static const ImageTypeBodyPartFormatter *self;
69 
70 public:
72  {
73  if (!self) {
74  self = new ImageTypeBodyPartFormatter();
75  }
76  return self;
77  }
78 
79  MessagePart::Ptr process(Interface::BodyPart &part) const override
80  {
81  KMime::Content *node = part.content();
82  auto mp = AttachmentMessagePart::Ptr(new AttachmentMessagePart(part.objectTreeParser(), node, part.source()->decryptMessage()));
83  mp->setIsImage(true);
84  part.processResult()->setInlineSignatureState(mp->signatureState());
85  part.processResult()->setInlineEncryptionState(mp->encryptionState());
86 
87  auto preferredMode = part.source()->preferredMode();
88  const bool isHtmlPreferred = (preferredMode == Util::Html) || (preferredMode == Util::MultipartHtml);
89  if (node->parent() && node->parent()->contentType()->subType() == "related" && isHtmlPreferred) {
90  part.nodeHelper()->setNodeDisplayedEmbedded(node, true);
91  part.nodeHelper()->setNodeDisplayedHidden(node, true);
92  return mp;
93  }
94 
95  return mp;
96  }
97 };
98 
99 const ImageTypeBodyPartFormatter *ImageTypeBodyPartFormatter::self = nullptr;
100 
101 class MessageRfc822BodyPartFormatter : public MimeTreeParser::Interface::BodyPartFormatter
102 {
103  static const MessageRfc822BodyPartFormatter *self;
104 
105 public:
106  MessagePart::Ptr process(Interface::BodyPart &) const override;
108 };
109 
110 const MessageRfc822BodyPartFormatter *MessageRfc822BodyPartFormatter::self;
111 
112 const MimeTreeParser::Interface::BodyPartFormatter *MessageRfc822BodyPartFormatter::create()
113 {
114  if (!self) {
115  self = new MessageRfc822BodyPartFormatter();
116  }
117  return self;
118 }
119 
120 MessagePart::Ptr MessageRfc822BodyPartFormatter::process(Interface::BodyPart &part) const
121 {
123  return MessagePart::Ptr(new EncapsulatedRfc822MessagePart(part.objectTreeParser(), part.content(), message));
124 }
125 } // anon namespace
126 
127 void BodyPartFormatterFactoryPrivate::messageviewer_create_builtin_bodypart_formatters()
128 {
129  insert(QStringLiteral("application/pkcs7-mime"), ApplicationPkcs7MimeBodyPartFormatter::create());
130  insert(QStringLiteral("application/x-pkcs7-mime"), ApplicationPkcs7MimeBodyPartFormatter::create());
131  insert(QStringLiteral("application/pgp-encrypted"), ApplicationPGPEncryptedBodyPartFormatter::create());
132 
133  insert(QStringLiteral("application/octet-stream"), ApplicationPkcs7MimeBodyPartFormatter::create());
134  insert(QStringLiteral("application/octet-stream"), EncryptedBodyPartFormatter::create());
135  insert(QStringLiteral("application/octet-stream"), AnyTypeBodyPartFormatter::create());
136 
137  insert(QStringLiteral("text/pgp"), EncryptedBodyPartFormatter::create());
138  insert(QStringLiteral("text/html"), TextHtmlBodyPartFormatter::create());
139  insert(QStringLiteral("text/rtf"), AnyTypeBodyPartFormatter::create());
140  insert(QStringLiteral("text/plain"), MailmanBodyPartFormatter::create());
141  insert(QStringLiteral("text/plain"), TextPlainBodyPartFormatter::create());
142 
143  insert(QStringLiteral("image/png"), ImageTypeBodyPartFormatter::create());
144  insert(QStringLiteral("image/jpeg"), ImageTypeBodyPartFormatter::create());
145  insert(QStringLiteral("image/gif"), ImageTypeBodyPartFormatter::create());
146  insert(QStringLiteral("image/svg+xml"), ImageTypeBodyPartFormatter::create());
147  insert(QStringLiteral("image/bmp"), ImageTypeBodyPartFormatter::create());
148  insert(QStringLiteral("image/vnd.microsoft.icon"), ImageTypeBodyPartFormatter::create());
149 
150  insert(QStringLiteral("message/rfc822"), MessageRfc822BodyPartFormatter::create());
151 
152  insert(QStringLiteral("multipart/alternative"), MultiPartAlternativeBodyPartFormatter::create());
153  insert(QStringLiteral("multipart/encrypted"), MultiPartEncryptedBodyPartFormatter::create());
154  insert(QStringLiteral("multipart/signed"), MultiPartSignedBodyPartFormatter::create());
155  insert(QStringLiteral("multipart/mixed"), MultiPartMixedBodyPartFormatter::create());
156 }
KCOREADDONS_EXPORT void message(KMessage::MessageType messageType, const QString &text, const QString &caption=QString())
QByteArray subType() const
QAction * create(StandardAction id, const QObject *recvr, Func slot, QObject *parent)
virtual bool decryptMessage() const =0
Return true if an encrypted mail should be decrypted.
A HTML message, non-multipart.
A multipart/alternative message, the HTML part is currently displayed.
bool insert(Part *part, qint64 *insertId=nullptr)
Headers::ContentType * contentType(bool create=true)
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.
QSharedPointer< Message > bodyAsMessage() const
interface of message body parts.
Definition: bodypart.h:44
Content * parent() const
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.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Dec 4 2021 23:12:52 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.