KFileMetaData

popplerextractor.cpp
1 /*
2  <one line to give the library's name and an idea of what it does.>
3  Copyright (C) 2012 Vishesh Handa <[email protected]>
4  Copyright (C) 2012 Jörg Ehrichs <[email protected]>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 
21 
22 #include "popplerextractor.h"
23 
24 #include <QScopedPointer>
25 #include <QDebug>
26 
27 using namespace KFileMetaData;
28 
29 PopplerExtractor::PopplerExtractor(QObject* parent)
30  : ExtractorPlugin(parent)
31 {
32 
33 }
34 
35 const QStringList supportedMimeTypes = {
36  QStringLiteral("application/pdf"),
37 };
38 
39 QStringList PopplerExtractor::mimetypes() const
40 {
41  return supportedMimeTypes;
42 }
43 
44 void PopplerExtractor::extract(ExtractionResult* result)
45 {
46  const QString fileUrl = result->inputUrl();
47  QScopedPointer<Poppler::Document> pdfDoc(Poppler::Document::load(fileUrl, QByteArray(), QByteArray()));
48 
49  if (!pdfDoc || pdfDoc->isLocked()) {
50  return;
51  }
52 
53  result->addType(Type::Document);
54 
55  if (result->inputFlags() & ExtractionResult::ExtractMetaData) {
56  QString title = pdfDoc->info(QStringLiteral("Title")).trimmed();
57 
58  if (!title.isEmpty()) {
59  result->add(Property::Title, title);
60  }
61 
62  QString subject = pdfDoc->info(QStringLiteral("Subject"));
63  if (!subject.isEmpty()) {
64  result->add(Property::Subject, subject);
65  }
66 
67  QString author = pdfDoc->info(QStringLiteral("Author"));
68  if (!author.isEmpty()) {
69  result->add(Property::Author, author);
70  }
71 
72  QString generator = pdfDoc->info(QStringLiteral("Producer"));
73  if (!generator.isEmpty()) {
74  result->add(Property::Generator, generator);
75  }
76 
77  QString creationDate = pdfDoc->info(QStringLiteral("CreationDate"));
78  if (!creationDate.isEmpty()) {
79  QByteArray utf8 = creationDate.toUtf8();
80  result->add(Property::CreationDate, Poppler::convertDate(utf8.data()));
81  }
82  }
83 
84  if (!(result->inputFlags() & ExtractionResult::ExtractPlainText)) {
85  return;
86  }
87 
88  for (int i = 0; i < pdfDoc->numPages(); i++) {
89  QScopedPointer<Poppler::Page> page(pdfDoc->page(i));
90  if (!page) { // broken pdf files do not return a valid page
91  qWarning() << "Could not read page content from" << fileUrl;
92  break;
93  }
94  result->append(page->text(QRectF()));
95  }
96 }
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.
bool isEmpty() const const
QString trimmed() const const
QString subject() const
char * data()
The ExtractionResult class is where all the data extracted by the indexer is saved.
Flags inputFlags() const
The flags which the extraction plugin should considering following when extracting metadata from the ...
QString inputUrl() const
The input url which the plugins will use to locate the file.
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue May 26 2020 23:11:36 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.