2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2004 Net Integration Technologies, Inc.
5 * This is the PrintQueue class for handling various queue functions
9 #include <wvtimeoutstream.h>
11 #include <sys/types.h>
13 #include <cups/cups.h>
16 #include "wvcupsmanager.h"
17 #include "wvprintuniconfkeys.h"
19 // This macro dose nothing, just to markup the msgid for xgettext
20 #define N_(string) string
22 const unsigned int MAX_JOBS
= 1000;
24 /* I wodner what is the usefulness of that function */
25 PrintQueue
* PrintQueue::new_queue(WvStringParm name
, const UniConf cfg
,
28 PrintQueue
* queue
= 0;
30 queue
= new PrintQueue(name
, cfg
, log
, false);
35 PrintQueue
* PrintQueue::new_alias(WvStringParm name
, const UniConf cfg
,
38 PrintQueue
* queue
= 0;
40 queue
= new PrintQueue(name
, cfg
, log
, true);
45 PrintQueue::PrintQueue(WvStringParm _name
, const UniConf _cfg
, WvLog
&_log
, bool _is_alias
):
46 cfg(_cfg
), log(_log
), name(_name
),
47 refresh_interval(15), last_refreshed(0),
48 configured(false), cups(NULL
), is_alias(_is_alias
)
52 log(WvLog::Debug
, "Setting up queue %s as an alias\n", name
);
56 log(WvLog::Debug
, "Setting up queue %s\n", name
);
60 PrintQueue::~PrintQueue()
64 cups
->ipp_delete_printer(name
);
66 log(WvLog::Debug
, "destroying PrintQueue() instance\n");
70 bool PrintQueue::reconfig(void)
72 WvString realqueuename
;
75 realqueuename
= cfg
["aliases"][name
].getme();
82 UniConf qcfg
= cfg
["queues"][realqueuename
];
83 WvString device
= qcfg
["device"].getme("");
85 // default to parallel for the port, if there's none
86 // if there's none it's probably because in older versions
87 // there was no "port", and "parallel" will actually print
88 // to USB printers anyway
89 WvString port
= qcfg
["port"].getme("parallel");
94 log(WvLog::Debug
, "Creating alias queue %s for %s\n", name
, realqueuename
);
95 description
= WvString("Alias to %s", realqueuename
);
99 log(WvLog::Debug
, "Creating printer queue for %s on device %s:%s\n", name
, port
, device
);
100 description
= qcfg
["description"].getme();
105 configured
= cups
->ipp_create_printer(name
, WvString("%s:%s", port
, device
),
117 bool PrintQueue::cancel(int id
, WvStringParm agent
)
121 return cups
->cancel_job(name
, id
);
126 unsigned int PrintQueue::get_active_id() const
129 // return active ? active->get_id() : 0;
132 size_t PrintQueue::job_count() const
136 return cups
->count_jobs(name
);
142 bool PrintQueue::increase_priority(unsigned int job_id
)
145 PrintJobList::Iter
i(jobs
);
147 while (i
.next() && i().get_id() != job_id
)
150 if (i().get_id() == job_id
)
152 PrintJob
*tempjob
= new PrintJob(*i
);
154 jobs
.prepend(tempjob
, true);
159 log("increase_priority() not implemented\n");
163 bool PrintQueue::job_exists(WvStringParm queuename
, int job_num
)
167 return cups
->job_exists(queuename
, job_num
);
172 PrintJobVector
& PrintQueue::get_jobs()
175 cups
->get_cups_jobs(name
, jobs
, *this);
181 bool PrintQueue::enable(void)
183 bool is_configured
= false;
186 is_configured
= reconfig();
188 if (configured
&& cups
)
190 bool result
= cups
->ipp_enable_printer(name
);
193 log(WvLog::Error
, "Print queue enabling failed !\n");
196 return is_configured
;
199 bool PrintQueue::disable(void)
201 bool is_configured
= false;
204 is_configured
= reconfig();
206 if (configured
&& cups
)
208 bool result
= cups
->ipp_disable_printer(name
);
211 log(WvLog::Error
, "Print queue disabling failed !\n");
214 return is_configured
;
217 void PrintQueue::set_default(void)
221 bool result
= cups
->set_default_printer(name
);
224 log(WvLog::Error
, "Unable to make '%s' the default print queue.\n",
230 void PrintQueue::restart(void)
234 bool result
= cups
->ipp_enable_printer(name
);
237 log(WvLog::Error
, "Unable restart queue '%s'.\n",
245 Convert an ipp_pstate_t to a string
247 WvString
PrintQueue::ipp_state_to_string(ipp_pstate_t state
)
251 case IPP_PRINTER_IDLE
:
254 case IPP_PRINTER_PROCESSING
:
255 return N_("Processing...");
257 case IPP_PRINTER_STOPPED
:
258 return N_("Stopped");
263 return N_("Unknown");
267 WvString
PrintQueue::get_state(void)
270 ipp_pstate_t state
= IPP_PRINTER_STOPPED
;
277 if (configured
&& cups
&& cups
->ipp_get_printer_state(name
, state
))
279 state_str
= ipp_state_to_string(state
);
283 state_str
= N_("ERROR");