HEAD: fix compile problem in wvmapi/utils.
[wvapps.git] / wvprint / wvcupsmanager.cc
blob374f87327f8e2387d429c6178818cbca8bae11a5
1 /*
2 * Worldvisions Weaver Software:
3 * Copyright (C) 2004 Net Integration Technologies, Inc.
5 * This is the CUPS Manager class for WvPrint
6 *
7 * Author: Hubert Figuiere <hub@nit.ca>
8 */
12 #include <cups/cups.h>
14 #include "wvcupsmanager.h"
17 WvBaseCUPSManager* WvBaseCUPSManager::instance = NULL;
20 WvBaseCUPSManager* WvCUPSManager::get_instance(void)
22 if (WvBaseCUPSManager::instance == NULL)
24 WvBaseCUPSManager::instance = new WvCUPSManager();
26 return WvBaseCUPSManager::instance;
29 WvCUPSManager::WvCUPSManager()
30 : WvBaseCUPSManager()
32 cupsSetPasswordCB(cups_password_callback);
37 const char* WvCUPSManager::cups_password_callback(const char* prompt)
39 static WvString passwd = "scs";
41 cupsSetUser("_root");
43 return passwd;
46 bool WvCUPSManager::ipp_get_printer_state(const WvString& printer, ipp_pstate_t &state)
48 bool result = true;
49 ipp_t* request;
50 ipp_t* response;
52 http_t* http;
54 http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
55 if (http == NULL)
57 log(WvLog::Error, "IPP: http connection failed: %s\n", errno);
58 return false;
61 request = ippNew();
63 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
64 "attributes-charset", NULL, "utf-8");
65 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
66 "attributes-natural-language", NULL, "en");
67 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
68 "printer-uri", NULL, WvString("ipp://localhost/printers/%s",
69 printer));
70 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
71 request->request.any.request_id = 1;
73 response = cupsDoRequest(http, request, "/");
74 if (response)
76 if (response->request.status.status_code > IPP_OK_CONFLICT)
78 log(WvLog::Error, "Error getting printer status for printer %s", printer);
79 result = false;
81 else
83 ipp_attribute_t *attr;
84 if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL)
86 state = (ipp_pstate_t)attr->values[0].integer;
88 else
90 state = IPP_PRINTER_STOPPED;
93 log(WvLog::Debug1, "IPP Response: %s: %s\n",response->request.status.status_code,
94 ippErrorString(cupsLastError()));
96 ippDelete(response);
98 else
100 log(WvLog::Error, "IPP Error for printer %s: %s\n", printer, ippErrorString(cupsLastError()));
101 result = false;
104 httpClose(http);
105 return result;
108 bool WvCUPSManager::ipp_create_printer(const WvString& printer, const WvString& device,
109 WvStringParm description)
111 log(WvLog::Debug1, "ipp_create_printer: %s %s %s\n", printer, device, description);
112 return ipp_request_printer(CUPS_ADD_PRINTER, printer, device, description);
116 bool WvCUPSManager::ipp_delete_printer(const WvString& printer)
118 log(WvLog::Debug1, "ipp_delete_printer: %s\n", printer);
119 return ipp_request_printer(CUPS_DELETE_PRINTER, printer);
122 bool WvCUPSManager::ipp_enable_printer(const WvString& printer)
124 log(WvLog::Debug1, "ipp_enable_printer: %s\n", printer);
125 return ipp_request_printer(IPP_RESUME_PRINTER, printer);
129 bool WvCUPSManager::ipp_disable_printer(const WvString& printer)
131 log(WvLog::Debug1, "ipp_disable_printer: %s\n", printer);
132 return ipp_request_printer(IPP_PAUSE_PRINTER, printer);
137 bool WvCUPSManager::ipp_request_printer(ipp_op_t op, const WvString& printer,
138 const WvString& device, WvStringParm description)
140 ipp_t* request;
141 ipp_t* response;
143 http_t* http;
145 http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
146 if (http == NULL)
148 log(WvLog::Error, "IPP: http connection failed: %s\n", errno);
149 return false;
152 request = ippNew();
154 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
155 "attributes-charset", NULL, "utf-8");
156 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
157 "attributes-natural-language", NULL, "en");
158 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
159 "printer-uri", NULL, WvString("ipp://localhost/printers/%s",
160 printer));
161 if (!!device)
163 WvString real_device;
164 if (device[0] == '/')
166 real_device = WvString("file:%s", device);
168 else
170 real_device = device;
172 log(WvLog::Debug1, "adding device-uri %s\n", real_device);
173 ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_URI,
174 "device-uri", NULL, real_device);
177 if (!!description)
179 ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-info", NULL,
180 description);
183 if (op == CUPS_ADD_PRINTER)
185 ippAddBoolean (request, IPP_TAG_PRINTER, "printer-is-accepting-jobs", 1);
186 ippAddInteger(request, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state",
187 IPP_PRINTER_IDLE);
190 request->request.op.operation_id = op;
191 request->request.any.request_id = 1;
193 response = cupsDoRequest(http, request, "/admin/");
194 if (response)
196 log(WvLog::Debug1, "IPP Response: %s: %s\n",response->request.status.status_code, ippErrorString(cupsLastError()));
197 ippDelete(response);
199 else
201 log(WvLog::Error, "IPP Error: %s\n", ippErrorString(cupsLastError()));
204 httpClose(http);
205 return true;
209 bool WvCUPSManager::has_queue(WvStringParm queue)
211 bool found = false;
212 char **printers;
213 int i;
215 int num = cupsGetPrinters(&printers);
217 for (i = 0; i < num; i++)
219 if (!found && (printers[i] == queue))
221 found = true;
223 free(printers[i]);
225 free(printers);
227 return found;
230 int WvCUPSManager::count_queues(void)
232 int i;
233 char **printers;
235 int num = cupsGetPrinters(&printers);
237 for (i = 0; i < num; i++)
239 free(printers[i]);
241 free(printers);
242 return num;
245 bool WvCUPSManager::job_exists(WvStringParm queue, int job_num)
247 bool found = false;
248 cups_job_t *jobs;
249 int i;
251 int num_jobs = cupsGetJobs (&jobs, queue, 0, 0);
253 if (num_jobs > 0)
255 for (i = 0; i < num_jobs; i++)
257 if(!found && (WvString(jobs[i].id) == job_num))
259 found = true;
262 cupsFreeJobs(num_jobs, jobs);
265 return found;
268 int WvCUPSManager::count_jobs(WvStringParm queue)
270 cups_job_t *jobs;
271 int num_jobs = cupsGetJobs (&jobs, queue, 0, 0);
272 if (num_jobs > 0)
274 cupsFreeJobs(num_jobs, jobs);
276 return num_jobs;
280 bool WvCUPSManager::cancel_job(WvStringParm queue, int job_num)
282 int result = cupsCancelJob(queue, job_num);
284 return (result == 1);
291 int WvCUPSManager::get_cups_jobs(WvStringParm queue, PrintJobList &jobs, PrintQueue &q)
293 cups_job_t *cups_jobs;
294 int i;
295 int num_jobs = cupsGetJobs(&cups_jobs, queue, 0, 0);
297 for (i = 0; i < num_jobs; i++)
299 jobs.add(new PrintJob(&cups_jobs[i], log, q), true);
302 cupsFreeJobs(num_jobs, cups_jobs);
303 return num_jobs;
307 bool WvCUPSManager::set_default_printer(WvStringParm queue)
309 return ipp_request_printer(CUPS_SET_DEFAULT, queue);