diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-06-04 15:21:05 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-06-04 15:21:05 +0200 |
commit | 964f0003584a0b745045db75ca6184f54eddd859 (patch) | |
tree | 1019133d8c4a48cc90bb9c9091d626bc58e05cca | |
parent | e452707fdfbd61be1e5633b516b653b7337e7865 (diff) | |
download | kube-964f0003584a0b745045db75ca6184f54eddd859.tar.gz kube-964f0003584a0b745045db75ca6184f54eddd859.zip |
Rely on isAttachment to collect attachments
6 files changed, 11 insertions, 111 deletions
diff --git a/framework/src/domain/mime/mimetreeparser/autotests/attachmenttest.cpp b/framework/src/domain/mime/mimetreeparser/autotests/attachmenttest.cpp index 47b5cbce..789c9049 100644 --- a/framework/src/domain/mime/mimetreeparser/autotests/attachmenttest.cpp +++ b/framework/src/domain/mime/mimetreeparser/autotests/attachmenttest.cpp | |||
@@ -22,6 +22,7 @@ | |||
22 | #include "setupenv.h" | 22 | #include "setupenv.h" |
23 | 23 | ||
24 | #include <qtest.h> | 24 | #include <qtest.h> |
25 | #include <QDebug> | ||
25 | 26 | ||
26 | using namespace MimeTreeParser; | 27 | using namespace MimeTreeParser; |
27 | 28 | ||
@@ -58,10 +59,10 @@ void AttachmentTest::testEncryptedAttachment() | |||
58 | ObjectTreeParser otp(&nodeHelper); | 59 | ObjectTreeParser otp(&nodeHelper); |
59 | otp.parseObjectTree(msg.data()); | 60 | otp.parseObjectTree(msg.data()); |
60 | otp.decryptParts(); | 61 | otp.decryptParts(); |
62 | otp.print(); | ||
61 | 63 | ||
62 | auto attachments = msg->attachments(); | 64 | auto attachmentParts = otp.collectAttachmentParts(); |
63 | auto encAtts = nodeHelper.attachmentsOfExtraContents(); | 65 | QCOMPARE(attachmentParts.size(), 2); |
64 | QCOMPARE(attachments.size() + encAtts.size(), 2); | ||
65 | } | 66 | } |
66 | 67 | ||
67 | #include "attachmenttest.moc" | 68 | #include "attachmenttest.moc" |
diff --git a/framework/src/domain/mime/mimetreeparser/messagepart.cpp b/framework/src/domain/mime/mimetreeparser/messagepart.cpp index f93d4ea5..b6dfd30a 100644 --- a/framework/src/domain/mime/mimetreeparser/messagepart.cpp +++ b/framework/src/domain/mime/mimetreeparser/messagepart.cpp | |||
@@ -185,7 +185,10 @@ PartMetaData *MessagePart::partMetaData() | |||
185 | 185 | ||
186 | bool MessagePart::isAttachment() const | 186 | bool MessagePart::isAttachment() const |
187 | { | 187 | { |
188 | return true; | 188 | if (mNode) { |
189 | return KMime::isAttachment(mNode); | ||
190 | } | ||
191 | return false; | ||
189 | } | 192 | } |
190 | 193 | ||
191 | KMime::Content *MessagePart::node() const | 194 | KMime::Content *MessagePart::node() const |
diff --git a/framework/src/domain/mime/mimetreeparser/messagepart.h b/framework/src/domain/mime/mimetreeparser/messagepart.h index 04fb30c3..0c206210 100644 --- a/framework/src/domain/mime/mimetreeparser/messagepart.h +++ b/framework/src/domain/mime/mimetreeparser/messagepart.h | |||
@@ -83,7 +83,7 @@ public: | |||
83 | 83 | ||
84 | virtual QString text() const; | 84 | virtual QString text() const; |
85 | void setText(const QString &text); | 85 | void setText(const QString &text); |
86 | bool isAttachment() const; | 86 | virtual bool isAttachment() const; |
87 | 87 | ||
88 | void setIsRoot(bool root); | 88 | void setIsRoot(bool root); |
89 | bool isRoot() const; | 89 | bool isRoot() const; |
@@ -196,6 +196,7 @@ public: | |||
196 | typedef QSharedPointer<AttachmentMessagePart> Ptr; | 196 | typedef QSharedPointer<AttachmentMessagePart> Ptr; |
197 | AttachmentMessagePart(MimeTreeParser::ObjectTreeParser *otp, KMime::Content *node); | 197 | AttachmentMessagePart(MimeTreeParser::ObjectTreeParser *otp, KMime::Content *node); |
198 | virtual ~AttachmentMessagePart(); | 198 | virtual ~AttachmentMessagePart(); |
199 | virtual bool isAttachment() const Q_DECL_OVERRIDE { return true; } | ||
199 | 200 | ||
200 | }; | 201 | }; |
201 | 202 | ||
diff --git a/framework/src/domain/mime/mimetreeparser/nodehelper.cpp b/framework/src/domain/mime/mimetreeparser/nodehelper.cpp index 3005ea0f..4e60fa68 100644 --- a/framework/src/domain/mime/mimetreeparser/nodehelper.cpp +++ b/framework/src/domain/mime/mimetreeparser/nodehelper.cpp | |||
@@ -456,89 +456,4 @@ QList< KMime::Content * > NodeHelper::extraContents(KMime::Content *topLevelnode | |||
456 | return mExtraContents.value(topLevelnode); | 456 | return mExtraContents.value(topLevelnode); |
457 | } | 457 | } |
458 | 458 | ||
459 | void NodeHelper::mergeExtraNodes(KMime::Content *node) | ||
460 | { | ||
461 | if (!node) { | ||
462 | return; | ||
463 | } | ||
464 | |||
465 | const QList<KMime::Content * > extraNodes = extraContents(node); | ||
466 | for (KMime::Content *extra : extraNodes) { | ||
467 | if (node->bodyIsMessage()) { | ||
468 | qCWarning(MIMETREEPARSER_LOG) << "Asked to attach extra content to a kmime::message, this does not make sense. Attaching to:" << node << | ||
469 | node->encodedContent() << "\n====== with =======\n" << extra << extra->encodedContent(); | ||
470 | continue; | ||
471 | } | ||
472 | KMime::Content *c = new KMime::Content(node); | ||
473 | c->setContent(extra->encodedContent()); | ||
474 | c->parse(); | ||
475 | node->addContent(c); | ||
476 | } | ||
477 | |||
478 | Q_FOREACH (KMime::Content *child, node->contents()) { | ||
479 | mergeExtraNodes(child); | ||
480 | } | ||
481 | } | ||
482 | |||
483 | void NodeHelper::cleanFromExtraNodes(KMime::Content *node) | ||
484 | { | ||
485 | if (!node) { | ||
486 | return; | ||
487 | } | ||
488 | const QList<KMime::Content * > extraNodes = extraContents(node); | ||
489 | for (KMime::Content *extra : extraNodes) { | ||
490 | QByteArray s = extra->encodedContent(); | ||
491 | const auto children = node->contents(); | ||
492 | for (KMime::Content *c : children) { | ||
493 | if (c->encodedContent() == s) { | ||
494 | node->removeContent(c); | ||
495 | } | ||
496 | } | ||
497 | } | ||
498 | Q_FOREACH (KMime::Content *child, node->contents()) { | ||
499 | cleanFromExtraNodes(child); | ||
500 | } | ||
501 | } | ||
502 | |||
503 | KMime::Message *NodeHelper::messageWithExtraContent(KMime::Content *topLevelNode) | ||
504 | { | ||
505 | /*The merge is done in several steps: | ||
506 | 1) merge the extra nodes into topLevelNode | ||
507 | 2) copy the modified (merged) node tree into a new node tree | ||
508 | 3) restore the original node tree in topLevelNode by removing the extra nodes from it | ||
509 | |||
510 | The reason is that extra nodes are assigned by pointer value to the nodes in the original tree. | ||
511 | */ | ||
512 | if (!topLevelNode) { | ||
513 | return nullptr; | ||
514 | } | ||
515 | |||
516 | mergeExtraNodes(topLevelNode); | ||
517 | |||
518 | KMime::Message *m = new KMime::Message; | ||
519 | m->setContent(topLevelNode->encodedContent()); | ||
520 | m->parse(); | ||
521 | |||
522 | cleanFromExtraNodes(topLevelNode); | ||
523 | // qCDebug(MIMETREEPARSER_LOG) << "MESSAGE WITH EXTRA: " << m->encodedContent(); | ||
524 | // qCDebug(MIMETREEPARSER_LOG) << "MESSAGE WITHOUT EXTRA: " << topLevelNode->encodedContent(); | ||
525 | |||
526 | return m; | ||
527 | } | ||
528 | |||
529 | QVector<KMime::Content *> NodeHelper::attachmentsOfExtraContents() const | ||
530 | { | ||
531 | QVector<KMime::Content *> result; | ||
532 | for (auto it = mExtraContents.begin(); it != mExtraContents.end(); ++it) { | ||
533 | foreach (auto content, it.value()) { | ||
534 | if (KMime::isAttachment(content)) { | ||
535 | result.push_back(content); | ||
536 | } else { | ||
537 | result += content->attachments(); | ||
538 | } | ||
539 | } | ||
540 | } | ||
541 | return result; | ||
542 | } | ||
543 | |||
544 | } | 459 | } |
diff --git a/framework/src/domain/mime/mimetreeparser/nodehelper.h b/framework/src/domain/mime/mimetreeparser/nodehelper.h index 2891f3bd..bf472413 100644 --- a/framework/src/domain/mime/mimetreeparser/nodehelper.h +++ b/framework/src/domain/mime/mimetreeparser/nodehelper.h | |||
@@ -76,11 +76,6 @@ public: | |||
76 | /** Get the extra nodes attached to the @param topLevelNode and all sub-nodes of @param topLevelNode */ | 76 | /** Get the extra nodes attached to the @param topLevelNode and all sub-nodes of @param topLevelNode */ |
77 | QList<KMime::Content *> extraContents(KMime::Content *topLevelNode) const; | 77 | QList<KMime::Content *> extraContents(KMime::Content *topLevelNode) const; |
78 | 78 | ||
79 | /** Return a modified message (node tree) starting from @param topLevelNode that has the original nodes and the extra nodes. | ||
80 | The caller has the responsibility to delete the new message. | ||
81 | */ | ||
82 | KMime::Message *messageWithExtraContent(KMime::Content *topLevelNode); | ||
83 | |||
84 | /** Get a QTextCodec suitable for this message part */ | 79 | /** Get a QTextCodec suitable for this message part */ |
85 | const QTextCodec *codec(KMime::Content *node); | 80 | const QTextCodec *codec(KMime::Content *node); |
86 | 81 | ||
@@ -149,14 +144,6 @@ public: | |||
149 | 144 | ||
150 | QString fromAsString(KMime::Content *node) const; | 145 | QString fromAsString(KMime::Content *node) const; |
151 | 146 | ||
152 | /** | ||
153 | * Returns a list of attachments of attached extra content nodes. | ||
154 | * This is mainly useful is order to get attachments of encrypted messages. | ||
155 | * Note that this does not include attachments from the primary node tree. | ||
156 | * @see KMime::Content::attachments(). | ||
157 | */ | ||
158 | QVector<KMime::Content *> attachmentsOfExtraContents() const; | ||
159 | |||
160 | Q_SIGNALS: | 147 | Q_SIGNALS: |
161 | void update(MimeTreeParser::UpdateMode); | 148 | void update(MimeTreeParser::UpdateMode); |
162 | 149 | ||
@@ -165,9 +152,6 @@ private: | |||
165 | bool unencryptedMessage_helper(KMime::Content *node, QByteArray &resultingData, bool addHeaders, | 152 | bool unencryptedMessage_helper(KMime::Content *node, QByteArray &resultingData, bool addHeaders, |
166 | int recursionLevel = 1); | 153 | int recursionLevel = 1); |
167 | 154 | ||
168 | void mergeExtraNodes(KMime::Content *node); | ||
169 | void cleanFromExtraNodes(KMime::Content *node); | ||
170 | |||
171 | /** Creates a persistent index string that bridges the gap between the | 155 | /** Creates a persistent index string that bridges the gap between the |
172 | permanent nodes and the temporary ones. | 156 | permanent nodes and the temporary ones. |
173 | 157 | ||
diff --git a/framework/src/domain/mime/mimetreeparser/objecttreeparser.cpp b/framework/src/domain/mime/mimetreeparser/objecttreeparser.cpp index 914298b9..a37c8b27 100644 --- a/framework/src/domain/mime/mimetreeparser/objecttreeparser.cpp +++ b/framework/src/domain/mime/mimetreeparser/objecttreeparser.cpp | |||
@@ -278,10 +278,7 @@ QVector<MessagePart::Ptr> ObjectTreeParser::collectAttachmentParts() | |||
278 | return true; | 278 | return true; |
279 | }, | 279 | }, |
280 | [] (const MessagePartPtr &part) { | 280 | [] (const MessagePartPtr &part) { |
281 | if (const auto attachment = dynamic_cast<MimeTreeParser::AttachmentMessagePart*>(part.data())) { | 281 | return part->isAttachment(); |
282 | return true; | ||
283 | } | ||
284 | return false; | ||
285 | }); | 282 | }); |
286 | return contentParts; | 283 | return contentParts; |
287 | } | 284 | } |
@@ -296,7 +293,6 @@ void ObjectTreeParser::decryptParts() | |||
296 | } | 293 | } |
297 | return false; | 294 | return false; |
298 | }); | 295 | }); |
299 | print(); | ||
300 | ::collect(mParsedPart, | 296 | ::collect(mParsedPart, |
301 | [] (const MessagePartPtr &part) { return true; }, | 297 | [] (const MessagePartPtr &part) { return true; }, |
302 | [] (const MessagePartPtr &part) { | 298 | [] (const MessagePartPtr &part) { |