File indexing completed on 2024-06-16 05:00:14

0001 /*  -*- c++ -*-
0002     bodypartformatter.cpp
0003 
0004     This file is part of KMail, the KDE mail client.
0005     SPDX-FileCopyrightText: 2003 Marc Mutz <mutz@kde.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include "bodyformatter/applicationpgpencrypted.h"
0011 #include "bodyformatter/applicationpkcs7mime.h"
0012 #include "bodyformatter/encrypted.h"
0013 #include "bodyformatter/mailman.h"
0014 #include "bodyformatter/multipartalternative.h"
0015 #include "bodyformatter/multipartencrypted.h"
0016 #include "bodyformatter/multipartmixed.h"
0017 #include "bodyformatter/multipartsigned.h"
0018 #include "bodyformatter/texthtml.h"
0019 #include "bodyformatter/textplain.h"
0020 
0021 #include "interfaces/bodypart.h"
0022 #include "interfaces/bodypartformatter.h"
0023 
0024 #include "bodypartformatterfactory_p.h"
0025 
0026 #include "messagepart.h"
0027 #include "objecttreeparser.h"
0028 
0029 #include <KMime/Content>
0030 
0031 using namespace MimeTreeParser;
0032 
0033 namespace
0034 {
0035 class AnyTypeBodyPartFormatter : public MimeTreeParser::Interface::BodyPartFormatter
0036 {
0037     static const AnyTypeBodyPartFormatter *self;
0038 
0039 public:
0040     MessagePart::Ptr process(Interface::BodyPart &part) const override
0041     {
0042         KMime::Content *node = part.content();
0043         const auto mp = AttachmentMessagePart::Ptr(new AttachmentMessagePart(part.objectTreeParser(), node, part.source()->decryptMessage()));
0044         part.processResult()->setInlineSignatureState(mp->signatureState());
0045         part.processResult()->setInlineEncryptionState(mp->encryptionState());
0046         part.processResult()->setNeverDisplayInline(true);
0047         mp->setNeverDisplayInline(true);
0048         mp->setIsImage(false);
0049         return mp;
0050     }
0051 
0052     static const MimeTreeParser::Interface::BodyPartFormatter *create()
0053     {
0054         if (!self) {
0055             self = new AnyTypeBodyPartFormatter();
0056         }
0057         return self;
0058     }
0059 };
0060 
0061 const AnyTypeBodyPartFormatter *AnyTypeBodyPartFormatter::self = nullptr;
0062 
0063 class ImageTypeBodyPartFormatter : public MimeTreeParser::Interface::BodyPartFormatter
0064 {
0065     static const ImageTypeBodyPartFormatter *self;
0066 
0067 public:
0068     static const MimeTreeParser::Interface::BodyPartFormatter *create()
0069     {
0070         if (!self) {
0071             self = new ImageTypeBodyPartFormatter();
0072         }
0073         return self;
0074     }
0075 
0076     MessagePart::Ptr process(Interface::BodyPart &part) const override
0077     {
0078         KMime::Content *node = part.content();
0079         auto mp = AttachmentMessagePart::Ptr(new AttachmentMessagePart(part.objectTreeParser(), node, part.source()->decryptMessage()));
0080         mp->setIsImage(true);
0081         part.processResult()->setInlineSignatureState(mp->signatureState());
0082         part.processResult()->setInlineEncryptionState(mp->encryptionState());
0083 
0084         auto preferredMode = part.source()->preferredMode();
0085         const bool isHtmlPreferred = (preferredMode == Util::Html) || (preferredMode == Util::MultipartHtml);
0086         if (node->parent() && node->parent()->contentType()->subType() == "related" && isHtmlPreferred) {
0087             part.nodeHelper()->setNodeDisplayedEmbedded(node, true);
0088             part.nodeHelper()->setNodeDisplayedHidden(node, true);
0089             return mp;
0090         }
0091 
0092         return mp;
0093     }
0094 };
0095 
0096 const ImageTypeBodyPartFormatter *ImageTypeBodyPartFormatter::self = nullptr;
0097 
0098 class MessageRfc822BodyPartFormatter : public MimeTreeParser::Interface::BodyPartFormatter
0099 {
0100     static const MessageRfc822BodyPartFormatter *self;
0101 
0102 public:
0103     MessagePart::Ptr process(Interface::BodyPart &) const override;
0104     static const MimeTreeParser::Interface::BodyPartFormatter *create();
0105 };
0106 
0107 const MessageRfc822BodyPartFormatter *MessageRfc822BodyPartFormatter::self;
0108 
0109 const MimeTreeParser::Interface::BodyPartFormatter *MessageRfc822BodyPartFormatter::create()
0110 {
0111     if (!self) {
0112         self = new MessageRfc822BodyPartFormatter();
0113     }
0114     return self;
0115 }
0116 
0117 MessagePart::Ptr MessageRfc822BodyPartFormatter::process(Interface::BodyPart &part) const
0118 {
0119     const KMime::Message::Ptr message = part.content()->bodyAsMessage();
0120     return MessagePart::Ptr(new EncapsulatedRfc822MessagePart(part.objectTreeParser(), part.content(), message));
0121 }
0122 } // anon namespace
0123 
0124 void BodyPartFormatterFactoryPrivate::messageviewer_create_builtin_bodypart_formatters()
0125 {
0126     insert(QStringLiteral("application/pkcs7-mime"), ApplicationPkcs7MimeBodyPartFormatter::create());
0127     insert(QStringLiteral("application/x-pkcs7-mime"), ApplicationPkcs7MimeBodyPartFormatter::create());
0128     insert(QStringLiteral("application/pgp-encrypted"), ApplicationPGPEncryptedBodyPartFormatter::create());
0129 
0130     insert(QStringLiteral("application/octet-stream"), ApplicationPkcs7MimeBodyPartFormatter::create());
0131     insert(QStringLiteral("application/octet-stream"), EncryptedBodyPartFormatter::create(EncryptedBodyPartFormatter::AutoPGP));
0132     insert(QStringLiteral("application/octet-stream"), AnyTypeBodyPartFormatter::create());
0133 
0134     insert(QStringLiteral("text/pgp"), EncryptedBodyPartFormatter::create(EncryptedBodyPartFormatter::ForcePGP));
0135     insert(QStringLiteral("text/html"), TextHtmlBodyPartFormatter::create());
0136     insert(QStringLiteral("text/rtf"), AnyTypeBodyPartFormatter::create());
0137     insert(QStringLiteral("text/plain"), MailmanBodyPartFormatter::create());
0138     insert(QStringLiteral("text/plain"), TextPlainBodyPartFormatter::create());
0139 
0140     insert(QStringLiteral("image/png"), ImageTypeBodyPartFormatter::create());
0141     insert(QStringLiteral("image/jpeg"), ImageTypeBodyPartFormatter::create());
0142     insert(QStringLiteral("image/gif"), ImageTypeBodyPartFormatter::create());
0143     insert(QStringLiteral("image/svg+xml"), ImageTypeBodyPartFormatter::create());
0144     insert(QStringLiteral("image/bmp"), ImageTypeBodyPartFormatter::create());
0145     insert(QStringLiteral("image/vnd.microsoft.icon"), ImageTypeBodyPartFormatter::create());
0146 
0147     insert(QStringLiteral("message/rfc822"), MessageRfc822BodyPartFormatter::create());
0148 
0149     insert(QStringLiteral("multipart/alternative"), MultiPartAlternativeBodyPartFormatter::create());
0150     insert(QStringLiteral("multipart/encrypted"), MultiPartEncryptedBodyPartFormatter::create());
0151     insert(QStringLiteral("multipart/signed"), MultiPartSignedBodyPartFormatter::create());
0152     insert(QStringLiteral("multipart/mixed"), MultiPartMixedBodyPartFormatter::create());
0153 }