Make poppler optional
[pykit.git] / Viewer.cc
1 // $Id$
2 //
3 // Copyright (C) 2010
4 //     Stefan Bund <info@j32.de>
5
6 /** \file
7     \brief Viewer non-inline non-template implementation */
8
9 #include "Viewer.hh"
10 //#include "Viewer.ih"
11
12 // Custom includes
13 #include <iostream>
14 #include <QWebPluginFactory>
15 #include "Publisher.hh"
16 #include "PDFWidget.hh"
17
18 //#include "Viewer.mpp"
19 #define prefix_
20 ///////////////////////////////cc.p////////////////////////////////////////
21
22 #ifdef POPPLER
23
24 namespace {
25
26     class PDFWebPluginFactory
27         : public QWebPluginFactory
28     {
29     public:
30         PDFWebPluginFactory(QNetworkAccessManager * manager, QObject * parent = 0);
31
32         QObject * create(QString const & mimeType, QUrl const & url,
33                          QStringList const & argumentNames, QStringList const & argumentValues)
34             const;
35         QList<QWebPluginFactory::Plugin> plugins() const;
36
37     private:
38         QNetworkAccessManager * manager_;
39     };
40
41 }
42
43 prefix_ PDFWebPluginFactory::PDFWebPluginFactory(QNetworkAccessManager * manager,
44                                                  QObject * parent)
45     : QWebPluginFactory(parent), manager_ (manager)
46 {}
47
48 prefix_ QObject * PDFWebPluginFactory::create(QString const & mimeType, QUrl const & url,
49                                               QStringList const & /* argumentNames */,
50                                               QStringList const & /* argumentValues */)
51     const
52 {
53     if (mimeType == "application/x-pdf") {
54         pykit::PDFWidget * plugin = new pykit::PDFWidget(manager_);
55         plugin->load(url);
56         return plugin;
57     }
58     return 0;
59 }
60
61 prefix_ QList<QWebPluginFactory::Plugin> PDFWebPluginFactory::plugins()
62     const
63 {
64     QList<QWebPluginFactory::Plugin> plugins;
65
66     {
67         QWebPluginFactory::Plugin plugin;
68         plugin.name = "PDF viewer";
69         plugin.description = "View PDF files";
70         {
71             QWebPluginFactory::MimeType mimeType;
72             mimeType.name = "application/x-pdf";
73             mimeType.description = "PDF file";
74             plugin.mimeTypes += mimeType;
75         }
76         plugins += plugin;
77     }
78
79     return plugins;
80 }
81
82 #endif
83
84 ///////////////////////////////////////////////////////////////////////////
85 // pykit::Viewer
86
87 prefix_ pykit::Viewer::Viewer(QUrl const & url, Publisher * publisher, QWidget * parent)
88     : QWebView (parent)
89 {
90     QWebSettings::globalSettings()->setAttribute(QWebSettings::PluginsEnabled, true);
91     page()->setNetworkAccessManager(
92         new InternalNetworkAccessManager(page()->networkAccessManager(), this, publisher));
93 #ifdef POPPLER
94     page()->setPluginFactory(new PDFWebPluginFactory (page()->networkAccessManager(), this));
95 #endif
96     load(url);
97 }
98
99 ///////////////////////////////cc.e////////////////////////////////////////
100 #undef prefix_
101 //#include "Viewer.mpp"
102
103 \f
104 // Local Variables:
105 // mode: c++
106 // fill-column: 100
107 // comment-column: 40
108 // c-file-style: "j32"
109 // indent-tabs-mode: nil
110 // ispell-local-dictionary: "american"
111 // compile-command: "scons -U"
112 // End: