Coverage Report

Created: 2026-03-12 07:14

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