4 * Class status CGI for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products.
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
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * main() - Main entry for CGI.
27 * show_all_classes() - Show all classes...
28 * show_class() - Show a single class.
32 * Include necessary headers...
35 #include "cgi-private.h"
42 void show_all_classes(http_t
*http
, const char *username
);
43 void show_class(http_t
*http
, const char *printer
);
47 * 'main()' - Main entry for CGI.
50 int /* O - Exit status */
51 main(int argc
, /* I - Number of command-line arguments */
52 char *argv
[]) /* I - Command-line arguments */
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 */
64 "printer-uri-supported"
69 * Get any form variables...
74 op
= cgiGetVariable("OP");
77 * Set the web interface section...
80 cgiSetVariable("SECTION", "classes");
83 * See if we are displaying a printer or all classes...
86 if ((pclass
= getenv("PATH_INFO")) != NULL
)
95 * See who is logged in...
98 user
= getenv("REMOTE_USER");
101 * Connect to the HTTP server...
104 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
107 * Get the default printer...
113 * Get the default destination...
116 request
= ippNewRequest(CUPS_GET_DEFAULT
);
118 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
119 "requested-attributes",
120 sizeof(def_attrs
) / sizeof(def_attrs
[0]), NULL
, def_attrs
);
122 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
124 if ((attr
= ippFindAttribute(response
, "printer-name", IPP_TAG_NAME
)) != NULL
)
125 cgiSetVariable("DEFAULT_NAME", attr
->values
[0].string
.text
);
127 if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
129 char url
[HTTP_MAX_URI
]; /* New URL */
132 cgiSetVariable("DEFAULT_URI",
133 cgiRewriteURL(attr
->values
[0].string
.text
,
134 url
, sizeof(url
), NULL
));
141 * See if we need to show a list of classes or the status of a
146 show_all_classes(http
, user
);
148 show_class(http
, pclass
);
150 else if (!strcasecmp(op
, "print-test-page") && pclass
)
151 cgiPrintTestPage(http
, pclass
);
152 else if (!strcasecmp(op
, "move-jobs") && pclass
)
153 cgiMoveJobs(http
, pclass
, 0);
157 * Unknown/bad operation...
161 cgiStartHTML(pclass
);
163 cgiStartHTML(cgiText(_("Classes")));
165 cgiCopyTemplateLang("error-op.tmpl");
170 * Close the HTTP server connection...
176 * Return with no errors...
184 * 'show_all_classes()' - Show all classes...
188 show_all_classes(http_t
*http
, /* I - Connection to server */
189 const char *user
) /* I - Username */
191 int i
; /* Looping var */
192 ipp_t
*request
, /* IPP request */
193 *response
; /* IPP response */
194 cups_array_t
*classes
; /* Array of class objects */
195 ipp_attribute_t
*pclass
; /* Class object */
196 int ascending
, /* Order of classes (0 = descending) */
197 first
, /* First class to show */
198 count
; /* Number of classes */
199 const char *var
; /* Form variable */
200 void *search
; /* Search data */
201 char url
[1024], /* URL for prev/next/this */
202 *urlptr
, /* Position in URL */
203 *urlend
; /* End of URL */
207 * Show the standard header...
210 cgiStartHTML(cgiText(_("Classes")));
213 * Build a CUPS_GET_CLASSES request, which requires the following
217 * attributes-natural-language
218 * requesting-user-name
221 request
= ippNewRequest(CUPS_GET_CLASSES
);
224 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
225 "requesting-user-name", NULL
, user
);
227 cgiGetAttributes(request
, "classes.tmpl");
230 * Do the request and get back a response...
233 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
236 * Get a list of matching job objects.
239 if ((var
= cgiGetVariable("QUERY")) != NULL
)
240 search
= cgiCompileSearch(var
);
244 classes
= cgiGetIPPObjects(response
, search
);
245 count
= cupsArrayCount(classes
);
248 cgiFreeSearch(search
);
251 * Figure out which classes to display...
254 if ((var
= cgiGetVariable("FIRST")) != NULL
)
260 first
= count
- CUPS_PAGE_MAX
;
262 first
= (first
/ CUPS_PAGE_MAX
) * CUPS_PAGE_MAX
;
267 sprintf(url
, "%d", count
);
268 cgiSetVariable("TOTAL", url
);
270 if ((var
= cgiGetVariable("ORDER")) != NULL
)
271 ascending
= !strcasecmp(var
, "asc");
277 for (i
= 0, pclass
= (ipp_attribute_t
*)cupsArrayIndex(classes
, first
);
278 i
< CUPS_PAGE_MAX
&& pclass
;
279 i
++, pclass
= (ipp_attribute_t
*)cupsArrayNext(classes
))
280 cgiSetIPPObjectVars(pclass
, NULL
, i
);
284 for (i
= 0, pclass
= (ipp_attribute_t
*)cupsArrayIndex(classes
, count
- first
- 1);
285 i
< CUPS_PAGE_MAX
&& pclass
;
286 i
++, pclass
= (ipp_attribute_t
*)cupsArrayPrev(classes
))
287 cgiSetIPPObjectVars(pclass
, NULL
, i
);
291 * Save navigation URLs...
294 urlend
= url
+ sizeof(url
);
296 if ((var
= cgiGetVariable("QUERY")) != NULL
)
298 strlcpy(url
, "/classes/?QUERY=", sizeof(url
));
299 urlptr
= url
+ strlen(url
);
301 cgiFormEncode(urlptr
, var
, urlend
- urlptr
);
302 urlptr
+= strlen(urlptr
);
304 strlcpy(urlptr
, "&", urlend
- urlptr
);
305 urlptr
+= strlen(urlptr
);
309 strlcpy(url
, "/classes/?", sizeof(url
));
310 urlptr
= url
+ strlen(url
);
313 snprintf(urlptr
, urlend
- urlptr
, "FIRST=%d", first
);
314 cgiSetVariable("THISURL", url
);
318 snprintf(urlptr
, urlend
- urlptr
, "FIRST=%d&ORDER=%s",
319 first
- CUPS_PAGE_MAX
, ascending
? "asc" : "dec");
320 cgiSetVariable("PREVURL", url
);
323 if ((first
+ CUPS_PAGE_MAX
) < count
)
325 snprintf(urlptr
, urlend
- urlptr
, "FIRST=%d&ORDER=%s",
326 first
+ CUPS_PAGE_MAX
, ascending
? "asc" : "dec");
327 cgiSetVariable("NEXTURL", url
);
331 * Then show everything...
334 cgiCopyTemplateLang("search.tmpl");
336 cgiCopyTemplateLang("classes-header.tmpl");
339 cgiCopyTemplateLang("pager.tmpl");
341 cgiCopyTemplateLang("classes.tmpl");
344 cgiCopyTemplateLang("pager.tmpl");
347 * Delete the response...
350 cupsArrayDelete(classes
);
359 cgiShowIPPError(_("Unable to get class list:"));
367 * 'show_class()' - Show a single class.
371 show_class(http_t
*http
, /* I - Connection to server */
372 const char *pclass
) /* I - Name of class */
374 ipp_t
*request
, /* IPP request */
375 *response
; /* IPP response */
376 ipp_attribute_t
*attr
; /* IPP attribute */
377 char uri
[HTTP_MAX_URI
]; /* Printer URI */
378 char refresh
[1024]; /* Refresh URL */
382 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
386 * attributes-natural-language
390 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
392 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
393 "localhost", 0, "/classes/%s", pclass
);
394 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
397 cgiGetAttributes(request
, "classes.tmpl");
400 * Do the request and get back a response...
403 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
406 * Got the result; set the CGI variables and check the status of a
407 * single-queue request...
410 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
412 if (pclass
&& (attr
= ippFindAttribute(response
, "printer-state",
413 IPP_TAG_ENUM
)) != NULL
&&
414 attr
->values
[0].integer
== IPP_PRINTER_PROCESSING
)
417 * Class is processing - automatically refresh the page until we
418 * are done printing...
421 cgiFormEncode(uri
, pclass
, sizeof(uri
));
422 snprintf(refresh
, sizeof(refresh
), "10;URL=/classes/%s", uri
);
423 cgiSetVariable("refresh_page", refresh
);
427 * Delete the response...
433 * Show the standard header...
436 cgiStartHTML(pclass
);
439 * Show the class status...
442 cgiCopyTemplateLang("classes.tmpl");
445 * Show jobs for the specified class...
448 cgiCopyTemplateLang("class-jobs-header.tmpl");
449 cgiShowJobs(http
, pclass
);
454 * Show the IPP error...
457 cgiStartHTML(pclass
);
458 cgiShowIPPError(_("Unable to get class status:"));