00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
#include "kprintpreview.h"
00022
#include "kmfactory.h"
00023
00024
#include <qfile.h>
00025
#include <qlayout.h>
00026
#include <qvbox.h>
00027
00028
#include <kparts/part.h>
00029
#include <kaccel.h>
00030
#include <kaction.h>
00031
#include <klibloader.h>
00032
#include <ktrader.h>
00033
#include <kuserprofile.h>
00034
#include <krun.h>
00035
#include <kapplication.h>
00036
#include <kstandarddirs.h>
00037
#include <klocale.h>
00038
#include <kmessagebox.h>
00039
#include <kdebug.h>
00040
#include <kconfig.h>
00041
#include <ktoolbar.h>
00042
#include <kmimetype.h>
00043
00044 KPreviewProc::KPreviewProc()
00045 : KProcess()
00046 {
00047 m_bOk =
false;
00048 connect(
this, SIGNAL(processExited(KProcess*)), SLOT(slotProcessExited(KProcess*)));
00049 }
00050
00051 KPreviewProc::~KPreviewProc()
00052 {
00053 }
00054
00055
bool KPreviewProc::startPreview()
00056 {
00057
if (start())
00058 {
00059 kapp->enter_loop();
00060
return m_bOk;
00061 }
00062
else
00063
return false;
00064 }
00065
00066
void KPreviewProc::slotProcessExited(KProcess* proc)
00067 {
00068 kapp->exit_loop();
00069
if ( proc->normalExit() && proc->exitStatus() == 0 )
00070 m_bOk =
true;
00071
else
00072 kdDebug(500) <<
"KPreviewProc::slotProcessExited: normalExit=" << proc->normalExit()
00073 <<
" exitStatus=" << proc->exitStatus() << endl;
00074 }
00075
00076
00077
00078
class KPrintPreview::KPrintPreviewPrivate
00079 {
00080
public:
00081 KPrintPreviewPrivate(KPrintPreview *dlg) : gvpart_(0)
00082 {
00083 mainwidget_ =
new QWidget(dlg,
"MainWidget");
00084 toolbar_ =
new KToolBar(mainwidget_,
"PreviewToolBar",
true);
00085 actions_ =
new KActionCollection(dlg);
00086 accel_ =
new KAccel(dlg);
00087 previewonly_ =
false;
00088 }
00089 ~KPrintPreviewPrivate()
00090 {
00091
if (gvpart_)
delete gvpart_;
00092 }
00093
void plugAction(KAction *act)
00094 {
00095 act->plug(toolbar_);
00096 act->plugAccel(accel_);
00097 }
00098
00099 KParts::ReadOnlyPart *gvpart_;
00100 KToolBar *toolbar_;
00101 KActionCollection *actions_;
00102
QWidget *mainwidget_;
00103 KAccel *accel_;
00104
bool previewonly_;
00105 };
00106
00107
static KLibFactory* componentFactory()
00108 {
00109 kdDebug(500) <<
"kdeprint: querying trader for 'application/postscript' service" << endl;
00110 KLibFactory *factory(0);
00111 KTrader::OfferList offers = KTrader::self()->query(QString::fromLatin1(
"application/postscript"), QString::fromLatin1(
"KParts/ReadOnlyPart"), QString::null, QString::null);
00112
for (KTrader::OfferList::ConstIterator it = offers.begin(); it != offers.end(); ++it)
00113 {
00114 KService::Ptr service = *it;
00115 factory = KLibLoader::self()->factory(QFile::encodeName(service->library()));
00116
if (factory)
00117
break;
00118 }
00119
if (!factory)
00120 {
00121
00122 factory = KLibLoader::self()->factory(
"libkghostviewpart");
00123 }
00124
return factory;
00125 }
00126
00127
static bool continuePrint(
const QString& msg_,
QWidget *parent,
bool previewOnly)
00128 {
00129
QString msg(msg_);
00130
if (previewOnly)
00131 {
00132 KMessageBox::error(parent, msg);
00133
return false;
00134 }
00135
else
00136 {
00137 msg.append(
" ").append(i18n(
"Do you want to continue printing anyway?"));
00138
return (KMessageBox::warningYesNo(parent, msg, QString::null, KGuiItem(i18n(
"Print"),
"fileprint"), KStdGuiItem::cancel()) == KMessageBox::Yes);
00139 }
00140 }
00141
00142
00143
00144 KPrintPreview::KPrintPreview(
QWidget *parent,
bool previewOnly)
00145 : KDialogBase(parent, "PreviewDlg", true, i18n("Print Preview"), 0)
00146 {
00147 kdDebug(500) <<
"kdeprint: creating preview dialog" << endl;
00148 d =
new KPrintPreviewPrivate(
this);
00149 d->previewonly_ = previewOnly;
00150
00151
00152 setMainWidget(d->mainwidget_);
00153
if (previewOnly)
00154 KStdAction::close(
this, SLOT(reject()), d->actions_,
"close_print");
00155
else
00156 {
00157
new KAction(i18n(
"Print"),
"fileprint", Qt::Key_Return,
this, SLOT(accept()), d->actions_,
"continue_print");
00158
new KAction(i18n(
"Cancel"),
"stop", Qt::Key_Escape,
this, SLOT(reject()), d->actions_,
"stop_print");
00159 }
00160
00161 }
00162
00163 KPrintPreview::~KPrintPreview()
00164 {
00165
delete d;
00166 }
00167
00168
void KPrintPreview::initView(KLibFactory *factory)
00169 {
00170
00171 d->gvpart_ = (KParts::ReadOnlyPart*)factory->create(d->mainwidget_,
"gvpart",
"KParts::ReadOnlyPart");
00172
00173
00174
if (d->previewonly_)
00175 d->plugAction(d->actions_->action(
"close_print"));
00176
else
00177 {
00178 d->plugAction(d->actions_->action(
"continue_print"));
00179 d->plugAction(d->actions_->action(
"stop_print"));
00180 }
00181
if (d->gvpart_)
00182 {
00183
QDomNodeList l = d->gvpart_->domDocument().elementsByTagName(
"ToolBar" );
00184
if ( l.length() > 0 )
00185 {
00186 d->toolbar_->insertLineSeparator();
00187
QDomNodeList acts = l.item( 0 ).toElement().elementsByTagName(
"Action" );
00188
for ( uint i=0; i<acts.length(); i++ )
00189 {
00190
QDomElement a = acts.item( i ).toElement();
00191
if ( a.attribute(
"name" ) ==
"goToPage" )
00192
continue;
00193 KAction *act = d->gvpart_->action( a );
00194
if ( act != 0 )
00195 d->plugAction( act );
00196 }
00197 }
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211 }
00212 d->toolbar_->setIconText(KToolBar::IconTextRight);
00213 d->toolbar_->setBarPos(KToolBar::Top);
00214 d->toolbar_->setMovingEnabled(
false);
00215
00216
00217
00218
QVBoxLayout *l0 =
new QVBoxLayout(d->mainwidget_, 0, 0);
00219 l0->addWidget(d->toolbar_, AlignTop);
00220
if (d->gvpart_)
00221 l0->addWidget(d->gvpart_->widget());
00222
00223 resize(855, 500);
00224 setCaption(i18n(
"Print Preview"));
00225 }
00226
00227
void KPrintPreview::openFile(
const QString& file)
00228 {
00229 d->gvpart_->openURL(KURL(file));
00230 }
00231
00232
bool KPrintPreview::isValid()
const
00233
{
00234
return (d->gvpart_ != 0);
00235 }
00236
00237
bool KPrintPreview::preview(
const QString& file,
bool previewOnly, WId parentId)
00238 {
00239 KMimeType::Ptr mime = KMimeType::findByPath( file );
00240
bool isPS = ( mime->name() ==
"application/postscript" );
00241
if ( !isPS )
00242 kdDebug( 500 ) <<
"Previewing a non PostScript file, built-in preview disabled" << endl;
00243
00244 KConfig *conf = KMFactory::self()->printConfig();
00245 conf->setGroup(
"General");
00246 KLibFactory *factory(0);
00247
bool externalPreview = conf->readBoolEntry(
"ExternalPreview",
false);
00248
QWidget *parentW = QWidget::find(parentId);
00249
QString exe;
00250
if (!externalPreview && isPS && (factory = componentFactory()) != 0)
00251 {
00252 KPrintPreview dlg(parentW, previewOnly);
00253 dlg.initView(factory);
00254
00255
if (dlg.isValid())
00256 {
00257 dlg.openFile(file);
00258
return dlg.exec();
00259 }
00260
else
00261
00262
00263 ;
00264 }
00265
00266
00267
00268 KPreviewProc proc;
00269
if (externalPreview && isPS )
00270 {
00271 exe = conf->readPathEntry(
"PreviewCommand",
"gv");
00272
if (KStandardDirs::findExe(exe).isEmpty())
00273 {
00274
QString msg = i18n(
"The preview program %1 cannot be found. "
00275
"Check that the program is correctly installed and "
00276
"located in a directory included in your PATH "
00277
"environment variable.").arg(exe);
00278
return continuePrint(msg, parentW, previewOnly);
00279 }
00280 proc << exe << file;
00281 }
00282
else
00283 {
00284 KService::Ptr serv = KServiceTypeProfile::preferredService( mime->name(), QString::null );
00285
if ( serv )
00286 {
00287 KURL url;
00288 url.setPath( file );
00289
QStringList args = KRun::processDesktopExec( *serv, url,
false );
00290 proc << args;
00291 exe = serv->name();
00292 }
00293
else
00294 {
00295
00296
00297
QString msg;
00298
if ( isPS )
00299 msg = i18n(
"Preview failed: neither the internal KDE PostScript "
00300
"viewer (KGhostView) nor any other external PostScript "
00301
"viewer could be found.");
00302
else
00303 msg = i18n(
"Preview failed: KDE could not find any application "
00304
"to preview files of type %1." ).arg( mime->name() );
00305
00306
return continuePrint(msg, parentW, previewOnly);
00307 }
00308 }
00309
00310
00311
if (!proc.startPreview())
00312 {
00313
QString msg = i18n(
"Preview failed: unable to start program %1.").arg(exe);
00314
return continuePrint(msg, parentW, previewOnly);
00315 }
00316
else if (!previewOnly)
00317 {
00318
return (KMessageBox::questionYesNo(parentW, i18n(
"Do you want to continue printing?"), QString::null, KGuiItem(i18n(
"Print"),
"fileprint"), KStdGuiItem::cancel(),
"continuePrinting") == KMessageBox::Yes);
00319 }
00320
else
00321
return false;
00322 }
00323
00324
#include "kprintpreview.moc"