File indexing completed on 2024-05-12 15:37:05

0001 /*
0002     SPDX-FileCopyrightText: 2013 Vishesh Handa <me@vhanda.in>
0003 
0004     Code adapted from kdegraphics-mobipocket/strigi/
0005     SPDX-FileCopyrightText: 2008 Jakub Stachowski <qbast@go2.pl>
0006 
0007     SPDX-License-Identifier: LGPL-2.1-or-later
0008 */
0009 
0010 
0011 #include "mobiextractor.h"
0012 
0013 #include <qmobipocket/mobipocket.h>
0014 
0015 #include <QFile>
0016 #include <QTextDocument>
0017 
0018 using namespace KFileMetaData;
0019 
0020 class QFileStream : public Mobipocket::Stream
0021 {
0022 public:
0023     QFileStream(const QString& name) : d(name) {
0024         d.open(QIODevice::ReadOnly);
0025     }
0026     int read(char* buf, int size) override {
0027         return d.read(buf, size);
0028     }
0029     bool seek(int pos) override {
0030         return d.seek(pos);
0031     }
0032 private:
0033     QFile d;
0034 };
0035 
0036 MobiExtractor::MobiExtractor(QObject* parent)
0037     : ExtractorPlugin(parent)
0038 {
0039 
0040 }
0041 
0042 static const QStringList supportedMimeTypes =
0043 {
0044     QStringLiteral("application/x-mobipocket-ebook"),
0045 };
0046 
0047 QStringList MobiExtractor::mimetypes() const
0048 {
0049     return supportedMimeTypes;
0050 }
0051 
0052 void MobiExtractor::extract(ExtractionResult* result)
0053 {
0054     QFileStream stream(result->inputUrl());
0055     Mobipocket::Document doc(&stream);
0056     if (!doc.isValid())
0057         return;
0058 
0059     result->addType(Type::Document);
0060 
0061     if (result->inputFlags() & ExtractionResult::ExtractMetaData) {
0062         QMapIterator<Mobipocket::Document::MetaKey, QString> it(doc.metadata());
0063         while (it.hasNext()) {
0064             it.next();
0065             switch (it.key()) {
0066             case Mobipocket::Document::Title:
0067                 result->add(Property::Title, it.value());
0068                 break;
0069             case Mobipocket::Document::Author: {
0070                 result->add(Property::Author, it.value());
0071                 break;
0072             }
0073             case Mobipocket::Document::Description: {
0074                 QTextDocument document;
0075                 document.setHtml(it.value());
0076 
0077                 QString plain = document.toPlainText();
0078                 if (!plain.isEmpty())
0079                     result->add(Property::Description, it.value());
0080                 break;
0081             }
0082             case Mobipocket::Document::Subject:
0083                 result->add(Property::Subject, it.value());
0084                 break;
0085             case Mobipocket::Document::Copyright:
0086                 result->add(Property::Copyright, it.value());
0087                 break;
0088             }
0089         }
0090     }
0091 
0092     if ((result->inputFlags() & ExtractionResult::Flag::ExtractPlainText) && !doc.hasDRM()) {
0093         QString html = doc.text();
0094 
0095         QTextDocument document;
0096         document.setHtml(html);
0097 
0098         result->append(document.toPlainText());
0099     }
0100 
0101 }
0102 
0103 #include "moc_mobiextractor.cpp"