Fixed setform and addform functions.
[tprouty/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 /* Add a form */
24
25 PyObject *spoolss_addform(PyObject *self, PyObject *args, PyObject *kw)
26 {
27         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
28         WERROR werror;
29         PyObject *py_form, *py_form_name;
30         char *form_name;
31         FORM form;
32         int level = 1;
33         static char *kwlist[] = {"form", "level", NULL};
34
35         /* Parse parameters */
36
37         if (!PyArg_ParseTupleAndKeywords(
38                     args, kw, "O!|i", kwlist, &PyDict_Type, &py_form, &level))
39                 return NULL;
40         
41         /* Call rpc function */
42
43         if (!py_to_FORM(&form, py_form) ||
44             !(py_form_name = PyDict_GetItemString(py_form, "name")) ||
45             !(form_name = PyString_AsString(py_form_name))) {
46                 PyErr_SetString(spoolss_error, "invalid form");
47                 return NULL;
48         }
49
50         switch (level) {
51         case 1:
52                 init_unistr2(&form.name, form_name, strlen(form_name) + 1);
53                 break;
54         default:
55                 PyErr_SetString(spoolss_error, "unsupported info level");
56                 return NULL;
57         }
58
59         werror = cli_spoolss_addform(hnd->cli, hnd->mem_ctx, &hnd->pol,
60                                      level, &form);
61
62
63         if (!W_ERROR_IS_OK(werror)) {
64                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
65                 return NULL;
66         }
67
68         Py_INCREF(Py_None);
69         return Py_None;
70 }
71
72 /* Get form properties */
73
74 PyObject *spoolss_getform(PyObject *self, PyObject *args, PyObject *kw)
75 {
76         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
77         WERROR werror;
78         PyObject *result;
79         char *form_name;
80         int level = 1;
81         static char *kwlist[] = {"form_name", "level", NULL};
82         uint32 needed;
83         FORM_1 form;
84
85         /* Parse parameters */
86
87         if (!PyArg_ParseTupleAndKeywords(args, kw, "s|i", kwlist, 
88                                          &form_name, &level))
89                 return NULL;
90         
91         /* Call rpc function */
92
93         werror = cli_spoolss_getform(hnd->cli, hnd->mem_ctx, 0, &needed,
94                                      &hnd->pol, form_name, 1, &form);
95
96         if (W_ERROR_V(werror) == ERRinsufficientbuffer)
97                 werror = cli_spoolss_getform(
98                         hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol,
99                         form_name, 1, &form);
100
101         if (!W_ERROR_IS_OK(werror)) {
102                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
103                 return NULL;
104         }
105
106         result = Py_None;
107
108         switch(level) {
109         case 1:
110                 py_from_FORM_1(&result, &form);
111                 break;
112         }
113
114         Py_INCREF(result);
115         return result;
116 }
117
118 /* Set form properties */
119
120 PyObject *spoolss_setform(PyObject *self, PyObject *args, PyObject *kw)
121 {
122         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
123         WERROR werror;
124         PyObject *py_form, *py_form_name;
125         int level = 1;
126         static char *kwlist[] = {"form", "level", NULL};
127         char *form_name;
128         FORM form;
129
130         /* Parse parameters */
131
132         if (!PyArg_ParseTupleAndKeywords(args, kw, "O!|i", kwlist, 
133                                          &PyDict_Type, &py_form, &level))
134                 return NULL;
135         
136         /* Call rpc function */
137
138         if (!py_to_FORM(&form, py_form) ||
139             !(py_form_name = PyDict_GetItemString(py_form, "name")) ||
140             !(form_name = PyString_AsString(py_form_name))) {
141                 PyErr_SetString(spoolss_error, "invalid form");
142                 return NULL;
143         }
144
145         init_unistr2(&form.name, form_name, strlen(form_name) + 1);
146
147         werror = cli_spoolss_setform(hnd->cli, hnd->mem_ctx, &hnd->pol,
148                                      level, form_name, &form);
149
150         if (!W_ERROR_IS_OK(werror)) {
151                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
152                 return NULL;
153         }
154
155         Py_INCREF(Py_None);
156         return Py_None;
157 }
158
159 /* Delete a form */
160
161 PyObject *spoolss_deleteform(PyObject *self, PyObject *args, PyObject *kw)
162 {
163         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
164         WERROR werror;
165         int level = 1;
166         static char *kwlist[] = {"form_name", "level", NULL};
167         char *form_name;
168
169         /* Parse parameters */
170         
171         if (!PyArg_ParseTupleAndKeywords(
172                     args, kw, "s|i", kwlist, &form_name, &level))
173                 return NULL;
174         
175         /* Call rpc function */
176
177         werror = cli_spoolss_deleteform(
178                 hnd->cli, hnd->mem_ctx, &hnd->pol, form_name);
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 /* Enumerate forms */
190
191 PyObject *spoolss_enumforms(PyObject *self, PyObject *args, PyObject *kw)
192 {
193         PyObject *result;
194         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
195         WERROR werror;
196         uint32 level = 1, num_forms, needed, i;
197         static char *kwlist[] = {"level", NULL};
198         FORM_1 *forms;
199
200         /* Parse parameters */
201         
202         if (!PyArg_ParseTupleAndKeywords(
203                     args, kw, "|i", kwlist, &level))
204                 return NULL;
205         
206         /* Call rpc function */
207
208         werror = cli_spoolss_enumforms(
209                 hnd->cli, hnd->mem_ctx, 0, &needed, &hnd->pol, level,
210                 &num_forms, &forms);
211
212         if (W_ERROR_V(werror) == ERRinsufficientbuffer)
213                 werror = cli_spoolss_enumforms(
214                         hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol, level,
215                         &num_forms, &forms);
216
217         if (!W_ERROR_IS_OK(werror)) {
218                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
219                 return NULL;
220         }
221
222         result = PyList_New(num_forms);
223
224         for (i = 0; i < num_forms; i++) {
225                 PyObject *obj = NULL;
226
227                 switch(level) {
228                 case 1:
229                         py_from_FORM_1(&obj, &forms[i]);
230                         break;
231                 }
232
233                 PyList_SetItem(result, i, obj);
234         }
235
236         return result;
237 }