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