~ubuntu-branches/ubuntu/vivid/qpdfview/vivid

« back to all changes in this revision

Viewing changes to sources/pluginhandler.cpp

  • Committer: Package Import Robot
  • Author(s): Benjamin Eltzner
  • Date: 2013-04-22 17:40:43 UTC
  • mfrom: (1.2.7)
  • Revision ID: package-import@ubuntu.com-20130422174043-0kav0mabth2lv35f
Tags: 0.4.2-1
* New upstream release.
* Added Hardening.
* Adjusted Exit mnemonic. (Closes: #702749)
  Thanks to David Fries <david@fries.net>

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
Copyright 2012-2013 Adam Reichold
 
4
 
 
5
This file is part of qpdfview.
 
6
 
 
7
qpdfview is free software: you can redistribute it and/or modify
 
8
it under the terms of the GNU General Public License as published by
 
9
the Free Software Foundation, either version 2 of the License, or
 
10
(at your option) any later version.
 
11
 
 
12
qpdfview 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
 
15
GNU General Public License for more details.
 
16
 
 
17
You should have received a copy of the GNU General Public License
 
18
along with qpdfview.  If not, see <http://www.gnu.org/licenses/>.
 
19
 
 
20
*/
 
21
 
 
22
#include "pluginhandler.h"
 
23
 
 
24
#include <QApplication>
 
25
#include <QDebug>
 
26
#include <QDir>
 
27
#include <QFileInfo>
 
28
#include <QMessageBox>
 
29
#include <QPluginLoader>
 
30
 
 
31
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
 
32
 
 
33
#include <QMimeDatabase>
 
34
 
 
35
#endif // QT_VERSION
 
36
 
 
37
#ifdef WITH_MAGIC
 
38
 
 
39
#include <magic.h>
 
40
 
 
41
#endif // WITH_MAGIC
 
42
 
 
43
#include "model.h"
 
44
 
 
45
#ifdef WITH_PDF
 
46
 
 
47
Plugin* PluginHandler::s_pdfPlugin = 0;
 
48
 
 
49
#endif // WITH_PDF
 
50
 
 
51
#ifdef WITH_PS
 
52
 
 
53
Plugin* PluginHandler::s_psPlugin = 0;
 
54
 
 
55
#endif // WITH_PS
 
56
 
 
57
#ifdef WITH_DJVU
 
58
 
 
59
Plugin* PluginHandler::s_djvuPlugin = 0;
 
60
 
 
61
#endif // WITH_DJVU
 
62
 
 
63
Model::Document* PluginHandler::loadDocument(const QString& filePath)
 
64
{
 
65
    enum { UnknownType = 0, PDF = 1, PS = 2, DjVu = 3 } fileType = UnknownType;
 
66
 
 
67
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
 
68
 
 
69
    QMimeDatabase mimeDatabase;
 
70
    QMimeType mimeType = mimeDatabase.mimeTypeForFile(filePath, QMimeDatabase::MatchContent);
 
71
 
 
72
    if(mimeType.name() == "application/pdf")
 
73
    {
 
74
        fileType = PDF;
 
75
    }
 
76
    else if(mimeType.name() == "application/postscript")
 
77
    {
 
78
        fileType = PS;
 
79
    }
 
80
    else if(mimeType.name() == "image/vnd.djvu")
 
81
    {
 
82
        fileType = DjVu;
 
83
    }
 
84
    else
 
85
    {
 
86
        qDebug() << "Unknown file type:" << mimeType.name();
 
87
    }
 
88
 
 
89
#else
 
90
 
 
91
#ifdef WITH_MAGIC
 
92
 
 
93
    magic_t cookie = magic_open(MAGIC_MIME_TYPE | MAGIC_SYMLINK);
 
94
 
 
95
    if(magic_load(cookie, 0) == 0)
 
96
    {
 
97
        const char* mime_type = magic_file(cookie, QFile::encodeName(filePath));
 
98
 
 
99
        if(qstrncmp(mime_type, "application/pdf", 15) == 0)
 
100
        {
 
101
            fileType = PDF;
 
102
        }
 
103
        else if(qstrncmp(mime_type, "application/postscript", 22) == 0)
 
104
        {
 
105
            fileType = PS;
 
106
        }
 
107
        else if(qstrncmp(mime_type, "image/vnd.djvu", 14) == 0)
 
108
        {
 
109
            fileType = DjVu;
 
110
        }
 
111
        else
 
112
        {
 
113
            qDebug() << "Unknown file type:" << mime_type;
 
114
        }
 
115
    }
 
116
 
 
117
    magic_close(cookie);
 
118
 
 
119
#else
 
120
 
 
121
    QFileInfo fileInfo(filePath);
 
122
 
 
123
    if(fileInfo.suffix().toLower() == "pdf")
 
124
    {
 
125
        fileType = PDF;
 
126
    }
 
127
    else if(fileInfo.suffix().toLower() == "ps")
 
128
    {
 
129
        fileType = PS;
 
130
    }
 
131
    else if(fileInfo.suffix().toLower() == "djvu" || fileInfo.suffix().toLower() == "djv")
 
132
    {
 
133
        fileType = DjVu;
 
134
    }
 
135
    else
 
136
    {
 
137
        qDebug() << "Unkown file type:" << fileInfo.suffix().toLower();
 
138
    }
 
139
 
 
140
#endif // WITH_MAGIC
 
141
 
 
142
#endif // QT_VERSION
 
143
 
 
144
#ifdef WITH_PDF
 
145
 
 
146
    if(fileType == PDF)
 
147
    {
 
148
        loadPdfPlugin();
 
149
 
 
150
        return s_pdfPlugin != 0 ? s_pdfPlugin->loadDocument(filePath) : 0;
 
151
    }
 
152
 
 
153
#endif // WITH_PDF
 
154
 
 
155
#ifdef WITH_PS
 
156
 
 
157
    if(fileType == PS)
 
158
    {
 
159
        loadPsPlugin();
 
160
 
 
161
        return s_psPlugin != 0 ? s_psPlugin->loadDocument(filePath) : 0;
 
162
    }
 
163
 
 
164
#endif // WITH_PS
 
165
 
 
166
#ifdef WITH_DJVU
 
167
 
 
168
    if(fileType == DjVu)
 
169
    {
 
170
        loadDjVuPlugin();
 
171
 
 
172
        return s_djvuPlugin != 0 ? s_djvuPlugin->loadDocument(filePath) : 0;
 
173
    }
 
174
 
 
175
#endif // WITH_DJVU
 
176
 
 
177
    return 0;
 
178
}
 
179
 
 
180
#ifdef WITH_PDF
 
181
 
 
182
SettingsWidget* PluginHandler::createPdfSettingsWidget(QWidget* parent)
 
183
{
 
184
    loadPdfPlugin();
 
185
 
 
186
    return s_pdfPlugin != 0 ? s_pdfPlugin->createSettingsWidget(parent) : 0;
 
187
}
 
188
 
 
189
#endif // WITH_PDF
 
190
 
 
191
#ifdef WITH_PS
 
192
 
 
193
SettingsWidget* PluginHandler::createPsSettingsWidget(QWidget* parent)
 
194
{
 
195
    loadPsPlugin();
 
196
 
 
197
    return s_psPlugin != 0 ? s_psPlugin->createSettingsWidget(parent) : 0;
 
198
}
 
199
 
 
200
#endif // WITH_PS
 
201
 
 
202
 
 
203
Plugin* PluginHandler::loadPlugin(const QString& fileName)
 
204
{
 
205
    QPluginLoader pluginLoader(QDir(QApplication::applicationDirPath()).absoluteFilePath(fileName));
 
206
 
 
207
    if(!pluginLoader.load())
 
208
    {
 
209
        pluginLoader.setFileName(QDir(PLUGIN_INSTALL_PATH).absoluteFilePath(fileName));
 
210
 
 
211
        if(!pluginLoader.load())
 
212
        {
 
213
            qCritical() << "Could not load plug-in:" << fileName;
 
214
            qCritical() << pluginLoader.errorString();
 
215
 
 
216
            return 0;
 
217
        }
 
218
    }
 
219
 
 
220
    Plugin* plugin = qobject_cast< Plugin* >(pluginLoader.instance());
 
221
 
 
222
    if(plugin == 0)
 
223
    {
 
224
        qCritical() << "Could not instantiate plug-in:" << fileName;
 
225
        qCritical() << pluginLoader.errorString();
 
226
    }
 
227
 
 
228
    return plugin;
 
229
}
 
230
 
 
231
Plugin* PluginHandler::loadStaticPlugin(const QString& objectName)
 
232
{
 
233
    foreach(QObject* object, QPluginLoader::staticInstances())
 
234
    {
 
235
        if(object->objectName() == objectName)
 
236
        {
 
237
            Plugin* plugin = qobject_cast< Plugin* >(object);
 
238
 
 
239
            if(plugin != 0)
 
240
            {
 
241
                return plugin;
 
242
            }
 
243
        }
 
244
    }
 
245
 
 
246
    qCritical() << "Could not load static plug-in:" << objectName;
 
247
 
 
248
    return 0;
 
249
}
 
250
 
 
251
 
 
252
#ifdef WITH_PDF
 
253
 
 
254
#ifdef STATIC_PDF_PLUGIN
 
255
 
 
256
Q_IMPORT_PLUGIN(qpdfview_pdf)
 
257
 
 
258
#endif // STATIC_PDF_PLUGIN
 
259
 
 
260
void PluginHandler::loadPdfPlugin()
 
261
{
 
262
    if(s_pdfPlugin == 0)
 
263
    {
 
264
#ifndef STATIC_PDF_PLUGIN
 
265
        Plugin* pdfPlugin = loadPlugin(PDF_PLUGIN_NAME);
 
266
#else
 
267
        Plugin* pdfPlugin = loadStaticPlugin("PdfPlugin");
 
268
#endif // STATIC_PDF_PLUGIN
 
269
 
 
270
        if(pdfPlugin != 0)
 
271
        {
 
272
            s_pdfPlugin = pdfPlugin;
 
273
        }
 
274
        else
 
275
        {
 
276
            QMessageBox::critical(0, tr("Critical"), tr("Could not load PDF plug-in!"));
 
277
        }
 
278
    }
 
279
}
 
280
 
 
281
#endif // WITH_PDF
 
282
 
 
283
 
 
284
#ifdef WITH_PS
 
285
 
 
286
#ifdef STATIC_PS_PLUGIN
 
287
 
 
288
Q_IMPORT_PLUGIN(qpdfview_ps)
 
289
 
 
290
#endif // STATIC_PS_PLUGIN
 
291
 
 
292
void PluginHandler::loadPsPlugin()
 
293
{
 
294
    if(s_psPlugin == 0)
 
295
    {
 
296
#ifndef STATIC_PS_PLUGIN
 
297
        Plugin* psPlugin = loadPlugin(PS_PLUGIN_NAME);
 
298
#else
 
299
        Plugin* psPlugin = loadStaticPlugin("PsPlugin");
 
300
#endif // STATIC_PS_PLUGIN
 
301
 
 
302
        if(psPlugin != 0)
 
303
        {
 
304
            s_psPlugin = psPlugin;
 
305
        }
 
306
        else
 
307
        {
 
308
            QMessageBox::critical(0, tr("Critical"), tr("Could not load PS plug-in!"));
 
309
        }
 
310
 
 
311
    }
 
312
}
 
313
 
 
314
#endif // WITH_PS
 
315
 
 
316
 
 
317
#ifdef WITH_DJVU
 
318
 
 
319
#ifdef STATIC_DJVU_PLUGIN
 
320
 
 
321
Q_IMPORT_PLUGIN(qpdfview_djvu)
 
322
 
 
323
#endif // STATIC_DJVU_PLUGIN
 
324
 
 
325
void PluginHandler::loadDjVuPlugin()
 
326
{
 
327
    if(s_djvuPlugin == 0)
 
328
    {
 
329
#ifndef STATIC_DJVU_PLUGIN
 
330
        Plugin* djvuPlugin = loadPlugin(DJVU_PLUGIN_NAME);
 
331
#else
 
332
        Plugin* djvuPlugin = loadStaticPlugin("DjVuPlugin");
 
333
#endif // STATIC_DJVU_PLUGIN
 
334
 
 
335
        if(djvuPlugin != 0)
 
336
        {
 
337
            s_djvuPlugin = djvuPlugin;
 
338
        }
 
339
        else
 
340
        {
 
341
            QMessageBox::critical(0, tr("Critical"), tr("Could not load DjVu plug-in!"));
 
342
        }
 
343
 
 
344
    }
 
345
}
 
346
 
 
347
#endif // WITH_DJVU