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

tdeprint

  • tdeprint
  • cups
kmcupsjobmanager.cpp
1/*
2 * This file is part of the KDE libraries
3 * Copyright (c) 2001 Michael Goffioul <tdeprint@swing.be>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License version 2 as published by the Free Software Foundation.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 **/
19
20#include "kmcupsjobmanager.h"
21#include "kmcupsmanager.h"
22#include "kmjob.h"
23#include "cupsinfos.h"
24#include "ipprequest.h"
25#include "pluginaction.h"
26#include "kprinter.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"
34
35#include <tdelocale.h>
36#include <kdebug.h>
37#include <kurl.h>
38
39#include "config.h"
40
41KMCupsJobManager::KMCupsJobManager(TQObject *parent, const char *name, const TQStringList & /*args*/)
42: KMJobManager(parent,name)
43{
44}
45
46KMCupsJobManager::~KMCupsJobManager()
47{
48}
49
50int KMCupsJobManager::actions()
51{
52 return KMJob::All;
53}
54
55bool KMCupsJobManager::sendCommandSystemJob(const TQPtrList<KMJob>& jobs, int action, const TQString& argstr)
56{
57 IppRequest req;
58 TQString uri;
59 bool value(true);
60
61 TQPtrListIterator<KMJob> it(jobs);
62 for (;it.current() && value;++it)
63 {
64 // hypothesis: job operation are always done on local jobs. The only operation
65 // allowed on remote jobs is listing (done elsewhere).
66
67 req.addURI(IPP_TAG_OPERATION,"job-uri",it.current()->uri());
68 req.addName(IPP_TAG_OPERATION,"requesting-user-name",CupsInfos::self()->login());
69 /*
70 TQString jobHost;
71 if (!it.current()->uri().isEmpty())
72 {
73 KURL url(it.current()->uri());
74 req.setHost(url.host());
75 req.setPort(url.port());
76 jobHost = url.host();
77 }
78 */
79
80 switch (action)
81 {
82 case KMJob::Remove:
83 req.setOperation(IPP_CANCEL_JOB);
84 break;
85 case KMJob::Hold:
86 req.setOperation(IPP_HOLD_JOB);
87 break;
88 case KMJob::Resume:
89 req.setOperation(IPP_RELEASE_JOB);
90 break;
91 case KMJob::Restart:
92 req.setOperation(IPP_RESTART_JOB);
93 break;
94 case KMJob::Move:
95 if (argstr.isEmpty()) return false;
96 req.setOperation(CUPS_MOVE_JOB);
97 uri =
98 TQString::fromLatin1("ipp://%1/printers/%2").arg(CupsInfos::self()->hostaddr(),
99 argstr);
100 req.addURI(IPP_TAG_OPERATION, "job-printer-uri", uri);
101 break;
102 default:
103 return false;
104 }
105
106 if (!(value = req.doRequest("/jobs/")))
107 KMManager::self()->setErrorMsg(req.statusMessage());
108 }
109
110 return value;
111}
112
113bool KMCupsJobManager::listJobs(const TQString& prname, KMJobManager::JobType type, int limit)
114{
115 IppRequest req;
116 TQStringList keys;
117 CupsInfos *infos = CupsInfos::self();
118
119 // wanted attributes
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");
132
133 req.setOperation(IPP_GET_JOBS);
134
135 // add printer-uri
136 KMPrinter *mp = KMManager::self()->findPrinter(prname);
137 if (!mp)
138 return false;
139
140 if (!mp->uri().isEmpty())
141 {
142 req.addURI(IPP_TAG_OPERATION, "printer-uri", mp->uri().prettyURL());
143 /*
144 req.setHost(mp->uri().host());
145 req.setPort(mp->uri().port());
146 */
147 }
148 else
149 req.addURI(IPP_TAG_OPERATION, "printer-uri", TQString("ipp://%1/%2/%3").arg(infos->hostaddr(),
150 (mp&&mp->isClass())?"classes":"printers", prname));
151
152 // other attributes
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"));
156 if (limit > 0)
157 req.addInteger(IPP_TAG_OPERATION,"limit",limit);
158
159 // send request
160 if (req.doRequest("/"))
161 parseListAnswer(req, mp);
162 else
163 return false;
164
165 return true;
166}
167
168void KMCupsJobManager::parseListAnswer(IppRequest& req, KMPrinter *pr)
169{
170 ipp_attribute_t *attr = req.first();
171 ipp_attribute_t *nextAttr;
172 KMJob *job = new KMJob();
173 TQString uri;
174 while (attr)
175 {
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")
181 {
182 switch (ippGetInteger(attr, 0))
183 {
184 case IPP_JOB_PENDING:
185 job->setState(KMJob::Queued);
186 break;
187 case IPP_JOB_HELD:
188 job->setState(KMJob::Held);
189 break;
190 case IPP_JOB_PROCESSING:
191 job->setState(KMJob::Printing);
192 break;
193 case IPP_JOB_STOPPED:
194 job->setState(KMJob::Error);
195 break;
196 case IPP_JOB_CANCELLED:
197 job->setState(KMJob::Cancelled);
198 break;
199 case IPP_JOB_ABORTED:
200 job->setState(KMJob::Aborted);
201 break;
202 case IPP_JOB_COMPLETED:
203 job->setState(KMJob::Completed);
204 break;
205 default:
206 job->setState(KMJob::Unknown);
207 break;
208 }
209 }
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())
216 {
217 TQString str(ippGetString(attr, 0, NULL));
218 int p = str.findRev('/');
219 if (p != -1)
220 job->setPrinter(str.mid(p+1));
221 }
222 else if (name == "job-priority")
223 {
224 job->setAttribute(0, TQString::fromLatin1("%1").arg(ippGetInteger(attr, 0), 3));
225 }
226 else if (name == "job-billing")
227 {
228 job->setAttributeCount(2);
229 job->setAttribute(1, TQString::fromLocal8Bit(ippGetString(attr, 0, NULL)));
230 }
231
232 nextAttr = ippNextAttribute(req.request());
233 if (name.isEmpty() || (!nextAttr))
234 {
235 if (job->printer().isEmpty())
236 job->setPrinter(pr->printerName());
237 job->setRemote(pr->isRemote());
238 addJob(job); // don't use job after this call !!!
239 job = new KMJob();
240 }
241 attr = nextAttr;
242 }
243 delete job;
244}
245
246bool KMCupsJobManager::doPluginAction(int ID, const TQPtrList<KMJob>& jobs)
247{
248 switch (ID)
249 {
250 case 0:
251 if (jobs.count() == 1)
252 return jobIppReport(jobs.getFirst());
253 break;
254 case 1:
255 return changePriority(jobs, true);
256 case 2:
257 return changePriority(jobs, false);
258 case 3:
259 return editJobAttributes(jobs.getFirst());
260 }
261 return false;
262}
263
264bool KMCupsJobManager::jobIppReport(KMJob *j)
265{
266 IppRequest req;
267
268 req.setOperation(IPP_GET_JOB_ATTRIBUTES);
269 req.addURI(IPP_TAG_OPERATION, "job-uri", j->uri());
270 bool result(true);
271 /*
272 if (!j->uri().isEmpty())
273 {
274 KURL url(j->uri());
275 req.setHost(url.host());
276 req.setPort(url.port());
277 }
278 */
279 if ((result=req.doRequest("/")))
280 static_cast<KMCupsManager*>(KMManager::self())->ippReport(req, IPP_TAG_JOB, i18n("Job Report"));
281 else
282 KMManager::self()->setErrorMsg(i18n("Unable to retrieve job information: ")+req.statusMessage());
283 return result;
284}
285
286TQValueList<TDEAction*> KMCupsJobManager::createPluginActions(TDEActionCollection *coll)
287{
288 TQValueList<TDEAction*> list;
289 TDEAction *act(0);
290
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");
299
300 return list;
301}
302
303void KMCupsJobManager::validatePluginActions(TDEActionCollection *coll, const TQPtrList<KMJob>& joblist)
304{
305 TQPtrListIterator<KMJob> it(joblist);
306 bool flag(true);
307 for (; it.current(); ++it)
308 {
309 flag = (flag && it.current()->type() == KMJob::System
310 && (it.current()->state() == KMJob::Queued || it.current()->state() == KMJob::Held)
311 /*&& !it.current()->isRemote()*/);
312 }
313 flag = (flag && joblist.count() > 0);
314 TDEAction *a;
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 ) );
323}
324
325bool KMCupsJobManager::changePriority(const TQPtrList<KMJob>& jobs, bool up)
326{
327 TQPtrListIterator<KMJob> it(jobs);
328 bool result(true);
329 for (; it.current() && result; ++it)
330 {
331 int value = it.current()->attribute(0).toInt();
332 if (up) value = TQMIN(value+10, 100);
333 else value = TQMAX(value-10, 1);
334
335 IppRequest req;
336 /*
337 if (!it.current()->uri().isEmpty())
338 {
339 KURL url(it.current()->uri());
340 req.setHost(url.host());
341 req.setPort(url.port());
342 }
343 */
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);
348
349 if (!(result = req.doRequest("/jobs/")))
350 KMManager::self()->setErrorMsg(i18n("Unable to change job priority: ")+req.statusMessage());
351 }
352 return result;
353}
354
355static TQString processRange(const TQString& range)
356{
357 TQStringList l = TQStringList::split(',', range, false);
358 TQString s;
359 for (TQStringList::ConstIterator it=l.begin(); it!=l.end(); ++it)
360 {
361 s.append(*it);
362 if ((*it).find('-') == -1)
363 s.append("-").append(*it);
364 s.append(",");
365 }
366 if (!s.isEmpty())
367 s.truncate(s.length()-1);
368 return s;
369}
370
371bool KMCupsJobManager::editJobAttributes(KMJob *j)
372{
373 IppRequest req;
374
375 req.setOperation(IPP_GET_JOB_ATTRIBUTES);
376 req.addURI(IPP_TAG_OPERATION, "job-uri", j->uri());
377 /*
378 if (!j->uri().isEmpty())
379 {
380 KURL url(j->uri());
381 req.setHost(url.host());
382 req.setPort(url.port());
383 }
384 */
385 if (!req.doRequest("/"))
386 {
387 KMManager::self()->setErrorMsg(i18n("Unable to retrieve job information: ")+req.statusMessage());
388 return false;
389 }
390
391 TQMap<TQString,TQString> opts = req.toMap(IPP_TAG_JOB);
392 // translate the "Copies" option to non-CUPS syntax
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"];
403
404 // find printer and construct dialog
405 KMPrinter *prt = KMManager::self()->findPrinter(j->printer());
406 if (!prt)
407 {
408 KMManager::self()->setErrorMsg(i18n("Unable to find printer %1.").arg(j->printer()));
409 return false;
410 }
411 KMManager::self()->completePrinterShort(prt);
412 KPrinter::ApplicationType oldAppType = KPrinter::applicationType();
413 KPrinter::setApplicationType(KPrinter::StandAlone);
414 KPrinterPropertyDialog dlg(prt);
415 dlg.setDriver(KMManager::self()->loadPrinterDriver(prt));
416 KMFactory::self()->uiManager()->setupPrinterPropertyDialog(&dlg);
417 KPrinter::setApplicationType( oldAppType );
418 if (dlg.driver())
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()));
426 if (dlg.exec())
427 {
428 opts.clear();
429 // include default values to override non-default values
430 dlg.getOptions(opts, true);
431 // translate the "Copies" options from non-CUPS syntax
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"));
436 // it seems CUPS is buggy. Disable page-ranges modification, otherwise nothing gets printed
437 opts["page-ranges"] = processRange(opts["kde-range"]);
438
439 req.init();
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());
443 req.setMap(opts);
444 //req.dump(1);
445 if (!req.doRequest("/jobs/"))
446 {
447 KMManager::self()->setErrorMsg(i18n("Unable to set job attributes: ")+req.statusMessage());
448 return false;
449 }
450 }
451
452 return true;
453}
454
455#include "kmcupsjobmanager.moc"
KPrinter::ApplicationType
ApplicationType
Defines the type of the application, this affects the GUI of the print dialog:
Definition kprinter.h:126
KPrinter::setApplicationType
static void setApplicationType(ApplicationType type)
Sets the application type concerning the print dialog.
Definition kprinter.cpp:265
KPrinter::applicationType
static ApplicationType applicationType()
Returns the application type concerning the print dialog.
Definition kprinter.cpp:270

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.15.0
This website is maintained by Timothy Pearson.