Imported Upstream version 3.21.91
[platform/upstream/python-gobject.git] / gi / 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, see <http://www.gnu.org/licenses/>.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #include <pyglib.h>
26 #include "pygoptioncontext.h"
27 #include "pygi-error.h"
28
29 PYGLIB_DEFINE_TYPE("gi._glib.OptionContext", PyGOptionContext_Type, PyGOptionContext)
30
31 /**
32  * pyg_option_context_new:
33  * @context: a GOptionContext
34  *
35  * Returns: A new GOptionContext wrapper.
36  */
37 PyObject *
38 pyg_option_context_new (GOptionContext *context)
39 {
40     PyGOptionContext *self;
41
42     self = (PyGOptionContext *)PyObject_NEW(PyGOptionContext, &PyGOptionContext_Type);
43     if (self == NULL)
44         return NULL;
45
46     self->context = context;
47     self->main_group = NULL;
48
49     return (PyObject *)self;
50 }
51
52 static int
53 pyg_option_context_init(PyGOptionContext *self,
54                         PyObject *args,
55                         PyObject *kwargs)
56 {
57     char *parameter_string;
58
59     if (!PyArg_ParseTuple(args, "s:gi._glib.GOptionContext.__init__",
60                           &parameter_string))
61         return -1;
62
63     self->context = g_option_context_new(parameter_string);
64     return 0;
65 }
66
67 static void
68 pyg_option_context_dealloc(PyGOptionContext *self)
69 {
70     Py_CLEAR(self->main_group);
71
72     if (self->context != NULL)
73     {
74         GOptionContext *tmp = self->context;
75         self->context = NULL;
76         g_option_context_free(tmp);
77     }
78
79     PyObject_Del(self);
80 }
81
82 static PyObject *
83 pyg_option_context_parse(PyGOptionContext *self,
84                          PyObject *args,
85                          PyObject *kwargs)
86 {
87     static char *kwlist[] = { "argv", NULL };
88     PyObject *arg;
89     PyObject *new_argv, *argv;
90     Py_ssize_t argv_length, pos;
91     gint argv_length_int;
92     char **argv_content, **original;
93     GError *error = NULL;
94     gboolean result;
95
96     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:GOptionContext.parse",
97                                      kwlist, &argv))
98         return NULL;
99
100     if (!PyList_Check(argv))
101     {
102         PyErr_SetString(PyExc_TypeError,
103                         "GOptionContext.parse expects a list of strings.");
104         return NULL;
105     }
106
107     argv_length = PyList_Size(argv);
108     if (argv_length == -1)
109     {
110         PyErr_SetString(PyExc_TypeError,
111                         "GOptionContext.parse expects a list of strings.");
112         return NULL;
113     }
114
115     argv_content = g_new(char*, argv_length + 1);
116     argv_content[argv_length] = NULL;
117     for (pos = 0; pos < argv_length; pos++)
118     {
119         arg = PyList_GetItem(argv, pos);
120         argv_content[pos] = g_strdup(PYGLIB_PyUnicode_AsString(arg));
121         if (argv_content[pos] == NULL)
122         {
123             g_strfreev(argv_content);
124             return NULL;
125         }
126     }
127     original = g_strdupv(argv_content);
128
129     g_assert(argv_length <= G_MAXINT);
130     argv_length_int = argv_length;
131     Py_BEGIN_ALLOW_THREADS;
132     result = g_option_context_parse(self->context, &argv_length_int, &argv_content,
133                                     &error);
134     Py_END_ALLOW_THREADS;
135     argv_length = argv_length_int;
136
137     if (!result)
138     {
139         g_strfreev(argv_content);
140         g_strfreev(original);
141         pygi_error_check(&error);
142         return NULL;
143     }
144
145     new_argv = PyList_New(g_strv_length(argv_content));
146     for (pos = 0; pos < argv_length; pos++)
147     {
148         arg = PYGLIB_PyUnicode_FromString(argv_content[pos]);
149         PyList_SetItem(new_argv, pos, arg);
150     }
151
152     g_strfreev(original);
153     g_strfreev(argv_content);
154     return new_argv;
155 }
156
157 static PyObject *
158 pyg_option_context_set_help_enabled(PyGOptionContext *self,
159                                     PyObject *args,
160                                     PyObject *kwargs)
161 {
162     static char *kwlist[] = { "help_enable", NULL };
163
164     PyObject *help_enabled;
165     if (! PyArg_ParseTupleAndKeywords(args, kwargs,
166                                       "O:GOptionContext.set_help_enabled",
167                                       kwlist, &help_enabled))
168         return NULL;
169
170     g_option_context_set_help_enabled(self->context, PyObject_IsTrue(help_enabled));
171
172     Py_INCREF(Py_None);
173     return Py_None;
174 }
175
176 static PyObject *
177 pyg_option_context_get_help_enabled(PyGOptionContext *self)
178 {
179     return PyBool_FromLong(g_option_context_get_help_enabled(self->context));
180 }
181
182 static PyObject *
183 pyg_option_context_set_ignore_unknown_options(PyGOptionContext *self,
184                                               PyObject *args,
185                                               PyObject *kwargs)
186 {
187     static char *kwlist[] = { "ignore_unknown_options", NULL };
188     PyObject *ignore_unknown_options;
189
190     if (! PyArg_ParseTupleAndKeywords(args, kwargs,
191                                 "O:GOptionContext.set_ignore_unknown_options",
192                                 kwlist, &ignore_unknown_options))
193         return NULL;
194
195     g_option_context_set_ignore_unknown_options(self->context,
196                                                 PyObject_IsTrue(ignore_unknown_options));
197     
198
199     Py_INCREF(Py_None);
200     return Py_None;
201 }
202
203 static PyObject *
204 pyg_option_context_get_ignore_unknown_options(PyGOptionContext *self)
205 {
206     return PyBool_FromLong(
207         g_option_context_get_ignore_unknown_options(self->context));
208 }
209
210 static PyObject *
211 pyg_option_context_set_main_group(PyGOptionContext *self,
212                                   PyObject *args,
213                                   PyObject *kwargs)
214 {
215     static char *kwlist[] = { "group", NULL };
216     GOptionGroup *g_group;
217     PyObject *group;
218
219     if (! PyArg_ParseTupleAndKeywords(args, kwargs,
220                                       "O:GOptionContext.set_main_group",
221                                       kwlist, &group))
222         return NULL;
223
224     if (PyObject_IsInstance(group, (PyObject*) &PyGOptionGroup_Type) != 1) {
225         PyErr_SetString(PyExc_TypeError,
226                         "GOptionContext.set_main_group expects a GOptionGroup.");
227         return NULL;
228     }
229
230     g_group = pyglib_option_group_transfer_group(group);
231     if (g_group == NULL)
232     {
233         PyErr_SetString(PyExc_RuntimeError,
234                         "Group is already in a OptionContext.");
235         return NULL;
236     }
237
238     g_option_context_set_main_group(self->context, g_group);
239
240     Py_INCREF(group);
241     self->main_group = (PyGOptionGroup*) group;
242
243     Py_INCREF(Py_None);
244     return Py_None;
245 }
246
247 static PyObject *
248 pyg_option_context_get_main_group(PyGOptionContext *self)
249 {
250     if (self->main_group == NULL)
251     {
252         Py_INCREF(Py_None);
253         return Py_None;
254     }
255     Py_INCREF(self->main_group);
256     return (PyObject*) self->main_group;
257 }
258
259 static PyObject *
260 pyg_option_context_add_group(PyGOptionContext *self,
261                              PyObject *args,
262                              PyObject *kwargs)
263 {
264     static char *kwlist[] = { "group", NULL };
265     GOptionGroup *g_group;
266     PyObject *group;
267
268     if (! PyArg_ParseTupleAndKeywords(args, kwargs,
269                                       "O:GOptionContext.add_group",
270                                       kwlist, &group))
271         return NULL;
272
273     if (PyObject_IsInstance(group, (PyObject*) &PyGOptionGroup_Type) != 1) {
274         PyErr_SetString(PyExc_TypeError,
275                         "GOptionContext.add_group expects a GOptionGroup.");
276         return NULL;
277     }
278
279     g_group = pyglib_option_group_transfer_group(group);
280     if (g_group == NULL)
281     {
282         PyErr_SetString(PyExc_RuntimeError,
283                         "Group is already in a OptionContext.");
284         return NULL;
285     }
286     Py_INCREF(group);
287
288     g_option_context_add_group(self->context, g_group);
289
290     Py_INCREF(Py_None);
291     return Py_None;
292 }
293
294 static PyObject*
295 pyg_option_context_richcompare(PyObject *self, PyObject *other, int op)
296 {
297     if (Py_TYPE(self) == Py_TYPE(other) && Py_TYPE(self) == &PyGOptionContext_Type)
298         return _pyglib_generic_ptr_richcompare(((PyGOptionContext*)self)->context,
299                                                ((PyGOptionContext*)other)->context,
300                                                op);
301     else {
302        Py_INCREF(Py_NotImplemented);
303        return Py_NotImplemented;
304     }
305 }
306
307 static PyObject *
308 pyg_option_get_context(PyGOptionContext *self)
309 {
310     return PYGLIB_CPointer_WrapPointer(self->context, "goption.context");
311 }
312
313 static PyMethodDef pyg_option_context_methods[] = {
314     { "parse", (PyCFunction)pyg_option_context_parse, METH_VARARGS | METH_KEYWORDS },
315     { "set_help_enabled", (PyCFunction)pyg_option_context_set_help_enabled, METH_VARARGS | METH_KEYWORDS },
316     { "get_help_enabled", (PyCFunction)pyg_option_context_get_help_enabled, METH_NOARGS },
317     { "set_ignore_unknown_options", (PyCFunction)pyg_option_context_set_ignore_unknown_options, METH_VARARGS | METH_KEYWORDS },
318     { "get_ignore_unknown_options", (PyCFunction)pyg_option_context_get_ignore_unknown_options, METH_NOARGS },
319     { "set_main_group", (PyCFunction)pyg_option_context_set_main_group, METH_VARARGS | METH_KEYWORDS },
320     { "get_main_group", (PyCFunction)pyg_option_context_get_main_group, METH_NOARGS },
321     { "add_group", (PyCFunction)pyg_option_context_add_group, METH_VARARGS | METH_KEYWORDS },
322     { "_get_context", (PyCFunction)pyg_option_get_context, METH_NOARGS },
323     { NULL, NULL, 0 },
324 };
325
326 void
327 pyglib_option_context_register_types(PyObject *d)
328 {
329     PyGOptionContext_Type.tp_dealloc = (destructor)pyg_option_context_dealloc;
330     PyGOptionContext_Type.tp_richcompare = pyg_option_context_richcompare;
331     PyGOptionContext_Type.tp_flags = Py_TPFLAGS_DEFAULT;
332     PyGOptionContext_Type.tp_methods = pyg_option_context_methods;
333     PyGOptionContext_Type.tp_init = (initproc)pyg_option_context_init;
334     PYGLIB_REGISTER_TYPE(d, PyGOptionContext_Type, "OptionContext");
335 }