~ubuntu-branches/ubuntu/precise/rhythmbox/precise-201203091205

« back to all changes in this revision

Viewing changes to bindings/python/override_common.c

Tags: upstream-0.9.5
ImportĀ upstreamĀ versionĀ 0.9.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 
2
 *
 
3
 * Utility functions for Python bindings.
 
4
 * Stolen from Epiphany.
 
5
 *
 
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>
 
9
 *
 
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.
 
14
 *
 
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.
 
19
 *
 
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.
 
23
 *
 
24
 */
 
25
 
 
26
#define NO_IMPORT_PYGOBJECT
 
27
#include "pygobject.h"
 
28
#include <pygtk/pygtk.h>
 
29
 
 
30
#include "override_common.h"
 
31
 
 
32
PyObject *
 
33
_helper_wrap_gobject_glist (const GList *list)
 
34
{
 
35
    PyObject *py_list;
 
36
    const GList *tmp;
 
37
 
 
38
    if ((py_list = PyList_New(0)) == NULL) {
 
39
        return NULL;
 
40
    }
 
41
    for (tmp = list; tmp != NULL; tmp = tmp->next) {
 
42
        PyObject *py_obj = pygobject_new(G_OBJECT(tmp->data));
 
43
 
 
44
        if (py_obj == NULL) {
 
45
            Py_DECREF(py_list);
 
46
            return NULL;
 
47
        }
 
48
        PyList_Append(py_list, py_obj);
 
49
        Py_DECREF(py_obj);
 
50
    }
 
51
    return py_list;
 
52
}
 
53
 
 
54
PyObject *
 
55
_helper_wrap_pointer_glist (const GList *list, GType boxed_type)
 
56
{
 
57
    PyObject *py_list;
 
58
    const GList *tmp;
 
59
 
 
60
    if ((py_list = PyList_New(0)) == NULL) {
 
61
        return NULL;
 
62
    }
 
63
    for (tmp = list; tmp != NULL; tmp = tmp->next) {
 
64
        PyObject *py_obj = pyg_pointer_new(boxed_type, tmp->data);
 
65
 
 
66
        if (py_obj == NULL) {
 
67
            Py_DECREF(py_list);
 
68
            return NULL;
 
69
        }
 
70
        PyList_Append(py_list, py_obj);
 
71
        Py_DECREF(py_obj);
 
72
    }
 
73
    return py_list;
 
74
}
 
75
 
 
76
PyObject *
 
77
_helper_wrap_boxed_glist (const GList *list,
 
78
                          GType boxed_type,
 
79
                          gboolean copy_boxed,
 
80
                          gboolean own_ref)
 
81
{
 
82
    PyObject *py_list;
 
83
    const GList *tmp;
 
84
 
 
85
    if ((py_list = PyList_New(0)) == NULL) {
 
86
        return NULL;
 
87
    }
 
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);
 
90
 
 
91
        if (py_obj == NULL) {
 
92
            Py_DECREF(py_list);
 
93
            return NULL;
 
94
        }
 
95
        PyList_Append(py_list, py_obj);
 
96
        Py_DECREF(py_obj);
 
97
    }
 
98
    return py_list;
 
99
}
 
100
 
 
101
PyObject *
 
102
_helper_wrap_string_glist (const GList *list)
 
103
{
 
104
    const GList *tmp;
 
105
    PyObject *py_list;
 
106
 
 
107
    if ((py_list = PyList_New(0)) == NULL) {
 
108
        return NULL;
 
109
    }
 
110
    for (tmp = list; tmp != NULL; tmp = tmp->next) {
 
111
        PyObject *str_obj =  PyString_FromString ((char*)tmp->data);
 
112
 
 
113
        if (str_obj == NULL) {
 
114
            Py_DECREF(py_list);
 
115
            return NULL;
 
116
        }
 
117
        PyList_Append(py_list, str_obj);
 
118
        Py_DECREF(str_obj);
 
119
    }
 
120
    return py_list;
 
121
}
 
122
 
 
123
PyObject *
 
124
_helper_wrap_boxed_gptrarray (GPtrArray *list, GType type, gboolean own_ref, gboolean dealloc)
 
125
{
 
126
    PyObject *py_list;
 
127
    int i;
 
128
 
 
129
    if ((py_list = PyList_New(0)) == NULL) {
 
130
        return NULL;
 
131
    }
 
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);
 
136
    }
 
137
    if (dealloc) g_ptr_array_free (list, TRUE);
 
138
    return py_list;
 
139
}
 
140
 
 
141
GList *
 
142
_helper_unwrap_pointer_pylist (PyObject *py_list, GType type)
 
143
{
 
144
        int size, i;
 
145
        GList *list = NULL;
 
146
 
 
147
        size = PyList_Size (py_list);
 
148
        for (i = 0; i < size; i++) {
 
149
                PyObject *py_ptr;
 
150
                gpointer ptr;
 
151
 
 
152
                py_ptr = PyList_GetItem (py_list, i);
 
153
                if (!pyg_pointer_check (py_ptr, type)) {
 
154
                        g_list_free (list);
 
155
                        return NULL;
 
156
                }
 
157
                ptr = pyg_pointer_get (py_ptr, void);
 
158
                list = g_list_prepend (list, ptr);
 
159
        }
 
160
 
 
161
        list = g_list_reverse (list);
 
162
        return list;
 
163
}
 
164
 
 
165
GList *
 
166
_helper_unwrap_string_pylist (PyObject *py_list)
 
167
{
 
168
    int size, i;
 
169
    GList *list = NULL;
 
170
 
 
171
    size = PyList_Size (py_list);
 
172
    for (i = 0; i < size; i++) {
 
173
        PyObject *py_str;
 
174
        char *str;
 
175
 
 
176
        py_str = PyList_GetItem (py_list, i);
 
177
        str = PyString_AsString (py_str);
 
178
        list = g_list_prepend (list, str);
 
179
    }
 
180
 
 
181
    list = g_list_reverse (list);
 
182
    return list;
 
183
}
 
184