Moved form related functions as methods in the policy handle object.
[kai/samba-autobuild/.git] / source / 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 "includes.h"
22 #include "Python.h"
23 #include "python/py_spoolss.h"
24
25 struct pyconv py_FORM[] = {
26         { "flags", PY_UINT32, offsetof(FORM, flags) },
27         { "width", PY_UINT32, offsetof(FORM, size_x) },
28         { "length", PY_UINT32, offsetof(FORM, size_y) },
29         { "top", PY_UINT32, offsetof(FORM, top) },
30         { "left", PY_UINT32, offsetof(FORM, left) },
31         { "right", PY_UINT32, offsetof(FORM, right) },
32         { "bottom", PY_UINT32, offsetof(FORM, bottom) },
33         { NULL }
34 };
35
36 struct pyconv py_FORM_1[] = {
37         { "flags", PY_UINT32, offsetof(FORM_1, flag) },
38         { "width", PY_UINT32, offsetof(FORM_1, width) },
39         { "length", PY_UINT32, offsetof(FORM_1, length) },
40         { "top", PY_UINT32, offsetof(FORM_1, top) },
41         { "left", PY_UINT32, offsetof(FORM_1, left) },
42         { "right", PY_UINT32, offsetof(FORM_1, right) },
43         { "bottom", PY_UINT32, offsetof(FORM_1, bottom) },
44         { "name", PY_UNISTR, offsetof(FORM_1, name) },
45         { NULL }
46 };
47
48 /* Add a form */
49
50 PyObject *spoolss_addform(PyObject *self, PyObject *args, PyObject *kw)
51 {
52         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
53         WERROR werror;
54         PyObject *py_form;
55         FORM form;
56         int level = 1;
57         static char *kwlist[] = {"form", "level", NULL};
58
59         /* Parse parameters */
60
61         if (!PyArg_ParseTupleAndKeywords(
62                     args, kw, "O!|i", kwlist, &PyDict_Type, &py_form, &level))
63                 return NULL;
64         
65         /* Call rpc function */
66
67         switch (level) {
68         case 1: {
69                 PyObject *py_form_name;
70                 char *form_name;
71
72                 to_struct(&form, py_form, py_FORM);
73
74                 py_form_name = PyDict_GetItemString(py_form, "name");
75                 form_name = PyString_AsString(py_form_name);
76
77                 init_unistr2(&form.name, form_name, strlen(form_name) + 1);
78
79                 break;
80         }
81         default:
82                 PyErr_SetString(spoolss_error, "unsupported info level");
83                 return NULL;
84         }
85
86         werror = cli_spoolss_addform(hnd->cli, hnd->mem_ctx, &hnd->pol,
87                                      level, &form);
88
89
90         if (!W_ERROR_IS_OK(werror)) {
91                 PyErr_SetObject(spoolss_werror,
92                                 PyInt_FromLong(W_ERROR_V(werror)));
93                 return NULL;
94         }
95
96         Py_INCREF(Py_None);
97         return Py_None;
98 }
99
100 /* Get form properties */
101
102 PyObject *spoolss_getform(PyObject *self, PyObject *args, PyObject *kw)
103 {
104         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
105         WERROR werror;
106         PyObject *result;
107         char *form_name;
108         int level = 1;
109         static char *kwlist[] = {"form_name", "level", NULL};
110         uint32 needed;
111         FORM_1 form;
112
113         /* Parse parameters */
114
115         if (!PyArg_ParseTupleAndKeywords(args, kw, "s|i", kwlist, &form_name, &level))
116                 return NULL;
117         
118         /* Call rpc function */
119
120         werror = cli_spoolss_getform(hnd->cli, hnd->mem_ctx, 0, &needed,
121                                      &hnd->pol, form_name, 1, &form);
122
123         if (W_ERROR_V(werror) == ERRinsufficientbuffer)
124                 werror = cli_spoolss_getform(
125                         hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol,
126                         form_name, 1, &form);
127
128         if (!W_ERROR_IS_OK(werror)) {
129                 PyErr_SetObject(spoolss_werror,
130                                 PyInt_FromLong(W_ERROR_V(werror)));
131                 return NULL;
132         }
133
134         result = Py_None;
135
136         switch(level) {
137         case 1:
138                 result = from_struct(&form, py_FORM_1);
139                 break;
140         }
141
142         Py_INCREF(result);
143         return result;
144 }
145
146 /* Set form properties */
147
148 PyObject *spoolss_setform(PyObject *self, PyObject *args, PyObject *kw)
149 {
150         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
151         WERROR werror;
152         PyObject *py_form;
153         int level = 1;
154         static char *kwlist[] = {"form_name", "form", "level", NULL};
155         char *form_name;
156         FORM form;
157
158         /* Parse parameters */
159
160         if (!PyArg_ParseTupleAndKeywords(args, kw, "sO!|i", kwlist, 
161                                          &form_name, &PyDict_Type, &py_form, &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 }