KFileMetaData

mobiextractor.cpp
1 /*
2  Copyright (C) 2013 Vishesh Handa <[email protected]>
3 
4  Code adapted from kdegraphics-mobipocket/strigi/
5  Copyright (C) 2008 by Jakub Stachowski <[email protected]>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2.1 of the License, or (at your option) any later version.
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, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 
22 
23 #include "mobiextractor.h"
24 
25 #include <qmobipocket/mobipocket.h>
26 
27 #include <QFile>
28 
29 using namespace KFileMetaData;
30 
31 class QFileStream : public Mobipocket::Stream
32 {
33 public:
34  QFileStream(const QString& name) : d(name) {
35  d.open(QIODevice::ReadOnly);
36  }
37  int read(char* buf, int size) override {
38  return d.read(buf, size);
39  }
40  bool seek(int pos) override {
41  return d.seek(pos);
42  }
43 private:
44  QFile d;
45 };
46 
47 MobiExtractor::MobiExtractor(QObject* parent)
48  : ExtractorPlugin(parent)
49 {
50 
51 }
52 
53 static const QStringList supportedMimeTypes =
54 {
55  QStringLiteral("application/x-mobipocket-ebook"),
56 };
57 
58 QStringList MobiExtractor::mimetypes() const
59 {
60  return supportedMimeTypes;
61 }
62 
63 void MobiExtractor::extract(ExtractionResult* result)
64 {
65  QFileStream stream(result->inputUrl());
66  Mobipocket::Document doc(&stream);
67  if (!doc.isValid())
68  return;
69 
70  result->addType(Type::Document);
71 
73  while (it.hasNext()) {
74  it.next();
75  switch (it.key()) {
76  case Mobipocket::Document::Title:
77  result->add(Property::Title, it.value());
78  break;
79  case Mobipocket::Document::Author: {
80  result->add(Property::Author, it.value());
81  break;
82  }
83  case Mobipocket::Document::Description: {
84  QTextDocument document;
85  document.setHtml(it.value());
86 
87  QString plain = document.toPlainText();
88  if (!plain.isEmpty())
89  result->add(Property::Description, it.value());
90  break;
91  }
92  case Mobipocket::Document::Subject:
93  result->add(Property::Subject, it.value());
94  break;
95  case Mobipocket::Document::Copyright:
96  result->add(Property::Copyright, it.value());
97  break;
98  }
99  }
100 
101  if (!doc.hasDRM()) {
102  QString html = doc.text();
103 
104  QTextDocument document;
105  document.setHtml(html);
106 
107  result->append(document.toPlainText());
108  }
109 
110 }
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...
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...
The ExtractorPlugin is the base class for all file metadata extractors.
virtual void addType(Type::Type type)=0
This function is called by the plugins.
QMapIterator::Item next()
bool isEmpty() const const
QString toPlainText() const const
void setHtml(const QString &html)
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.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon May 25 2020 23:11:16 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.