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