Added commands for startpageprinter, endpageprinter, setjob and getjob.
[samba.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_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 }