Merged revisions 10677-11457 via svnmerge from
[wvapps.git] / wvprint / queue.cc
blob91cb0fea989f5f3be0f936b2a915eb5c071a48ae
1 /*
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2004 Net Integration Technologies, Inc.
5 * This is the PrintQueue class for handling various queue functions
6 * under WvPrint.
7 */
8 #include <wvpipe.h>
9 #include <wvtimeoutstream.h>
10 #include <sys/stat.h>
11 #include <sys/types.h>
13 #include <cups/cups.h>
15 #include "queue.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,
26 WvLog& log)
28 PrintQueue* queue = 0;
30 queue = new PrintQueue(name, cfg, log, false);
32 return queue;
35 PrintQueue* PrintQueue::new_alias(WvStringParm name, const UniConf cfg,
36 WvLog& log)
38 PrintQueue* queue = 0;
40 queue = new PrintQueue(name, cfg, log, true);
42 return queue;
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)
50 if (is_alias)
52 log(WvLog::Debug, "Setting up queue %s as an alias\n", name);
54 else
56 log(WvLog::Debug, "Setting up queue %s\n", name);
60 PrintQueue::~PrintQueue()
62 if (cups)
64 cups->ipp_delete_printer(name);
66 log(WvLog::Debug, "destroying PrintQueue() instance\n");
70 bool PrintQueue::reconfig(void)
72 WvString realqueuename;
73 if(is_alias)
75 realqueuename = cfg["aliases"][name].getme();
77 else
79 realqueuename = name;
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");
90 WvString description;
92 if (is_alias)
94 log(WvLog::Debug, "Creating alias queue %s for %s\n", name, realqueuename);
95 description = WvString("Alias to %s", realqueuename);
97 else
99 log(WvLog::Debug, "Creating printer queue for %s on device %s:%s\n", name, port, device);
100 description = qcfg["description"].getme();
103 if (cups)
105 configured = cups->ipp_create_printer(name, WvString("%s:%s", port, device),
106 description);
108 else
110 configured = true;
112 return configured;
117 bool PrintQueue::cancel(int id, WvStringParm agent)
119 if(cups)
121 return cups->cancel_job(name, id);
123 return false;
126 unsigned int PrintQueue::get_active_id() const
128 return 0;
129 // return active ? active->get_id() : 0;
132 size_t PrintQueue::job_count() const
134 if (cups)
136 return cups->count_jobs(name);
138 return 0;
142 bool PrintQueue::increase_priority(unsigned int job_id)
144 #if 0
145 PrintJobList::Iter i(jobs);
146 i.rewind();
147 while (i.next() && i().get_id() != job_id)
150 if (i().get_id() == job_id)
152 PrintJob *tempjob = new PrintJob(*i);
153 jobs.unlink(&(*i));
154 jobs.prepend(tempjob, true);
156 else
157 return false;
158 #endif
159 log("increase_priority() not implemented\n");
160 return true;
163 bool PrintQueue::job_exists(WvStringParm queuename, int job_num)
165 if (cups)
167 return cups->job_exists(queuename, job_num);
169 return false;
172 PrintJobVector& PrintQueue::get_jobs()
174 jobs.zap();
175 cups->get_cups_jobs(name, jobs, *this);
177 return jobs;
181 bool PrintQueue::enable(void)
183 bool is_configured = false;
184 if (!configured)
186 is_configured = reconfig();
188 if (configured && cups)
190 bool result = cups->ipp_enable_printer(name);
191 if (!result)
193 log(WvLog::Error, "Print queue enabling failed !\n");
196 return is_configured;
199 bool PrintQueue::disable(void)
201 bool is_configured = false;
202 if (!configured)
204 is_configured = reconfig();
206 if (configured && cups)
208 bool result = cups->ipp_disable_printer(name);
209 if (!result)
211 log(WvLog::Error, "Print queue disabling failed !\n");
214 return is_configured;
217 void PrintQueue::set_default(void)
219 if (cups)
221 bool result = cups->set_default_printer(name);
222 if (!result)
224 log(WvLog::Error, "Unable to make '%s' the default print queue.\n",
225 name);
230 void PrintQueue::restart(void)
232 if (cups)
234 bool result = cups->ipp_enable_printer(name);
235 if (!result)
237 log(WvLog::Error, "Unable restart queue '%s'.\n",
238 name);
245 Convert an ipp_pstate_t to a string
247 WvString PrintQueue::ipp_state_to_string(ipp_pstate_t state)
249 switch(state)
251 case IPP_PRINTER_IDLE:
252 return N_("Idle");
253 break;
254 case IPP_PRINTER_PROCESSING:
255 return N_("Processing...");
256 break;
257 case IPP_PRINTER_STOPPED:
258 return N_("Stopped");
259 break;
260 default:
261 break;
263 return N_("Unknown");
267 WvString PrintQueue::get_state(void)
269 WvString state_str;
270 ipp_pstate_t state = IPP_PRINTER_STOPPED;
272 if (!configured)
274 reconfig();
277 if (configured && cups && cups->ipp_get_printer_state(name, state))
279 state_str = ipp_state_to_string(state);
281 else
283 state_str = N_("ERROR");
285 return state_str;