1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * pygobject - Python bindings for the GLib, GObject and GIO
3 * Copyright (C) 2006 Johannes Hoelzl
5 * pygoptiongroup.c: GOptionContext and GOptionGroup wrapper
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.
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.
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/>.
26 #include "pygoptiongroup.h"
27 #include "pygi-error.h"
29 PYGLIB_DEFINE_TYPE("gi._gi.OptionGroup", PyGOptionGroup_Type, PyGOptionGroup)
32 * pyg_option_group_new:
33 * @group: a GOptionGroup
35 * The returned GOptionGroup can't be used to set any hooks, translation domains
36 * or add entries. It's only intend is, to use for GOptionContext.add_group().
38 * Returns: the GOptionGroup wrapper.
41 pyg_option_group_new (GOptionGroup *group)
45 self = (PyGOptionGroup *)PyObject_NEW(PyGOptionGroup,
46 &PyGOptionGroup_Type);
51 self->other_owner = TRUE;
52 self->is_in_context = FALSE;
54 return (PyObject *)self;
58 check_if_owned(PyGOptionGroup *self)
60 if (self->other_owner)
62 PyErr_SetString(PyExc_ValueError, "The GOptionGroup was not created by "
63 "gi._gi.OptionGroup(), so operation is not possible.");
70 destroy_g_group(PyGOptionGroup *self)
72 PyGILState_STATE state;
73 state = PyGILState_Ensure();
76 Py_CLEAR(self->callback);
77 g_slist_foreach(self->strings, (GFunc) g_free, NULL);
78 g_slist_free(self->strings);
81 if (self->is_in_context)
86 PyGILState_Release(state);
90 pyg_option_group_init(PyGOptionGroup *self, PyObject *args, PyObject *kwargs)
92 static char *kwlist[] = { "name", "description", "help_description",
94 char *name, *description, *help_description;
97 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "zzzO:GOptionGroup.__init__",
98 kwlist, &name, &description,
99 &help_description, &callback))
102 self->group = g_option_group_new(name, description, help_description,
103 self, (GDestroyNotify) destroy_g_group);
104 self->other_owner = FALSE;
105 self->is_in_context = FALSE;
108 self->callback = callback;
114 pyg_option_group_dealloc(PyGOptionGroup *self)
116 if (!self->other_owner && !self->is_in_context)
118 GOptionGroup *tmp = self->group;
121 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
122 g_option_group_free(tmp);
123 G_GNUC_END_IGNORE_DEPRECATIONS
131 arg_func(const gchar *option_name,
133 PyGOptionGroup *self,
137 PyGILState_STATE state;
140 state = PyGILState_Ensure();
142 ret = PyObject_CallFunction(self->callback, "sOO",
143 option_name, Py_None, self);
145 ret = PyObject_CallFunction(self->callback, "ssO",
146 option_name, value, self);
153 no_error = pygi_gerror_exception_check(error) != -1;
155 PyGILState_Release(state);
160 pyg_option_group_add_entries(PyGOptionGroup *self, PyObject *args,
163 static char *kwlist[] = { "entries", NULL };
164 gssize entry_count, pos;
165 PyObject *entry_tuple, *list;
166 GOptionEntry *entries;
168 if (check_if_owned(self))
171 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:GOptionGroup.add_entries",
175 if (!PyList_Check(list))
177 PyErr_SetString(PyExc_TypeError,
178 "GOptionGroup.add_entries expected a list of entries");
182 entry_count = PyList_Size(list);
183 if (entry_count == -1)
185 PyErr_SetString(PyExc_TypeError,
186 "GOptionGroup.add_entries expected a list of entries");
190 entries = g_new0(GOptionEntry, entry_count + 1);
191 for (pos = 0; pos < entry_count; pos++)
193 gchar *long_name, *description, *arg_description;
194 entry_tuple = PyList_GetItem(list, pos);
195 if (!PyTuple_Check(entry_tuple))
197 PyErr_SetString(PyExc_TypeError, "GOptionGroup.add_entries "
198 "expected a list of entries");
202 if (!PyArg_ParseTuple(entry_tuple, "scisz",
204 &(entries[pos].short_name),
205 &(entries[pos].flags),
209 PyErr_SetString(PyExc_TypeError, "GOptionGroup.add_entries "
210 "expected a list of entries");
214 long_name = g_strdup(long_name);
215 self->strings = g_slist_prepend(self->strings, long_name);
216 entries[pos].long_name = long_name;
218 description = g_strdup(description);
219 self->strings = g_slist_prepend(self->strings, description);
220 entries[pos].description = description;
222 arg_description = g_strdup(arg_description);
223 self->strings = g_slist_prepend(self->strings, arg_description);
224 entries[pos].arg_description = arg_description;
226 entries[pos].arg = G_OPTION_ARG_CALLBACK;
227 entries[pos].arg_data = arg_func;
230 g_option_group_add_entries(self->group, entries);
240 pyg_option_group_set_translation_domain(PyGOptionGroup *self,
244 static char *kwlist[] = { "domain", NULL };
247 if (check_if_owned(self))
250 if (self->group == NULL)
252 PyErr_SetString(PyExc_RuntimeError,
253 "The corresponding GOptionGroup was already freed, "
254 "probably through the release of GOptionContext");
258 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
259 "z:GOptionGroup.set_translate_domain",
263 g_option_group_set_translation_domain(self->group, domain);
270 pyg_option_group_richcompare(PyObject *self, PyObject *other, int op)
272 if (Py_TYPE(self) == Py_TYPE(other) &&
273 Py_TYPE(self) == &PyGOptionGroup_Type) {
274 return _pyglib_generic_ptr_richcompare(((PyGOptionGroup*)self)->group,
275 ((PyGOptionGroup*)other)->group,
278 Py_INCREF(Py_NotImplemented);
279 return Py_NotImplemented;
283 static PyMethodDef pyg_option_group_methods[] = {
284 { "add_entries", (PyCFunction)pyg_option_group_add_entries, METH_VARARGS | METH_KEYWORDS },
285 { "set_translation_domain", (PyCFunction)pyg_option_group_set_translation_domain, METH_VARARGS | METH_KEYWORDS },
290 pyglib_option_group_register_types(PyObject *d)
292 PyGOptionGroup_Type.tp_dealloc = (destructor)pyg_option_group_dealloc;
293 PyGOptionGroup_Type.tp_richcompare = pyg_option_group_richcompare;
294 PyGOptionGroup_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
295 PyGOptionGroup_Type.tp_methods = pyg_option_group_methods;
296 PyGOptionGroup_Type.tp_init = (initproc)pyg_option_group_init;
297 PYGLIB_REGISTER_TYPE(d, PyGOptionGroup_Type, "OptionGroup");