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/>.
23 #include "pygoptiongroup.h"
24 #include "pygi-error.h"
25 #include "pygi-util.h"
27 PYGLIB_DEFINE_TYPE("gi._gi.OptionGroup", PyGOptionGroup_Type
, PyGOptionGroup
)
30 * pyg_option_group_new:
31 * @group: a GOptionGroup
33 * The returned GOptionGroup can't be used to set any hooks, translation domains
34 * or add entries. It's only intend is, to use for GOptionContext.add_group().
36 * Returns: the GOptionGroup wrapper.
39 pyg_option_group_new (GOptionGroup
*group
)
43 self
= (PyGOptionGroup
*)PyObject_NEW(PyGOptionGroup
,
44 &PyGOptionGroup_Type
);
49 self
->other_owner
= TRUE
;
50 self
->is_in_context
= FALSE
;
52 return (PyObject
*)self
;
56 check_if_owned(PyGOptionGroup
*self
)
58 if (self
->other_owner
)
60 PyErr_SetString(PyExc_ValueError
, "The GOptionGroup was not created by "
61 "gi._gi.OptionGroup(), so operation is not possible.");
68 destroy_g_group(PyGOptionGroup
*self
)
70 PyGILState_STATE state
;
71 state
= PyGILState_Ensure();
74 Py_CLEAR(self
->callback
);
75 g_slist_foreach(self
->strings
, (GFunc
) g_free
, NULL
);
76 g_slist_free(self
->strings
);
79 if (self
->is_in_context
)
84 PyGILState_Release(state
);
88 pyg_option_group_init(PyGOptionGroup
*self
, PyObject
*args
, PyObject
*kwargs
)
90 static char *kwlist
[] = { "name", "description", "help_description",
92 char *name
, *description
, *help_description
;
95 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "zzzO:GOptionGroup.__init__",
96 kwlist
, &name
, &description
,
97 &help_description
, &callback
))
100 self
->group
= g_option_group_new(name
, description
, help_description
,
101 self
, (GDestroyNotify
) destroy_g_group
);
102 self
->other_owner
= FALSE
;
103 self
->is_in_context
= FALSE
;
106 self
->callback
= callback
;
112 pyg_option_group_dealloc(PyGOptionGroup
*self
)
114 if (!self
->other_owner
&& !self
->is_in_context
)
116 GOptionGroup
*tmp
= self
->group
;
119 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
120 g_option_group_free(tmp
);
121 G_GNUC_END_IGNORE_DEPRECATIONS
129 arg_func(const gchar
*option_name
,
131 PyGOptionGroup
*self
,
135 PyGILState_STATE state
;
138 state
= PyGILState_Ensure();
140 ret
= PyObject_CallFunction(self
->callback
, "sOO",
141 option_name
, Py_None
, self
);
143 ret
= PyObject_CallFunction(self
->callback
, "ssO",
144 option_name
, value
, self
);
151 no_error
= pygi_gerror_exception_check(error
) != -1;
153 PyGILState_Release(state
);
158 pyg_option_group_add_entries(PyGOptionGroup
*self
, PyObject
*args
,
161 static char *kwlist
[] = { "entries", NULL
};
162 gssize entry_count
, pos
;
163 PyObject
*entry_tuple
, *list
;
164 GOptionEntry
*entries
;
166 if (check_if_owned(self
))
169 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "O:GOptionGroup.add_entries",
173 if (!PyList_Check(list
))
175 PyErr_SetString(PyExc_TypeError
,
176 "GOptionGroup.add_entries expected a list of entries");
180 entry_count
= PyList_Size(list
);
181 if (entry_count
== -1)
183 PyErr_SetString(PyExc_TypeError
,
184 "GOptionGroup.add_entries expected a list of entries");
188 entries
= g_new0(GOptionEntry
, entry_count
+ 1);
189 for (pos
= 0; pos
< entry_count
; pos
++)
191 gchar
*long_name
, *description
, *arg_description
;
192 entry_tuple
= PyList_GetItem(list
, pos
);
193 if (!PyTuple_Check(entry_tuple
))
195 PyErr_SetString(PyExc_TypeError
, "GOptionGroup.add_entries "
196 "expected a list of entries");
200 if (!PyArg_ParseTuple(entry_tuple
, "scisz",
202 &(entries
[pos
].short_name
),
203 &(entries
[pos
].flags
),
207 PyErr_SetString(PyExc_TypeError
, "GOptionGroup.add_entries "
208 "expected a list of entries");
212 long_name
= g_strdup(long_name
);
213 self
->strings
= g_slist_prepend(self
->strings
, long_name
);
214 entries
[pos
].long_name
= long_name
;
216 description
= g_strdup(description
);
217 self
->strings
= g_slist_prepend(self
->strings
, description
);
218 entries
[pos
].description
= description
;
220 arg_description
= g_strdup(arg_description
);
221 self
->strings
= g_slist_prepend(self
->strings
, arg_description
);
222 entries
[pos
].arg_description
= arg_description
;
224 entries
[pos
].arg
= G_OPTION_ARG_CALLBACK
;
225 entries
[pos
].arg_data
= arg_func
;
228 g_option_group_add_entries(self
->group
, entries
);
238 pyg_option_group_set_translation_domain(PyGOptionGroup
*self
,
242 static char *kwlist
[] = { "domain", NULL
};
245 if (check_if_owned(self
))
248 if (self
->group
== NULL
)
250 PyErr_SetString(PyExc_RuntimeError
,
251 "The corresponding GOptionGroup was already freed, "
252 "probably through the release of GOptionContext");
256 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
,
257 "z:GOptionGroup.set_translate_domain",
261 g_option_group_set_translation_domain(self
->group
, domain
);
268 pyg_option_group_richcompare(PyObject
*self
, PyObject
*other
, int op
)
270 if (Py_TYPE(self
) == Py_TYPE(other
) &&
271 Py_TYPE(self
) == &PyGOptionGroup_Type
) {
272 return pyg_ptr_richcompare(((PyGOptionGroup
*)self
)->group
,
273 ((PyGOptionGroup
*)other
)->group
,
276 Py_INCREF(Py_NotImplemented
);
277 return Py_NotImplemented
;
281 static PyMethodDef pyg_option_group_methods
[] = {
282 { "add_entries", (PyCFunction
)pyg_option_group_add_entries
, METH_VARARGS
| METH_KEYWORDS
},
283 { "set_translation_domain", (PyCFunction
)pyg_option_group_set_translation_domain
, METH_VARARGS
| METH_KEYWORDS
},
288 * Returns 0 on success, or -1 and sets an exception.
291 pygi_option_group_register_types(PyObject
*d
)
293 PyGOptionGroup_Type
.tp_dealloc
= (destructor
)pyg_option_group_dealloc
;
294 PyGOptionGroup_Type
.tp_richcompare
= pyg_option_group_richcompare
;
295 PyGOptionGroup_Type
.tp_flags
= Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
;
296 PyGOptionGroup_Type
.tp_methods
= pyg_option_group_methods
;
297 PyGOptionGroup_Type
.tp_init
= (initproc
)pyg_option_group_init
;
298 PYGLIB_REGISTER_TYPE(d
, PyGOptionGroup_Type
, "OptionGroup");