release
[pygobject.git] / gi / pygi-foreign.c
blob84af1ec3467943d8add72a2f81e6d31b58515688
1 /* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2 /*
3 * Copyright (c) 2010 litl, LLC
4 * Copyright (c) 2010 Collabora Ltd. <http://www.collabora.co.uk/>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
29 #include "pygobject-internal.h"
30 #include "pygi-foreign.h"
32 #include <girepository.h>
34 typedef struct {
35 const char *namespace;
36 const char *name;
37 PyGIArgOverrideToGIArgumentFunc to_func;
38 PyGIArgOverrideFromGIArgumentFunc from_func;
39 PyGIArgOverrideReleaseFunc release_func;
40 } PyGIForeignStruct;
42 static GPtrArray *foreign_structs = NULL;
44 static void
45 init_foreign_structs (void)
47 foreign_structs = g_ptr_array_new ();
50 static PyGIForeignStruct *
51 do_lookup (const gchar *namespace, const gchar *name)
53 guint i;
54 for (i = 0; i < foreign_structs->len; i++) {
55 PyGIForeignStruct *foreign_struct = \
56 g_ptr_array_index (foreign_structs, i);
58 if ( (strcmp (namespace, foreign_struct->namespace) == 0) &&
59 (strcmp (name, foreign_struct->name) == 0)) {
60 return foreign_struct;
63 return NULL;
66 static PyObject *
67 pygi_struct_foreign_load_module (const char *namespace)
69 gchar *module_name = g_strconcat ("gi._gi_", namespace, NULL);
70 PyObject *module = PyImport_ImportModule (module_name);
71 g_free (module_name);
72 return module;
75 static PyGIForeignStruct *
76 pygi_struct_foreign_lookup_by_name (const char *namespace, const char *name)
78 PyGIForeignStruct *result;
80 result = do_lookup (namespace, name);
82 if (result == NULL) {
83 PyObject *module = pygi_struct_foreign_load_module (namespace);
85 if (module == NULL)
86 PyErr_Clear ();
87 else {
88 Py_DECREF (module);
89 result = do_lookup (namespace, name);
93 if (result == NULL) {
94 PyErr_Format (PyExc_TypeError,
95 "Couldn't find foreign struct converter for '%s.%s'",
96 namespace,
97 name);
100 return result;
103 static PyGIForeignStruct *
104 pygi_struct_foreign_lookup (GIBaseInfo *base_info)
106 const gchar *namespace = g_base_info_get_namespace (base_info);
107 const gchar *name = g_base_info_get_name (base_info);
109 return pygi_struct_foreign_lookup_by_name (namespace, name);
112 PyObject *
113 pygi_struct_foreign_convert_to_g_argument (PyObject *value,
114 GIInterfaceInfo *interface_info,
115 GITransfer transfer,
116 GIArgument *arg)
118 PyObject *result;
120 GIBaseInfo *base_info = (GIBaseInfo *) interface_info;
121 PyGIForeignStruct *foreign_struct = pygi_struct_foreign_lookup (base_info);
123 if (foreign_struct == NULL) {
124 PyErr_Format(PyExc_KeyError, "could not find foreign type %s",
125 g_base_info_get_name (base_info));
126 return FALSE;
129 result = foreign_struct->to_func (value, interface_info, transfer, arg);
130 return result;
133 PyObject *
134 pygi_struct_foreign_convert_from_g_argument (GIInterfaceInfo *interface_info,
135 GITransfer transfer,
136 GIArgument *arg)
138 GIBaseInfo *base_info = (GIBaseInfo *) interface_info;
139 PyGIForeignStruct *foreign_struct = pygi_struct_foreign_lookup (base_info);
141 if (foreign_struct == NULL)
142 return NULL;
144 return foreign_struct->from_func (interface_info, transfer, arg);
147 PyObject *
148 pygi_struct_foreign_release (GIBaseInfo *base_info,
149 gpointer struct_)
151 PyGIForeignStruct *foreign_struct = pygi_struct_foreign_lookup (base_info);
153 if (foreign_struct == NULL)
154 return NULL;
156 if (!foreign_struct->release_func)
157 Py_RETURN_NONE;
159 return foreign_struct->release_func (base_info, struct_);
162 void
163 pygi_register_foreign_struct (const char* namespace_,
164 const char* name,
165 PyGIArgOverrideToGIArgumentFunc to_func,
166 PyGIArgOverrideFromGIArgumentFunc from_func,
167 PyGIArgOverrideReleaseFunc release_func)
169 PyGIForeignStruct *new_struct = g_slice_new (PyGIForeignStruct);
170 new_struct->namespace = namespace_;
171 new_struct->name = name;
172 new_struct->to_func = to_func;
173 new_struct->from_func = from_func;
174 new_struct->release_func = release_func;
176 g_ptr_array_add (foreign_structs, new_struct);
179 PyObject *
180 pygi_require_foreign (PyObject *self, PyObject *args, PyObject *kwargs)
182 static char *kwlist[] = { "namespace", "symbol", NULL };
183 const char *namespace = NULL;
184 const char *symbol = NULL;
186 if (!PyArg_ParseTupleAndKeywords (args, kwargs,
187 "s|z:require_foreign",
188 kwlist, &namespace, &symbol)) {
189 return NULL;
192 if (symbol) {
193 PyGIForeignStruct *foreign;
194 foreign = pygi_struct_foreign_lookup_by_name (namespace, symbol);
195 if (foreign == NULL) {
196 return NULL;
198 } else {
199 PyObject *module = pygi_struct_foreign_load_module (namespace);
200 if (module) {
201 Py_DECREF (module);
202 } else {
203 return NULL;
207 Py_RETURN_NONE;
210 void
211 pygi_foreign_init (void)
213 if (foreign_structs == NULL) {
214 init_foreign_structs ();