summaryrefslogtreecommitdiffstats
path: root/framework/src/domain/mime/mimetreeparser/nodehelper.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'framework/src/domain/mime/mimetreeparser/nodehelper.cpp')
-rw-r--r--framework/src/domain/mime/mimetreeparser/nodehelper.cpp189
1 files changed, 9 insertions, 180 deletions
diff --git a/framework/src/domain/mime/mimetreeparser/nodehelper.cpp b/framework/src/domain/mime/mimetreeparser/nodehelper.cpp
index 4e60fa68..bbd705f9 100644
--- a/framework/src/domain/mime/mimetreeparser/nodehelper.cpp
+++ b/framework/src/domain/mime/mimetreeparser/nodehelper.cpp
@@ -46,8 +46,7 @@
46namespace MimeTreeParser 46namespace MimeTreeParser
47{ 47{
48 48
49NodeHelper::NodeHelper() : 49NodeHelper::NodeHelper()
50 mAttachmentFilesDir(new AttachmentTemporaryFilesDirs())
51{ 50{
52 //TODO(Andras) add methods to modify these prefixes 51 //TODO(Andras) add methods to modify these prefixes
53 52
@@ -78,11 +77,6 @@ NodeHelper::NodeHelper() :
78 77
79NodeHelper::~NodeHelper() 78NodeHelper::~NodeHelper()
80{ 79{
81 if (mAttachmentFilesDir) {
82 mAttachmentFilesDir->forceCleanTempFiles();
83 delete mAttachmentFilesDir;
84 mAttachmentFilesDir = nullptr;
85 }
86 clear(); 80 clear();
87} 81}
88 82
@@ -109,20 +103,6 @@ void NodeHelper::setNodeUnprocessed(KMime::Content *node, bool recurse)
109 } 103 }
110 mProcessedNodes.removeAll(node); 104 mProcessedNodes.removeAll(node);
111 105
112 //avoid double addition of extra nodes, eg. encrypted attachments
113 const QMap<KMime::Content *, QList<KMime::Content *> >::iterator it = mExtraContents.find(node);
114 if (it != mExtraContents.end()) {
115 Q_FOREACH (KMime::Content *c, it.value()) {
116 KMime::Content *p = c->parent();
117 if (p) {
118 p->removeContent(c);
119 }
120 }
121 qDeleteAll(it.value());
122 qCDebug(MIMETREEPARSER_LOG) << "mExtraContents deleted for" << it.key();
123 mExtraContents.erase(it);
124 }
125
126 qCDebug(MIMETREEPARSER_LOG) << "Node UNprocessed: " << node; 106 qCDebug(MIMETREEPARSER_LOG) << "Node UNprocessed: " << node;
127 if (recurse) { 107 if (recurse) {
128 const auto contents = node->contents(); 108 const auto contents = node->contents();
@@ -144,19 +124,6 @@ void NodeHelper::clear()
144{ 124{
145 mProcessedNodes.clear(); 125 mProcessedNodes.clear();
146 mOverrideCodecs.clear(); 126 mOverrideCodecs.clear();
147 QMap<KMime::Content *, QList<KMime::Content *> >::ConstIterator end(mExtraContents.constEnd());
148
149 for (QMap<KMime::Content *, QList<KMime::Content *> >::ConstIterator it = mExtraContents.constBegin(); it != end; ++it) {
150 Q_FOREACH (KMime::Content *c, it.value()) {
151 KMime::Content *p = c->parent();
152 if (p) {
153 p->removeContent(c);
154 }
155 }
156 qDeleteAll(it.value());
157 qCDebug(MIMETREEPARSER_LOG) << "mExtraContents deleted for" << it.key();
158 }
159 mExtraContents.clear();
160} 127}
161 128
162 129
@@ -170,25 +137,6 @@ void NodeHelper::setPartMetaData(KMime::Content *node, const PartMetaData &metaD
170 mPartMetaDatas.insert(node, metaData); 137 mPartMetaDatas.insert(node, metaData);
171} 138}
172 139
173void NodeHelper::forceCleanTempFiles()
174{
175 mAttachmentFilesDir->forceCleanTempFiles();
176 delete mAttachmentFilesDir;
177 mAttachmentFilesDir = nullptr;
178}
179
180void NodeHelper::removeTempFiles()
181{
182 //Don't delete it it will delete in class
183 mAttachmentFilesDir->removeTempFiles();
184 mAttachmentFilesDir = new AttachmentTemporaryFilesDirs();
185}
186
187void NodeHelper::addTempFile(const QString &file)
188{
189 mAttachmentFilesDir->addTempFile(file);
190}
191
192bool NodeHelper::isInEncapsulatedMessage(KMime::Content *node) 140bool NodeHelper::isInEncapsulatedMessage(KMime::Content *node)
193{ 141{
194 const KMime::Content *const topLevel = node->topLevel(); 142 const KMime::Content *const topLevel = node->topLevel();
@@ -278,114 +226,6 @@ QString NodeHelper::fileName(const KMime::Content *node)
278 return name; 226 return name;
279} 227}
280 228
281/*!
282 Creates a persistent index string that bridges the gap between the
283 permanent nodes and the temporary ones.
284
285 Used internally for robust indexing.
286*/
287QString NodeHelper::persistentIndex(const KMime::Content *node) const
288{
289 if (!node) {
290 return QString();
291 }
292
293 QString indexStr = node->index().toString();
294 if (indexStr.isEmpty()) {
295 QMapIterator<KMime::Message::Content *, QList<KMime::Content *> > it(mExtraContents);
296 while (it.hasNext()) {
297 it.next();
298 const auto &extraNodes = it.value();
299 for (int i = 0; i < extraNodes.size(); i++) {
300 if (extraNodes[i] == node) {
301 indexStr = QString::fromLatin1("e%1").arg(i);
302 const QString parentIndex = persistentIndex(it.key());
303 if (!parentIndex.isEmpty()) {
304 indexStr = QString::fromLatin1("%1:%2").arg(parentIndex, indexStr);
305 }
306 qWarning() << "Persistentindex: " << indexStr;
307 return indexStr;
308 }
309 }
310 }
311 } else {
312 const KMime::Content *const topLevel = node->topLevel();
313 //if the node is an extra node, prepend the index of the extra node to the url
314 QMapIterator<KMime::Message::Content *, QList<KMime::Content *> > it(mExtraContents);
315 while (it.hasNext()) {
316 it.next();
317 const QList<KMime::Content *> &extraNodes = extraContents(it.key());
318 for (int i = 0; i < extraNodes.size(); ++i) {
319 KMime::Content *const extraNode = extraNodes[i];
320 if (topLevel == extraNode) {
321 indexStr.prepend(QStringLiteral("e%1:").arg(i));
322 const QString parentIndex = persistentIndex(it.key());
323 if (!parentIndex.isEmpty()) {
324 indexStr = QStringLiteral("%1:%2").arg(parentIndex, indexStr);
325 }
326 qWarning() << "Persistentindex: " << indexStr;
327 return indexStr;
328 }
329 }
330 }
331 }
332
333 qWarning() << "Persistentindex: " << indexStr;
334 return indexStr;
335}
336
337KMime::Content *NodeHelper::contentFromIndex(KMime::Content *node, const QString &persistentIndex) const
338{
339 KMime::Content *c = node->topLevel();
340 if (c) {
341 const QStringList pathParts = persistentIndex.split(QLatin1Char(':'), QString::SkipEmptyParts);
342 const int pathPartsSize(pathParts.size());
343 for (int i = 0; i < pathPartsSize; ++i) {
344 const QString &path = pathParts[i];
345 if (path.startsWith(QLatin1Char('e'))) {
346 const QList<KMime::Content *> &extraParts = mExtraContents.value(c);
347 const int idx = path.midRef(1, -1).toInt();
348 c = (idx < extraParts.size()) ? extraParts[idx] : nullptr;
349 } else {
350 c = c->content(KMime::ContentIndex(path));
351 }
352 if (!c) {
353 break;
354 }
355 }
356 }
357 return c;
358}
359
360QString NodeHelper::asHREF(const KMime::Content *node, const QString &place) const
361{
362 return QStringLiteral("attachment:%1?place=%2").arg(persistentIndex(node), place);
363}
364
365KMime::Content *NodeHelper::fromHREF(const KMime::Message::Ptr &mMessage, const QUrl &url) const
366{
367 if (url.isEmpty()) {
368 return mMessage.data();
369 }
370
371 if (!url.isLocalFile()) {
372 return contentFromIndex(mMessage.data(), url.adjusted(QUrl::StripTrailingSlash).path());
373 } else {
374 const QString path = url.toLocalFile();
375 // extract from /<path>/qttestn28554.index.2.3:0:2/unnamed -> "2.3:0:2"
376 // start of the index is something that is not a number followed by a dot: \D.
377 // index is only made of numbers,"." and ":": ([0-9.:]+)
378 // index is the last part of the folder name: /
379 const QRegExp rIndex(QStringLiteral("\\D\\.([e0-9.:]+)/"));
380
381 //search the occurence at most at the end
382 if (rIndex.lastIndexIn(path) != -1) {
383 return contentFromIndex(mMessage.data(), rIndex.cap(1));
384 }
385 return mMessage.data();
386 }
387}
388
389QString NodeHelper::fixEncoding(const QString &encoding) 229QString NodeHelper::fixEncoding(const QString &encoding)
390{ 230{
391 QString returnEncoding = encoding; 231 QString returnEncoding = encoding;
@@ -432,28 +272,17 @@ QString NodeHelper::fromAsString(KMime::Content *node) const
432{ 272{
433 if (auto topLevel = dynamic_cast<KMime::Message *>(node->topLevel())) { 273 if (auto topLevel = dynamic_cast<KMime::Message *>(node->topLevel())) {
434 return topLevel->from()->asUnicodeString(); 274 return topLevel->from()->asUnicodeString();
435 } else { 275 // } else {
436 auto realNode = std::find_if(mExtraContents.cbegin(), mExtraContents.cend(), 276 // auto realNode = std::find_if(mExtraContents.cbegin(), mExtraContents.cend(),
437 [node](const QList<KMime::Content *> &nodes) { 277 // [node](const QList<KMime::Content *> &nodes) {
438 return nodes.contains(node); 278 // return nodes.contains(node);
439 }); 279 // });
440 if (realNode != mExtraContents.cend()) { 280 // if (realNode != mExtraContents.cend()) {
441 return fromAsString(realNode.key()); 281 // return fromAsString(realNode.key());
442 } 282 // }
443 } 283 }
444 284
445 return QString(); 285 return QString();
446} 286}
447 287
448void NodeHelper::attachExtraContent(KMime::Content *topLevelNode, KMime::Content *content)
449{
450 qCDebug(MIMETREEPARSER_LOG) << "mExtraContents added for" << topLevelNode << " extra content: " << content;
451 mExtraContents[topLevelNode].append(content);
452}
453
454QList< KMime::Content * > NodeHelper::extraContents(KMime::Content *topLevelnode) const
455{
456 return mExtraContents.value(topLevelnode);
457}
458
459} 288}