1 /* -*- Mode: C; c-file-style: "gnu"; -*- */
2 /* GObject introspection: Repository implementation
4 * Copyright (C) 2005 Matthias Clasen
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
26 #include <glib/gprintf.h>
28 #include "girepository.h"
31 static GIRepository *default_repository = NULL;
32 static GHashTable *default_typelib = NULL;
33 static GSList *search_path = NULL;
35 struct _GIRepositoryPrivate
37 GHashTable *typelib; /* (string) namespace -> GTypelib */
40 G_DEFINE_TYPE (GIRepository, g_irepository, G_TYPE_OBJECT);
43 g_irepository_init (GIRepository *repository)
45 repository->priv = G_TYPE_INSTANCE_GET_PRIVATE (repository, G_TYPE_IREPOSITORY,
50 g_irepository_finalize (GObject *object)
52 GIRepository *repository = G_IREPOSITORY (object);
54 g_hash_table_destroy (repository->priv->typelib);
56 (* G_OBJECT_CLASS (g_irepository_parent_class)->finalize) (G_OBJECT (repository));
60 g_irepository_class_init (GIRepositoryClass *class)
62 GObjectClass *gobject_class;
64 gobject_class = G_OBJECT_CLASS (class);
66 gobject_class->finalize = g_irepository_finalize;
68 g_type_class_add_private (class, sizeof (GIRepositoryPrivate));
72 g_irepository_register (GIRepository *repository,
80 g_return_val_if_fail (typelib != NULL, NULL);
82 header = (Header *)typelib->data;
84 g_return_val_if_fail (header != NULL, NULL);
86 if (repository != NULL)
88 if (repository->priv->typelib == NULL)
89 repository->priv->typelib = g_hash_table_new_full (g_str_hash, g_str_equal,
90 (GDestroyNotify) NULL,
91 (GDestroyNotify) g_typelib_free);
92 table = repository->priv->typelib;
96 if (default_typelib == NULL)
97 default_typelib = g_hash_table_new_full (g_str_hash, g_str_equal,
98 (GDestroyNotify) NULL,
99 (GDestroyNotify) g_typelib_free);
100 table = default_typelib;
103 name = g_typelib_get_string (typelib, header->namespace);
105 if (g_hash_table_lookup (table, name))
107 g_printerr ("typelib (%p) for '%s' already registered\n",
112 g_hash_table_insert (table, g_strdup(name), (void *)typelib);
114 if (typelib->module == NULL)
115 typelib->module = g_module_open (NULL, 0);
117 if (g_getenv ("G_IREPOSITORY_VERBOSE"))
119 g_printerr ("Loaded typelib %s\n", name);
127 g_irepository_unregister (GIRepository *repository,
128 const gchar *namespace)
132 if (repository != NULL)
133 table = repository->priv->typelib;
135 table = default_typelib;
137 if (!g_hash_table_remove (table, namespace))
139 g_printerr ("namespace '%s' not registered\n", namespace);
144 g_irepository_is_registered (GIRepository *repository,
145 const gchar *namespace)
149 if (repository != NULL)
150 table = repository->priv->typelib;
152 table = default_typelib;
154 return g_hash_table_lookup (table, namespace) != NULL;
158 g_irepository_get_default (void)
160 if (default_repository == NULL)
162 default_repository = g_object_new (G_TYPE_IREPOSITORY, NULL);
163 if (default_typelib == NULL)
164 default_typelib = g_hash_table_new_full (g_str_hash, g_str_equal,
165 (GDestroyNotify) NULL,
166 (GDestroyNotify) g_typelib_free);
167 default_repository->priv->typelib = default_typelib;
170 return default_repository;
174 count_interfaces (gpointer key,
178 guchar *typelib = ((GTypelib *) value)->data;
179 gint *n_interfaces = (gint *)data;
181 *n_interfaces += ((Header *)typelib)->n_local_entries;
185 g_irepository_get_n_infos (GIRepository *repository,
186 const gchar *namespace)
188 gint n_interfaces = 0;
194 typelib = g_hash_table_lookup (repository->priv->typelib, namespace);
197 n_interfaces = ((Header *)typelib->data)->n_local_entries;
201 g_hash_table_foreach (repository->priv->typelib,
202 count_interfaces, &n_interfaces);
217 find_interface (gpointer key,
222 GTypelib *typelib = (GTypelib *)value;
223 IfaceData *iface_data = (IfaceData *)data;
232 n_entries = ((Header *)typelib->data)->n_local_entries;
234 if (iface_data->name)
236 for (i = 1; i <= n_entries; i++)
238 entry = g_typelib_get_dir_entry (typelib, i);
239 name = g_typelib_get_string (typelib, entry->name);
240 if (strcmp (name, iface_data->name) == 0)
247 else if (iface_data->type)
249 for (i = 1; i <= n_entries; i++)
251 entry = g_typelib_get_dir_entry (typelib, i);
252 if (entry->blob_type < 4)
255 offset = *(guint32*)&typelib->data[entry->offset + 8];
256 type = g_typelib_get_string (typelib, offset);
257 if (strcmp (type, iface_data->type) == 0)
264 else if (iface_data->index > n_entries)
265 iface_data->index -= n_entries;
266 else if (iface_data->index > 0)
268 index = iface_data->index;
269 iface_data->index = 0;
274 entry = g_typelib_get_dir_entry (typelib, index);
275 iface_data->iface = g_info_new (entry->blob_type, NULL,
276 typelib, entry->offset);
281 g_irepository_get_info (GIRepository *repository,
282 const gchar *namespace,
289 data.index = index + 1;
296 typelib = g_hash_table_lookup (repository->priv->typelib, namespace);
299 find_interface ((void *)namespace, typelib, &data);
302 g_hash_table_foreach (repository->priv->typelib, find_interface, &data);
308 g_irepository_find_by_gtype (GIRepository *repository,
314 data.type = g_type_name (type);
318 g_hash_table_foreach (repository->priv->typelib, find_interface, &data);
324 g_irepository_find_by_name (GIRepository *repository,
325 const gchar *namespace,
339 typelib = g_hash_table_lookup (repository->priv->typelib, namespace);
342 find_interface ((void *)namespace, typelib, &data);
345 g_hash_table_foreach (repository->priv->typelib, find_interface, &data);
351 collect_namespaces (gpointer key,
357 *list = g_list_append (*list, key);
361 g_irepository_get_namespaces (GIRepository *repository)
363 GList *l, *list = NULL;
367 g_hash_table_foreach (repository->priv->typelib, collect_namespaces, &list);
369 names = g_malloc0 (sizeof (gchar *) * (g_list_length (list) + 1));
371 for (l = list; l; l = l->next)
372 names[i++] = g_strdup (l->data);
379 g_irepository_get_shared_library (GIRepository *repository,
380 const gchar *namespace)
385 typelib = g_hash_table_lookup (repository->priv->typelib, namespace);
388 header = (Header *) typelib->data;
389 if (header->shared_library)
390 return g_typelib_get_string (typelib, header->shared_library);
396 g_irepository_build_search_path (void)
401 if (g_getenv ("GIREPOPATH")) {
403 path = g_strconcat (g_getenv ("GIREPOPATH"), ":", GIREPO_DEFAULT_SEARCH_PATH, NULL);
404 tokens = g_strsplit (path, ":", 0);
407 tokens = g_strsplit (GIREPO_DEFAULT_SEARCH_PATH, ":", 0);
409 search_path = g_slist_prepend (search_path, ".");
410 for (dir = tokens; *dir; ++dir)
411 search_path = g_slist_prepend (search_path, *dir);
412 search_path = g_slist_reverse (search_path);
417 g_irepository_register_file (GIRepository *repository,
418 const gchar *namespace,
423 gchar *fname, *full_path;
425 GError *error1 = NULL;
426 GTypelib *typelib = NULL;
427 const gchar *typelib_namespace, *shlib_fname;
432 if (repository != NULL)
433 table = repository->priv->typelib;
435 table = default_typelib;
437 /* don't bother loading a namespace if already registered */
438 if (g_hash_table_lookup (table, namespace))
441 if (search_path == NULL)
442 g_irepository_build_search_path ();
444 fname = g_strconcat (namespace, ".repo", NULL);
446 for (ldir = search_path; ldir; ldir = ldir->next) {
448 full_path = g_build_filename (dir, fname, NULL);
449 mfile = g_mapped_file_new (full_path, FALSE, &error1);
451 g_debug ("Failed to mmap \"%s\": %s", full_path, error1->message);
452 g_clear_error (&error1);
457 typelib = g_typelib_new_from_mapped_file (mfile);
458 typelib_namespace = g_typelib_get_string (typelib, ((Header *) typelib->data)->namespace);
459 if (strcmp (typelib_namespace, namespace) != 0) {
460 g_set_error (error, G_IREPOSITORY_ERROR,
461 G_IREPOSITORY_ERROR_NAMESPACE_MISMATCH,
462 "Typelib file %s for namespace '%s' contains namespace '%s'"
463 " which doesn't match the file name",
464 full_path, namespace, typelib_namespace);
470 if (typelib == NULL) {
471 g_set_error (error, G_IREPOSITORY_ERROR,
472 G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
473 "Typelib file for namespace '%s' was not found in search"
474 " path or could not be openened", namespace);
477 /* optionally load shared library and attach it to the typelib */
478 shlib = ((Header *) typelib->data)->shared_library;
480 shlib_fname = g_typelib_get_string (typelib, shlib);
481 module = g_module_open (shlib_fname, G_MODULE_BIND_LAZY|G_MODULE_BIND_LOCAL);
482 if (module == NULL) {
483 g_set_error (error, G_IREPOSITORY_ERROR,
484 G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
485 "Typelib for namespace '%s' references shared library %s,"
486 " but it could not be openened (%s)",
487 namespace, shlib_fname, g_module_error ());
492 g_hash_table_remove (table, namespace);
493 return g_irepository_register (repository, typelib);
498 g_irepository_error_quark (void)
500 static GQuark quark = 0;
502 quark = g_quark_from_static_string ("g-irepository-error-quark");