summaryrefslogtreecommitdiffstats
path: root/framework/domain/mimetreeparser/interface.cpp
blob: e34ffda766e9397ee826175d980c93dc0d5c0868 (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
    Copyright (c) 2016 Sandro Knauß <knauss@kolabsystems.com>

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library 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 Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

#include "interface.h"
#include "interface_p.h"

#include "stringhtmlwriter.h"
#include "objecttreesource.h"

#include <KMime/Content>
#include <MimeTreeParser/ObjectTreeParser>
#include <MimeTreeParser/MessagePart>
#include <MimeTreeParser/NodeHelper>

#include <QDebug>

class PartPrivate
{
public:
    PartPrivate(Part *part);
    void appendSubPart(Part::Ptr subpart);

    QVector<Part::Ptr> subParts();

    Part *parent() const;
private:
    Part *q;
    Part *mParent;
    QVector<Part::Ptr> mSubParts;
};

PartPrivate::PartPrivate(Part* part)
    : q(part)
    , mParent(Q_NULLPTR)
{

}

void PartPrivate::appendSubPart(Part::Ptr subpart)
{
    subpart->d->mParent = q;
    mSubParts.append(subpart);
}

Part *PartPrivate::parent() const
{
    return mParent;
}

QVector< Part::Ptr > PartPrivate::subParts()
{
    return mSubParts;
}

Part::Part()
    : d(std::unique_ptr<PartPrivate>(new PartPrivate(this)))
{

}

bool Part::hasSubParts() const
{
    return !subParts().isEmpty();
}

QVector<Part::Ptr> Part::subParts() const
{
    return d->subParts();
}

QByteArray Part::type() const
{
    return "Part";
}

QVector<Encryption> Part::encryptions() const
{
    auto parent = d->parent();
    if (parent) {
        return parent->encryptions();
    } else {
        return QVector<Encryption>();
    }
}

QVector<Signature> Part::signatures() const
{
    auto parent = d->parent();
    if (parent) {
        return parent->signatures();
    } else {
        return QVector<Signature>();
    }
}

class ContentPrivate
{
public:
    QByteArray mContent;
    QByteArray mCodec;
    Part *mParent;
    Content *q;
};

Content::Content(const QByteArray& content, ContentPart *parent)
    : d(std::unique_ptr<ContentPrivate>(new ContentPrivate))
{
    d->q = this;
    d->mContent = content;
    d->mCodec = "utf-8";
    d->mParent = parent;
}

Content::~Content()
{
}

QVector< Encryption > Content::encryptions() const
{
    if (d->mParent) {
        return d->mParent->encryptions();
    }
    return QVector<Encryption>();
}

QVector< Signature > Content::signatures() const
{
    if (d->mParent) {
        return d->mParent->signatures();
    }
    return QVector<Signature>();
}

QByteArray Content::content() const
{
    return d->mContent;
}

QByteArray Content::charset() const
{
    return d->mCodec;
}

class ContentPartPrivate
{
public:
    void fillFrom(MimeTreeParser::TextMessagePart::Ptr part);
    void fillFrom(MimeTreeParser::HtmlMessagePart::Ptr part);
    void fillFrom(MimeTreeParser::AlternativeMessagePart::Ptr part);

    QVector<Content::Ptr> content(ContentPart::Type ct) const;

    ContentPart *q;

    ContentPart::Types types() const;

private:
    QMap<ContentPart::Type, QVector<Content::Ptr>> mContent;
    ContentPart::Types mTypes;
};

void ContentPartPrivate::fillFrom(MimeTreeParser::TextMessagePart::Ptr part)
{
    qDebug() << "jepp";
    mTypes = ContentPart::PlainText;
    foreach (const auto &mp, part->subParts()) {
        auto content = std::make_shared<Content>(mp->text().toLocal8Bit(), q);
        mContent[ContentPart::PlainText].append(content);
    }
}

void ContentPartPrivate::fillFrom(MimeTreeParser::HtmlMessagePart::Ptr part)
{
    mTypes = ContentPart::Html;
    auto content = std::make_shared<Content>(part->text().toLocal8Bit(), q);
    mContent[ContentPart::Html].append(content);
}

void ContentPartPrivate::fillFrom(MimeTreeParser::AlternativeMessagePart::Ptr part)
{
    mTypes = ContentPart::Html | ContentPart::PlainText;

    auto content = std::make_shared<Content>(part->htmlContent().toLocal8Bit(), q);
    mContent[ContentPart::Html].append(content);
    content = std::make_shared<Content>(part->plaintextContent().toLocal8Bit(), q);
    mContent[ContentPart::PlainText].append(content);
}

ContentPart::Types ContentPartPrivate::types() const
{
    return mTypes;
}

QVector<Content::Ptr> ContentPartPrivate::content(ContentPart::Type ct) const
{
    return mContent[ct];
}

QVector<Content::Ptr> ContentPart::content(ContentPart::Type ct) const
{
    return d->content(ct);
}


ContentPart::ContentPart()
    : d(std::unique_ptr<ContentPartPrivate>(new ContentPartPrivate))
{
    d->q = this;
}

ContentPart::~ContentPart()
{

}

QByteArray ContentPart::type() const
{
    return "ContentPart";
}

ContentPart::Types ContentPart::availableContents() const
{
    return d->types();
}

class MimePartPrivate
{
public:
    void fillFrom(MimeTreeParser::MessagePart::Ptr part);
};

QByteArray MimePart::type() const
{
    return "MimePart";
}

class AttachmentPartPrivate
{
public:
    void fillFrom(MimeTreeParser::AttachmentMessagePart::Ptr part);
};

void AttachmentPartPrivate::fillFrom(MimeTreeParser::AttachmentMessagePart::Ptr part)
{

}

QByteArray AttachmentPart::type() const
{
    return "AttachmentPart";
}

ParserPrivate::ParserPrivate(Parser* parser)
    : q(parser)
    , mNodeHelper(std::make_shared<MimeTreeParser::NodeHelper>())
{

}

void ParserPrivate::setMessage(const QByteArray& mimeMessage)
{
    const auto mailData = KMime::CRLFtoLF(mimeMessage);
    KMime::Message::Ptr msg(new KMime::Message);
    msg->setContent(mailData);
    msg->parse();

    // render the mail
    StringHtmlWriter htmlWriter;
    ObjectTreeSource source(&htmlWriter);
    MimeTreeParser::ObjectTreeParser otp(&source, mNodeHelper.get());

    otp.parseObjectTree(msg.data());
    mPartTree = otp.parsedPart().dynamicCast<MimeTreeParser::MessagePart>();

    mEmbeddedPartMap = htmlWriter.embeddedParts();
    mHtml = htmlWriter.html();

    mTree = std::make_shared<Part>();
    createTree(mPartTree, mTree);
}


void ParserPrivate::createTree(const MimeTreeParser::MessagePart::Ptr &start, const Part::Ptr &tree)
{
    foreach (const auto &mp, start->subParts()) {
        const auto m = mp.dynamicCast<MimeTreeParser::MessagePart>();
        const auto text = mp.dynamicCast<MimeTreeParser::TextMessagePart>();
        const auto alternative = mp.dynamicCast<MimeTreeParser::AlternativeMessagePart>();
        const auto html = mp.dynamicCast<MimeTreeParser::HtmlMessagePart>();
        const auto attachment = mp.dynamicCast<MimeTreeParser::AttachmentMessagePart>();
         if (attachment) {
            auto part = std::make_shared<AttachmentPart>();
            part->d->fillFrom(attachment);
            mTree->d->appendSubPart(part);
         } else if (text) {
            auto part = std::make_shared<ContentPart>();
            part->d->fillFrom(text);
            mTree->d->appendSubPart(part);
        } else if (alternative) {
            auto part = std::make_shared<ContentPart>();
            part->d->fillFrom(alternative);
            mTree->d->appendSubPart(part);
        } else if (html) {
            auto part = std::make_shared<ContentPart>();
            part->d->fillFrom(html);
            mTree->d->appendSubPart(part);
        } else {
            createTree(m, tree);
        }
    }
}

Parser::Parser(const QByteArray& mimeMessage)
    :d(std::unique_ptr<ParserPrivate>(new ParserPrivate(this)))
{
    d->setMessage(mimeMessage);
}

Parser::~Parser()
{
}

ContentPart::Ptr Parser::collectContentPart(const Part::Ptr &start) const
{
    const auto ret = collect<ContentPart>(start, [](const Part::Ptr &p){return p->type() == "ContentPart";}, [](const ContentPart::Ptr &p){return true;});
    if (ret.size() > 0) {
        return ret[0];
    };
    return ContentPart::Ptr();
}

ContentPart::Ptr Parser::collectContentPart() const
{
    return collectContentPart(d->mTree);
}

template <typename T>
QVector<typename T::Ptr> Parser::collect(const Part::Ptr &start, std::function<bool(const Part::Ptr &)> select, std::function<bool(const typename T::Ptr &)> filter) const
{
    QVector<typename T::Ptr> ret;
    foreach (const auto &part, start->subParts()) {
        if (select(part)){
            const auto p = std::dynamic_pointer_cast<T>(part);
            if (p && filter(p)) {
                ret.append(p);
            }
            ret += collect<T>(part, select, filter);
        }
    }
    return ret;
}