source: terepaima/terepaima-0.4.16/sources/pluginhandler.cpp @ 1f4adec

desarrollostretch
Last change on this file since 1f4adec was 1f4adec, checked in by aosorio <aosorio@…>, 8 years ago

Agregado proyecto base, esto luego del dh_make -f

  • Property mode set to 100644
File size: 10.8 KB
Line 
1/*
2
3Copyright 2012-2013, 2015 Adam Reichold
4
5This file is part of qpdfview.
6
7qpdfview is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation, either version 2 of the License, or
10(at your option) any later version.
11
12qpdfview is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along 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 <QImageReader>
29#include <QMessageBox>
30#include <QPluginLoader>
31
32#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
33
34#include <QMimeDatabase>
35
36#endif // QT_VERSION
37
38#ifdef WITH_MAGIC
39
40#include <magic.h>
41
42#endif // WITH_MAGIC
43
44#include "model.h"
45
46namespace
47{
48
49using namespace qpdfview;
50
51Plugin* loadStaticPlugin(const QString& objectName)
52{
53    foreach(QObject* object, QPluginLoader::staticInstances())
54    {
55        if(object->objectName() == objectName)
56        {
57            Plugin* plugin = qobject_cast< Plugin* >(object);
58
59            if(plugin != 0)
60            {
61                return plugin;
62            }
63        }
64    }
65
66    qCritical() << "Could not load static plug-in:" << objectName;
67
68    return 0;
69}
70
71Plugin* loadPlugin(const QString& fileName)
72{
73    QPluginLoader pluginLoader(QDir(QApplication::applicationDirPath()).absoluteFilePath(fileName));
74
75    if(!pluginLoader.load())
76    {
77        const QString firstFileName = pluginLoader.fileName();
78        const QString firstErrorString = pluginLoader.errorString();
79
80        pluginLoader.setFileName(QDir(PLUGIN_INSTALL_PATH).absoluteFilePath(fileName));
81
82        if(!pluginLoader.load())
83        {
84            qCritical() << "Could not load plug-in in first attempt:" << firstFileName;
85            qCritical() << firstErrorString;
86
87            qCritical() << "Could not load plug-in in second attempt:" << pluginLoader.fileName();
88            qCritical() << pluginLoader.errorString();
89
90            return 0;
91        }
92    }
93
94    Plugin* plugin = qobject_cast< Plugin* >(pluginLoader.instance());
95
96    if(plugin == 0)
97    {
98        qCritical() << "Could not instantiate plug-in:" << pluginLoader.fileName();
99        qCritical() << pluginLoader.errorString();
100    }
101
102    return plugin;
103}
104
105#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
106
107bool isSupportedImageFormat(const QMimeType& mimeType)
108{
109    const QByteArray name = mimeType.name().toLocal8Bit();
110
111    return QImageReader::supportedMimeTypes().contains(name);
112}
113
114#else
115
116bool isSupportedImageFormat(const QString& fileName)
117{
118    return !QImageReader::imageFormat(fileName).isEmpty();
119}
120
121#endif // QT_VERSION
122
123QStringList supportedImageFormats()
124{
125    QStringList formats;
126
127    foreach(const QByteArray& format, QImageReader::supportedImageFormats())
128    {
129        const QString name = QString::fromLocal8Bit(format);
130
131        formats.append(QLatin1String("*.") + name);
132    }
133
134    return formats;
135}
136
137const char* const pdfMimeType = "application/pdf";
138const char* const psMimeType = "application/postscript";
139const char* const djvuMimeType = "image/vnd.djvu";
140
141PluginHandler::FileType matchFileType(const QString& filePath)
142{
143    PluginHandler::FileType fileType = PluginHandler::Unknown;
144
145#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
146
147    const QMimeType mimeType = QMimeDatabase().mimeTypeForFile(filePath, QMimeDatabase::MatchContent);
148
149    if(mimeType.inherits(pdfMimeType))
150    {
151        fileType = PluginHandler::PDF;
152    }
153    else if(mimeType.inherits(psMimeType))
154    {
155        fileType = PluginHandler::PS;
156    }
157    else if(mimeType.inherits(djvuMimeType))
158    {
159        fileType = PluginHandler::DjVu;
160    }
161    else if(isSupportedImageFormat(mimeType))
162    {
163        fileType = PluginHandler::Image;
164    }
165    else
166    {
167        qDebug() << "Unknown MIME type:" << mimeType.name();
168    }
169
170#else
171
172#ifdef WITH_MAGIC
173
174    magic_t cookie = magic_open(MAGIC_MIME_TYPE | MAGIC_SYMLINK);
175
176    if(magic_load(cookie, 0) == 0)
177    {
178        const char* mimeType = magic_file(cookie, QFile::encodeName(filePath));
179
180        if(qstrncmp(mimeType, pdfMimeType, qstrlen(pdfMimeType)) == 0)
181        {
182            fileType = PluginHandler::PDF;
183        }
184        else if(qstrncmp(mimeType, psMimeType, qstrlen(psMimeType)) == 0)
185        {
186            fileType = PluginHandler::PS;
187        }
188        else if(qstrncmp(mimeType, djvuMimeType, qstrlen(djvuMimeType)) == 0)
189        {
190            fileType = PluginHandler::DjVu;
191        }
192        else if(isSupportedImageFormat(filePath))
193        {
194            fileType = PluginHandler::Image;
195        }
196        else
197        {
198            qDebug() << "Unknown MIME type:" << mimeType;
199        }
200    }
201
202    magic_close(cookie);
203
204#else
205
206    const QString suffix = QFileInfo(filePath).suffix().toLower();
207
208    if(suffix == QLatin1String("pdf"))
209    {
210        fileType = PluginHandler::PDF;
211    }
212    else if(suffix == QLatin1String("ps") || suffix == QLatin1String("eps"))
213    {
214        fileType = PluginHandler::PS;
215    }
216    else if(suffix == QLatin1String("djvu") || suffix == QLatin1String("djv"))
217    {
218        fileType = PluginHandler::DjVu;
219    }
220    else if(isSupportedImageFormat(filePath))
221    {
222        fileType = PluginHandler::Image;
223    }
224    else
225    {
226        qDebug() << "Unkown file suffix:" << suffix;
227    }
228
229#endif // WITH_MAGIC
230
231#endif // QT_VERSION
232
233    return fileType;
234}
235
236} // anonymous
237
238namespace qpdfview
239{
240
241PluginHandler* PluginHandler::s_instance = 0;
242
243PluginHandler* PluginHandler::instance()
244{
245    if(s_instance == 0)
246    {
247        s_instance = new PluginHandler(qApp);
248    }
249
250    return s_instance;
251}
252
253PluginHandler::~PluginHandler()
254{
255    s_instance = 0;
256}
257
258QString PluginHandler::fileTypeName(PluginHandler::FileType fileType)
259{
260    switch(fileType)
261    {
262    default:
263    case PluginHandler::Unknown:
264        return QLatin1String("Unknown");
265    case PluginHandler::PDF:
266        return QLatin1String("PDF");
267    case PluginHandler::PS:
268        return QLatin1String("PS");
269    case PluginHandler::DjVu:
270        return QLatin1String("DjVu");
271    case PluginHandler::Image:
272        return QLatin1String("Image");
273    }
274}
275
276QStringList PluginHandler::openFilter()
277{
278    QStringList openFilter;
279    QStringList supportedFormats;
280
281#if defined(WITH_PDF) || defined(WITH_FITZ)
282
283    openFilter.append(QLatin1String("Portable document format (*.pdf)"));
284    supportedFormats.append(QLatin1String("*.pdf"));
285
286#endif // WITH_PDF // WITH_FITZ
287
288#ifdef WITH_PS
289
290    openFilter.append(QLatin1String("PostScript (*.ps)"));
291    openFilter.append(QLatin1String("Encapsulated PostScript (*.eps)"));
292    supportedFormats.append(QLatin1String("*.ps *.eps"));
293
294#endif // WITH_PS
295
296#ifdef WITH_DJVU
297
298    openFilter.append(QLatin1String("DjVu (*.djvu *.djv)"));
299    supportedFormats.append(QLatin1String("*.djvu *.djv"));
300
301#endif // WITH_DJVU
302
303#ifdef WITH_IMAGE
304
305    const QStringList imageFormats = supportedImageFormats();
306
307    openFilter.append(tr("Image (%1)").arg(imageFormats.join(QLatin1String(" "))));
308    supportedFormats.append(imageFormats);
309
310#endif // WITH_IMAGE
311
312    openFilter.prepend(tr("Supported formats (%1)").arg(supportedFormats.join(QLatin1String(" "))));
313
314    return openFilter;
315}
316
317Model::Document* PluginHandler::loadDocument(const QString& filePath)
318{
319    const FileType fileType = matchFileType(filePath);
320
321    if(fileType == Unknown)
322    {
323        qWarning() << tr("Could not match file type of '%1'!").arg(filePath);
324
325        return 0;
326    }
327
328    if(!loadPlugin(fileType))
329    {
330        QMessageBox::critical(0, tr("Critical"), tr("Could not load plug-in for file type '%1'!").arg(fileTypeName(fileType)));
331
332        return 0;
333    }
334
335    return m_plugins.value(fileType)->loadDocument(filePath);
336}
337
338SettingsWidget* PluginHandler::createSettingsWidget(FileType fileType, QWidget* parent)
339{
340    return loadPlugin(fileType) ? m_plugins.value(fileType)->createSettingsWidget(parent) : 0;
341}
342
343PluginHandler::PluginHandler(QObject* parent) : QObject(parent),
344    m_plugins()
345{
346#ifdef WITH_IMAGE
347#ifdef STATIC_IMAGE_PLUGIN
348    m_objectNames.insertMulti(Image, QLatin1String("ImagePlugin"));
349#else
350    m_fileNames.insertMulti(Image, QLatin1String(IMAGE_PLUGIN_NAME));
351#endif // STATIC_IMAGE_PLUGIN
352#endif // WITH_IMAGE
353
354#ifdef WITH_FITZ
355#ifdef STATIC_FITZ_PLUGIN
356    m_objectNames.insertMulti(PDF, QLatin1String("FitzPlugin"));
357#else
358    m_fileNames.insertMulti(PDF, QLatin1String(FITZ_PLUGIN_NAME));
359#endif // STATIC_FITZ_PLUGIN
360#endif // WITH_FITZ
361
362#ifdef WITH_PDF
363#ifdef STATIC_PDF_PLUGIN
364    m_objectNames.insertMulti(PDF, QLatin1String("PdfPlugin"));
365#else
366    m_fileNames.insertMulti(PDF, QLatin1String(PDF_PLUGIN_NAME));
367#endif // STATIC_PDF_PLUGIN
368#endif // WITH_PDF
369
370#ifdef WITH_PS
371#ifdef STATIC_PS_PLUGIN
372    m_objectNames.insertMulti(PS, QLatin1String("PsPlugin"));
373#else
374    m_fileNames.insertMulti(PS, QLatin1String(PS_PLUGIN_NAME));
375#endif // STATIC_PS_PLUGIN
376#endif // WITH_PS
377
378#ifdef WITH_DJVU
379#ifdef STATIC_DJVU_PLUGIN
380    m_objectNames.insertMulti(DjVu, QLatin1String("DjVuPlugin"));
381#else
382    m_fileNames.insertMulti(DjVu, QLatin1String(DJVU_PLUGIN_NAME));
383#endif // STATIC_DJVU_PLUGIN
384#endif // WITH_DJVU
385}
386
387bool PluginHandler::loadPlugin(FileType fileType)
388{
389    if(m_plugins.contains(fileType))
390    {
391        return true;
392    }
393
394    foreach(const QString& objectName, m_objectNames.values(fileType))
395    {
396        if(Plugin* plugin = ::loadStaticPlugin(objectName))
397        {
398            m_plugins.insert(fileType, plugin);
399
400            return true;
401        }
402    }
403
404    foreach(const QString& fileName, m_fileNames.values(fileType))
405    {
406        if(Plugin* plugin = ::loadPlugin(fileName))
407        {
408            m_plugins.insert(fileType, plugin);
409
410            return true;
411        }
412    }
413
414    return false;
415}
416
417} // qpdfview
418
419#ifdef STATIC_IMAGE_PLUGIN
420    #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
421        Q_IMPORT_PLUGIN(qpdfview_image)
422    #else
423        Q_IMPORT_PLUGIN(ImagePlugin)
424    #endif // QT_VERSION
425#endif // STATIC_IMAGE_PLUGIN
426
427#ifdef STATIC_FITZ_PLUGIN
428    #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
429        Q_IMPORT_PLUGIN(qpdfview_fitz)
430    #else
431        Q_IMPORT_PLUGIN(FitzPlugin)
432    #endif // QT_VERSION
433#endif // STATIC_FITZ_PLUGIN
434
435#ifdef STATIC_PDF_PLUGIN
436    #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
437        Q_IMPORT_PLUGIN(qpdfview_pdf)
438    #else
439        Q_IMPORT_PLUGIN(PdfPlugin)
440    #endif // QT_VERSION
441#endif // STATIC_PDF_PLUGIN
442
443#ifdef STATIC_PS_PLUGIN
444    #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
445        Q_IMPORT_PLUGIN(qpdfview_ps)
446    #else
447        Q_IMPORT_PLUGIN(PsPlugin)
448    #endif // QT_VERSION
449#endif // STATIC_PS_PLUGIN
450
451#ifdef STATIC_DJVU_PLUGIN
452    #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
453        Q_IMPORT_PLUGIN(qpdfview_djvu)
454    #else
455        Q_IMPORT_PLUGIN(DjvuPlugin)
456    #endif // QT_VERSION
457#endif // STATIC_DJVU_PLUGIN
Note: See TracBrowser for help on using the repository browser.