fix tricky regression noticed by Vyacheslav Tokarev on Google Reader.
[kdelibs.git] / kutils / kprintpreview.cpp
blob4f45e1382b4be2b1a7eee153713b85ea9a2491dd
1 /*
2 * This file is part of the KDE libraries
3 * Copyright (c) 2007 Alex Merry <alex.merry@kdemail.net>
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.
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.
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 **/
20 #include "kprintpreview.h"
22 #include <QtCore/QFile>
23 #include <QtGui/QLabel>
24 #include <QtGui/QPrinter>
25 #include <QtGui/QShowEvent>
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>
36 class KPrintPreviewPrivate
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)
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";
55 void getPart();
56 bool doPreview();
57 void fail();
59 KPrintPreview *q;
61 QPrinter *printer;
62 QWidget *mainWidget;
64 KTempDir tempdir;
65 QString filename;
67 KParts::ReadOnlyPart *previewPart;
68 QWidget *failMessage;
71 void KPrintPreviewPrivate::getPart()
73 if (previewPart) {
74 kDebug(500) << "already got a part";
75 return;
77 kDebug(500) << "querying trader for application/pdf service";
79 KPluginFactory *factory(0);
80 const KService::List offers =
81 KMimeTypeTrader::self()->query("application/pdf", "KParts/ReadOnlyPart");
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();
90 ++it;
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";
101 bool KPrintPreviewPrivate::doPreview()
103 if (!QFile::exists(filename)) {
104 kWarning() << "Nothing was produced to be previewed";
105 return false;
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);
120 void KPrintPreviewPrivate::fail()
122 if (!failMessage) {
123 failMessage = new QLabel(i18n("Could not load print preview part"), q);
125 q->setMainWidget(failMessage);
131 KPrintPreview::KPrintPreview(QPrinter *printer, QWidget *parent)
132 : KDialog(parent)
133 , d(new KPrintPreviewPrivate(this, printer))
135 kDebug(500) << "kdeprint: creating preview dialog";
137 // Set up the dialog
138 setCaption(i18n("Print Preview"));
139 setButtons(KDialog::Close);
141 // Set up the printer
142 kDebug(500) << "Will print to" << d->filename;
143 printer->setOutputFileName(d->filename);
146 KPrintPreview::~KPrintPreview()
148 delete d;
151 void KPrintPreview::showEvent(QShowEvent *event)
153 if (!event->spontaneous()) {
154 // being shown for the first time
155 if (!d->doPreview()) {
156 event->accept();
157 return;
160 KDialog::showEvent(event);
163 #include "kprintpreview.moc"