21 #include "kprintpreview.h"
22 #include "kmfactory.h"
28 #include <tdeparts/part.h>
30 #include <tdeaction.h>
31 #include <klibloader.h>
33 #include <kuserprofile.h>
35 #include <tdeapplication.h>
36 #include <kstandarddirs.h>
37 #include <tdelocale.h>
38 #include <tdemessagebox.h>
40 #include <tdeconfig.h>
41 #include <tdetoolbar.h>
42 #include <kmimetype.h>
44 KPreviewProc::KPreviewProc()
48 connect(
this, TQ_SIGNAL(processExited(TDEProcess*)), TQ_SLOT(slotProcessExited(TDEProcess*)));
51 KPreviewProc::~KPreviewProc()
55 bool KPreviewProc::startPreview()
66 void KPreviewProc::slotProcessExited(TDEProcess* proc)
69 if ( proc->normalExit() && proc->exitStatus() == 0 )
72 kdDebug(500) <<
"KPreviewProc::slotProcessExited: normalExit=" << proc->normalExit()
73 <<
" exitStatus=" << proc->exitStatus() << endl;
78 class KPrintPreview::KPrintPreviewPrivate
81 KPrintPreviewPrivate(KPrintPreview *dlg) : gvpart_(0)
83 mainwidget_ =
new TQWidget(dlg,
"MainWidget");
84 toolbar_ =
new TDEToolBar(mainwidget_,
"PreviewToolBar",
true);
85 actions_ =
new TDEActionCollection(dlg);
86 accel_ =
new TDEAccel(dlg);
89 ~KPrintPreviewPrivate()
91 if (gvpart_)
delete gvpart_;
93 void plugAction(TDEAction *act)
96 act->plugAccel(accel_);
99 KParts::ReadOnlyPart *gvpart_;
100 TDEToolBar *toolbar_;
101 TDEActionCollection *actions_;
102 TQWidget *mainwidget_;
107 static KLibFactory* componentFactory()
109 kdDebug(500) <<
"tdeprint: querying trader for 'application/postscript' service" << endl;
110 KLibFactory *factory(0);
111 factory = KLibLoader::self()->factory(
"libkghostviewpart");
114 TDETrader::OfferList offers = TDETrader::self()->query(TQString::fromLatin1(
"application/postscript"), TQString::fromLatin1(
"KParts/ReadOnlyPart"), TQString::null, TQString::null);
115 for (TDETrader::OfferList::ConstIterator it = offers.begin(); it != offers.end(); ++it)
117 KService::Ptr service = *it;
118 factory = KLibLoader::self()->factory(TQFile::encodeName(service->library()));
125 factory = KLibLoader::self()->factory(
"libkghostviewpart");
130 static bool continuePrint(
const TQString& msg_, TQWidget *parent,
bool previewOnly)
135 KMessageBox::error(parent, msg);
140 msg.append(
" ").append(i18n(
"Do you want to continue printing anyway?"));
141 return (KMessageBox::warningContinueCancel(parent, msg, TQString::null, KGuiItem(i18n(
"Print"),
"document-print")) == KMessageBox::Continue);
147 KPrintPreview::KPrintPreview(TQWidget *parent,
bool previewOnly)
148 : KDialogBase(parent,
"PreviewDlg", true, i18n(
"Print Preview"), 0)
150 kdDebug(500) <<
"tdeprint: creating preview dialog" << endl;
151 d =
new KPrintPreviewPrivate(
this);
152 d->previewonly_ = previewOnly;
155 setMainWidget(d->mainwidget_);
157 KStdAction::close(
this, TQ_SLOT(reject()), d->actions_,
"close_print");
160 new TDEAction(i18n(
"Print"),
"document-print", TQt::Key_Return,
this, TQ_SLOT(accept()), d->actions_,
"continue_print");
161 new TDEAction(i18n(
"Cancel"),
"process-stop", TQt::Key_Escape,
this, TQ_SLOT(reject()), d->actions_,
"stop_print");
166 KPrintPreview::~KPrintPreview()
171 void KPrintPreview::initView(KLibFactory *factory)
174 d->gvpart_ = (KParts::ReadOnlyPart*)factory->create(d->mainwidget_,
"gvpart",
"KParts::ReadOnlyPart");
178 d->plugAction(d->actions_->action(
"close_print"));
181 d->plugAction(d->actions_->action(
"continue_print"));
182 d->plugAction(d->actions_->action(
"stop_print"));
186 TQDomNodeList l = d->gvpart_->domDocument().elementsByTagName(
"ToolBar" );
187 if ( l.length() > 0 )
189 d->toolbar_->insertLineSeparator();
190 TQDomNodeList acts = l.item( 0 ).toElement().elementsByTagName(
"Action" );
191 for ( uint i=0; i<acts.length(); i++ )
193 TQDomElement a = acts.item( i ).toElement();
194 if ( a.attribute(
"name" ) ==
"goToPage" )
196 TDEAction *act = d->gvpart_->action( a );
198 d->plugAction( act );
215 d->toolbar_->setIconText(TDEToolBar::IconTextRight);
216 d->toolbar_->setBarPos(TDEToolBar::Top);
217 d->toolbar_->setMovingEnabled(
false);
221 TQVBoxLayout *l0 =
new TQVBoxLayout(d->mainwidget_, 0, 0);
222 l0->addWidget(d->toolbar_, AlignTop);
224 l0->addWidget(d->gvpart_->widget());
227 setCaption(i18n(
"Print Preview"));
230 void KPrintPreview::openFile(
const TQString& file)
232 d->gvpart_->openURL(KURL(file));
235 bool KPrintPreview::isValid()
const
237 return (d->gvpart_ != 0);
240 bool KPrintPreview::preview(
const TQString& file,
bool previewOnly, WId parentId)
242 KMimeType::Ptr mime = KMimeType::findByPath( file );
243 bool isPS = ( mime->name() ==
"application/postscript" );
245 kdDebug( 500 ) <<
"Previewing a non PostScript file, built-in preview disabled" << endl;
247 TDEConfig *conf = KMFactory::self()->printConfig();
248 conf->setGroup(
"General");
249 KLibFactory *factory(0);
250 bool externalPreview = conf->readBoolEntry(
"ExternalPreview",
false);
251 TQWidget *parentW = TQWidget::find(parentId);
253 if (!externalPreview && isPS && (factory = componentFactory()) != 0)
255 KPrintPreview dlg(parentW, previewOnly);
256 dlg.initView(factory);
273 if (externalPreview && isPS )
275 exe = conf->readPathEntry(
"PreviewCommand",
"gv");
276 if (TDEStandardDirs::findExe(exe).isEmpty())
278 TQString msg = i18n(
"The preview program %1 cannot be found. "
279 "Check that the program is correctly installed and "
280 "located in a directory included in your PATH "
281 "environment variable.").arg(exe);
282 return continuePrint(msg, parentW, previewOnly);
288 KService::Ptr serv = KServiceTypeProfile::preferredService( mime->name(), TQString::null );
293 TQStringList args = KRun::processDesktopExec( *serv, url,
false );
303 msg = i18n(
"Preview failed: neither the internal TDE PostScript "
304 "viewer (KGhostView) nor any other external PostScript "
305 "viewer could be found.");
307 msg = i18n(
"Preview failed: TDE could not find any application "
308 "to preview files of type %1." ).arg( mime->name() );
310 return continuePrint(msg, parentW, previewOnly);
315 if (!proc.startPreview())
317 TQString msg = i18n(
"Preview failed: unable to start program %1.").arg(exe);
318 return continuePrint(msg, parentW, previewOnly);
320 else if (!previewOnly)
322 return (KMessageBox::questionYesNo(parentW, i18n(
"Do you want to continue printing?"), TQString::null, KGuiItem(i18n(
"Print"),
"document-print"), KStdGuiItem::cancel(),
"continuePrinting") == KMessageBox::Yes);
328 #include "kprintpreview.moc"