ef5abaa7ad32a7689c067afe280143cf892464eb
[platform/upstream/pygobject2.git] / gi / pygi-foreign.c
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/>
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #include "pygobject.h"
30 #include "pygi-foreign.h"
31
32 #include <girepository.h>
33
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;
41
42 static GPtrArray *foreign_structs = NULL;
43
44 static void
45 init_foreign_structs ()
46 {
47     foreign_structs = g_ptr_array_new ();
48 }
49
50 static PyGIForeignStruct *
51 do_lookup (const gchar *namespace, const gchar *name)
52 {
53     gint i;
54     for (i = 0; i < foreign_structs->len; i++) {
55         PyGIForeignStruct *foreign_struct = \
56                 g_ptr_array_index (foreign_structs, i);
57
58         if ( (strcmp (namespace, foreign_struct->namespace) == 0) &&
59                 (strcmp (name, foreign_struct->name) == 0)) {
60             return foreign_struct;
61         }
62     }
63     return NULL;
64 }
65
66 static PyGIForeignStruct *
67 pygi_struct_foreign_lookup (GIBaseInfo *base_info)
68 {
69     PyGIForeignStruct *result;
70     const gchar *namespace = g_base_info_get_namespace (base_info);
71     const gchar *name = g_base_info_get_name (base_info);
72
73     if (foreign_structs == NULL) {
74         init_foreign_structs ();
75     }
76
77     result = do_lookup (namespace, name);
78
79     if (result == NULL) {
80         gchar *module_name = g_strconcat ("gi._gi_", namespace, NULL);
81         PyObject *module = PyImport_ImportModule (module_name);
82
83         g_free (module_name);
84
85         if (module == NULL)
86             PyErr_Clear ();
87         else {
88             Py_DECREF (module);
89             result = do_lookup (namespace, name);
90         }
91     }
92
93     if (result == NULL) {
94         PyErr_Format (PyExc_TypeError,
95                       "Couldn't find conversion for foreign struct '%s.%s'",
96                       namespace,
97                       name);
98     }
99
100     return result;
101 }
102
103 PyObject *
104 pygi_struct_foreign_convert_to_g_argument (PyObject        *value,
105                                            GIInterfaceInfo *interface_info,
106                                            GITransfer       transfer,
107                                            GIArgument      *arg)
108 {
109     PyObject *result;
110
111     GIBaseInfo *base_info = (GIBaseInfo *) interface_info;
112     PyGIForeignStruct *foreign_struct = pygi_struct_foreign_lookup (base_info);
113
114     if (foreign_struct == NULL) {
115         PyErr_Format(PyExc_KeyError, "could not find foreign type %s",
116                      g_base_info_get_name (base_info));
117         return FALSE;
118     }
119
120     result = foreign_struct->to_func (value, interface_info, transfer, arg);
121     return result;
122 }
123
124 PyObject *
125 pygi_struct_foreign_convert_from_g_argument (GIInterfaceInfo *interface_info,
126                                              GIArgument      *arg)
127 {
128     GIBaseInfo *base_info = (GIBaseInfo *) interface_info;
129     PyGIForeignStruct *foreign_struct = pygi_struct_foreign_lookup (base_info);
130
131     if (foreign_struct == NULL)
132         return NULL;
133
134     return foreign_struct->from_func (interface_info, arg);
135 }
136
137 PyObject *
138 pygi_struct_foreign_release (GIBaseInfo *base_info,
139                              gpointer    struct_)
140 {
141     PyGIForeignStruct *foreign_struct = pygi_struct_foreign_lookup (base_info);
142
143     if (foreign_struct == NULL)
144         return NULL;
145
146     if (!foreign_struct->release_func)
147         Py_RETURN_NONE;
148
149     return foreign_struct->release_func (base_info, struct_);
150 }
151
152 void
153 pygi_register_foreign_struct_real (const char* namespace_,
154                                    const char* name,
155                                    PyGIArgOverrideToGIArgumentFunc to_func,
156                                    PyGIArgOverrideFromGIArgumentFunc from_func,
157                                    PyGIArgOverrideReleaseFunc release_func)
158 {
159     PyGIForeignStruct *new_struct = g_slice_new (PyGIForeignStruct);
160     new_struct->namespace = namespace_;
161     new_struct->name = name;
162     new_struct->to_func = to_func;
163     new_struct->from_func = from_func;
164     new_struct->release_func = release_func;
165
166     g_ptr_array_add (foreign_structs, new_struct);
167 }