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/Content>
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 MessageCore::ImageCollector::ImageCollectorPrivate
37 {
38 public:
39  std::vector<KMime::Content *> mImages;
40 };
41 
43  : d(new ImageCollectorPrivate)
44 {
45 }
46 
48 
50 {
51  KMime::Content *parent = nullptr;
52 
53  while (node) {
54  parent = node->parent();
55 
56  if (node->topLevel()->textContent() == node) {
57  node = MessageCore::NodeHelper::next(node);
58  continue;
59  }
60 
61  if (isInExclusionList(node)) {
62  node = MessageCore::NodeHelper::next(node);
63  continue;
64  }
65 
66  if (parent && parent->contentType()->isMultipart() && parent->contentType(false)->subType() == "related") {
67  qCWarning(MESSAGECORE_LOG) << "Adding image" << node->contentID();
68  d->mImages.push_back(node);
69  node = MessageCore::NodeHelper::next(node); // skip embedded images
70  continue;
71  }
72 
73  node = MessageCore::NodeHelper::next(node);
74  }
75 }
76 
77 const std::vector<KMime::Content *> &MessageCore::ImageCollector::images() const
78 {
79  return d->mImages;
80 }
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-2021 The KDE developers.
Generated on Tue Nov 30 2021 23:05:46 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.