KFileMetaData

mobiextractor.cpp
1 /*
2  SPDX-FileCopyrightText: 2013 Vishesh Handa <[email protected]>
3 
4  Code adapted from kdegraphics-mobipocket/strigi/
5  SPDX-FileCopyrightText: 2008 Jakub Stachowski <[email protected]>
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 
18 using namespace KFileMetaData;
19 
20 class QFileStream : public Mobipocket::Stream
21 {
22 public:
23  QFileStream(const QString& name) : d(name) {
24  d.open(QIODevice::ReadOnly);
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  }
32 private:
33  QFile d;
34 };
35 
36 MobiExtractor::MobiExtractor(QObject* parent)
37  : ExtractorPlugin(parent)
38 {
39 
40 }
41 
42 static const QStringList supportedMimeTypes =
43 {
44  QStringLiteral("application/x-mobipocket-ebook"),
45 };
46 
47 QStringList MobiExtractor::mimetypes() const
48 {
49  return supportedMimeTypes;
50 }
51 
52 void 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 
62  while (it.hasNext()) {
63  it.next();
64  switch (it.key()) {
65  case Mobipocket::Document::Title:
66  result->add(Property::Title, it.value());
67  break;
68  case Mobipocket::Document::Author: {
69  result->add(Property::Author, it.value());
70  break;
71  }
72  case Mobipocket::Document::Description: {
73  QTextDocument document;
74  document.setHtml(it.value());
75 
76  QString plain = document.toPlainText();
77  if (!plain.isEmpty())
78  result->add(Property::Description, it.value());
79  break;
80  }
81  case Mobipocket::Document::Subject:
82  result->add(Property::Subject, it.value());
83  break;
84  case Mobipocket::Document::Copyright:
85  result->add(Property::Copyright, it.value());
86  break;
87  }
88  }
89 
90  if (!doc.hasDRM()) {
91  QString html = doc.text();
92 
93  QTextDocument document;
94  document.setHtml(html);
95 
96  result->append(document.toPlainText());
97  }
98 
99 }
virtual void addType(Type::Type type)=0
This function is called by the plugins.
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.
QString toPlainText() const const
bool isEmpty() const const
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...
void setHtml(const QString &html)
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...
const char * name(StandardAction id)
QVariant read(const QByteArray &data, int versionOverride=0)
The ExtractorPlugin is the base class for all file metadata extractors. It is responsible for extract...
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu May 26 2022 03:46:07 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.