Coverage Report

Created: 2026-07-15 07:31

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
20.5k
    : KIO::ThumbnailCreator(parent, args)
20
20.5k
{
21
20.5k
}
22
23
20.5k
OpenDocumentCreator::~OpenDocumentCreator() = default;
24
25
KIO::ThumbnailResult OpenDocumentCreator::create(const KIO::ThumbnailRequest &request)
26
20.5k
{
27
20.5k
    KZip zip(request.url().toLocalFile());
28
20.5k
    if (!zip.open(QIODevice::ReadOnly)) {
29
9.70k
        return KIO::ThumbnailResult::fail();
30
9.70k
    }
31
32
    // Open Document
33
10.8k
    const KArchiveEntry *entry = zip.directory()->entry(QStringLiteral("Thumbnails/thumbnail.png"));
34
35
10.8k
    if (entry && entry->isFile()) {
36
875
        const KZipFileEntry *zipFileEntry = static_cast<const KZipFileEntry *>(entry);
37
875
        QImage image;
38
875
        if (image.loadFromData(zipFileEntry->data(), "PNG")) {
39
40
            return KIO::ThumbnailResult::pass(image);
40
40
        }
41
875
    }
42
43
    // Open Packaging Conventions (e.g. Office "Open" XML)
44
10.8k
    const KArchiveEntry *relsEntry = zip.directory()->entry(QStringLiteral("_rels/.rels"));
45
10.8k
    if (relsEntry && relsEntry->isFile()) {
46
9.90k
        const auto *relsFileEntry = static_cast<const KZipFileEntry *>(relsEntry);
47
48
9.90k
        QScopedPointer<QIODevice> relsDevice(relsFileEntry->createDevice());
49
50
9.90k
        QString thumbnailPath;
51
52
9.90k
        QXmlStreamReader xml(relsDevice.data());
53
8.61M
        while (!xml.atEnd() && !xml.hasError()) {
54
8.60M
            xml.readNext();
55
8.60M
            if (xml.isStartElement() && xml.name() == QLatin1String("Relationship")) {
56
148
                const auto attributes = xml.attributes();
57
148
                if (attributes.value(QStringLiteral("Type"))
58
148
                    == QLatin1String("http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail")) {
59
0
                    thumbnailPath = attributes.value(QStringLiteral("Target")).toString();
60
0
                    break;
61
0
                }
62
148
            }
63
8.60M
        }
64
65
9.90k
        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
9.90k
    }
74
75
10.8k
    return KIO::ThumbnailResult::fail();
76
10.8k
}
77
78
#include "moc_opendocumentcreator.cpp"
79
#include "opendocumentcreator.moc"