2 * Destination job support for CUPS.
4 * Copyright 2012-2016 by Apple Inc.
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
10 * file is missing or damaged, see the license at "http://www.cups.org/".
12 * This file is subject to the Apple OS-Developed Software exception.
16 * Include necessary headers...
19 #include "cups-private.h"
23 * 'cupsCancelDestJob()' - Cancel a job on a destination.
25 * The "job_id" is the number returned by cupsCreateDestJob.
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.
31 * @since CUPS 1.6/OS X 10.8@
35 cupsCancelDestJob(http_t
*http
, /* I - Connection to destination */
36 cups_dest_t
*dest
, /* I - Destination */
37 int job_id
) /* I - Job ID */
39 cups_dinfo_t
*info
; /* Destination information */
42 if ((info
= cupsCopyDestInfo(http
, dest
)) != NULL
)
44 ipp_t
*request
; /* Cancel-Job request */
46 request
= ippNewRequest(IPP_OP_CANCEL_JOB
);
48 ippSetVersion(request
, info
->version
/ 10, info
->version
% 10);
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());
54 ippDelete(cupsDoRequest(http
, request
, info
->resource
));
55 cupsFreeDestInfo(info
);
58 return (cupsLastError());
63 * 'cupsCloseDestJob()' - Close a job and start printing.
65 * Use when the last call to cupsStartDocument passed 0 for "last_document".
66 * "job_id" is the job ID returned by cupsCreateDestJob. Returns @code IPP_STATUS_OK@
69 * @since CUPS 1.6/OS X 10.8@
72 ipp_status_t
/* O - IPP status code */
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 */
79 int i
; /* Looping var */
80 ipp_t
*request
= NULL
;/* Close-Job/Send-Document request */
81 ipp_attribute_t
*attr
; /* operations-supported attribute */
84 DEBUG_printf(("cupsCloseDestJob(http=%p, dest=%p(%s/%s), info=%p, job_id=%d)",
85 http
, dest
, dest
? dest
->name
: NULL
,
86 dest
? dest
->instance
: NULL
, info
, job_id
));
89 * Range check input...
92 if (!http
|| !dest
|| !info
|| job_id
<= 0)
94 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
95 DEBUG_puts("1cupsCloseDestJob: Bad arguments.");
96 return (IPP_STATUS_ERROR_INTERNAL
);
100 * Build a Close-Job or empty Send-Document request...
103 if ((attr
= ippFindAttribute(info
->attrs
, "operations-supported",
104 IPP_TAG_ENUM
)) != NULL
)
106 for (i
= 0; i
< attr
->num_values
; i
++)
107 if (attr
->values
[i
].integer
== IPP_OP_CLOSE_JOB
)
109 request
= ippNewRequest(IPP_OP_CLOSE_JOB
);
115 request
= ippNewRequest(IPP_OP_SEND_DOCUMENT
);
119 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(ENOMEM
), 0);
120 DEBUG_puts("1cupsCloseDestJob: Unable to create Close-Job/Send-Document "
122 return (IPP_STATUS_ERROR_INTERNAL
);
125 ippSetVersion(request
, info
->version
/ 10, info
->version
% 10);
127 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
129 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
131 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
133 if (ippGetOperation(request
) == IPP_OP_SEND_DOCUMENT
)
134 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
137 * Send the request and return the status...
140 ippDelete(cupsDoRequest(http
, request
, info
->resource
));
142 DEBUG_printf(("1cupsCloseDestJob: %s (%s)", ippErrorString(cupsLastError()),
143 cupsLastErrorString()));
145 return (cupsLastError());
150 * 'cupsCreateDestJob()' - Create a job on a destination.
152 * Returns @code IPP_STATUS_OK@ or @code IPP_STATUS_OK_SUBST@ on success, saving the job ID
153 * in the variable pointed to by "job_id".
155 * @since CUPS 1.6/OS X 10.8@
158 ipp_status_t
/* O - IPP status code */
160 http_t
*http
, /* I - Connection to destination */
161 cups_dest_t
*dest
, /* I - Destination */
162 cups_dinfo_t
*info
, /* I - Destination information */
163 int *job_id
, /* O - Job ID or 0 on error */
164 const char *title
, /* I - Job name */
165 int num_options
, /* I - Number of job options */
166 cups_option_t
*options
) /* I - Job options */
168 ipp_t
*request
, /* Create-Job request */
169 *response
; /* Create-Job response */
170 ipp_attribute_t
*attr
; /* job-id attribute */
173 DEBUG_printf(("cupsCreateDestJob(http=%p, dest=%p(%s/%s), info=%p, "
174 "job_id=%p, title=\"%s\", num_options=%d, options=%p)",
175 http
, dest
, dest
? dest
->name
: NULL
,
176 dest
? dest
->instance
: NULL
, info
, job_id
, title
, num_options
,
180 * Range check input...
186 if (!http
|| !dest
|| !info
|| !job_id
)
188 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
189 DEBUG_puts("1cupsCreateDestJob: Bad arguments.");
190 return (IPP_STATUS_ERROR_INTERNAL
);
194 * Build a Create-Job request...
197 if ((request
= ippNewRequest(IPP_OP_CREATE_JOB
)) == NULL
)
199 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(ENOMEM
), 0);
200 DEBUG_puts("1cupsCreateDestJob: Unable to create Create-Job request.");
201 return (IPP_STATUS_ERROR_INTERNAL
);
204 ippSetVersion(request
, info
->version
/ 10, info
->version
% 10);
206 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
208 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
211 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
214 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_OPERATION
);
215 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_JOB
);
216 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_SUBSCRIPTION
);
219 * Send the request and get the job-id...
222 response
= cupsDoRequest(http
, request
, info
->resource
);
224 if ((attr
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
)) != NULL
)
226 *job_id
= attr
->values
[0].integer
;
227 DEBUG_printf(("1cupsCreateDestJob: job-id=%d", *job_id
));
233 * Return the status code from the Create-Job request...
236 DEBUG_printf(("1cupsCreateDestJob: %s (%s)", ippErrorString(cupsLastError()),
237 cupsLastErrorString()));
239 return (cupsLastError());
244 * 'cupsFinishDestDocument()' - Finish the current document.
246 * Returns @code IPP_STATUS_OK@ or @code IPP_STATUS_OK_SUBST@ on success.
248 * @since CUPS 1.6/OS X 10.8@
251 ipp_status_t
/* O - Status of document submission */
252 cupsFinishDestDocument(
253 http_t
*http
, /* I - Connection to destination */
254 cups_dest_t
*dest
, /* I - Destination */
255 cups_dinfo_t
*info
) /* I - Destination information */
257 DEBUG_printf(("cupsFinishDestDocument(http=%p, dest=%p(%s/%s), info=%p)",
258 http
, dest
, dest
? dest
->name
: NULL
,
259 dest
? dest
->instance
: NULL
, info
));
262 * Range check input...
265 if (!http
|| !dest
|| !info
)
267 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
268 DEBUG_puts("1cupsFinishDestDocument: Bad arguments.");
269 return (IPP_STATUS_ERROR_INTERNAL
);
273 * Get the response at the end of the document and return it...
276 ippDelete(cupsGetResponse(http
, info
->resource
));
278 DEBUG_printf(("1cupsFinishDestDocument: %s (%s)",
279 ippErrorString(cupsLastError()), cupsLastErrorString()));
281 return (cupsLastError());
286 * 'cupsStartDestDocument()' - Start a new document.
288 * "job_id" is the job ID returned by cupsCreateDestJob. "docname" is the name
289 * of the document/file being printed, "format" is the MIME media type for the
290 * document (see CUPS_FORMAT_xxx constants), and "num_options" and "options"
291 * are the options do be applied to the document. "last_document" should be 1
292 * if this is the last document to be submitted in the job. Returns
293 * @code HTTP_CONTINUE@ on success.
295 * @since CUPS 1.6/OS X 10.8@
298 http_status_t
/* O - Status of document creation */
299 cupsStartDestDocument(
300 http_t
*http
, /* I - Connection to destination */
301 cups_dest_t
*dest
, /* I - Destination */
302 cups_dinfo_t
*info
, /* I - Destination information */
303 int job_id
, /* I - Job ID */
304 const char *docname
, /* I - Document name */
305 const char *format
, /* I - Document format */
306 int num_options
, /* I - Number of document options */
307 cups_option_t
*options
, /* I - Document options */
308 int last_document
) /* I - 1 if this is the last document */
310 ipp_t
*request
; /* Send-Document request */
311 http_status_t status
; /* HTTP status */
314 DEBUG_printf(("cupsStartDestDocument(http=%p, dest=%p(%s/%s), info=%p, "
315 "job_id=%d, docname=\"%s\", format=\"%s\", num_options=%d, "
316 "options=%p, last_document=%d)",
317 http
, dest
, dest
? dest
->name
: NULL
,
318 dest
? dest
->instance
: NULL
, info
, job_id
, docname
, format
,
319 num_options
, options
, last_document
));
322 * Range check input...
325 if (!http
|| !dest
|| !info
|| job_id
<= 0)
327 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
328 DEBUG_puts("1cupsStartDestDocument: Bad arguments.");
329 return (HTTP_STATUS_ERROR
);
333 * Create a Send-Document request...
336 if ((request
= ippNewRequest(IPP_OP_SEND_DOCUMENT
)) == NULL
)
338 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(ENOMEM
), 0);
339 DEBUG_puts("1cupsStartDestDocument: Unable to create Send-Document "
341 return (HTTP_STATUS_ERROR
);
344 ippSetVersion(request
, info
->version
/ 10, info
->version
% 10);
346 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
348 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", job_id
);
349 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
352 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "document-name",
355 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
356 "document-format", NULL
, format
);
357 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", (char)last_document
);
359 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_OPERATION
);
360 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_DOCUMENT
);
363 * Send and delete the request, then return the status...
366 status
= cupsSendRequest(http
, request
, info
->resource
, CUPS_LENGTH_VARIABLE
);