2 * "$Id: classes.c 6889 2007-08-29 22:23:35Z mike $"
4 * Class status CGI for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
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/".
17 * main() - Main entry for CGI.
18 * show_all_classes() - Show all classes...
19 * show_class() - Show a single class.
23 * Include necessary headers...
26 #include "cgi-private.h"
33 void show_all_classes(http_t
*http
, const char *username
);
34 void show_class(http_t
*http
, const char *printer
);
38 * 'main()' - Main entry for CGI.
41 int /* O - Exit status */
42 main(int argc
, /* I - Number of command-line arguments */
43 char *argv
[]) /* I - Command-line arguments */
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 */
55 "printer-uri-supported"
60 * Get any form variables...
65 op
= cgiGetVariable("OP");
68 * Set the web interface section...
71 cgiSetVariable("SECTION", "classes");
74 * See if we are displaying a printer or all classes...
77 if ((pclass
= getenv("PATH_INFO")) != NULL
)
86 * See who is logged in...
89 user
= getenv("REMOTE_USER");
92 * Connect to the HTTP server...
95 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
98 * Get the default printer...
101 if (!op
|| !cgiIsPOST())
104 * Get the default destination...
107 request
= ippNewRequest(CUPS_GET_DEFAULT
);
109 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
110 "requested-attributes",
111 sizeof(def_attrs
) / sizeof(def_attrs
[0]), NULL
, def_attrs
);
113 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
115 if ((attr
= ippFindAttribute(response
, "printer-name", IPP_TAG_NAME
)) != NULL
)
116 cgiSetVariable("DEFAULT_NAME", attr
->values
[0].string
.text
);
118 if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
120 char url
[HTTP_MAX_URI
]; /* New URL */
123 cgiSetVariable("DEFAULT_URI",
124 cgiRewriteURL(attr
->values
[0].string
.text
,
125 url
, sizeof(url
), NULL
));
132 * See if we need to show a list of classes or the status of a
137 show_all_classes(http
, user
);
139 show_class(http
, pclass
);
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);
148 * Unknown/bad operation...
152 cgiStartHTML(pclass
);
154 cgiStartHTML(cgiText(_("Classes")));
156 cgiCopyTemplateLang("error-op.tmpl");
161 * Close the HTTP server connection...
167 * Return with no errors...
175 * 'show_all_classes()' - Show all classes...
179 show_all_classes(http_t
*http
, /* I - Connection to server */
180 const char *user
) /* I - Username */
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 */
196 * Show the standard header...
199 cgiStartHTML(cgiText(_("Classes")));
202 * Build a CUPS_GET_CLASSES request, which requires the following
206 * attributes-natural-language
207 * requesting-user-name
210 request
= ippNewRequest(CUPS_GET_CLASSES
);
213 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
214 "requesting-user-name", NULL
, user
);
216 cgiGetAttributes(request
, "classes.tmpl");
219 * Do the request and get back a response...
222 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
225 * Get a list of matching job objects.
228 if ((var
= cgiGetVariable("QUERY")) != NULL
&&
229 !cgiGetVariable("CLEAR"))
230 search
= cgiCompileSearch(var
);
234 classes
= cgiGetIPPObjects(response
, search
);
235 count
= cupsArrayCount(classes
);
238 cgiFreeSearch(search
);
241 * Figure out which classes to display...
244 if ((var
= cgiGetVariable("FIRST")) != NULL
)
250 first
= count
- CUPS_PAGE_MAX
;
252 first
= (first
/ CUPS_PAGE_MAX
) * CUPS_PAGE_MAX
;
257 sprintf(val
, "%d", count
);
258 cgiSetVariable("TOTAL", val
);
260 if ((var
= cgiGetVariable("ORDER")) != NULL
)
261 ascending
= !strcasecmp(var
, "asc");
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
);
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
);
281 * Save navigation URLs...
284 cgiSetVariable("THISURL", "/classes/");
288 sprintf(val
, "%d", first
- CUPS_PAGE_MAX
);
289 cgiSetVariable("PREV", val
);
292 if ((first
+ CUPS_PAGE_MAX
) < count
)
294 sprintf(val
, "%d", first
+ CUPS_PAGE_MAX
);
295 cgiSetVariable("NEXT", val
);
299 * Then show everything...
302 cgiCopyTemplateLang("search.tmpl");
304 cgiCopyTemplateLang("classes-header.tmpl");
307 cgiCopyTemplateLang("pager.tmpl");
309 cgiCopyTemplateLang("classes.tmpl");
312 cgiCopyTemplateLang("pager.tmpl");
315 * Delete the response...
318 cupsArrayDelete(classes
);
327 cgiShowIPPError(_("Unable to get class list:"));
335 * 'show_class()' - Show a single class.
339 show_class(http_t
*http
, /* I - Connection to server */
340 const char *pclass
) /* I - Name of class */
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 */
350 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
354 * attributes-natural-language
358 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
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
,
365 cgiGetAttributes(request
, "classes.tmpl");
368 * Do the request and get back a response...
371 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
374 * Got the result; set the CGI variables and check the status of a
375 * single-queue request...
378 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
380 if (pclass
&& (attr
= ippFindAttribute(response
, "printer-state",
381 IPP_TAG_ENUM
)) != NULL
&&
382 attr
->values
[0].integer
== IPP_PRINTER_PROCESSING
)
385 * Class is processing - automatically refresh the page until we
386 * are done printing...
389 cgiFormEncode(uri
, pclass
, sizeof(uri
));
390 snprintf(refresh
, sizeof(refresh
), "10;URL=/classes/%s", uri
);
391 cgiSetVariable("refresh_page", refresh
);
395 * Delete the response...
401 * Show the standard header...
404 cgiStartHTML(pclass
);
407 * Show the class status...
410 cgiSetVariable("_SINGLE_DEST", "1");
411 cgiCopyTemplateLang("classes.tmpl");
414 * Show jobs for the specified class...
417 cgiCopyTemplateLang("class-jobs-header.tmpl");
418 cgiShowJobs(http
, pclass
);
423 * Show the IPP error...
426 cgiStartHTML(pclass
);
427 cgiShowIPPError(_("Unable to get class status:"));
435 * End of "$Id: classes.c 6889 2007-08-29 22:23:35Z mike $".