2 * "$Id: classes.c 5160 2006-02-24 01:14:18Z mike $"
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
)
90 * See who is logged in...
93 if ((user
= getenv("REMOTE_USER")) == NULL
)
97 * Connect to the HTTP server...
100 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
103 * Get the default printer...
109 * Get the default destination...
112 request
= ippNewRequest(CUPS_GET_DEFAULT
);
114 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
115 "requested-attributes",
116 sizeof(def_attrs
) / sizeof(def_attrs
[0]), NULL
, def_attrs
);
118 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
120 if ((attr
= ippFindAttribute(response
, "printer-name", IPP_TAG_NAME
)) != NULL
)
121 cgiSetVariable("DEFAULT_NAME", attr
->values
[0].string
.text
);
123 if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
125 char url
[HTTP_MAX_URI
]; /* New URL */
128 cgiSetVariable("DEFAULT_URI",
129 cgiRewriteURL(attr
->values
[0].string
.text
,
130 url
, sizeof(url
), NULL
));
137 * See if we need to show a list of classes or the status of a
142 show_all_classes(http
, user
);
144 show_class(http
, pclass
);
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);
153 * Unknown/bad operation...
157 cgiStartHTML(pclass
);
159 cgiStartHTML(cgiText(_("Classes")));
161 cgiCopyTemplateLang("error-op.tmpl");
166 * Close the HTTP server connection...
172 * Return with no errors...
180 * 'show_all_classes()' - Show all classes...
184 show_all_classes(http_t
*http
, /* I - Connection to server */
185 const char *user
) /* I - Username */
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 */
203 * Show the standard header...
206 cgiStartHTML(cgiText(_("Classes")));
209 * Build a CUPS_GET_CLASSES request, which requires the following
213 * attributes-natural-language
214 * requesting-user-name
217 request
= ippNewRequest(CUPS_GET_CLASSES
);
219 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
220 "requesting-user-name", NULL
, user
);
222 cgiGetAttributes(request
, "classes.tmpl");
225 * Do the request and get back a response...
228 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
231 * Get a list of matching job objects.
234 if ((var
= cgiGetVariable("QUERY")) != NULL
)
235 search
= cgiCompileSearch(var
);
239 classes
= cgiGetIPPObjects(response
, search
);
240 count
= cupsArrayCount(classes
);
243 cgiFreeSearch(search
);
246 * Figure out which classes to display...
249 if ((var
= cgiGetVariable("FIRST")) != NULL
)
255 first
= count
- CUPS_PAGE_MAX
;
257 first
= (first
/ CUPS_PAGE_MAX
) * CUPS_PAGE_MAX
;
262 sprintf(url
, "%d", count
);
263 cgiSetVariable("TOTAL", url
);
265 if ((var
= cgiGetVariable("ORDER")) != NULL
)
266 ascending
= !strcasecmp(var
, "asc");
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
);
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
);
286 * Save navigation URLs...
289 urlend
= url
+ sizeof(url
);
291 if ((var
= cgiGetVariable("QUERY")) != NULL
)
293 strlcpy(url
, "/classes/?QUERY=", sizeof(url
));
294 urlptr
= url
+ strlen(url
);
296 cgiFormEncode(urlptr
, var
, urlend
- urlptr
);
297 urlptr
+= strlen(urlptr
);
299 strlcpy(urlptr
, "&", urlend
- urlptr
);
300 urlptr
+= strlen(urlptr
);
304 strlcpy(url
, "/classes/?", sizeof(url
));
305 urlptr
= url
+ strlen(url
);
308 snprintf(urlptr
, urlend
- urlptr
, "FIRST=%d", first
);
309 cgiSetVariable("THISURL", url
);
313 snprintf(urlptr
, urlend
- urlptr
, "FIRST=%d&ORDER=%s",
314 first
- CUPS_PAGE_MAX
, ascending
? "asc" : "dec");
315 cgiSetVariable("PREVURL", url
);
318 if ((first
+ CUPS_PAGE_MAX
) < count
)
320 snprintf(urlptr
, urlend
- urlptr
, "FIRST=%d&ORDER=%s",
321 first
+ CUPS_PAGE_MAX
, ascending
? "asc" : "dec");
322 cgiSetVariable("NEXTURL", url
);
326 * Then show everything...
329 cgiCopyTemplateLang("search.tmpl");
331 cgiCopyTemplateLang("classes-header.tmpl");
334 cgiCopyTemplateLang("pager.tmpl");
336 cgiCopyTemplateLang("classes.tmpl");
339 cgiCopyTemplateLang("pager.tmpl");
342 * Delete the response...
345 cupsArrayDelete(classes
);
354 cgiShowIPPError(_("Unable to get class list:"));
362 * 'show_class()' - Show a single class.
366 show_class(http_t
*http
, /* I - Connection to server */
367 const char *pclass
) /* I - Name of class */
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 */
377 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
381 * attributes-natural-language
385 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
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
,
392 cgiGetAttributes(request
, "classes.tmpl");
395 * Do the request and get back a response...
398 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
401 * Got the result; set the CGI variables and check the status of a
402 * single-queue request...
405 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
407 if (pclass
&& (attr
= ippFindAttribute(response
, "printer-state",
408 IPP_TAG_ENUM
)) != NULL
&&
409 attr
->values
[0].integer
== IPP_PRINTER_PROCESSING
)
412 * Class is processing - automatically refresh the page until we
413 * are done printing...
416 cgiFormEncode(uri
, pclass
, sizeof(uri
));
417 snprintf(refresh
, sizeof(refresh
), "10;/classes/%s", uri
);
418 cgiSetVariable("refresh_page", refresh
);
422 * Delete the response...
428 * Show the standard header...
431 cgiStartHTML(pclass
);
434 * Show the class status...
437 cgiCopyTemplateLang("classes.tmpl");
440 * Show jobs for the specified class...
443 cgiCopyTemplateLang("class-jobs-header.tmpl");
444 cgiShowJobs(http
, pclass
);
449 * Show the IPP error...
452 cgiStartHTML(pclass
);
453 cgiShowIPPError(_("Unable to get class status:"));
461 * End of "$Id: classes.c 5160 2006-02-24 01:14:18Z mike $".