2 * "$Id: classes.c 6649 2007-07-11 21:46:42Z 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...
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 url
[1024], /* URL for prev/next/this */
193 *urlptr
, /* Position in URL */
194 *urlend
; /* End of URL */
198 * Show the standard header...
201 cgiStartHTML(cgiText(_("Classes")));
204 * Build a CUPS_GET_CLASSES request, which requires the following
208 * attributes-natural-language
209 * requesting-user-name
212 request
= ippNewRequest(CUPS_GET_CLASSES
);
215 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
216 "requesting-user-name", NULL
, user
);
218 cgiGetAttributes(request
, "classes.tmpl");
221 * Do the request and get back a response...
224 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
227 * Get a list of matching job objects.
230 if ((var
= cgiGetVariable("QUERY")) != NULL
)
231 search
= cgiCompileSearch(var
);
235 classes
= cgiGetIPPObjects(response
, search
);
236 count
= cupsArrayCount(classes
);
239 cgiFreeSearch(search
);
242 * Figure out which classes to display...
245 if ((var
= cgiGetVariable("FIRST")) != NULL
)
251 first
= count
- CUPS_PAGE_MAX
;
253 first
= (first
/ CUPS_PAGE_MAX
) * CUPS_PAGE_MAX
;
258 sprintf(url
, "%d", count
);
259 cgiSetVariable("TOTAL", url
);
261 if ((var
= cgiGetVariable("ORDER")) != NULL
)
262 ascending
= !strcasecmp(var
, "asc");
268 for (i
= 0, pclass
= (ipp_attribute_t
*)cupsArrayIndex(classes
, first
);
269 i
< CUPS_PAGE_MAX
&& pclass
;
270 i
++, pclass
= (ipp_attribute_t
*)cupsArrayNext(classes
))
271 cgiSetIPPObjectVars(pclass
, NULL
, i
);
275 for (i
= 0, pclass
= (ipp_attribute_t
*)cupsArrayIndex(classes
, count
- first
- 1);
276 i
< CUPS_PAGE_MAX
&& pclass
;
277 i
++, pclass
= (ipp_attribute_t
*)cupsArrayPrev(classes
))
278 cgiSetIPPObjectVars(pclass
, NULL
, i
);
282 * Save navigation URLs...
285 urlend
= url
+ sizeof(url
);
287 if ((var
= cgiGetVariable("QUERY")) != NULL
)
289 strlcpy(url
, "/classes/?QUERY=", sizeof(url
));
290 urlptr
= url
+ strlen(url
);
292 cgiFormEncode(urlptr
, var
, urlend
- urlptr
);
293 urlptr
+= strlen(urlptr
);
295 strlcpy(urlptr
, "&", urlend
- urlptr
);
296 urlptr
+= strlen(urlptr
);
300 strlcpy(url
, "/classes/?", sizeof(url
));
301 urlptr
= url
+ strlen(url
);
304 snprintf(urlptr
, urlend
- urlptr
, "FIRST=%d", first
);
305 cgiSetVariable("THISURL", url
);
309 snprintf(urlptr
, urlend
- urlptr
, "FIRST=%d&ORDER=%s",
310 first
- CUPS_PAGE_MAX
, ascending
? "asc" : "dec");
311 cgiSetVariable("PREVURL", url
);
314 if ((first
+ CUPS_PAGE_MAX
) < count
)
316 snprintf(urlptr
, urlend
- urlptr
, "FIRST=%d&ORDER=%s",
317 first
+ CUPS_PAGE_MAX
, ascending
? "asc" : "dec");
318 cgiSetVariable("NEXTURL", url
);
322 * Then show everything...
325 cgiCopyTemplateLang("search.tmpl");
327 cgiCopyTemplateLang("classes-header.tmpl");
330 cgiCopyTemplateLang("pager.tmpl");
332 cgiCopyTemplateLang("classes.tmpl");
335 cgiCopyTemplateLang("pager.tmpl");
338 * Delete the response...
341 cupsArrayDelete(classes
);
350 cgiShowIPPError(_("Unable to get class list:"));
358 * 'show_class()' - Show a single class.
362 show_class(http_t
*http
, /* I - Connection to server */
363 const char *pclass
) /* I - Name of class */
365 ipp_t
*request
, /* IPP request */
366 *response
; /* IPP response */
367 ipp_attribute_t
*attr
; /* IPP attribute */
368 char uri
[HTTP_MAX_URI
]; /* Printer URI */
369 char refresh
[1024]; /* Refresh URL */
373 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
377 * attributes-natural-language
381 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
383 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
384 "localhost", 0, "/classes/%s", pclass
);
385 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
388 cgiGetAttributes(request
, "classes.tmpl");
391 * Do the request and get back a response...
394 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
397 * Got the result; set the CGI variables and check the status of a
398 * single-queue request...
401 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
403 if (pclass
&& (attr
= ippFindAttribute(response
, "printer-state",
404 IPP_TAG_ENUM
)) != NULL
&&
405 attr
->values
[0].integer
== IPP_PRINTER_PROCESSING
)
408 * Class is processing - automatically refresh the page until we
409 * are done printing...
412 cgiFormEncode(uri
, pclass
, sizeof(uri
));
413 snprintf(refresh
, sizeof(refresh
), "10;URL=/classes/%s", uri
);
414 cgiSetVariable("refresh_page", refresh
);
418 * Delete the response...
424 * Show the standard header...
427 cgiStartHTML(pclass
);
430 * Show the class status...
433 cgiCopyTemplateLang("classes.tmpl");
436 * Show jobs for the specified class...
439 cgiCopyTemplateLang("class-jobs-header.tmpl");
440 cgiShowJobs(http
, pclass
);
445 * Show the IPP error...
448 cgiStartHTML(pclass
);
449 cgiShowIPPError(_("Unable to get class status:"));
457 * End of "$Id: classes.c 6649 2007-07-11 21:46:42Z mike $".