KFileMetaData

mobiextractor.cpp
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 "mobiextractor.h"
12
13#include <qmobipocket/mobipocket.h>
14
15#include <QFile>
16#include <QTextDocument>
17
18using namespace KFileMetaData;
19
20class QFileStream : public Mobipocket::Stream
21{
22public:
23 QFileStream(const QString& name) : d(name) {
25 }
26 int read(char* buf, int size) override {
27 return d.read(buf, size);
28 }
29 bool seek(int pos) override {
30 return d.seek(pos);
31 }
32private:
33 QFile d;
34};
35
36MobiExtractor::MobiExtractor(QObject* parent)
37 : ExtractorPlugin(parent)
38{
39
40}
41
42static const QStringList supportedMimeTypes =
43{
44 QStringLiteral("application/x-mobipocket-ebook"),
45};
46
47QStringList MobiExtractor::mimetypes() const
48{
49 return supportedMimeTypes;
50}
51
52void MobiExtractor::extract(ExtractionResult* result)
53{
54 QFileStream stream(result->inputUrl());
55 Mobipocket::Document doc(&stream);
56 if (!doc.isValid())
57 return;
58
59 result->addType(Type::Document);
60
61 if (result->inputFlags() & ExtractionResult::ExtractMetaData) {
63 while (it.hasNext()) {
64 it.next();
65 switch (it.key()) {
66 case Mobipocket::Document::Title:
67 result->add(Property::Title, it.value());
68 break;
69 case Mobipocket::Document::Author: {
70 result->add(Property::Author, it.value());
71 break;
72 }
73 case Mobipocket::Document::Description: {
74 QTextDocument document;
75 document.setHtml(it.value());
76
77 QString plain = document.toPlainText();
78 if (!plain.isEmpty())
79 result->add(Property::Description, it.value());
80 break;
81 }
82 case Mobipocket::Document::Subject:
83 result->add(Property::Subject, it.value());
84 break;
85 case Mobipocket::Document::Copyright:
86 result->add(Property::Copyright, it.value());
87 break;
88 }
89 }
90 }
91
92 if ((result->inputFlags() & ExtractionResult::Flag::ExtractPlainText) && !doc.hasDRM()) {
93 QString html = doc.text();
94
95 QTextDocument document;
96 document.setHtml(html);
97
98 result->append(document.toPlainText());
99 }
100
101}
102
103#include "moc_mobiextractor.cpp"
The ExtractionResult class is where all the data extracted by the indexer is saved.
QString inputUrl() const
The input URL which the plugins will use to locate the file.
virtual void addType(Type::Type type)=0
This function is called by the plugins.
virtual void add(Property::Property property, const QVariant &value)=0
This function is called by the plugins when they wish to add a key value pair which should be indexed...
Flags inputFlags() const
The flags which the extraction plugin should considering following when extracting metadata from the ...
virtual void append(const QString &text)=0
This function is called by plugins when they wish for some plain text to be indexed without any prope...
The ExtractorPlugin is the base class for all file metadata extractors.
@ Subject
Refers to the subject of the file.
Definition properties.h:127
@ Title
Refers to the Title of the content of the file.
Definition properties.h:121
@ Author
The Author field indicated the primary creator of a document.
Definition properties.h:114
@ Description
Represents the description stored in the file.
Definition properties.h:351
@ Copyright
The copyright of the file.
Definition properties.h:164
@ Document
Any file which counts as a document.
Definition types.h:63
The KFileMetaData namespace.
QString name(StandardAction id)
bool open(FILE *fh, OpenMode mode, FileHandleFlags handleFlags)
virtual bool seek(qint64 pos) override
QByteArray read(qint64 maxSize)
bool isEmpty() const const
void setHtml(const QString &html)
QString toPlainText() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:41 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.