]> git.ipfire.org Git - thirdparty/cups.git/blame - cgi-bin/classes.c
Merge changes from CUPS 1.4svn-r7961.
[thirdparty/cups.git] / cgi-bin / classes.c
CommitLineData
ef416fc2 1/*
b19ccc9e 2 * "$Id: classes.c 7940 2008-09-16 00:45:16Z mike $"
ef416fc2 3 *
4 * Class status CGI for the Common UNIX Printing System (CUPS).
5 *
bc44d920 6 * Copyright 2007 by Apple Inc.
ef416fc2 7 * Copyright 1997-2006 by Easy Software Products.
8 *
9 * These coded instructions, statements, and computer programs are the
bc44d920 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/".
ef416fc2 14 *
15 * Contents:
16 *
fa73b229 17 * main() - Main entry for CGI.
18 * show_all_classes() - Show all classes...
19 * show_class() - Show a single class.
ef416fc2 20 */
21
22/*
23 * Include necessary headers...
24 */
25
26#include "cgi-private.h"
27
28
fa73b229 29/*
30 * Local functions...
31 */
32
33void show_all_classes(http_t *http, const char *username);
34void show_class(http_t *http, const char *printer);
35
36
ef416fc2 37/*
38 * 'main()' - Main entry for CGI.
39 */
40
41int /* O - Exit status */
42main(int argc, /* I - Number of command-line arguments */
43 char *argv[]) /* I - Command-line arguments */
44{
fa73b229 45 const char *pclass; /* Class name */
46 const char *user; /* Username */
ef416fc2 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 */
ef416fc2 51 const char *op; /* Operation to perform, if any */
fa73b229 52 static const char *def_attrs[] = /* Attributes for default printer */
ef416fc2 53 {
54 "printer-name",
55 "printer-uri-supported"
56 };
57
58
59 /*
60 * Get any form variables...
61 */
62
63 cgiInitialize();
fa73b229 64
ef416fc2 65 op = cgiGetVariable("OP");
66
67 /*
68 * Set the web interface section...
69 */
70
71 cgiSetVariable("SECTION", "classes");
72
73 /*
fa73b229 74 * See if we are displaying a printer or all classes...
ef416fc2 75 */
76
b423cd4c 77 if ((pclass = getenv("PATH_INFO")) != NULL)
4744bd90 78 {
b423cd4c 79 pclass ++;
ef416fc2 80
4744bd90 81 if (!*pclass)
82 pclass = NULL;
83 }
84
ef416fc2 85 /*
fa73b229 86 * See who is logged in...
ef416fc2 87 */
88
f301802f 89 user = getenv("REMOTE_USER");
ef416fc2 90
91 /*
fa73b229 92 * Connect to the HTTP server...
ef416fc2 93 */
94
fa73b229 95 http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
ef416fc2 96
97 /*
fa73b229 98 * Get the default printer...
ef416fc2 99 */
100
2e4ff8af 101 if (!op || !cgiIsPOST())
ef416fc2 102 {
ef416fc2 103 /*
104 * Get the default destination...
105 */
106
fa73b229 107 request = ippNewRequest(CUPS_GET_DEFAULT);
ef416fc2 108
109 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
110 "requested-attributes",
111 sizeof(def_attrs) / sizeof(def_attrs[0]), NULL, def_attrs);
112
113 if ((response = cupsDoRequest(http, request, "/")) != NULL)
114 {
115 if ((attr = ippFindAttribute(response, "printer-name", IPP_TAG_NAME)) != NULL)
116 cgiSetVariable("DEFAULT_NAME", attr->values[0].string.text);
117
118 if ((attr = ippFindAttribute(response, "printer-uri-supported", IPP_TAG_URI)) != NULL)
119 {
120 char url[HTTP_MAX_URI]; /* New URL */
121
122
123 cgiSetVariable("DEFAULT_URI",
124 cgiRewriteURL(attr->values[0].string.text,
125 url, sizeof(url), NULL));
126 }
127
128 ippDelete(response);
129 }
130
131 /*
fa73b229 132 * See if we need to show a list of classes or the status of a
133 * single printer...
ef416fc2 134 */
135
fa73b229 136 if (!pclass)
137 show_all_classes(http, user);
138 else
139 show_class(http, pclass);
140 }
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);
145 else
146 {
147 /*
148 * Unknown/bad operation...
149 */
ef416fc2 150
fa73b229 151 if (pclass)
152 cgiStartHTML(pclass);
153 else
154 cgiStartHTML(cgiText(_("Classes")));
ef416fc2 155
fa73b229 156 cgiCopyTemplateLang("error-op.tmpl");
157 cgiEndHTML();
158 }
ef416fc2 159
fa73b229 160 /*
161 * Close the HTTP server connection...
162 */
ef416fc2 163
fa73b229 164 httpClose(http);
ef416fc2 165
fa73b229 166 /*
167 * Return with no errors...
168 */
ef416fc2 169
fa73b229 170 return (0);
171}
ef416fc2 172
ef416fc2 173
fa73b229 174/*
175 * 'show_all_classes()' - Show all classes...
176 */
ef416fc2 177
fa73b229 178void
179show_all_classes(http_t *http, /* I - Connection to server */
f301802f 180 const char *user) /* I - Username */
fa73b229 181{
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 */
2e4ff8af 192 char val[1024]; /* Form variable */
ef416fc2 193
ef416fc2 194
fa73b229 195 /*
196 * Show the standard header...
197 */
198
199 cgiStartHTML(cgiText(_("Classes")));
200
201 /*
202 * Build a CUPS_GET_CLASSES request, which requires the following
203 * attributes:
204 *
205 * attributes-charset
206 * attributes-natural-language
207 * requesting-user-name
208 */
209
210 request = ippNewRequest(CUPS_GET_CLASSES);
211
f301802f 212 if (user)
213 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
214 "requesting-user-name", NULL, user);
fa73b229 215
216 cgiGetAttributes(request, "classes.tmpl");
217
218 /*
219 * Do the request and get back a response...
220 */
221
222 if ((response = cupsDoRequest(http, request, "/")) != NULL)
223 {
ef416fc2 224 /*
fa73b229 225 * Get a list of matching job objects.
ef416fc2 226 */
227
2e4ff8af
MS
228 if ((var = cgiGetVariable("QUERY")) != NULL &&
229 !cgiGetVariable("CLEAR"))
fa73b229 230 search = cgiCompileSearch(var);
231 else
232 search = NULL;
233
234 classes = cgiGetIPPObjects(response, search);
235 count = cupsArrayCount(classes);
236
237 if (search)
238 cgiFreeSearch(search);
ef416fc2 239
240 /*
fa73b229 241 * Figure out which classes to display...
ef416fc2 242 */
243
fa73b229 244 if ((var = cgiGetVariable("FIRST")) != NULL)
245 first = atoi(var);
246 else
247 first = 0;
ef416fc2 248
fa73b229 249 if (first >= count)
250 first = count - CUPS_PAGE_MAX;
ef416fc2 251
fa73b229 252 first = (first / CUPS_PAGE_MAX) * CUPS_PAGE_MAX;
ef416fc2 253
fa73b229 254 if (first < 0)
255 first = 0;
ef416fc2 256
2e4ff8af
MS
257 sprintf(val, "%d", count);
258 cgiSetVariable("TOTAL", val);
ef416fc2 259
fa73b229 260 if ((var = cgiGetVariable("ORDER")) != NULL)
261 ascending = !strcasecmp(var, "asc");
262 else
263 ascending = 1;
ef416fc2 264
fa73b229 265 if (ascending)
266 {
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);
271 }
272 else
273 {
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);
278 }
ef416fc2 279
fa73b229 280 /*
281 * Save navigation URLs...
282 */
ef416fc2 283
2e4ff8af 284 cgiSetVariable("THISURL", "/classes/");
fa73b229 285
286 if (first > 0)
287 {
2e4ff8af
MS
288 sprintf(val, "%d", first - CUPS_PAGE_MAX);
289 cgiSetVariable("PREV", val);
ef416fc2 290 }
fa73b229 291
292 if ((first + CUPS_PAGE_MAX) < count)
293 {
2e4ff8af
MS
294 sprintf(val, "%d", first + CUPS_PAGE_MAX);
295 cgiSetVariable("NEXT", val);
fa73b229 296 }
297
ef416fc2 298 /*
fa73b229 299 * Then show everything...
ef416fc2 300 */
301
fa73b229 302 cgiCopyTemplateLang("search.tmpl");
ef416fc2 303
fa73b229 304 cgiCopyTemplateLang("classes-header.tmpl");
ef416fc2 305
b19ccc9e 306 if (count > CUPS_PAGE_MAX)
fa73b229 307 cgiCopyTemplateLang("pager.tmpl");
ef416fc2 308
fa73b229 309 cgiCopyTemplateLang("classes.tmpl");
ef416fc2 310
b19ccc9e 311 if (count > CUPS_PAGE_MAX)
fa73b229 312 cgiCopyTemplateLang("pager.tmpl");
ef416fc2 313
314 /*
fa73b229 315 * Delete the response...
ef416fc2 316 */
317
bd7854cb 318 cupsArrayDelete(classes);
fa73b229 319 ippDelete(response);
320 }
321 else
322 {
323 /*
324 * Show the error...
325 */
ef416fc2 326
fa73b229 327 cgiShowIPPError(_("Unable to get class list:"));
328 }
ef416fc2 329
fa73b229 330 cgiEndHTML();
331}
ef416fc2 332
ef416fc2 333
fa73b229 334/*
335 * 'show_class()' - Show a single class.
336 */
ef416fc2 337
fa73b229 338void
339show_class(http_t *http, /* I - Connection to server */
340 const char *pclass) /* I - Name of class */
341{
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 */
347
348
349 /*
350 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
351 * attributes:
352 *
353 * attributes-charset
354 * attributes-natural-language
355 * printer-uri
356 */
ef416fc2 357
fa73b229 358 request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
ef416fc2 359
a4d04587 360 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
361 "localhost", 0, "/classes/%s", pclass);
fa73b229 362 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
363 uri);
ef416fc2 364
b19ccc9e 365 cgiGetAttributes(request, "class.tmpl");
fa73b229 366
367 /*
368 * Do the request and get back a response...
369 */
370
371 if ((response = cupsDoRequest(http, request, "/")) != NULL)
372 {
ef416fc2 373 /*
fa73b229 374 * Got the result; set the CGI variables and check the status of a
375 * single-queue request...
ef416fc2 376 */
377
fa73b229 378 cgiSetIPPVars(response, NULL, NULL, NULL, 0);
379
380 if (pclass && (attr = ippFindAttribute(response, "printer-state",
381 IPP_TAG_ENUM)) != NULL &&
382 attr->values[0].integer == IPP_PRINTER_PROCESSING)
ef416fc2 383 {
fa73b229 384 /*
385 * Class is processing - automatically refresh the page until we
386 * are done printing...
387 */
ef416fc2 388
fa73b229 389 cgiFormEncode(uri, pclass, sizeof(uri));
f301802f 390 snprintf(refresh, sizeof(refresh), "10;URL=/classes/%s", uri);
fa73b229 391 cgiSetVariable("refresh_page", refresh);
ef416fc2 392 }
ef416fc2 393
fa73b229 394 /*
395 * Delete the response...
396 */
397
398 ippDelete(response);
ef416fc2 399
400 /*
401 * Show the standard header...
402 */
403
fa73b229 404 cgiStartHTML(pclass);
ef416fc2 405
406 /*
fa73b229 407 * Show the class status...
ef416fc2 408 */
409
ac884b6a 410 cgiSetVariable("_SINGLE_DEST", "1");
b19ccc9e 411 cgiCopyTemplateLang("class.tmpl");
ef416fc2 412
fa73b229 413 /*
414 * Show jobs for the specified class...
415 */
ef416fc2 416
fa73b229 417 cgiCopyTemplateLang("class-jobs-header.tmpl");
418 cgiShowJobs(http, pclass);
419 }
420 else
421 {
422 /*
423 * Show the IPP error...
424 */
ef416fc2 425
fa73b229 426 cgiStartHTML(pclass);
427 cgiShowIPPError(_("Unable to get class status:"));
428 }
ef416fc2 429
fa73b229 430 cgiEndHTML();
ef416fc2 431}
432
433
434/*
b19ccc9e 435 * End of "$Id: classes.c 7940 2008-09-16 00:45:16Z mike $".
ef416fc2 436 */