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 tmp_path = build_search_path_with_overrides ();
943 for (ldir = tmp_path; ldir; ldir = ldir->next)
945 char *path = g_build_filename (ldir->data, fname, NULL);
947 mfile = g_mapped_file_new (path, FALSE, &error);
951 g_clear_error (&error);
958 g_slist_free (tmp_path);
963 parse_version (const char *version,
970 *major = strtol (version, &end, 10);
971 dot = strchr (version, '.');
979 *minor = strtol (dot+1, &end, 10);
980 if (end != (version + strlen (version)))
986 compare_version (const char *v1,
990 int v1_major, v1_minor;
991 int v2_major, v2_minor;
993 success = parse_version (v1, &v1_major, &v1_minor);
996 success = parse_version (v2, &v2_major, &v2_minor);
999 if (v1_major > v2_major)
1001 else if (v2_major > v1_major)
1003 else if (v1_minor > v2_minor)
1005 else if (v2_minor > v1_minor)
1010 struct NamespaceVersionCandidadate
1019 compare_candidate_reverse (struct NamespaceVersionCandidadate *c1,
1020 struct NamespaceVersionCandidadate *c2)
1022 int result = compare_version (c1->version, c2->version);
1023 /* First, check the version */
1026 else if (result < 0)
1030 /* Now check the path index, which says how early in the search path
1031 * we found it. This ensures that of equal version targets, we
1032 * pick the earlier one.
1034 if (c1->path_index == c2->path_index)
1036 else if (c1->path_index > c2->path_index)
1044 free_candidate (struct NamespaceVersionCandidadate *candidate)
1046 g_mapped_file_unref (candidate->mfile);
1047 g_free (candidate->path);
1048 g_free (candidate->version);
1049 g_slice_free (struct NamespaceVersionCandidadate, candidate);
1053 enumerate_namespace_versions (const gchar *namespace)
1055 GSList *candidates = NULL;
1056 GHashTable *found_versions = g_hash_table_new (g_str_hash, g_str_equal);
1057 char *namespace_dash;
1058 char *namespace_typelib;
1061 GError *error = NULL;
1064 namespace_dash = g_strdup_printf ("%s-", namespace);
1065 namespace_typelib = g_strdup_printf ("%s.typelib", namespace);
1068 tmp_path = build_search_path_with_overrides ();
1069 for (ldir = tmp_path; ldir; ldir = ldir->next)
1072 const char *dirname;
1075 dirname = (const char*)ldir->data;
1076 dir = g_dir_open (dirname, 0, NULL);
1079 while ((entry = g_dir_read_name (dir)) != NULL)
1082 char *path, *version;
1083 struct NamespaceVersionCandidadate *candidate;
1085 if (!g_str_has_suffix (entry, ".typelib"))
1088 if (g_str_has_prefix (entry, namespace_dash))
1090 const char *last_dash;
1091 const char *name_end;
1094 name_end = strrchr (entry, '.');
1095 last_dash = strrchr (entry, '-');
1096 version = g_strndup (last_dash+1, name_end-(last_dash+1));
1097 if (!parse_version (version, &major, &minor))
1103 if (g_hash_table_lookup (found_versions, version) != NULL)
1105 g_hash_table_insert (found_versions, version, version);
1107 path = g_build_filename (dirname, entry, NULL);
1108 mfile = g_mapped_file_new (path, FALSE, &error);
1113 g_clear_error (&error);
1116 candidate = g_slice_new0 (struct NamespaceVersionCandidadate);
1117 candidate->mfile = mfile;
1118 candidate->path_index = index;
1119 candidate->path = path;
1120 candidate->version = version;
1121 candidates = g_slist_prepend (candidates, candidate);
1127 g_slist_free (tmp_path);
1128 g_free (namespace_dash);
1129 g_free (namespace_typelib);
1130 g_hash_table_destroy (found_versions);
1135 static GMappedFile *
1136 find_namespace_latest (const gchar *namespace,
1137 gchar **version_ret,
1141 GMappedFile *result = NULL;
1143 *version_ret = NULL;
1146 candidates = enumerate_namespace_versions (namespace);
1148 if (candidates != NULL)
1150 struct NamespaceVersionCandidadate *elected;
1151 candidates = g_slist_sort (candidates, (GCompareFunc) compare_candidate_reverse);
1153 elected = (struct NamespaceVersionCandidadate *) candidates->data;
1154 /* Remove the elected one so we don't try to free its contents */
1155 candidates = g_slist_delete_link (candidates, candidates);
1157 result = elected->mfile;
1158 *path_ret = elected->path;
1159 *version_ret = elected->version;
1160 g_slice_free (struct NamespaceVersionCandidadate, elected); /* just free the container */
1161 g_slist_foreach (candidates, (GFunc) free_candidate, NULL);
1162 g_slist_free (candidates);
1168 * g_irepository_enumerate:
1169 * @repository: (allow-none): Repository
1170 * @namespace_: GI namespace, e.g. "Gtk"
1172 * Returns: (element-type utf8) (transfer full): An array of versions available for
1176 g_irepository_enumerate (GIRepository *repository,
1177 const gchar *namespace_)
1180 GSList *candidates, *link;
1182 candidates = enumerate_namespace_versions (namespace_);
1183 for (link = candidates; link; link = link->next)
1185 struct NamespaceVersionCandidadate *candidate = link->data;
1186 ret = g_list_append (ret, g_strdup (candidate->version));
1187 free_candidate (candidate);
1189 g_slist_free (candidates);
1194 * g_irepository_require:
1195 * @repository: (allow-none): Repository, may be %NULL for the default
1196 * @namespace_: GI namespace to use, e.g. "Gtk"
1197 * @version: (allow-none): Version of namespace, may be %NULL for latest
1198 * @flags: Set of %GIRepositoryLoadFlags, may be 0
1199 * @error: a #GError.
1201 * Force the namespace @namespace_ to be loaded if it isn't already.
1202 * If @namespace_ is not loaded, this function will search for a
1203 * ".typelib" file using the repository search path. In addition, a
1204 * version @version of namespace may be specified. If @version is
1205 * not specified, the latest will be used.
1207 * Returns: a pointer to the #GTypelib if successful, %NULL otherwise
1210 g_irepository_require (GIRepository *repository,
1211 const gchar *namespace,
1212 const gchar *version,
1213 GIRepositoryLoadFlags flags,
1217 GTypelib *ret = NULL;
1219 GTypelib *typelib = NULL;
1220 const gchar *typelib_namespace, *typelib_version;
1221 gboolean allow_lazy = (flags & G_IREPOSITORY_LOAD_FLAG_LAZY) > 0;
1223 char *version_conflict = NULL;
1225 char *tmp_version = NULL;
1227 g_return_val_if_fail (namespace != NULL, FALSE);
1229 repository = get_repository (repository);
1231 typelib = get_registered_status (repository, namespace, version, allow_lazy,
1232 &is_lazy, &version_conflict);
1236 if (version_conflict != NULL)
1238 g_set_error (error, G_IREPOSITORY_ERROR,
1239 G_IREPOSITORY_ERROR_NAMESPACE_VERSION_CONFLICT,
1240 "Requiring namespace '%s' version '%s', but '%s' is already loaded",
1241 namespace, version, version_conflict);
1245 if (version != NULL)
1247 mfile = find_namespace_version (namespace, version, &path);
1248 tmp_version = g_strdup (version);
1252 mfile = find_namespace_latest (namespace, &tmp_version, &path);
1257 if (version != NULL)
1258 g_set_error (error, G_IREPOSITORY_ERROR,
1259 G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
1260 "Typelib file for namespace '%s', version '%s' not found",
1261 namespace, version);
1263 g_set_error (error, G_IREPOSITORY_ERROR,
1264 G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
1265 "Typelib file for namespace '%s' (any version) not found",
1271 GError *temp_error = NULL;
1272 typelib = g_typelib_new_from_mapped_file (mfile, &temp_error);
1275 g_set_error (error, G_IREPOSITORY_ERROR,
1276 G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
1277 "Failed to load typelib file '%s' for namespace '%s': %s",
1278 path, namespace, temp_error->message);
1279 g_clear_error (&temp_error);
1283 header = (Header *) typelib->data;
1284 typelib_namespace = g_typelib_get_string (typelib, header->namespace);
1285 typelib_version = g_typelib_get_string (typelib, header->nsversion);
1287 if (strcmp (typelib_namespace, namespace) != 0)
1289 g_set_error (error, G_IREPOSITORY_ERROR,
1290 G_IREPOSITORY_ERROR_NAMESPACE_MISMATCH,
1291 "Typelib file %s for namespace '%s' contains "
1292 "namespace '%s' which doesn't match the file name",
1293 path, namespace, typelib_namespace);
1296 if (version != NULL && strcmp (typelib_version, version) != 0)
1298 g_set_error (error, G_IREPOSITORY_ERROR,
1299 G_IREPOSITORY_ERROR_NAMESPACE_MISMATCH,
1300 "Typelib file %s for namespace '%s' contains "
1301 "version '%s' which doesn't match the expected version '%s'",
1302 path, namespace, typelib_version, version);
1306 if (!register_internal (repository, path, allow_lazy,
1309 g_typelib_free (typelib);
1314 g_free (tmp_version);
1320 g_irepository_introspect_cb (const char *option_name,
1325 gboolean ret = g_irepository_dump (value, error);
1329 static const GOptionEntry introspection_args[] = {
1330 { "introspect-dump", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK,
1331 g_irepository_introspect_cb, "Dump introspection information",
1332 "infile.txt,outfile.xml" },
1337 g_irepository_get_option_group (void)
1339 GOptionGroup *group;
1340 group = g_option_group_new ("girepository", "Introspection Options", "Show Introspection Options", NULL, NULL);
1342 g_option_group_add_entries (group, introspection_args);
1347 g_irepository_error_quark (void)
1349 static GQuark quark = 0;
1351 quark = g_quark_from_static_string ("g-irepository-error-quark");
1356 g_type_tag_to_string (GITypeTag type)
1360 case GI_TYPE_TAG_VOID:
1362 case GI_TYPE_TAG_BOOLEAN:
1364 case GI_TYPE_TAG_INT8:
1366 case GI_TYPE_TAG_UINT8:
1368 case GI_TYPE_TAG_INT16:
1370 case GI_TYPE_TAG_UINT16:
1372 case GI_TYPE_TAG_INT32:
1374 case GI_TYPE_TAG_UINT32:
1376 case GI_TYPE_TAG_INT64:
1378 case GI_TYPE_TAG_UINT64:
1380 case GI_TYPE_TAG_FLOAT:
1382 case GI_TYPE_TAG_DOUBLE:
1384 case GI_TYPE_TAG_GTYPE:
1386 case GI_TYPE_TAG_UTF8:
1388 case GI_TYPE_TAG_FILENAME:
1390 case GI_TYPE_TAG_ARRAY:
1392 case GI_TYPE_TAG_INTERFACE:
1394 case GI_TYPE_TAG_GLIST:
1396 case GI_TYPE_TAG_GSLIST:
1398 case GI_TYPE_TAG_GHASH:
1400 case GI_TYPE_TAG_ERROR:
1408 * g_info_type_to_string:
1411 * Returns: (transfer none): Description for this info type
1414 g_info_type_to_string (GIInfoType type)
1418 case GI_INFO_TYPE_INVALID:
1420 case GI_INFO_TYPE_FUNCTION:
1422 case GI_INFO_TYPE_CALLBACK:
1424 case GI_INFO_TYPE_STRUCT:
1426 case GI_INFO_TYPE_BOXED:
1428 case GI_INFO_TYPE_ENUM:
1430 case GI_INFO_TYPE_FLAGS:
1432 case GI_INFO_TYPE_OBJECT:
1434 case GI_INFO_TYPE_INTERFACE:
1436 case GI_INFO_TYPE_CONSTANT:
1438 case GI_INFO_TYPE_ERROR_DOMAIN:
1439 return "error domain";
1440 case GI_INFO_TYPE_UNION:
1442 case GI_INFO_TYPE_VALUE:
1444 case GI_INFO_TYPE_SIGNAL:
1446 case GI_INFO_TYPE_VFUNC:
1448 case GI_INFO_TYPE_PROPERTY:
1450 case GI_INFO_TYPE_FIELD:
1452 case GI_INFO_TYPE_ARG:
1454 case GI_INFO_TYPE_TYPE:
1456 case GI_INFO_TYPE_UNRESOLVED:
1457 return "unresolved";