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