Coverage Report

Created: 2026-10-28 07:09

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