20 #include "kmjobviewer.h"
21 #include "kmjobmanager.h"
22 #include "kmfactory.h"
24 #include "kmprinter.h"
25 #include "kmmanager.h"
26 #include "kmuimanager.h"
29 #include "kmconfigjobs.h"
30 #include "kmconfigpage.h"
33 #include <tdelistview.h>
34 #include <kstatusbar.h>
35 #include <tqpopupmenu.h>
36 #include <tdemessagebox.h>
37 #include <tdelocale.h>
38 #include <tdepopupmenu.h>
39 #include <tdeaction.h>
40 #include <kstdaction.h>
41 #include <kiconloader.h>
42 #include <tdeapplication.h>
44 #include <tdemenubar.h>
47 #include <tdeio/netaccess.h>
51 #include <tqlineedit.h>
52 #include <kdialogbase.h>
53 #include <tqcheckbox.h>
55 #include <tdeconfig.h>
58 #define m_manager KMFactory::self()->jobManager()
60 class KJobListView :
public TDEListView
63 KJobListView( TQWidget *parent = 0,
const char *name = 0 );
66 bool acceptDrag( TQDropEvent* )
const;
69 KJobListView::KJobListView( TQWidget *parent,
const char *name )
70 : TDEListView( parent, name )
72 setAcceptDrops(
true );
73 setDropVisualizer(
false );
76 bool KJobListView::acceptDrag( TQDropEvent *e )
const
78 if ( KURLDrag::canDecode( e ) )
81 return TDEListView::acceptDrag( e );
84 KMJobViewer::KMJobViewer(TQWidget *parent,
const char *name)
85 : TDEMainWindow(parent,name)
89 m_jobs.setAutoDelete(
false);
90 m_items.setAutoDelete(
false);
91 m_printers.setAutoDelete(
false);
92 m_type = KMJobManager::ActiveJobs;
94 m_standalone = ( parent == NULL );
96 setToolBarsMovable(
false);
101 setCaption(i18n(
"No Printer"));
102 TDEConfig *conf = KMFactory::self()->printConfig();
103 TQSize defSize( 550, 250 );
104 conf->setGroup(
"Jobs" );
105 resize( conf->readSizeEntry(
"Size", &defSize ) );
108 connect(KMFactory::self()->manager(), TQ_SIGNAL(printerListUpdated()),TQ_SLOT(slotPrinterListUpdated()));
111 KMJobViewer::~KMJobViewer()
115 kdDebug( 500 ) <<
"Destroying stand-alone job viewer window" << endl;
116 TDEConfig *conf = KMFactory::self()->printConfig();
117 conf->setGroup(
"Jobs" );
118 conf->writeEntry(
"Size", size() );
119 emit viewerDestroyed(
this);
124 void KMJobViewer::setPrinter(KMPrinter *p)
126 setPrinter((p ? p->printerName() : TQString::null));
129 void KMJobViewer::setPrinter(
const TQString& prname)
135 if (m_prname != prname)
140 m_view->setAcceptDrops( prname != i18n(
"All Printers" ) );
145 void KMJobViewer::updateCaption()
150 TQString pixname(
"document-print");
151 if (!m_prname.isEmpty())
153 setCaption(i18n(
"Print Jobs for %1").arg(m_prname));
154 KMPrinter *prt = KMManager::self()->findPrinter(m_prname);
156 pixname = prt->pixmap();
160 setCaption(i18n(
"No Printer"));
162 KWin::setIcons(winId(), DesktopIcon(pixname), SmallIcon(pixname));
165 void KMJobViewer::updateStatusBar()
170 int limit = m_manager->limit();
172 statusBar()->changeItem(i18n(
"Max.: %1").arg(i18n(
"Unlimited")), 0);
174 statusBar()->changeItem(i18n(
"Max.: %1").arg(limit), 0);
177 void KMJobViewer::addToManager()
179 if (m_prname == i18n(
"All Printers"))
182 TQPtrListIterator<KMPrinter> it(m_printers);
183 for (; it.current(); ++it)
184 m_manager->addPrinter(it.current()->printerName(), (KMJobManager::JobType)m_type, it.current()->isSpecial());
186 else if (!m_prname.isEmpty())
188 KMPrinter *prt = KMManager::self()->findPrinter( m_prname );
189 bool isSpecial = ( prt ? prt->isSpecial() : false );
190 m_manager->addPrinter(m_prname, (KMJobManager::JobType)m_type, isSpecial);
194 void KMJobViewer::removeFromManager()
196 if (m_prname == i18n(
"All Printers"))
198 TQPtrListIterator<KMPrinter> it(m_printers);
199 for (; it.current(); ++it)
200 m_manager->removePrinter(it.current()->printerName(), (KMJobManager::JobType)m_type);
202 else if (!m_prname.isEmpty())
204 m_manager->removePrinter(m_prname, (KMJobManager::JobType)m_type);
208 void KMJobViewer::refresh(
bool reload)
211 TQPtrListIterator<KMJob> it(m_manager->jobList(reload));
212 bool all = (m_prname == i18n(
"All Printers")), active = (m_type == KMJobManager::ActiveJobs);
213 for (; it.current(); ++it)
214 if ((all || it.current()->printer() == m_prname)
215 && ((it.current()->state() >= KMJob::Cancelled && !active)
216 || (it.current()->state() < KMJob::Cancelled && active))
217 && (m_username.isEmpty() || m_username == it.current()->owner()))
218 m_jobs.append(it.current());
229 emit jobsShown(
this, (m_jobs.count() != 0));
232 void KMJobViewer::init()
236 m_view =
new KJobListView(
this);
237 m_view->addColumn(i18n(
"Job ID"));
238 m_view->addColumn(i18n(
"Owner"));
239 m_view->addColumn(i18n(
"Name"), 150);
240 m_view->addColumn(i18n(
"Status",
"State"));
241 m_view->addColumn(i18n(
"Size (KB)"));
242 m_view->addColumn(i18n(
"Page(s)"));
243 m_view->setColumnAlignment(5,TQt::AlignRight|TQt::AlignVCenter);
244 connect( m_view, TQ_SIGNAL( dropped( TQDropEvent*, TQListViewItem* ) ), TQ_SLOT( slotDropped( TQDropEvent*, TQListViewItem* ) ) );
247 KMFactory::self()->uiManager()->setupJobViewer(m_view);
248 m_view->setFrameStyle(TQFrame::WinPanel|TQFrame::Sunken);
249 m_view->setLineWidth(1);
250 m_view->setSorting(0);
251 m_view->setAllColumnsShowFocus(
true);
252 m_view->setSelectionMode(TQListView::Extended);
253 connect(m_view,TQ_SIGNAL(selectionChanged()),TQ_SLOT(slotSelectionChanged()));
254 connect(m_view,TQ_SIGNAL(rightButtonPressed(TQListViewItem*,
const TQPoint&,
int)),TQ_SLOT(slotRightClicked(TQListViewItem*,
const TQPoint&,
int)));
255 setCentralWidget(m_view);
261 void KMJobViewer::initActions()
264 TDEAction *hact =
new TDEAction(i18n(
"&Hold"),
"process-stop",0,
this,TQ_SLOT(slotHold()),actionCollection(),
"job_hold");
265 TDEAction *ract =
new TDEAction(i18n(
"&Resume"),
"system-run",0,
this,TQ_SLOT(slotResume()),actionCollection(),
"job_resume");
266 TDEAction *dact =
new TDEAction(i18n(
"Remo&ve"),
"edittrash",TQt::Key_Delete,
this,TQ_SLOT(slotRemove()),actionCollection(),
"job_remove");
267 TDEAction *sact =
new TDEAction(i18n(
"Res&tart"),
"edit-redo",0,
this,TQ_SLOT(slotRestart()),actionCollection(),
"job_restart");
268 TDEActionMenu *mact =
new TDEActionMenu(i18n(
"&Move to Printer"),
"document-print",actionCollection(),
"job_move");
269 mact->setDelayed(
false);
270 connect(mact->popupMenu(),TQ_SIGNAL(activated(
int)),TQ_SLOT(slotMove(
int)));
271 connect(mact->popupMenu(),TQ_SIGNAL(aboutToShow()),KMTimer::self(),TQ_SLOT(hold()));
272 connect(mact->popupMenu(),TQ_SIGNAL(aboutToHide()),KMTimer::self(),TQ_SLOT(release()));
273 connect(mact->popupMenu(),TQ_SIGNAL(aboutToShow()),TQ_SLOT(slotShowMoveMenu()));
274 TDEToggleAction *tact =
new TDEToggleAction(i18n(
"&Toggle Completed Jobs"),
"history",0,actionCollection(),
"view_completed");
275 tact->setEnabled(m_manager->actions() & KMJob::ShowCompleted);
276 connect(tact,TQ_SIGNAL(toggled(
bool)),TQ_SLOT(slotShowCompleted(
bool)));
277 TDEToggleAction *uact =
new TDEToggleAction(i18n(
"Show Only User Jobs"),
"preferences-desktop-personal", 0, actionCollection(),
"view_user_jobs");
278 uact->setCheckedState(KGuiItem(i18n(
"Hide Only User Jobs"),
"preferences-desktop-personal"));
279 connect(uact, TQ_SIGNAL(toggled(
bool)), TQ_SLOT(slotUserOnly(
bool)));
280 m_userfield =
new TQLineEdit(0);
281 m_userfield->setText(getenv(
"USER"));
282 connect(m_userfield, TQ_SIGNAL(returnPressed()), TQ_SLOT(slotUserChanged()));
283 connect(uact, TQ_SIGNAL(toggled(
bool)), m_userfield, TQ_SLOT(setEnabled(
bool)));
284 m_userfield->setEnabled(
false);
285 m_userfield->setSizePolicy(TQSizePolicy(TQSizePolicy::Fixed, TQSizePolicy::Fixed));
286 KWidgetAction *ufact =
new KWidgetAction(m_userfield, i18n(
"User Name"), 0, 0, 0, actionCollection(),
"view_username");
290 m_pop =
new TQPopupMenu(
this);
291 connect(m_pop,TQ_SIGNAL(aboutToShow()),KMTimer::self(),TQ_SLOT(hold()));
292 connect(m_pop,TQ_SIGNAL(aboutToHide()),KMTimer::self(),TQ_SLOT(release()));
295 m_pop->insertSeparator();
298 m_pop->insertSeparator();
303 TDEActionMenu *fact =
new TDEActionMenu(i18n(
"&Select Printer"),
"tdeprint_printer", actionCollection(),
"filter_modify");
304 fact->setDelayed(
false);
305 connect(fact->popupMenu(),TQ_SIGNAL(activated(
int)),TQ_SLOT(slotPrinterSelected(
int)));
306 connect(fact->popupMenu(),TQ_SIGNAL(aboutToShow()),KMTimer::self(),TQ_SLOT(hold()));
307 connect(fact->popupMenu(),TQ_SIGNAL(aboutToHide()),KMTimer::self(),TQ_SLOT(release()));
308 connect(fact->popupMenu(),TQ_SIGNAL(aboutToShow()),TQ_SLOT(slotShowPrinterMenu()));
312 TDEToolBar *toolbar = toolBar();
315 toolbar->insertSeparator();
318 toolbar->insertSeparator();
320 toolbar->insertSeparator();
323 ufact->plug(toolbar);
327 KStdAction::quit(kapp,TQ_SLOT(quit()),actionCollection());
328 KStdAction::close(
this,TQ_SLOT(slotClose()),actionCollection());
329 KStdAction::preferences(
this, TQ_SLOT(slotConfigure()), actionCollection());
332 new TDEAction(i18n(
"Refresh"),
"reload",0,
this,TQ_SLOT(slotRefresh()),actionCollection(),
"refresh");
335 KStatusBar *statusbar = statusBar();
336 m_stickybox =
new TQCheckBox( i18n(
"Keep window permanent" ), statusbar );
338 TDEConfig *conf = KMFactory::self()->printConfig();
339 conf->setGroup(
"Jobs");
340 m_stickybox->setChecked(conf->readBoolEntry(
"KeepWindow",
false));
341 connect(m_stickybox, TQ_SIGNAL(toggled(
bool)), TQ_SLOT(slotKeepWindowChange(
bool)));
342 statusbar->addWidget( m_stickybox, 1,
false );
343 statusbar->insertItem(
" " + i18n(
"Max.: %1").arg(i18n(
"Unlimited"))+
" ", 0, 0,
true);
344 statusbar->setItemFixed(0);
351 slotSelectionChanged();
354 void KMJobViewer::buildPrinterMenu(TQPopupMenu *menu,
bool use_all,
bool use_specials)
359 TQPtrListIterator<KMPrinter> it(m_printers);
363 menu->insertItem(SmallIcon(
"document-print"), i18n(
"All Printers"), i++);
364 menu->insertSeparator();
366 for (; it.current(); ++it, i++)
368 if ( !it.current()->instanceName().isEmpty() ||
369 ( it.current()->isSpecial() && !use_specials ) )
371 menu->insertItem(SmallIcon(it.current()->pixmap()), it.current()->printerName(), i);
375 void KMJobViewer::slotKeepWindowChange(
bool val )
377 TDEConfig *conf = KMFactory::self()->printConfig();
378 conf->setGroup(
"Jobs");
379 conf->writeEntry(
"KeepWindow",val);
382 void KMJobViewer::slotShowMoveMenu()
384 TQPopupMenu *menu =
static_cast<TDEActionMenu*
>(actionCollection()->action(
"job_move"))->popupMenu();
385 buildPrinterMenu(menu,
false,
false);
388 void KMJobViewer::slotShowPrinterMenu()
390 TQPopupMenu *menu =
static_cast<TDEActionMenu*
>(actionCollection()->action(
"filter_modify"))->popupMenu();
391 buildPrinterMenu(menu,
true,
true);
394 void KMJobViewer::updateJobs()
396 TQPtrListIterator<JobItem> jit(m_items);
397 for (;jit.current();++jit)
398 jit.current()->setDiscarded(
true);
400 TQPtrListIterator<KMJob> it(m_jobs);
401 for (;it.current();++it)
403 KMJob *j(it.current());
404 JobItem *item = findItem(j->uri());
407 item->setDiscarded(
false);
411 m_items.append(
new JobItem(m_view,j));
414 for (uint i=0; i<m_items.count(); i++)
415 if (m_items.at(i)->isDiscarded())
417 delete m_items.take(i);
421 slotSelectionChanged();
424 JobItem* KMJobViewer::findItem(
const TQString& uri)
426 TQPtrListIterator<JobItem> it(m_items);
427 for (;it.current();++it)
428 if (it.current()->jobUri() == uri)
return it.current();
432 void KMJobViewer::slotSelectionChanged()
434 int acts = m_manager->actions();
437 bool completed(
true), remote(
false);
439 TQPtrListIterator<JobItem> it(m_items);
440 TQPtrList<KMJob> joblist;
442 joblist.setAutoDelete(
false);
443 for (;it.current();++it)
445 if (it.current()->isSelected())
452 if (it.current()->job()->type() == KMJob::Threaded) thread |= 0x1;
455 if (state == -1) state = it.current()->job()->state();
456 else if (state != 0 && state != it.current()->job()->state()) state = 0;
458 completed = (completed && it.current()->job()->isCompleted());
459 joblist.append(it.current()->job());
460 if (it.current()->job()->isRemote())
467 actionCollection()->action(
"job_remove")->setEnabled((thread == 1) || ( !completed && (state >= 0) && (acts & KMJob::Remove)));
468 actionCollection()->action(
"job_hold")->setEnabled( !completed && (thread == 2) && (state > 0) && (state != KMJob::Held) && (acts & KMJob::Hold));
469 actionCollection()->action(
"job_resume")->setEnabled( !completed && (thread == 2) && (state > 0) && (state == KMJob::Held) && (acts & KMJob::Resume));
470 actionCollection()->action(
"job_move")->setEnabled(!remote && !completed && (thread == 2) && (state >= 0) && (acts & KMJob::Move));
471 actionCollection()->action(
"job_restart")->setEnabled(!remote && (thread == 2) && (state >= 0) && (completed) && (acts & KMJob::Restart));
473 m_manager->validatePluginActions(actionCollection(), joblist);
476 void KMJobViewer::jobSelection(TQPtrList<KMJob>& l)
478 l.setAutoDelete(
false);
479 TQPtrListIterator<JobItem> it(m_items);
480 for (;it.current();++it)
481 if (it.current()->isSelected())
482 l.append(it.current()->job());
485 void KMJobViewer::send(
int cmd,
const TQString& name,
const TQString& arg)
487 KMTimer::self()->hold();
491 if (!m_manager->sendCommand(l,cmd,arg))
493 KMessageBox::error(
this,
"<qt>"+i18n(
"Unable to perform action \"%1\" on selected jobs. Error received from manager:").arg(name)+
"<p>"+KMManager::self()->errorMsg()+
"</p></qt>");
495 KMManager::self()->setErrorMsg(TQString::null);
500 KMTimer::self()->release();
503 void KMJobViewer::slotHold()
505 send(KMJob::Hold,i18n(
"Hold"));
508 void KMJobViewer::slotResume()
510 send(KMJob::Resume,i18n(
"Resume"));
513 void KMJobViewer::slotRemove()
515 send(KMJob::Remove,i18n(
"Remove"));
518 void KMJobViewer::slotRestart()
520 send(KMJob::Restart,i18n(
"Restart"));
523 void KMJobViewer::slotMove(
int prID)
525 if (prID >= 0 && prID < (
int)(m_printers.count()))
527 KMPrinter *p = m_printers.at(prID);
528 send(KMJob::Move,i18n(
"Move to %1").arg(p->printerName()),p->printerName());
532 void KMJobViewer::slotRightClicked(TQListViewItem*,
const TQPoint& p,
int)
534 if (m_pop) m_pop->popup(p);
537 void KMJobViewer::slotPrinterListUpdated()
542 void KMJobViewer::loadPrinters()
547 TQPtrListIterator<KMPrinter> it(*(KMFactory::self()->manager()->printerList(
false)));
548 for (;it.current();++it)
551 if ((it.current()->isPrinter() || it.current()->isClass(
false) ||
552 ( it.current()->isSpecial() && it.current()->isValid() ) )
553 && (it.current()->name() == it.current()->printerName()))
554 m_printers.append(it.current());
558 void KMJobViewer::slotPrinterSelected(
int prID)
560 if (prID >= 0 && prID < (
int)(m_printers.count()+1))
562 TQString prname = (prID == 0 ? i18n(
"All Printers") : m_printers.at(prID-1)->printerName());
563 emit printerChanged(
this, prname);
567 void KMJobViewer::slotRefresh()
572 void KMJobViewer::triggerRefresh()
581 emit refreshClicked();
584 void KMJobViewer::slotShowCompleted(
bool on)
587 m_type = (on ? KMJobManager::CompletedJobs : KMJobManager::ActiveJobs);
592 void KMJobViewer::slotClose()
597 void KMJobViewer::loadPluginActions()
599 int mpopindex(7), toolbarindex(!m_standalone?7:8), menuindex(7);
605 TDEAction *act = actionCollection()->action(
"job_restart");
606 for (
int i=0;i<act->containerCount();i++)
608 if (menuBar()->findItem(act->itemId(i), &menu))
610 menuindex = mpopindex = menu->indexOf(act->itemId(i))+1;
616 TQValueList<TDEAction*> acts = m_manager->createPluginActions(actionCollection());
617 for (TQValueListIterator<TDEAction*> it=acts.begin(); it!=acts.end(); ++it)
620 connect((*it), TQ_SIGNAL(activated(
int)), TQ_SLOT(pluginActionActivated(
int)));
623 (*it)->plug(toolBar(), toolbarindex++);
625 (*it)->plug(m_pop, mpopindex++);
627 (*it)->plug(
static_cast<TQPopupMenu*
>(menu), menuindex++);
631 void KMJobViewer::removePluginActions()
633 TQValueList<TDEAction*> acts = actionCollection()->actions(
"plugin");
634 for (TQValueListIterator<TDEAction*> it=acts.begin(); it!=acts.end(); ++it)
653 void KMJobViewer::reload()
655 removePluginActions();
666 for (
int c=m_view->columns()-1; c>5; c--)
667 m_view->removeColumn(c);
668 KMFactory::self()->uiManager()->setupJobViewer(m_view);
671 actionCollection()->action(
"view_completed")->setEnabled(m_manager->actions() & KMJob::ShowCompleted);
672 static_cast<TDEToggleAction*
>(actionCollection()->action(
"view_completed"))->setChecked(
false);
675 void KMJobViewer::closeEvent(TQCloseEvent *e)
677 if (m_standalone && !kapp->sessionSaving())
686 void KMJobViewer::pluginActionActivated(
int ID)
688 KMTimer::self()->hold();
690 TQPtrList<KMJob> joblist;
691 jobSelection(joblist);
692 if (!m_manager->doPluginAction(ID, joblist))
693 KMessageBox::error(
this,
"<qt>"+i18n(
"Operation failed.")+
"<p>"+KMManager::self()->errorMsg()+
"</p></qt>");
696 KMTimer::self()->release();
699 void KMJobViewer::slotUserOnly(
bool on)
701 m_username = (on ? m_userfield->text() : TQString::null);
705 void KMJobViewer::slotUserChanged()
707 if (m_userfield->isEnabled())
709 m_username = m_userfield->text();
714 void KMJobViewer::slotConfigure()
716 KMTimer::self()->hold();
718 KDialogBase dlg(
this, 0,
true, i18n(
"Print Job Settings"), KDialogBase::Ok|KDialogBase::Cancel);
719 KMConfigJobs *w =
new KMConfigJobs(&dlg);
720 dlg.setMainWidget(w);
722 TDEConfig *conf = KMFactory::self()->printConfig();
731 KMTimer::self()->release();
734 bool KMJobViewer::isSticky()
const
736 return ( m_stickybox ? m_stickybox->isChecked() :
false );
739 void KMJobViewer::slotDropped( TQDropEvent *e, TQListViewItem* )
745 KURLDrag::decode( e, uris );
746 for ( KURL::List::ConstIterator it = uris.begin();
747 it != uris.end(); ++it)
749 if ( TDEIO::NetAccess::download( *it, target, 0 ) )
753 if ( files.count() > 0 )
761 #include "kmjobviewer.moc"
This class is the main interface to access the TDE print framework.
bool printFiles(const TQStringList &files, bool removeafter=false, bool startviewer=true)
Prints the files given in argument.
bool autoConfigure(const TQString &prname=TQString::null, TQWidget *parent=0)
Configure the KPrinter object to be used with the printer named prname.