preparing for release of 3.0.1rc1
[Samba.git] / source / python / py_spoolss_forms.c
blob66a6540e074b21bb76acb75bf9311801be6585ad
1 /*
2 Python wrappers for DCERPC/SMB client routines.
4 Copyright (C) Tim Potter, 2002
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.
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.
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.
21 #include "python/py_spoolss.h"
23 /* Add a form */
25 PyObject *spoolss_hnd_addform(PyObject *self, PyObject *args, PyObject *kw)
27 spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
28 WERROR werror;
29 PyObject *info;
30 FORM form;
31 int level;
32 static char *kwlist[] = {"form", NULL};
34 /* Parse parameters */
36 if (!PyArg_ParseTupleAndKeywords(
37 args, kw, "O!", kwlist, &PyDict_Type, &info))
38 return NULL;
40 /* Call rpc function */
42 if (!py_to_FORM(&form, info)) {
43 PyErr_SetString(spoolss_error, "invalid form");
44 return NULL;
47 if (!get_level_value(info, &level)) {
48 PyErr_SetString(spoolss_error, "invalid info level");
49 return NULL;
52 if (level != 1) {
53 PyErr_SetString(spoolss_error, "unsupported info level");
54 return NULL;
57 switch (level) {
58 case 1: {
59 PyObject *obj = PyDict_GetItemString(info, "name");
60 char *form_name = PyString_AsString(obj);
62 init_unistr2(&form.name, form_name, UNI_STR_TERMINATE);
63 break;
65 default:
66 PyErr_SetString(spoolss_error, "unsupported info level");
67 return NULL;
70 werror = cli_spoolss_addform(hnd->cli, hnd->mem_ctx, &hnd->pol,
71 level, &form);
74 if (!W_ERROR_IS_OK(werror)) {
75 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
76 return NULL;
79 Py_INCREF(Py_None);
80 return Py_None;
83 /* Get form properties */
85 PyObject *spoolss_hnd_getform(PyObject *self, PyObject *args, PyObject *kw)
87 spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
88 WERROR werror;
89 PyObject *result;
90 char *form_name;
91 int level = 1;
92 static char *kwlist[] = {"form_name", "level", NULL};
93 uint32 needed;
94 FORM_1 form;
96 /* Parse parameters */
98 if (!PyArg_ParseTupleAndKeywords(
99 args, kw, "s|i", kwlist, &form_name, &level))
100 return NULL;
102 /* Call rpc function */
104 werror = cli_spoolss_getform(hnd->cli, hnd->mem_ctx, 0, &needed,
105 &hnd->pol, form_name, level, &form);
107 if (W_ERROR_V(werror) == ERRinsufficientbuffer)
108 werror = cli_spoolss_getform(
109 hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol,
110 form_name, 1, &form);
112 if (!W_ERROR_IS_OK(werror)) {
113 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
114 return NULL;
117 result = Py_None;
119 switch(level) {
120 case 1:
121 py_from_FORM_1(&result, &form);
122 break;
125 Py_INCREF(result);
126 return result;
129 /* Set form properties */
131 PyObject *spoolss_hnd_setform(PyObject *self, PyObject *args, PyObject *kw)
133 spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
134 WERROR werror;
135 PyObject *info, *form_name;
136 int level;
137 static char *kwlist[] = { "form", NULL};
138 FORM form;
140 /* Parse parameters */
142 if (!PyArg_ParseTupleAndKeywords(
143 args, kw, "O!", kwlist, &PyDict_Type, &info))
144 return NULL;
146 if (!get_level_value(info, &level)) {
147 PyErr_SetString(spoolss_error, "invalid info level");
148 return NULL;
151 if (level != 1) {
152 PyErr_SetString(spoolss_error, "unsupported info level");
153 return NULL;
156 /* Call rpc function */
158 if (!py_to_FORM(&form, info)) {
159 PyErr_SetString(spoolss_error, "invalid form");
160 return NULL;
163 form_name = PyDict_GetItemString(info, "name");
165 werror = cli_spoolss_setform(
166 hnd->cli, hnd->mem_ctx, &hnd->pol, level,
167 PyString_AsString(form_name), &form);
169 if (!W_ERROR_IS_OK(werror)) {
170 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
171 return NULL;
174 Py_INCREF(Py_None);
175 return Py_None;
178 /* Delete a form */
180 PyObject *spoolss_hnd_deleteform(PyObject *self, PyObject *args, PyObject *kw)
182 spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
183 WERROR werror;
184 static char *kwlist[] = {"form_name", NULL};
185 char *form_name;
187 /* Parse parameters */
189 if (!PyArg_ParseTupleAndKeywords(
190 args, kw, "s", kwlist, &form_name))
191 return NULL;
193 /* Call rpc function */
195 werror = cli_spoolss_deleteform(
196 hnd->cli, hnd->mem_ctx, &hnd->pol, form_name);
198 if (!W_ERROR_IS_OK(werror)) {
199 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
200 return NULL;
203 Py_INCREF(Py_None);
204 return Py_None;
207 /* Enumerate forms */
209 PyObject *spoolss_hnd_enumforms(PyObject *self, PyObject *args, PyObject *kw)
211 PyObject *result;
212 spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
213 WERROR werror;
214 uint32 level = 1, num_forms, needed, i;
215 static char *kwlist[] = {"level", NULL};
216 FORM_1 *forms;
218 /* Parse parameters */
220 if (!PyArg_ParseTupleAndKeywords(
221 args, kw, "|i", kwlist, &level))
222 return NULL;
224 /* Call rpc function */
226 werror = cli_spoolss_enumforms(
227 hnd->cli, hnd->mem_ctx, 0, &needed, &hnd->pol, level,
228 &num_forms, &forms);
230 if (W_ERROR_V(werror) == ERRinsufficientbuffer)
231 werror = cli_spoolss_enumforms(
232 hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol, level,
233 &num_forms, &forms);
235 if (!W_ERROR_IS_OK(werror)) {
236 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
237 return NULL;
240 switch(level) {
241 case 1:
242 result = PyDict_New();
244 for (i = 0; i < num_forms; i++) {
245 PyObject *value;
246 fstring name;
248 rpcstr_pull(name, forms[i].name.buffer,
249 sizeof(fstring), -1, STR_TERMINATE);
251 py_from_FORM_1(&value, &forms[i]);
253 PyDict_SetItemString(
254 value, "level", PyInt_FromLong(1));
256 PyDict_SetItemString(result, name, value);
259 break;
260 default:
261 PyErr_SetString(spoolss_error, "unknown info level");
262 return NULL;
265 return result;