~ubuntu-branches/debian/sid/qpdfview/sid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*

Copyright 2012-2013 Adam Reichold

This file is part of qpdfview.

qpdfview is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

qpdfview is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with qpdfview.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "pluginhandler.h"

#include <QApplication>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QMessageBox>
#include <QPluginLoader>

#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)

#include <QMimeDatabase>

#endif // QT_VERSION

#ifdef WITH_MAGIC

#include <magic.h>

#endif // WITH_MAGIC

#include "model.h"

#ifdef WITH_PDF

Plugin* PluginHandler::s_pdfPlugin = 0;

#endif // WITH_PDF

#ifdef WITH_PS

Plugin* PluginHandler::s_psPlugin = 0;

#endif // WITH_PS

#ifdef WITH_DJVU

Plugin* PluginHandler::s_djvuPlugin = 0;

#endif // WITH_DJVU

Model::Document* PluginHandler::loadDocument(const QString& filePath)
{
    enum { UnknownType = 0, PDF = 1, PS = 2, DjVu = 3 } fileType = UnknownType;

#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)

    const QMimeType mimeType = QMimeDatabase().mimeTypeForFile(filePath, QMimeDatabase::MatchContent);

    if(mimeType.name() == "application/pdf")
    {
        fileType = PDF;
    }
    else if(mimeType.name() == "application/postscript")
    {
        fileType = PS;
    }
    else if(mimeType.name() == "image/vnd.djvu")
    {
        fileType = DjVu;
    }
    else
    {
        qDebug() << "Unknown file type:" << mimeType.name();
    }

#else

#ifdef WITH_MAGIC

    magic_t cookie = magic_open(MAGIC_MIME_TYPE | MAGIC_SYMLINK);

    if(magic_load(cookie, 0) == 0)
    {
        const char* mime_type = magic_file(cookie, QFile::encodeName(filePath));

        if(qstrncmp(mime_type, "application/pdf", 15) == 0)
        {
            fileType = PDF;
        }
        else if(qstrncmp(mime_type, "application/postscript", 22) == 0)
        {
            fileType = PS;
        }
        else if(qstrncmp(mime_type, "image/vnd.djvu", 14) == 0)
        {
            fileType = DjVu;
        }
        else
        {
            qDebug() << "Unknown file type:" << mime_type;
        }
    }

    magic_close(cookie);

#else

    const QFileInfo fileInfo(filePath);

    if(fileInfo.suffix().toLower() == "pdf")
    {
        fileType = PDF;
    }
    else if(fileInfo.suffix().toLower() == "ps")
    {
        fileType = PS;
    }
    else if(fileInfo.suffix().toLower() == "djvu" || fileInfo.suffix().toLower() == "djv")
    {
        fileType = DjVu;
    }
    else
    {
        qDebug() << "Unkown file type:" << fileInfo.suffix().toLower();
    }

#endif // WITH_MAGIC

#endif // QT_VERSION

#ifdef WITH_PDF

    if(fileType == PDF)
    {
        loadPdfPlugin();

        return s_pdfPlugin != 0 ? s_pdfPlugin->loadDocument(filePath) : 0;
    }

#endif // WITH_PDF

#ifdef WITH_PS

    if(fileType == PS)
    {
        loadPsPlugin();

        return s_psPlugin != 0 ? s_psPlugin->loadDocument(filePath) : 0;
    }

#endif // WITH_PS

#ifdef WITH_DJVU

    if(fileType == DjVu)
    {
        loadDjVuPlugin();

        return s_djvuPlugin != 0 ? s_djvuPlugin->loadDocument(filePath) : 0;
    }

#endif // WITH_DJVU

    return 0;
}

#ifdef WITH_PDF

SettingsWidget* PluginHandler::createPdfSettingsWidget(QWidget* parent)
{
    loadPdfPlugin();

    return s_pdfPlugin != 0 ? s_pdfPlugin->createSettingsWidget(parent) : 0;
}

#endif // WITH_PDF

#ifdef WITH_PS

SettingsWidget* PluginHandler::createPsSettingsWidget(QWidget* parent)
{
    loadPsPlugin();

    return s_psPlugin != 0 ? s_psPlugin->createSettingsWidget(parent) : 0;
}

#endif // WITH_PS


Plugin* PluginHandler::loadPlugin(const QString& fileName)
{
    QPluginLoader pluginLoader(QDir(QApplication::applicationDirPath()).absoluteFilePath(fileName));

    if(!pluginLoader.load())
    {
        pluginLoader.setFileName(QDir(PLUGIN_INSTALL_PATH).absoluteFilePath(fileName));

        if(!pluginLoader.load())
        {
            qCritical() << "Could not load plug-in:" << fileName;
            qCritical() << pluginLoader.errorString();

            return 0;
        }
    }

    Plugin* plugin = qobject_cast< Plugin* >(pluginLoader.instance());

    if(plugin == 0)
    {
        qCritical() << "Could not instantiate plug-in:" << fileName;
        qCritical() << pluginLoader.errorString();
    }

    return plugin;
}

Plugin* PluginHandler::loadStaticPlugin(const QString& objectName)
{
    foreach(QObject* object, QPluginLoader::staticInstances())
    {
        if(object->objectName() == objectName)
        {
            Plugin* plugin = qobject_cast< Plugin* >(object);

            if(plugin != 0)
            {
                return plugin;
            }
        }
    }

    qCritical() << "Could not load static plug-in:" << objectName;

    return 0;
}


#ifdef WITH_PDF

#ifdef STATIC_PDF_PLUGIN

Q_IMPORT_PLUGIN(qpdfview_pdf)

#endif // STATIC_PDF_PLUGIN

void PluginHandler::loadPdfPlugin()
{
    if(s_pdfPlugin == 0)
    {
#ifndef STATIC_PDF_PLUGIN
        Plugin* pdfPlugin = loadPlugin(PDF_PLUGIN_NAME);
#else
        Plugin* pdfPlugin = loadStaticPlugin("PdfPlugin");
#endif // STATIC_PDF_PLUGIN

        if(pdfPlugin != 0)
        {
            s_pdfPlugin = pdfPlugin;
        }
        else
        {
            QMessageBox::critical(0, tr("Critical"), tr("Could not load PDF plug-in!"));
        }
    }
}

#endif // WITH_PDF


#ifdef WITH_PS

#ifdef STATIC_PS_PLUGIN

Q_IMPORT_PLUGIN(qpdfview_ps)

#endif // STATIC_PS_PLUGIN

void PluginHandler::loadPsPlugin()
{
    if(s_psPlugin == 0)
    {
#ifndef STATIC_PS_PLUGIN
        Plugin* psPlugin = loadPlugin(PS_PLUGIN_NAME);
#else
        Plugin* psPlugin = loadStaticPlugin("PsPlugin");
#endif // STATIC_PS_PLUGIN

        if(psPlugin != 0)
        {
            s_psPlugin = psPlugin;
        }
        else
        {
            QMessageBox::critical(0, tr("Critical"), tr("Could not load PS plug-in!"));
        }

    }
}

#endif // WITH_PS


#ifdef WITH_DJVU

#ifdef STATIC_DJVU_PLUGIN

Q_IMPORT_PLUGIN(qpdfview_djvu)

#endif // STATIC_DJVU_PLUGIN

void PluginHandler::loadDjVuPlugin()
{
    if(s_djvuPlugin == 0)
    {
#ifndef STATIC_DJVU_PLUGIN
        Plugin* djvuPlugin = loadPlugin(DJVU_PLUGIN_NAME);
#else
        Plugin* djvuPlugin = loadStaticPlugin("DjVuPlugin");
#endif // STATIC_DJVU_PLUGIN

        if(djvuPlugin != 0)
        {
            s_djvuPlugin = djvuPlugin;
        }
        else
        {
            QMessageBox::critical(0, tr("Critical"), tr("Could not load DjVu plug-in!"));
        }

    }
}

#endif // WITH_DJVU