Added getprinterdata and setprinterdata functions.
[samba.git] / 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
23 static BOOL py_from_printerdata(PyObject **dict, char *value,
24                                 uint32 data_type, char *data, 
25                                 uint32 data_size) 
26 {
27         *dict = PyDict_New();
28
29         PyDict_SetItemString(*dict, "type", Py_BuildValue("i", data_type));
30         PyDict_SetItemString(*dict, "value", Py_BuildValue("s", value));
31
32         PyDict_SetItemString(*dict, "data", 
33                              Py_BuildValue("s#", data, data_size));
34
35         return True;
36 }
37
38 static BOOL py_to_printerdata(char **value, uint32 *data_type, 
39                               char **data, uint32 *data_size, 
40                               PyObject *dict)
41 {
42         PyObject *obj;
43
44         if ((obj = PyDict_GetItemString(dict, "type"))) {
45
46                 if (!PyInt_Check(obj)) {
47                         PyErr_SetString(spoolss_error,
48                                         "type not an integer");
49                         return False;
50                 }
51
52                 *data_type = PyInt_AsLong(obj);
53         } else {
54                 PyErr_SetString(spoolss_error, "no type present");
55                 return False;
56         }
57
58         if ((obj = PyDict_GetItemString(dict, "value"))) {
59
60                 if (!PyString_Check(obj)) {
61                         PyErr_SetString(spoolss_error,
62                                         "value not a string");
63                         return False;
64                 }
65
66                 *value = PyString_AsString(obj);
67         } else {
68                 PyErr_SetString(spoolss_error, "no value present");
69                 return False;
70         }
71
72         if ((obj = PyDict_GetItemString(dict, "data"))) {
73
74                 if (!PyString_Check(obj)) {
75                         PyErr_SetString(spoolss_error,
76                                         "data not a string");
77                         return False;
78                 }
79
80                 *data = PyString_AsString(obj);
81                 *data_size = PyString_Size(obj);
82         } else {
83                 PyErr_SetString(spoolss_error, "no data present");
84                 return False;
85         }
86
87         return True;
88 }
89
90 PyObject *spoolss_getprinterdata(PyObject *self, PyObject *args, PyObject *kw)
91 {
92         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
93         static char *kwlist[] = { "value", NULL };
94         char *value;
95         WERROR werror;
96         uint32 needed, data_type, data_size;
97         char *data;
98         PyObject *result;
99
100         /* Parse parameters */
101
102         if (!PyArg_ParseTupleAndKeywords(args, kw, "s", kwlist, &value))
103             return NULL;
104
105         /* Call rpc function */
106
107         werror = cli_spoolss_getprinterdata(
108                 hnd->cli, hnd->mem_ctx, 0, &needed, &hnd->pol, value,
109                 &data_type, &data, &data_size);
110
111         if (W_ERROR_V(werror) == ERRmoredata) 
112                 werror = cli_spoolss_getprinterdata(
113                         hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol, value,
114                         &data_type, &data, &data_size);
115
116         if (!W_ERROR_IS_OK(werror)) {
117                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
118                 return NULL;
119         }
120
121         py_from_printerdata(&result, value, data_type, data, needed);
122
123         return result;
124 }
125
126 PyObject *spoolss_setprinterdata(PyObject *self, PyObject *args, PyObject *kw)
127 {
128         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
129         static char *kwlist[] = { "data", NULL };
130         PyObject *py_data;
131         char *value, *data;
132         uint32 data_size, data_type;
133         WERROR werror;
134
135         if (!PyArg_ParseTupleAndKeywords(args, kw, "O!", kwlist,
136                                          &PyDict_Type, &py_data))
137                 return NULL;
138         
139         if (!py_to_printerdata(&value, &data_type, &data, &data_size, py_data))
140                 return NULL;
141
142         /* Call rpc function */
143
144         werror = cli_spoolss_setprinterdata(
145                 hnd->cli, hnd->mem_ctx, &hnd->pol, value, data_type,
146                 data, data_size);
147
148         if (!W_ERROR_IS_OK(werror)) {
149                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
150                 return NULL;
151         }
152
153         Py_INCREF(Py_None);
154         return Py_None;
155 }
156
157 PyObject *spoolss_enumprinterdata(PyObject *self, PyObject *args, PyObject *kw)
158 {
159         return NULL;
160 }