Imported Upstream version 2.28.6
[platform/upstream/pygobject2.git] / glib / pygoptioncontext.c
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2  * pygtk- Python bindings for the GTK toolkit.
3  * Copyright (C) 2006  Johannes Hoelzl
4  *
5  *   pygoptioncontext.c: GOptionContext wrapper
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20  * USA
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #include <pyglib.h>
28 #include "pyglib-private.h"
29 #include "pygoptioncontext.h"
30
31 PYGLIB_DEFINE_TYPE("glib.OptionContext", PyGOptionContext_Type, PyGOptionContext)
32
33 /**
34  * pyg_option_context_new:
35  * @context: a GOptionContext
36  *
37  * Returns: A new GOptionContext wrapper.
38  */
39 PyObject *
40 pyg_option_context_new (GOptionContext *context)
41 {
42     PyGOptionContext *self;
43
44     self = (PyGOptionContext *)PyObject_NEW(PyGOptionContext, &PyGOptionContext_Type);
45     if (self == NULL)
46         return NULL;
47
48     self->context = context;
49     self->main_group = NULL;
50
51     return (PyObject *)self;
52 }
53
54 static int
55 pyg_option_context_init(PyGOptionContext *self,
56                         PyObject *args,
57                         PyObject *kwargs)
58 {
59     char *parameter_string;
60
61     if (!PyArg_ParseTuple(args, "s:glib.GOptionContext.__init__",
62                           &parameter_string))
63         return -1;
64
65     self->context = g_option_context_new(parameter_string);
66     return 0;
67 }
68
69 static void
70 pyg_option_context_dealloc(PyGOptionContext *self)
71 {
72     Py_CLEAR(self->main_group);
73
74     if (self->context != NULL)
75     {
76         GOptionContext *tmp = self->context;
77         self->context = NULL;
78         g_option_context_free(tmp);
79     }
80
81     PyObject_Del(self);
82 }
83
84 static PyObject *
85 pyg_option_context_parse(PyGOptionContext *self,
86                          PyObject *args,
87                          PyObject *kwargs)
88 {
89     static char *kwlist[] = { "argv", NULL };
90     PyObject *arg;
91     PyObject *new_argv, *argv;
92     Py_ssize_t argv_length, pos;
93     gint argv_length_int;
94     char **argv_content, **original;
95     GError *error = NULL;
96     gboolean result;
97
98     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:GOptionContext.parse",
99                                      kwlist, &argv))
100         return NULL;
101
102     if (!PyList_Check(argv))
103     {
104         PyErr_SetString(PyExc_TypeError,
105                         "GOptionContext.parse expects a list of strings.");
106         return NULL;
107     }
108
109     argv_length = PyList_Size(argv);
110     if (argv_length == -1)
111     {
112         PyErr_SetString(PyExc_TypeError,
113                         "GOptionContext.parse expects a list of strings.");
114         return NULL;
115     }
116
117     argv_content = g_new(char*, argv_length + 1);
118     argv_content[argv_length] = NULL;
119     for (pos = 0; pos < argv_length; pos++)
120     {
121         arg = PyList_GetItem(argv, pos);
122         argv_content[pos] = g_strdup(PYGLIB_PyUnicode_AsString(arg));
123         if (argv_content[pos] == NULL)
124         {
125             g_strfreev(argv_content);
126             return NULL;
127         }
128     }
129     original = g_strdupv(argv_content);
130
131     g_assert(argv_length <= G_MAXINT);
132     argv_length_int = argv_length;
133     pyglib_begin_allow_threads;
134     result = g_option_context_parse(self->context, &argv_length_int, &argv_content,
135                                     &error);
136     pyglib_end_allow_threads;
137     argv_length = argv_length_int;
138
139     if (!result)
140     {
141         g_strfreev(argv_content);
142         g_strfreev(original);
143         pyglib_error_check(&error);
144         return NULL;
145     }
146
147     new_argv = PyList_New(g_strv_length(argv_content));
148     for (pos = 0; pos < argv_length; pos++)
149     {
150         arg = PYGLIB_PyUnicode_FromString(argv_content[pos]);
151         PyList_SetItem(new_argv, pos, arg);
152     }
153
154     g_strfreev(original);
155     g_strfreev(argv_content);
156     return new_argv;
157 }
158
159 static PyObject *
160 pyg_option_context_set_help_enabled(PyGOptionContext *self,
161                                     PyObject *args,
162                                     PyObject *kwargs)
163 {
164     static char *kwlist[] = { "help_enable", NULL };
165
166     PyObject *help_enabled;
167     if (! PyArg_ParseTupleAndKeywords(args, kwargs,
168                                       "O:GOptionContext.set_help_enabled",
169                                       kwlist, &help_enabled))
170         return NULL;
171
172     g_option_context_set_help_enabled(self->context, PyObject_IsTrue(help_enabled));
173
174     Py_INCREF(Py_None);
175     return Py_None;
176 }
177
178 static PyObject *
179 pyg_option_context_get_help_enabled(PyGOptionContext *self)
180 {
181     return PyBool_FromLong(g_option_context_get_help_enabled(self->context));
182 }
183
184 static PyObject *
185 pyg_option_context_set_ignore_unknown_options(PyGOptionContext *self,
186                                               PyObject *args,
187                                               PyObject *kwargs)
188 {
189     static char *kwlist[] = { "ignore_unknown_options", NULL };
190     PyObject *ignore_unknown_options;
191
192     if (! PyArg_ParseTupleAndKeywords(args, kwargs,
193                                 "O:GOptionContext.set_ignore_unknown_options",
194                                 kwlist, &ignore_unknown_options))
195         return NULL;
196
197     g_option_context_set_ignore_unknown_options(self->context,
198                                                 PyObject_IsTrue(ignore_unknown_options));
199     
200
201     Py_INCREF(Py_None);
202     return Py_None;
203 }
204
205 static PyObject *
206 pyg_option_context_get_ignore_unknown_options(PyGOptionContext *self)
207 {
208     return PyBool_FromLong(
209         g_option_context_get_ignore_unknown_options(self->context));
210 }
211
212 static PyObject *
213 pyg_option_context_set_main_group(PyGOptionContext *self,
214                                   PyObject *args,
215                                   PyObject *kwargs)
216 {
217     static char *kwlist[] = { "group", NULL };
218     GOptionGroup *g_group;
219     PyObject *group;
220
221     if (! PyArg_ParseTupleAndKeywords(args, kwargs,
222                                       "O:GOptionContext.set_main_group",
223                                       kwlist, &group))
224         return NULL;
225
226     if (PyObject_IsInstance(group, (PyObject*) &PyGOptionGroup_Type) != 1) {
227         PyErr_SetString(PyExc_TypeError,
228                         "GOptionContext.set_main_group expects a GOptionGroup.");
229         return NULL;
230     }
231
232     g_group = pyglib_option_group_transfer_group(group);
233     if (g_group == NULL)
234     {
235         PyErr_SetString(PyExc_RuntimeError,
236                         "Group is already in a OptionContext.");
237         return NULL;
238     }
239
240     g_option_context_set_main_group(self->context, g_group);
241
242     Py_INCREF(group);
243     self->main_group = (PyGOptionGroup*) group;
244
245     Py_INCREF(Py_None);
246     return Py_None;
247 }
248
249 static PyObject *
250 pyg_option_context_get_main_group(PyGOptionContext *self)
251 {
252     if (self->main_group == NULL)
253     {
254         Py_INCREF(Py_None);
255         return Py_None;
256     }
257     Py_INCREF(self->main_group);
258     return (PyObject*) self->main_group;
259 }
260
261 static PyObject *
262 pyg_option_context_add_group(PyGOptionContext *self,
263                              PyObject *args,
264                              PyObject *kwargs)
265 {
266     static char *kwlist[] = { "group", NULL };
267     GOptionGroup *g_group;
268     PyObject *group;
269
270     if (! PyArg_ParseTupleAndKeywords(args, kwargs,
271                                       "O:GOptionContext.add_group",
272                                       kwlist, &group))
273         return NULL;
274
275     if (PyObject_IsInstance(group, (PyObject*) &PyGOptionGroup_Type) != 1) {
276         PyErr_SetString(PyExc_TypeError,
277                         "GOptionContext.add_group expects a GOptionGroup.");
278         return NULL;
279     }
280
281     g_group = pyglib_option_group_transfer_group(group);
282     if (g_group == NULL)
283     {
284         PyErr_SetString(PyExc_RuntimeError,
285                         "Group is already in a OptionContext.");
286         return NULL;
287     }
288     Py_INCREF(group);
289
290     g_option_context_add_group(self->context, g_group);
291
292     Py_INCREF(Py_None);
293     return Py_None;
294 }
295
296 static PyObject*
297 pyg_option_context_richcompare(PyObject *self, PyObject *other, int op)
298 {
299     if (Py_TYPE(self) == Py_TYPE(other) && Py_TYPE(self) == &PyGOptionContext_Type)
300         return _pyglib_generic_ptr_richcompare(((PyGOptionContext*)self)->context,
301                                                ((PyGOptionContext*)other)->context,
302                                                op);
303     else {
304        Py_INCREF(Py_NotImplemented);
305        return Py_NotImplemented;
306     }
307 }
308
309 static PyObject *
310 pyg_option_get_context(PyGOptionContext *self)
311 {
312     return PYGLIB_CPointer_WrapPointer(self->context, "goption.context");
313 }
314
315 static PyMethodDef pyg_option_context_methods[] = {
316     { "parse", (PyCFunction)pyg_option_context_parse, METH_VARARGS | METH_KEYWORDS },
317     { "set_help_enabled", (PyCFunction)pyg_option_context_set_help_enabled, METH_VARARGS | METH_KEYWORDS },
318     { "get_help_enabled", (PyCFunction)pyg_option_context_get_help_enabled, METH_NOARGS },
319     { "set_ignore_unknown_options", (PyCFunction)pyg_option_context_set_ignore_unknown_options, METH_VARARGS | METH_KEYWORDS },
320     { "get_ignore_unknown_options", (PyCFunction)pyg_option_context_get_ignore_unknown_options, METH_NOARGS },
321     { "set_main_group", (PyCFunction)pyg_option_context_set_main_group, METH_VARARGS | METH_KEYWORDS },
322     { "get_main_group", (PyCFunction)pyg_option_context_get_main_group, METH_NOARGS },
323     { "add_group", (PyCFunction)pyg_option_context_add_group, METH_VARARGS | METH_KEYWORDS },
324     { "_get_context", (PyCFunction)pyg_option_get_context, METH_NOARGS },
325     { NULL, NULL, 0 },
326 };
327
328 void
329 pyglib_option_context_register_types(PyObject *d)
330 {
331     PyGOptionContext_Type.tp_dealloc = (destructor)pyg_option_context_dealloc;
332     PyGOptionContext_Type.tp_richcompare = pyg_option_context_richcompare;
333     PyGOptionContext_Type.tp_flags = Py_TPFLAGS_DEFAULT;
334     PyGOptionContext_Type.tp_methods = pyg_option_context_methods;
335     PyGOptionContext_Type.tp_init = (initproc)pyg_option_context_init;
336     PYGLIB_REGISTER_TYPE(d, PyGOptionContext_Type, "OptionContext");
337 }