1 | /* |
2 | * This file is part of the KDE libraries |
3 | * Copyright (c) 2007 Alex Merry <alex.merry@kdemail.net> |
4 | * |
5 | * This library is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU Library General Public |
7 | * License version 2 as published by the Free Software Foundation. |
8 | * |
9 | * This library is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | * Library General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU Library General Public License |
15 | * along with this library; see the file COPYING.LIB. If not, write to |
16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
17 | * Boston, MA 02110-1301, USA. |
18 | **/ |
19 | |
20 | #include "kprintpreview.h" |
21 | |
22 | #include <QtCore/QFile> |
23 | #include <QtGui/QLabel> |
24 | #include <QtGui/QPrinter> |
25 | #include <QtGui/QShowEvent> |
26 | |
27 | #include <kmimetypetrader.h> |
28 | #include <kparts/part.h> |
29 | #include <kpluginfactory.h> |
30 | #include <kpluginloader.h> |
31 | #include <kservice.h> |
32 | #include <ktempdir.h> |
33 | #include <kdebug.h> |
34 | |
35 | |
36 | class KPrintPreviewPrivate |
37 | { |
38 | public: |
39 | KPrintPreviewPrivate(KPrintPreview *host, QPrinter * _printer) |
40 | : q(host) |
41 | , printer(_printer) |
42 | , mainWidget(new QWidget(host)) |
43 | , previewPart(0) |
44 | , failMessage(0) |
45 | { |
46 | if ( tempdir.exists() ) { |
47 | filename = tempdir.name() + "print_preview.pdf" ; |
48 | } else { |
49 | // XXX: not portable! |
50 | kWarning() << "Failed to create temporary directory" ; |
51 | filename = "/dev/null" ; |
52 | } |
53 | } |
54 | |
55 | void getPart(); |
56 | bool doPreview(); |
57 | void fail(); |
58 | |
59 | KPrintPreview *q; |
60 | |
61 | QPrinter *printer; |
62 | QWidget *mainWidget; |
63 | |
64 | KTempDir tempdir; |
65 | QString filename; |
66 | |
67 | KParts::ReadOnlyPart *previewPart; |
68 | QWidget *failMessage; |
69 | }; |
70 | |
71 | void KPrintPreviewPrivate::getPart() |
72 | { |
73 | if (previewPart) { |
74 | kDebug(500) << "already got a part" ; |
75 | return; |
76 | } |
77 | kDebug(500) << "querying trader for application/pdf service" ; |
78 | |
79 | KPluginFactory *factory(0); |
80 | const KService::List offers = |
81 | KMimeTypeTrader::self()->query("application/pdf" , "KParts/ReadOnlyPart" ); |
82 | |
83 | KService::List::ConstIterator it = offers.begin(); |
84 | while (!factory && it != offers.end()) { |
85 | KPluginLoader loader(**it); |
86 | factory = loader.factory(); |
87 | if (!factory) { |
88 | kDebug(500) << "Loading failed:" << loader.errorString(); |
89 | } |
90 | ++it; |
91 | } |
92 | if (factory) { |
93 | kDebug(500) << "Trying to create a part" ; |
94 | previewPart = factory->create<KParts::ReadOnlyPart>(q, (QVariantList() << "Print/Preview" )); |
95 | if (!previewPart) { |
96 | kDebug(500) << "Part creation failed" ; |
97 | } |
98 | } |
99 | } |
100 | |
101 | bool KPrintPreviewPrivate::doPreview() |
102 | { |
103 | if (!QFile::exists(filename)) { |
104 | kWarning() << "Nothing was produced to be previewed" ; |
105 | return false; |
106 | } |
107 | |
108 | getPart(); |
109 | if (!previewPart) { |
110 | //TODO: error dialog |
111 | kWarning() << "Could not find a PDF viewer for the preview dialog" ; |
112 | fail(); |
113 | return false; |
114 | } else { |
115 | q->setMainWidget(previewPart->widget()); |
116 | return previewPart->openUrl(filename); |
117 | } |
118 | } |
119 | |
120 | void KPrintPreviewPrivate::fail() |
121 | { |
122 | if (!failMessage) { |
123 | failMessage = new QLabel(i18n("Could not load print preview part" ), q); |
124 | } |
125 | q->setMainWidget(failMessage); |
126 | } |
127 | |
128 | |
129 | |
130 | |
131 | KPrintPreview::KPrintPreview(QPrinter *printer, QWidget *parent) |
132 | : KDialog(parent) |
133 | , d(new KPrintPreviewPrivate(this, printer)) |
134 | { |
135 | kDebug(500) << "kdeprint: creating preview dialog" ; |
136 | |
137 | //There is no printing on wince |
138 | #ifndef _WIN32_WCE |
139 | // Set up the dialog |
140 | setCaption(i18n("Print Preview" )); |
141 | setButtons(KDialog::Close); |
142 | |
143 | // Set up the printer |
144 | kDebug(500) << "Will print to" << d->filename; |
145 | printer->setOutputFileName(d->filename); |
146 | |
147 | setInitialSize(QSize(600, 500)); |
148 | #endif |
149 | } |
150 | |
151 | KPrintPreview::~KPrintPreview() |
152 | { |
153 | delete d; |
154 | } |
155 | |
156 | void KPrintPreview::showEvent(QShowEvent *event) |
157 | { |
158 | if (!event->spontaneous()) { |
159 | // being shown for the first time |
160 | if (!d->doPreview()) { |
161 | event->accept(); |
162 | return; |
163 | } |
164 | } |
165 | KDialog::showEvent(event); |
166 | } |
167 | |
168 | bool KPrintPreview::isAvailable() |
169 | { |
170 | return !KMimeTypeTrader::self()->query("application/pdf" , "KParts/ReadOnlyPart" ).isEmpty(); |
171 | } |
172 | |
173 | #include "kprintpreview.moc" |
174 | |
175 | |
176 | |
177 | |