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 <mutz@kde.org>
6 * SPDX-FileCopyrightText: 2011 Torgny Nyblom <nyblom@kde.org>
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
18static 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
36class MessageCore::ImageCollector::ImageCollectorPrivate
37{
38public:
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) {
58 continue;
59 }
60
61 if (isInExclusionList(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
74 }
75}
76
77const std::vector<KMime::Content *> &MessageCore::ImageCollector::images() const
78{
79 return d->mImages;
80}
Headers::ContentType * contentType(bool create=true)
Content * topLevel() const
Content * parent() const
Content * textContent()
Headers::ContentID * contentID(bool create=true)
QByteArray subType() const
void collectImagesFrom(KMime::Content *content)
Starts collecting the images.
ImageCollector()
Creates a new image collector.
const std::vector< KMime::Content * > & images() const
Returns the collected images.
~ImageCollector()
Destroys the image collector.
MESSAGECORE_EXPORT KMime::Content * next(KMime::Content *node, bool allowChildren=true)
Returns the next node (child, sibling or parent) of the given node.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 24 2024 11:55:43 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.