source: terepaima/terepaima-0.4.16/sources/pluginhandler.cpp @ 9e3d987

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

Comentadas líneas de depuración agregadas para buscar la falla de carga un plugin

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