Removed unused arg to deleteform.
[samba.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 "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         static char *kwlist[] = {"form_name", "level", NULL};
166         char *form_name;
167
168         /* Parse parameters */
169         
170         if (!PyArg_ParseTupleAndKeywords(
171                     args, kw, "s", kwlist, &form_name))
172                 return NULL;
173         
174         /* Call rpc function */
175
176         werror = cli_spoolss_deleteform(
177                 hnd->cli, hnd->mem_ctx, &hnd->pol, form_name);
178
179         if (!W_ERROR_IS_OK(werror)) {
180                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
181                 return NULL;
182         }
183
184         Py_INCREF(Py_None);
185         return Py_None;
186 }
187
188 /* Enumerate forms */
189
190 PyObject *spoolss_enumforms(PyObject *self, PyObject *args, PyObject *kw)
191 {
192         PyObject *result;
193         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
194         WERROR werror;
195         uint32 level = 1, num_forms, needed, i;
196         static char *kwlist[] = {"level", NULL};
197         FORM_1 *forms;
198
199         /* Parse parameters */
200         
201         if (!PyArg_ParseTupleAndKeywords(
202                     args, kw, "|i", kwlist, &level))
203                 return NULL;
204         
205         /* Call rpc function */
206
207         werror = cli_spoolss_enumforms(
208                 hnd->cli, hnd->mem_ctx, 0, &needed, &hnd->pol, level,
209                 &num_forms, &forms);
210
211         if (W_ERROR_V(werror) == ERRinsufficientbuffer)
212                 werror = cli_spoolss_enumforms(
213                         hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol, level,
214                         &num_forms, &forms);
215
216         if (!W_ERROR_IS_OK(werror)) {
217                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
218                 return NULL;
219         }
220
221         result = PyList_New(num_forms);
222
223         for (i = 0; i < num_forms; i++) {
224                 PyObject *obj = NULL;
225
226                 switch(level) {
227                 case 1:
228                         py_from_FORM_1(&obj, &forms[i]);
229                         break;
230                 }
231
232                 PyList_SetItem(result, i, obj);
233         }
234
235         return result;
236 }