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();
176 TQString name(ippGetName(attr));
177 if (name ==
"job-id") job->setId(ippGetInteger(attr, 0));
178 else if (name ==
"job-uri") job->setUri(TQString::fromLocal8Bit(ippGetString(attr, 0, NULL)));
179 else if (name ==
"job-name") job->setName(TQString::fromLocal8Bit(ippGetString(attr, 0, NULL)));
180 else if (name ==
"job-state")
182 switch (ippGetInteger(attr, 0))
184 case IPP_JOB_PENDING:
185 job->setState(KMJob::Queued);
188 job->setState(KMJob::Held);
190 case IPP_JOB_PROCESSING:
191 job->setState(KMJob::Printing);
193 case IPP_JOB_STOPPED:
194 job->setState(KMJob::Error);
196 case IPP_JOB_CANCELLED:
197 job->setState(KMJob::Cancelled);
199 case IPP_JOB_ABORTED:
200 job->setState(KMJob::Aborted);
202 case IPP_JOB_COMPLETED:
203 job->setState(KMJob::Completed);
206 job->setState(KMJob::Unknown);
210 else if (name ==
"job-k-octets") job->setSize(ippGetInteger(attr, 0));
211 else if (name ==
"job-originating-user-name") job->setOwner(TQString::fromLocal8Bit(ippGetString(attr, 0, NULL)));
212 else if (name ==
"job-k-octets-completed") job->setProcessedSize(ippGetInteger(attr, 0));
213 else if (name ==
"job-media-sheets") job->setPages(ippGetInteger(attr, 0));
214 else if (name ==
"job-media-sheets-completed") job->setProcessedPages(ippGetInteger(attr, 0));
215 else if (name ==
"job-printer-uri" && !pr->isRemote())
217 TQString str(ippGetString(attr, 0, NULL));
218 int p = str.findRev(
'/');
220 job->setPrinter(str.mid(p+1));
222 else if (name ==
"job-priority")
224 job->setAttribute(0, TQString::fromLatin1(
"%1").arg(ippGetInteger(attr, 0), 3));
226 else if (name ==
"job-billing")
228 job->setAttributeCount(2);
229 job->setAttribute(1, TQString::fromLocal8Bit(ippGetString(attr, 0, NULL)));
232 nextAttr = ippNextAttribute(req.request());
233 if (name.isEmpty() || (!nextAttr))
235 if (job->printer().isEmpty())
236 job->setPrinter(pr->printerName());
237 job->setRemote(pr->isRemote());
246 bool KMCupsJobManager::doPluginAction(
int ID,
const TQPtrList<KMJob>& jobs)
251 if (jobs.count() == 1)
252 return jobIppReport(jobs.getFirst());
255 return changePriority(jobs,
true);
257 return changePriority(jobs,
false);
259 return editJobAttributes(jobs.getFirst());
264 bool KMCupsJobManager::jobIppReport(KMJob *j)
268 req.setOperation(IPP_GET_JOB_ATTRIBUTES);
269 req.addURI(IPP_TAG_OPERATION,
"job-uri", j->uri());
279 if ((result=req.doRequest(
"/")))
280 static_cast<KMCupsManager*
>(KMManager::self())->ippReport(req, IPP_TAG_JOB, i18n(
"Job Report"));
282 KMManager::self()->setErrorMsg(i18n(
"Unable to retrieve job information: ")+req.statusMessage());
286 TQValueList<TDEAction*> KMCupsJobManager::createPluginActions(TDEActionCollection *coll)
288 TQValueList<TDEAction*> list;
291 list << (act =
new PluginAction(0, i18n(
"&Job IPP Report"),
"tdeprint_report", 0, coll,
"plugin_ipp"));
292 act->setGroup(
"plugin");
293 list << (act =
new PluginAction(1, i18n(
"&Increase Priority"),
"go-up", 0, coll,
"plugin_prioup"));
294 act->setGroup(
"plugin");
295 list << (act =
new PluginAction(2, i18n(
"&Decrease Priority"),
"go-down", 0, coll,
"plugin_priodown"));
296 act->setGroup(
"plugin");
297 list << (act =
new PluginAction(3, i18n(
"&Edit Attributes..."),
"edit", 0, coll,
"plugin_editjob"));
298 act->setGroup(
"plugin");
303 void KMCupsJobManager::validatePluginActions(TDEActionCollection *coll,
const TQPtrList<KMJob>& joblist)
305 TQPtrListIterator<KMJob> it(joblist);
307 for (; it.current(); ++it)
309 flag = (flag && it.current()->type() == KMJob::System
310 && (it.current()->state() == KMJob::Queued || it.current()->state() == KMJob::Held)
313 flag = (flag && joblist.count() > 0);
315 if ( ( a = coll->action(
"plugin_ipp" ) ) )
316 a->setEnabled( joblist.count() == 1 );
317 if ( ( a = coll->action(
"plugin_prioup" ) ) )
318 a->setEnabled( flag );
319 if ( ( a = coll->action(
"plugin_priodown" ) ) )
320 a->setEnabled( flag );
321 if ( ( a = coll->action(
"plugin_editjob" ) ) )
322 a->setEnabled( flag && ( joblist.count() == 1 ) );
325 bool KMCupsJobManager::changePriority(
const TQPtrList<KMJob>& jobs,
bool up)
327 TQPtrListIterator<KMJob> it(jobs);
329 for (; it.current() && result; ++it)
331 int value = it.current()->attribute(0).toInt();
332 if (up) value = TQMIN(value+10, 100);
333 else value = TQMAX(value-10, 1);
344 req.setOperation(IPP_SET_JOB_ATTRIBUTES);
345 req.addURI(IPP_TAG_OPERATION,
"job-uri", it.current()->uri());
346 req.addName(IPP_TAG_OPERATION,
"requesting-user-name", CupsInfos::self()->login());
347 req.addInteger(IPP_TAG_JOB,
"job-priority", value);
349 if (!(result = req.doRequest(
"/jobs/")))
350 KMManager::self()->setErrorMsg(i18n(
"Unable to change job priority: ")+req.statusMessage());
355 static TQString processRange(
const TQString& range)
357 TQStringList l = TQStringList::split(
',', range,
false);
359 for (TQStringList::ConstIterator it=l.begin(); it!=l.end(); ++it)
362 if ((*it).find(
'-') == -1)
363 s.append(
"-").append(*it);
367 s.truncate(s.length()-1);
371 bool KMCupsJobManager::editJobAttributes(KMJob *j)
375 req.setOperation(IPP_GET_JOB_ATTRIBUTES);
376 req.addURI(IPP_TAG_OPERATION,
"job-uri", j->uri());
385 if (!req.doRequest(
"/"))
387 KMManager::self()->setErrorMsg(i18n(
"Unable to retrieve job information: ")+req.statusMessage());
391 TQMap<TQString,TQString> opts = req.toMap(IPP_TAG_JOB);
393 if (opts.contains(
"copies"))
394 opts[
"kde-copies"] = opts[
"copies"];
395 if (opts.contains(
"page-set"))
396 opts[
"kde-pageset"] = (opts[
"page-set"] ==
"even" ?
"2" : (opts[
"page-set"] ==
"odd" ?
"1" :
"0"));
397 if (opts.contains(
"OutputOrder"))
398 opts[
"kde-pageorder"] = opts[
"OutputOrder"];
399 if (opts.contains(
"multiple-document-handling"))
400 opts[
"kde-collate"] = (opts[
"multiple-document-handling"] ==
"separate-documents-collated-copies" ?
"Collate" :
"Uncollate");
401 if (opts.contains(
"page-ranges"))
402 opts[
"kde-range"] = opts[
"page-ranges"];
405 KMPrinter *prt = KMManager::self()->findPrinter(j->printer());
408 KMManager::self()->setErrorMsg(i18n(
"Unable to find printer %1.").arg(j->printer()));
411 KMManager::self()->completePrinterShort(prt);
414 KPrinterPropertyDialog dlg(prt);
415 dlg.setDriver(KMManager::self()->loadPrinterDriver(prt));
416 KMFactory::self()->uiManager()->setupPrinterPropertyDialog(&dlg);
419 dlg.addPage(
new KPDriverPage(prt, dlg.driver(), &dlg));
420 dlg.addPage(
new KPCopiesPage(0, &dlg));
421 dlg.addPage(
new KPSchedulePage(&dlg));
422 dlg.addPage(
new KPTagsPage(
true, &dlg));
423 dlg.setOptions(opts);
424 dlg.enableSaveButton(
false);
425 dlg.setCaption(i18n(
"Attributes of Job %1@%2 (%3)").arg(j->id()).arg(j->printer()).arg(j->name()));
430 dlg.getOptions(opts,
true);
432 opts[
"copies"] = opts[
"kde-copies"];
433 opts[
"OutputOrder"] = opts[
"kde-pageorder"];
434 opts[
"multiple-document-handling"] = (opts[
"kde-collate"] ==
"Collate" ?
"separate-documents-collated-copies" :
"separate-documents-uncollated-copies");
435 opts[
"page-set"] = (opts[
"kde-pageset"] ==
"1" ?
"odd" : (opts[
"kde-pageset"] ==
"2" ?
"even" :
"all"));
437 opts[
"page-ranges"] = processRange(opts[
"kde-range"]);
440 req.setOperation(IPP_SET_JOB_ATTRIBUTES);
441 req.addURI(IPP_TAG_OPERATION,
"job-uri", j->uri());
442 req.addName(IPP_TAG_OPERATION,
"requesting-user-name", CupsInfos::self()->login());
445 if (!req.doRequest(
"/jobs/"))
447 KMManager::self()->setErrorMsg(i18n(
"Unable to set job attributes: ")+req.statusMessage());
455 #include "kmcupsjobmanager.moc"