Return dictionary of printerdata in enumprinterdataex.
[sfrench/samba-autobuild/.git] / source3 / python / py_spoolss_printerdata.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 static BOOL py_from_printerdata(PyObject **dict, char *key, char *value,
24                                 uint32 data_type, char *data, 
25                                 uint32 data_size) 
26 {
27         *dict = PyDict_New();
28
29         PyDict_SetItemString(*dict, "key", Py_BuildValue("s", key ? key : ""));
30         PyDict_SetItemString(*dict, "value", Py_BuildValue("s", value));
31         PyDict_SetItemString(*dict, "type", Py_BuildValue("i", data_type));
32
33         PyDict_SetItemString(*dict, "data", 
34                              Py_BuildValue("s#", data, data_size));
35
36         return True;
37 }
38
39 static BOOL py_to_printerdata(char **key, char **value, uint32 *data_type, 
40                               char **data, uint32 *data_size, 
41                               PyObject *dict)
42 {
43         PyObject *obj;
44
45         if ((obj = PyDict_GetItemString(dict, "key"))) {
46
47                 if (!PyString_Check(obj)) {
48                         PyErr_SetString(spoolss_error,
49                                         "key not a string");
50                         return False;
51                 }
52
53                 *key = PyString_AsString(obj);
54
55                 if (!key[0])
56                         *key = NULL;
57         } else
58                 *key = NULL;
59
60         if ((obj = PyDict_GetItemString(dict, "value"))) {
61
62                 if (!PyString_Check(obj)) {
63                         PyErr_SetString(spoolss_error,
64                                         "value not a string");
65                         return False;
66                 }
67
68                 *value = PyString_AsString(obj);
69         } else {
70                 PyErr_SetString(spoolss_error, "no value present");
71                 return False;
72         }
73
74         if ((obj = PyDict_GetItemString(dict, "type"))) {
75
76                 if (!PyInt_Check(obj)) {
77                         PyErr_SetString(spoolss_error,
78                                         "type not an integer");
79                         return False;
80                 }
81
82                 *data_type = PyInt_AsLong(obj);
83         } else {
84                 PyErr_SetString(spoolss_error, "no type present");
85                 return False;
86         }
87         
88         if ((obj = PyDict_GetItemString(dict, "data"))) {
89
90                 if (!PyString_Check(obj)) {
91                         PyErr_SetString(spoolss_error,
92                                         "data not a string");
93                         return False;
94                 }
95
96                 *data = PyString_AsString(obj);
97                 *data_size = PyString_Size(obj);
98         } else {
99                 PyErr_SetString(spoolss_error, "no data present");
100                 return False;
101         }
102
103         return True;
104 }
105
106 PyObject *spoolss_hnd_getprinterdata(PyObject *self, PyObject *args, PyObject *kw)
107 {
108         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
109         static char *kwlist[] = { "value", NULL };
110         char *value;
111         WERROR werror;
112         uint32 needed, data_type, data_size;
113         char *data;
114         PyObject *result;
115
116         /* Parse parameters */
117
118         if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &value))
119                 return NULL;
120
121         /* Call rpc function */
122
123         werror = cli_spoolss_getprinterdata(
124                 hnd->cli, hnd->mem_ctx, 0, &needed, &hnd->pol, value,
125                 &data_type, &data, &data_size);
126
127         if (W_ERROR_V(werror) == ERRmoredata) 
128                 werror = cli_spoolss_getprinterdata(
129                         hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol, value,
130                         &data_type, &data, &data_size);
131
132         if (!W_ERROR_IS_OK(werror)) {
133                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
134                 return NULL;
135         }
136
137         py_from_printerdata(&result, NULL, value, data_type, data, needed);
138
139         return result;
140 }
141
142 PyObject *spoolss_hnd_setprinterdata(PyObject *self, PyObject *args, PyObject *kw)
143 {
144         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
145         static char *kwlist[] = { "data", NULL };
146         PyObject *py_data;
147         char *key, *value, *data;
148         uint32 data_size, data_type;
149         WERROR werror;
150
151         if (!PyArg_ParseTupleAndKeywords(
152                     args, kw, "O!", kwlist, &PyDict_Type, &py_data))
153                 return NULL;
154         
155         if (!py_to_printerdata(&key, &value, &data_type, &data, &data_size, py_data))
156                 return NULL;
157
158         /* Call rpc function */
159
160         werror = cli_spoolss_setprinterdata(
161                 hnd->cli, hnd->mem_ctx, &hnd->pol, value, data_type,
162                 data, data_size);
163
164         if (!W_ERROR_IS_OK(werror)) {
165                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
166                 return NULL;
167         }
168
169         Py_INCREF(Py_None);
170         return Py_None;
171 }
172
173 PyObject *spoolss_hnd_enumprinterdata(PyObject *self, PyObject *args, PyObject *kw)
174 {
175         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
176         static char *kwlist[] = { NULL };
177         uint32 data_needed, value_needed, ndx = 0, data_size, data_type;
178         char *value, *data;
179         WERROR werror;
180         PyObject *result;
181
182         if (!PyArg_ParseTupleAndKeywords(args, kw, "", kwlist))
183                 return NULL;
184
185         /* Get max buffer sizes for value and data */
186
187         werror = cli_spoolss_enumprinterdata(
188                 hnd->cli, hnd->mem_ctx, &hnd->pol, ndx, 0, 0,
189                 &value_needed, &data_needed, NULL, NULL, NULL, NULL);
190
191         if (!W_ERROR_IS_OK(werror)) {
192                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
193                 return NULL;
194         }
195
196         /* Iterate over all printerdata */
197
198         result = PyDict_New();
199
200         while (W_ERROR_IS_OK(werror)) {
201                 PyObject *obj;
202
203                 werror = cli_spoolss_enumprinterdata(
204                         hnd->cli, hnd->mem_ctx, &hnd->pol, ndx,
205                         value_needed, data_needed, NULL, NULL,
206                         &value, &data_type, &data, &data_size); 
207
208                 if (py_from_printerdata(
209                             &obj, NULL, value, data_type, data, data_size))
210                         PyDict_SetItemString(result, value, obj);
211
212                 ndx++;
213         }
214
215         return result;
216 }
217
218 PyObject *spoolss_hnd_deleteprinterdata(PyObject *self, PyObject *args, PyObject *kw)
219 {
220         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
221         static char *kwlist[] = { "value", NULL };
222         char *value;
223         WERROR werror;
224
225         /* Parse parameters */
226
227         if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &value))
228                 return NULL;
229
230         /* Call rpc function */
231
232         werror = cli_spoolss_deleteprinterdata(
233                 hnd->cli, hnd->mem_ctx, &hnd->pol, value);
234
235         if (!W_ERROR_IS_OK(werror)) {
236                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
237                 return NULL;
238         }
239
240         Py_INCREF(Py_None);
241         return Py_None;
242 }
243
244 PyObject *spoolss_hnd_getprinterdataex(PyObject *self, PyObject *args, PyObject *kw)
245 {
246         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
247         static char *kwlist[] = { "key", "value", NULL };
248         char *key, *value;
249         WERROR werror;
250         uint32 needed, data_type, data_size;
251         char *data;
252         PyObject *result;
253
254         /* Parse parameters */
255
256         if (!PyArg_ParseTupleAndKeywords(args, kw, "ss", kwlist, &key, &value))
257                 return NULL;
258
259         /* Call rpc function */
260
261         werror = cli_spoolss_getprinterdataex(
262                 hnd->cli, hnd->mem_ctx, 0, &needed, &hnd->pol, key,
263                 value, &data_type, &data, &data_size);
264
265         if (W_ERROR_V(werror) == ERRmoredata) 
266                 werror = cli_spoolss_getprinterdataex(
267                         hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol, key,
268                         value, &data_type, &data, &data_size);
269
270         if (!W_ERROR_IS_OK(werror)) {
271                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
272                 return NULL;
273         }
274
275         py_from_printerdata(&result, key, value, data_type, data, needed);
276
277         return result;
278 }
279
280 PyObject *spoolss_hnd_setprinterdataex(PyObject *self, PyObject *args, PyObject *kw)
281 {
282         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
283         static char *kwlist[] = { "data", NULL };
284         PyObject *py_data;
285         char *key, *value, *data;
286         uint32 data_size, data_type;
287         WERROR werror;
288
289         if (!PyArg_ParseTupleAndKeywords(
290                     args, kw, "O!", kwlist, &PyDict_Type, &py_data))
291                 return NULL;
292         
293         if (!py_to_printerdata(&key, &value, &data_type, &data, &data_size, py_data))
294                 return NULL;
295
296         /* Call rpc function */
297
298         werror = cli_spoolss_setprinterdataex(
299                 hnd->cli, hnd->mem_ctx, &hnd->pol, key, value, data_type,
300                 data, data_size);
301
302         if (!W_ERROR_IS_OK(werror)) {
303                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
304                 return NULL;
305         }
306
307         Py_INCREF(Py_None);
308         return Py_None;
309 }
310
311 PyObject *spoolss_hnd_enumprinterdataex(PyObject *self, PyObject *args, PyObject *kw)
312 {
313         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
314         static char *kwlist[] = { NULL };
315         uint32 needed, returned, i;
316         char *key;
317         WERROR werror;
318         PyObject *result;
319         PRINTER_ENUM_VALUES *values;
320
321         if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &key))
322                 return NULL;
323
324         /* Get max buffer sizes for value and data */
325
326         werror = cli_spoolss_enumprinterdataex(
327                 hnd->cli, hnd->mem_ctx, 0, &needed, &hnd->pol, key,
328                 &returned, &values);
329
330         if (W_ERROR_V(werror) == ERRmoredata) 
331                 werror = cli_spoolss_enumprinterdataex(
332                         hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol, key,
333                         &returned, &values);
334
335         if (!W_ERROR_IS_OK(werror)) {
336                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
337                 return NULL;
338         }
339
340         /* Iterate over all printerdata */
341
342         result = PyDict_New();
343
344         for (i = 0; i < returned; i++) {
345                 PyObject *item;
346                 fstring value = "";
347
348                 rpcstr_pull(value, values[i].valuename.buffer, sizeof(value), -1, STR_TERMINATE);
349                 item = PyDict_New();
350                 py_from_printerdata(&item, key, value, values[i].type, values[i].data, 
351                                     values[i].data_len);
352
353                 PyDict_SetItemString(result, value, item);
354         }
355         
356         return result;
357 }
358
359 PyObject *spoolss_hnd_deleteprinterdataex(PyObject *self, PyObject *args, PyObject *kw)
360 {
361         PyErr_SetString(spoolss_error, "Not implemented");
362         return NULL;
363 }