diff options
author | Sandro Knauß <sknauss@kde.org> | 2016-07-19 14:46:23 +0200 |
---|---|---|
committer | Sandro Knauß <sknauss@kde.org> | 2016-07-19 14:51:28 +0200 |
commit | 9516f3b02f74f239ce2776abf7cf1147952065cd (patch) | |
tree | 45c02205335e2d5e8796adc7e75489a1d365d33c | |
parent | 2fdddd2f795da4645dc9a48fee4865324143a21b (diff) | |
download | kube-9516f3b02f74f239ce2776abf7cf1147952065cd.tar.gz kube-9516f3b02f74f239ce2776abf7cf1147952065cd.zip |
new mimetreeparser interface
Reviewers: cmollekopf
Maniphest Tasks: T3208
Differential Revision: https://phabricator.kde.org/D2221
-rw-r--r-- | framework/domain/mimetreeparser/interface.cpp | 89 | ||||
-rw-r--r-- | framework/domain/mimetreeparser/interface.h | 14 | ||||
-rw-r--r-- | framework/domain/mimetreeparser/tests/data/html.mbox | 12 | ||||
-rw-r--r-- | framework/domain/mimetreeparser/tests/interfacetest.cpp | 25 |
4 files changed, 98 insertions, 42 deletions
diff --git a/framework/domain/mimetreeparser/interface.cpp b/framework/domain/mimetreeparser/interface.cpp index 6a399015..c239fcc0 100644 --- a/framework/domain/mimetreeparser/interface.cpp +++ b/framework/domain/mimetreeparser/interface.cpp | |||
@@ -38,26 +38,27 @@ public: | |||
38 | 38 | ||
39 | QVector<Part::Ptr> subParts(); | 39 | QVector<Part::Ptr> subParts(); |
40 | 40 | ||
41 | const std::weak_ptr<Part> &parent() const; | 41 | Part *parent() const; |
42 | private: | 42 | private: |
43 | Part *q; | 43 | Part *q; |
44 | std::weak_ptr<Part> mParent; | 44 | Part *mParent; |
45 | QVector<Part::Ptr> mSubParts; | 45 | QVector<Part::Ptr> mSubParts; |
46 | }; | 46 | }; |
47 | 47 | ||
48 | PartPrivate::PartPrivate(Part* part) | 48 | PartPrivate::PartPrivate(Part* part) |
49 | :q(part) | 49 | : q(part) |
50 | , mParent(Q_NULLPTR) | ||
50 | { | 51 | { |
51 | 52 | ||
52 | } | 53 | } |
53 | 54 | ||
54 | void PartPrivate::appendSubPart(Part::Ptr subpart) | 55 | void PartPrivate::appendSubPart(Part::Ptr subpart) |
55 | { | 56 | { |
56 | subpart->d->mParent = Part::Ptr(q); | 57 | subpart->d->mParent = q; |
57 | mSubParts.append(subpart); | 58 | mSubParts.append(subpart); |
58 | } | 59 | } |
59 | 60 | ||
60 | const std::weak_ptr<Part> &PartPrivate::parent() const | 61 | Part *PartPrivate::parent() const |
61 | { | 62 | { |
62 | return mParent; | 63 | return mParent; |
63 | } | 64 | } |
@@ -90,7 +91,7 @@ QByteArray Part::type() const | |||
90 | 91 | ||
91 | QVector<Encryption> Part::encryptions() const | 92 | QVector<Encryption> Part::encryptions() const |
92 | { | 93 | { |
93 | auto parent = d->parent().lock(); | 94 | auto parent = d->parent(); |
94 | if (parent) { | 95 | if (parent) { |
95 | return parent->encryptions(); | 96 | return parent->encryptions(); |
96 | } else { | 97 | } else { |
@@ -100,7 +101,7 @@ QVector<Encryption> Part::encryptions() const | |||
100 | 101 | ||
101 | QVector<Signature> Part::signatures() const | 102 | QVector<Signature> Part::signatures() const |
102 | { | 103 | { |
103 | auto parent = d->parent().lock(); | 104 | auto parent = d->parent(); |
104 | if (parent) { | 105 | if (parent) { |
105 | return parent->signatures(); | 106 | return parent->signatures(); |
106 | } else { | 107 | } else { |
@@ -146,6 +147,16 @@ QVector< Signature > Content::signatures() const | |||
146 | return QVector<Signature>(); | 147 | return QVector<Signature>(); |
147 | } | 148 | } |
148 | 149 | ||
150 | QByteArray Content::content() const | ||
151 | { | ||
152 | return d->mContent; | ||
153 | } | ||
154 | |||
155 | QByteArray Content::charset() const | ||
156 | { | ||
157 | return d->mCodec; | ||
158 | } | ||
159 | |||
149 | class ContentPartPrivate | 160 | class ContentPartPrivate |
150 | { | 161 | { |
151 | public: | 162 | public: |
@@ -153,12 +164,14 @@ public: | |||
153 | void fillFrom(MimeTreeParser::HtmlMessagePart::Ptr part); | 164 | void fillFrom(MimeTreeParser::HtmlMessagePart::Ptr part); |
154 | void fillFrom(MimeTreeParser::AlternativeMessagePart::Ptr part); | 165 | void fillFrom(MimeTreeParser::AlternativeMessagePart::Ptr part); |
155 | 166 | ||
156 | QVector<Content::Ptr> contents() const; | 167 | QVector<Content::Ptr> content() const; |
157 | 168 | ||
158 | ContentPart *q; | 169 | ContentPart *q; |
170 | |||
171 | ContentPart::Types types() const; | ||
159 | 172 | ||
160 | private: | 173 | private: |
161 | QVector<Content::Ptr> mContents; | 174 | QVector<Content::Ptr> mContent; |
162 | ContentPart::Types mTypes; | 175 | ContentPart::Types mTypes; |
163 | }; | 176 | }; |
164 | 177 | ||
@@ -167,7 +180,7 @@ void ContentPartPrivate::fillFrom(MimeTreeParser::TextMessagePart::Ptr part) | |||
167 | mTypes = ContentPart::PlainText; | 180 | mTypes = ContentPart::PlainText; |
168 | foreach (const auto &mp, part->subParts()) { | 181 | foreach (const auto &mp, part->subParts()) { |
169 | auto content = std::make_shared<Content>(mp->text().toLocal8Bit(), q); | 182 | auto content = std::make_shared<Content>(mp->text().toLocal8Bit(), q); |
170 | mContents.append(content); | 183 | mContent.append(content); |
171 | } | 184 | } |
172 | } | 185 | } |
173 | 186 | ||
@@ -181,6 +194,22 @@ void ContentPartPrivate::fillFrom(MimeTreeParser::AlternativeMessagePart::Ptr pa | |||
181 | mTypes = ContentPart::Html | ContentPart::PlainText; | 194 | mTypes = ContentPart::Html | ContentPart::PlainText; |
182 | } | 195 | } |
183 | 196 | ||
197 | ContentPart::Types ContentPartPrivate::types() const | ||
198 | { | ||
199 | return mTypes; | ||
200 | } | ||
201 | |||
202 | QVector<Content::Ptr> ContentPartPrivate::content() const | ||
203 | { | ||
204 | return mContent; | ||
205 | } | ||
206 | |||
207 | QVector<Content::Ptr> ContentPart::content(ContentPart::Type ct) const | ||
208 | { | ||
209 | return d->content(); | ||
210 | } | ||
211 | |||
212 | |||
184 | ContentPart::ContentPart() | 213 | ContentPart::ContentPart() |
185 | : d(std::unique_ptr<ContentPartPrivate>(new ContentPartPrivate)) | 214 | : d(std::unique_ptr<ContentPartPrivate>(new ContentPartPrivate)) |
186 | { | 215 | { |
@@ -197,6 +226,11 @@ QByteArray ContentPart::type() const | |||
197 | return "ContentPart"; | 226 | return "ContentPart"; |
198 | } | 227 | } |
199 | 228 | ||
229 | ContentPart::Types ContentPart::availableContents() const | ||
230 | { | ||
231 | return d->types(); | ||
232 | } | ||
233 | |||
200 | class MimePartPrivate | 234 | class MimePartPrivate |
201 | { | 235 | { |
202 | public: | 236 | public: |
@@ -230,7 +264,7 @@ public: | |||
230 | ParserPrivate(Parser *parser); | 264 | ParserPrivate(Parser *parser); |
231 | 265 | ||
232 | void setMessage(const QByteArray &mimeMessage); | 266 | void setMessage(const QByteArray &mimeMessage); |
233 | void createTree(MimeTreeParser::MessagePart::Ptr start, Part::Ptr tree); | 267 | void createTree(const MimeTreeParser::MessagePart::Ptr& start, const Part::Ptr& tree); |
234 | 268 | ||
235 | Part::Ptr mTree; | 269 | Part::Ptr mTree; |
236 | private: | 270 | private: |
@@ -273,9 +307,8 @@ void ParserPrivate::setMessage(const QByteArray& mimeMessage) | |||
273 | } | 307 | } |
274 | 308 | ||
275 | 309 | ||
276 | void ParserPrivate::createTree(MimeTreeParser::MessagePart::Ptr start, Part::Ptr tree) | 310 | void ParserPrivate::createTree(const MimeTreeParser::MessagePart::Ptr &start, const Part::Ptr &tree) |
277 | { | 311 | { |
278 | |||
279 | foreach (const auto &mp, start->subParts()) { | 312 | foreach (const auto &mp, start->subParts()) { |
280 | const auto m = mp.dynamicCast<MimeTreeParser::MessagePart>(); | 313 | const auto m = mp.dynamicCast<MimeTreeParser::MessagePart>(); |
281 | const auto text = mp.dynamicCast<MimeTreeParser::TextMessagePart>(); | 314 | const auto text = mp.dynamicCast<MimeTreeParser::TextMessagePart>(); |
@@ -316,16 +349,10 @@ Parser::~Parser() | |||
316 | 349 | ||
317 | ContentPart::Ptr Parser::collectContentPart(const Part::Ptr &start) const | 350 | ContentPart::Ptr Parser::collectContentPart(const Part::Ptr &start) const |
318 | { | 351 | { |
319 | foreach (const auto &part, start->subParts()) { | 352 | const auto ret = collect<ContentPart>(start, [](const Part::Ptr &p){return p->type() == "ContentPart";}, [](const ContentPart::Ptr &p){return true;}); |
320 | if (part->type() == "ContentPart") { | 353 | if (ret.size() > 0) { |
321 | return std::dynamic_pointer_cast<ContentPart>(part); | 354 | return ret[0]; |
322 | } else { | 355 | }; |
323 | auto ret = collectContentPart(part); | ||
324 | if (ret) { | ||
325 | return ret; | ||
326 | } | ||
327 | } | ||
328 | } | ||
329 | return ContentPart::Ptr(); | 356 | return ContentPart::Ptr(); |
330 | } | 357 | } |
331 | 358 | ||
@@ -333,3 +360,19 @@ ContentPart::Ptr Parser::collectContentPart() const | |||
333 | { | 360 | { |
334 | return collectContentPart(d->mTree); | 361 | return collectContentPart(d->mTree); |
335 | } | 362 | } |
363 | |||
364 | template <typename T> | ||
365 | 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 | ||
366 | { | ||
367 | QVector<typename T::Ptr> ret; | ||
368 | foreach (const auto &part, start->subParts()) { | ||
369 | if (select(part)){ | ||
370 | const auto p = std::dynamic_pointer_cast<T>(part); | ||
371 | if (p && filter(p)) { | ||
372 | ret.append(p); | ||
373 | } | ||
374 | ret += collect<T>(part, select, filter); | ||
375 | } | ||
376 | } | ||
377 | return ret; | ||
378 | } | ||
diff --git a/framework/domain/mimetreeparser/interface.h b/framework/domain/mimetreeparser/interface.h index 82f88e73..8a0047ff 100644 --- a/framework/domain/mimetreeparser/interface.h +++ b/framework/domain/mimetreeparser/interface.h | |||
@@ -66,7 +66,7 @@ public: | |||
66 | 66 | ||
67 | bool hasSubParts() const; | 67 | bool hasSubParts() const; |
68 | QVector<Part::Ptr> subParts() const; | 68 | QVector<Part::Ptr> subParts() const; |
69 | Part::Ptr parent() const; | 69 | Part *parent() const; |
70 | 70 | ||
71 | virtual QVector<Signature> signatures() const; | 71 | virtual QVector<Signature> signatures() const; |
72 | virtual QVector<Encryption> encryptions() const; | 72 | virtual QVector<Encryption> encryptions() const; |
@@ -85,6 +85,8 @@ public: | |||
85 | 85 | ||
86 | QByteArray content() const; | 86 | QByteArray content() const; |
87 | 87 | ||
88 | QByteArray charset() const; | ||
89 | |||
88 | //Use default charset | 90 | //Use default charset |
89 | QString encodedContent() const; | 91 | QString encodedContent() const; |
90 | 92 | ||
@@ -162,8 +164,8 @@ public: | |||
162 | 164 | ||
163 | ContentPart(); | 165 | ContentPart(); |
164 | virtual ~ContentPart(); | 166 | virtual ~ContentPart(); |
165 | 167 | ||
166 | QVector<Content> content(Type ct) const; | 168 | QVector<Content::Ptr> content(Type ct) const; |
167 | 169 | ||
168 | Types availableContents() const; | 170 | Types availableContents() const; |
169 | 171 | ||
@@ -259,7 +261,6 @@ private: | |||
259 | std::unique_ptr<CertPartPrivate> d; | 261 | std::unique_ptr<CertPartPrivate> d; |
260 | }; | 262 | }; |
261 | 263 | ||
262 | |||
263 | class Key | 264 | class Key |
264 | { | 265 | { |
265 | QString keyid() const; | 266 | QString keyid() const; |
@@ -309,7 +310,7 @@ public: | |||
309 | 310 | ||
310 | Part::Ptr getPart(QUrl url); | 311 | Part::Ptr getPart(QUrl url); |
311 | 312 | ||
312 | //template <typename T> QVector<T::Ptr> collect<T>(Part start, std::function<bool(const Part &)> select, std::function<bool(const T::Ptr &)> filter) const; | 313 | template <typename T> QVector<typename T::Ptr> collect(const Part::Ptr &start, std::function<bool(const Part::Ptr &)> select, std::function<bool(const typename T::Ptr &)> filter) const; |
313 | QVector<AttachmentPart::Ptr> collectAttachments(Part::Ptr start, std::function<bool(const Part::Ptr &)> select, std::function<bool(const AttachmentPart::Ptr &)> filter) const; | 314 | QVector<AttachmentPart::Ptr> collectAttachments(Part::Ptr start, std::function<bool(const Part::Ptr &)> select, std::function<bool(const AttachmentPart::Ptr &)> filter) const; |
314 | ContentPart::Ptr collectContentPart(Part::Ptr start, std::function<bool(const Part::Ptr &)> select, std::function<bool(const ContentPart::Ptr &)> filter) const; | 315 | ContentPart::Ptr collectContentPart(Part::Ptr start, std::function<bool(const Part::Ptr &)> select, std::function<bool(const ContentPart::Ptr &)> filter) const; |
315 | ContentPart::Ptr collectContentPart(const Part::Ptr& start) const; | 316 | ContentPart::Ptr collectContentPart(const Part::Ptr& start) const; |
@@ -324,4 +325,5 @@ signals: | |||
324 | 325 | ||
325 | private: | 326 | private: |
326 | std::unique_ptr<ParserPrivate> d; | 327 | std::unique_ptr<ParserPrivate> d; |
327 | }; \ No newline at end of file | 328 | }; |
329 | |||
diff --git a/framework/domain/mimetreeparser/tests/data/html.mbox b/framework/domain/mimetreeparser/tests/data/html.mbox index eebd4283..d476a8d4 100644 --- a/framework/domain/mimetreeparser/tests/data/html.mbox +++ b/framework/domain/mimetreeparser/tests/data/html.mbox | |||
@@ -9,16 +9,6 @@ X-KMail-Drafts: 7 | |||
9 | X-KMail-Templates: 9 | 9 | X-KMail-Templates: 9 |
10 | User-Agent: KMail/4.6 beta5 (Linux/2.6.34.7-0.7-desktop; KDE/4.6.41; x86_64; git-0269848; 2011-04-19) | 10 | User-Agent: KMail/4.6 beta5 (Linux/2.6.34.7-0.7-desktop; KDE/4.6.41; x86_64; git-0269848; 2011-04-19) |
11 | MIME-Version: 1.0 | 11 | MIME-Version: 1.0 |
12 | Content-Type: multipart/alternative; boundary="nextPart8606278.tpV19BTJKu" | ||
13 | Content-Transfer-Encoding: 7Bit | ||
14 | |||
15 | |||
16 | --nextPart8606278.tpV19BTJKu | ||
17 | Content-Transfer-Encoding: 7Bit | ||
18 | Content-Type: text/plain; charset="windows-1252" | ||
19 | |||
20 | Some HTML text | ||
21 | --nextPart8606278.tpV19BTJKu | ||
22 | Content-Transfer-Encoding: 7Bit | 12 | Content-Transfer-Encoding: 7Bit |
23 | Content-Type: text/html; charset="windows-1252" | 13 | Content-Type: text/html; charset="windows-1252" |
24 | 14 | ||
@@ -27,5 +17,3 @@ Content-Type: text/html; charset="windows-1252" | |||
27 | p, li { white-space: pre-wrap; } | 17 | p, li { white-space: pre-wrap; } |
28 | </style></head><body style=" font-family:'Sans Serif'; font-size:9pt; font-weight:400; font-style:normal;"> | 18 | </style></head><body style=" font-family:'Sans Serif'; font-size:9pt; font-weight:400; font-style:normal;"> |
29 | <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;">Some <span style=" font-weight:600;">HTML</span> text</p></body></html> | 19 | <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;">Some <span style=" font-weight:600;">HTML</span> text</p></body></html> |
30 | --nextPart8606278.tpV19BTJKu-- | ||
31 | |||
diff --git a/framework/domain/mimetreeparser/tests/interfacetest.cpp b/framework/domain/mimetreeparser/tests/interfacetest.cpp index 1e8c5302..fd828960 100644 --- a/framework/domain/mimetreeparser/tests/interfacetest.cpp +++ b/framework/domain/mimetreeparser/tests/interfacetest.cpp | |||
@@ -39,7 +39,30 @@ private slots: | |||
39 | { | 39 | { |
40 | Parser parser(readMailFromFile("plaintext.mbox")); | 40 | Parser parser(readMailFromFile("plaintext.mbox")); |
41 | auto contentPart = parser.collectContentPart(); | 41 | auto contentPart = parser.collectContentPart(); |
42 | //QVERIFY((bool)contentPart); | 42 | QVERIFY((bool)contentPart); |
43 | QCOMPARE(contentPart->availableContents(), ContentPart::PlainText); | ||
44 | auto contentList = contentPart->content(ContentPart::PlainText); | ||
45 | QCOMPARE(contentList.size(), 1); | ||
46 | QCOMPARE(contentList[0]->content(), QStringLiteral("If you can see this text it means that your email client couldn't display our newsletter properly.\nPlease visit this link to view the newsletter on our website: http://www.gog.com/newsletter/\n\n- GOG.com Team\n\n").toLocal8Bit()); | ||
47 | QCOMPARE(contentList[0]->charset(), QStringLiteral("utf-8").toLocal8Bit()); | ||
48 | QCOMPARE(contentList[0]->encryptions().size(), 0); | ||
49 | QCOMPARE(contentList[0]->signatures().size(), 0); | ||
50 | } | ||
51 | |||
52 | void testTextAlternative() | ||
53 | { | ||
54 | Parser parser(readMailFromFile("alternative.mbox")); | ||
55 | auto contentPart = parser.collectContentPart(); | ||
56 | QVERIFY((bool)contentPart); | ||
57 | QCOMPARE(contentPart->availableContents(), ContentPart::PlainText | ContentPart::Html); | ||
58 | } | ||
59 | |||
60 | void testTextHtml() | ||
61 | { | ||
62 | Parser parser(readMailFromFile("html.mbox")); | ||
63 | auto contentPart = parser.collectContentPart(); | ||
64 | QVERIFY((bool)contentPart); | ||
65 | QCOMPARE(contentPart->availableContents(), ContentPart::Html); | ||
43 | } | 66 | } |
44 | }; | 67 | }; |
45 | 68 | ||