1 /*
2 * peas-utils.c
3 * This file is part of libpeas
4 *
5 * Copyright (C) 2010 Steve Frécinaux
6 * Copyright (C) 2011-2017 Garrett Regier
7 *
8 * libpeas is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * libpeas is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <string.h>
28
29 #include <gobject/gvaluecollector.h>
30
31 #include "peas-utils.h"
32
33 static const gchar *all_plugin_loaders[] = {
34 "c", "lua5.1", "python", "python3"
35 };
36
37 static const gchar *all_plugin_loader_modules[] = {
38 "cloader", "lua51loader", "pythonloader", "python3loader"
39 };
40
41 static const gint conflicting_plugin_loaders[PEAS_UTILS_N_LOADERS][2] = {
42 { -1, -1 }, /* c => {} */
43 { -1, -1 }, /* lua5.1 => {} */
44 { 3, -1 }, /* python => { python3 } */
45 { 2, -1 } /* python3 => { python } */
46 };
47
48 G_STATIC_ASSERT (G_N_ELEMENTS (all_plugin_loaders) == PEAS_UTILS_N_LOADERS);
49 G_STATIC_ASSERT (G_N_ELEMENTS (all_plugin_loader_modules) == PEAS_UTILS_N_LOADERS);
50 G_STATIC_ASSERT (G_N_ELEMENTS (conflicting_plugin_loaders) == PEAS_UTILS_N_LOADERS);
51
52 static
53 G_DEFINE_QUARK (peas-extension-base-class-and-interfaces-cache,
54 exten_type_cache)
55
56 static void
add_all_prerequisites(GType iface_type,GType * base_type,GPtrArray * ifaces)57 add_all_prerequisites (GType iface_type,
58 GType *base_type,
59 GPtrArray *ifaces)
60 {
61 GType *prereq;
62 guint n_prereq;
63 guint i;
64
65 g_ptr_array_add (ifaces, g_type_default_interface_ref (iface_type));
66
67 prereq = g_type_interface_prerequisites (iface_type, &n_prereq);
68
69 for (i = 0; i < n_prereq; ++i)
70 {
71 if (G_TYPE_IS_INTERFACE (prereq[i]))
72 {
73 add_all_prerequisites (prereq[i], base_type, ifaces);
74 continue;
75 }
76
77 if (!G_TYPE_IS_OBJECT (prereq[i]))
78 continue;
79
80 if (*base_type != G_TYPE_INVALID)
81 {
82 /* We already have the descendant GType */
83 if (g_type_is_a (*base_type, prereq[i]))
84 continue;
85
86 /* Neither GType are descendant of the other, this is an
87 * error and GObject will not be able to create an object
88 */
89 g_warn_if_fail (g_type_is_a (prereq[i], *base_type));
90 }
91
92 *base_type = prereq[i];
93 }
94
95 g_free (prereq);
96 }
97
98 static gpointer *
find_base_class_and_interfaces(GType exten_type)99 find_base_class_and_interfaces (GType exten_type)
100 {
101 GPtrArray *results;
102 GType base_type = G_TYPE_INVALID;
103 static GMutex cache_lock;
104 gpointer *data, *cached_data;
105
106 results = g_ptr_array_new ();
107
108 /* This is used for the GObjectClass of the base_type */
109 g_ptr_array_add (results, NULL);
110
111 if (G_TYPE_IS_INTERFACE (exten_type))
112 {
113 add_all_prerequisites (exten_type, &base_type, results);
114 }
115 else
116 {
117 gint i;
118 GType *interfaces;
119
120 interfaces = g_type_interfaces (exten_type, NULL);
121 for (i = 0; interfaces[i] != G_TYPE_INVALID; ++i)
122 add_all_prerequisites (interfaces[i], &base_type, results);
123
124 base_type = exten_type;
125
126 g_free (interfaces);
127 }
128
129 if (base_type != G_TYPE_INVALID)
130 g_ptr_array_index (results, 0) = g_type_class_ref (base_type);
131
132 g_ptr_array_add (results, NULL);
133 data = g_ptr_array_free (results, FALSE);
134
135 g_mutex_lock (&cache_lock);
136 cached_data = g_type_get_qdata (exten_type, exten_type_cache_quark ());
137
138 if (cached_data != NULL)
139 {
140 g_free (data);
141 data = cached_data;
142 }
143 else
144 {
145 g_type_set_qdata (exten_type, exten_type_cache_quark (), data);
146 }
147
148 g_mutex_unlock (&cache_lock);
149 return data;
150 }
151
152 static inline gpointer *
get_base_class_and_interfaces(GType exten_type,GObjectClass ** base_class)153 get_base_class_and_interfaces (GType exten_type,
154 GObjectClass **base_class)
155 {
156 gpointer *data;
157
158 data = g_type_get_qdata (exten_type, exten_type_cache_quark ());
159 if (G_UNLIKELY (data == NULL))
160 data = find_base_class_and_interfaces (exten_type);
161
162 *base_class = data[0];
163 return &data[1];
164 }
165
166 static inline GParamSpec *
find_param_spec_for_prerequisites(const gchar * name,GObjectClass * base_class,gpointer * ifaces)167 find_param_spec_for_prerequisites (const gchar *name,
168 GObjectClass *base_class,
169 gpointer *ifaces)
170 {
171 guint i;
172 GParamSpec *pspec = NULL;
173
174 if (base_class != NULL)
175 pspec = g_object_class_find_property (base_class, name);
176
177 for (i = 0; ifaces[i] != NULL && pspec == NULL; ++i)
178 pspec = g_object_interface_find_property (ifaces[i], name);
179
180 return pspec;
181 }
182
183 gboolean
peas_utils_properties_array_to_parameter_list(GType exten_type,guint n_properties,const gchar ** prop_names,const GValue * prop_values,GParameter * parameters)184 peas_utils_properties_array_to_parameter_list (GType exten_type,
185 guint n_properties,
186 const gchar **prop_names,
187 const GValue *prop_values,
188 GParameter *parameters)
189 {
190 guint i;
191 gpointer *ifaces;
192 GObjectClass *base_class;
193
194 g_return_val_if_fail (n_properties == 0 || prop_names != NULL, FALSE);
195 g_return_val_if_fail (n_properties == 0 || prop_values != NULL, FALSE);
196 g_return_val_if_fail (n_properties == 0 || parameters != NULL, FALSE);
197
198 ifaces = get_base_class_and_interfaces (exten_type, &base_class);
199 memset (parameters, 0, sizeof (GParameter) * n_properties);
200 for (i = 0; i < n_properties; i++)
201 {
202 GParamSpec *pspec;
203 if (prop_names[i] == NULL)
204 {
205 g_warning ("The property name at index %u should not be NULL.", i);
206 goto error;
207 }
208 if (!G_IS_VALUE (&prop_values[i]))
209 {
210 g_warning ("The property value at index %u should be an initialized GValue.", i);
211 goto error;
212 }
213 pspec = find_param_spec_for_prerequisites (prop_names[i], base_class,
214 ifaces);
215 if (!pspec)
216 {
217 g_warning ("%s: type '%s' has no property named '%s'",
218 G_STRFUNC, g_type_name (exten_type), prop_names[i]);
219 goto error;
220 }
221
222 parameters[i].name = prop_names[i];
223
224 g_value_init (¶meters[i].value,
225 G_VALUE_TYPE (&prop_values[i]));
226 g_value_copy (&prop_values[i], ¶meters[i].value);
227 }
228 return TRUE;
229
230 error:
231 n_properties = i;
232 for (i = 0; i < n_properties; i++)
233 g_value_unset (¶meters[i].value);
234 return FALSE;
235 }
236
237 gboolean
peas_utils_valist_to_parameter_list(GType exten_type,const gchar * first_property,va_list args,GParameter ** params,guint * n_params)238 peas_utils_valist_to_parameter_list (GType exten_type,
239 const gchar *first_property,
240 va_list args,
241 GParameter **params,
242 guint *n_params)
243 {
244 gpointer *ifaces;
245 GObjectClass *base_class;
246 const gchar *name;
247 guint n_allocated_params;
248
249 g_return_val_if_fail (G_TYPE_IS_INTERFACE (exten_type) ||
250 G_TYPE_IS_OBJECT (exten_type), FALSE);
251
252 ifaces = get_base_class_and_interfaces (exten_type, &base_class);
253
254 *n_params = 0;
255 n_allocated_params = 16;
256 *params = g_new0 (GParameter, n_allocated_params);
257
258 name = first_property;
259 while (name)
260 {
261 gchar *error_msg = NULL;
262 GParamSpec *pspec;
263
264 pspec = find_param_spec_for_prerequisites (name, base_class, ifaces);
265
266 if (!pspec)
267 {
268 g_warning ("%s: type '%s' has no property named '%s'",
269 G_STRFUNC, g_type_name (exten_type), name);
270 goto error;
271 }
272
273 if (*n_params >= n_allocated_params)
274 {
275 n_allocated_params += 16;
276 *params = g_renew (GParameter, *params, n_allocated_params);
277 memset (*params + (n_allocated_params - 16),
278 0, sizeof (GParameter) * 16);
279 }
280
281 (*params)[*n_params].name = name;
282 G_VALUE_COLLECT_INIT (&(*params)[*n_params].value, pspec->value_type,
283 args, 0, &error_msg);
284
285 (*n_params)++;
286
287 if (error_msg)
288 {
289 g_warning ("%s: %s", G_STRFUNC, error_msg);
290 g_free (error_msg);
291 goto error;
292 }
293
294 name = va_arg (args, gchar*);
295 }
296
297 return TRUE;
298
299 error:
300
301 for (; *n_params > 0; --(*n_params))
302 g_value_unset (&(*params)[*n_params].value);
303
304 g_free (*params);
305 return FALSE;
306 }
307
308 gint
peas_utils_get_loader_id(const gchar * loader)309 peas_utils_get_loader_id (const gchar *loader)
310 {
311 gint i;
312 gsize len;
313 gchar lowercase[32];
314
315 len = strlen (loader);
316
317 /* No loader has a name that long */
318 if (len >= G_N_ELEMENTS (lowercase))
319 return -1;
320
321 for (i = 0; i < len; ++i)
322 lowercase[i] = g_ascii_tolower (loader[i]);
323
324 lowercase[len] = '\0';
325
326 for (i = 0; i < G_N_ELEMENTS (all_plugin_loaders); ++i)
327 {
328 if (g_strcmp0 (lowercase, all_plugin_loaders[i]) == 0)
329 return i;
330 }
331
332 return -1;
333 }
334
335 const gchar *
peas_utils_get_loader_from_id(gint loader_id)336 peas_utils_get_loader_from_id (gint loader_id)
337 {
338 g_return_val_if_fail (loader_id >= 0, NULL);
339 g_return_val_if_fail (loader_id < PEAS_UTILS_N_LOADERS, NULL);
340
341 return all_plugin_loaders[loader_id];
342 }
343
344 const gchar *
peas_utils_get_loader_module_from_id(gint loader_id)345 peas_utils_get_loader_module_from_id (gint loader_id)
346 {
347 g_return_val_if_fail (loader_id >= 0, NULL);
348 g_return_val_if_fail (loader_id < PEAS_UTILS_N_LOADERS, NULL);
349
350 return all_plugin_loader_modules[loader_id];
351 }
352
353 const gint *
peas_utils_get_conflicting_loaders_from_id(gint loader_id)354 peas_utils_get_conflicting_loaders_from_id (gint loader_id)
355 {
356 g_return_val_if_fail (loader_id >= 0, NULL);
357 g_return_val_if_fail (loader_id < PEAS_UTILS_N_LOADERS, NULL);
358
359 return conflicting_plugin_loaders[loader_id];
360 }
361
362