bodypartformatter.cpp 8.67 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*  -*- c++ -*-
    bodypartformatter.cpp

    This file is part of KMail, the KDE mail client.
    Copyright (c) 2003 Marc Mutz <mutz@kde.org>

    KMail is free software; you can redistribute it and/or modify it
    under the terms of the GNU General Public License, version 2, as
    published by the Free Software Foundation.

    KMail is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

    In addition, as a special exception, the copyright holders give
    permission to link the code of this program with any edition of
    the Qt library by Trolltech AS, Norway (or with modified versions
    of Qt that use the same license as Qt), and distribute linked
    combinations including the two.  You must obey the GNU General
    Public License in all respects for all of the code used other than
    Qt.  If you modify this file, you may extend this exception to
    your version of the file, but you are not obligated to do so.  If
    you do not wish to do so, delete this exception statement from
    your version.
*/

32
#include "mimetreeparser_debug.h"
33

34
#include "bodyformatter/applicationpgpencrypted.h"
35 36
#include "bodyformatter/mailman.h"

37 38 39
#include "interfaces/bodypartformatter.h"
#include "interfaces/bodypart.h"

Sandro Knauß's avatar
Sandro Knauß committed
40 41 42 43
#include "viewer/bodypartformatterbasefactory.h"
#include "viewer/bodypartformatterbasefactory_p.h"

#include "viewer/attachmentstrategy.h"
44
#include "viewer/objecttreeparser.h"
Sandro Knauß's avatar
Sandro Knauß committed
45
#include "viewer/messagepart.h"
46

47
#include <KMime/Content>
48

49
using namespace MimeTreeParser;
50 51 52 53

namespace
{
class AnyTypeBodyPartFormatter
54
    : public MimeTreeParser::Interface::BodyPartFormatter
55 56 57
{
    static const AnyTypeBodyPartFormatter *self;
public:
58
    Result format(Interface::BodyPart *, HtmlWriter *) const Q_DECL_OVERRIDE
59
    {
60
        qCDebug(MIMETREEPARSER_LOG) << "Acting as a Interface::BodyPartFormatter!";
61 62 63 64
        return AsIcon;
    }

    // unhide the overload with three arguments
65
    using MimeTreeParser::Interface::BodyPartFormatter::format;
66

67
    void adaptProcessResult(ProcessResult &result) const Q_DECL_OVERRIDE
68 69 70
    {
        result.setNeverDisplayInline(true);
    }
71
    static const MimeTreeParser::Interface::BodyPartFormatter *create()
72 73 74 75 76 77 78 79 80 81 82
    {
        if (!self) {
            self = new AnyTypeBodyPartFormatter();
        }
        return self;
    }
};

const AnyTypeBodyPartFormatter *AnyTypeBodyPartFormatter::self = 0;

class ImageTypeBodyPartFormatter
83
    : public MimeTreeParser::Interface::BodyPartFormatter
84 85 86
{
    static const ImageTypeBodyPartFormatter *self;
public:
87
    Result format(Interface::BodyPart *, HtmlWriter *) const Q_DECL_OVERRIDE
88 89 90 91 92
    {
        return AsIcon;
    }

    // unhide the overload with three arguments
93
    using MimeTreeParser::Interface::BodyPartFormatter::format;
94

95
    void adaptProcessResult(ProcessResult &result) const Q_DECL_OVERRIDE
96
    {
Laurent Montel's avatar
Laurent Montel committed
97
        result.setNeverDisplayInline(false);
98 99
        result.setIsImage(true);
    }
100
    static const MimeTreeParser::Interface::BodyPartFormatter *create()
101 102 103 104 105 106 107 108 109 110 111
    {
        if (!self) {
            self = new ImageTypeBodyPartFormatter();
        }
        return self;
    }
};

const ImageTypeBodyPartFormatter *ImageTypeBodyPartFormatter::self = 0;

class MessageRfc822BodyPartFormatter
112
    : public MimeTreeParser::Interface::BodyPartFormatter
113 114 115
{
    static const MessageRfc822BodyPartFormatter *self;
public:
116
    Interface::MessagePart::Ptr process(Interface::BodyPart &) const Q_DECL_OVERRIDE;
117 118 119
    MimeTreeParser::Interface::BodyPartFormatter::Result format(Interface::BodyPart *, HtmlWriter *) const Q_DECL_OVERRIDE;
    using MimeTreeParser::Interface::BodyPartFormatter::format;
    static const MimeTreeParser::Interface::BodyPartFormatter *create();
120 121 122 123
};

const MessageRfc822BodyPartFormatter *MessageRfc822BodyPartFormatter::self;

124
const MimeTreeParser::Interface::BodyPartFormatter *MessageRfc822BodyPartFormatter::create()
125 126 127 128 129 130 131
{
    if (!self) {
        self = new MessageRfc822BodyPartFormatter();
    }
    return self;
}

132
Interface::MessagePart::Ptr MessageRfc822BodyPartFormatter::process(Interface::BodyPart &part) const
133
{
134 135
    const KMime::Message::Ptr message = part.content()->bodyAsMessage();
    return MessagePart::Ptr(new EncapsulatedRfc822MessagePart(part.objectTreeParser(), part.content(), message));
136 137
}

138
Interface::BodyPartFormatter::Result MessageRfc822BodyPartFormatter::format(Interface::BodyPart *part, HtmlWriter *writer) const
139 140 141
{
    Q_UNUSED(writer)
    const ObjectTreeParser *otp = part->objectTreeParser();
142 143
    const auto p = process(*part);
    const auto mp =  static_cast<MessagePart *>(p.data());
Laurent Montel's avatar
Laurent Montel committed
144 145 146 147 148 149 150
    if (mp) {
        if (!otp->attachmentStrategy()->inlineNestedMessages() && !otp->showOnlyOneMimePart()) {
            return Failed;
        } else {
            mp->html(true);
            return Ok;
        }
151
    } else {
Laurent Montel's avatar
Laurent Montel committed
152
        return Failed;
153 154 155 156 157
    }
}

#define CREATE_BODY_PART_FORMATTER(subtype) \
    class subtype##BodyPartFormatter \
158
        : public MimeTreeParser::Interface::BodyPartFormatter \
159 160 161
    { \
        static const subtype##BodyPartFormatter *self; \
    public: \
162
        Interface::MessagePart::Ptr process(Interface::BodyPart &part) const Q_DECL_OVERRIDE; \
163 164 165
        MimeTreeParser::Interface::BodyPartFormatter::Result format(Interface::BodyPart *, HtmlWriter *) const Q_DECL_OVERRIDE; \
        using MimeTreeParser::Interface::BodyPartFormatter::format; \
        static const MimeTreeParser::Interface::BodyPartFormatter *create(); \
166 167 168 169
    }; \
    \
    const subtype##BodyPartFormatter *subtype##BodyPartFormatter::self; \
    \
170
    const MimeTreeParser::Interface::BodyPartFormatter *subtype##BodyPartFormatter::create() { \
171 172 173 174 175
        if ( !self ) { \
            self = new subtype##BodyPartFormatter(); \
        } \
        return self; \
    } \
176
    Interface::MessagePart::Ptr subtype##BodyPartFormatter::process(Interface::BodyPart &part) const { \
177
        return part.objectTreeParser()->process##subtype##Subtype(part.content(), *part.processResult()); \
178 179
    } \
    \
180
    MimeTreeParser::Interface::BodyPartFormatter::Result subtype##BodyPartFormatter::format(Interface::BodyPart *part, HtmlWriter *writer) const { \
181
        Q_UNUSED(writer) \
182 183 184
        const auto p = process(*part);\
        const auto mp = static_cast<MessagePart *>(p.data());\
        if (mp) { \
185 186 187 188
            mp->html(false);\
            return Ok;\
        }\
        return Failed;\
189 190 191 192 193 194 195 196 197
    }

CREATE_BODY_PART_FORMATTER(TextPlain)
CREATE_BODY_PART_FORMATTER(TextHtml)

CREATE_BODY_PART_FORMATTER(ApplicationPkcs7Mime)

CREATE_BODY_PART_FORMATTER(MultiPartMixed)
CREATE_BODY_PART_FORMATTER(MultiPartAlternative)
198 199
CREATE_BODY_PART_FORMATTER(MultiPartSigned)
CREATE_BODY_PART_FORMATTER(MultiPartEncrypted)
200 201 202 203 204 205

typedef TextPlainBodyPartFormatter ApplicationPgpBodyPartFormatter;

#undef CREATE_BODY_PART_FORMATTER
} // anon namespace

206
void BodyPartFormatterBaseFactoryPrivate::messageviewer_create_builtin_bodypart_formatters()
207
{
208 209 210 211
    insert("application", "octet-stream", AnyTypeBodyPartFormatter::create());
    insert("application", "pgp", ApplicationPgpBodyPartFormatter::create());
    insert("application", "pkcs7-mime", ApplicationPkcs7MimeBodyPartFormatter::create());
    insert("application", "x-pkcs7-mime", ApplicationPkcs7MimeBodyPartFormatter::create());
212
    insert("application", "pgp-encrypted", ApplicationPGPEncryptedBodyPartFormatter::create());
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    insert("application", "*", AnyTypeBodyPartFormatter::create());

    insert("text", "html", TextHtmlBodyPartFormatter::create());
    insert("text", "rtf", AnyTypeBodyPartFormatter::create());
    insert("text", "vcard", AnyTypeBodyPartFormatter::create());
    insert("text", "x-vcard", AnyTypeBodyPartFormatter::create());
    insert("text", "plain", MailmanBodyPartFormatter::create());
    insert("text", "plain", TextPlainBodyPartFormatter::create());
    insert("text", "*", MailmanBodyPartFormatter::create());
    insert("text", "*", TextPlainBodyPartFormatter::create());

    insert("image", "*", ImageTypeBodyPartFormatter::create());

    insert("message", "rfc822", MessageRfc822BodyPartFormatter::create());
    insert("message", "*", AnyTypeBodyPartFormatter::create());

    insert("multipart", "alternative", MultiPartAlternativeBodyPartFormatter::create());
    insert("multipart", "encrypted", MultiPartEncryptedBodyPartFormatter::create());
    insert("multipart", "signed", MultiPartSignedBodyPartFormatter::create());
    insert("multipart", "*", MultiPartMixedBodyPartFormatter::create());
    insert("*", "*", AnyTypeBodyPartFormatter::create());
234
}