Aborted experiment to avoid namespace pollution and prototype hell and
[samba.git] / source3 / python / py_spoolss_forms.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 struct pyconv py_FORM[] = {
24         { "flags", PY_UINT32, offsetof(FORM, flags) },
25         { "width", PY_UINT32, offsetof(FORM, size_x) },
26         { "length", PY_UINT32, offsetof(FORM, size_y) },
27         { "top", PY_UINT32, offsetof(FORM, top) },
28         { "left", PY_UINT32, offsetof(FORM, left) },
29         { "right", PY_UINT32, offsetof(FORM, right) },
30         { "bottom", PY_UINT32, offsetof(FORM, bottom) },
31         { NULL }
32 };
33
34 struct pyconv py_FORM_1[] = {
35         { "flags", PY_UINT32, offsetof(FORM_1, flag) },
36         { "width", PY_UINT32, offsetof(FORM_1, width) },
37         { "length", PY_UINT32, offsetof(FORM_1, length) },
38         { "top", PY_UINT32, offsetof(FORM_1, top) },
39         { "left", PY_UINT32, offsetof(FORM_1, left) },
40         { "right", PY_UINT32, offsetof(FORM_1, right) },
41         { "bottom", PY_UINT32, offsetof(FORM_1, bottom) },
42         { "name", PY_UNISTR, offsetof(FORM_1, name) },
43         { NULL }
44 };
45
46 /* Add a form */
47
48 PyObject *spoolss_addform(PyObject *self, PyObject *args, PyObject *kw)
49 {
50         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
51         WERROR werror;
52         PyObject *py_form;
53         FORM form;
54         int level = 1;
55         static char *kwlist[] = {"form", "level", NULL};
56
57         /* Parse parameters */
58
59         if (!PyArg_ParseTupleAndKeywords(
60                     args, kw, "O!|i", kwlist, &PyDict_Type, &py_form, &level))
61                 return NULL;
62         
63         /* Call rpc function */
64
65         switch (level) {
66         case 1: {
67                 PyObject *py_form_name;
68                 char *form_name;
69
70                 to_struct(&form, py_form, py_FORM);
71
72                 py_form_name = PyDict_GetItemString(py_form, "name");
73                 form_name = PyString_AsString(py_form_name);
74
75                 init_unistr2(&form.name, form_name, strlen(form_name) + 1);
76
77                 break;
78         }
79         default:
80                 PyErr_SetString(spoolss_error, "unsupported info level");
81                 return NULL;
82         }
83
84         werror = cli_spoolss_addform(hnd->cli, hnd->mem_ctx, &hnd->pol,
85                                      level, &form);
86
87
88         if (!W_ERROR_IS_OK(werror)) {
89                 PyErr_SetObject(spoolss_werror,
90                                 PyInt_FromLong(W_ERROR_V(werror)));
91                 return NULL;
92         }
93
94         Py_INCREF(Py_None);
95         return Py_None;
96 }
97
98 /* Get form properties */
99
100 PyObject *spoolss_getform(PyObject *self, PyObject *args, PyObject *kw)
101 {
102         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
103         WERROR werror;
104         PyObject *result;
105         char *form_name;
106         int level = 1;
107         static char *kwlist[] = {"form_name", "level", NULL};
108         uint32 needed;
109         FORM_1 form;
110
111         /* Parse parameters */
112
113         if (!PyArg_ParseTupleAndKeywords(args, kw, "s|i", kwlist, 
114                                          &form_name, &level))
115                 return NULL;
116         
117         /* Call rpc function */
118
119         werror = cli_spoolss_getform(hnd->cli, hnd->mem_ctx, 0, &needed,
120                                      &hnd->pol, form_name, 1, &form);
121
122         if (W_ERROR_V(werror) == ERRinsufficientbuffer)
123                 werror = cli_spoolss_getform(
124                         hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol,
125                         form_name, 1, &form);
126
127         if (!W_ERROR_IS_OK(werror)) {
128                 PyErr_SetObject(spoolss_werror,
129                                 PyInt_FromLong(W_ERROR_V(werror)));
130                 return NULL;
131         }
132
133         result = Py_None;
134
135         switch(level) {
136         case 1:
137                 result = from_struct(&form, py_FORM_1);
138                 break;
139         }
140
141         Py_INCREF(result);
142         return result;
143 }
144
145 /* Set form properties */
146
147 PyObject *spoolss_setform(PyObject *self, PyObject *args, PyObject *kw)
148 {
149         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
150         WERROR werror;
151         PyObject *py_form;
152         int level = 1;
153         static char *kwlist[] = {"form_name", "form", "level", NULL};
154         char *form_name;
155         FORM form;
156
157         /* Parse parameters */
158
159         if (!PyArg_ParseTupleAndKeywords(args, kw, "sO!|i", kwlist, 
160                                          &form_name, &PyDict_Type, &py_form,
161                                          &level))
162                 return NULL;
163         
164         /* Call rpc function */
165
166         to_struct(&form, py_form, py_FORM);
167         init_unistr2(&form.name, form_name, strlen(form_name) + 1);
168
169         werror = cli_spoolss_setform(hnd->cli, hnd->mem_ctx, &hnd->pol,
170                                      level, form_name, &form);
171
172         if (!W_ERROR_IS_OK(werror)) {
173                 PyErr_SetObject(spoolss_werror, 
174                                 PyInt_FromLong(W_ERROR_V(werror)));
175
176                 return NULL;
177         }
178
179         Py_INCREF(Py_None);
180         return Py_None;
181 }
182
183 /* Delete a form */
184
185 PyObject *spoolss_deleteform(PyObject *self, PyObject *args, PyObject *kw)
186 {
187         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
188         WERROR werror;
189         int level = 1;
190         static char *kwlist[] = {"form_name", "level", NULL};
191         char *form_name;
192
193         /* Parse parameters */
194         
195         if (!PyArg_ParseTupleAndKeywords(
196                     args, kw, "s|i", kwlist, &form_name, &level))
197                 return NULL;
198         
199         /* Call rpc function */
200
201         werror = cli_spoolss_deleteform(
202                 hnd->cli, hnd->mem_ctx, &hnd->pol, form_name);
203
204         if (!W_ERROR_IS_OK(werror)) {
205                 PyErr_SetObject(spoolss_werror,
206                                 PyInt_FromLong(W_ERROR_V(werror)));
207                 return NULL;
208         }
209
210         Py_INCREF(Py_None);
211         return Py_None;
212 }
213
214 /* Enumerate forms */
215
216 PyObject *spoolss_enumforms(PyObject *self, PyObject *args, PyObject *kw)
217 {
218         PyObject *result;
219         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
220         WERROR werror;
221         uint32 level = 1, num_forms, needed, i;
222         static char *kwlist[] = {"level", NULL};
223         FORM_1 *forms;
224
225         /* Parse parameters */
226         
227         if (!PyArg_ParseTupleAndKeywords(
228                     args, kw, "|i", kwlist, &level))
229                 return NULL;
230         
231         /* Call rpc function */
232
233         werror = cli_spoolss_enumforms(
234                 hnd->cli, hnd->mem_ctx, 0, &needed, &hnd->pol, level,
235                 &num_forms, &forms);
236
237         if (W_ERROR_V(werror) == ERRinsufficientbuffer)
238                 werror = cli_spoolss_enumforms(
239                         hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol, level,
240                         &num_forms, &forms);
241
242         if (!W_ERROR_IS_OK(werror)) {
243                 PyErr_SetObject(spoolss_werror,
244                                 PyInt_FromLong(W_ERROR_V(werror)));
245                 return NULL;
246         }
247
248         result = PyList_New(num_forms);
249
250         for (i = 0; i < num_forms; i++) {
251                 PyObject *obj = NULL;
252
253                 switch(level) {
254                 case 1:
255                         obj = from_struct(&forms[i], py_FORM_1);
256                         break;
257                 }
258
259                 PyList_SetItem(result, i, obj);
260         }
261
262         return result;
263 }