• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeprint
 

tdeprint

  • tdeprint
kprintpreview.cpp
1/*
2 * This file is part of the KDE libraries
3 * Copyright (c) 2001 Michael Goffioul <tdeprint@swing.be>
4 *
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License version 2 as published by the Free Software Foundation.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 **/
20
21#include "kprintpreview.h"
22#include "kmfactory.h"
23
24#include <tqfile.h>
25#include <tqlayout.h>
26#include <tqvbox.h>
27
28#include <tdeparts/part.h>
29#include <tdeaccel.h>
30#include <tdeaction.h>
31#include <klibloader.h>
32#include <ktrader.h>
33#include <kuserprofile.h>
34#include <krun.h>
35#include <tdeapplication.h>
36#include <tdestandarddirs.h>
37#include <tdelocale.h>
38#include <tdemessagebox.h>
39#include <kdebug.h>
40#include <tdeconfig.h>
41#include <tdetoolbar.h>
42#include <kmimetype.h>
43
44KPreviewProc::KPreviewProc()
45: TDEProcess()
46{
47 m_bOk = false;
48 connect(this, TQ_SIGNAL(processExited(TDEProcess*)), TQ_SLOT(slotProcessExited(TDEProcess*)));
49}
50
51KPreviewProc::~KPreviewProc()
52{
53}
54
55bool KPreviewProc::startPreview()
56{
57 if (start())
58 {
59 tdeApp->enter_loop();
60 return m_bOk;
61 }
62 else
63 return false;
64}
65
66void KPreviewProc::slotProcessExited(TDEProcess* proc)
67{
68 tdeApp->exit_loop();
69 if ( proc->normalExit() && proc->exitStatus() == 0 )
70 m_bOk = true;
71 else
72 kdDebug(500) << "KPreviewProc::slotProcessExited: normalExit=" << proc->normalExit()
73 << " exitStatus=" << proc->exitStatus() << endl;
74}
75
76//*******************************************************************************************
77
78class KPrintPreview::KPrintPreviewPrivate
79{
80public:
81 KPrintPreviewPrivate(KPrintPreview *dlg) : gvpart_(0)
82 {
83 mainwidget_ = new TQWidget(dlg, "MainWidget");
84 toolbar_ = new TDEToolBar(mainwidget_, "PreviewToolBar", true);
85 actions_ = new TDEActionCollection(dlg);
86 accel_ = new TDEAccel(dlg);
87 previewonly_ = false;
88 }
89 ~KPrintPreviewPrivate()
90 {
91 if (gvpart_) delete gvpart_;
92 }
93 void plugAction(TDEAction *act)
94 {
95 act->plug(toolbar_);
96 act->plugAccel(accel_);
97 }
98
99 KParts::ReadOnlyPart *gvpart_;
100 TDEToolBar *toolbar_;
101 TDEActionCollection *actions_;
102 TQWidget *mainwidget_;
103 TDEAccel *accel_;
104 bool previewonly_;
105};
106
107static KLibFactory* componentFactory()
108{
109 kdDebug(500) << "tdeprint: querying trader for 'application/postscript' service" << endl;
110 KLibFactory *factory(0);
111 factory = KLibLoader::self()->factory("libkghostviewpart");
112 if( factory )
113 return factory;
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)
116 {
117 KService::Ptr service = *it;
118 factory = KLibLoader::self()->factory(TQFile::encodeName(service->library()));
119 if (factory)
120 break;
121 }
122 if (!factory)
123 {
124 // nothing has been found, try to load directly the KGhostview part
125 factory = KLibLoader::self()->factory("libkghostviewpart");
126 }
127 return factory;
128}
129
130static bool continuePrint(const TQString& msg_, TQWidget *parent, bool previewOnly)
131{
132 TQString msg(msg_);
133 if (previewOnly)
134 {
135 KMessageBox::error(parent, msg);
136 return false;
137 }
138 else
139 {
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);
142 }
143}
144
145//*******************************************************************************************
146
147KPrintPreview::KPrintPreview(TQWidget *parent, bool previewOnly)
148: KDialogBase(parent, "PreviewDlg", true, i18n("Print Preview"), 0)
149{
150 kdDebug(500) << "tdeprint: creating preview dialog" << endl;
151 d = new KPrintPreviewPrivate(this);
152 d->previewonly_ = previewOnly;
153
154 // create main view and actions
155 setMainWidget(d->mainwidget_);
156 if (previewOnly)
157 KStdAction::close(this, TQ_SLOT(reject()), d->actions_, "close_print");
158 else
159 {
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");
162 }
163
164}
165
166KPrintPreview::~KPrintPreview()
167{
168 delete d;
169}
170
171void KPrintPreview::initView(KLibFactory *factory)
172{
173 // load the component
174 d->gvpart_ = (KParts::ReadOnlyPart*)factory->create(d->mainwidget_, "gvpart", "KParts::ReadOnlyPart");
175
176 // populate the toolbar
177 if (d->previewonly_)
178 d->plugAction(d->actions_->action("close_print"));
179 else
180 {
181 d->plugAction(d->actions_->action("continue_print"));
182 d->plugAction(d->actions_->action("stop_print"));
183 }
184 if (d->gvpart_)
185 {
186 TQDomNodeList l = d->gvpart_->domDocument().elementsByTagName( "ToolBar" );
187 if ( l.length() > 0 )
188 {
189 d->toolbar_->insertLineSeparator();
190 TQDomNodeList acts = l.item( 0 ).toElement().elementsByTagName( "Action" );
191 for ( uint i=0; i<acts.length(); i++ )
192 {
193 TQDomElement a = acts.item( i ).toElement();
194 if ( a.attribute( "name" ) == "goToPage" )
195 continue;
196 TDEAction *act = d->gvpart_->action( a );
197 if ( act != 0 )
198 d->plugAction( act );
199 }
200 }
201 /*
202 TDEAction *act;
203 d->toolbar_->insertLineSeparator();
204 if ((act = d->gvpart_->action("zoomIn")) != 0)
205 d->plugAction(act);
206 if ((act = d->gvpart_->action("zoomOut")) != 0)
207 d->plugAction(act);
208 d->toolbar_->insertSeparator();
209 if ((act = d->gvpart_->action("prevPage")) != 0)
210 d->plugAction(act);
211 if ((act = d->gvpart_->action("nextPage")) != 0)
212 d->plugAction(act);
213 */
214 }
215 d->toolbar_->setIconText(TDEToolBar::IconTextRight);
216 d->toolbar_->setBarPos(TDEToolBar::Top);
217 d->toolbar_->setMovingEnabled(false);
218 //d->adjustSize();
219
220 // construct the layout
221 TQVBoxLayout *l0 = new TQVBoxLayout(d->mainwidget_, 0, 0);
222 l0->addWidget(d->toolbar_, AlignTop);
223 if (d->gvpart_)
224 l0->addWidget(d->gvpart_->widget());
225
226 resize(855, 500);
227 setCaption(i18n("Print Preview"));
228}
229
230void KPrintPreview::openFile(const TQString& file)
231{
232 d->gvpart_->openURL(KURL(file));
233}
234
235bool KPrintPreview::isValid() const
236{
237 return (d->gvpart_ != 0);
238}
239
240bool KPrintPreview::preview(const TQString& file, bool previewOnly, WId parentId)
241{
242 KMimeType::Ptr mime = KMimeType::findByPath( file );
243 bool isPS = ( mime->name() == "application/postscript" );
244 if ( !isPS )
245 kdDebug( 500 ) << "Previewing a non PostScript file, built-in preview disabled" << endl;
246
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);
252 TQString exe;
253 if (!externalPreview && isPS && (factory = componentFactory()) != 0)
254 {
255 KPrintPreview dlg(parentW, previewOnly);
256 dlg.initView(factory);
257
258 if (dlg.isValid())
259 {
260 dlg.openFile(file);
261 return dlg.exec();
262 }
263 else
264 {
265 // do nothing at that point: try to use the other way around by
266 // using an external PS viewer if possible
267 }
268 }
269
270 // Either the PS viewer component was not found, or an external
271 // preview program has been specified
272 KPreviewProc proc;
273 if (externalPreview && isPS )
274 {
275 exe = conf->readPathEntry("PreviewCommand", "gv");
276 if (TDEStandardDirs::findExe(exe).isEmpty())
277 {
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);
283 }
284 proc << exe << file;
285 }
286 else
287 {
288 KService::Ptr serv = KServiceTypeProfile::preferredService( mime->name(), TQString::null );
289 if ( serv )
290 {
291 KURL url;
292 url.setPath( file );
293 TQStringList args = KRun::processDesktopExec( *serv, url, false );
294 proc << args;
295 exe = serv->name();
296 }
297 else
298 {
299 // in that case, the PS viewer component could not be loaded and no service
300 // could be found to view PS
301 TQString msg;
302 if ( isPS )
303 msg = i18n("Preview failed: neither the internal TDE PostScript "
304 "viewer (KGhostView) nor any other external PostScript "
305 "viewer could be found.");
306 else
307 msg = i18n( "Preview failed: TDE could not find any application "
308 "to preview files of type %1." ).arg( mime->name() );
309
310 return continuePrint(msg, parentW, previewOnly);
311 }
312 }
313
314 // start the preview process
315 if (!proc.startPreview())
316 {
317 TQString msg = i18n("Preview failed: unable to start program %1.").arg(exe);
318 return continuePrint(msg, parentW, previewOnly);
319 }
320 else if (!previewOnly)
321 {
322 return (KMessageBox::questionYesNo(parentW, i18n("Do you want to continue printing?"), TQString::null, KGuiItem(i18n("Print"),"document-print"), KStdGuiItem::cancel(), "continuePrinting") == KMessageBox::Yes);
323 }
324 else
325 return false;
326}
327
328#include "kprintpreview.moc"

tdeprint

Skip menu "tdeprint"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

tdeprint

Skip menu "tdeprint"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeprint by doxygen 1.9.4
This website is maintained by Timothy Pearson.