Changeset View
Changeset View
Standalone View
Standalone View
thumbnail/ebookcreator.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (c) 2019 Kai Uwe Broulik <kde@broulik.de> | ||||
3 | * | ||||
4 | * This library is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU Lesser General Public | ||||
6 | * License as published by the Free Software Foundation; either | ||||
7 | * version 2.1 of the License, or (at your option) version 3, or any | ||||
8 | * later version accepted by the membership of KDE e.V. (or its | ||||
9 | * successor approved by the membership of KDE e.V.), which shall | ||||
10 | * act as a proxy defined in Section 6 of version 3 of the license. | ||||
11 | * | ||||
12 | * This library is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
15 | * Lesser General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU Lesser General Public | ||||
18 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "ebookcreator.h" | ||||
22 | | ||||
23 | #include <QFile> | ||||
24 | #include <QImage> | ||||
25 | #include <QMimeDatabase> | ||||
26 | #include <QXmlStreamReader> | ||||
27 | | ||||
28 | #include <KZip> | ||||
29 | | ||||
30 | extern "C" | ||||
31 | { | ||||
32 | Q_DECL_EXPORT ThumbCreator *new_creator() | ||||
33 | { | ||||
34 | return new EbookCreator; | ||||
35 | } | ||||
36 | } | ||||
37 | | ||||
38 | EbookCreator::EbookCreator() = default; | ||||
39 | | ||||
40 | EbookCreator::~EbookCreator() = default; | ||||
41 | | ||||
42 | bool EbookCreator::create(const QString &path, int width, int height, QImage &image) | ||||
43 | { | ||||
44 | Q_UNUSED(width); | ||||
45 | Q_UNUSED(height); | ||||
46 | | ||||
47 | QMimeType mimeType = QMimeDatabase().mimeTypeForFile(path); | ||||
48 | | ||||
49 | if (mimeType.name() == QLatin1String("application/epub+zip")) { | ||||
50 | return createEpub(path, image); | ||||
51 | | ||||
52 | } else if (mimeType.name() == QLatin1String("application/x-fictionbook+xml")) { | ||||
53 | QFile file(path); | ||||
54 | if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { | ||||
55 | return false; | ||||
56 | } | ||||
57 | | ||||
58 | return createFb2(&file, image); | ||||
59 | | ||||
60 | } else if (mimeType.name() == QLatin1String("application/x-zip-compressed-fb2")) { | ||||
61 | KZip zip(path); | ||||
62 | if (!zip.open(QIODevice::ReadOnly)) { | ||||
63 | return false; | ||||
64 | } | ||||
65 | | ||||
66 | QScopedPointer<QIODevice> zipDevice; | ||||
67 | | ||||
68 | const auto entries = zip.directory()->entries(); | ||||
69 | for (const QString &entryPath : entries) { | ||||
70 | if (entries.count() > 1 && !entryPath.endsWith(QLatin1String(".fb2"))) { // can this done a bit more cleverly? | ||||
71 | continue; | ||||
72 | } | ||||
73 | | ||||
74 | const auto *entry = zip.directory()->entry(entryPath); | ||||
75 | if (!entry || !entry->isFile()) { | ||||
76 | return false; | ||||
77 | } | ||||
78 | | ||||
79 | zipDevice.reset(static_cast<const KZipFileEntry *>(entry)->createDevice()); | ||||
80 | } | ||||
81 | | ||||
82 | return createFb2(zipDevice.data(), image); | ||||
83 | } | ||||
84 | | ||||
85 | return false; | ||||
86 | } | ||||
87 | | ||||
88 | bool EbookCreator::createEpub(const QString &path, QImage &image) | ||||
89 | { | ||||
90 | KZip zip(path); | ||||
91 | if (!zip.open(QIODevice::ReadOnly)) { | ||||
92 | return false; | ||||
93 | } | ||||
94 | | ||||
95 | QScopedPointer<QIODevice> zipDevice; | ||||
96 | QString opfPath; | ||||
97 | QString coverId; | ||||
98 | QString coverHref; | ||||
99 | | ||||
100 | // First figure out where the OPF file with metadata is | ||||
101 | const auto *entry = zip.directory()->entry(QStringLiteral("META-INF/container.xml")); | ||||
102 | | ||||
103 | if (!entry || !entry->isFile()) { | ||||
104 | return false; | ||||
105 | } | ||||
106 | | ||||
107 | const auto *zipEntry = static_cast<const KZipFileEntry *>(entry); | ||||
108 | | ||||
109 | zipDevice.reset(zipEntry->createDevice()); | ||||
110 | | ||||
111 | QXmlStreamReader xml(zipDevice.data()); | ||||
112 | while (!xml.atEnd() && !xml.hasError()) { | ||||
113 | xml.readNext(); | ||||
114 | | ||||
115 | if (xml.isStartElement() && xml.name() == QLatin1String("rootfile")) { | ||||
116 | opfPath = xml.attributes().value(QStringLiteral("full-path")).toString(); | ||||
117 | } | ||||
118 | } | ||||
119 | | ||||
120 | if (opfPath.isEmpty()) { | ||||
121 | return false; | ||||
122 | } | ||||
123 | | ||||
124 | // Now read the OPF file and look for a <meta name="cover" content="..."> | ||||
125 | entry = zip.directory()->entry(opfPath); | ||||
126 | if (!entry || !entry->isFile()) { | ||||
127 | return false; | ||||
128 | } | ||||
129 | | ||||
130 | zipEntry = static_cast<const KZipFileEntry *>(entry); | ||||
131 | | ||||
132 | zipDevice.reset(zipEntry->createDevice()); | ||||
133 | | ||||
134 | xml.setDevice(zipDevice.data()); | ||||
135 | | ||||
136 | bool inMetadata = false; | ||||
137 | | ||||
138 | while (!xml.atEnd() && !xml.hasError()) { | ||||
139 | xml.readNext(); | ||||
140 | | ||||
141 | if (xml.name() == QLatin1String("metadata")) { | ||||
142 | if (xml.isStartElement()) { | ||||
143 | inMetadata = true; | ||||
144 | } else if (xml.isEndElement()) { | ||||
145 | break; | ||||
146 | } | ||||
147 | } | ||||
148 | | ||||
149 | if (!inMetadata) { | ||||
150 | continue; | ||||
151 | } | ||||
152 | | ||||
153 | if (xml.isStartElement() && xml.name() == QLatin1String("meta")) { | ||||
154 | const auto attributes = xml.attributes(); | ||||
155 | if (attributes.value(QStringLiteral("name")) == QLatin1String("cover")) { | ||||
156 | coverId = attributes.value(QStringLiteral("content")).toString(); | ||||
157 | break; | ||||
158 | } | ||||
159 | } | ||||
160 | } | ||||
161 | | ||||
162 | if (coverId.isEmpty()) { | ||||
163 | // Maybe we're lucky and the archive contains an iTunesArtwork file from iBooks | ||||
164 | entry = zip.directory()->entry(QStringLiteral("iTunesArtwork")); | ||||
165 | if (entry && entry->isFile()) { | ||||
166 | return image.loadFromData(static_cast<const KZipFileEntry *>(entry)->data()); | ||||
167 | } | ||||
168 | | ||||
169 | // Maybe there's a file called "cover" somewhere | ||||
170 | const QStringList entries = getEntryList(zip.directory(), QString()); | ||||
171 | | ||||
172 | for (const QString &name : entries) { | ||||
173 | if (!name.contains(QLatin1String("cover"), Qt::CaseInsensitive)) { | ||||
174 | continue; | ||||
175 | } | ||||
176 | | ||||
177 | entry = zip.directory()->entry(name); | ||||
178 | if (!entry || !entry->isFile()) { | ||||
179 | continue; | ||||
180 | } | ||||
181 | | ||||
182 | if (image.loadFromData(static_cast<const KZipFileEntry *>(entry)->data())) { | ||||
183 | return true; | ||||
184 | } | ||||
185 | } | ||||
186 | } | ||||
187 | | ||||
188 | // Read OPF file again from beginning and look for <item id="our id from above" href="..."> | ||||
189 | zipDevice->seek(0); | ||||
190 | xml.setDevice(zipDevice.data()); | ||||
191 | | ||||
192 | bool inManifest = false; | ||||
193 | | ||||
194 | while (!xml.atEnd() && !xml.hasError()) { | ||||
195 | xml.readNext(); | ||||
196 | | ||||
197 | if (xml.name() == QLatin1String("manifest")) { | ||||
198 | if (xml.isStartElement()) { | ||||
199 | inManifest = true; | ||||
200 | } else if (xml.isEndElement()) { | ||||
201 | break; | ||||
202 | } | ||||
203 | } | ||||
204 | | ||||
205 | if (!inManifest) { | ||||
206 | continue; | ||||
207 | } | ||||
208 | | ||||
209 | if (xml.isStartElement() && xml.name() == QLatin1String("item")) { | ||||
210 | const auto attributes = xml.attributes(); | ||||
211 | if (attributes.value(QStringLiteral("id")).compare(coverId, Qt::CaseInsensitive) == 0) { | ||||
212 | coverHref = attributes.value(QStringLiteral("href")).toString(); | ||||
213 | break; | ||||
214 | } | ||||
215 | } | ||||
216 | } | ||||
217 | | ||||
218 | if (coverHref.isEmpty()) { | ||||
219 | return false; | ||||
220 | } | ||||
221 | | ||||
222 | // Make coverHref relative to OPF location | ||||
223 | const int lastOpfSlash = opfPath.lastIndexOf(QLatin1Char('/')); | ||||
224 | if (lastOpfSlash > -1) { | ||||
225 | QString basePath = opfPath.left(lastOpfSlash + 1); | ||||
226 | coverHref.prepend(basePath); | ||||
227 | } | ||||
228 | | ||||
229 | // Finally, just load the cover image file | ||||
230 | entry = zip.directory()->entry(coverHref); | ||||
231 | if (!entry || !entry->isFile()) { | ||||
232 | return false; | ||||
233 | } | ||||
234 | | ||||
235 | return image.loadFromData(static_cast<const KZipFileEntry *>(entry)->data()); | ||||
236 | } | ||||
237 | | ||||
238 | bool EbookCreator::createFb2(QIODevice *device, QImage &image) | ||||
239 | { | ||||
240 | QString coverId; | ||||
241 | | ||||
242 | QXmlStreamReader xml(device); | ||||
243 | | ||||
244 | bool inFictionBook = false; | ||||
245 | bool inDescription = false; | ||||
246 | bool inTitleInfo = false; | ||||
247 | bool inCoverPage = false; | ||||
248 | | ||||
249 | while (!xml.atEnd() && !xml.hasError()) { | ||||
250 | xml.readNext(); | ||||
251 | | ||||
252 | if (xml.name() == QLatin1String("FictionBook")) { | ||||
253 | if (xml.isStartElement()) { | ||||
254 | inFictionBook = true; | ||||
255 | } else if (xml.isEndElement()) { | ||||
256 | break; | ||||
257 | } | ||||
258 | } else if (xml.name() == QLatin1String("description")) { | ||||
259 | if (xml.isStartElement()) { | ||||
260 | inDescription = true; | ||||
261 | } else if (xml.isEndElement()) { | ||||
262 | inDescription = false; | ||||
263 | } | ||||
264 | } else if (xml.name() == QLatin1String("title-info")) { | ||||
265 | if (xml.isStartElement()) { | ||||
266 | inTitleInfo = true; | ||||
267 | } else if (xml.isEndElement()) { | ||||
268 | inTitleInfo = false; | ||||
269 | } | ||||
270 | } else if (xml.name() == QLatin1String("coverpage")) { | ||||
271 | if (xml.isStartElement()) { | ||||
272 | inCoverPage = true; | ||||
273 | } else if (xml.isEndElement()) { | ||||
274 | inCoverPage = false; | ||||
275 | } | ||||
276 | } | ||||
277 | | ||||
278 | if (!inFictionBook) { | ||||
279 | continue; | ||||
280 | } | ||||
281 | | ||||
282 | if (inDescription) { | ||||
283 | if (inTitleInfo && inCoverPage) { | ||||
284 | if (xml.isStartElement() && xml.name() == QLatin1String("image")) { | ||||
285 | const auto attributes = xml.attributes(); | ||||
286 | | ||||
287 | // value() wants a namespace but we don't care, so iterate until we find any "href" | ||||
288 | for (const auto &attribute : attributes) { | ||||
289 | if (attribute.name() == QLatin1String("href")) { | ||||
290 | coverId = attribute.value().toString(); | ||||
291 | if (coverId.startsWith(QLatin1Char('#'))) { | ||||
292 | coverId = coverId.mid(1); | ||||
293 | } | ||||
294 | } | ||||
295 | } | ||||
296 | } | ||||
297 | } | ||||
298 | } else { | ||||
299 | if (!coverId.isEmpty() && xml.isStartElement() && xml.name() == QLatin1String("binary")) { | ||||
300 | if (xml.attributes().value(QStringLiteral("id")) == coverId) { | ||||
301 | return image.loadFromData(QByteArray::fromBase64(xml.readElementText().toLatin1())); | ||||
302 | } | ||||
303 | } | ||||
304 | } | ||||
305 | } | ||||
306 | | ||||
307 | return false; | ||||
308 | } | ||||
309 | | ||||
310 | QStringList EbookCreator::getEntryList(const KArchiveDirectory *dir, const QString &path) | ||||
311 | { | ||||
312 | QStringList list; | ||||
313 | | ||||
314 | const QStringList entries = dir->entries(); | ||||
315 | for (const QString &name : entries) { | ||||
316 | const KArchiveEntry *entry = dir->entry(name); | ||||
317 | | ||||
318 | QString fullPath = name; | ||||
319 | | ||||
320 | if (!path.isEmpty()) { | ||||
321 | fullPath.prepend(QLatin1Char('/')); | ||||
322 | fullPath.prepend(path); | ||||
323 | } | ||||
324 | | ||||
325 | if (entry->isFile()) { | ||||
326 | list << fullPath; | ||||
327 | } else { | ||||
328 | list << getEntryList(static_cast<const KArchiveDirectory *>(entry), fullPath); | ||||
329 | } | ||||
330 | } | ||||
331 | | ||||
332 | return list; | ||||
333 | } |