1 /* -*- Mode: C; c-file-style: "gnu"; -*- */
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 -> GTypelib */
46 GHashTable *lazy_typelibs; /* (string) namespace-version -> GTypelib */
47 GHashTable *info_by_gtype; /* GType -> GIBaseInfo */
50 G_DEFINE_TYPE (GIRepository, g_irepository, G_TYPE_OBJECT);
53 g_irepository_init (GIRepository *repository)
55 repository->priv = G_TYPE_INSTANCE_GET_PRIVATE (repository, G_TYPE_IREPOSITORY,
57 repository->priv->typelibs
58 = g_hash_table_new_full (g_str_hash, g_str_equal,
59 (GDestroyNotify) NULL,
60 (GDestroyNotify) g_typelib_free);
61 repository->priv->lazy_typelibs
62 = g_hash_table_new (g_str_hash, g_str_equal);
63 repository->priv->info_by_gtype
64 = g_hash_table_new_full (g_direct_hash, g_direct_equal,
65 (GDestroyNotify) NULL,
66 (GDestroyNotify) g_base_info_unref);
70 g_irepository_finalize (GObject *object)
72 GIRepository *repository = G_IREPOSITORY (object);
74 g_hash_table_destroy (repository->priv->typelibs);
75 g_hash_table_destroy (repository->priv->lazy_typelibs);
76 g_hash_table_destroy (repository->priv->info_by_gtype);
78 (* G_OBJECT_CLASS (g_irepository_parent_class)->finalize) (G_OBJECT (repository));
82 g_irepository_class_init (GIRepositoryClass *class)
84 GObjectClass *gobject_class;
86 gobject_class = G_OBJECT_CLASS (class);
88 gobject_class->finalize = g_irepository_finalize;
90 g_type_class_add_private (class, sizeof (GIRepositoryPrivate));
96 g_static_mutex_lock (&globals_lock);
98 if (default_repository == NULL)
100 default_repository = g_object_new (G_TYPE_IREPOSITORY, NULL);
103 if (search_path == NULL)
107 const gchar *type_lib_path_env;
109 /* This variable is intended to take precedence over both the default
110 * search path, as well as anything written into code with g_irepository_prepend_search_path.
112 type_lib_path_env = g_getenv ("GI_TYPELIB_PATH");
115 override_search_path = NULL;
116 if (type_lib_path_env)
121 custom_dirs = g_strsplit (type_lib_path_env, G_SEARCHPATH_SEPARATOR_S, 0);
126 override_search_path = g_slist_prepend (override_search_path, *d);
130 /* ownership of the array content was passed to the list */
131 g_free (custom_dirs);
134 if (override_search_path != NULL)
135 override_search_path = g_slist_reverse (override_search_path);
137 libdir = GOBJECT_INTROSPECTION_LIBDIR;
139 typelib_dir = g_build_filename (libdir, "girepository-1.0", NULL);
141 search_path = g_slist_prepend (search_path, typelib_dir);
143 search_path = g_slist_reverse (search_path);
146 g_static_mutex_unlock (&globals_lock);
150 g_irepository_prepend_search_path (const char *directory)
153 search_path = g_slist_prepend (search_path, g_strdup (directory));
157 * g_irepository_get_search_path:
159 * Returns the search path the GIRepository will use when looking for typelibs.
160 * The string is internal to GIRespository and should not be freed, nor should
163 * Return value: (element-type filename) (transfer none): list of strings
166 g_irepository_get_search_path (void)
173 build_search_path_with_overrides (void)
176 if (override_search_path != NULL)
178 result = g_slist_copy (override_search_path);
179 g_slist_last (result)->next = g_slist_copy (search_path);
182 result = g_slist_copy (search_path);
187 build_typelib_key (const char *name, const char *source)
189 GString *str = g_string_new (name);
190 g_string_append_c (str, '\0');
191 g_string_append (str, source);
192 return g_string_free (str, FALSE);
196 get_typelib_dependencies (GTypelib *typelib)
199 const char *dependencies_glob;
201 header = (Header *)typelib->data;
203 if (header->dependencies == 0)
206 dependencies_glob = g_typelib_get_string (typelib, header->dependencies);
207 return g_strsplit (dependencies_glob, "|", 0);
210 static GIRepository *
211 get_repository (GIRepository *repository)
215 if (repository != NULL)
218 return default_repository;
222 check_version_conflict (GTypelib *typelib,
223 const gchar *namespace,
224 const gchar *expected_version,
225 char **version_conflict)
228 const char *loaded_version;
230 if (expected_version == NULL)
232 if (version_conflict)
233 *version_conflict = NULL;
237 header = (Header*)typelib->data;
238 loaded_version = g_typelib_get_string (typelib, header->nsversion);
239 g_assert (loaded_version != NULL);
241 if (strcmp (expected_version, loaded_version) != 0)
243 if (version_conflict)
244 *version_conflict = (char*)loaded_version;
247 if (version_conflict)
248 *version_conflict = NULL;
253 get_registered_status (GIRepository *repository,
254 const char *namespace,
257 gboolean *lazy_status,
258 char **version_conflict)
261 repository = get_repository (repository);
263 *lazy_status = FALSE;
264 typelib = g_hash_table_lookup (repository->priv->typelibs, namespace);
266 return check_version_conflict (typelib, namespace, version, version_conflict);
267 typelib = g_hash_table_lookup (repository->priv->lazy_typelibs, namespace);
274 return check_version_conflict (typelib, namespace, version, version_conflict);
278 get_registered (GIRepository *repository,
279 const char *namespace,
282 return get_registered_status (repository, namespace, version, TRUE, NULL, NULL);
286 load_dependencies_recurse (GIRepository *repository,
292 dependencies = get_typelib_dependencies (typelib);
294 if (dependencies != NULL)
298 for (i = 0; dependencies[i]; i++)
300 char *dependency = dependencies[i];
301 const char *last_dash;
302 char *dependency_namespace;
303 const char *dependency_version;
305 last_dash = strrchr (dependency, '-');
306 dependency_namespace = g_strndup (dependency, last_dash - dependency);
307 dependency_version = last_dash+1;
309 if (!g_irepository_require (repository, dependency_namespace, dependency_version,
312 g_free (dependency_namespace);
313 g_strfreev (dependencies);
316 g_free (dependency_namespace);
318 g_strfreev (dependencies);
324 register_internal (GIRepository *repository,
331 const gchar *namespace;
332 const gchar *version;
334 g_return_val_if_fail (typelib != NULL, FALSE);
336 header = (Header *)typelib->data;
338 g_return_val_if_fail (header != NULL, FALSE);
340 namespace = g_typelib_get_string (typelib, header->namespace);
341 version = g_typelib_get_string (typelib, header->nsversion);
345 g_assert (!g_hash_table_lookup (repository->priv->lazy_typelibs,
347 g_hash_table_insert (repository->priv->lazy_typelibs,
348 build_typelib_key (namespace, source), (void *)typelib);
355 /* First, try loading all the dependencies */
356 if (!load_dependencies_recurse (repository, typelib, error))
359 /* Check if we are transitioning from lazily loaded state */
360 if (g_hash_table_lookup_extended (repository->priv->lazy_typelibs,
362 (gpointer)&key, &value))
363 g_hash_table_remove (repository->priv->lazy_typelibs, key);
365 key = build_typelib_key (namespace, source);
367 g_hash_table_insert (repository->priv->typelibs, key, (void *)typelib);
374 * g_irepository_get_dependencies:
375 * @repository: A #GIRepository, may be %NULL for the default
376 * @namespace_: Namespace of interest
378 * Return an array of all (transitive) dependencies for namespace
379 * @namespace_, including version. The returned strings are of the
380 * form <code>namespace-version</code>.
382 * Note: The namespace must have already been loaded using a function
383 * such as #g_irepository_require before calling this function.
385 * Returns: Zero-terminated string array of versioned dependencies
388 g_irepository_get_dependencies (GIRepository *repository,
389 const char *namespace)
393 g_return_val_if_fail (namespace != NULL, NULL);
395 repository = get_repository (repository);
397 typelib = get_registered (repository, namespace, NULL);
398 g_return_val_if_fail (typelib != NULL, NULL);
400 return get_typelib_dependencies (typelib);
404 g_irepository_load_typelib (GIRepository *repository,
406 GIRepositoryLoadFlags flags,
410 const char *namespace;
411 const char *nsversion;
412 gboolean allow_lazy = flags & G_IREPOSITORY_LOAD_FLAG_LAZY;
414 char *version_conflict;
416 repository = get_repository (repository);
418 header = (Header *) typelib->data;
419 namespace = g_typelib_get_string (typelib, header->namespace);
420 nsversion = g_typelib_get_string (typelib, header->nsversion);
422 if (get_registered_status (repository, namespace, nsversion, allow_lazy,
423 &is_lazy, &version_conflict))
425 if (version_conflict != NULL)
427 g_set_error (error, G_IREPOSITORY_ERROR,
428 G_IREPOSITORY_ERROR_NAMESPACE_VERSION_CONFLICT,
429 "Attempting to load namespace '%s', version '%s', but '%s' is already loaded",
430 namespace, nsversion, version_conflict);
435 return register_internal (repository, "<builtin>",
436 allow_lazy, typelib, error);
440 * g_irepository_is_registered:
441 * @repository: A #GIRepository, may be %NULL for the default
442 * @namespace_: Namespace of interest
443 * @version: (allow-none): Required version, may be %NULL for latest
445 * Check whether a particular namespace (and optionally, a specific
446 * version thereof) is currently loaded. This function is likely to
447 * only be useful in unusual circumstances; in order to act upon
448 * metadata in the namespace, you should call #g_irepository_require
449 * instead which will ensure the namespace is loaded, and return as
450 * quickly as this function will if it has already been loaded.
452 * Returns: %TRUE if namespace-version is loaded, %FALSE otherwise
455 g_irepository_is_registered (GIRepository *repository,
456 const gchar *namespace,
457 const gchar *version)
459 repository = get_repository (repository);
460 return get_registered (repository, namespace, version) != NULL;
464 * g_irepository_get_default:
466 * Returns the singleton process-global default #GIRepository. It is
467 * not currently supported to have multiple repositories in a
468 * particular process, but this function is provided in the unlikely
469 * eventuality that it would become possible, and as a convenience for
470 * higher level language bindings to conform to the GObject method
473 * All methods on #GIRepository also accept %NULL as an instance
474 * parameter to mean this default repository, which is usually more
477 * Returns: (transfer none): The global singleton #GIRepository
480 g_irepository_get_default (void)
482 return get_repository (NULL);
486 * g_irepository_get_n_infos:
487 * @repository: A #GIRepository, may be %NULL for the default
488 * @namespace_: Namespace to inspect
490 * This function returns the number of metadata entries in
491 * given namespace @namespace_. The namespace must have
492 * already been loaded before calling this function.
494 * Returns: number of metadata entries
497 g_irepository_get_n_infos (GIRepository *repository,
498 const gchar *namespace)
501 gint n_interfaces = 0;
503 g_return_val_if_fail (namespace != NULL, -1);
505 repository = get_repository (repository);
507 typelib = get_registered (repository, namespace, NULL);
509 g_return_val_if_fail (typelib != NULL, -1);
511 n_interfaces = ((Header *)typelib->data)->n_local_entries;
521 gboolean type_firstpass;
527 find_interface (gpointer key,
532 GTypelib *typelib = (GTypelib *)value;
533 Header *header = (Header *) typelib->data;
534 IfaceData *iface_data = (IfaceData *)data;
542 n_entries = ((Header *)typelib->data)->n_local_entries;
544 if (iface_data->name)
546 for (i = 1; i <= n_entries; i++)
548 entry = g_typelib_get_dir_entry (typelib, i);
549 name = g_typelib_get_string (typelib, entry->name);
550 if (strcmp (name, iface_data->name) == 0)
557 else if (iface_data->type)
559 const char *c_prefix;
560 /* Inside each typelib, we include the "C prefix" which acts as
561 * a namespace mechanism. For GtkTreeView, the C prefix is Gtk.
562 * Given the assumption that GTypes for a library also use the
563 * C prefix, we know we can skip examining a typelib if our
564 * target type does not have this typelib's C prefix.
566 * However, not every class library necessarily conforms to this,
567 * e.g. Clutter has Cogl inside it. So, we split this into two
568 * passes. First we try a lookup, skipping things which don't
569 * have the prefix. If that fails then we try a global lookup,
570 * ignoring the prefix.
572 * See http://bugzilla.gnome.org/show_bug.cgi?id=564016
574 c_prefix = g_typelib_get_string (typelib, header->c_prefix);
575 if (iface_data->type_firstpass && c_prefix != NULL)
577 if (g_ascii_strncasecmp (c_prefix, iface_data->type, strlen (c_prefix)) != 0)
581 for (i = 1; i <= n_entries; i++)
583 RegisteredTypeBlob *blob;
585 entry = g_typelib_get_dir_entry (typelib, i);
586 if (!BLOB_IS_REGISTERED_TYPE (entry))
589 blob = (RegisteredTypeBlob *)(&typelib->data[entry->offset]);
590 if (!blob->gtype_name)
593 type = g_typelib_get_string (typelib, blob->gtype_name);
594 if (strcmp (type, iface_data->type) == 0)
601 else if (iface_data->index > n_entries)
602 iface_data->index -= n_entries;
603 else if (iface_data->index > 0)
605 index = iface_data->index;
606 iface_data->index = 0;
611 entry = g_typelib_get_dir_entry (typelib, index);
612 iface_data->iface = _g_info_new_full (entry->blob_type,
614 NULL, typelib, entry->offset);
619 * g_irepository_get_info:
620 * @repository: A #GIRepository, may be %NULL for the default
621 * @namespace_: Namespace to inspect
622 * @index: Offset into namespace metadata for entry
624 * This function returns a particular metadata entry in the
625 * given namespace @namespace_. The namespace must have
626 * already been loaded before calling this function.
628 * Returns: #GIBaseInfo containing metadata
631 g_irepository_get_info (GIRepository *repository,
632 const gchar *namespace,
638 g_return_val_if_fail (namespace != NULL, NULL);
640 repository = get_repository (repository);
642 data.repo = repository;
645 data.index = index + 1;
648 typelib = get_registered (repository, namespace, NULL);
650 g_return_val_if_fail (typelib != NULL, NULL);
652 find_interface ((void *)namespace, typelib, &data);
658 * g_irepository_find_by_gtype:
659 * @repository: A #GIRepository, may be %NULL for the default
660 * @gtype: GType to search for
662 * Searches all loaded namespaces for a particular #GType. Note that
663 * in order to locate the metadata, the namespace corresponding to
664 * the type must first have been loaded. There is currently no
665 * mechanism for determining the namespace which corresponds to an
666 * arbitrary GType - thus, this function will operate most reliably
667 * when you know the GType to originate from be from a loaded namespace.
669 * Returns: #GIBaseInfo representing metadata about @type, or %NULL
672 g_irepository_find_by_gtype (GIRepository *repository,
677 repository = get_repository (repository);
679 data.iface = g_hash_table_lookup (repository->priv->info_by_gtype,
683 return g_base_info_ref (data.iface);
685 data.repo = repository;
687 data.type_firstpass = TRUE;
688 data.type = g_type_name (gtype);
692 g_hash_table_foreach (repository->priv->typelibs, find_interface, &data);
693 g_hash_table_foreach (repository->priv->lazy_typelibs, find_interface, &data);
695 /* We do two passes; see comment in find_interface */
698 data.type_firstpass = FALSE;
699 g_hash_table_foreach (repository->priv->typelibs, find_interface, &data);
700 g_hash_table_foreach (repository->priv->lazy_typelibs, find_interface, &data);
704 g_hash_table_insert (repository->priv->info_by_gtype,
706 g_base_info_ref (data.iface));
712 * g_irepository_find_by_name:
713 * @repository: A #GIRepository, may be %NULL for the default
714 * @namespace_: Namespace which will be searched
715 * @name: Entry name to find
717 * Searches for a particular entry in a namespace. Before calling
718 * this function for a particular namespace, you must call
719 * #g_irepository_require once to load the namespace, or otherwise
720 * ensure the namespace has already been loaded.
722 * Returns: #GIBaseInfo representing metadata about @name, or %NULL
725 g_irepository_find_by_name (GIRepository *repository,
726 const gchar *namespace,
732 g_return_val_if_fail (namespace != NULL, NULL);
734 repository = get_repository (repository);
736 data.repo = repository;
742 typelib = get_registered (repository, namespace, NULL);
744 g_return_val_if_fail (typelib != NULL, NULL);
746 find_interface ((void *)namespace, typelib, &data);
752 collect_namespaces (gpointer key,
758 *list = g_list_append (*list, key);
762 * g_irepository_get_namespaces:
763 * @repository: A #GIRepository, may be %NULL for the default
765 * Return the list of currently loaded namespaces.
767 * Returns: (utf8) (transfer full): List of namespaces
770 g_irepository_get_loaded_namespaces (GIRepository *repository)
772 GList *l, *list = NULL;
776 repository = get_repository (repository);
778 g_hash_table_foreach (repository->priv->typelibs, collect_namespaces, &list);
779 g_hash_table_foreach (repository->priv->lazy_typelibs, collect_namespaces, &list);
781 names = g_malloc0 (sizeof (gchar *) * (g_list_length (list) + 1));
783 for (l = list; l; l = l->next)
784 names[i++] = g_strdup (l->data);
791 * g_irepository_get_version:
792 * @repository: A #GIRepository, may be %NULL for the default
793 * @namespace_: Namespace to inspect
795 * This function returns the loaded version associated with the given
796 * namespace @namespace_.
798 * Note: The namespace must have already been loaded using a function
799 * such as #g_irepository_require before calling this function.
801 * Returns: Loaded version
804 g_irepository_get_version (GIRepository *repository,
805 const gchar *namespace)
810 g_return_val_if_fail (namespace != NULL, NULL);
812 repository = get_repository (repository);
814 typelib = get_registered (repository, namespace, NULL);
816 g_return_val_if_fail (typelib != NULL, NULL);
818 header = (Header *) typelib->data;
819 return g_typelib_get_string (typelib, header->nsversion);
823 * g_irepository_get_shared_library:
824 * @repository: A #GIRepository, may be %NULL for the default
825 * @namespace_: Namespace to inspect
827 * This function returns the full path to the shared C library
828 * associated with the given namespace @namespace_. There may be no
829 * shared library path associated, in which case this function will
832 * Note: The namespace must have already been loaded using a function
833 * such as #g_irepository_require before calling this function.
835 * Returns: Full path to shared library, or %NULL if none associated
838 g_irepository_get_shared_library (GIRepository *repository,
839 const gchar *namespace)
844 g_return_val_if_fail (namespace != NULL, NULL);
846 repository = get_repository (repository);
848 typelib = get_registered (repository, namespace, NULL);
850 g_return_val_if_fail (typelib != NULL, NULL);
852 header = (Header *) typelib->data;
853 if (header->shared_library)
854 return g_typelib_get_string (typelib, header->shared_library);
860 * g_irepository_get_c_prefix
861 * @repository: A #GIRepository, may be %NULL for the default
862 * @namespace_: Namespace to inspect
864 * This function returns the "C prefix", or the C level namespace
865 * associated with the given introspection namespace. Each C symbol
866 * starts with this prefix, as well each #GType in the library.
868 * Note: The namespace must have already been loaded using a function
869 * such as #g_irepository_require before calling this function.
871 * Returns: C namespace prefix, or %NULL if none associated
874 g_irepository_get_c_prefix (GIRepository *repository,
875 const gchar *namespace_)
880 g_return_val_if_fail (namespace_ != NULL, NULL);
882 repository = get_repository (repository);
884 typelib = get_registered (repository, namespace_, NULL);
886 g_return_val_if_fail (typelib != NULL, NULL);
888 header = (Header *) typelib->data;
889 if (header->shared_library)
890 return g_typelib_get_string (typelib, header->c_prefix);
896 * g_irepository_get_typelib_path
897 * @repository: Repository, may be %NULL for the default
898 * @namespace_: GI namespace to use, e.g. "Gtk"
900 * If namespace @namespace_ is loaded, return the full path to the
901 * .typelib file it was loaded from. If the typelib for
902 * namespace @namespace_ was included in a shared library, return
903 * the special string "$lt;builtin$gt;".
905 * Returns: Filesystem path (or $lt;builtin$gt;) if successful, %NULL if namespace is not loaded
909 g_irepository_get_typelib_path (GIRepository *repository,
910 const gchar *namespace)
912 gpointer orig_key, value;
914 repository = get_repository (repository);
916 if (!g_hash_table_lookup_extended (repository->priv->typelibs, namespace,
919 if (!g_hash_table_lookup_extended (repository->priv->lazy_typelibs, namespace,
924 return ((char*)orig_key) + strlen ((char *) orig_key) + 1;
927 /* This simple search function looks for a specified namespace-version;
928 it's faster than the full directory listing required for latest version. */
930 find_namespace_version (const gchar *namespace,
931 const gchar *version,
936 GError *error = NULL;
937 GMappedFile *mfile = NULL;
940 fname = g_strdup_printf ("%s-%s.typelib", namespace, version);
942 for (ldir = search_path; ldir; ldir = ldir->next)
944 char *path = g_build_filename (ldir->data, fname, NULL);
946 mfile = g_mapped_file_new (path, FALSE, &error);
950 g_clear_error (&error);
961 parse_version (const char *version,
968 *major = strtol (version, &end, 10);
969 dot = strchr (version, '.');
977 *minor = strtol (dot+1, &end, 10);
978 if (end != (version + strlen (version)))
984 compare_version (const char *v1,
988 int v1_major, v1_minor;
989 int v2_major, v2_minor;
991 success = parse_version (v1, &v1_major, &v1_minor);
994 success = parse_version (v2, &v2_major, &v2_minor);
997 if (v1_major > v2_major)
999 else if (v2_major > v1_major)
1001 else if (v1_minor > v2_minor)
1003 else if (v2_minor > v1_minor)
1008 struct NamespaceVersionCandidadate
1017 compare_candidate_reverse (struct NamespaceVersionCandidadate *c1,
1018 struct NamespaceVersionCandidadate *c2)
1020 int result = compare_version (c1->version, c2->version);
1021 /* First, check the version */
1024 else if (result < 0)
1028 /* Now check the path index, which says how early in the search path
1029 * we found it. This ensures that of equal version targets, we
1030 * pick the earlier one.
1032 if (c1->path_index == c2->path_index)
1034 else if (c1->path_index > c2->path_index)
1042 free_candidate (struct NamespaceVersionCandidadate *candidate)
1044 g_mapped_file_unref (candidate->mfile);
1045 g_free (candidate->path);
1046 g_free (candidate->version);
1047 g_slice_free (struct NamespaceVersionCandidadate, candidate);
1051 enumerate_namespace_versions (const gchar *namespace,
1052 GSList *search_path)
1054 GSList *candidates = NULL;
1055 GHashTable *found_versions = g_hash_table_new (g_str_hash, g_str_equal);
1056 char *namespace_dash;
1057 char *namespace_typelib;
1059 GError *error = NULL;
1062 namespace_dash = g_strdup_printf ("%s-", namespace);
1063 namespace_typelib = g_strdup_printf ("%s.typelib", namespace);
1066 for (ldir = search_path; ldir; ldir = ldir->next)
1069 const char *dirname;
1072 dirname = (const char*)ldir->data;
1073 dir = g_dir_open (dirname, 0, NULL);
1076 while ((entry = g_dir_read_name (dir)) != NULL)
1079 char *path, *version;
1080 struct NamespaceVersionCandidadate *candidate;
1082 if (!g_str_has_suffix (entry, ".typelib"))
1085 if (g_str_has_prefix (entry, namespace_dash))
1087 const char *last_dash;
1088 const char *name_end;
1091 name_end = strrchr (entry, '.');
1092 last_dash = strrchr (entry, '-');
1093 version = g_strndup (last_dash+1, name_end-(last_dash+1));
1094 if (!parse_version (version, &major, &minor))
1100 if (g_hash_table_lookup (found_versions, version) != NULL)
1102 g_hash_table_insert (found_versions, version, version);
1104 path = g_build_filename (dirname, entry, NULL);
1105 mfile = g_mapped_file_new (path, FALSE, &error);
1110 g_clear_error (&error);
1113 candidate = g_slice_new0 (struct NamespaceVersionCandidadate);
1114 candidate->mfile = mfile;
1115 candidate->path_index = index;
1116 candidate->path = path;
1117 candidate->version = version;
1118 candidates = g_slist_prepend (candidates, candidate);
1124 g_free (namespace_dash);
1125 g_free (namespace_typelib);
1126 g_hash_table_destroy (found_versions);
1131 static GMappedFile *
1132 find_namespace_latest (const gchar *namespace,
1133 GSList *search_path,
1134 gchar **version_ret,
1138 GMappedFile *result = NULL;
1140 *version_ret = NULL;
1143 candidates = enumerate_namespace_versions (namespace, search_path);
1145 if (candidates != NULL)
1147 struct NamespaceVersionCandidadate *elected;
1148 candidates = g_slist_sort (candidates, (GCompareFunc) compare_candidate_reverse);
1150 elected = (struct NamespaceVersionCandidadate *) candidates->data;
1151 /* Remove the elected one so we don't try to free its contents */
1152 candidates = g_slist_delete_link (candidates, candidates);
1154 result = elected->mfile;
1155 *path_ret = elected->path;
1156 *version_ret = elected->version;
1157 g_slice_free (struct NamespaceVersionCandidadate, elected); /* just free the container */
1158 g_slist_foreach (candidates, (GFunc) free_candidate, NULL);
1159 g_slist_free (candidates);
1165 * g_irepository_enumerate_versions:
1166 * @repository: (allow-none): the repository
1167 * @namespace_: GI namespace, e.g. "Gtk"
1169 * Obtain an unordered list of versions (either currently loaded or
1170 * available) for @namespace_ in this @repository.
1172 * Returns: (element-type utf8) (transfer full): the array of versions.
1175 g_irepository_enumerate_versions (GIRepository *repository,
1176 const gchar *namespace_)
1179 GSList *search_path;
1180 GSList *candidates, *link;
1181 const gchar *loaded_version;
1183 search_path = build_search_path_with_overrides ();
1184 candidates = enumerate_namespace_versions (namespace_, search_path);
1185 g_slist_free (search_path);
1187 for (link = candidates; link; link = link->next)
1189 struct NamespaceVersionCandidadate *candidate = link->data;
1190 ret = g_list_prepend (ret, g_strdup (candidate->version));
1191 free_candidate (candidate);
1193 g_slist_free (candidates);
1195 /* The currently loaded version of a namespace is also part of the
1196 * available versions, as it could have been loaded using
1197 * require_private().
1199 if (g_irepository_is_registered (repository, namespace_, NULL))
1201 loaded_version = g_irepository_get_version (repository, namespace_);
1202 if (loaded_version && !g_list_find_custom (ret, loaded_version, g_str_equal))
1203 ret = g_list_prepend (ret, g_strdup (loaded_version));
1210 require_internal (GIRepository *repository,
1211 const gchar *namespace,
1212 const gchar *version,
1213 GIRepositoryLoadFlags flags,
1214 GSList *search_path,
1218 GTypelib *ret = NULL;
1220 GTypelib *typelib = NULL;
1221 const gchar *typelib_namespace, *typelib_version;
1222 gboolean allow_lazy = (flags & G_IREPOSITORY_LOAD_FLAG_LAZY) > 0;
1224 char *version_conflict = NULL;
1226 char *tmp_version = NULL;
1228 g_return_val_if_fail (namespace != NULL, FALSE);
1230 repository = get_repository (repository);
1232 typelib = get_registered_status (repository, namespace, version, allow_lazy,
1233 &is_lazy, &version_conflict);
1237 if (version_conflict != NULL)
1239 g_set_error (error, G_IREPOSITORY_ERROR,
1240 G_IREPOSITORY_ERROR_NAMESPACE_VERSION_CONFLICT,
1241 "Requiring namespace '%s' version '%s', but '%s' is already loaded",
1242 namespace, version, version_conflict);
1246 if (version != NULL)
1248 mfile = find_namespace_version (namespace, version,
1249 search_path, &path);
1250 tmp_version = g_strdup (version);
1254 mfile = find_namespace_latest (namespace, search_path,
1255 &tmp_version, &path);
1260 if (version != NULL)
1261 g_set_error (error, G_IREPOSITORY_ERROR,
1262 G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
1263 "Typelib file for namespace '%s', version '%s' not found",
1264 namespace, version);
1266 g_set_error (error, G_IREPOSITORY_ERROR,
1267 G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
1268 "Typelib file for namespace '%s' (any version) not found",
1274 GError *temp_error = NULL;
1275 typelib = g_typelib_new_from_mapped_file (mfile, &temp_error);
1278 g_set_error (error, G_IREPOSITORY_ERROR,
1279 G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
1280 "Failed to load typelib file '%s' for namespace '%s': %s",
1281 path, namespace, temp_error->message);
1282 g_clear_error (&temp_error);
1286 header = (Header *) typelib->data;
1287 typelib_namespace = g_typelib_get_string (typelib, header->namespace);
1288 typelib_version = g_typelib_get_string (typelib, header->nsversion);
1290 if (strcmp (typelib_namespace, namespace) != 0)
1292 g_set_error (error, G_IREPOSITORY_ERROR,
1293 G_IREPOSITORY_ERROR_NAMESPACE_MISMATCH,
1294 "Typelib file %s for namespace '%s' contains "
1295 "namespace '%s' which doesn't match the file name",
1296 path, namespace, typelib_namespace);
1297 g_typelib_free (typelib);
1300 if (version != NULL && strcmp (typelib_version, version) != 0)
1302 g_set_error (error, G_IREPOSITORY_ERROR,
1303 G_IREPOSITORY_ERROR_NAMESPACE_MISMATCH,
1304 "Typelib file %s for namespace '%s' contains "
1305 "version '%s' which doesn't match the expected version '%s'",
1306 path, namespace, typelib_version, version);
1307 g_typelib_free (typelib);
1311 if (!register_internal (repository, path, allow_lazy,
1314 g_typelib_free (typelib);
1319 g_free (tmp_version);
1325 * g_irepository_require:
1326 * @repository: (allow-none): Repository, may be %NULL for the default
1327 * @namespace_: GI namespace to use, e.g. "Gtk"
1328 * @version: (allow-none): Version of namespace, may be %NULL for latest
1329 * @flags: Set of %GIRepositoryLoadFlags, may be 0
1330 * @error: a #GError.
1332 * Force the namespace @namespace_ to be loaded if it isn't already.
1333 * If @namespace_ is not loaded, this function will search for a
1334 * ".typelib" file using the repository search path. In addition, a
1335 * version @version of namespace may be specified. If @version is
1336 * not specified, the latest will be used.
1338 * Returns: a pointer to the #GTypelib if successful, %NULL otherwise
1341 g_irepository_require (GIRepository *repository,
1342 const gchar *namespace,
1343 const gchar *version,
1344 GIRepositoryLoadFlags flags,
1347 GSList *search_path;
1350 search_path = build_search_path_with_overrides ();
1351 typelib = require_internal (repository, namespace, version, flags,
1352 search_path, error);
1353 g_slist_free (search_path);
1359 * g_irepository_require_private:
1360 * @repository: (allow-none): Repository, may be %NULL for the default
1361 * @typelib_dir: Private directory where to find the requested typelib
1362 * @namespace_: GI namespace to use, e.g. "Gtk"
1363 * @version: (allow-none): Version of namespace, may be %NULL for latest
1364 * @flags: Set of %GIRepositoryLoadFlags, may be 0
1365 * @error: a #GError.
1367 * Force the namespace @namespace_ to be loaded if it isn't already.
1368 * If @namespace_ is not loaded, this function will search for a
1369 * ".typelib" file within the private directory only. In addition, a
1370 * version @version of namespace should be specified. If @version is
1371 * not specified, the latest will be used.
1373 * Returns: a pointer to the #GTypelib if successful, %NULL otherwise
1376 g_irepository_require_private (GIRepository *repository,
1377 const gchar *typelib_dir,
1378 const gchar *namespace,
1379 const gchar *version,
1380 GIRepositoryLoadFlags flags,
1383 GSList search_path = { (gpointer) typelib_dir, NULL };
1385 return require_internal (repository, namespace, version, flags,
1386 &search_path, error);
1390 g_irepository_introspect_cb (const char *option_name,
1395 GError *tmp_error = NULL;
1396 gboolean ret = g_irepository_dump (value, &tmp_error);
1399 g_error ("Failed to extract GType data: %s",
1400 tmp_error->message);
1406 static const GOptionEntry introspection_args[] = {
1407 { "introspect-dump", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK,
1408 g_irepository_introspect_cb, "Dump introspection information",
1409 "infile.txt,outfile.xml" },
1414 g_irepository_get_option_group (void)
1416 GOptionGroup *group;
1417 group = g_option_group_new ("girepository", "Introspection Options", "Show Introspection Options", NULL, NULL);
1419 g_option_group_add_entries (group, introspection_args);
1424 g_irepository_error_quark (void)
1426 static GQuark quark = 0;
1428 quark = g_quark_from_static_string ("g-irepository-error-quark");
1433 * g_type_tag_to_string:
1434 * @type: the type_tag
1436 * Obtain a string representation of @type
1438 * Returns: the string
1441 g_type_tag_to_string (GITypeTag type)
1445 case GI_TYPE_TAG_VOID:
1447 case GI_TYPE_TAG_BOOLEAN:
1449 case GI_TYPE_TAG_INT8:
1451 case GI_TYPE_TAG_UINT8:
1453 case GI_TYPE_TAG_INT16:
1455 case GI_TYPE_TAG_UINT16:
1457 case GI_TYPE_TAG_INT32:
1459 case GI_TYPE_TAG_UINT32:
1461 case GI_TYPE_TAG_INT64:
1463 case GI_TYPE_TAG_UINT64:
1465 case GI_TYPE_TAG_FLOAT:
1467 case GI_TYPE_TAG_DOUBLE:
1469 case GI_TYPE_TAG_GTYPE:
1471 case GI_TYPE_TAG_UTF8:
1473 case GI_TYPE_TAG_FILENAME:
1475 case GI_TYPE_TAG_ARRAY:
1477 case GI_TYPE_TAG_INTERFACE:
1479 case GI_TYPE_TAG_GLIST:
1481 case GI_TYPE_TAG_GSLIST:
1483 case GI_TYPE_TAG_GHASH:
1485 case GI_TYPE_TAG_ERROR:
1493 * g_info_type_to_string:
1494 * @type: the info type
1496 * Obtain a string representation of @type
1498 * Returns: the string
1501 g_info_type_to_string (GIInfoType type)
1505 case GI_INFO_TYPE_INVALID:
1507 case GI_INFO_TYPE_FUNCTION:
1509 case GI_INFO_TYPE_CALLBACK:
1511 case GI_INFO_TYPE_STRUCT:
1513 case GI_INFO_TYPE_BOXED:
1515 case GI_INFO_TYPE_ENUM:
1517 case GI_INFO_TYPE_FLAGS:
1519 case GI_INFO_TYPE_OBJECT:
1521 case GI_INFO_TYPE_INTERFACE:
1523 case GI_INFO_TYPE_CONSTANT:
1525 case GI_INFO_TYPE_ERROR_DOMAIN:
1526 return "error domain";
1527 case GI_INFO_TYPE_UNION:
1529 case GI_INFO_TYPE_VALUE:
1531 case GI_INFO_TYPE_SIGNAL:
1533 case GI_INFO_TYPE_VFUNC:
1535 case GI_INFO_TYPE_PROPERTY:
1537 case GI_INFO_TYPE_FIELD:
1539 case GI_INFO_TYPE_ARG:
1541 case GI_INFO_TYPE_TYPE:
1543 case GI_INFO_TYPE_UNRESOLVED:
1544 return "unresolved";