Initial import
[samba] / source / 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 #include "python/py_conv.h"
23
24 static BOOL py_from_printerdata(PyObject **dict, char *key, char *value,
25                                 uint16 data_type, uint8 *data, 
26                                 uint32 data_size) 
27 {
28         *dict = PyDict_New();
29
30         PyDict_SetItemString(*dict, "key", Py_BuildValue("s", key ? key : ""));
31         PyDict_SetItemString(*dict, "value", Py_BuildValue("s", value));
32         PyDict_SetItemString(*dict, "type", Py_BuildValue("i", data_type));
33
34         PyDict_SetItemString(*dict, "data", 
35                              Py_BuildValue("s#", data, data_size));
36
37         return True;
38 }
39
40 static BOOL py_to_printerdata(char **key, char **value, uint16 *data_type, 
41                               uint8 **data, uint32 *data_size, 
42                               PyObject *dict)
43 {
44         PyObject *obj;
45
46         if ((obj = PyDict_GetItemString(dict, "key"))) {
47
48                 if (!PyString_Check(obj)) {
49                         PyErr_SetString(spoolss_error,
50                                         "key not a string");
51                         return False;
52                 }
53
54                 if (key) {
55                         *key = PyString_AsString(obj);
56
57                         if (!key[0])
58                                 *key = NULL;
59                 }
60         } else
61                 *key = NULL;
62
63         if ((obj = PyDict_GetItemString(dict, "value"))) {
64
65                 if (!PyString_Check(obj)) {
66                         PyErr_SetString(spoolss_error,
67                                         "value not a string");
68                         return False;
69                 }
70
71                 *value = PyString_AsString(obj);
72         } else {
73                 PyErr_SetString(spoolss_error, "no value present");
74                 return False;
75         }
76
77         if ((obj = PyDict_GetItemString(dict, "type"))) {
78
79                 if (!PyInt_Check(obj)) {
80                         PyErr_SetString(spoolss_error,
81                                         "type not an integer");
82                         return False;
83                 }
84
85                 *data_type = PyInt_AsLong(obj);
86         } else {
87                 PyErr_SetString(spoolss_error, "no type present");
88                 return False;
89         }
90         
91         if ((obj = PyDict_GetItemString(dict, "data"))) {
92
93                 if (!PyString_Check(obj)) {
94                         PyErr_SetString(spoolss_error,
95                                         "data not a string");
96                         return False;
97                 }
98
99                 *data = PyString_AsString(obj);
100                 *data_size = PyString_Size(obj);
101         } else {
102                 PyErr_SetString(spoolss_error, "no data present");
103                 return False;
104         }
105
106         return True;
107 }
108
109 PyObject *spoolss_hnd_getprinterdata(PyObject *self, PyObject *args, PyObject *kw)
110 {
111         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
112         static char *kwlist[] = { "value", NULL };
113         char *valuename;
114         WERROR werror;
115         PyObject *result;
116         REGISTRY_VALUE value;
117
118         /* Parse parameters */
119
120         if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &valuename))
121                 return NULL;
122
123         /* Call rpc function */
124
125         werror = rpccli_spoolss_getprinterdata(
126                 hnd->cli, hnd->mem_ctx, &hnd->pol, valuename,
127                 &value);
128
129         if (!W_ERROR_IS_OK(werror)) {
130                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
131                 return NULL;
132         }
133
134         py_from_printerdata(
135                 &result, NULL, valuename, value.type, value.data_p, 
136                 value.size);
137
138         return result;
139 }
140
141 PyObject *spoolss_hnd_setprinterdata(PyObject *self, PyObject *args, PyObject *kw)
142 {
143         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
144         static char *kwlist[] = { "data", NULL };
145         PyObject *py_data;
146         char *valuename;
147         WERROR werror;
148         REGISTRY_VALUE value;
149
150         if (!PyArg_ParseTupleAndKeywords(
151                     args, kw, "O!", kwlist, &PyDict_Type, &py_data))
152                 return NULL;
153         
154         if (!py_to_printerdata(
155                     NULL, &valuename, &value.type, &value.data_p, 
156                     &value.size, py_data))
157                 return NULL;
158
159         fstrcpy(value.valuename, valuename);
160         
161         /* Call rpc function */
162
163         werror = rpccli_spoolss_setprinterdata(
164                 hnd->cli, hnd->mem_ctx, &hnd->pol, &value);
165
166         if (!W_ERROR_IS_OK(werror)) {
167                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
168                 return NULL;
169         }
170
171         Py_INCREF(Py_None);
172         return Py_None;
173 }
174
175 PyObject *spoolss_hnd_enumprinterdata(PyObject *self, PyObject *args, PyObject *kw)
176 {
177         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
178         static char *kwlist[] = { NULL };
179         uint32 data_needed, value_needed, ndx = 0;
180         WERROR werror;
181         PyObject *result;
182         REGISTRY_VALUE value;
183
184         if (!PyArg_ParseTupleAndKeywords(args, kw, "", kwlist))
185                 return NULL;
186
187         /* Get max buffer sizes for value and data */
188
189         werror = rpccli_spoolss_enumprinterdata(
190                 hnd->cli, hnd->mem_ctx, &hnd->pol, ndx, 0, 0,
191                 &value_needed, &data_needed, NULL);
192
193         if (!W_ERROR_IS_OK(werror)) {
194                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
195                 return NULL;
196         }
197
198         /* Iterate over all printerdata */
199
200         result = PyDict_New();
201
202         while (W_ERROR_IS_OK(werror)) {
203                 PyObject *obj;
204
205                 werror = rpccli_spoolss_enumprinterdata(
206                         hnd->cli, hnd->mem_ctx, &hnd->pol, ndx,
207                         value_needed, data_needed, NULL, NULL, &value);
208
209                 if (py_from_printerdata(
210                             &obj, NULL, value.valuename, value.type, 
211                             value.data_p, value.size))
212                         PyDict_SetItemString(result, value.valuename, obj);
213
214                 ndx++;
215         }
216
217         return result;
218 }
219
220 PyObject *spoolss_hnd_deleteprinterdata(PyObject *self, PyObject *args, PyObject *kw)
221 {
222         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
223         static char *kwlist[] = { "value", NULL };
224         char *value;
225         WERROR werror;
226
227         /* Parse parameters */
228
229         if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &value))
230                 return NULL;
231
232         /* Call rpc function */
233
234         werror = rpccli_spoolss_deleteprinterdata(
235                 hnd->cli, hnd->mem_ctx, &hnd->pol, value);
236
237         if (!W_ERROR_IS_OK(werror)) {
238                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
239                 return NULL;
240         }
241
242         Py_INCREF(Py_None);
243         return Py_None;
244 }
245
246 PyObject *spoolss_hnd_getprinterdataex(PyObject *self, PyObject *args, PyObject *kw)
247 {
248         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
249         static char *kwlist[] = { "key", "value", NULL };
250         char *key, *valuename;
251         WERROR werror;
252         PyObject *result;
253         REGISTRY_VALUE value;
254
255         /* Parse parameters */
256
257         if (!PyArg_ParseTupleAndKeywords(args, kw, "ss", kwlist, &key, &valuename))
258                 return NULL;
259
260         /* Call rpc function */
261
262         werror = rpccli_spoolss_getprinterdataex(
263                 hnd->cli, hnd->mem_ctx, &hnd->pol, key,
264                 valuename, &value);
265
266         if (!W_ERROR_IS_OK(werror)) {
267                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
268                 return NULL;
269         }
270
271         py_from_printerdata(
272                 &result, key, valuename, value.type, value.data_p, value.size);
273
274         return result;
275 }
276
277 PyObject *spoolss_hnd_setprinterdataex(PyObject *self, PyObject *args, PyObject *kw)
278 {
279         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
280         static char *kwlist[] = { "data", NULL };
281         PyObject *py_data;
282         char *keyname, *valuename;
283         WERROR werror;
284         REGISTRY_VALUE value;
285
286         if (!PyArg_ParseTupleAndKeywords(
287                     args, kw, "O!", kwlist, &PyDict_Type, &py_data))
288                 return NULL;
289         
290         if (!py_to_printerdata(
291                     &keyname, &valuename, &value.type, &value.data_p, &value.size, py_data))
292                 return NULL;
293
294         fstrcpy(value.valuename,  valuename);
295
296         /* Call rpc function */
297
298         werror = rpccli_spoolss_setprinterdataex(
299                 hnd->cli, hnd->mem_ctx, &hnd->pol, keyname, &value);
300
301         if (!W_ERROR_IS_OK(werror)) {
302                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
303                 return NULL;
304         }
305
306         Py_INCREF(Py_None);
307         return Py_None;
308 }
309
310 PyObject *spoolss_hnd_enumprinterdataex(PyObject *self, PyObject *args, PyObject *kw)
311 {
312         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
313         static char *kwlist[] = { "key", NULL };
314         uint32 i;
315         char *key;
316         WERROR werror;
317         PyObject *result;
318         REGVAL_CTR ctr;
319
320         if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &key))
321                 return NULL;
322
323         /* Get max buffer sizes for value and data */
324
325         werror = rpccli_spoolss_enumprinterdataex(
326                 hnd->cli, hnd->mem_ctx, &hnd->pol, key, &ctr);
327
328         if (!W_ERROR_IS_OK(werror)) {
329                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
330                 return NULL;
331         }
332
333         /* Iterate over all printerdata */
334
335         result = PyDict_New();
336
337         for (i = 0; i < regval_ctr_numvals(&ctr); i++) {
338                 REGISTRY_VALUE *value;
339                 PyObject *item;
340
341                 item = PyDict_New();
342                 value = regval_ctr_specific_value(&ctr, i);
343
344                 if (py_from_printerdata(
345                             &item, key, value->valuename, value->type, 
346                             value->data_p, value->size))
347                         PyDict_SetItemString(result, value->valuename, item);
348         }
349         
350         return result;
351 }
352
353 PyObject *spoolss_hnd_deleteprinterdataex(PyObject *self, PyObject *args, PyObject *kw)
354 {
355         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
356         static char *kwlist[] = { "key", "value", NULL };
357         char *key, *value;
358         WERROR werror;
359
360         /* Parse parameters */
361
362         if (!PyArg_ParseTupleAndKeywords(args, kw, "ss", kwlist, &key, &value))
363                 return NULL;
364
365         /* Call rpc function */
366
367         werror = rpccli_spoolss_deleteprinterdataex(
368                 hnd->cli, hnd->mem_ctx, &hnd->pol, key, value);
369
370         if (!W_ERROR_IS_OK(werror)) {
371                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
372                 return NULL;
373         }
374
375         Py_INCREF(Py_None);
376         return Py_None;
377 }
378
379 PyObject *spoolss_hnd_enumprinterkey(PyObject *self, PyObject *args,
380                                      PyObject *kw)
381 {
382         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
383         static char *kwlist[] = { "key", NULL };
384         char *keyname;
385         WERROR werror;
386         uint32 keylist_len;
387         uint16 *keylist;
388         PyObject *result;
389
390         /* Parse parameters */
391
392         if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &keyname))
393                 return NULL;
394
395         /* Call rpc function */
396
397         werror = rpccli_spoolss_enumprinterkey(
398                 hnd->cli, hnd->mem_ctx, &hnd->pol, keyname, &keylist, 
399                 &keylist_len);
400
401         if (!W_ERROR_IS_OK(werror)) {
402                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
403                 return NULL;
404         }
405
406         result = from_unistr_list(keylist);
407
408         return result;
409 }
410
411 #if 0
412
413 PyObject *spoolss_hnd_deleteprinterkey(PyObject *self, PyObject *args,
414                                        PyObject *kw)
415 {
416         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
417         static char *kwlist[] = { "key", NULL };
418         char *keyname;
419         WERROR werror;
420
421         if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &keyname))
422                 return NULL;
423
424         Py_INCREF(Py_None);
425         return Py_None;
426 }
427
428 #endif