20 #include "kmcupsjobmanager.h"
21 #include "kmcupsmanager.h"
23 #include "cupsinfos.h"
24 #include "ipprequest.h"
25 #include "pluginaction.h"
27 #include "kprinterpropertydialog.h"
28 #include "kmuimanager.h"
29 #include "kmfactory.h"
30 #include "kpdriverpage.h"
31 #include "kpschedulepage.h"
32 #include "kpcopiespage.h"
33 #include "kptagspage.h"
35 #include <tdelocale.h>
41 KMCupsJobManager::KMCupsJobManager(TQObject *parent,
const char *name,
const TQStringList & )
42 : KMJobManager(parent,name)
46 KMCupsJobManager::~KMCupsJobManager()
50 int KMCupsJobManager::actions()
55 bool KMCupsJobManager::sendCommandSystemJob(
const TQPtrList<KMJob>& jobs,
int action,
const TQString& argstr)
61 TQPtrListIterator<KMJob> it(jobs);
62 for (;it.current() && value;++it)
67 req.addURI(IPP_TAG_OPERATION,
"job-uri",it.current()->uri());
68 req.addName(IPP_TAG_OPERATION,
"requesting-user-name",CupsInfos::self()->login());
83 req.setOperation(IPP_CANCEL_JOB);
86 req.setOperation(IPP_HOLD_JOB);
89 req.setOperation(IPP_RELEASE_JOB);
92 req.setOperation(IPP_RESTART_JOB);
95 if (argstr.isEmpty())
return false;
96 req.setOperation(CUPS_MOVE_JOB);
98 TQString::fromLatin1(
"ipp://%1/printers/%2").arg(CupsInfos::self()->hostaddr(),
100 req.addURI(IPP_TAG_OPERATION,
"job-printer-uri", uri);
106 if (!(value = req.doRequest(
"/jobs/")))
107 KMManager::self()->setErrorMsg(req.statusMessage());
113 bool KMCupsJobManager::listJobs(
const TQString& prname, KMJobManager::JobType type,
int limit)
117 CupsInfos *infos = CupsInfos::self();
120 keys.append(
"job-id");
121 keys.append(
"job-uri");
122 keys.append(
"job-name");
123 keys.append(
"job-state");
124 keys.append(
"job-printer-uri");
125 keys.append(
"job-k-octets");
126 keys.append(
"job-originating-user-name");
127 keys.append(
"job-k-octets-completed");
128 keys.append(
"job-media-sheets");
129 keys.append(
"job-media-sheets-completed");
130 keys.append(
"job-priority");
131 keys.append(
"job-billing");
133 req.setOperation(IPP_GET_JOBS);
136 KMPrinter *mp = KMManager::self()->findPrinter(prname);
140 if (!mp->uri().isEmpty())
142 req.addURI(IPP_TAG_OPERATION,
"printer-uri", mp->uri().prettyURL());
149 req.addURI(IPP_TAG_OPERATION,
"printer-uri", TQString(
"ipp://%1/%2/%3").arg(infos->hostaddr(),
150 (mp&&mp->isClass())?
"classes":
"printers", prname));
153 req.addKeyword(IPP_TAG_OPERATION,
"requested-attributes", keys);
154 if (type == KMJobManager::CompletedJobs)
155 req.addKeyword(IPP_TAG_OPERATION,
"which-jobs",TQString::fromLatin1(
"completed"));
157 req.addInteger(IPP_TAG_OPERATION,
"limit",limit);
160 if (req.doRequest(
"/"))
161 parseListAnswer(req, mp);
168 void KMCupsJobManager::parseListAnswer(IppRequest& req, KMPrinter *pr)
170 ipp_attribute_t *attr = req.first();
171 ipp_attribute_t *nextAttr;
172 KMJob *job =
new KMJob();
177 TQString name(ippGetName(attr));
178 if (name ==
"job-id") job->setId(ippGetInteger(attr, 0));
179 else if (name ==
"job-uri") job->setUri(TQString::fromLocal8Bit(ippGetString(attr, 0, NULL)));
180 else if (name ==
"job-name") job->setName(TQString::fromLocal8Bit(ippGetString(attr, 0, NULL)));
181 else if (name ==
"job-state")
183 switch (ippGetInteger(attr, 0))
185 case IPP_JOB_PENDING:
186 job->setState(KMJob::Queued);
189 job->setState(KMJob::Held);
191 case IPP_JOB_PROCESSING:
192 job->setState(KMJob::Printing);
194 case IPP_JOB_STOPPED:
195 job->setState(KMJob::Error);
197 case IPP_JOB_CANCELLED:
198 job->setState(KMJob::Cancelled);
200 case IPP_JOB_ABORTED:
201 job->setState(KMJob::Aborted);
203 case IPP_JOB_COMPLETED:
204 job->setState(KMJob::Completed);
207 job->setState(KMJob::Unknown);
211 else if (name ==
"job-k-octets") job->setSize(ippGetInteger(attr, 0));
212 else if (name ==
"job-originating-user-name") job->setOwner(TQString::fromLocal8Bit(ippGetString(attr, 0, NULL)));
213 else if (name ==
"job-k-octets-completed") job->setProcessedSize(ippGetInteger(attr, 0));
214 else if (name ==
"job-media-sheets") job->setPages(ippGetInteger(attr, 0));
215 else if (name ==
"job-media-sheets-completed") job->setProcessedPages(ippGetInteger(attr, 0));
216 else if (name ==
"job-printer-uri" && !pr->isRemote())
218 TQString str(ippGetString(attr, 0, NULL));
219 int p = str.findRev(
'/');
221 job->setPrinter(str.mid(p+1));
223 else if (name ==
"job-priority")
225 job->setAttribute(0, TQString::fromLatin1(
"%1").arg(ippGetInteger(attr, 0), 3));
227 else if (name ==
"job-billing")
229 job->setAttributeCount(2);
230 job->setAttribute(1, TQString::fromLocal8Bit(ippGetString(attr, 0, NULL)));
233 nextAttr = ippNextAttribute(req.request());
234 if (name.isEmpty() || (!nextAttr))
236 if (job->printer().isEmpty())
237 job->setPrinter(pr->printerName());
238 job->setRemote(pr->isRemote());
244 TQString name(attr->name);
245 if (name ==
"job-id") job->setId(attr->values[0].integer);
246 else if (name ==
"job-uri") job->setUri(TQString::fromLocal8Bit(attr->values[0].string.text));
247 else if (name ==
"job-name") job->setName(TQString::fromLocal8Bit(attr->values[0].string.text));
248 else if (name ==
"job-state")
250 switch (attr->values[0].integer)
252 case IPP_JOB_PENDING:
253 job->setState(KMJob::Queued);
256 job->setState(KMJob::Held);
258 case IPP_JOB_PROCESSING:
259 job->setState(KMJob::Printing);
261 case IPP_JOB_STOPPED:
262 job->setState(KMJob::Error);
264 case IPP_JOB_CANCELLED:
265 job->setState(KMJob::Cancelled);
267 case IPP_JOB_ABORTED:
268 job->setState(KMJob::Aborted);
270 case IPP_JOB_COMPLETED:
271 job->setState(KMJob::Completed);
274 job->setState(KMJob::Unknown);
278 else if (name ==
"job-k-octets") job->setSize(attr->values[0].integer);
279 else if (name ==
"job-originating-user-name") job->setOwner(TQString::fromLocal8Bit(attr->values[0].string.text));
280 else if (name ==
"job-k-octets-completed") job->setProcessedSize(attr->values[0].integer);
281 else if (name ==
"job-media-sheets") job->setPages(attr->values[0].integer);
282 else if (name ==
"job-media-sheets-completed") job->setProcessedPages(attr->values[0].integer);
283 else if (name ==
"job-printer-uri" && !pr->isRemote())
285 TQString str(attr->values[0].string.text);
286 int p = str.findRev(
'/');
288 job->setPrinter(str.mid(p+1));
290 else if (name ==
"job-priority")
292 job->setAttribute(0, TQString::fromLatin1(
"%1").arg(attr->values[0].integer, 3));
294 else if (name ==
"job-billing")
296 job->setAttributeCount(2);
297 job->setAttribute(1, TQString::fromLocal8Bit(attr->values[0].string.text));
300 if (name.isEmpty() || attr == req.last())
302 if (job->printer().isEmpty())
303 job->setPrinter(pr->printerName());
304 job->setRemote(pr->isRemote());
315 bool KMCupsJobManager::doPluginAction(
int ID,
const TQPtrList<KMJob>& jobs)
320 if (jobs.count() == 1)
321 return jobIppReport(jobs.getFirst());
324 return changePriority(jobs,
true);
326 return changePriority(jobs,
false);
328 return editJobAttributes(jobs.getFirst());
333 bool KMCupsJobManager::jobIppReport(KMJob *j)
337 req.setOperation(IPP_GET_JOB_ATTRIBUTES);
338 req.addURI(IPP_TAG_OPERATION,
"job-uri", j->uri());
348 if ((result=req.doRequest(
"/")))
349 static_cast<KMCupsManager*
>(KMManager::self())->ippReport(req, IPP_TAG_JOB, i18n(
"Job Report"));
351 KMManager::self()->setErrorMsg(i18n(
"Unable to retrieve job information: ")+req.statusMessage());
355 TQValueList<TDEAction*> KMCupsJobManager::createPluginActions(TDEActionCollection *coll)
357 TQValueList<TDEAction*> list;
360 list << (act =
new PluginAction(0, i18n(
"&Job IPP Report"),
"tdeprint_report", 0, coll,
"plugin_ipp"));
361 act->setGroup(
"plugin");
362 list << (act =
new PluginAction(1, i18n(
"&Increase Priority"),
"go-up", 0, coll,
"plugin_prioup"));
363 act->setGroup(
"plugin");
364 list << (act =
new PluginAction(2, i18n(
"&Decrease Priority"),
"go-down", 0, coll,
"plugin_priodown"));
365 act->setGroup(
"plugin");
366 list << (act =
new PluginAction(3, i18n(
"&Edit Attributes..."),
"edit", 0, coll,
"plugin_editjob"));
367 act->setGroup(
"plugin");
372 void KMCupsJobManager::validatePluginActions(TDEActionCollection *coll,
const TQPtrList<KMJob>& joblist)
374 TQPtrListIterator<KMJob> it(joblist);
376 for (; it.current(); ++it)
378 flag = (flag && it.current()->type() == KMJob::System
379 && (it.current()->state() == KMJob::Queued || it.current()->state() == KMJob::Held)
382 flag = (flag && joblist.count() > 0);
384 if ( ( a = coll->action(
"plugin_ipp" ) ) )
385 a->setEnabled( joblist.count() == 1 );
386 if ( ( a = coll->action(
"plugin_prioup" ) ) )
387 a->setEnabled( flag );
388 if ( ( a = coll->action(
"plugin_priodown" ) ) )
389 a->setEnabled( flag );
390 if ( ( a = coll->action(
"plugin_editjob" ) ) )
391 a->setEnabled( flag && ( joblist.count() == 1 ) );
394 bool KMCupsJobManager::changePriority(
const TQPtrList<KMJob>& jobs,
bool up)
396 TQPtrListIterator<KMJob> it(jobs);
398 for (; it.current() && result; ++it)
400 int value = it.current()->attribute(0).toInt();
401 if (up) value = TQMIN(value+10, 100);
402 else value = TQMAX(value-10, 1);
413 req.setOperation(IPP_SET_JOB_ATTRIBUTES);
414 req.addURI(IPP_TAG_OPERATION,
"job-uri", it.current()->uri());
415 req.addName(IPP_TAG_OPERATION,
"requesting-user-name", CupsInfos::self()->login());
416 req.addInteger(IPP_TAG_JOB,
"job-priority", value);
418 if (!(result = req.doRequest(
"/jobs/")))
419 KMManager::self()->setErrorMsg(i18n(
"Unable to change job priority: ")+req.statusMessage());
424 static TQString processRange(
const TQString& range)
426 TQStringList l = TQStringList::split(
',', range,
false);
428 for (TQStringList::ConstIterator it=l.begin(); it!=l.end(); ++it)
431 if ((*it).find(
'-') == -1)
432 s.append(
"-").append(*it);
436 s.truncate(s.length()-1);
440 bool KMCupsJobManager::editJobAttributes(KMJob *j)
444 req.setOperation(IPP_GET_JOB_ATTRIBUTES);
445 req.addURI(IPP_TAG_OPERATION,
"job-uri", j->uri());
454 if (!req.doRequest(
"/"))
456 KMManager::self()->setErrorMsg(i18n(
"Unable to retrieve job information: ")+req.statusMessage());
460 TQMap<TQString,TQString> opts = req.toMap(IPP_TAG_JOB);
462 if (opts.contains(
"copies"))
463 opts[
"kde-copies"] = opts[
"copies"];
464 if (opts.contains(
"page-set"))
465 opts[
"kde-pageset"] = (opts[
"page-set"] ==
"even" ?
"2" : (opts[
"page-set"] ==
"odd" ?
"1" :
"0"));
466 if (opts.contains(
"OutputOrder"))
467 opts[
"kde-pageorder"] = opts[
"OutputOrder"];
468 if (opts.contains(
"multiple-document-handling"))
469 opts[
"kde-collate"] = (opts[
"multiple-document-handling"] ==
"separate-documents-collated-copies" ?
"Collate" :
"Uncollate");
470 if (opts.contains(
"page-ranges"))
471 opts[
"kde-range"] = opts[
"page-ranges"];
474 KMPrinter *prt = KMManager::self()->findPrinter(j->printer());
477 KMManager::self()->setErrorMsg(i18n(
"Unable to find printer %1.").arg(j->printer()));
480 KMManager::self()->completePrinterShort(prt);
483 KPrinterPropertyDialog dlg(prt);
484 dlg.setDriver(KMManager::self()->loadPrinterDriver(prt));
485 KMFactory::self()->uiManager()->setupPrinterPropertyDialog(&dlg);
488 dlg.addPage(
new KPDriverPage(prt, dlg.driver(), &dlg));
489 dlg.addPage(
new KPCopiesPage(0, &dlg));
490 dlg.addPage(
new KPSchedulePage(&dlg));
491 dlg.addPage(
new KPTagsPage(
true, &dlg));
492 dlg.setOptions(opts);
493 dlg.enableSaveButton(
false);
494 dlg.setCaption(i18n(
"Attributes of Job %1@%2 (%3)").arg(j->id()).arg(j->printer()).arg(j->name()));
499 dlg.getOptions(opts,
true);
501 opts[
"copies"] = opts[
"kde-copies"];
502 opts[
"OutputOrder"] = opts[
"kde-pageorder"];
503 opts[
"multiple-document-handling"] = (opts[
"kde-collate"] ==
"Collate" ?
"separate-documents-collated-copies" :
"separate-documents-uncollated-copies");
504 opts[
"page-set"] = (opts[
"kde-pageset"] ==
"1" ?
"odd" : (opts[
"kde-pageset"] ==
"2" ?
"even" :
"all"));
506 opts[
"page-ranges"] = processRange(opts[
"kde-range"]);
509 req.setOperation(IPP_SET_JOB_ATTRIBUTES);
510 req.addURI(IPP_TAG_OPERATION,
"job-uri", j->uri());
511 req.addName(IPP_TAG_OPERATION,
"requesting-user-name", CupsInfos::self()->login());
514 if (!req.doRequest(
"/jobs/"))
516 KMManager::self()->setErrorMsg(i18n(
"Unable to set job attributes: ")+req.statusMessage());
524 #include "kmcupsjobmanager.moc"
ApplicationType
Defines the type of the application, this affects the GUI of the print dialog:
static void setApplicationType(ApplicationType type)
Sets the application type concerning the print dialog.
static ApplicationType applicationType()
Returns the application type concerning the print dialog.