Coverage Report

Created: 2026-08-17 07:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/kio-extras/thumbnail/opendocumentcreator.cpp
Line
Count
Source
1
/*
2
 * SPDX-FileCopyrightText: 2018-2019 Kai Uwe Broulik <kde@broulik.de>
3
 *
4
 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5
 */
6
7
#include "opendocumentcreator.h"
8
9
#include <QImage>
10
#include <QScopedPointer>
11
#include <QXmlStreamReader>
12
13
#include <KPluginFactory>
14
#include <KZip>
15
16
0
K_PLUGIN_CLASS_WITH_JSON(OpenDocumentCreator, "opendocumentthumbnail.json")
Unexecuted instantiation: opendocumentthumbnail_factory::tr(char const*, char const*, int)
Unexecuted instantiation: opendocumentthumbnail_factory::~opendocumentthumbnail_factory()
17
0
18
0
OpenDocumentCreator::OpenDocumentCreator(QObject *parent, const QVariantList &args)
19
22.4k
    : KIO::ThumbnailCreator(parent, args)
20
22.4k
{
21
22.4k
}
22
23
22.4k
OpenDocumentCreator::~OpenDocumentCreator() = default;
24
25
KIO::ThumbnailResult OpenDocumentCreator::create(const KIO::ThumbnailRequest &request)
26
22.4k
{
27
22.4k
    KZip zip(request.url().toLocalFile());
28
22.4k
    if (!zip.open(QIODevice::ReadOnly)) {
29
10.4k
        return KIO::ThumbnailResult::fail();
30
10.4k
    }
31
32
    // Open Document
33
12.0k
    const KArchiveEntry *entry = zip.directory()->entry(QStringLiteral("Thumbnails/thumbnail.png"));
34
35
12.0k
    if (entry && entry->isFile()) {
36
966
        const KZipFileEntry *zipFileEntry = static_cast<const KZipFileEntry *>(entry);
37
966
        QImage image;
38
966
        if (image.loadFromData(zipFileEntry->data(), "PNG")) {
39
38
            return KIO::ThumbnailResult::pass(image);
40
38
        }
41
966
    }
42
43
    // Open Packaging Conventions (e.g. Office "Open" XML)
44
11.9k
    const KArchiveEntry *relsEntry = zip.directory()->entry(QStringLiteral("_rels/.rels"));
45
11.9k
    if (relsEntry && relsEntry->isFile()) {
46
10.9k
        const auto *relsFileEntry = static_cast<const KZipFileEntry *>(relsEntry);
47
48
10.9k
        QScopedPointer<QIODevice> relsDevice(relsFileEntry->createDevice());
49
50
10.9k
        QString thumbnailPath;
51
52
10.9k
        QXmlStreamReader xml(relsDevice.data());
53
12.5M
        while (!xml.atEnd() && !xml.hasError()) {
54
12.5M
            xml.readNext();
55
12.5M
            if (xml.isStartElement() && xml.name() == QLatin1String("Relationship")) {
56
129
                const auto attributes = xml.attributes();
57
129
                if (attributes.value(QStringLiteral("Type"))
58
129
                    == QLatin1String("http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail")) {
59
0
                    thumbnailPath = attributes.value(QStringLiteral("Target")).toString();
60
0
                    break;
61
0
                }
62
129
            }
63
12.5M
        }
64
65
10.9k
        if (!thumbnailPath.isEmpty()) {
66
0
            const auto *thumbnailEntry = zip.directory()->entry(thumbnailPath);
67
0
            if (thumbnailEntry && thumbnailEntry->isFile()) {
68
0
                QImage image;
69
0
                image.loadFromData(static_cast<const KZipFileEntry *>(thumbnailEntry)->data());
70
0
                return KIO::ThumbnailResult::pass(image);
71
0
            }
72
0
        }
73
10.9k
    }
74
75
11.9k
    return KIO::ThumbnailResult::fail();
76
11.9k
}
77
78
#include "moc_opendocumentcreator.cpp"
79
#include "opendocumentcreator.moc"