File: opendocumentcreator.cpp

package info (click to toggle)
kio-extras 4%3A22.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 10,768 kB
  • sloc: cpp: 26,898; ansic: 4,443; perl: 1,058; xml: 97; sh: 69; python: 28; makefile: 9
file content (76 lines) | stat: -rw-r--r-- 2,448 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * SPDX-FileCopyrightText: 2018-2019 Kai Uwe Broulik <kde@broulik.de>
 *
 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
 */

#include "opendocumentcreator.h"

#include "macros.h"

#include <QImage>
#include <QScopedPointer>
#include <QXmlStreamReader>

#include <KZip>

EXPORT_THUMBNAILER_WITH_JSON(OpenDocumentCreator, "opendocumentthumbnail.json")

OpenDocumentCreator::OpenDocumentCreator() = default;

OpenDocumentCreator::~OpenDocumentCreator() = default;

bool OpenDocumentCreator::create(const QString &path, int width, int height, QImage &image)
{
    Q_UNUSED(width);
    Q_UNUSED(height);

    KZip zip(path);
    if (!zip.open(QIODevice::ReadOnly)) {
        return false;
    }

    // Open Document
    const KArchiveEntry *entry = zip.directory()->entry(QStringLiteral("Thumbnails/thumbnail.png"));

    if (entry && entry->isFile()) {
        const KZipFileEntry *zipFileEntry = static_cast<const KZipFileEntry *>(entry);

        if (image.loadFromData(zipFileEntry->data(), "PNG")) {
            return true;
        }
    }

    // Open Packaging Conventions (e.g. Office "Open" XML)
    const KArchiveEntry *relsEntry = zip.directory()->entry(QStringLiteral("_rels/.rels"));
    if (relsEntry && relsEntry->isFile()) {
        const auto *relsFileEntry = static_cast<const KZipFileEntry *>(relsEntry);

        QScopedPointer<QIODevice> relsDevice(relsFileEntry->createDevice());

        QString thumbnailPath;

        QXmlStreamReader xml(relsDevice.data());
        while (!xml.atEnd() && !xml.hasError()) {
            xml.readNext();
            if (xml.isStartElement() && xml.name() == QLatin1String("Relationship")) {
                const auto attributes = xml.attributes();
                if (attributes.value(QStringLiteral("Type")) == QLatin1String("http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail")) {
                    thumbnailPath = attributes.value(QStringLiteral("Target")).toString();
                    break;
                }
            }
        }

        if (!thumbnailPath.isEmpty()) {
            const auto *thumbnailEntry = zip.directory()->entry(thumbnailPath);
            if (thumbnailEntry && thumbnailEntry->isFile()) {
                return image.loadFromData(static_cast<const KZipFileEntry *>(thumbnailEntry)->data());
            }
        }
    }

    return false;
}

#include "opendocumentcreator.moc"