1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 * GObject introspection: Repository implementation
4 * Copyright (C) 2005 Matthias Clasen
5 * Copyright (C) 2008 Colin Walters <walters@verbum.org>
6 * Copyright (C) 2008 Red Hat, Inc.
8 * This library 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 of the License, or (at your option) any later version.
13 * This library 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.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
31 #include <glib/gprintf.h>
33 #include "girepository.h"
34 #include "gitypelib-internal.h"
35 #include "girepository-private.h"
38 * SECTION:girepository
39 * @short_description: GObject Introspection repository manager
40 * @include: girepository.h
42 * #GIRepository is used to manage repositories of namespaces. Namespaces
43 * are represented on disk by type libraries (.typelib files).
47 static GIRepository *default_repository = NULL;
48 static GSList *search_path = NULL;
50 struct _GIRepositoryPrivate
52 GHashTable *typelibs; /* (string) namespace -> GITypelib */
53 GHashTable *lazy_typelibs; /* (string) namespace-version -> GITypelib */
54 GHashTable *info_by_gtype; /* GType -> GIBaseInfo */
55 GHashTable *info_by_error_domain; /* GQuark -> GIBaseInfo */
58 G_DEFINE_TYPE (GIRepository, g_irepository, G_TYPE_OBJECT);
60 #ifdef G_PLATFORM_WIN32
64 static HMODULE girepository_dll = NULL;
69 DllMain (HINSTANCE hinstDLL,
73 if (fdwReason == DLL_PROCESS_ATTACH)
74 girepository_dll = hinstDLL;
81 #undef GOBJECT_INTROSPECTION_LIBDIR
83 /* GOBJECT_INTROSPECTION_LIBDIR is used only in code called just once,
84 * so no problem leaking this
86 #define GOBJECT_INTROSPECTION_LIBDIR \
87 g_build_filename (g_win32_get_package_installation_directory_of_module (girepository_dll), \
94 g_irepository_init (GIRepository *repository)
96 repository->priv = G_TYPE_INSTANCE_GET_PRIVATE (repository, G_TYPE_IREPOSITORY,
98 repository->priv->typelibs
99 = g_hash_table_new_full (g_str_hash, g_str_equal,
100 (GDestroyNotify) NULL,
101 (GDestroyNotify) g_typelib_free);
102 repository->priv->lazy_typelibs
103 = g_hash_table_new_full (g_str_hash, g_str_equal,
104 (GDestroyNotify) g_free,
105 (GDestroyNotify) NULL);
106 repository->priv->info_by_gtype
107 = g_hash_table_new_full (g_direct_hash, g_direct_equal,
108 (GDestroyNotify) NULL,
109 (GDestroyNotify) g_base_info_unref);
110 repository->priv->info_by_error_domain
111 = g_hash_table_new_full (g_direct_hash, g_direct_equal,
112 (GDestroyNotify) NULL,
113 (GDestroyNotify) g_base_info_unref);
117 g_irepository_finalize (GObject *object)
119 GIRepository *repository = G_IREPOSITORY (object);
121 g_hash_table_destroy (repository->priv->typelibs);
122 g_hash_table_destroy (repository->priv->lazy_typelibs);
123 g_hash_table_destroy (repository->priv->info_by_gtype);
124 g_hash_table_destroy (repository->priv->info_by_error_domain);
126 (* G_OBJECT_CLASS (g_irepository_parent_class)->finalize) (G_OBJECT (repository));
130 g_irepository_class_init (GIRepositoryClass *class)
132 GObjectClass *gobject_class;
134 gobject_class = G_OBJECT_CLASS (class);
136 gobject_class->finalize = g_irepository_finalize;
138 g_type_class_add_private (class, sizeof (GIRepositoryPrivate));
144 static gsize initialized = 0;
146 if (!g_once_init_enter (&initialized))
149 if (default_repository == NULL)
150 default_repository = g_object_new (G_TYPE_IREPOSITORY, NULL);
152 if (search_path == NULL)
156 const gchar *type_lib_path_env;
158 /* This variable is intended to take precedence over both:
159 * - the default search path;
160 * - all g_irepository_prepend_search_path() calls.
162 type_lib_path_env = g_getenv ("GI_TYPELIB_PATH");
165 if (type_lib_path_env)
170 custom_dirs = g_strsplit (type_lib_path_env, G_SEARCHPATH_SEPARATOR_S, 0);
175 search_path = g_slist_prepend (search_path, *d);
179 /* ownership of the array content was passed to the list */
180 g_free (custom_dirs);
183 libdir = GOBJECT_INTROSPECTION_LIBDIR;
185 typelib_dir = g_build_filename (libdir, "girepository-1.0", NULL);
187 search_path = g_slist_prepend (search_path, typelib_dir);
189 search_path = g_slist_reverse (search_path);
192 g_once_init_leave (&initialized, 1);
196 * g_irepository_prepend_search_path:
197 * @directory: (type filename): directory name to prepend to the typelib
200 * Prepends @directory to the typelib search path.
201 * See g_irepository_get_search_path().
204 g_irepository_prepend_search_path (const char *directory)
207 search_path = g_slist_prepend (search_path, g_strdup (directory));
211 * g_irepository_get_search_path:
213 * Returns the current search path #GIRepository will use when loading
214 * typelib files. The list is internal to #GIRespository and should not
215 * be freed, nor should its string elements.
217 * Returns: (element-type filename) (transfer none): #GSList of strings
220 g_irepository_get_search_path (void)
226 build_typelib_key (const char *name, const char *source)
228 GString *str = g_string_new (name);
229 g_string_append_c (str, '\0');
230 g_string_append (str, source);
231 return g_string_free (str, FALSE);
234 /* Note: Returns %NULL (not an empty %NULL-terminated array) if there are no
237 get_typelib_dependencies (GITypelib *typelib)
240 const char *dependencies_glob;
242 header = (Header *)typelib->data;
244 if (header->dependencies == 0)
247 dependencies_glob = g_typelib_get_string (typelib, header->dependencies);
248 return g_strsplit (dependencies_glob, "|", 0);
251 static GIRepository *
252 get_repository (GIRepository *repository)
256 if (repository != NULL)
259 return default_repository;
263 check_version_conflict (GITypelib *typelib,
264 const gchar *namespace,
265 const gchar *expected_version,
266 char **version_conflict)
269 const char *loaded_version;
271 if (expected_version == NULL)
273 if (version_conflict)
274 *version_conflict = NULL;
278 header = (Header*)typelib->data;
279 loaded_version = g_typelib_get_string (typelib, header->nsversion);
280 g_assert (loaded_version != NULL);
282 if (strcmp (expected_version, loaded_version) != 0)
284 if (version_conflict)
285 *version_conflict = (char*)loaded_version;
288 if (version_conflict)
289 *version_conflict = NULL;
294 get_registered_status (GIRepository *repository,
295 const char *namespace,
298 gboolean *lazy_status,
299 char **version_conflict)
302 repository = get_repository (repository);
304 *lazy_status = FALSE;
305 typelib = g_hash_table_lookup (repository->priv->typelibs, namespace);
307 return check_version_conflict (typelib, namespace, version, version_conflict);
308 typelib = g_hash_table_lookup (repository->priv->lazy_typelibs, namespace);
315 return check_version_conflict (typelib, namespace, version, version_conflict);
319 get_registered (GIRepository *repository,
320 const char *namespace,
323 return get_registered_status (repository, namespace, version, TRUE, NULL, NULL);
327 load_dependencies_recurse (GIRepository *repository,
333 dependencies = get_typelib_dependencies (typelib);
335 if (dependencies != NULL)
339 for (i = 0; dependencies[i]; i++)
341 char *dependency = dependencies[i];
342 const char *last_dash;
343 char *dependency_namespace;
344 const char *dependency_version;
346 last_dash = strrchr (dependency, '-');
347 dependency_namespace = g_strndup (dependency, last_dash - dependency);
348 dependency_version = last_dash+1;
350 if (!g_irepository_require (repository, dependency_namespace, dependency_version,
353 g_free (dependency_namespace);
354 g_strfreev (dependencies);
357 g_free (dependency_namespace);
359 g_strfreev (dependencies);
365 register_internal (GIRepository *repository,
372 const gchar *namespace;
374 g_return_val_if_fail (typelib != NULL, FALSE);
376 header = (Header *)typelib->data;
378 g_return_val_if_fail (header != NULL, FALSE);
380 namespace = g_typelib_get_string (typelib, header->namespace);
384 g_assert (!g_hash_table_lookup (repository->priv->lazy_typelibs,
386 g_hash_table_insert (repository->priv->lazy_typelibs,
387 build_typelib_key (namespace, source), (void *)typelib);
394 /* First, try loading all the dependencies */
395 if (!load_dependencies_recurse (repository, typelib, error))
398 /* Check if we are transitioning from lazily loaded state */
399 if (g_hash_table_lookup_extended (repository->priv->lazy_typelibs,
401 (gpointer)&key, &value))
402 g_hash_table_remove (repository->priv->lazy_typelibs, key);
404 key = build_typelib_key (namespace, source);
406 g_hash_table_insert (repository->priv->typelibs, key, (void *)typelib);
413 * g_irepository_get_immediate_dependencies:
414 * @repository: (nullable): A #GIRepository or %NULL for the singleton
415 * process-global default #GIRepository
416 * @namespace_: Namespace of interest
418 * Return an array of the immediate versioned dependencies for @namespace_.
419 * Returned strings are of the form <code>namespace-version</code>.
421 * Note: @namespace_ must have already been loaded using a function
422 * such as g_irepository_require() before calling this function.
424 * To get the transitive closure of dependencies for @namespace_, use
425 * g_irepository_get_dependencies().
427 * Returns: (transfer full): Zero-terminated string array of immediate versioned
433 g_irepository_get_immediate_dependencies (GIRepository *repository,
434 const char *namespace)
439 g_return_val_if_fail (namespace != NULL, NULL);
441 repository = get_repository (repository);
443 typelib = get_registered (repository, namespace, NULL);
444 g_return_val_if_fail (typelib != NULL, NULL);
446 /* Ensure we always return a non-%NULL vector. */
447 deps = get_typelib_dependencies (typelib);
449 deps = g_strsplit ("", "|", 0);
454 /* Load the transitive closure of dependency namespace-version strings for the
455 * given @typelib. @repository must be non-%NULL. @transitive_dependencies must
456 * be a pre-existing GHashTable<owned utf8, owned utf8> set for storing the
459 get_typelib_dependencies_transitive (GIRepository *repository,
461 GHashTable *transitive_dependencies)
463 gchar **immediate_dependencies;
466 immediate_dependencies = get_typelib_dependencies (typelib);
468 for (i = 0; immediate_dependencies != NULL && immediate_dependencies[i]; i++)
471 const gchar *last_dash;
472 gchar *dependency_namespace;
474 dependency = immediate_dependencies[i];
476 /* Steal from the strv. */
477 g_hash_table_add (transitive_dependencies, dependency);
478 immediate_dependencies[i] = NULL;
480 /* Recurse for this namespace. */
481 last_dash = strrchr (dependency, '-');
482 dependency_namespace = g_strndup (dependency, last_dash - dependency);
484 typelib = get_registered (repository, dependency_namespace, NULL);
485 g_return_if_fail (typelib != NULL);
486 get_typelib_dependencies_transitive (repository, typelib,
487 transitive_dependencies);
489 g_free (dependency_namespace);
492 g_free (immediate_dependencies);
496 * g_irepository_get_dependencies:
497 * @repository: (allow-none): A #GIRepository or %NULL for the singleton
498 * process-global default #GIRepository
499 * @namespace_: Namespace of interest
501 * Return an array of all (transitive) versioned dependencies for
502 * @namespace_. Returned strings are of the form
503 * <code>namespace-version</code>.
505 * Note: @namespace_ must have already been loaded using a function
506 * such as g_irepository_require() before calling this function.
508 * To get only the immediate dependencies for @namespace_, use
509 * g_irepository_get_immediate_dependencies().
511 * Returns: (transfer full): Zero-terminated string array of all versioned
515 g_irepository_get_dependencies (GIRepository *repository,
516 const char *namespace)
519 GHashTable *transitive_dependencies; /* set of owned utf8 */
522 GPtrArray *out; /* owned utf8 elements */
524 g_return_val_if_fail (namespace != NULL, NULL);
526 repository = get_repository (repository);
528 typelib = get_registered (repository, namespace, NULL);
529 g_return_val_if_fail (typelib != NULL, NULL);
531 /* Load the dependencies. */
532 transitive_dependencies = g_hash_table_new (g_str_hash, g_str_equal);
533 get_typelib_dependencies_transitive (repository, typelib,
534 transitive_dependencies);
536 /* Convert to a string array. */
537 out = g_ptr_array_new_full (g_hash_table_size (transitive_dependencies),
539 g_hash_table_iter_init (&iter, transitive_dependencies);
541 while (g_hash_table_iter_next (&iter, (gpointer) &dependency, NULL))
543 g_ptr_array_add (out, dependency);
544 g_hash_table_iter_steal (&iter);
547 g_hash_table_unref (transitive_dependencies);
549 /* Add a NULL terminator. */
550 g_ptr_array_add (out, NULL);
552 return (gchar **) g_ptr_array_free (out, FALSE);
556 * g_irepository_load_typelib:
557 * @repository: (allow-none): A #GIRepository or %NULL for the singleton
558 * process-global default #GIRepository
566 g_irepository_load_typelib (GIRepository *repository,
568 GIRepositoryLoadFlags flags,
572 const char *namespace;
573 const char *nsversion;
574 gboolean allow_lazy = flags & G_IREPOSITORY_LOAD_FLAG_LAZY;
576 char *version_conflict;
578 repository = get_repository (repository);
580 header = (Header *) typelib->data;
581 namespace = g_typelib_get_string (typelib, header->namespace);
582 nsversion = g_typelib_get_string (typelib, header->nsversion);
584 if (get_registered_status (repository, namespace, nsversion, allow_lazy,
585 &is_lazy, &version_conflict))
587 if (version_conflict != NULL)
589 g_set_error (error, G_IREPOSITORY_ERROR,
590 G_IREPOSITORY_ERROR_NAMESPACE_VERSION_CONFLICT,
591 "Attempting to load namespace '%s', version '%s', but '%s' is already loaded",
592 namespace, nsversion, version_conflict);
597 return register_internal (repository, "<builtin>",
598 allow_lazy, typelib, error);
602 * g_irepository_is_registered:
603 * @repository: (allow-none): A #GIRepository or %NULL for the singleton
604 * process-global default #GIRepository
605 * @namespace_: Namespace of interest
606 * @version: (allow-none): Required version, may be %NULL for latest
608 * Check whether a particular namespace (and optionally, a specific
609 * version thereof) is currently loaded. This function is likely to
610 * only be useful in unusual circumstances; in order to act upon
611 * metadata in the namespace, you should call g_irepository_require()
612 * instead which will ensure the namespace is loaded, and return as
613 * quickly as this function will if it has already been loaded.
615 * Returns: %TRUE if namespace-version is loaded, %FALSE otherwise
618 g_irepository_is_registered (GIRepository *repository,
619 const gchar *namespace,
620 const gchar *version)
622 repository = get_repository (repository);
623 return get_registered (repository, namespace, version) != NULL;
627 * g_irepository_get_default:
629 * Returns the singleton process-global default #GIRepository. It is
630 * not currently supported to have multiple repositories in a
631 * particular process, but this function is provided in the unlikely
632 * eventuality that it would become possible, and as a convenience for
633 * higher level language bindings to conform to the GObject method
636 * All methods on #GIRepository also accept %NULL as an instance
637 * parameter to mean this default repository, which is usually more
640 * Returns: (transfer none): The global singleton #GIRepository
643 g_irepository_get_default (void)
645 return get_repository (NULL);
649 * g_irepository_get_n_infos:
650 * @repository: (allow-none): A #GIRepository or %NULL for the singleton
651 * process-global default #GIRepository
652 * @namespace_: Namespace to inspect
654 * This function returns the number of metadata entries in
655 * given namespace @namespace_. The namespace must have
656 * already been loaded before calling this function.
658 * Returns: number of metadata entries
661 g_irepository_get_n_infos (GIRepository *repository,
662 const gchar *namespace)
665 gint n_interfaces = 0;
667 g_return_val_if_fail (namespace != NULL, -1);
669 repository = get_repository (repository);
671 typelib = get_registered (repository, namespace, NULL);
673 g_return_val_if_fail (typelib != NULL, -1);
675 n_interfaces = ((Header *)typelib->data)->n_local_entries;
681 * g_irepository_get_info:
682 * @repository: (allow-none): A #GIRepository or %NULL for the singleton
683 * process-global default #GIRepository
684 * @namespace_: Namespace to inspect
685 * @index: 0-based offset into namespace metadata for entry
687 * This function returns a particular metadata entry in the
688 * given namespace @namespace_. The namespace must have
689 * already been loaded before calling this function.
690 * See g_irepository_get_n_infos() to find the maximum number of
693 * Returns: (transfer full): #GIBaseInfo containing metadata
696 g_irepository_get_info (GIRepository *repository,
697 const gchar *namespace,
703 g_return_val_if_fail (namespace != NULL, NULL);
705 repository = get_repository (repository);
707 typelib = get_registered (repository, namespace, NULL);
709 g_return_val_if_fail (typelib != NULL, NULL);
711 entry = g_typelib_get_dir_entry (typelib, index + 1);
714 return _g_info_new_full (entry->blob_type,
716 NULL, typelib, entry->offset);
720 const gchar *gtype_name;
721 GITypelib *result_typelib;
725 find_by_gtype (GHashTable *table, FindByGTypeData *data, gboolean check_prefix)
731 g_hash_table_iter_init (&iter, table);
732 while (g_hash_table_iter_next (&iter, &key, &value))
734 GITypelib *typelib = (GITypelib*)value;
737 if (!g_typelib_matches_gtype_name_prefix (typelib, data->gtype_name))
741 ret = g_typelib_get_dir_entry_by_gtype_name (typelib, data->gtype_name);
744 data->result_typelib = typelib;
753 * g_irepository_find_by_gtype:
754 * @repository: (allow-none): A #GIRepository or %NULL for the singleton
755 * process-global default #GIRepository
756 * @gtype: GType to search for
758 * Searches all loaded namespaces for a particular #GType. Note that
759 * in order to locate the metadata, the namespace corresponding to
760 * the type must first have been loaded. There is currently no
761 * mechanism for determining the namespace which corresponds to an
762 * arbitrary GType - thus, this function will operate most reliably
763 * when you know the GType to originate from be from a loaded namespace.
765 * Returns: (transfer full): #GIBaseInfo representing metadata about @type, or %NULL
768 g_irepository_find_by_gtype (GIRepository *repository,
771 FindByGTypeData data;
775 repository = get_repository (repository);
777 cached = g_hash_table_lookup (repository->priv->info_by_gtype,
781 return g_base_info_ref (cached);
783 data.gtype_name = g_type_name (gtype);
784 data.result_typelib = NULL;
786 /* Inside each typelib, we include the "C prefix" which acts as
787 * a namespace mechanism. For GtkTreeView, the C prefix is Gtk.
788 * Given the assumption that GTypes for a library also use the
789 * C prefix, we know we can skip examining a typelib if our
790 * target type does not have this typelib's C prefix. Use this
791 * assumption as our first attempt at locating the DirEntry.
793 entry = find_by_gtype (repository->priv->typelibs, &data, TRUE);
795 entry = find_by_gtype (repository->priv->lazy_typelibs, &data, TRUE);
797 /* Not ever class library necessarily specifies a correct c_prefix,
798 * so take a second pass. This time we will try a global lookup,
800 * See http://bugzilla.gnome.org/show_bug.cgi?id=564016
803 entry = find_by_gtype (repository->priv->typelibs, &data, FALSE);
805 entry = find_by_gtype (repository->priv->lazy_typelibs, &data, FALSE);
809 cached = _g_info_new_full (entry->blob_type,
811 NULL, data.result_typelib, entry->offset);
813 g_hash_table_insert (repository->priv->info_by_gtype,
815 g_base_info_ref (cached));
822 * g_irepository_find_by_name:
823 * @repository: (allow-none): A #GIRepository or %NULL for the singleton
824 * process-global default #GIRepository
825 * @namespace_: Namespace which will be searched
826 * @name: Entry name to find
828 * Searches for a particular entry in a namespace. Before calling
829 * this function for a particular namespace, you must call
830 * g_irepository_require() once to load the namespace, or otherwise
831 * ensure the namespace has already been loaded.
833 * Returns: (transfer full): #GIBaseInfo representing metadata about @name, or %NULL
836 g_irepository_find_by_name (GIRepository *repository,
837 const gchar *namespace,
843 g_return_val_if_fail (namespace != NULL, NULL);
845 repository = get_repository (repository);
846 typelib = get_registered (repository, namespace, NULL);
847 g_return_val_if_fail (typelib != NULL, NULL);
849 entry = g_typelib_get_dir_entry_by_name (typelib, name);
852 return _g_info_new_full (entry->blob_type,
854 NULL, typelib, entry->offset);
858 GIRepository *repository;
861 GITypelib *result_typelib;
863 } FindByErrorDomainData;
866 find_by_error_domain_foreach (gpointer key,
870 GITypelib *typelib = (GITypelib*)value;
871 FindByErrorDomainData *data = datap;
873 if (data->result != NULL)
876 data->result = g_typelib_get_dir_entry_by_error_domain (typelib, data->domain);
878 data->result_typelib = typelib;
882 * g_irepository_find_by_error_domain:
883 * @repository: (allow-none): A #GIRepository or %NULL for the singleton
884 * process-global default #GIRepository
885 * @domain: a #GError domain
887 * Searches for the enum type corresponding to the given #GError
888 * domain. Before calling this function for a particular namespace,
889 * you must call g_irepository_require() once to load the namespace, or
890 * otherwise ensure the namespace has already been loaded.
892 * Returns: (transfer full): #GIEnumInfo representing metadata about @domain's
893 * enum type, or %NULL
897 g_irepository_find_by_error_domain (GIRepository *repository,
900 FindByErrorDomainData data;
903 repository = get_repository (repository);
905 cached = g_hash_table_lookup (repository->priv->info_by_error_domain,
906 GUINT_TO_POINTER (domain));
909 return g_base_info_ref ((GIBaseInfo *)cached);
911 data.repository = repository;
912 data.domain = domain;
913 data.result_typelib = NULL;
916 g_hash_table_foreach (repository->priv->typelibs, find_by_error_domain_foreach, &data);
917 if (data.result == NULL)
918 g_hash_table_foreach (repository->priv->lazy_typelibs, find_by_error_domain_foreach, &data);
920 if (data.result != NULL)
922 cached = _g_info_new_full (data.result->blob_type,
924 NULL, data.result_typelib, data.result->offset);
926 g_hash_table_insert (repository->priv->info_by_error_domain,
927 GUINT_TO_POINTER (domain),
928 g_base_info_ref (cached));
935 collect_namespaces (gpointer key,
941 *list = g_list_append (*list, key);
945 * g_irepository_get_loaded_namespaces:
946 * @repository: (allow-none): A #GIRepository or %NULL for the singleton
947 * process-global default #GIRepository
949 * Return the list of currently loaded namespaces.
951 * Returns: (element-type utf8) (transfer full): List of namespaces
954 g_irepository_get_loaded_namespaces (GIRepository *repository)
956 GList *l, *list = NULL;
960 repository = get_repository (repository);
962 g_hash_table_foreach (repository->priv->typelibs, collect_namespaces, &list);
963 g_hash_table_foreach (repository->priv->lazy_typelibs, collect_namespaces, &list);
965 names = g_malloc0 (sizeof (gchar *) * (g_list_length (list) + 1));
967 for (l = list; l; l = l->next)
968 names[i++] = g_strdup (l->data);
975 * g_irepository_get_version:
976 * @repository: (allow-none): A #GIRepository or %NULL for the singleton
977 * process-global default #GIRepository
978 * @namespace_: Namespace to inspect
980 * This function returns the loaded version associated with the given
981 * namespace @namespace_.
983 * Note: The namespace must have already been loaded using a function
984 * such as g_irepository_require() before calling this function.
986 * Returns: Loaded version
989 g_irepository_get_version (GIRepository *repository,
990 const gchar *namespace)
995 g_return_val_if_fail (namespace != NULL, NULL);
997 repository = get_repository (repository);
999 typelib = get_registered (repository, namespace, NULL);
1001 g_return_val_if_fail (typelib != NULL, NULL);
1003 header = (Header *) typelib->data;
1004 return g_typelib_get_string (typelib, header->nsversion);
1008 * g_irepository_get_shared_library:
1009 * @repository: (allow-none): A #GIRepository or %NULL for the singleton
1010 * process-global default #GIRepository
1011 * @namespace_: Namespace to inspect
1013 * This function returns a comma-separated list of paths to the
1014 * shared C libraries associated with the given namespace @namespace_.
1015 * There may be no shared library path associated, in which case this
1016 * function will return %NULL.
1018 * Note: The namespace must have already been loaded using a function
1019 * such as g_irepository_require() before calling this function.
1021 * Returns: Comma-separated list of paths to shared libraries,
1022 * or %NULL if none are associated
1025 g_irepository_get_shared_library (GIRepository *repository,
1026 const gchar *namespace)
1031 g_return_val_if_fail (namespace != NULL, NULL);
1033 repository = get_repository (repository);
1035 typelib = get_registered (repository, namespace, NULL);
1037 g_return_val_if_fail (typelib != NULL, NULL);
1039 header = (Header *) typelib->data;
1040 if (header->shared_library)
1041 return g_typelib_get_string (typelib, header->shared_library);
1047 * g_irepository_get_c_prefix:
1048 * @repository: (allow-none): A #GIRepository or %NULL for the singleton
1049 * process-global default #GIRepository
1050 * @namespace_: Namespace to inspect
1052 * This function returns the "C prefix", or the C level namespace
1053 * associated with the given introspection namespace. Each C symbol
1054 * starts with this prefix, as well each #GType in the library.
1056 * Note: The namespace must have already been loaded using a function
1057 * such as g_irepository_require() before calling this function.
1059 * Returns: C namespace prefix, or %NULL if none associated
1062 g_irepository_get_c_prefix (GIRepository *repository,
1063 const gchar *namespace_)
1068 g_return_val_if_fail (namespace_ != NULL, NULL);
1070 repository = get_repository (repository);
1072 typelib = get_registered (repository, namespace_, NULL);
1074 g_return_val_if_fail (typelib != NULL, NULL);
1076 header = (Header *) typelib->data;
1077 if (header->c_prefix)
1078 return g_typelib_get_string (typelib, header->c_prefix);
1084 * g_irepository_get_typelib_path:
1085 * @repository: (allow-none): A #GIRepository or %NULL for the singleton
1086 * process-global default #GIRepository
1087 * @namespace_: GI namespace to use, e.g. "Gtk"
1089 * If namespace @namespace_ is loaded, return the full path to the
1090 * .typelib file it was loaded from. If the typelib for
1091 * namespace @namespace_ was included in a shared library, return
1092 * the special string "<builtin>".
1094 * Returns: Filesystem path (or $lt;builtin$gt;) if successful, %NULL if namespace is not loaded
1098 g_irepository_get_typelib_path (GIRepository *repository,
1099 const gchar *namespace)
1101 gpointer orig_key, value;
1103 repository = get_repository (repository);
1105 if (!g_hash_table_lookup_extended (repository->priv->typelibs, namespace,
1108 if (!g_hash_table_lookup_extended (repository->priv->lazy_typelibs, namespace,
1113 return ((char*)orig_key) + strlen ((char *) orig_key) + 1;
1116 /* This simple search function looks for a specified namespace-version;
1117 it's faster than the full directory listing required for latest version. */
1118 static GMappedFile *
1119 find_namespace_version (const gchar *namespace,
1120 const gchar *version,
1121 GSList *search_path,
1125 GError *error = NULL;
1126 GMappedFile *mfile = NULL;
1129 fname = g_strdup_printf ("%s-%s.typelib", namespace, version);
1131 for (ldir = search_path; ldir; ldir = ldir->next)
1133 char *path = g_build_filename (ldir->data, fname, NULL);
1135 mfile = g_mapped_file_new (path, FALSE, &error);
1139 g_clear_error (&error);
1150 parse_version (const char *version,
1157 *major = strtol (version, &end, 10);
1158 dot = strchr (version, '.');
1166 *minor = strtol (dot+1, &end, 10);
1167 if (end != (version + strlen (version)))
1173 compare_version (const char *v1,
1177 int v1_major, v1_minor;
1178 int v2_major, v2_minor;
1180 success = parse_version (v1, &v1_major, &v1_minor);
1183 success = parse_version (v2, &v2_major, &v2_minor);
1186 if (v1_major > v2_major)
1188 else if (v2_major > v1_major)
1190 else if (v1_minor > v2_minor)
1192 else if (v2_minor > v1_minor)
1197 struct NamespaceVersionCandidadate
1206 compare_candidate_reverse (struct NamespaceVersionCandidadate *c1,
1207 struct NamespaceVersionCandidadate *c2)
1209 int result = compare_version (c1->version, c2->version);
1210 /* First, check the version */
1213 else if (result < 0)
1217 /* Now check the path index, which says how early in the search path
1218 * we found it. This ensures that of equal version targets, we
1219 * pick the earlier one.
1221 if (c1->path_index == c2->path_index)
1223 else if (c1->path_index > c2->path_index)
1231 free_candidate (struct NamespaceVersionCandidadate *candidate)
1233 g_mapped_file_unref (candidate->mfile);
1234 g_free (candidate->path);
1235 g_free (candidate->version);
1236 g_slice_free (struct NamespaceVersionCandidadate, candidate);
1240 enumerate_namespace_versions (const gchar *namespace,
1241 GSList *search_path)
1243 GSList *candidates = NULL;
1244 GHashTable *found_versions = g_hash_table_new (g_str_hash, g_str_equal);
1245 char *namespace_dash;
1246 char *namespace_typelib;
1248 GError *error = NULL;
1251 namespace_dash = g_strdup_printf ("%s-", namespace);
1252 namespace_typelib = g_strdup_printf ("%s.typelib", namespace);
1255 for (ldir = search_path; ldir; ldir = ldir->next)
1258 const char *dirname;
1261 dirname = (const char*)ldir->data;
1262 dir = g_dir_open (dirname, 0, NULL);
1265 while ((entry = g_dir_read_name (dir)) != NULL)
1268 char *path, *version;
1269 struct NamespaceVersionCandidadate *candidate;
1271 if (!g_str_has_suffix (entry, ".typelib"))
1274 if (g_str_has_prefix (entry, namespace_dash))
1276 const char *last_dash;
1277 const char *name_end;
1280 name_end = strrchr (entry, '.');
1281 last_dash = strrchr (entry, '-');
1282 version = g_strndup (last_dash+1, name_end-(last_dash+1));
1283 if (!parse_version (version, &major, &minor))
1292 if (g_hash_table_lookup (found_versions, version) != NULL)
1297 g_hash_table_insert (found_versions, version, version);
1299 path = g_build_filename (dirname, entry, NULL);
1300 mfile = g_mapped_file_new (path, FALSE, &error);
1305 g_clear_error (&error);
1308 candidate = g_slice_new0 (struct NamespaceVersionCandidadate);
1309 candidate->mfile = mfile;
1310 candidate->path_index = index;
1311 candidate->path = path;
1312 candidate->version = version;
1313 candidates = g_slist_prepend (candidates, candidate);
1319 g_free (namespace_dash);
1320 g_free (namespace_typelib);
1321 g_hash_table_destroy (found_versions);
1326 static GMappedFile *
1327 find_namespace_latest (const gchar *namespace,
1328 GSList *search_path,
1329 gchar **version_ret,
1333 GMappedFile *result = NULL;
1335 *version_ret = NULL;
1338 candidates = enumerate_namespace_versions (namespace, search_path);
1340 if (candidates != NULL)
1342 struct NamespaceVersionCandidadate *elected;
1343 candidates = g_slist_sort (candidates, (GCompareFunc) compare_candidate_reverse);
1345 elected = (struct NamespaceVersionCandidadate *) candidates->data;
1346 /* Remove the elected one so we don't try to free its contents */
1347 candidates = g_slist_delete_link (candidates, candidates);
1349 result = elected->mfile;
1350 *path_ret = elected->path;
1351 *version_ret = elected->version;
1352 g_slice_free (struct NamespaceVersionCandidadate, elected); /* just free the container */
1353 g_slist_foreach (candidates, (GFunc) free_candidate, NULL);
1354 g_slist_free (candidates);
1360 * g_irepository_enumerate_versions:
1361 * @repository: (allow-none): A #GIRepository or %NULL for the singleton
1362 * process-global default #GIRepository
1363 * @namespace_: GI namespace, e.g. "Gtk"
1365 * Obtain an unordered list of versions (either currently loaded or
1366 * available) for @namespace_ in this @repository.
1368 * Returns: (element-type utf8) (transfer full): the array of versions.
1371 g_irepository_enumerate_versions (GIRepository *repository,
1372 const gchar *namespace_)
1375 GSList *candidates, *link;
1376 const gchar *loaded_version;
1379 candidates = enumerate_namespace_versions (namespace_, search_path);
1381 for (link = candidates; link; link = link->next)
1383 struct NamespaceVersionCandidadate *candidate = link->data;
1384 ret = g_list_prepend (ret, g_strdup (candidate->version));
1385 free_candidate (candidate);
1387 g_slist_free (candidates);
1389 /* The currently loaded version of a namespace is also part of the
1390 * available versions, as it could have been loaded using
1391 * require_private().
1393 if (g_irepository_is_registered (repository, namespace_, NULL))
1395 loaded_version = g_irepository_get_version (repository, namespace_);
1396 if (loaded_version && !g_list_find_custom (ret, loaded_version, g_str_equal))
1397 ret = g_list_prepend (ret, g_strdup (loaded_version));
1404 require_internal (GIRepository *repository,
1405 const gchar *namespace,
1406 const gchar *version,
1407 GIRepositoryLoadFlags flags,
1408 GSList *search_path,
1412 GITypelib *ret = NULL;
1414 GITypelib *typelib = NULL;
1415 const gchar *typelib_namespace, *typelib_version;
1416 gboolean allow_lazy = (flags & G_IREPOSITORY_LOAD_FLAG_LAZY) > 0;
1418 char *version_conflict = NULL;
1420 char *tmp_version = NULL;
1422 g_return_val_if_fail (namespace != NULL, FALSE);
1424 repository = get_repository (repository);
1426 typelib = get_registered_status (repository, namespace, version, allow_lazy,
1427 &is_lazy, &version_conflict);
1431 if (version_conflict != NULL)
1433 g_set_error (error, G_IREPOSITORY_ERROR,
1434 G_IREPOSITORY_ERROR_NAMESPACE_VERSION_CONFLICT,
1435 "Requiring namespace '%s' version '%s', but '%s' is already loaded",
1436 namespace, version, version_conflict);
1440 if (version != NULL)
1442 mfile = find_namespace_version (namespace, version,
1443 search_path, &path);
1444 tmp_version = g_strdup (version);
1448 mfile = find_namespace_latest (namespace, search_path,
1449 &tmp_version, &path);
1454 if (version != NULL)
1455 g_set_error (error, G_IREPOSITORY_ERROR,
1456 G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
1457 "Typelib file for namespace '%s', version '%s' not found",
1458 namespace, version);
1460 g_set_error (error, G_IREPOSITORY_ERROR,
1461 G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
1462 "Typelib file for namespace '%s' (any version) not found",
1468 GError *temp_error = NULL;
1469 typelib = g_typelib_new_from_mapped_file (mfile, &temp_error);
1472 g_set_error (error, G_IREPOSITORY_ERROR,
1473 G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
1474 "Failed to load typelib file '%s' for namespace '%s': %s",
1475 path, namespace, temp_error->message);
1476 g_clear_error (&temp_error);
1480 header = (Header *) typelib->data;
1481 typelib_namespace = g_typelib_get_string (typelib, header->namespace);
1482 typelib_version = g_typelib_get_string (typelib, header->nsversion);
1484 if (strcmp (typelib_namespace, namespace) != 0)
1486 g_set_error (error, G_IREPOSITORY_ERROR,
1487 G_IREPOSITORY_ERROR_NAMESPACE_MISMATCH,
1488 "Typelib file %s for namespace '%s' contains "
1489 "namespace '%s' which doesn't match the file name",
1490 path, namespace, typelib_namespace);
1491 g_typelib_free (typelib);
1494 if (version != NULL && strcmp (typelib_version, version) != 0)
1496 g_set_error (error, G_IREPOSITORY_ERROR,
1497 G_IREPOSITORY_ERROR_NAMESPACE_MISMATCH,
1498 "Typelib file %s for namespace '%s' contains "
1499 "version '%s' which doesn't match the expected version '%s'",
1500 path, namespace, typelib_version, version);
1501 g_typelib_free (typelib);
1505 if (!register_internal (repository, path, allow_lazy,
1508 g_typelib_free (typelib);
1513 g_free (tmp_version);
1519 * g_irepository_require:
1520 * @repository: (allow-none): A #GIRepository or %NULL for the singleton
1521 * process-global default #GIRepository
1522 * @namespace_: GI namespace to use, e.g. "Gtk"
1523 * @version: (allow-none): Version of namespace, may be %NULL for latest
1524 * @flags: Set of %GIRepositoryLoadFlags, may be 0
1525 * @error: a #GError.
1527 * Force the namespace @namespace_ to be loaded if it isn't already.
1528 * If @namespace_ is not loaded, this function will search for a
1529 * ".typelib" file using the repository search path. In addition, a
1530 * version @version of namespace may be specified. If @version is
1531 * not specified, the latest will be used.
1533 * Returns: (transfer none): a pointer to the #GITypelib if successful, %NULL otherwise
1536 g_irepository_require (GIRepository *repository,
1537 const gchar *namespace,
1538 const gchar *version,
1539 GIRepositoryLoadFlags flags,
1545 typelib = require_internal (repository, namespace, version, flags,
1546 search_path, error);
1552 * g_irepository_require_private:
1553 * @repository: (allow-none): A #GIRepository or %NULL for the singleton
1554 * process-global default #GIRepository
1555 * @typelib_dir: Private directory where to find the requested typelib
1556 * @namespace_: GI namespace to use, e.g. "Gtk"
1557 * @version: (allow-none): Version of namespace, may be %NULL for latest
1558 * @flags: Set of %GIRepositoryLoadFlags, may be 0
1559 * @error: a #GError.
1561 * Force the namespace @namespace_ to be loaded if it isn't already.
1562 * If @namespace_ is not loaded, this function will search for a
1563 * ".typelib" file within the private directory only. In addition, a
1564 * version @version of namespace should be specified. If @version is
1565 * not specified, the latest will be used.
1567 * Returns: (transfer none): a pointer to the #GITypelib if successful, %NULL otherwise
1570 g_irepository_require_private (GIRepository *repository,
1571 const gchar *typelib_dir,
1572 const gchar *namespace,
1573 const gchar *version,
1574 GIRepositoryLoadFlags flags,
1577 GSList search_path = { (gpointer) typelib_dir, NULL };
1579 return require_internal (repository, namespace, version, flags,
1580 &search_path, error);
1584 g_irepository_introspect_cb (const char *option_name,
1589 GError *tmp_error = NULL;
1590 gboolean ret = g_irepository_dump (value, &tmp_error);
1593 g_error ("Failed to extract GType data: %s",
1594 tmp_error->message);
1600 static const GOptionEntry introspection_args[] = {
1601 { "introspect-dump", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK,
1602 g_irepository_introspect_cb, "Dump introspection information",
1603 "infile.txt,outfile.xml" },
1608 * g_irepository_get_option_group: (skip)
1610 * Obtain the option group for girepository, it's used
1611 * by the dumper and for programs that wants to provide
1612 * introspection information
1614 * Returns: (transfer full): the option group
1617 g_irepository_get_option_group (void)
1619 GOptionGroup *group;
1620 group = g_option_group_new ("girepository", "Introspection Options", "Show Introspection Options", NULL, NULL);
1622 g_option_group_add_entries (group, introspection_args);
1627 g_irepository_error_quark (void)
1629 static GQuark quark = 0;
1631 quark = g_quark_from_static_string ("g-irepository-error-quark");
1636 * g_type_tag_to_string:
1637 * @type: the type_tag
1639 * Obtain a string representation of @type
1641 * Returns: the string
1644 g_type_tag_to_string (GITypeTag type)
1648 case GI_TYPE_TAG_VOID:
1650 case GI_TYPE_TAG_BOOLEAN:
1652 case GI_TYPE_TAG_INT8:
1654 case GI_TYPE_TAG_UINT8:
1656 case GI_TYPE_TAG_INT16:
1658 case GI_TYPE_TAG_UINT16:
1660 case GI_TYPE_TAG_INT32:
1662 case GI_TYPE_TAG_UINT32:
1664 case GI_TYPE_TAG_INT64:
1666 case GI_TYPE_TAG_UINT64:
1668 case GI_TYPE_TAG_FLOAT:
1670 case GI_TYPE_TAG_DOUBLE:
1672 case GI_TYPE_TAG_UNICHAR:
1674 case GI_TYPE_TAG_GTYPE:
1676 case GI_TYPE_TAG_UTF8:
1678 case GI_TYPE_TAG_FILENAME:
1680 case GI_TYPE_TAG_ARRAY:
1682 case GI_TYPE_TAG_INTERFACE:
1684 case GI_TYPE_TAG_GLIST:
1686 case GI_TYPE_TAG_GSLIST:
1688 case GI_TYPE_TAG_GHASH:
1690 case GI_TYPE_TAG_ERROR:
1698 * g_info_type_to_string:
1699 * @type: the info type
1701 * Obtain a string representation of @type
1703 * Returns: the string
1706 g_info_type_to_string (GIInfoType type)
1710 case GI_INFO_TYPE_INVALID:
1712 case GI_INFO_TYPE_FUNCTION:
1714 case GI_INFO_TYPE_CALLBACK:
1716 case GI_INFO_TYPE_STRUCT:
1718 case GI_INFO_TYPE_BOXED:
1720 case GI_INFO_TYPE_ENUM:
1722 case GI_INFO_TYPE_FLAGS:
1724 case GI_INFO_TYPE_OBJECT:
1726 case GI_INFO_TYPE_INTERFACE:
1728 case GI_INFO_TYPE_CONSTANT:
1730 case GI_INFO_TYPE_UNION:
1732 case GI_INFO_TYPE_VALUE:
1734 case GI_INFO_TYPE_SIGNAL:
1736 case GI_INFO_TYPE_VFUNC:
1738 case GI_INFO_TYPE_PROPERTY:
1740 case GI_INFO_TYPE_FIELD:
1742 case GI_INFO_TYPE_ARG:
1744 case GI_INFO_TYPE_TYPE:
1746 case GI_INFO_TYPE_UNRESOLVED:
1747 return "unresolved";