]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/dest-job.c
Fix source file header text duplication text duplication.
[thirdparty/cups.git] / cups / dest-job.c
CommitLineData
dcb445bc 1/*
7e86f2f6 2 * Destination job support for CUPS.
dcb445bc 3 *
46385a1a 4 * Copyright 2012-2016 by Apple Inc.
dcb445bc 5 *
7e86f2f6
MS
6 * These coded instructions, statements, and computer programs are the
7 * property of Apple Inc. and are protected by Federal copyright
8 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
9 * which should have been included with this file. If this file is
57b7b66b 10 * missing or damaged, see the license at "http://www.cups.org/".
dcb445bc 11 *
7e86f2f6 12 * This file is subject to the Apple OS-Developed Software exception.
dcb445bc
MS
13 */
14
15/*
16 * Include necessary headers...
17 */
18
19#include "cups-private.h"
20
21
22/*
23 * 'cupsCancelDestJob()' - Cancel a job on a destination.
24 *
25 * The "job_id" is the number returned by cupsCreateDestJob.
26 *
46385a1a
MS
27 * Returns @code IPP_STATUS_OK@ on success and
28 * @code IPP_STATUS_ERRPR_NOT_AUTHORIZED@ or
29 * @code IPP_STATUS_ERROR_FORBIDDEN@ on failure.
dcb445bc 30 *
8072030b 31 * @since CUPS 1.6/macOS 10.8@
dcb445bc
MS
32 */
33
34ipp_status_t
35cupsCancelDestJob(http_t *http, /* I - Connection to destination */
36 cups_dest_t *dest, /* I - Destination */
37 int job_id) /* I - Job ID */
38{
46385a1a
MS
39 cups_dinfo_t *info; /* Destination information */
40
41
42 if ((info = cupsCopyDestInfo(http, dest)) != NULL)
43 {
44 ipp_t *request; /* Cancel-Job request */
45
46 request = ippNewRequest(IPP_OP_CANCEL_JOB);
47
48 ippSetVersion(request, info->version / 10, info->version % 10);
49
50 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, info->uri);
51 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", job_id);
52 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", NULL, cupsUser());
7e86f2f6 53
46385a1a
MS
54 ippDelete(cupsDoRequest(http, request, info->resource));
55 cupsFreeDestInfo(info);
56 }
57
58 return (cupsLastError());
dcb445bc
MS
59}
60
61
62/*
63 * 'cupsCloseDestJob()' - Close a job and start printing.
64 *
65 * Use when the last call to cupsStartDocument passed 0 for "last_document".
cb7f98ee 66 * "job_id" is the job ID returned by cupsCreateDestJob. Returns @code IPP_STATUS_OK@
82cc1f9a 67 * on success.
dcb445bc 68 *
8072030b 69 * @since CUPS 1.6/macOS 10.8@
dcb445bc
MS
70 */
71
82cc1f9a 72ipp_status_t /* O - IPP status code */
dcb445bc 73cupsCloseDestJob(
82cc1f9a
MS
74 http_t *http, /* I - Connection to destination */
75 cups_dest_t *dest, /* I - Destination */
76 cups_dinfo_t *info, /* I - Destination information */
77 int job_id) /* I - Job ID */
dcb445bc 78{
82cc1f9a
MS
79 int i; /* Looping var */
80 ipp_t *request = NULL;/* Close-Job/Send-Document request */
81 ipp_attribute_t *attr; /* operations-supported attribute */
82
83
807315e6 84 DEBUG_printf(("cupsCloseDestJob(http=%p, dest=%p(%s/%s), info=%p, job_id=%d)", (void *)http, (void *)dest, dest ? dest->name : NULL, dest ? dest->instance : NULL, (void *)info, job_id));
82cc1f9a
MS
85
86 /*
87 * Range check input...
88 */
89
90 if (!http || !dest || !info || job_id <= 0)
91 {
cb7f98ee 92 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
82cc1f9a 93 DEBUG_puts("1cupsCloseDestJob: Bad arguments.");
cb7f98ee 94 return (IPP_STATUS_ERROR_INTERNAL);
82cc1f9a
MS
95 }
96
97 /*
98 * Build a Close-Job or empty Send-Document request...
99 */
100
101 if ((attr = ippFindAttribute(info->attrs, "operations-supported",
102 IPP_TAG_ENUM)) != NULL)
103 {
104 for (i = 0; i < attr->num_values; i ++)
cb7f98ee 105 if (attr->values[i].integer == IPP_OP_CLOSE_JOB)
82cc1f9a 106 {
cb7f98ee 107 request = ippNewRequest(IPP_OP_CLOSE_JOB);
82cc1f9a
MS
108 break;
109 }
110 }
111
112 if (!request)
cb7f98ee 113 request = ippNewRequest(IPP_OP_SEND_DOCUMENT);
82cc1f9a
MS
114
115 if (!request)
116 {
cb7f98ee 117 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(ENOMEM), 0);
82cc1f9a
MS
118 DEBUG_puts("1cupsCloseDestJob: Unable to create Close-Job/Send-Document "
119 "request.");
cb7f98ee 120 return (IPP_STATUS_ERROR_INTERNAL);
82cc1f9a
MS
121 }
122
6961465f
MS
123 ippSetVersion(request, info->version / 10, info->version % 10);
124
82cc1f9a
MS
125 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
126 NULL, info->uri);
127 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id",
128 job_id);
129 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
130 NULL, cupsUser());
cb7f98ee 131 if (ippGetOperation(request) == IPP_OP_SEND_DOCUMENT)
82cc1f9a
MS
132 ippAddBoolean(request, IPP_TAG_OPERATION, "last-document", 1);
133
134 /*
135 * Send the request and return the status...
136 */
137
138 ippDelete(cupsDoRequest(http, request, info->resource));
139
140 DEBUG_printf(("1cupsCloseDestJob: %s (%s)", ippErrorString(cupsLastError()),
141 cupsLastErrorString()));
142
143 return (cupsLastError());
dcb445bc
MS
144}
145
146
147/*
148 * 'cupsCreateDestJob()' - Create a job on a destination.
149 *
cb7f98ee 150 * Returns @code IPP_STATUS_OK@ or @code IPP_STATUS_OK_SUBST@ on success, saving the job ID
82cc1f9a 151 * in the variable pointed to by "job_id".
dcb445bc 152 *
8072030b 153 * @since CUPS 1.6/macOS 10.8@
dcb445bc
MS
154 */
155
156ipp_status_t /* O - IPP status code */
157cupsCreateDestJob(
158 http_t *http, /* I - Connection to destination */
159 cups_dest_t *dest, /* I - Destination */
160 cups_dinfo_t *info, /* I - Destination information */
161 int *job_id, /* O - Job ID or 0 on error */
162 const char *title, /* I - Job name */
163 int num_options, /* I - Number of job options */
164 cups_option_t *options) /* I - Job options */
165{
82cc1f9a
MS
166 ipp_t *request, /* Create-Job request */
167 *response; /* Create-Job response */
168 ipp_attribute_t *attr; /* job-id attribute */
169
170
171 DEBUG_printf(("cupsCreateDestJob(http=%p, dest=%p(%s/%s), info=%p, "
807315e6 172 "job_id=%p, title=\"%s\", num_options=%d, options=%p)", (void *)http, (void *)dest, dest ? dest->name : NULL, dest ? dest->instance : NULL, (void *)info, (void *)job_id, title, num_options, (void *)options));
82cc1f9a
MS
173
174 /*
175 * Range check input...
176 */
177
178 if (job_id)
179 *job_id = 0;
180
181 if (!http || !dest || !info || !job_id)
182 {
cb7f98ee 183 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
82cc1f9a 184 DEBUG_puts("1cupsCreateDestJob: Bad arguments.");
cb7f98ee 185 return (IPP_STATUS_ERROR_INTERNAL);
82cc1f9a
MS
186 }
187
188 /*
189 * Build a Create-Job request...
190 */
191
cb7f98ee 192 if ((request = ippNewRequest(IPP_OP_CREATE_JOB)) == NULL)
82cc1f9a 193 {
cb7f98ee 194 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(ENOMEM), 0);
82cc1f9a 195 DEBUG_puts("1cupsCreateDestJob: Unable to create Create-Job request.");
cb7f98ee 196 return (IPP_STATUS_ERROR_INTERNAL);
82cc1f9a
MS
197 }
198
6961465f
MS
199 ippSetVersion(request, info->version / 10, info->version % 10);
200
82cc1f9a
MS
201 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
202 NULL, info->uri);
203 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
204 NULL, cupsUser());
205 if (title)
206 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
207 title);
a29fd7dd 208
a469f8a5 209 cupsEncodeOptions2(request, num_options, options, IPP_TAG_OPERATION);
a29fd7dd
MS
210 cupsEncodeOptions2(request, num_options, options, IPP_TAG_JOB);
211 cupsEncodeOptions2(request, num_options, options, IPP_TAG_SUBSCRIPTION);
82cc1f9a
MS
212
213 /*
214 * Send the request and get the job-id...
215 */
dcb445bc 216
82cc1f9a
MS
217 response = cupsDoRequest(http, request, info->resource);
218
219 if ((attr = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER)) != NULL)
220 {
221 *job_id = attr->values[0].integer;
222 DEBUG_printf(("1cupsCreateDestJob: job-id=%d", *job_id));
223 }
224
225 ippDelete(response);
226
227 /*
228 * Return the status code from the Create-Job request...
229 */
230
231 DEBUG_printf(("1cupsCreateDestJob: %s (%s)", ippErrorString(cupsLastError()),
232 cupsLastErrorString()));
233
234 return (cupsLastError());
dcb445bc
MS
235}
236
237
238/*
239 * 'cupsFinishDestDocument()' - Finish the current document.
240 *
cb7f98ee 241 * Returns @code IPP_STATUS_OK@ or @code IPP_STATUS_OK_SUBST@ on success.
dcb445bc 242 *
8072030b 243 * @since CUPS 1.6/macOS 10.8@
dcb445bc
MS
244 */
245
82cc1f9a 246ipp_status_t /* O - Status of document submission */
dcb445bc 247cupsFinishDestDocument(
82cc1f9a
MS
248 http_t *http, /* I - Connection to destination */
249 cups_dest_t *dest, /* I - Destination */
250 cups_dinfo_t *info) /* I - Destination information */
dcb445bc 251{
807315e6 252 DEBUG_printf(("cupsFinishDestDocument(http=%p, dest=%p(%s/%s), info=%p)", (void *)http, (void *)dest, dest ? dest->name : NULL, dest ? dest->instance : NULL, (void *)info));
82cc1f9a
MS
253
254 /*
255 * Range check input...
256 */
257
258 if (!http || !dest || !info)
259 {
cb7f98ee 260 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
82cc1f9a 261 DEBUG_puts("1cupsFinishDestDocument: Bad arguments.");
cb7f98ee 262 return (IPP_STATUS_ERROR_INTERNAL);
82cc1f9a
MS
263 }
264
265 /*
266 * Get the response at the end of the document and return it...
267 */
268
269 ippDelete(cupsGetResponse(http, info->resource));
270
271 DEBUG_printf(("1cupsFinishDestDocument: %s (%s)",
272 ippErrorString(cupsLastError()), cupsLastErrorString()));
273
274 return (cupsLastError());
dcb445bc
MS
275}
276
277
278/*
279 * 'cupsStartDestDocument()' - Start a new document.
280 *
281 * "job_id" is the job ID returned by cupsCreateDestJob. "docname" is the name
282 * of the document/file being printed, "format" is the MIME media type for the
283 * document (see CUPS_FORMAT_xxx constants), and "num_options" and "options"
284 * are the options do be applied to the document. "last_document" should be 1
285 * if this is the last document to be submitted in the job. Returns
82cc1f9a 286 * @code HTTP_CONTINUE@ on success.
dcb445bc 287 *
8072030b 288 * @since CUPS 1.6/macOS 10.8@
dcb445bc
MS
289 */
290
82cc1f9a 291http_status_t /* O - Status of document creation */
dcb445bc
MS
292cupsStartDestDocument(
293 http_t *http, /* I - Connection to destination */
294 cups_dest_t *dest, /* I - Destination */
295 cups_dinfo_t *info, /* I - Destination information */
296 int job_id, /* I - Job ID */
297 const char *docname, /* I - Document name */
298 const char *format, /* I - Document format */
299 int num_options, /* I - Number of document options */
300 cups_option_t *options, /* I - Document options */
301 int last_document) /* I - 1 if this is the last document */
302{
82cc1f9a
MS
303 ipp_t *request; /* Send-Document request */
304 http_status_t status; /* HTTP status */
305
306
807315e6 307 DEBUG_printf(("cupsStartDestDocument(http=%p, dest=%p(%s/%s), info=%p, job_id=%d, docname=\"%s\", format=\"%s\", num_options=%d, options=%p, last_document=%d)", (void *)http, (void *)dest, dest ? dest->name : NULL, dest ? dest->instance : NULL, (void *)info, job_id, docname, format, num_options, (void *)options, last_document));
82cc1f9a
MS
308
309 /*
310 * Range check input...
311 */
312
313 if (!http || !dest || !info || job_id <= 0)
314 {
cb7f98ee 315 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
82cc1f9a 316 DEBUG_puts("1cupsStartDestDocument: Bad arguments.");
cb7f98ee 317 return (HTTP_STATUS_ERROR);
82cc1f9a
MS
318 }
319
320 /*
321 * Create a Send-Document request...
322 */
323
cb7f98ee 324 if ((request = ippNewRequest(IPP_OP_SEND_DOCUMENT)) == NULL)
82cc1f9a 325 {
cb7f98ee 326 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(ENOMEM), 0);
82cc1f9a
MS
327 DEBUG_puts("1cupsStartDestDocument: Unable to create Send-Document "
328 "request.");
cb7f98ee 329 return (HTTP_STATUS_ERROR);
82cc1f9a
MS
330 }
331
6961465f
MS
332 ippSetVersion(request, info->version / 10, info->version % 10);
333
82cc1f9a
MS
334 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
335 NULL, info->uri);
336 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", job_id);
337 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
338 NULL, cupsUser());
339 if (docname)
340 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "document-name",
341 NULL, docname);
342 if (format)
343 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE,
344 "document-format", NULL, format);
7e86f2f6 345 ippAddBoolean(request, IPP_TAG_OPERATION, "last-document", (char)last_document);
82cc1f9a 346
a469f8a5 347 cupsEncodeOptions2(request, num_options, options, IPP_TAG_OPERATION);
82cc1f9a
MS
348 cupsEncodeOptions2(request, num_options, options, IPP_TAG_DOCUMENT);
349
350 /*
351 * Send and delete the request, then return the status...
352 */
353
354 status = cupsSendRequest(http, request, info->resource, CUPS_LENGTH_VARIABLE);
355
356 ippDelete(request);
357
358 return (status);
dcb445bc 359}