import HEAD into svn+ssh://svn.samba.org/home/svn/samba/trunk
[metze/old/v3-2-winbind-ndr.git] / source / 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_hnd_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                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
57                 goto done;
58         }
59
60         result = PyList_New(num_jobs);
61
62         switch (level) {
63         case 1: 
64                 for (i = 0; i < num_jobs; i++) {
65                         PyObject *value;
66
67                         py_from_JOB_INFO_1(&value, &ctr.job.job_info_1[i]);
68
69                         PyList_SetItem(result, i, value);
70                 }
71
72                 break;
73         case 2:
74                 for(i = 0; i < num_jobs; i++) {
75                         PyObject *value;
76
77                         py_from_JOB_INFO_2(&value, &ctr.job.job_info_2[i]);
78
79                         PyList_SetItem(result, i, value);
80                 }
81                 
82                 break;
83         }
84
85  done:
86         Py_INCREF(result);
87         return result;
88 }
89
90 /* Set job command */
91
92 PyObject *spoolss_hnd_setjob(PyObject *self, PyObject *args, PyObject *kw)
93 {
94         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
95         WERROR werror;
96         uint32 level = 0, command, jobid;
97         static char *kwlist[] = {"jobid", "command", "level", NULL};
98
99         /* Parse parameters */
100
101         if (!PyArg_ParseTupleAndKeywords(
102                     args, kw, "ii|i", kwlist, &jobid, &command, &level))
103                 return NULL;
104         
105         /* Call rpc function */
106         
107         werror = cli_spoolss_setjob(hnd->cli, hnd->mem_ctx, &hnd->pol,
108                                     jobid, level, command);
109
110         if (!W_ERROR_IS_OK(werror)) {
111                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
112                 return NULL;
113         }
114         
115         Py_INCREF(Py_None);
116         return Py_None;
117 }
118
119 /* Get job */
120
121 PyObject *spoolss_hnd_getjob(PyObject *self, PyObject *args, PyObject *kw)
122 {
123         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
124         WERROR werror;
125         PyObject *result;
126         uint32 level = 1, jobid, needed;
127         static char *kwlist[] = {"jobid", "level", NULL};
128         JOB_INFO_CTR ctr;
129
130         /* Parse parameters */
131
132         if (!PyArg_ParseTupleAndKeywords(
133                     args, kw, "i|i", kwlist, &jobid, &level))
134                 return NULL;
135         
136         /* Call rpc function */
137         
138         werror = cli_spoolss_getjob(hnd->cli, hnd->mem_ctx, 0, &needed,
139                                     &hnd->pol, jobid, level, &ctr);
140
141         if (W_ERROR_V(werror) == ERRinsufficientbuffer)
142                 werror = cli_spoolss_getjob(
143                         hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol,
144                         jobid, level, &ctr);
145
146         if (!W_ERROR_IS_OK(werror)) {
147                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
148                 return NULL;
149         }
150
151         switch(level) {
152         case 1:
153                 py_from_JOB_INFO_1(&result, &ctr.job.job_info_1[0]);
154                 break;
155         case 2:
156                 py_from_JOB_INFO_2(&result, &ctr.job.job_info_2[0]);
157                 break;
158         }
159
160         return result;
161 }
162
163 /* Start page printer.  This notifies the spooler that a page is about to be
164    printed on the specified printer. */
165
166 PyObject *spoolss_hnd_startpageprinter(PyObject *self, PyObject *args, PyObject *kw)
167 {
168         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
169         WERROR werror;
170         static char *kwlist[] = { NULL };
171
172         /* Parse parameters */
173
174         if (!PyArg_ParseTupleAndKeywords(args, kw, "", kwlist))
175                 return NULL;
176         
177         /* Call rpc function */
178         
179         werror = cli_spoolss_startpageprinter(
180                 hnd->cli, hnd->mem_ctx, &hnd->pol);
181
182         if (!W_ERROR_IS_OK(werror)) {
183                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
184                 return NULL;
185         }
186         
187         Py_INCREF(Py_None);
188         return Py_None;
189 }
190
191 /* End page printer.  This notifies the spooler that a page has finished
192    being printed on the specified printer. */
193
194 PyObject *spoolss_hnd_endpageprinter(PyObject *self, PyObject *args, PyObject *kw)
195 {
196         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
197         WERROR werror;
198         static char *kwlist[] = { NULL };
199
200         /* Parse parameters */
201
202         if (!PyArg_ParseTupleAndKeywords(args, kw, "", kwlist))
203                 return NULL;
204         
205         /* Call rpc function */
206         
207         werror = cli_spoolss_endpageprinter(
208                 hnd->cli, hnd->mem_ctx, &hnd->pol);
209
210         if (!W_ERROR_IS_OK(werror)) {
211                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
212                 return NULL;
213         }
214         
215         Py_INCREF(Py_None);
216         return Py_None;
217 }
218
219 /* Start doc printer.  This notifies the spooler that a document is about to be
220    printed on the specified printer. */
221
222 PyObject *spoolss_hnd_startdocprinter(PyObject *self, PyObject *args, PyObject *kw)
223 {
224         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
225         WERROR werror;
226         static char *kwlist[] = { "document_info", NULL };
227         PyObject *info, *obj;
228         uint32 level, jobid;
229         char *document_name = NULL, *output_file = NULL, *data_type = NULL;
230
231         /* Parse parameters */
232
233         if (!PyArg_ParseTupleAndKeywords(
234                     args, kw, "O!", kwlist, &PyDict_Type, &info))
235                 return NULL;
236         
237         /* Check document_info parameter */
238
239         if (!get_level_value(info, &level)) {
240                 PyErr_SetString(spoolss_error, "invalid info level");
241                 return NULL;
242         }
243
244         if (level != 1) {
245                 PyErr_SetString(spoolss_error, "unsupported info level");
246                 return NULL;
247         }
248
249         if ((obj = PyDict_GetItemString(info, "document_name"))) {
250
251                 if (!PyString_Check(obj) && obj != Py_None) {
252                         PyErr_SetString(spoolss_error,
253                                         "document_name not a string");
254                         return NULL;
255                 }
256                 
257                 if (PyString_Check(obj))
258                         document_name = PyString_AsString(obj);
259
260         } else {
261                 PyErr_SetString(spoolss_error, "no document_name present");
262                 return NULL;
263         }
264
265         if ((obj = PyDict_GetItemString(info, "output_file"))) {
266
267                 if (!PyString_Check(obj) && obj != Py_None) {
268                         PyErr_SetString(spoolss_error,
269                                         "output_file not a string");
270                         return NULL;
271                 }
272                 
273                 if (PyString_Check(obj))
274                         output_file = PyString_AsString(obj);
275
276         } else {
277                 PyErr_SetString(spoolss_error, "no output_file present");
278                 return NULL;
279         }
280
281         if ((obj = PyDict_GetItemString(info, "data_type"))) {
282                 
283                 if (!PyString_Check(obj) && obj != Py_None) {
284                         PyErr_SetString(spoolss_error,
285                                         "data_type not a string");
286                         return NULL;
287                 }
288
289                 if (PyString_Check(obj))
290                         data_type = PyString_AsString(obj);
291
292         } else {
293                 PyErr_SetString(spoolss_error, "no data_type present");
294                 return NULL;
295         }
296
297         /* Call rpc function */
298         
299         werror = cli_spoolss_startdocprinter(
300                 hnd->cli, hnd->mem_ctx, &hnd->pol, document_name,
301                 output_file, data_type, &jobid);
302
303         if (!W_ERROR_IS_OK(werror)) {
304                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
305                 return NULL;
306         }
307         
308         /* The return value is zero for an error (where does the status
309            code come from now??) and the return value is the jobid
310            allocated for the new job. */
311
312         return Py_BuildValue("i", jobid);
313 }
314
315 /* End doc printer.  This notifies the spooler that a document has finished
316    being printed on the specified printer. */
317
318 PyObject *spoolss_hnd_enddocprinter(PyObject *self, PyObject *args, PyObject *kw)
319 {
320         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
321         WERROR werror;
322         static char *kwlist[] = { NULL };
323
324         /* Parse parameters */
325
326         if (!PyArg_ParseTupleAndKeywords(args, kw, "", kwlist))
327                 return NULL;
328         
329         /* Call rpc function */
330         
331         werror = cli_spoolss_enddocprinter(hnd->cli, hnd->mem_ctx, &hnd->pol);
332
333         if (!W_ERROR_IS_OK(werror)) {
334                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
335                 return NULL;
336         }
337         
338         Py_INCREF(Py_None);
339         return Py_None;
340 }
341
342 /* Write data to a printer */
343
344 PyObject *spoolss_hnd_writeprinter(PyObject *self, PyObject *args, PyObject *kw)
345 {
346         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
347         WERROR werror;
348         static char *kwlist[] = { "data", NULL };
349         PyObject *data;
350         uint32 num_written;
351
352         /* Parse parameters */
353
354         if (!PyArg_ParseTupleAndKeywords(
355                     args, kw, "O!", kwlist, &PyString_Type, &data))
356                 return NULL;
357         
358         /* Call rpc function */
359         
360         werror = cli_spoolss_writeprinter(
361                 hnd->cli, hnd->mem_ctx, &hnd->pol, PyString_Size(data),
362                 PyString_AsString(data), &num_written);
363
364         if (!W_ERROR_IS_OK(werror)) {
365                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
366                 return NULL;
367         }
368
369         Py_INCREF(Py_None);
370         return Py_None;
371 }
372
373 PyObject *spoolss_hnd_addjob(PyObject *self, PyObject *args, PyObject *kw)
374 {
375         PyErr_SetString(spoolss_error, "Not implemented");
376         return NULL;
377 }