Messagelib

imagecollector.cpp
1 /* -*- c++ -*-
2  * imagecollector.cpp
3  *
4  * This file is part of KMail, the KDE mail client.
5  * SPDX-FileCopyrightText: 2004 Marc Mutz <[email protected]>
6  * SPDX-FileCopyrightText: 2011 Torgny Nyblom <[email protected]>
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10 
11 #include "imagecollector.h"
12 
13 #include "MessageCore/NodeHelper"
14 
15 #include "messagecore_debug.h"
16 #include <kmime/kmime_content.h>
17 
18 static bool isInExclusionList(KMime::Content *node)
19 {
20  if (!node) {
21  return true;
22  }
23 
24  auto ct = node->contentType(); //Create if necessary
25  if (ct->mediaType() != "image") {
26  return true;
27  }
28 
29  if (ct->isMultipart()) {
30  return true;
31  }
32 
33  return false;
34 }
35 
36 class Q_DECL_HIDDEN MessageCore::ImageCollector::Private
37 {
38 public:
39  std::vector<KMime::Content *> mImages;
40 };
41 
43  : d(new Private)
44 {
45 }
46 
48 {
49  delete d;
50 }
51 
53 {
54  KMime::Content *parent = nullptr;
55 
56  while (node) {
57  parent = node->parent();
58 
59  if (node->topLevel()->textContent() == node) {
60  node = MessageCore::NodeHelper::next(node);
61  continue;
62  }
63 
64  if (isInExclusionList(node)) {
65  node = MessageCore::NodeHelper::next(node);
66  continue;
67  }
68 
69  if (parent && parent->contentType()->isMultipart()
70  && parent->contentType()->subType() == "related") {
71  qCWarning(MESSAGECORE_LOG) << "Adding image" << node->contentID();
72  d->mImages.push_back(node);
73  node = MessageCore::NodeHelper::next(node); // skip embedded images
74  continue;
75  }
76 
77  node = MessageCore::NodeHelper::next(node);
78  }
79 }
80 
81 const std::vector<KMime::Content *> &MessageCore::ImageCollector::images() const
82 {
83  return d->mImages;
84 }
QByteArray subType() const
~ImageCollector()
Destroys the image collector.
void collectImagesFrom(KMime::Content *content)
Starts collecting the images.
MESSAGECORE_EXPORT KMime::Content * next(KMime::Content *node, bool allowChildren=true)
Returns the next node (child, sibling or parent) of the given node.
ImageCollector()
Creates a new image collector.
Headers::ContentID * contentID(bool create=true)
Headers::ContentType * contentType(bool create=true)
Content * topLevel() const
Content * textContent()
Content * parent() const
const std::vector< KMime::Content * > & images() const
Returns the collected images.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue Sep 29 2020 23:21:17 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.