| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2013 Vishesh Handa <me@vhanda.in> |
| 3 | |
| 4 | Code adapted from kdegraphics-mobipocket/strigi/ |
| 5 | SPDX-FileCopyrightText: 2008 Jakub Stachowski <qbast@go2.pl> |
| 6 | |
| 7 | SPDX-License-Identifier: LGPL-2.1-or-later |
| 8 | */ |
| 9 | |
| 10 | |
| 11 | #include "kfilemetadata_debug.h" |
| 12 | #include "mobiextractor.h" |
| 13 | |
| 14 | #include <qmobipocket_version.h> |
| 15 | #include <qmobipocket/mobipocket.h> |
| 16 | |
| 17 | #include <QFile> |
| 18 | #include <QTextDocument> |
| 19 | |
| 20 | using namespace KFileMetaData; |
| 21 | |
| 22 | #if QMOBIPOCKET_VERSION_MAJOR < 3 |
| 23 | class QFileStream : public Mobipocket::Stream |
| 24 | { |
| 25 | public: |
| 26 | QFileStream(const QString& name) : d(name) { |
| 27 | d.open(QIODevice::ReadOnly); |
| 28 | } |
| 29 | int read(char* buf, int size) override { |
| 30 | return d.read(buf, size); |
| 31 | } |
| 32 | bool seek(int pos) override { |
| 33 | return d.seek(pos); |
| 34 | } |
| 35 | private: |
| 36 | QFile d; |
| 37 | }; |
| 38 | #endif |
| 39 | |
| 40 | MobiExtractor::(QObject* parent) |
| 41 | : ExtractorPlugin(parent) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | static const QStringList supportedMimeTypes = |
| 46 | { |
| 47 | QStringLiteral("application/x-mobipocket-ebook" ), |
| 48 | }; |
| 49 | |
| 50 | QStringList MobiExtractor::() const |
| 51 | { |
| 52 | return supportedMimeTypes; |
| 53 | } |
| 54 | |
| 55 | void MobiExtractor::(ExtractionResult* result) |
| 56 | { |
| 57 | #if QMOBIPOCKET_VERSION_MAJOR < 3 |
| 58 | QFileStream stream(result->inputUrl()); |
| 59 | Mobipocket::Document doc(&stream); |
| 60 | #else |
| 61 | QFile file(result->inputUrl()); |
| 62 | if (!file.open(flags: QIODevice::ReadOnly)) { |
| 63 | qCDebug(KFILEMETADATA_LOG) << "Invalid file:" << result->inputUrl(); |
| 64 | return; |
| 65 | } |
| 66 | Mobipocket::Document doc(&file); |
| 67 | #endif |
| 68 | |
| 69 | if (!doc.isValid()) { |
| 70 | qCDebug(KFILEMETADATA_LOG) << "Invalid file:" << result->inputUrl(); |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | result->addType(type: Type::Document); |
| 75 | |
| 76 | if (result->inputFlags() & ExtractionResult::ExtractMetaData) { |
| 77 | QMapIterator<Mobipocket::Document::MetaKey, QString> it(doc.metadata()); |
| 78 | while (it.hasNext()) { |
| 79 | it.next(); |
| 80 | switch (it.key()) { |
| 81 | case Mobipocket::Document::Title: |
| 82 | result->add(property: Property::Title, value: it.value()); |
| 83 | break; |
| 84 | case Mobipocket::Document::Author: { |
| 85 | result->add(property: Property::Author, value: it.value()); |
| 86 | break; |
| 87 | } |
| 88 | case Mobipocket::Document::Description: { |
| 89 | QTextDocument document; |
| 90 | document.setHtml(it.value()); |
| 91 | |
| 92 | QString plain = document.toPlainText(); |
| 93 | if (!plain.isEmpty()) |
| 94 | result->add(property: Property::Description, value: it.value()); |
| 95 | break; |
| 96 | } |
| 97 | case Mobipocket::Document::Subject: |
| 98 | result->add(property: Property::Subject, value: it.value()); |
| 99 | break; |
| 100 | case Mobipocket::Document::Copyright: |
| 101 | result->add(property: Property::Copyright, value: it.value()); |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | #if defined(ENABLE_TEXT_EXTRACTION) |
| 108 | if (result->inputFlags() & ExtractionResult::Flag::ExtractPlainText) { |
| 109 | if (doc.hasDRM()) { |
| 110 | qCDebug(KFILEMETADATA_LOG) << "Skip DRM protected content:" << result->inputUrl(); |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | QString html = doc.text(); |
| 115 | |
| 116 | QTextDocument document; |
| 117 | document.setHtml(html); |
| 118 | |
| 119 | result->append(document.toPlainText()); |
| 120 | } |
| 121 | #endif |
| 122 | } |
| 123 | |
| 124 | #include "moc_mobiextractor.cpp" |
| 125 | |