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.
29 #include <glib/gprintf.h>
31 #include "girepository.h"
32 #include "gitypelib-internal.h"
33 #include "girepository-private.h"
34 #include "glib-compat.h"
38 static GStaticMutex globals_lock = G_STATIC_MUTEX_INIT;
39 static GIRepository *default_repository = NULL;
40 static GSList *search_path = NULL;
41 static GSList *override_search_path = NULL;
43 struct _GIRepositoryPrivate
45 GHashTable *typelibs; /* (string) namespace -> GITypelib */
46 GHashTable *lazy_typelibs; /* (string) namespace-version -> GITypelib */
47 GHashTable *info_by_gtype; /* GType -> GIBaseInfo */
48 GHashTable *info_by_error_domain; /* GQuark -> GIBaseInfo */
51 G_DEFINE_TYPE (GIRepository, g_irepository, G_TYPE_OBJECT);
53 #ifdef G_PLATFORM_WIN32
57 static HMODULE girepository_dll = NULL;
62 DllMain (HINSTANCE hinstDLL,
66 if (fdwReason == DLL_PROCESS_ATTACH)
67 girepository_dll = hinstDLL;
74 #undef GOBJECT_INTROSPECTION_LIBDIR
76 /* GOBJECT_INTROSPECTION_LIBDIR is used only in code called just once,
77 * so no problem leaking this
79 #define GOBJECT_INTROSPECTION_LIBDIR \
80 g_build_filename (g_win32_get_package_installation_directory_of_module (girepository_dll), \
87 g_irepository_init (GIRepository *repository)
89 repository->priv = G_TYPE_INSTANCE_GET_PRIVATE (repository, G_TYPE_IREPOSITORY,
91 repository->priv->typelibs
92 = g_hash_table_new_full (g_str_hash, g_str_equal,
93 (GDestroyNotify) NULL,
94 (GDestroyNotify) g_typelib_free);
95 repository->priv->lazy_typelibs
96 = g_hash_table_new_full (g_str_hash, g_str_equal,
97 (GDestroyNotify) g_free,
98 (GDestroyNotify) NULL);
99 repository->priv->info_by_gtype
100 = g_hash_table_new_full (g_direct_hash, g_direct_equal,
101 (GDestroyNotify) NULL,
102 (GDestroyNotify) g_base_info_unref);
103 repository->priv->info_by_error_domain
104 = g_hash_table_new_full (g_direct_hash, g_direct_equal,
105 (GDestroyNotify) NULL,
106 (GDestroyNotify) g_base_info_unref);
110 g_irepository_finalize (GObject *object)
112 GIRepository *repository = G_IREPOSITORY (object);
114 g_hash_table_destroy (repository->priv->typelibs);
115 g_hash_table_destroy (repository->priv->lazy_typelibs);
116 g_hash_table_destroy (repository->priv->info_by_gtype);
117 g_hash_table_destroy (repository->priv->info_by_error_domain);
119 (* G_OBJECT_CLASS (g_irepository_parent_class)->finalize) (G_OBJECT (repository));
123 g_irepository_class_init (GIRepositoryClass *class)
125 GObjectClass *gobject_class;
127 gobject_class = G_OBJECT_CLASS (class);
129 gobject_class->finalize = g_irepository_finalize;
131 g_type_class_add_private (class, sizeof (GIRepositoryPrivate));
137 g_static_mutex_lock (&globals_lock);
139 if (default_repository == NULL)
141 default_repository = g_object_new (G_TYPE_IREPOSITORY, NULL);
144 if (search_path == NULL)
148 const gchar *type_lib_path_env;
150 /* This variable is intended to take precedence over both the default
151 * search path, as well as anything written into code with g_irepository_prepend_search_path.
153 type_lib_path_env = g_getenv ("GI_TYPELIB_PATH");
156 override_search_path = NULL;
157 if (type_lib_path_env)
162 custom_dirs = g_strsplit (type_lib_path_env, G_SEARCHPATH_SEPARATOR_S, 0);
167 override_search_path = g_slist_prepend (override_search_path, *d);
171 /* ownership of the array content was passed to the list */
172 g_free (custom_dirs);
175 if (override_search_path != NULL)
176 override_search_path = g_slist_reverse (override_search_path);
178 libdir = GOBJECT_INTROSPECTION_LIBDIR;
180 typelib_dir = g_build_filename (libdir, "girepository-1.0", NULL);
182 search_path = g_slist_prepend (search_path, typelib_dir);
184 search_path = g_slist_reverse (search_path);
187 g_static_mutex_unlock (&globals_lock);
191 g_irepository_prepend_search_path (const char *directory)
194 search_path = g_slist_prepend (search_path, g_strdup (directory));
198 * g_irepository_get_search_path:
200 * Returns the search path the GIRepository will use when looking for typelibs.
201 * The string is internal to GIRespository and should not be freed, nor should
204 * Return value: (element-type filename) (transfer none): list of strings
207 g_irepository_get_search_path (void)
213 build_search_path_with_overrides (void)
219 if (override_search_path != NULL)
221 result = g_slist_copy (override_search_path);
222 g_slist_last (result)->next = g_slist_copy (search_path);
225 result = g_slist_copy (search_path);
230 build_typelib_key (const char *name, const char *source)
232 GString *str = g_string_new (name);
233 g_string_append_c (str, '\0');
234 g_string_append (str, source);
235 return g_string_free (str, FALSE);
239 get_typelib_dependencies (GITypelib *typelib)
242 const char *dependencies_glob;
244 header = (Header *)typelib->data;
246 if (header->dependencies == 0)
249 dependencies_glob = g_typelib_get_string (typelib, header->dependencies);
250 return g_strsplit (dependencies_glob, "|", 0);
253 static GIRepository *
254 get_repository (GIRepository *repository)
258 if (repository != NULL)
261 return default_repository;
265 check_version_conflict (GITypelib *typelib,
266 const gchar *namespace,
267 const gchar *expected_version,
268 char **version_conflict)
271 const char *loaded_version;
273 if (expected_version == NULL)
275 if (version_conflict)
276 *version_conflict = NULL;
280 header = (Header*)typelib->data;
281 loaded_version = g_typelib_get_string (typelib, header->nsversion);
282 g_assert (loaded_version != NULL);
284 if (strcmp (expected_version, loaded_version) != 0)
286 if (version_conflict)
287 *version_conflict = (char*)loaded_version;
290 if (version_conflict)
291 *version_conflict = NULL;
296 get_registered_status (GIRepository *repository,
297 const char *namespace,
300 gboolean *lazy_status,
301 char **version_conflict)
304 repository = get_repository (repository);
306 *lazy_status = FALSE;
307 typelib = g_hash_table_lookup (repository->priv->typelibs, namespace);
309 return check_version_conflict (typelib, namespace, version, version_conflict);
310 typelib = g_hash_table_lookup (repository->priv->lazy_typelibs, namespace);
317 return check_version_conflict (typelib, namespace, version, version_conflict);
321 get_registered (GIRepository *repository,
322 const char *namespace,
325 return get_registered_status (repository, namespace, version, TRUE, NULL, NULL);
329 load_dependencies_recurse (GIRepository *repository,
335 dependencies = get_typelib_dependencies (typelib);
337 if (dependencies != NULL)
341 for (i = 0; dependencies[i]; i++)
343 char *dependency = dependencies[i];
344 const char *last_dash;
345 char *dependency_namespace;
346 const char *dependency_version;
348 last_dash = strrchr (dependency, '-');
349 dependency_namespace = g_strndup (dependency, last_dash - dependency);
350 dependency_version = last_dash+1;
352 if (!g_irepository_require (repository, dependency_namespace, dependency_version,
355 g_free (dependency_namespace);
356 g_strfreev (dependencies);
359 g_free (dependency_namespace);
361 g_strfreev (dependencies);
367 register_internal (GIRepository *repository,
374 const gchar *namespace;
375 const gchar *version;
377 g_return_val_if_fail (typelib != NULL, FALSE);
379 header = (Header *)typelib->data;
381 g_return_val_if_fail (header != NULL, FALSE);
383 namespace = g_typelib_get_string (typelib, header->namespace);
384 version = g_typelib_get_string (typelib, header->nsversion);
388 g_assert (!g_hash_table_lookup (repository->priv->lazy_typelibs,
390 g_hash_table_insert (repository->priv->lazy_typelibs,
391 build_typelib_key (namespace, source), (void *)typelib);
398 /* First, try loading all the dependencies */
399 if (!load_dependencies_recurse (repository, typelib, error))
402 /* Check if we are transitioning from lazily loaded state */
403 if (g_hash_table_lookup_extended (repository->priv->lazy_typelibs,
405 (gpointer)&key, &value))
406 g_hash_table_remove (repository->priv->lazy_typelibs, key);
408 key = build_typelib_key (namespace, source);
410 g_hash_table_insert (repository->priv->typelibs, key, (void *)typelib);
417 * g_irepository_get_dependencies:
418 * @repository: (allow-none): A #GIRepository, may be %NULL for the default
419 * @namespace_: Namespace of interest
421 * Return an array of all (transitive) dependencies for namespace
422 * @namespace_, including version. The returned strings are of the
423 * form <code>namespace-version</code>.
425 * Note: The namespace must have already been loaded using a function
426 * such as g_irepository_require() before calling this function.
428 * Returns: (transfer full): Zero-terminated string array of versioned dependencies
431 g_irepository_get_dependencies (GIRepository *repository,
432 const char *namespace)
436 g_return_val_if_fail (namespace != NULL, NULL);
438 repository = get_repository (repository);
440 typelib = get_registered (repository, namespace, NULL);
441 g_return_val_if_fail (typelib != NULL, NULL);
443 return get_typelib_dependencies (typelib);
447 g_irepository_load_typelib (GIRepository *repository,
449 GIRepositoryLoadFlags flags,
453 const char *namespace;
454 const char *nsversion;
455 gboolean allow_lazy = flags & G_IREPOSITORY_LOAD_FLAG_LAZY;
457 char *version_conflict;
459 repository = get_repository (repository);
461 header = (Header *) typelib->data;
462 namespace = g_typelib_get_string (typelib, header->namespace);
463 nsversion = g_typelib_get_string (typelib, header->nsversion);
465 if (get_registered_status (repository, namespace, nsversion, allow_lazy,
466 &is_lazy, &version_conflict))
468 if (version_conflict != NULL)
470 g_set_error (error, G_IREPOSITORY_ERROR,
471 G_IREPOSITORY_ERROR_NAMESPACE_VERSION_CONFLICT,
472 "Attempting to load namespace '%s', version '%s', but '%s' is already loaded",
473 namespace, nsversion, version_conflict);
478 return register_internal (repository, "<builtin>",
479 allow_lazy, typelib, error);
483 * g_irepository_is_registered:
484 * @repository: (allow-none): A #GIRepository, may be %NULL for the default
485 * @namespace_: Namespace of interest
486 * @version: (allow-none): Required version, may be %NULL for latest
488 * Check whether a particular namespace (and optionally, a specific
489 * version thereof) is currently loaded. This function is likely to
490 * only be useful in unusual circumstances; in order to act upon
491 * metadata in the namespace, you should call g_irepository_require()
492 * instead which will ensure the namespace is loaded, and return as
493 * quickly as this function will if it has already been loaded.
495 * Returns: %TRUE if namespace-version is loaded, %FALSE otherwise
498 g_irepository_is_registered (GIRepository *repository,
499 const gchar *namespace,
500 const gchar *version)
502 repository = get_repository (repository);
503 return get_registered (repository, namespace, version) != NULL;
507 * g_irepository_get_default:
509 * Returns the singleton process-global default #GIRepository. It is
510 * not currently supported to have multiple repositories in a
511 * particular process, but this function is provided in the unlikely
512 * eventuality that it would become possible, and as a convenience for
513 * higher level language bindings to conform to the GObject method
516 * All methods on #GIRepository also accept %NULL as an instance
517 * parameter to mean this default repository, which is usually more
520 * Returns: (transfer none): The global singleton #GIRepository
523 g_irepository_get_default (void)
525 return get_repository (NULL);
529 * g_irepository_get_n_infos:
530 * @repository: (allow-none): A #GIRepository, may be %NULL for the default
531 * @namespace_: Namespace to inspect
533 * This function returns the number of metadata entries in
534 * given namespace @namespace_. The namespace must have
535 * already been loaded before calling this function.
537 * Returns: number of metadata entries
540 g_irepository_get_n_infos (GIRepository *repository,
541 const gchar *namespace)
544 gint n_interfaces = 0;
546 g_return_val_if_fail (namespace != NULL, -1);
548 repository = get_repository (repository);
550 typelib = get_registered (repository, namespace, NULL);
552 g_return_val_if_fail (typelib != NULL, -1);
554 n_interfaces = ((Header *)typelib->data)->n_local_entries;
560 * g_irepository_get_info:
561 * @repository: (allow-none): A #GIRepository, may be %NULL for the default
562 * @namespace_: Namespace to inspect
563 * @index: 0-based offset into namespace metadata for entry
565 * This function returns a particular metadata entry in the
566 * given namespace @namespace_. The namespace must have
567 * already been loaded before calling this function.
568 * See g_irepository_get_n_infos() to find the maximum number of
571 * Returns: (transfer full): #GIBaseInfo containing metadata
574 g_irepository_get_info (GIRepository *repository,
575 const gchar *namespace,
581 g_return_val_if_fail (namespace != NULL, NULL);
583 repository = get_repository (repository);
585 typelib = get_registered (repository, namespace, NULL);
587 g_return_val_if_fail (typelib != NULL, NULL);
589 entry = g_typelib_get_dir_entry (typelib, index + 1);
592 return _g_info_new_full (entry->blob_type,
594 NULL, typelib, entry->offset);
598 GIRepository *repository;
602 GITypelib *result_typelib;
607 find_by_gtype_foreach (gpointer key,
611 GITypelib *typelib = (GITypelib*)value;
612 FindByGTypeData *data = datap;
614 if (data->result != NULL)
617 data->result = g_typelib_get_dir_entry_by_gtype (typelib, data->fastpass, data->type);
619 data->result_typelib = typelib;
623 * g_irepository_find_by_gtype:
624 * @repository: (allow-none): A #GIRepository, may be %NULL for the default
625 * @gtype: GType to search for
627 * Searches all loaded namespaces for a particular #GType. Note that
628 * in order to locate the metadata, the namespace corresponding to
629 * the type must first have been loaded. There is currently no
630 * mechanism for determining the namespace which corresponds to an
631 * arbitrary GType - thus, this function will operate most reliably
632 * when you know the GType to originate from be from a loaded namespace.
634 * Returns: (transfer full): #GIBaseInfo representing metadata about @type, or %NULL
637 g_irepository_find_by_gtype (GIRepository *repository,
640 FindByGTypeData data;
643 repository = get_repository (repository);
645 cached = g_hash_table_lookup (repository->priv->info_by_gtype,
649 return g_base_info_ref (cached);
651 data.repository = repository;
652 data.fastpass = TRUE;
654 data.result_typelib = NULL;
657 g_hash_table_foreach (repository->priv->typelibs, find_by_gtype_foreach, &data);
658 if (data.result == NULL)
659 g_hash_table_foreach (repository->priv->lazy_typelibs, find_by_gtype_foreach, &data);
661 /* We do two passes; see comment in find_interface */
662 if (data.result == NULL)
664 data.fastpass = FALSE;
665 g_hash_table_foreach (repository->priv->typelibs, find_by_gtype_foreach, &data);
667 if (data.result == NULL)
668 g_hash_table_foreach (repository->priv->lazy_typelibs, find_by_gtype_foreach, &data);
670 if (data.result != NULL)
672 cached = _g_info_new_full (data.result->blob_type,
674 NULL, data.result_typelib, data.result->offset);
676 g_hash_table_insert (repository->priv->info_by_gtype,
678 g_base_info_ref (cached));
685 * g_irepository_find_by_name:
686 * @repository: (allow-none): A #GIRepository, may be %NULL for the default
687 * @namespace_: Namespace which will be searched
688 * @name: Entry name to find
690 * Searches for a particular entry in a namespace. Before calling
691 * this function for a particular namespace, you must call
692 * g_irepository_require() once to load the namespace, or otherwise
693 * ensure the namespace has already been loaded.
695 * Returns: (transfer full): #GIBaseInfo representing metadata about @name, or %NULL
698 g_irepository_find_by_name (GIRepository *repository,
699 const gchar *namespace,
705 g_return_val_if_fail (namespace != NULL, NULL);
707 repository = get_repository (repository);
708 typelib = get_registered (repository, namespace, NULL);
709 g_return_val_if_fail (typelib != NULL, NULL);
711 entry = g_typelib_get_dir_entry_by_name (typelib, name);
714 return _g_info_new_full (entry->blob_type,
716 NULL, typelib, entry->offset);
720 GIRepository *repository;
723 GITypelib *result_typelib;
725 } FindByErrorDomainData;
728 find_by_error_domain_foreach (gpointer key,
732 GITypelib *typelib = (GITypelib*)value;
733 FindByErrorDomainData *data = datap;
735 if (data->result != NULL)
738 data->result = g_typelib_get_dir_entry_by_error_domain (typelib, data->domain);
740 data->result_typelib = typelib;
744 * g_irepository_find_by_error_domain:
745 * @repository: (allow-none): A #GIRepository, may be %NULL for the default
746 * @domain: a #GError domain
748 * Searches for the enum type corresponding to the given #GError
749 * domain. Before calling this function for a particular namespace,
750 * you must call g_irepository_require() once to load the namespace, or
751 * otherwise ensure the namespace has already been loaded.
753 * Returns: (transfer full): #GIEnumInfo representing metadata about @domain's
754 * enum type, or %NULL
759 g_irepository_find_by_error_domain (GIRepository *repository,
762 FindByErrorDomainData data;
765 repository = get_repository (repository);
767 cached = g_hash_table_lookup (repository->priv->info_by_error_domain,
768 GUINT_TO_POINTER (domain));
771 return g_base_info_ref ((GIBaseInfo *)cached);
773 data.repository = repository;
774 data.domain = domain;
775 data.result_typelib = NULL;
778 g_hash_table_foreach (repository->priv->typelibs, find_by_error_domain_foreach, &data);
779 if (data.result == NULL)
780 g_hash_table_foreach (repository->priv->lazy_typelibs, find_by_error_domain_foreach, &data);
782 if (data.result != NULL)
784 cached = _g_info_new_full (data.result->blob_type,
786 NULL, data.result_typelib, data.result->offset);
788 g_hash_table_insert (repository->priv->info_by_error_domain,
789 GUINT_TO_POINTER (domain),
790 g_base_info_ref (cached));
797 collect_namespaces (gpointer key,
803 *list = g_list_append (*list, key);
807 * g_irepository_get_loaded_namespaces:
808 * @repository: (allow-none): A #GIRepository, may be %NULL for the default
810 * Return the list of currently loaded namespaces.
812 * Returns: (utf8) (transfer full): List of namespaces
815 g_irepository_get_loaded_namespaces (GIRepository *repository)
817 GList *l, *list = NULL;
821 repository = get_repository (repository);
823 g_hash_table_foreach (repository->priv->typelibs, collect_namespaces, &list);
824 g_hash_table_foreach (repository->priv->lazy_typelibs, collect_namespaces, &list);
826 names = g_malloc0 (sizeof (gchar *) * (g_list_length (list) + 1));
828 for (l = list; l; l = l->next)
829 names[i++] = g_strdup (l->data);
836 * g_irepository_get_version:
837 * @repository: (allow-none): A #GIRepository, may be %NULL for the default
838 * @namespace_: Namespace to inspect
840 * This function returns the loaded version associated with the given
841 * namespace @namespace_.
843 * Note: The namespace must have already been loaded using a function
844 * such as g_irepository_require() before calling this function.
846 * Returns: Loaded version
849 g_irepository_get_version (GIRepository *repository,
850 const gchar *namespace)
855 g_return_val_if_fail (namespace != NULL, NULL);
857 repository = get_repository (repository);
859 typelib = get_registered (repository, namespace, NULL);
861 g_return_val_if_fail (typelib != NULL, NULL);
863 header = (Header *) typelib->data;
864 return g_typelib_get_string (typelib, header->nsversion);
868 * g_irepository_get_shared_library:
869 * @repository: (allow-none): A #GIRepository, may be %NULL for the default
870 * @namespace_: Namespace to inspect
872 * This function returns the full path to the shared C library
873 * associated with the given namespace @namespace_. There may be no
874 * shared library path associated, in which case this function will
877 * Note: The namespace must have already been loaded using a function
878 * such as g_irepository_require() before calling this function.
880 * Returns: Full path to shared library, or %NULL if none associated
883 g_irepository_get_shared_library (GIRepository *repository,
884 const gchar *namespace)
889 g_return_val_if_fail (namespace != NULL, NULL);
891 repository = get_repository (repository);
893 typelib = get_registered (repository, namespace, NULL);
895 g_return_val_if_fail (typelib != NULL, NULL);
897 header = (Header *) typelib->data;
898 if (header->shared_library)
899 return g_typelib_get_string (typelib, header->shared_library);
905 * g_irepository_get_c_prefix
906 * @repository: (allow-none): A #GIRepository, may be %NULL for the default
907 * @namespace_: Namespace to inspect
909 * This function returns the "C prefix", or the C level namespace
910 * associated with the given introspection namespace. Each C symbol
911 * starts with this prefix, as well each #GType in the library.
913 * Note: The namespace must have already been loaded using a function
914 * such as g_irepository_require() before calling this function.
916 * Returns: C namespace prefix, or %NULL if none associated
919 g_irepository_get_c_prefix (GIRepository *repository,
920 const gchar *namespace_)
925 g_return_val_if_fail (namespace_ != NULL, NULL);
927 repository = get_repository (repository);
929 typelib = get_registered (repository, namespace_, NULL);
931 g_return_val_if_fail (typelib != NULL, NULL);
933 header = (Header *) typelib->data;
934 if (header->c_prefix)
935 return g_typelib_get_string (typelib, header->c_prefix);
941 * g_irepository_get_typelib_path
942 * @repository: (allow-none): Repository, may be %NULL for the default
943 * @namespace_: GI namespace to use, e.g. "Gtk"
945 * If namespace @namespace_ is loaded, return the full path to the
946 * .typelib file it was loaded from. If the typelib for
947 * namespace @namespace_ was included in a shared library, return
948 * the special string "$lt;builtin$gt;".
950 * Returns: Filesystem path (or $lt;builtin$gt;) if successful, %NULL if namespace is not loaded
954 g_irepository_get_typelib_path (GIRepository *repository,
955 const gchar *namespace)
957 gpointer orig_key, value;
959 repository = get_repository (repository);
961 if (!g_hash_table_lookup_extended (repository->priv->typelibs, namespace,
964 if (!g_hash_table_lookup_extended (repository->priv->lazy_typelibs, namespace,
969 return ((char*)orig_key) + strlen ((char *) orig_key) + 1;
972 /* This simple search function looks for a specified namespace-version;
973 it's faster than the full directory listing required for latest version. */
975 find_namespace_version (const gchar *namespace,
976 const gchar *version,
981 GError *error = NULL;
982 GMappedFile *mfile = NULL;
985 fname = g_strdup_printf ("%s-%s.typelib", namespace, version);
987 for (ldir = search_path; ldir; ldir = ldir->next)
989 char *path = g_build_filename (ldir->data, fname, NULL);
991 mfile = g_mapped_file_new (path, FALSE, &error);
995 g_clear_error (&error);
1006 parse_version (const char *version,
1013 *major = strtol (version, &end, 10);
1014 dot = strchr (version, '.');
1022 *minor = strtol (dot+1, &end, 10);
1023 if (end != (version + strlen (version)))
1029 compare_version (const char *v1,
1033 int v1_major, v1_minor;
1034 int v2_major, v2_minor;
1036 success = parse_version (v1, &v1_major, &v1_minor);
1039 success = parse_version (v2, &v2_major, &v2_minor);
1042 if (v1_major > v2_major)
1044 else if (v2_major > v1_major)
1046 else if (v1_minor > v2_minor)
1048 else if (v2_minor > v1_minor)
1053 struct NamespaceVersionCandidadate
1062 compare_candidate_reverse (struct NamespaceVersionCandidadate *c1,
1063 struct NamespaceVersionCandidadate *c2)
1065 int result = compare_version (c1->version, c2->version);
1066 /* First, check the version */
1069 else if (result < 0)
1073 /* Now check the path index, which says how early in the search path
1074 * we found it. This ensures that of equal version targets, we
1075 * pick the earlier one.
1077 if (c1->path_index == c2->path_index)
1079 else if (c1->path_index > c2->path_index)
1087 free_candidate (struct NamespaceVersionCandidadate *candidate)
1089 g_mapped_file_unref (candidate->mfile);
1090 g_free (candidate->path);
1091 g_free (candidate->version);
1092 g_slice_free (struct NamespaceVersionCandidadate, candidate);
1096 enumerate_namespace_versions (const gchar *namespace,
1097 GSList *search_path)
1099 GSList *candidates = NULL;
1100 GHashTable *found_versions = g_hash_table_new (g_str_hash, g_str_equal);
1101 char *namespace_dash;
1102 char *namespace_typelib;
1104 GError *error = NULL;
1107 namespace_dash = g_strdup_printf ("%s-", namespace);
1108 namespace_typelib = g_strdup_printf ("%s.typelib", namespace);
1111 for (ldir = search_path; ldir; ldir = ldir->next)
1114 const char *dirname;
1117 dirname = (const char*)ldir->data;
1118 dir = g_dir_open (dirname, 0, NULL);
1121 while ((entry = g_dir_read_name (dir)) != NULL)
1124 char *path, *version;
1125 struct NamespaceVersionCandidadate *candidate;
1127 if (!g_str_has_suffix (entry, ".typelib"))
1130 if (g_str_has_prefix (entry, namespace_dash))
1132 const char *last_dash;
1133 const char *name_end;
1136 name_end = strrchr (entry, '.');
1137 last_dash = strrchr (entry, '-');
1138 version = g_strndup (last_dash+1, name_end-(last_dash+1));
1139 if (!parse_version (version, &major, &minor))
1148 if (g_hash_table_lookup (found_versions, version) != NULL)
1153 g_hash_table_insert (found_versions, version, version);
1155 path = g_build_filename (dirname, entry, NULL);
1156 mfile = g_mapped_file_new (path, FALSE, &error);
1161 g_clear_error (&error);
1164 candidate = g_slice_new0 (struct NamespaceVersionCandidadate);
1165 candidate->mfile = mfile;
1166 candidate->path_index = index;
1167 candidate->path = path;
1168 candidate->version = version;
1169 candidates = g_slist_prepend (candidates, candidate);
1175 g_free (namespace_dash);
1176 g_free (namespace_typelib);
1177 g_hash_table_destroy (found_versions);
1182 static GMappedFile *
1183 find_namespace_latest (const gchar *namespace,
1184 GSList *search_path,
1185 gchar **version_ret,
1189 GMappedFile *result = NULL;
1191 *version_ret = NULL;
1194 candidates = enumerate_namespace_versions (namespace, search_path);
1196 if (candidates != NULL)
1198 struct NamespaceVersionCandidadate *elected;
1199 candidates = g_slist_sort (candidates, (GCompareFunc) compare_candidate_reverse);
1201 elected = (struct NamespaceVersionCandidadate *) candidates->data;
1202 /* Remove the elected one so we don't try to free its contents */
1203 candidates = g_slist_delete_link (candidates, candidates);
1205 result = elected->mfile;
1206 *path_ret = elected->path;
1207 *version_ret = elected->version;
1208 g_slice_free (struct NamespaceVersionCandidadate, elected); /* just free the container */
1209 g_slist_foreach (candidates, (GFunc) free_candidate, NULL);
1210 g_slist_free (candidates);
1216 * g_irepository_enumerate_versions:
1217 * @repository: (allow-none): the repository
1218 * @namespace_: GI namespace, e.g. "Gtk"
1220 * Obtain an unordered list of versions (either currently loaded or
1221 * available) for @namespace_ in this @repository.
1223 * Returns: (element-type utf8) (transfer full): the array of versions.
1226 g_irepository_enumerate_versions (GIRepository *repository,
1227 const gchar *namespace_)
1230 GSList *search_path;
1231 GSList *candidates, *link;
1232 const gchar *loaded_version;
1234 search_path = build_search_path_with_overrides ();
1235 candidates = enumerate_namespace_versions (namespace_, search_path);
1236 g_slist_free (search_path);
1238 for (link = candidates; link; link = link->next)
1240 struct NamespaceVersionCandidadate *candidate = link->data;
1241 ret = g_list_prepend (ret, g_strdup (candidate->version));
1242 free_candidate (candidate);
1244 g_slist_free (candidates);
1246 /* The currently loaded version of a namespace is also part of the
1247 * available versions, as it could have been loaded using
1248 * require_private().
1250 if (g_irepository_is_registered (repository, namespace_, NULL))
1252 loaded_version = g_irepository_get_version (repository, namespace_);
1253 if (loaded_version && !g_list_find_custom (ret, loaded_version, g_str_equal))
1254 ret = g_list_prepend (ret, g_strdup (loaded_version));
1261 require_internal (GIRepository *repository,
1262 const gchar *namespace,
1263 const gchar *version,
1264 GIRepositoryLoadFlags flags,
1265 GSList *search_path,
1269 GITypelib *ret = NULL;
1271 GITypelib *typelib = NULL;
1272 const gchar *typelib_namespace, *typelib_version;
1273 gboolean allow_lazy = (flags & G_IREPOSITORY_LOAD_FLAG_LAZY) > 0;
1275 char *version_conflict = NULL;
1277 char *tmp_version = NULL;
1279 g_return_val_if_fail (namespace != NULL, FALSE);
1281 repository = get_repository (repository);
1283 typelib = get_registered_status (repository, namespace, version, allow_lazy,
1284 &is_lazy, &version_conflict);
1288 if (version_conflict != NULL)
1290 g_set_error (error, G_IREPOSITORY_ERROR,
1291 G_IREPOSITORY_ERROR_NAMESPACE_VERSION_CONFLICT,
1292 "Requiring namespace '%s' version '%s', but '%s' is already loaded",
1293 namespace, version, version_conflict);
1297 if (version != NULL)
1299 mfile = find_namespace_version (namespace, version,
1300 search_path, &path);
1301 tmp_version = g_strdup (version);
1305 mfile = find_namespace_latest (namespace, search_path,
1306 &tmp_version, &path);
1311 if (version != NULL)
1312 g_set_error (error, G_IREPOSITORY_ERROR,
1313 G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
1314 "Typelib file for namespace '%s', version '%s' not found",
1315 namespace, version);
1317 g_set_error (error, G_IREPOSITORY_ERROR,
1318 G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
1319 "Typelib file for namespace '%s' (any version) not found",
1325 GError *temp_error = NULL;
1326 typelib = g_typelib_new_from_mapped_file (mfile, &temp_error);
1329 g_set_error (error, G_IREPOSITORY_ERROR,
1330 G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
1331 "Failed to load typelib file '%s' for namespace '%s': %s",
1332 path, namespace, temp_error->message);
1333 g_clear_error (&temp_error);
1337 header = (Header *) typelib->data;
1338 typelib_namespace = g_typelib_get_string (typelib, header->namespace);
1339 typelib_version = g_typelib_get_string (typelib, header->nsversion);
1341 if (strcmp (typelib_namespace, namespace) != 0)
1343 g_set_error (error, G_IREPOSITORY_ERROR,
1344 G_IREPOSITORY_ERROR_NAMESPACE_MISMATCH,
1345 "Typelib file %s for namespace '%s' contains "
1346 "namespace '%s' which doesn't match the file name",
1347 path, namespace, typelib_namespace);
1348 g_typelib_free (typelib);
1351 if (version != NULL && strcmp (typelib_version, version) != 0)
1353 g_set_error (error, G_IREPOSITORY_ERROR,
1354 G_IREPOSITORY_ERROR_NAMESPACE_MISMATCH,
1355 "Typelib file %s for namespace '%s' contains "
1356 "version '%s' which doesn't match the expected version '%s'",
1357 path, namespace, typelib_version, version);
1358 g_typelib_free (typelib);
1362 if (!register_internal (repository, path, allow_lazy,
1365 g_typelib_free (typelib);
1370 g_free (tmp_version);
1376 * g_irepository_require:
1377 * @repository: (allow-none): Repository, may be %NULL for the default
1378 * @namespace_: GI namespace to use, e.g. "Gtk"
1379 * @version: (allow-none): Version of namespace, may be %NULL for latest
1380 * @flags: Set of %GIRepositoryLoadFlags, may be 0
1381 * @error: a #GError.
1383 * Force the namespace @namespace_ to be loaded if it isn't already.
1384 * If @namespace_ is not loaded, this function will search for a
1385 * ".typelib" file using the repository search path. In addition, a
1386 * version @version of namespace may be specified. If @version is
1387 * not specified, the latest will be used.
1389 * Returns: (transfer none): a pointer to the #GITypelib if successful, %NULL otherwise
1392 g_irepository_require (GIRepository *repository,
1393 const gchar *namespace,
1394 const gchar *version,
1395 GIRepositoryLoadFlags flags,
1398 GSList *search_path;
1401 search_path = build_search_path_with_overrides ();
1402 typelib = require_internal (repository, namespace, version, flags,
1403 search_path, error);
1404 g_slist_free (search_path);
1410 * g_irepository_require_private:
1411 * @repository: (allow-none): Repository, may be %NULL for the default
1412 * @typelib_dir: Private directory where to find the requested typelib
1413 * @namespace_: GI namespace to use, e.g. "Gtk"
1414 * @version: (allow-none): Version of namespace, may be %NULL for latest
1415 * @flags: Set of %GIRepositoryLoadFlags, may be 0
1416 * @error: a #GError.
1418 * Force the namespace @namespace_ to be loaded if it isn't already.
1419 * If @namespace_ is not loaded, this function will search for a
1420 * ".typelib" file within the private directory only. In addition, a
1421 * version @version of namespace should be specified. If @version is
1422 * not specified, the latest will be used.
1424 * Returns: (transfer none): a pointer to the #GITypelib if successful, %NULL otherwise
1427 g_irepository_require_private (GIRepository *repository,
1428 const gchar *typelib_dir,
1429 const gchar *namespace,
1430 const gchar *version,
1431 GIRepositoryLoadFlags flags,
1434 GSList search_path = { (gpointer) typelib_dir, NULL };
1436 return require_internal (repository, namespace, version, flags,
1437 &search_path, error);
1441 g_irepository_introspect_cb (const char *option_name,
1446 GError *tmp_error = NULL;
1447 gboolean ret = g_irepository_dump (value, &tmp_error);
1450 g_error ("Failed to extract GType data: %s",
1451 tmp_error->message);
1457 static const GOptionEntry introspection_args[] = {
1458 { "introspect-dump", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK,
1459 g_irepository_introspect_cb, "Dump introspection information",
1460 "infile.txt,outfile.xml" },
1465 * g_irepository_get_option_group: (skip)
1467 * Obtain the option group for girepository, it's used
1468 * by the dumper and for programs that wants to provide
1469 * introspection information
1471 * Returns: (transfer full): the option group
1474 g_irepository_get_option_group (void)
1476 GOptionGroup *group;
1477 group = g_option_group_new ("girepository", "Introspection Options", "Show Introspection Options", NULL, NULL);
1479 g_option_group_add_entries (group, introspection_args);
1484 g_irepository_error_quark (void)
1486 static GQuark quark = 0;
1488 quark = g_quark_from_static_string ("g-irepository-error-quark");
1493 * g_type_tag_to_string:
1494 * @type: the type_tag
1496 * Obtain a string representation of @type
1498 * Returns: the string
1501 g_type_tag_to_string (GITypeTag type)
1505 case GI_TYPE_TAG_VOID:
1507 case GI_TYPE_TAG_BOOLEAN:
1509 case GI_TYPE_TAG_INT8:
1511 case GI_TYPE_TAG_UINT8:
1513 case GI_TYPE_TAG_INT16:
1515 case GI_TYPE_TAG_UINT16:
1517 case GI_TYPE_TAG_INT32:
1519 case GI_TYPE_TAG_UINT32:
1521 case GI_TYPE_TAG_INT64:
1523 case GI_TYPE_TAG_UINT64:
1525 case GI_TYPE_TAG_FLOAT:
1527 case GI_TYPE_TAG_DOUBLE:
1529 case GI_TYPE_TAG_UNICHAR:
1531 case GI_TYPE_TAG_GTYPE:
1533 case GI_TYPE_TAG_UTF8:
1535 case GI_TYPE_TAG_FILENAME:
1537 case GI_TYPE_TAG_ARRAY:
1539 case GI_TYPE_TAG_INTERFACE:
1541 case GI_TYPE_TAG_GLIST:
1543 case GI_TYPE_TAG_GSLIST:
1545 case GI_TYPE_TAG_GHASH:
1547 case GI_TYPE_TAG_ERROR:
1555 * g_info_type_to_string:
1556 * @type: the info type
1558 * Obtain a string representation of @type
1560 * Returns: the string
1563 g_info_type_to_string (GIInfoType type)
1567 case GI_INFO_TYPE_INVALID:
1569 case GI_INFO_TYPE_FUNCTION:
1571 case GI_INFO_TYPE_CALLBACK:
1573 case GI_INFO_TYPE_STRUCT:
1575 case GI_INFO_TYPE_BOXED:
1577 case GI_INFO_TYPE_ENUM:
1579 case GI_INFO_TYPE_FLAGS:
1581 case GI_INFO_TYPE_OBJECT:
1583 case GI_INFO_TYPE_INTERFACE:
1585 case GI_INFO_TYPE_CONSTANT:
1587 case GI_INFO_TYPE_UNION:
1589 case GI_INFO_TYPE_VALUE:
1591 case GI_INFO_TYPE_SIGNAL:
1593 case GI_INFO_TYPE_VFUNC:
1595 case GI_INFO_TYPE_PROPERTY:
1597 case GI_INFO_TYPE_FIELD:
1599 case GI_INFO_TYPE_ARG:
1601 case GI_INFO_TYPE_TYPE:
1603 case GI_INFO_TYPE_UNRESOLVED:
1604 return "unresolved";