SVN_SILENT made messages (.desktop file)
[kdegraphics.git] / okular / ui / fileprinterpreview.cpp
blob1508b5142574edb08fa155e59dcc27e75256821b
1 /***************************************************************************
2 * Copyright (C) 2007 by John Layt <john@layt.net> *
3 * *
4 * FilePrinterPreview based on KPrintPreview (originally LGPL) *
5 * Copyright (c) 2007 Alex Merry <huntedhacker@tiscali.co.uk> *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 ***************************************************************************/
13 #include "fileprinterpreview.h"
15 #include <QFile>
16 #include <QSize>
17 #include <QtCore/QFile>
18 #include <QtGui/QLabel>
19 #include <QtGui/QShowEvent>
21 #include <kmimetypetrader.h>
22 #include <kparts/part.h>
23 #include <kpluginfactory.h>
24 #include <kpluginloader.h>
25 #include <kservice.h>
26 #include <kdebug.h>
28 using namespace Okular;
30 // This code copied from KPrintPreview by Alex Merry, adapted to do PS files instead of PDF
32 class Okular::FilePrinterPreviewPrivate
34 public:
35 FilePrinterPreviewPrivate( FilePrinterPreview *host, const QString & _filename )
36 : q(host)
37 , mainWidget(new QWidget(host))
38 , previewPart(0)
39 , failMessage(0)
41 filename = _filename;
44 void getPart();
45 bool doPreview();
46 void fail();
48 FilePrinterPreview *q;
50 QWidget *mainWidget;
52 QString filename;
54 KParts::ReadOnlyPart *previewPart;
55 QWidget *failMessage;
58 void FilePrinterPreviewPrivate::getPart()
60 if (previewPart) {
61 kDebug(500) << "already got a part";
62 return;
64 kDebug(500) << "querying trader for application/ps service";
66 KPluginFactory *factory(0);
67 KService::List offers =
68 KMimeTypeTrader::self()->query("application/postscript", "KParts/ReadOnlyPart");
70 KService::List::ConstIterator it = offers.constBegin();
71 while (!factory && it != offers.constEnd()) {
72 KPluginLoader loader(**it);
73 factory = loader.factory();
74 if (!factory) {
75 kDebug(500) << "Loading failed:" << loader.errorString();
77 ++it;
79 if (factory) {
80 kDebug(500) << "Trying to create a part";
81 previewPart = factory->create<KParts::ReadOnlyPart>(q, (QVariantList() << "Print/Preview"));
82 if (!previewPart) {
83 kDebug(500) << "Part creation failed";
88 bool FilePrinterPreviewPrivate::doPreview()
90 if (!QFile::exists(filename)) {
91 kWarning() << "Nothing was produced to be previewed";
92 return false;
95 getPart();
96 if (!previewPart) {
97 //TODO: error dialog
98 kWarning() << "Could not find a PS viewer for the preview dialog";
99 fail();
100 return false;
101 } else {
102 q->setMainWidget(previewPart->widget());
103 return previewPart->openUrl(filename);
107 void FilePrinterPreviewPrivate::fail()
109 if (!failMessage) {
110 failMessage = new QLabel(i18n("Could not load print preview part"), q);
112 q->setMainWidget(failMessage);
118 FilePrinterPreview::FilePrinterPreview( const QString &filename, QWidget *parent )
119 : KDialog( parent )
120 , d( new FilePrinterPreviewPrivate( this, filename ) )
122 kDebug(500) << "kdeprint: creating preview dialog";
124 // Set up the dialog
125 setCaption(i18n("Print Preview"));
126 setButtons(KDialog::Close);
129 FilePrinterPreview::~FilePrinterPreview()
131 delete d;
134 void FilePrinterPreview::showEvent(QShowEvent *event)
136 if (!event->spontaneous()) {
137 // being shown for the first time
138 if (!d->doPreview()) {
139 event->accept();
140 return;
143 KDialog::showEvent(event);
146 #include "fileprinterpreview.moc"