2 * "$Id: classes.c 5097 2006-02-10 04:06:23Z 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 (!strcmp(argv
[0], "/") || strstr(argv
[0], "classes.cgi"))
92 * See who is logged in...
95 if ((user
= getenv("REMOTE_USER")) == NULL
)
99 * Connect to the HTTP server...
102 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
105 * Get the default printer...
111 * Get the default destination...
114 request
= ippNewRequest(CUPS_GET_DEFAULT
);
116 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
117 "requested-attributes",
118 sizeof(def_attrs
) / sizeof(def_attrs
[0]), NULL
, def_attrs
);
120 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
122 if ((attr
= ippFindAttribute(response
, "printer-name", IPP_TAG_NAME
)) != NULL
)
123 cgiSetVariable("DEFAULT_NAME", attr
->values
[0].string
.text
);
125 if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
127 char url
[HTTP_MAX_URI
]; /* New URL */
130 cgiSetVariable("DEFAULT_URI",
131 cgiRewriteURL(attr
->values
[0].string
.text
,
132 url
, sizeof(url
), NULL
));
139 * See if we need to show a list of classes or the status of a
144 show_all_classes(http
, user
);
146 show_class(http
, pclass
);
148 else if (!strcasecmp(op
, "print-test-page") && pclass
)
149 cgiPrintTestPage(http
, pclass
);
150 else if (!strcasecmp(op
, "move-jobs") && pclass
)
151 cgiMoveJobs(http
, pclass
, 0);
155 * Unknown/bad operation...
159 cgiStartHTML(pclass
);
161 cgiStartHTML(cgiText(_("Classes")));
163 cgiCopyTemplateLang("error-op.tmpl");
168 * Close the HTTP server connection...
174 * Return with no errors...
182 * 'show_all_classes()' - Show all classes...
186 show_all_classes(http_t
*http
, /* I - Connection to server */
187 const char *user
) /* I - Username */
189 int i
; /* Looping var */
190 ipp_t
*request
, /* IPP request */
191 *response
; /* IPP response */
192 cups_array_t
*classes
; /* Array of class objects */
193 ipp_attribute_t
*pclass
; /* Class object */
194 int ascending
, /* Order of classes (0 = descending) */
195 first
, /* First class to show */
196 count
; /* Number of classes */
197 const char *var
; /* Form variable */
198 void *search
; /* Search data */
199 char url
[1024], /* URL for prev/next/this */
200 *urlptr
, /* Position in URL */
201 *urlend
; /* End of URL */
205 * Show the standard header...
208 cgiStartHTML(cgiText(_("Classes")));
211 * Build a CUPS_GET_CLASSES request, which requires the following
215 * attributes-natural-language
216 * requesting-user-name
219 request
= ippNewRequest(CUPS_GET_CLASSES
);
221 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
222 "requesting-user-name", NULL
, user
);
224 cgiGetAttributes(request
, "classes.tmpl");
227 * Do the request and get back a response...
230 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
233 * Get a list of matching job objects.
236 if ((var
= cgiGetVariable("QUERY")) != NULL
)
237 search
= cgiCompileSearch(var
);
241 classes
= cgiGetIPPObjects(response
, search
);
242 count
= cupsArrayCount(classes
);
245 cgiFreeSearch(search
);
248 * Figure out which classes to display...
251 if ((var
= cgiGetVariable("FIRST")) != NULL
)
257 first
= count
- CUPS_PAGE_MAX
;
259 first
= (first
/ CUPS_PAGE_MAX
) * CUPS_PAGE_MAX
;
264 sprintf(url
, "%d", count
);
265 cgiSetVariable("TOTAL", url
);
267 if ((var
= cgiGetVariable("ORDER")) != NULL
)
268 ascending
= !strcasecmp(var
, "asc");
274 for (i
= 0, pclass
= (ipp_attribute_t
*)cupsArrayIndex(classes
, first
);
275 i
< CUPS_PAGE_MAX
&& pclass
;
276 i
++, pclass
= (ipp_attribute_t
*)cupsArrayNext(classes
))
277 cgiSetIPPObjectVars(pclass
, NULL
, i
);
281 for (i
= 0, pclass
= (ipp_attribute_t
*)cupsArrayIndex(classes
, count
- first
- 1);
282 i
< CUPS_PAGE_MAX
&& pclass
;
283 i
++, pclass
= (ipp_attribute_t
*)cupsArrayPrev(classes
))
284 cgiSetIPPObjectVars(pclass
, NULL
, i
);
288 * Save navigation URLs...
291 urlend
= url
+ sizeof(url
);
293 if ((var
= cgiGetVariable("QUERY")) != NULL
)
295 strlcpy(url
, "/classes/?QUERY=", sizeof(url
));
296 urlptr
= url
+ strlen(url
);
298 cgiFormEncode(urlptr
, var
, urlend
- urlptr
);
299 urlptr
+= strlen(urlptr
);
301 strlcpy(urlptr
, "&", urlend
- urlptr
);
302 urlptr
+= strlen(urlptr
);
306 strlcpy(url
, "/classes/?", sizeof(url
));
307 urlptr
= url
+ strlen(url
);
310 snprintf(urlptr
, urlend
- urlptr
, "FIRST=%d", first
);
311 cgiSetVariable("THISURL", url
);
315 snprintf(urlptr
, urlend
- urlptr
, "FIRST=%d&ORDER=%s",
316 first
- CUPS_PAGE_MAX
, ascending
? "asc" : "dec");
317 cgiSetVariable("PREVURL", url
);
320 if ((first
+ CUPS_PAGE_MAX
) < count
)
322 snprintf(urlptr
, urlend
- urlptr
, "FIRST=%d&ORDER=%s",
323 first
+ CUPS_PAGE_MAX
, ascending
? "asc" : "dec");
324 cgiSetVariable("NEXTURL", url
);
328 * Then show everything...
331 cgiCopyTemplateLang("search.tmpl");
333 cgiCopyTemplateLang("classes-header.tmpl");
336 cgiCopyTemplateLang("pager.tmpl");
338 cgiCopyTemplateLang("classes.tmpl");
341 cgiCopyTemplateLang("pager.tmpl");
344 * Delete the response...
347 cupsArrayDelete(classes
);
356 cgiShowIPPError(_("Unable to get class list:"));
364 * 'show_class()' - Show a single class.
368 show_class(http_t
*http
, /* I - Connection to server */
369 const char *pclass
) /* I - Name of class */
371 ipp_t
*request
, /* IPP request */
372 *response
; /* IPP response */
373 ipp_attribute_t
*attr
; /* IPP attribute */
374 char uri
[HTTP_MAX_URI
]; /* Printer URI */
375 char refresh
[1024]; /* Refresh URL */
379 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
383 * attributes-natural-language
387 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
389 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
390 "localhost", 0, "/classes/%s", pclass
);
391 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
394 cgiGetAttributes(request
, "classes.tmpl");
397 * Do the request and get back a response...
400 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
403 * Got the result; set the CGI variables and check the status of a
404 * single-queue request...
407 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
409 if (pclass
&& (attr
= ippFindAttribute(response
, "printer-state",
410 IPP_TAG_ENUM
)) != NULL
&&
411 attr
->values
[0].integer
== IPP_PRINTER_PROCESSING
)
414 * Class is processing - automatically refresh the page until we
415 * are done printing...
418 cgiFormEncode(uri
, pclass
, sizeof(uri
));
419 snprintf(refresh
, sizeof(refresh
), "10;/classes/%s", uri
);
420 cgiSetVariable("refresh_page", refresh
);
424 * Delete the response...
430 * Show the standard header...
433 cgiStartHTML(pclass
);
436 * Show the class status...
439 cgiCopyTemplateLang("classes.tmpl");
442 * Show jobs for the specified class...
445 cgiCopyTemplateLang("class-jobs-header.tmpl");
446 cgiShowJobs(http
, pclass
);
451 * Show the IPP error...
454 cgiStartHTML(pclass
);
455 cgiShowIPPError(_("Unable to get class status:"));
463 * End of "$Id: classes.c 5097 2006-02-10 04:06:23Z mike $".