]> git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/classes.c
Load cups into easysw/current.
[thirdparty/cups.git] / cgi-bin / classes.c
1 /*
2 * "$Id: classes.c 5160 2006-02-24 01:14:18Z mike $"
3 *
4 * Class status CGI for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2006 by Easy Software Products.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
26 * main() - Main entry for CGI.
27 * show_all_classes() - Show all classes...
28 * show_class() - Show a single class.
29 */
30
31 /*
32 * Include necessary headers...
33 */
34
35 #include "cgi-private.h"
36
37
38 /*
39 * Local functions...
40 */
41
42 void show_all_classes(http_t *http, const char *username);
43 void show_class(http_t *http, const char *printer);
44
45
46 /*
47 * 'main()' - Main entry for CGI.
48 */
49
50 int /* O - Exit status */
51 main(int argc, /* I - Number of command-line arguments */
52 char *argv[]) /* I - Command-line arguments */
53 {
54 const char *pclass; /* Class name */
55 const char *user; /* Username */
56 http_t *http; /* Connection to the server */
57 ipp_t *request, /* IPP request */
58 *response; /* IPP response */
59 ipp_attribute_t *attr; /* IPP attribute */
60 const char *op; /* Operation to perform, if any */
61 static const char *def_attrs[] = /* Attributes for default printer */
62 {
63 "printer-name",
64 "printer-uri-supported"
65 };
66
67
68 /*
69 * Get any form variables...
70 */
71
72 cgiInitialize();
73
74 op = cgiGetVariable("OP");
75
76 /*
77 * Set the web interface section...
78 */
79
80 cgiSetVariable("SECTION", "classes");
81
82 /*
83 * See if we are displaying a printer or all classes...
84 */
85
86 if ((pclass = getenv("PATH_INFO")) != NULL)
87 pclass ++;
88
89 /*
90 * See who is logged in...
91 */
92
93 if ((user = getenv("REMOTE_USER")) == NULL)
94 user = "guest";
95
96 /*
97 * Connect to the HTTP server...
98 */
99
100 http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
101
102 /*
103 * Get the default printer...
104 */
105
106 if (!op)
107 {
108 /*
109 * Get the default destination...
110 */
111
112 request = ippNewRequest(CUPS_GET_DEFAULT);
113
114 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
115 "requested-attributes",
116 sizeof(def_attrs) / sizeof(def_attrs[0]), NULL, def_attrs);
117
118 if ((response = cupsDoRequest(http, request, "/")) != NULL)
119 {
120 if ((attr = ippFindAttribute(response, "printer-name", IPP_TAG_NAME)) != NULL)
121 cgiSetVariable("DEFAULT_NAME", attr->values[0].string.text);
122
123 if ((attr = ippFindAttribute(response, "printer-uri-supported", IPP_TAG_URI)) != NULL)
124 {
125 char url[HTTP_MAX_URI]; /* New URL */
126
127
128 cgiSetVariable("DEFAULT_URI",
129 cgiRewriteURL(attr->values[0].string.text,
130 url, sizeof(url), NULL));
131 }
132
133 ippDelete(response);
134 }
135
136 /*
137 * See if we need to show a list of classes or the status of a
138 * single printer...
139 */
140
141 if (!pclass)
142 show_all_classes(http, user);
143 else
144 show_class(http, pclass);
145 }
146 else if (!strcasecmp(op, "print-test-page") && pclass)
147 cgiPrintTestPage(http, pclass);
148 else if (!strcasecmp(op, "move-jobs") && pclass)
149 cgiMoveJobs(http, pclass, 0);
150 else
151 {
152 /*
153 * Unknown/bad operation...
154 */
155
156 if (pclass)
157 cgiStartHTML(pclass);
158 else
159 cgiStartHTML(cgiText(_("Classes")));
160
161 cgiCopyTemplateLang("error-op.tmpl");
162 cgiEndHTML();
163 }
164
165 /*
166 * Close the HTTP server connection...
167 */
168
169 httpClose(http);
170
171 /*
172 * Return with no errors...
173 */
174
175 return (0);
176 }
177
178
179 /*
180 * 'show_all_classes()' - Show all classes...
181 */
182
183 void
184 show_all_classes(http_t *http, /* I - Connection to server */
185 const char *user) /* I - Username */
186 {
187 int i; /* Looping var */
188 ipp_t *request, /* IPP request */
189 *response; /* IPP response */
190 cups_array_t *classes; /* Array of class objects */
191 ipp_attribute_t *pclass; /* Class object */
192 int ascending, /* Order of classes (0 = descending) */
193 first, /* First class to show */
194 count; /* Number of classes */
195 const char *var; /* Form variable */
196 void *search; /* Search data */
197 char url[1024], /* URL for prev/next/this */
198 *urlptr, /* Position in URL */
199 *urlend; /* End of URL */
200
201
202 /*
203 * Show the standard header...
204 */
205
206 cgiStartHTML(cgiText(_("Classes")));
207
208 /*
209 * Build a CUPS_GET_CLASSES request, which requires the following
210 * attributes:
211 *
212 * attributes-charset
213 * attributes-natural-language
214 * requesting-user-name
215 */
216
217 request = ippNewRequest(CUPS_GET_CLASSES);
218
219 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
220 "requesting-user-name", NULL, user);
221
222 cgiGetAttributes(request, "classes.tmpl");
223
224 /*
225 * Do the request and get back a response...
226 */
227
228 if ((response = cupsDoRequest(http, request, "/")) != NULL)
229 {
230 /*
231 * Get a list of matching job objects.
232 */
233
234 if ((var = cgiGetVariable("QUERY")) != NULL)
235 search = cgiCompileSearch(var);
236 else
237 search = NULL;
238
239 classes = cgiGetIPPObjects(response, search);
240 count = cupsArrayCount(classes);
241
242 if (search)
243 cgiFreeSearch(search);
244
245 /*
246 * Figure out which classes to display...
247 */
248
249 if ((var = cgiGetVariable("FIRST")) != NULL)
250 first = atoi(var);
251 else
252 first = 0;
253
254 if (first >= count)
255 first = count - CUPS_PAGE_MAX;
256
257 first = (first / CUPS_PAGE_MAX) * CUPS_PAGE_MAX;
258
259 if (first < 0)
260 first = 0;
261
262 sprintf(url, "%d", count);
263 cgiSetVariable("TOTAL", url);
264
265 if ((var = cgiGetVariable("ORDER")) != NULL)
266 ascending = !strcasecmp(var, "asc");
267 else
268 ascending = 1;
269
270 if (ascending)
271 {
272 for (i = 0, pclass = (ipp_attribute_t *)cupsArrayIndex(classes, first);
273 i < CUPS_PAGE_MAX && pclass;
274 i ++, pclass = (ipp_attribute_t *)cupsArrayNext(classes))
275 cgiSetIPPObjectVars(pclass, NULL, i);
276 }
277 else
278 {
279 for (i = 0, pclass = (ipp_attribute_t *)cupsArrayIndex(classes, count - first - 1);
280 i < CUPS_PAGE_MAX && pclass;
281 i ++, pclass = (ipp_attribute_t *)cupsArrayPrev(classes))
282 cgiSetIPPObjectVars(pclass, NULL, i);
283 }
284
285 /*
286 * Save navigation URLs...
287 */
288
289 urlend = url + sizeof(url);
290
291 if ((var = cgiGetVariable("QUERY")) != NULL)
292 {
293 strlcpy(url, "/classes/?QUERY=", sizeof(url));
294 urlptr = url + strlen(url);
295
296 cgiFormEncode(urlptr, var, urlend - urlptr);
297 urlptr += strlen(urlptr);
298
299 strlcpy(urlptr, "&", urlend - urlptr);
300 urlptr += strlen(urlptr);
301 }
302 else
303 {
304 strlcpy(url, "/classes/?", sizeof(url));
305 urlptr = url + strlen(url);
306 }
307
308 snprintf(urlptr, urlend - urlptr, "FIRST=%d", first);
309 cgiSetVariable("THISURL", url);
310
311 if (first > 0)
312 {
313 snprintf(urlptr, urlend - urlptr, "FIRST=%d&ORDER=%s",
314 first - CUPS_PAGE_MAX, ascending ? "asc" : "dec");
315 cgiSetVariable("PREVURL", url);
316 }
317
318 if ((first + CUPS_PAGE_MAX) < count)
319 {
320 snprintf(urlptr, urlend - urlptr, "FIRST=%d&ORDER=%s",
321 first + CUPS_PAGE_MAX, ascending ? "asc" : "dec");
322 cgiSetVariable("NEXTURL", url);
323 }
324
325 /*
326 * Then show everything...
327 */
328
329 cgiCopyTemplateLang("search.tmpl");
330
331 cgiCopyTemplateLang("classes-header.tmpl");
332
333 if (count > 0)
334 cgiCopyTemplateLang("pager.tmpl");
335
336 cgiCopyTemplateLang("classes.tmpl");
337
338 if (count > 0)
339 cgiCopyTemplateLang("pager.tmpl");
340
341 /*
342 * Delete the response...
343 */
344
345 cupsArrayDelete(classes);
346 ippDelete(response);
347 }
348 else
349 {
350 /*
351 * Show the error...
352 */
353
354 cgiShowIPPError(_("Unable to get class list:"));
355 }
356
357 cgiEndHTML();
358 }
359
360
361 /*
362 * 'show_class()' - Show a single class.
363 */
364
365 void
366 show_class(http_t *http, /* I - Connection to server */
367 const char *pclass) /* I - Name of class */
368 {
369 ipp_t *request, /* IPP request */
370 *response; /* IPP response */
371 ipp_attribute_t *attr; /* IPP attribute */
372 char uri[HTTP_MAX_URI]; /* Printer URI */
373 char refresh[1024]; /* Refresh URL */
374
375
376 /*
377 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
378 * attributes:
379 *
380 * attributes-charset
381 * attributes-natural-language
382 * printer-uri
383 */
384
385 request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
386
387 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
388 "localhost", 0, "/classes/%s", pclass);
389 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
390 uri);
391
392 cgiGetAttributes(request, "classes.tmpl");
393
394 /*
395 * Do the request and get back a response...
396 */
397
398 if ((response = cupsDoRequest(http, request, "/")) != NULL)
399 {
400 /*
401 * Got the result; set the CGI variables and check the status of a
402 * single-queue request...
403 */
404
405 cgiSetIPPVars(response, NULL, NULL, NULL, 0);
406
407 if (pclass && (attr = ippFindAttribute(response, "printer-state",
408 IPP_TAG_ENUM)) != NULL &&
409 attr->values[0].integer == IPP_PRINTER_PROCESSING)
410 {
411 /*
412 * Class is processing - automatically refresh the page until we
413 * are done printing...
414 */
415
416 cgiFormEncode(uri, pclass, sizeof(uri));
417 snprintf(refresh, sizeof(refresh), "10;/classes/%s", uri);
418 cgiSetVariable("refresh_page", refresh);
419 }
420
421 /*
422 * Delete the response...
423 */
424
425 ippDelete(response);
426
427 /*
428 * Show the standard header...
429 */
430
431 cgiStartHTML(pclass);
432
433 /*
434 * Show the class status...
435 */
436
437 cgiCopyTemplateLang("classes.tmpl");
438
439 /*
440 * Show jobs for the specified class...
441 */
442
443 cgiCopyTemplateLang("class-jobs-header.tmpl");
444 cgiShowJobs(http, pclass);
445 }
446 else
447 {
448 /*
449 * Show the IPP error...
450 */
451
452 cgiStartHTML(pclass);
453 cgiShowIPPError(_("Unable to get class status:"));
454 }
455
456 cgiEndHTML();
457 }
458
459
460 /*
461 * End of "$Id: classes.c 5160 2006-02-24 01:14:18Z mike $".
462 */