Added startdocprinter and enddocprinter.
[sfrench/samba-autobuild/.git] / source3 / python / py_spoolss_jobs.c
1 /* 
2    Python wrappers for DCERPC/SMB client routines.
3
4    Copyright (C) Tim Potter, 2002
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "python/py_spoolss.h"
22
23 /* Enumerate jobs */
24
25 PyObject *spoolss_enumjobs(PyObject *self, PyObject *args, PyObject *kw)
26 {
27         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
28         WERROR werror;
29         PyObject *result;
30         int level = 1;
31         uint32 i, needed, num_jobs;
32         static char *kwlist[] = {"level", NULL};
33         JOB_INFO_CTR ctr;
34
35         /* Parse parameters */
36
37         if (!PyArg_ParseTupleAndKeywords(args, kw, "|i", kwlist, &level))
38                 return NULL;
39         
40         /* Call rpc function */
41         
42         werror = cli_spoolss_enumjobs(
43                 hnd->cli, hnd->mem_ctx, 0, &needed, &hnd->pol, level, 0,
44                 1000, &num_jobs, &ctr);
45
46         if (W_ERROR_V(werror) == ERRinsufficientbuffer)
47                 werror = cli_spoolss_enumjobs(
48                         hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol,
49                         level, 0, 1000, &num_jobs, &ctr);
50
51         /* Return value */
52         
53         result = Py_None;
54
55         if (!W_ERROR_IS_OK(werror))
56                 goto done;
57
58         result = PyList_New(num_jobs);
59
60         switch (level) {
61         case 1: 
62                 for (i = 0; i < num_jobs; i++) {
63                         PyObject *value;
64
65                         py_from_JOB_INFO_1(&value, &ctr.job.job_info_1[i]);
66
67                         PyList_SetItem(result, i, value);
68                 }
69
70                 break;
71         case 2:
72                 for(i = 0; i < num_jobs; i++) {
73                         PyObject *value;
74
75                         py_from_JOB_INFO_2(&value, &ctr.job.job_info_2[i]);
76
77                         PyList_SetItem(result, i, value);
78                 }
79                 
80                 break;
81         }
82
83  done:
84         Py_INCREF(result);
85         return result;
86 }
87
88 /* Set job command */
89
90 PyObject *spoolss_setjob(PyObject *self, PyObject *args, PyObject *kw)
91 {
92         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
93         WERROR werror;
94         uint32 level = 0, command, jobid;
95         static char *kwlist[] = {"jobid", "command", "level", NULL};
96
97         /* Parse parameters */
98
99         if (!PyArg_ParseTupleAndKeywords(args, kw, "ii|i", kwlist, &jobid,
100                                          &command, &level))
101                 return NULL;
102         
103         /* Call rpc function */
104         
105         werror = cli_spoolss_setjob(hnd->cli, hnd->mem_ctx, &hnd->pol,
106                                     jobid, level, command);
107
108         if (!W_ERROR_IS_OK(werror)) {
109                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
110                 return NULL;
111         }
112         
113         Py_INCREF(Py_None);
114         return Py_None;
115 }
116
117 /* Get job */
118
119 PyObject *spoolss_getjob(PyObject *self, PyObject *args, PyObject *kw)
120 {
121         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
122         WERROR werror;
123         PyObject *result;
124         uint32 level = 1, jobid, needed;
125         static char *kwlist[] = {"jobid", "level", NULL};
126         JOB_INFO_CTR ctr;
127
128         /* Parse parameters */
129
130         if (!PyArg_ParseTupleAndKeywords(args, kw, "i|i", kwlist, &jobid,
131                                          &level))
132                 return NULL;
133         
134         /* Call rpc function */
135         
136         werror = cli_spoolss_getjob(hnd->cli, hnd->mem_ctx, 0, &needed,
137                                     &hnd->pol, jobid, level, &ctr);
138
139         if (W_ERROR_V(werror) == ERRinsufficientbuffer)
140                 werror = cli_spoolss_getjob(
141                         hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol,
142                         jobid, level, &ctr);
143
144         if (!W_ERROR_IS_OK(werror)) {
145                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
146                 return NULL;
147         }
148
149         switch(level) {
150         case 1:
151                 py_from_JOB_INFO_1(&result, ctr.job.job_info_1);
152                 break;
153         case 2:
154                 py_from_JOB_INFO_2(&result, ctr.job.job_info_2);
155                 break;
156         }
157
158         return result;
159 }
160
161 /* Start page printer.  This notifies the spooler that a page is about to be
162    printed on the specified printer. */
163
164 PyObject *spoolss_startpageprinter(PyObject *self, PyObject *args, PyObject *kw)
165 {
166         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
167         WERROR werror;
168         static char *kwlist[] = { NULL };
169
170         /* Parse parameters */
171
172         if (!PyArg_ParseTupleAndKeywords(args, kw, "", kwlist))
173                 return NULL;
174         
175         /* Call rpc function */
176         
177         werror = cli_spoolss_startpageprinter(
178                 hnd->cli, hnd->mem_ctx, &hnd->pol);
179
180         if (!W_ERROR_IS_OK(werror)) {
181                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
182                 return NULL;
183         }
184         
185         Py_INCREF(Py_None);
186         return Py_None;
187 }
188
189 /* End page printer.  This notifies the spooler that a page has finished
190    being printed on the specified printer. */
191
192 PyObject *spoolss_endpageprinter(PyObject *self, PyObject *args, PyObject *kw)
193 {
194         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
195         WERROR werror;
196         static char *kwlist[] = { NULL };
197
198         /* Parse parameters */
199
200         if (!PyArg_ParseTupleAndKeywords(args, kw, "", kwlist))
201                 return NULL;
202         
203         /* Call rpc function */
204         
205         werror = cli_spoolss_endpageprinter(
206                 hnd->cli, hnd->mem_ctx, &hnd->pol);
207
208         if (!W_ERROR_IS_OK(werror)) {
209                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
210                 return NULL;
211         }
212         
213         Py_INCREF(Py_None);
214         return Py_None;
215 }
216
217 /* Start doc printer.  This notifies the spooler that a document is about to be
218    printed on the specified printer. */
219
220 PyObject *spoolss_startdocprinter(PyObject *self, PyObject *args, PyObject *kw)
221 {
222         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
223         WERROR werror;
224         static char *kwlist[] = { "document_info", NULL };
225         PyObject *doc_info, *obj;
226         uint32 level, jobid;
227         char *document_name = NULL, *output_file = NULL, *data_type = NULL;
228
229         /* Parse parameters */
230
231         if (!PyArg_ParseTupleAndKeywords(args, kw, "O!", kwlist,
232                 &PyDict_Type, &doc_info))
233                 return NULL;
234         
235         /* Check document_info parameter */
236
237         if ((obj = PyDict_GetItemString(doc_info, "level"))) {
238
239                 if (!PyInt_Check(obj)) {
240                         PyErr_SetString(spoolss_error,
241                                         "level not an integer");
242                         return NULL;
243                 }
244
245                 level = PyInt_AsLong(obj);
246
247                 /* Only level 1 supported by Samba! */
248
249                 if (level != 1) {
250                         PyErr_SetString(spoolss_error,
251                                         "unsupported info level");
252                         return NULL;
253                 }
254
255         } else {
256                 PyErr_SetString(spoolss_error, "no info level present");
257                 return NULL;
258         }
259
260         if ((obj = PyDict_GetItemString(doc_info, "document_name"))) {
261
262                 if (!PyString_Check(obj) && obj != Py_None) {
263                         PyErr_SetString(spoolss_error,
264                                         "document_name not a string");
265                         return NULL;
266                 }
267                 
268                 if (PyString_Check(obj))
269                         document_name = PyString_AsString(obj);
270
271         } else {
272                 PyErr_SetString(spoolss_error, "no document_name present");
273                 return NULL;
274         }
275
276         if ((obj = PyDict_GetItemString(doc_info, "output_file"))) {
277
278                 if (!PyString_Check(obj) && obj != Py_None) {
279                         PyErr_SetString(spoolss_error,
280                                         "output_file not a string");
281                         return NULL;
282                 }
283                 
284                 if (PyString_Check(obj))
285                         output_file = PyString_AsString(obj);
286
287         } else {
288                 PyErr_SetString(spoolss_error, "no output_file present");
289                 return NULL;
290         }
291
292         if ((obj = PyDict_GetItemString(doc_info, "data_type"))) {
293                 
294                 if (!PyString_Check(obj) && obj != Py_None) {
295                         PyErr_SetString(spoolss_error,
296                                         "data_type not a string");
297                         return NULL;
298                 }
299
300                 if (PyString_Check(obj))
301                         data_type = PyString_AsString(obj);
302
303         } else {
304                 PyErr_SetString(spoolss_error, "no data_type present");
305                 return NULL;
306         }
307
308         /* Call rpc function */
309         
310         werror = cli_spoolss_startdocprinter(
311                 hnd->cli, hnd->mem_ctx, &hnd->pol, document_name,
312                 output_file, data_type, &jobid);
313
314         if (!W_ERROR_IS_OK(werror)) {
315                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
316                 return NULL;
317         }
318         
319         /* The return value is zero for an error (where does the status
320            code come from now??) and the return value is the jobid
321            allocated for the new job. */
322
323         return Py_BuildValue("i", jobid);
324 }
325
326 /* End doc printer.  This notifies the spooler that a document has finished
327    being printed on the specified printer. */
328
329 PyObject *spoolss_enddocprinter(PyObject *self, PyObject *args, PyObject *kw)
330 {
331         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
332         WERROR werror;
333         static char *kwlist[] = { NULL };
334
335         /* Parse parameters */
336
337         if (!PyArg_ParseTupleAndKeywords(args, kw, "", kwlist))
338                 return NULL;
339         
340         /* Call rpc function */
341         
342         werror = cli_spoolss_enddocprinter(hnd->cli, hnd->mem_ctx, &hnd->pol);
343
344         if (!W_ERROR_IS_OK(werror)) {
345                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
346                 return NULL;
347         }
348         
349         Py_INCREF(Py_None);
350         return Py_None;
351 }