udapted vi.po
[rhythmbox.git] / bindings / python / override_common.c
blob37fe197eb95d8b54716f613f5259455ce0bc1a8b
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3 * Utility functions for Python bindings.
4 * Stolen from Epiphany.
6 * Copyright (C) 2005 Adam Hooper <adamh@cvs.gnome.org>
7 * Copyright (C) 2005 Christian Persch <chpe@cvs.gnome.org>
8 * Copyright (C) 2005 Crispin Flowerday <gnome@flowerday.cx>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
13 * any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26 #define NO_IMPORT_PYGOBJECT
27 #include "pygobject.h"
28 #include <pygtk/pygtk.h>
30 #include "override_common.h"
32 PyObject *
33 _helper_wrap_gobject_glist (const GList *list)
35 PyObject *py_list;
36 const GList *tmp;
38 if ((py_list = PyList_New(0)) == NULL) {
39 return NULL;
41 for (tmp = list; tmp != NULL; tmp = tmp->next) {
42 PyObject *py_obj = pygobject_new(G_OBJECT(tmp->data));
44 if (py_obj == NULL) {
45 Py_DECREF(py_list);
46 return NULL;
48 PyList_Append(py_list, py_obj);
49 Py_DECREF(py_obj);
51 return py_list;
54 PyObject *
55 _helper_wrap_pointer_glist (const GList *list, GType boxed_type)
57 PyObject *py_list;
58 const GList *tmp;
60 if ((py_list = PyList_New(0)) == NULL) {
61 return NULL;
63 for (tmp = list; tmp != NULL; tmp = tmp->next) {
64 PyObject *py_obj = pyg_pointer_new(boxed_type, tmp->data);
66 if (py_obj == NULL) {
67 Py_DECREF(py_list);
68 return NULL;
70 PyList_Append(py_list, py_obj);
71 Py_DECREF(py_obj);
73 return py_list;
76 PyObject *
77 _helper_wrap_boxed_glist (const GList *list,
78 GType boxed_type,
79 gboolean copy_boxed,
80 gboolean own_ref)
82 PyObject *py_list;
83 const GList *tmp;
85 if ((py_list = PyList_New(0)) == NULL) {
86 return NULL;
88 for (tmp = list; tmp != NULL; tmp = tmp->next) {
89 PyObject *py_obj = pyg_boxed_new(boxed_type, G_OBJECT(tmp->data), copy_boxed, own_ref);
91 if (py_obj == NULL) {
92 Py_DECREF(py_list);
93 return NULL;
95 PyList_Append(py_list, py_obj);
96 Py_DECREF(py_obj);
98 return py_list;
101 PyObject *
102 _helper_wrap_string_glist (const GList *list)
104 const GList *tmp;
105 PyObject *py_list;
107 if ((py_list = PyList_New(0)) == NULL) {
108 return NULL;
110 for (tmp = list; tmp != NULL; tmp = tmp->next) {
111 PyObject *str_obj = PyString_FromString ((char*)tmp->data);
113 if (str_obj == NULL) {
114 Py_DECREF(py_list);
115 return NULL;
117 PyList_Append(py_list, str_obj);
118 Py_DECREF(str_obj);
120 return py_list;
123 PyObject *
124 _helper_wrap_boxed_gptrarray (GPtrArray *list, GType type, gboolean own_ref, gboolean dealloc)
126 PyObject *py_list;
127 int i;
129 if ((py_list = PyList_New(0)) == NULL) {
130 return NULL;
132 for( i = 0; i < list->len; i++ ) {
133 PyObject *obj = pyg_boxed_new (type, g_ptr_array_index(list,i), FALSE, own_ref);
134 PyList_Append(py_list, obj);
135 Py_DECREF(obj);
137 if (dealloc) g_ptr_array_free (list, TRUE);
138 return py_list;
141 GList *
142 _helper_unwrap_pointer_pylist (PyObject *py_list, GType type)
144 int size, i;
145 GList *list = NULL;
147 size = PyList_Size (py_list);
148 for (i = 0; i < size; i++) {
149 PyObject *py_ptr;
150 gpointer ptr;
152 py_ptr = PyList_GetItem (py_list, i);
153 if (!pyg_pointer_check (py_ptr, type)) {
154 g_list_free (list);
155 return NULL;
157 ptr = pyg_pointer_get (py_ptr, void);
158 list = g_list_prepend (list, ptr);
161 list = g_list_reverse (list);
162 return list;
165 GList *
166 _helper_unwrap_string_pylist (PyObject *py_list)
168 int size, i;
169 GList *list = NULL;
171 size = PyList_Size (py_list);
172 for (i = 0; i < size; i++) {
173 PyObject *py_str;
174 char *str;
176 py_str = PyList_GetItem (py_list, i);
177 str = PyString_AsString (py_str);
178 list = g_list_prepend (list, str);
181 list = g_list_reverse (list);
182 return list;