preparing for release of alpha.0.15
[Samba.git] / source / printing / print_cups.c
blobfff135e2a2a250739cf1003dd35968dd8190dea2
1 /*
2 * Support code for the Common UNIX Printing System ("CUPS")
4 * Copyright 1999 by Easy Software Products
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "includes.h"
22 #include "smb.h"
24 #ifdef HAVE_LIBCUPS
25 #include <cups/cups.h>
26 #include <cups/language.h>
28 extern int DEBUGLEVEL;
32 * 'cups_printer_fn()' - Call a function for every printer known to the
33 * system.
36 void cups_printer_fn(void (*fn)(char *, char *))
38 http_t *http; /* HTTP connection to server */
39 ipp_t *request, /* IPP Request */
40 *response; /* IPP Response */
41 ipp_attribute_t *attr; /* Current attribute */
42 cups_lang_t *language; /* Default language */
43 char *name, /* printer-name attribute */
44 *make_model; /* make_model attribute */
48 * Try to connect to the server...
51 if ((http = httpConnect(cupsServer(), ippPort())) == NULL)
52 return;
55 * Build a CUPS_GET_PRINTERS request, which requires the following
56 * attributes:
58 * attributes-charset
59 * attributes-natural-language
62 request = ippNew();
64 request->request.op.operation_id = CUPS_GET_PRINTERS;
65 request->request.op.request_id = 1;
67 language = cupsLangDefault();
69 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
70 "attributes-charset", NULL, cupsLangEncoding(language));
72 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
73 "attributes-natural-language", NULL, language->language);
76 * Do the request and get back a response...
79 if ((response = cupsDoRequest(http, request, "/")) == NULL)
81 httpClose(http);
82 return;
85 for (attr = response->attrs; attr != NULL;)
88 * Skip leading attributes until we hit a printer...
91 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
92 attr = attr->next;
94 if (attr == NULL)
95 break;
98 * Pull the needed attributes from this printer...
101 name = NULL;
102 make_model = NULL;
104 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
106 if (strcmp(attr->name, "printer-name") == 0 &&
107 attr->value_tag == IPP_TAG_NAME)
108 name = attr->values[0].string.text;
110 if (strcmp(attr->name, "printer-make-and-model") == 0 &&
111 attr->value_tag == IPP_TAG_TEXT)
112 make_model = attr->values[0].string.text;
114 attr = attr->next;
118 * See if we have everything needed...
121 if (name == NULL)
122 break;
124 (*fn)(name, make_model);
127 ippDelete(response);
128 httpClose(http);
133 * provide the equivalent of pcap_printername_ok() for SVID/XPG4 conforming
134 * systems.
136 int cups_printername_ok(char *name)
138 http_t *http; /* HTTP connection to server */
139 ipp_t *request, /* IPP Request */
140 *response; /* IPP Response */
141 ipp_attribute_t *attr; /* Current attribute */
142 cups_lang_t *language; /* Default language */
143 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
146 * Try to connect to the server...
149 if ((http = httpConnect(cupsServer(), ippPort())) == NULL)
150 return (0);
153 * Build a IPP_GET_PRINTER_ATTRS request, which requires the following
154 * attributes:
156 * attributes-charset
157 * attributes-natural-language
158 * printer-uri
161 request = ippNew();
163 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
164 request->request.op.request_id = 1;
166 language = cupsLangDefault();
168 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
169 "attributes-charset", NULL, cupsLangEncoding(language));
171 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
172 "attributes-natural-language", NULL, language->language);
174 snprintf(uri, sizeof(uri), "ipp://localhost/printers/%s", name);
176 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
177 "printer-uri", NULL, uri);
180 * Do the request and get back a response...
183 if ((response = cupsDoRequest(http, request, "/")) == NULL)
185 httpClose(http);
186 return (0);
189 httpClose(http);
191 if (response->request.status.status_code >= IPP_OK_CONFLICT)
193 ippDelete(response);
194 return (0);
196 else
198 ippDelete(response);
199 return (1);
203 #else
204 /* this keeps fussy compilers happy */
205 void print_cups_dummy(void) {}
206 #endif /* HAVE_LIBCUPS */