Converted drivers, forms and ports functions to use new conversion routines
[samba.git] / source / python / py_spoolss_drivers.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 /* Enumerate printer drivers */
24
25 PyObject *spoolss_enumprinterdrivers(PyObject *self, PyObject *args,
26                                      PyObject *kw)
27 {
28         WERROR werror;
29         PyObject *result = Py_None, *creds = NULL;
30         PRINTER_DRIVER_CTR ctr;
31         int level = 1, i;
32         uint32 needed, num_drivers;
33         char *arch = "Windows NT x86", *server_name;
34         static char *kwlist[] = {"server", "level", "arch", "creds", NULL};
35         struct cli_state *cli = NULL;
36         TALLOC_CTX *mem_ctx = NULL;
37
38         /* Parse parameters */
39
40         if (!PyArg_ParseTupleAndKeywords(args, kw, "s|isO!", kwlist, 
41                                          &server_name, &level, &arch,
42                                          &PyDict_Type, &creds))
43                 return NULL;
44         
45         /* Call rpc function */
46         
47         if (!(cli = open_pipe_creds(server_name, creds, 
48                                     cli_spoolss_initialise, NULL))) {
49                 fprintf(stderr, "could not initialise cli state\n");
50                 goto done;
51         }
52
53         if (!(mem_ctx = talloc_init())) {
54                 fprintf(stderr, "unable to initialise talloc context\n");
55                 goto done;
56         }       
57
58         werror = cli_spoolss_enumprinterdrivers(
59                 cli, mem_ctx, 0, &needed, level, arch,
60                 &num_drivers, &ctr);
61
62         if (W_ERROR_V(werror) == ERRinsufficientbuffer)
63                 werror = cli_spoolss_enumprinterdrivers(
64                         cli, mem_ctx, needed, NULL, level, arch, 
65                         &num_drivers, &ctr);
66
67         if (!W_ERROR_IS_OK(werror)) {
68                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
69                 return NULL;
70         }
71
72         /* Return value */
73         
74         switch (level) {
75         case 1:
76                 result = PyList_New(num_drivers);
77                 
78                 for (i = 0; i < num_drivers; i++) {
79                         PyObject *value;
80                         
81                         py_from_DRIVER_INFO_1(&value, ctr.info1);
82                         PyList_SetItem(result, i, value);
83                 }
84                 
85                 break;
86         case 2: 
87                 result = PyList_New(num_drivers);
88
89                 for(i = 0; i < num_drivers; i++) {
90                         PyObject *value;
91
92                         py_from_DRIVER_INFO_2(&value, ctr.info2);
93                         PyList_SetItem(result, i, value);
94                 }
95
96                 break;
97         case 6: 
98                 result = PyList_New(num_drivers);
99
100                 for(i = 0; i < num_drivers; i++) {
101                         PyObject *value;
102
103                         py_from_DRIVER_INFO_6(&value, ctr.info6);
104                         PyList_SetItem(result, i, value);
105                 }
106
107                 break;
108         default:
109                 result = Py_None;
110                 break;
111         }
112         
113  done:
114         if (cli)
115                 cli_shutdown(cli);
116
117         if (mem_ctx)
118                 talloc_destroy(mem_ctx);
119
120         Py_INCREF(result);
121         return result;
122 }
123
124 /* Fetch printer driver */
125
126 PyObject *spoolss_getprinterdriver(PyObject *self, PyObject *args,
127                                    PyObject *kw)
128 {
129         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
130         WERROR werror;
131         PyObject *result = Py_None;
132         PRINTER_DRIVER_CTR ctr;
133         int level = 1;
134         uint32 needed;
135         char *arch = "Windows NT x86";
136         static char *kwlist[] = {"level", "arch", NULL};
137
138         /* Parse parameters */
139
140         if (!PyArg_ParseTupleAndKeywords(args, kw, "|is", kwlist, 
141                                          &level, &arch))
142                 return NULL;
143
144         /* Call rpc function */
145
146         werror = cli_spoolss_getprinterdriver(
147                 hnd->cli, hnd->mem_ctx, 0, &needed, &hnd->pol, level,
148                 arch, &ctr);
149
150         if (W_ERROR_V(werror) == ERRinsufficientbuffer)
151                 werror = cli_spoolss_getprinterdriver(
152                         hnd->cli, hnd->mem_ctx, needed, NULL, &hnd->pol,
153                         level, arch, &ctr);
154
155         if (!W_ERROR_IS_OK(werror)) {
156                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
157                 return NULL;
158         }
159
160         /* Return value */
161         
162         switch (level) {
163         case 1:
164                 py_from_DRIVER_INFO_1(&result, ctr.info1);
165                 break;
166         case 2: 
167                 py_from_DRIVER_INFO_2(&result, ctr.info2);
168                 break;
169         case 6:
170                 py_from_DRIVER_INFO_6(&result,  ctr.info6);
171                 break;
172         default:
173                 break;
174         }
175         
176         Py_INCREF(result);
177         return result;
178 }
179
180 /* Fetch printer driver directory */
181
182 PyObject *spoolss_getprinterdriverdir(PyObject *self, PyObject *args, 
183                                       PyObject *kw)
184 {
185         WERROR werror;
186         PyObject *result = Py_None, *creds = NULL;
187         DRIVER_DIRECTORY_CTR ctr;
188         uint32 needed, level;
189         char *arch = "Windows NT x86", *server_name;
190         static char *kwlist[] = {"server", "level", "arch", "creds", NULL};
191         struct cli_state *cli = NULL;
192         TALLOC_CTX *mem_ctx = NULL;
193
194         /* Parse parameters */
195
196         if (!PyArg_ParseTupleAndKeywords(args, kw, "s|isO!", kwlist, 
197                                          &server_name, &level, &arch,
198                                          &PyDict_Type, &creds))
199                 return NULL;
200
201         /* Call rpc function */
202
203         if (!(cli = open_pipe_creds(server_name, creds, 
204                                     cli_spoolss_initialise, NULL))) {
205                 fprintf(stderr, "could not initialise cli state\n");
206                 goto done;
207         }
208
209         if (!(mem_ctx = talloc_init())) {
210                 fprintf(stderr, "unable to initialise talloc context\n");
211                 goto done;
212         }       
213
214         werror = cli_spoolss_getprinterdriverdir(
215                 cli, mem_ctx, 0, &needed, level, arch, &ctr);
216
217         if (W_ERROR_V(werror) == ERRinsufficientbuffer)
218                 werror = cli_spoolss_getprinterdriverdir(
219                         cli, mem_ctx, needed, NULL, level, arch, &ctr);
220
221         if (!W_ERROR_IS_OK(werror)) {
222                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
223                 return NULL;
224         }
225
226         /* Return value */
227         
228         switch (level) {
229         case 1:
230                 py_from_DRIVER_DIRECTORY_1(&result, ctr.info1);
231                 break;
232         }
233         
234  done:
235         if (cli)
236                 cli_shutdown(cli);
237         
238         if (mem_ctx)
239                 talloc_destroy(mem_ctx);
240
241         Py_INCREF(result);
242         return result;
243 }