~ubuntu-branches/ubuntu/wily/kbibtex/wily

« back to all changes in this revision

Viewing changes to src/libkbibtexio/fileimporterpdf.cpp

  • Committer: Package Import Robot
  • Author(s): Michael Hanke
  • Date: 2011-07-18 09:29:48 UTC
  • mfrom: (1.1.6) (2.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20110718092948-ksxjmg7kdfamolmg
Tags: 0.3-1
* First upstream release for KDE4 (Closes: #634255). A number of search
  engines are still missing, in comparison to the 0.2 series.
* Bumped Standards-Version to 3.9.2, no changes necessary.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
*   Copyright (C) 2004-2010 by Thomas Fischer                             *
 
3
*   fischer@unix-ag.uni-kl.de                                             *
 
4
*                                                                         *
 
5
*   This program is free software; you can redistribute it and/or modify  *
 
6
*   it under the terms of the GNU General Public License as published by  *
 
7
*   the Free Software Foundation; either version 2 of the License, or     *
 
8
*   (at your option) any later version.                                   *
 
9
*                                                                         *
 
10
*   This program is distributed in the hope that it will be useful,       *
 
11
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
12
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
13
*   GNU General Public License for more details.                          *
 
14
*                                                                         *
 
15
*   You should have received a copy of the GNU General Public License     *
 
16
*   along with this program; if not, write to the                         *
 
17
*   Free Software Foundation, Inc.,                                       *
 
18
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 
19
***************************************************************************/
 
20
#include <QBuffer>
 
21
#include <QFile>
 
22
 
 
23
#include <KDebug>
 
24
#include <kio/netaccess.h>
 
25
 
 
26
#include <poppler-qt4.h>
 
27
 
 
28
#include "fileimporterpdf.h"
 
29
#include <file.h>
 
30
#include <fileimporterbibtex.h>
 
31
 
 
32
FileImporterPDF::FileImporterPDF()
 
33
{
 
34
    m_bibTeXimporter = new FileImporterBibTeX();
 
35
}
 
36
 
 
37
FileImporterPDF::~FileImporterPDF()
 
38
{
 
39
    delete m_bibTeXimporter;
 
40
}
 
41
 
 
42
File* FileImporterPDF::load(QIODevice *iodevice)
 
43
{
 
44
    m_cancelFlag = false;
 
45
    File* result = NULL;
 
46
    QByteArray buffer = iodevice->readAll();
 
47
 
 
48
    Poppler::Document *doc = Poppler::Document::loadFromData(buffer);
 
49
    if (doc == NULL) {
 
50
        kWarning() << "Could not load PDF document";
 
51
        return NULL;
 
52
    }
 
53
 
 
54
    if (doc->hasEmbeddedFiles()) {
 
55
        foreach(Poppler::EmbeddedFile *file, doc->embeddedFiles())
 
56
        if (file->name().endsWith(".bib")) {
 
57
            kDebug() << "filename is " << file->name();
 
58
            QByteArray data = file->data();
 
59
            QBuffer buffer(&data);
 
60
            FileImporterBibTeX bibTeXimporter;
 
61
            connect(&bibTeXimporter, SIGNAL(progress(int, int)), this, SIGNAL(progress(int, int)));
 
62
            buffer.open(QIODevice::ReadOnly);
 
63
            result = bibTeXimporter.load(&buffer);
 
64
            buffer.close();
 
65
 
 
66
            if (result)
 
67
                kDebug() << "result = " << result->count() << "  " << data.size() << "  " << buffer.size();
 
68
            else
 
69
                kDebug() << "result is empty";
 
70
            break;
 
71
        }
 
72
    }
 
73
 
 
74
    delete doc;
 
75
    return result;
 
76
}
 
77
 
 
78
bool FileImporterPDF::guessCanDecode(const QString &)
 
79
{
 
80
    return false;
 
81
}
 
82
 
 
83
void FileImporterPDF::cancel()
 
84
{
 
85
    m_cancelFlag = true;
 
86
    m_bibTeXimporter->cancel();
 
87
}
 
88
 
 
89
bool FileImporterPDF::containsBibTeXData(const KUrl &url)
 
90
{
 
91
    bool result = false;
 
92
 
 
93
    QString tmpFile;
 
94
    if (KIO::NetAccess::download(url, tmpFile, NULL)) {
 
95
        Poppler::Document *doc = Poppler::Document::load(tmpFile);
 
96
        if (doc != NULL) {
 
97
            if (doc->hasEmbeddedFiles())
 
98
                foreach(Poppler::EmbeddedFile *file, doc->embeddedFiles())
 
99
                if (file->name().endsWith(".bib")) {
 
100
                    result = true;
 
101
                    break;
 
102
                }
 
103
            delete doc;
 
104
        }
 
105
        KIO::NetAccess::removeTempFile(tmpFile);
 
106
    }
 
107
 
 
108
    return result;
 
109
}