2 * Worldvisions Weaver Software:
3 * Copyright (C) 2004 Net Integration Technologies, Inc.
5 * This is the CUPS Manager class for WvPrint
7 * Author: Hubert Figuiere <hub@nit.ca>
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()
32 cupsSetPasswordCB(cups_password_callback
);
37 const char* WvCUPSManager::cups_password_callback(const char* prompt
)
39 static WvString passwd
= "scs";
46 bool WvCUPSManager::ipp_get_printer_state(const WvString
& printer
, ipp_pstate_t
&state
)
54 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
57 log(WvLog::Error
, "IPP: http connection failed: %s\n", errno
);
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",
70 request
->request
.op
.operation_id
= IPP_GET_PRINTER_ATTRIBUTES
;
71 request
->request
.any
.request_id
= 1;
73 response
= cupsDoRequest(http
, request
, "/");
76 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
78 log(WvLog::Error
, "Error getting printer status for printer %s", printer
);
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
;
90 state
= IPP_PRINTER_STOPPED
;
93 log(WvLog::Debug1
, "IPP Response: %s: %s\n",response
->request
.status
.status_code
,
94 ippErrorString(cupsLastError()));
100 log(WvLog::Error
, "IPP Error for printer %s: %s\n", printer
, ippErrorString(cupsLastError()));
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
)
145 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
148 log(WvLog::Error
, "IPP: http connection failed: %s\n", errno
);
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",
163 WvString real_device
;
164 if (device
[0] == '/')
166 real_device
= WvString("file:%s", device
);
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
);
179 ippAddString(request
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info", NULL
,
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",
190 request
->request
.op
.operation_id
= op
;
191 request
->request
.any
.request_id
= 1;
193 response
= cupsDoRequest(http
, request
, "/admin/");
196 log(WvLog::Debug1
, "IPP Response: %s: %s\n",response
->request
.status
.status_code
, ippErrorString(cupsLastError()));
201 log(WvLog::Error
, "IPP Error: %s\n", ippErrorString(cupsLastError()));
209 bool WvCUPSManager::has_queue(WvStringParm queue
)
215 int num
= cupsGetPrinters(&printers
);
217 for (i
= 0; i
< num
; i
++)
219 if (!found
&& (printers
[i
] == queue
))
230 int WvCUPSManager::count_queues(void)
235 int num
= cupsGetPrinters(&printers
);
237 for (i
= 0; i
< num
; i
++)
245 bool WvCUPSManager::job_exists(WvStringParm queue
, int job_num
)
251 int num_jobs
= cupsGetJobs (&jobs
, queue
, 0, 0);
255 for (i
= 0; i
< num_jobs
; i
++)
257 if(!found
&& (WvString(jobs
[i
].id
) == job_num
))
262 cupsFreeJobs(num_jobs
, jobs
);
268 int WvCUPSManager::count_jobs(WvStringParm queue
)
271 int num_jobs
= cupsGetJobs (&jobs
, queue
, 0, 0);
274 cupsFreeJobs(num_jobs
, 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
;
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
);
307 bool WvCUPSManager::set_default_printer(WvStringParm queue
)
309 return ipp_request_printer(CUPS_SET_DEFAULT
, queue
);