]> git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/jobs.c
Load cups into easysw/current.
[thirdparty/cups.git] / cgi-bin / jobs.c
1 /*
2 * "$Id: jobs.c 4921 2006-01-12 21:26:26Z mike $"
3 *
4 * Job status CGI for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2006 by Easy Software Products.
7 *
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
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
26 * main() - Main entry for CGI.
27 * do_job_op() - Do a job operation.
28 */
29
30 /*
31 * Include necessary headers...
32 */
33
34 #include "cgi-private.h"
35
36
37 /*
38 * Local functions...
39 */
40
41 static void do_job_op(http_t *http, cups_lang_t *language, ipp_op_t op);
42
43
44 /*
45 * 'main()' - Main entry for CGI.
46 */
47
48 int /* O - Exit status */
49 main(int argc, /* I - Number of command-line arguments */
50 char *argv[]) /* I - Command-line arguments */
51 {
52 cups_lang_t *language; /* Language information */
53 http_t *http; /* Connection to the server */
54 const char *op; /* Operation name */
55
56
57 /*
58 * Get any form variables...
59 */
60
61 cgiInitialize();
62
63 /*
64 * Set the web interface section...
65 */
66
67 cgiSetVariable("SECTION", "jobs");
68
69 /*
70 * Get the request language...
71 */
72
73 language = cupsLangDefault();
74
75 /*
76 * Connect to the HTTP server...
77 */
78
79 http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
80
81 /*
82 * Tell the client to expect UTF-8 encoded HTML...
83 */
84
85 puts("Content-Type: text/html;charset=utf-8\n");
86
87 /*
88 * Send a standard header...
89 */
90
91 cgiSetVariable("TITLE", _cupsLangString(language, _("Jobs")));
92
93 cgiSetServerVersion();
94
95 cgiCopyTemplateLang("header.tmpl");
96
97 if ((op = cgiGetVariable("OP")) != NULL)
98 {
99 /*
100 * Do the operation...
101 */
102
103 if (!strcmp(op, "cancel-job"))
104 do_job_op(http, language, IPP_CANCEL_JOB);
105 else if (!strcmp(op, "hold-job"))
106 do_job_op(http, language, IPP_HOLD_JOB);
107 else if (!strcmp(op, "release-job"))
108 do_job_op(http, language, IPP_RELEASE_JOB);
109 else if (!strcmp(op, "restart-job"))
110 do_job_op(http, language, IPP_RESTART_JOB);
111 else
112 {
113 /*
114 * Bad operation code... Display an error...
115 */
116
117 cgiCopyTemplateLang("job-op.tmpl");
118 }
119 }
120 else
121 {
122 /*
123 * Show a list of jobs...
124 */
125
126 cgiShowJobs(http, NULL);
127 }
128
129 cgiCopyTemplateLang("trailer.tmpl");
130
131 /*
132 * Close the HTTP server connection...
133 */
134
135 httpClose(http);
136 cupsLangFree(language);
137
138 /*
139 * Return with no errors...
140 */
141
142 return (0);
143 }
144
145
146 /*
147 * 'do_job_op()' - Do a job operation.
148 */
149
150 static void
151 do_job_op(http_t *http, /* I - HTTP connection */
152 cups_lang_t *language, /* I - Client's language */
153 ipp_op_t op) /* I - Operation to perform */
154 {
155 ipp_t *request, /* IPP request */
156 *response; /* IPP response */
157 char uri[HTTP_MAX_URI]; /* Job URI */
158 const char *job; /* Job ID */
159 ipp_status_t status; /* Operation status... */
160
161
162 if ((job = cgiGetVariable("JOB_ID")) != NULL)
163 snprintf(uri, sizeof(uri), "ipp://localhost/jobs/%s", job);
164 else
165 {
166 cgiSetVariable("ERROR", ippErrorString(IPP_NOT_FOUND));
167 cgiCopyTemplateLang("error.tmpl");
168 return;
169 }
170
171 /*
172 * Build a job request, which requires the following
173 * attributes:
174 *
175 * attributes-charset
176 * attributes-natural-language
177 * job-uri or printer-uri (purge-jobs)
178 * requesting-user-name
179 */
180
181 request = ippNew();
182
183 request->request.op.operation_id = op;
184 request->request.op.request_id = 1;
185
186 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
187 "attributes-charset", NULL, cupsLangEncoding(language));
188
189 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
190 "attributes-natural-language", NULL, language->language);
191
192 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri",
193 NULL, uri);
194
195 if (getenv("REMOTE_USER") != NULL)
196 {
197 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
198 NULL, getenv("REMOTE_USER"));
199
200 if (strcmp(getenv("REMOTE_USER"), "root"))
201 ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1);
202 }
203 else
204 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
205 NULL, "unknown");
206
207 /*
208 * Do the request and get back a response...
209 */
210
211 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL)
212 {
213 status = response->request.status.status_code;
214
215 ippDelete(response);
216 }
217 else
218 status = cupsLastError();
219
220 if (status > IPP_OK_CONFLICT)
221 {
222 cgiSetVariable("ERROR", ippErrorString(status));
223 cgiCopyTemplateLang("error.tmpl");
224 }
225 else if (op == IPP_CANCEL_JOB)
226 cgiCopyTemplateLang("job-cancel.tmpl");
227 else if (op == IPP_HOLD_JOB)
228 cgiCopyTemplateLang("job-hold.tmpl");
229 else if (op == IPP_RELEASE_JOB)
230 cgiCopyTemplateLang("job-release.tmpl");
231 else if (op == IPP_RESTART_JOB)
232 cgiCopyTemplateLang("job-restart.tmpl");
233 }
234
235
236 /*
237 * End of "$Id: jobs.c 4921 2006-01-12 21:26:26Z mike $".
238 */