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 -> GITypelib */
46 GHashTable *lazy_typelibs; /* (string) namespace-version -> GITypelib */
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)
172 build_search_path_with_overrides (void)
178 if (override_search_path != NULL)
180 result = g_slist_copy (override_search_path);
181 g_slist_last (result)->next = g_slist_copy (search_path);
184 result = g_slist_copy (search_path);
189 build_typelib_key (const char *name, const char *source)
191 GString *str = g_string_new (name);
192 g_string_append_c (str, '\0');
193 g_string_append (str, source);
194 return g_string_free (str, FALSE);
198 get_typelib_dependencies (GITypelib *typelib)
201 const char *dependencies_glob;
203 header = (Header *)typelib->data;
205 if (header->dependencies == 0)
208 dependencies_glob = g_typelib_get_string (typelib, header->dependencies);
209 return g_strsplit (dependencies_glob, "|", 0);
212 static GIRepository *
213 get_repository (GIRepository *repository)
217 if (repository != NULL)
220 return default_repository;
224 check_version_conflict (GITypelib *typelib,
225 const gchar *namespace,
226 const gchar *expected_version,
227 char **version_conflict)
230 const char *loaded_version;
232 if (expected_version == NULL)
234 if (version_conflict)
235 *version_conflict = NULL;
239 header = (Header*)typelib->data;
240 loaded_version = g_typelib_get_string (typelib, header->nsversion);
241 g_assert (loaded_version != NULL);
243 if (strcmp (expected_version, loaded_version) != 0)
245 if (version_conflict)
246 *version_conflict = (char*)loaded_version;
249 if (version_conflict)
250 *version_conflict = NULL;
255 get_registered_status (GIRepository *repository,
256 const char *namespace,
259 gboolean *lazy_status,
260 char **version_conflict)
263 repository = get_repository (repository);
265 *lazy_status = FALSE;
266 typelib = g_hash_table_lookup (repository->priv->typelibs, namespace);
268 return check_version_conflict (typelib, namespace, version, version_conflict);
269 typelib = g_hash_table_lookup (repository->priv->lazy_typelibs, namespace);
276 return check_version_conflict (typelib, namespace, version, version_conflict);
280 get_registered (GIRepository *repository,
281 const char *namespace,
284 return get_registered_status (repository, namespace, version, TRUE, NULL, NULL);
288 load_dependencies_recurse (GIRepository *repository,
294 dependencies = get_typelib_dependencies (typelib);
296 if (dependencies != NULL)
300 for (i = 0; dependencies[i]; i++)
302 char *dependency = dependencies[i];
303 const char *last_dash;
304 char *dependency_namespace;
305 const char *dependency_version;
307 last_dash = strrchr (dependency, '-');
308 dependency_namespace = g_strndup (dependency, last_dash - dependency);
309 dependency_version = last_dash+1;
311 if (!g_irepository_require (repository, dependency_namespace, dependency_version,
314 g_free (dependency_namespace);
315 g_strfreev (dependencies);
318 g_free (dependency_namespace);
320 g_strfreev (dependencies);
326 register_internal (GIRepository *repository,
333 const gchar *namespace;
334 const gchar *version;
336 g_return_val_if_fail (typelib != NULL, FALSE);
338 header = (Header *)typelib->data;
340 g_return_val_if_fail (header != NULL, FALSE);
342 namespace = g_typelib_get_string (typelib, header->namespace);
343 version = g_typelib_get_string (typelib, header->nsversion);
347 g_assert (!g_hash_table_lookup (repository->priv->lazy_typelibs,
349 g_hash_table_insert (repository->priv->lazy_typelibs,
350 build_typelib_key (namespace, source), (void *)typelib);
357 /* First, try loading all the dependencies */
358 if (!load_dependencies_recurse (repository, typelib, error))
361 /* Check if we are transitioning from lazily loaded state */
362 if (g_hash_table_lookup_extended (repository->priv->lazy_typelibs,
364 (gpointer)&key, &value))
365 g_hash_table_remove (repository->priv->lazy_typelibs, key);
367 key = build_typelib_key (namespace, source);
369 g_hash_table_insert (repository->priv->typelibs, key, (void *)typelib);
376 * g_irepository_get_dependencies:
377 * @repository: A #GIRepository, may be %NULL for the default
378 * @namespace_: Namespace of interest
380 * Return an array of all (transitive) dependencies for namespace
381 * @namespace_, including version. The returned strings are of the
382 * form <code>namespace-version</code>.
384 * Note: The namespace must have already been loaded using a function
385 * such as #g_irepository_require before calling this function.
387 * Returns: (transfer full): Zero-terminated string array of versioned dependencies
390 g_irepository_get_dependencies (GIRepository *repository,
391 const char *namespace)
395 g_return_val_if_fail (namespace != NULL, NULL);
397 repository = get_repository (repository);
399 typelib = get_registered (repository, namespace, NULL);
400 g_return_val_if_fail (typelib != NULL, NULL);
402 return get_typelib_dependencies (typelib);
406 g_irepository_load_typelib (GIRepository *repository,
408 GIRepositoryLoadFlags flags,
412 const char *namespace;
413 const char *nsversion;
414 gboolean allow_lazy = flags & G_IREPOSITORY_LOAD_FLAG_LAZY;
416 char *version_conflict;
418 repository = get_repository (repository);
420 header = (Header *) typelib->data;
421 namespace = g_typelib_get_string (typelib, header->namespace);
422 nsversion = g_typelib_get_string (typelib, header->nsversion);
424 if (get_registered_status (repository, namespace, nsversion, allow_lazy,
425 &is_lazy, &version_conflict))
427 if (version_conflict != NULL)
429 g_set_error (error, G_IREPOSITORY_ERROR,
430 G_IREPOSITORY_ERROR_NAMESPACE_VERSION_CONFLICT,
431 "Attempting to load namespace '%s', version '%s', but '%s' is already loaded",
432 namespace, nsversion, version_conflict);
437 return register_internal (repository, "<builtin>",
438 allow_lazy, typelib, error);
442 * g_irepository_is_registered:
443 * @repository: A #GIRepository, may be %NULL for the default
444 * @namespace_: Namespace of interest
445 * @version: (allow-none): Required version, may be %NULL for latest
447 * Check whether a particular namespace (and optionally, a specific
448 * version thereof) is currently loaded. This function is likely to
449 * only be useful in unusual circumstances; in order to act upon
450 * metadata in the namespace, you should call #g_irepository_require
451 * instead which will ensure the namespace is loaded, and return as
452 * quickly as this function will if it has already been loaded.
454 * Returns: %TRUE if namespace-version is loaded, %FALSE otherwise
457 g_irepository_is_registered (GIRepository *repository,
458 const gchar *namespace,
459 const gchar *version)
461 repository = get_repository (repository);
462 return get_registered (repository, namespace, version) != NULL;
466 * g_irepository_get_default:
468 * Returns the singleton process-global default #GIRepository. It is
469 * not currently supported to have multiple repositories in a
470 * particular process, but this function is provided in the unlikely
471 * eventuality that it would become possible, and as a convenience for
472 * higher level language bindings to conform to the GObject method
475 * All methods on #GIRepository also accept %NULL as an instance
476 * parameter to mean this default repository, which is usually more
479 * Returns: (transfer none): The global singleton #GIRepository
482 g_irepository_get_default (void)
484 return get_repository (NULL);
488 * g_irepository_get_n_infos:
489 * @repository: A #GIRepository, may be %NULL for the default
490 * @namespace_: Namespace to inspect
492 * This function returns the number of metadata entries in
493 * given namespace @namespace_. The namespace must have
494 * already been loaded before calling this function.
496 * Returns: number of metadata entries
499 g_irepository_get_n_infos (GIRepository *repository,
500 const gchar *namespace)
503 gint n_interfaces = 0;
505 g_return_val_if_fail (namespace != NULL, -1);
507 repository = get_repository (repository);
509 typelib = get_registered (repository, namespace, NULL);
511 g_return_val_if_fail (typelib != NULL, -1);
513 n_interfaces = ((Header *)typelib->data)->n_local_entries;
523 gboolean type_firstpass;
529 find_interface (gpointer key,
534 GITypelib *typelib = (GITypelib *)value;
535 Header *header = (Header *) typelib->data;
536 IfaceData *iface_data = (IfaceData *)data;
544 n_entries = ((Header *)typelib->data)->n_local_entries;
546 if (iface_data->name)
548 for (i = 1; i <= n_entries; i++)
550 entry = g_typelib_get_dir_entry (typelib, i);
551 name = g_typelib_get_string (typelib, entry->name);
552 if (strcmp (name, iface_data->name) == 0)
559 else if (iface_data->type)
561 const char *c_prefix;
562 /* Inside each typelib, we include the "C prefix" which acts as
563 * a namespace mechanism. For GtkTreeView, the C prefix is Gtk.
564 * Given the assumption that GTypes for a library also use the
565 * C prefix, we know we can skip examining a typelib if our
566 * target type does not have this typelib's C prefix.
568 * However, not every class library necessarily conforms to this,
569 * e.g. Clutter has Cogl inside it. So, we split this into two
570 * passes. First we try a lookup, skipping things which don't
571 * have the prefix. If that fails then we try a global lookup,
572 * ignoring the prefix.
574 * See http://bugzilla.gnome.org/show_bug.cgi?id=564016
576 c_prefix = g_typelib_get_string (typelib, header->c_prefix);
577 if (iface_data->type_firstpass && c_prefix != NULL)
579 if (g_ascii_strncasecmp (c_prefix, iface_data->type, strlen (c_prefix)) != 0)
583 for (i = 1; i <= n_entries; i++)
585 RegisteredTypeBlob *blob;
587 entry = g_typelib_get_dir_entry (typelib, i);
588 if (!BLOB_IS_REGISTERED_TYPE (entry))
591 blob = (RegisteredTypeBlob *)(&typelib->data[entry->offset]);
592 if (!blob->gtype_name)
595 type = g_typelib_get_string (typelib, blob->gtype_name);
596 if (strcmp (type, iface_data->type) == 0)
603 else if (iface_data->index > n_entries)
604 iface_data->index -= n_entries;
605 else if (iface_data->index > 0)
607 index = iface_data->index;
608 iface_data->index = 0;
613 entry = g_typelib_get_dir_entry (typelib, index);
614 iface_data->iface = _g_info_new_full (entry->blob_type,
616 NULL, typelib, entry->offset);
621 * g_irepository_get_info:
622 * @repository: A #GIRepository, may be %NULL for the default
623 * @namespace_: Namespace to inspect
624 * @index: Offset into namespace metadata for entry
626 * This function returns a particular metadata entry in the
627 * given namespace @namespace_. The namespace must have
628 * already been loaded before calling this function.
630 * Returns: (transfer full): #GIBaseInfo containing metadata
633 g_irepository_get_info (GIRepository *repository,
634 const gchar *namespace,
640 g_return_val_if_fail (namespace != NULL, NULL);
642 repository = get_repository (repository);
644 data.repo = repository;
647 data.index = index + 1;
650 typelib = get_registered (repository, namespace, NULL);
652 g_return_val_if_fail (typelib != NULL, NULL);
654 find_interface ((void *)namespace, typelib, &data);
660 * g_irepository_find_by_gtype:
661 * @repository: A #GIRepository, may be %NULL for the default
662 * @gtype: GType to search for
664 * Searches all loaded namespaces for a particular #GType. Note that
665 * in order to locate the metadata, the namespace corresponding to
666 * the type must first have been loaded. There is currently no
667 * mechanism for determining the namespace which corresponds to an
668 * arbitrary GType - thus, this function will operate most reliably
669 * when you know the GType to originate from be from a loaded namespace.
671 * Returns: (transfer full): #GIBaseInfo representing metadata about @type, or %NULL
674 g_irepository_find_by_gtype (GIRepository *repository,
679 repository = get_repository (repository);
681 data.iface = g_hash_table_lookup (repository->priv->info_by_gtype,
685 return g_base_info_ref (data.iface);
687 data.repo = repository;
689 data.type_firstpass = TRUE;
690 data.type = g_type_name (gtype);
694 g_hash_table_foreach (repository->priv->typelibs, find_interface, &data);
695 g_hash_table_foreach (repository->priv->lazy_typelibs, find_interface, &data);
697 /* We do two passes; see comment in find_interface */
700 data.type_firstpass = FALSE;
701 g_hash_table_foreach (repository->priv->typelibs, find_interface, &data);
702 g_hash_table_foreach (repository->priv->lazy_typelibs, find_interface, &data);
706 g_hash_table_insert (repository->priv->info_by_gtype,
708 g_base_info_ref (data.iface));
714 * g_irepository_find_by_name:
715 * @repository: A #GIRepository, may be %NULL for the default
716 * @namespace_: Namespace which will be searched
717 * @name: Entry name to find
719 * Searches for a particular entry in a namespace. Before calling
720 * this function for a particular namespace, you must call
721 * #g_irepository_require once to load the namespace, or otherwise
722 * ensure the namespace has already been loaded.
724 * Returns: (transfer full): #GIBaseInfo representing metadata about @name, or %NULL
727 g_irepository_find_by_name (GIRepository *repository,
728 const gchar *namespace,
734 g_return_val_if_fail (namespace != NULL, NULL);
736 repository = get_repository (repository);
738 data.repo = repository;
744 typelib = get_registered (repository, namespace, NULL);
746 g_return_val_if_fail (typelib != NULL, NULL);
748 find_interface ((void *)namespace, typelib, &data);
754 collect_namespaces (gpointer key,
760 *list = g_list_append (*list, key);
764 * g_irepository_get_loaded_namespaces:
765 * @repository: A #GIRepository, may be %NULL for the default
767 * Return the list of currently loaded namespaces.
769 * Returns: (utf8) (transfer full): List of namespaces
772 g_irepository_get_loaded_namespaces (GIRepository *repository)
774 GList *l, *list = NULL;
778 repository = get_repository (repository);
780 g_hash_table_foreach (repository->priv->typelibs, collect_namespaces, &list);
781 g_hash_table_foreach (repository->priv->lazy_typelibs, collect_namespaces, &list);
783 names = g_malloc0 (sizeof (gchar *) * (g_list_length (list) + 1));
785 for (l = list; l; l = l->next)
786 names[i++] = g_strdup (l->data);
793 * g_irepository_get_version:
794 * @repository: A #GIRepository, may be %NULL for the default
795 * @namespace_: Namespace to inspect
797 * This function returns the loaded version associated with the given
798 * namespace @namespace_.
800 * Note: The namespace must have already been loaded using a function
801 * such as #g_irepository_require before calling this function.
803 * Returns: Loaded version
806 g_irepository_get_version (GIRepository *repository,
807 const gchar *namespace)
812 g_return_val_if_fail (namespace != NULL, NULL);
814 repository = get_repository (repository);
816 typelib = get_registered (repository, namespace, NULL);
818 g_return_val_if_fail (typelib != NULL, NULL);
820 header = (Header *) typelib->data;
821 return g_typelib_get_string (typelib, header->nsversion);
825 * g_irepository_get_shared_library:
826 * @repository: A #GIRepository, may be %NULL for the default
827 * @namespace_: Namespace to inspect
829 * This function returns the full path to the shared C library
830 * associated with the given namespace @namespace_. There may be no
831 * shared library path associated, in which case this function will
834 * Note: The namespace must have already been loaded using a function
835 * such as #g_irepository_require before calling this function.
837 * Returns: Full path to shared library, or %NULL if none associated
840 g_irepository_get_shared_library (GIRepository *repository,
841 const gchar *namespace)
846 g_return_val_if_fail (namespace != NULL, NULL);
848 repository = get_repository (repository);
850 typelib = get_registered (repository, namespace, NULL);
852 g_return_val_if_fail (typelib != NULL, NULL);
854 header = (Header *) typelib->data;
855 if (header->shared_library)
856 return g_typelib_get_string (typelib, header->shared_library);
862 * g_irepository_get_c_prefix
863 * @repository: A #GIRepository, may be %NULL for the default
864 * @namespace_: Namespace to inspect
866 * This function returns the "C prefix", or the C level namespace
867 * associated with the given introspection namespace. Each C symbol
868 * starts with this prefix, as well each #GType in the library.
870 * Note: The namespace must have already been loaded using a function
871 * such as #g_irepository_require before calling this function.
873 * Returns: C namespace prefix, or %NULL if none associated
876 g_irepository_get_c_prefix (GIRepository *repository,
877 const gchar *namespace_)
882 g_return_val_if_fail (namespace_ != NULL, NULL);
884 repository = get_repository (repository);
886 typelib = get_registered (repository, namespace_, NULL);
888 g_return_val_if_fail (typelib != NULL, NULL);
890 header = (Header *) typelib->data;
891 if (header->shared_library)
892 return g_typelib_get_string (typelib, header->c_prefix);
898 * g_irepository_get_typelib_path
899 * @repository: Repository, may be %NULL for the default
900 * @namespace_: GI namespace to use, e.g. "Gtk"
902 * If namespace @namespace_ is loaded, return the full path to the
903 * .typelib file it was loaded from. If the typelib for
904 * namespace @namespace_ was included in a shared library, return
905 * the special string "$lt;builtin$gt;".
907 * Returns: Filesystem path (or $lt;builtin$gt;) if successful, %NULL if namespace is not loaded
911 g_irepository_get_typelib_path (GIRepository *repository,
912 const gchar *namespace)
914 gpointer orig_key, value;
916 repository = get_repository (repository);
918 if (!g_hash_table_lookup_extended (repository->priv->typelibs, namespace,
921 if (!g_hash_table_lookup_extended (repository->priv->lazy_typelibs, namespace,
926 return ((char*)orig_key) + strlen ((char *) orig_key) + 1;
929 /* This simple search function looks for a specified namespace-version;
930 it's faster than the full directory listing required for latest version. */
932 find_namespace_version (const gchar *namespace,
933 const gchar *version,
938 GError *error = NULL;
939 GMappedFile *mfile = NULL;
942 fname = g_strdup_printf ("%s-%s.typelib", namespace, version);
944 for (ldir = search_path; ldir; ldir = ldir->next)
946 char *path = g_build_filename (ldir->data, fname, NULL);
948 mfile = g_mapped_file_new (path, FALSE, &error);
952 g_clear_error (&error);
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,
1054 GSList *search_path)
1056 GSList *candidates = NULL;
1057 GHashTable *found_versions = g_hash_table_new (g_str_hash, g_str_equal);
1058 char *namespace_dash;
1059 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 for (ldir = search_path; ldir; ldir = ldir->next)
1071 const char *dirname;
1074 dirname = (const char*)ldir->data;
1075 dir = g_dir_open (dirname, 0, NULL);
1078 while ((entry = g_dir_read_name (dir)) != NULL)
1081 char *path, *version;
1082 struct NamespaceVersionCandidadate *candidate;
1084 if (!g_str_has_suffix (entry, ".typelib"))
1087 if (g_str_has_prefix (entry, namespace_dash))
1089 const char *last_dash;
1090 const char *name_end;
1093 name_end = strrchr (entry, '.');
1094 last_dash = strrchr (entry, '-');
1095 version = g_strndup (last_dash+1, name_end-(last_dash+1));
1096 if (!parse_version (version, &major, &minor))
1102 if (g_hash_table_lookup (found_versions, version) != NULL)
1104 g_hash_table_insert (found_versions, version, version);
1106 path = g_build_filename (dirname, entry, NULL);
1107 mfile = g_mapped_file_new (path, FALSE, &error);
1112 g_clear_error (&error);
1115 candidate = g_slice_new0 (struct NamespaceVersionCandidadate);
1116 candidate->mfile = mfile;
1117 candidate->path_index = index;
1118 candidate->path = path;
1119 candidate->version = version;
1120 candidates = g_slist_prepend (candidates, candidate);
1126 g_free (namespace_dash);
1127 g_free (namespace_typelib);
1128 g_hash_table_destroy (found_versions);
1133 static GMappedFile *
1134 find_namespace_latest (const gchar *namespace,
1135 GSList *search_path,
1136 gchar **version_ret,
1140 GMappedFile *result = NULL;
1142 *version_ret = NULL;
1145 candidates = enumerate_namespace_versions (namespace, search_path);
1147 if (candidates != NULL)
1149 struct NamespaceVersionCandidadate *elected;
1150 candidates = g_slist_sort (candidates, (GCompareFunc) compare_candidate_reverse);
1152 elected = (struct NamespaceVersionCandidadate *) candidates->data;
1153 /* Remove the elected one so we don't try to free its contents */
1154 candidates = g_slist_delete_link (candidates, candidates);
1156 result = elected->mfile;
1157 *path_ret = elected->path;
1158 *version_ret = elected->version;
1159 g_slice_free (struct NamespaceVersionCandidadate, elected); /* just free the container */
1160 g_slist_foreach (candidates, (GFunc) free_candidate, NULL);
1161 g_slist_free (candidates);
1167 * g_irepository_enumerate_versions:
1168 * @repository: (allow-none): the repository
1169 * @namespace_: GI namespace, e.g. "Gtk"
1171 * Obtain an unordered list of versions (either currently loaded or
1172 * available) for @namespace_ in this @repository.
1174 * Returns: (element-type utf8) (transfer full): the array of versions.
1177 g_irepository_enumerate_versions (GIRepository *repository,
1178 const gchar *namespace_)
1181 GSList *search_path;
1182 GSList *candidates, *link;
1183 const gchar *loaded_version;
1185 search_path = build_search_path_with_overrides ();
1186 candidates = enumerate_namespace_versions (namespace_, search_path);
1187 g_slist_free (search_path);
1189 for (link = candidates; link; link = link->next)
1191 struct NamespaceVersionCandidadate *candidate = link->data;
1192 ret = g_list_prepend (ret, g_strdup (candidate->version));
1193 free_candidate (candidate);
1195 g_slist_free (candidates);
1197 /* The currently loaded version of a namespace is also part of the
1198 * available versions, as it could have been loaded using
1199 * require_private().
1201 if (g_irepository_is_registered (repository, namespace_, NULL))
1203 loaded_version = g_irepository_get_version (repository, namespace_);
1204 if (loaded_version && !g_list_find_custom (ret, loaded_version, g_str_equal))
1205 ret = g_list_prepend (ret, g_strdup (loaded_version));
1212 require_internal (GIRepository *repository,
1213 const gchar *namespace,
1214 const gchar *version,
1215 GIRepositoryLoadFlags flags,
1216 GSList *search_path,
1220 GITypelib *ret = NULL;
1222 GITypelib *typelib = NULL;
1223 const gchar *typelib_namespace, *typelib_version;
1224 gboolean allow_lazy = (flags & G_IREPOSITORY_LOAD_FLAG_LAZY) > 0;
1226 char *version_conflict = NULL;
1228 char *tmp_version = NULL;
1230 g_return_val_if_fail (namespace != NULL, FALSE);
1232 repository = get_repository (repository);
1234 typelib = get_registered_status (repository, namespace, version, allow_lazy,
1235 &is_lazy, &version_conflict);
1239 if (version_conflict != NULL)
1241 g_set_error (error, G_IREPOSITORY_ERROR,
1242 G_IREPOSITORY_ERROR_NAMESPACE_VERSION_CONFLICT,
1243 "Requiring namespace '%s' version '%s', but '%s' is already loaded",
1244 namespace, version, version_conflict);
1248 if (version != NULL)
1250 mfile = find_namespace_version (namespace, version,
1251 search_path, &path);
1252 tmp_version = g_strdup (version);
1256 mfile = find_namespace_latest (namespace, search_path,
1257 &tmp_version, &path);
1262 if (version != NULL)
1263 g_set_error (error, G_IREPOSITORY_ERROR,
1264 G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
1265 "Typelib file for namespace '%s', version '%s' not found",
1266 namespace, version);
1268 g_set_error (error, G_IREPOSITORY_ERROR,
1269 G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
1270 "Typelib file for namespace '%s' (any version) not found",
1276 GError *temp_error = NULL;
1277 typelib = g_typelib_new_from_mapped_file (mfile, &temp_error);
1280 g_set_error (error, G_IREPOSITORY_ERROR,
1281 G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
1282 "Failed to load typelib file '%s' for namespace '%s': %s",
1283 path, namespace, temp_error->message);
1284 g_clear_error (&temp_error);
1288 header = (Header *) typelib->data;
1289 typelib_namespace = g_typelib_get_string (typelib, header->namespace);
1290 typelib_version = g_typelib_get_string (typelib, header->nsversion);
1292 if (strcmp (typelib_namespace, namespace) != 0)
1294 g_set_error (error, G_IREPOSITORY_ERROR,
1295 G_IREPOSITORY_ERROR_NAMESPACE_MISMATCH,
1296 "Typelib file %s for namespace '%s' contains "
1297 "namespace '%s' which doesn't match the file name",
1298 path, namespace, typelib_namespace);
1299 g_typelib_free (typelib);
1302 if (version != NULL && strcmp (typelib_version, version) != 0)
1304 g_set_error (error, G_IREPOSITORY_ERROR,
1305 G_IREPOSITORY_ERROR_NAMESPACE_MISMATCH,
1306 "Typelib file %s for namespace '%s' contains "
1307 "version '%s' which doesn't match the expected version '%s'",
1308 path, namespace, typelib_version, version);
1309 g_typelib_free (typelib);
1313 if (!register_internal (repository, path, allow_lazy,
1316 g_typelib_free (typelib);
1321 g_free (tmp_version);
1327 * g_irepository_require:
1328 * @repository: (allow-none): Repository, may be %NULL for the default
1329 * @namespace_: GI namespace to use, e.g. "Gtk"
1330 * @version: (allow-none): Version of namespace, may be %NULL for latest
1331 * @flags: Set of %GIRepositoryLoadFlags, may be 0
1332 * @error: a #GError.
1334 * Force the namespace @namespace_ to be loaded if it isn't already.
1335 * If @namespace_ is not loaded, this function will search for a
1336 * ".typelib" file using the repository search path. In addition, a
1337 * version @version of namespace may be specified. If @version is
1338 * not specified, the latest will be used.
1340 * Returns: (transfer full): a pointer to the #GITypelib if successful, %NULL otherwise
1343 g_irepository_require (GIRepository *repository,
1344 const gchar *namespace,
1345 const gchar *version,
1346 GIRepositoryLoadFlags flags,
1349 GSList *search_path;
1352 search_path = build_search_path_with_overrides ();
1353 typelib = require_internal (repository, namespace, version, flags,
1354 search_path, error);
1355 g_slist_free (search_path);
1361 * g_irepository_require_private:
1362 * @repository: (allow-none): Repository, may be %NULL for the default
1363 * @typelib_dir: Private directory where to find the requested typelib
1364 * @namespace_: GI namespace to use, e.g. "Gtk"
1365 * @version: (allow-none): Version of namespace, may be %NULL for latest
1366 * @flags: Set of %GIRepositoryLoadFlags, may be 0
1367 * @error: a #GError.
1369 * Force the namespace @namespace_ to be loaded if it isn't already.
1370 * If @namespace_ is not loaded, this function will search for a
1371 * ".typelib" file within the private directory only. In addition, a
1372 * version @version of namespace should be specified. If @version is
1373 * not specified, the latest will be used.
1375 * Returns: (transfer full): a pointer to the #GITypelib if successful, %NULL otherwise
1378 g_irepository_require_private (GIRepository *repository,
1379 const gchar *typelib_dir,
1380 const gchar *namespace,
1381 const gchar *version,
1382 GIRepositoryLoadFlags flags,
1385 GSList search_path = { (gpointer) typelib_dir, NULL };
1387 return require_internal (repository, namespace, version, flags,
1388 &search_path, error);
1392 g_irepository_introspect_cb (const char *option_name,
1397 GError *tmp_error = NULL;
1398 gboolean ret = g_irepository_dump (value, &tmp_error);
1401 g_error ("Failed to extract GType data: %s",
1402 tmp_error->message);
1408 static const GOptionEntry introspection_args[] = {
1409 { "introspect-dump", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK,
1410 g_irepository_introspect_cb, "Dump introspection information",
1411 "infile.txt,outfile.xml" },
1416 * g_irepository_get_option_group: (skip)
1418 * Obtain the option group for girepository, it's used
1419 * by the dumper and for programs that wants to provide
1420 * introspection information
1422 * Returns: (transfer full): the option group
1425 g_irepository_get_option_group (void)
1427 GOptionGroup *group;
1428 group = g_option_group_new ("girepository", "Introspection Options", "Show Introspection Options", NULL, NULL);
1430 g_option_group_add_entries (group, introspection_args);
1435 g_irepository_error_quark (void)
1437 static GQuark quark = 0;
1439 quark = g_quark_from_static_string ("g-irepository-error-quark");
1444 * g_type_tag_to_string:
1445 * @type: the type_tag
1447 * Obtain a string representation of @type
1449 * Returns: the string
1452 g_type_tag_to_string (GITypeTag type)
1456 case GI_TYPE_TAG_VOID:
1458 case GI_TYPE_TAG_BOOLEAN:
1460 case GI_TYPE_TAG_INT8:
1462 case GI_TYPE_TAG_UINT8:
1464 case GI_TYPE_TAG_INT16:
1466 case GI_TYPE_TAG_UINT16:
1468 case GI_TYPE_TAG_INT32:
1470 case GI_TYPE_TAG_UINT32:
1472 case GI_TYPE_TAG_INT64:
1474 case GI_TYPE_TAG_UINT64:
1476 case GI_TYPE_TAG_FLOAT:
1478 case GI_TYPE_TAG_DOUBLE:
1480 case GI_TYPE_TAG_GTYPE:
1482 case GI_TYPE_TAG_UTF8:
1484 case GI_TYPE_TAG_FILENAME:
1486 case GI_TYPE_TAG_ARRAY:
1488 case GI_TYPE_TAG_INTERFACE:
1490 case GI_TYPE_TAG_GLIST:
1492 case GI_TYPE_TAG_GSLIST:
1494 case GI_TYPE_TAG_GHASH:
1496 case GI_TYPE_TAG_ERROR:
1504 * g_info_type_to_string:
1505 * @type: the info type
1507 * Obtain a string representation of @type
1509 * Returns: the string
1512 g_info_type_to_string (GIInfoType type)
1516 case GI_INFO_TYPE_INVALID:
1518 case GI_INFO_TYPE_FUNCTION:
1520 case GI_INFO_TYPE_CALLBACK:
1522 case GI_INFO_TYPE_STRUCT:
1524 case GI_INFO_TYPE_BOXED:
1526 case GI_INFO_TYPE_ENUM:
1528 case GI_INFO_TYPE_FLAGS:
1530 case GI_INFO_TYPE_OBJECT:
1532 case GI_INFO_TYPE_INTERFACE:
1534 case GI_INFO_TYPE_CONSTANT:
1536 case GI_INFO_TYPE_ERROR_DOMAIN:
1537 return "error domain";
1538 case GI_INFO_TYPE_UNION:
1540 case GI_INFO_TYPE_VALUE:
1542 case GI_INFO_TYPE_SIGNAL:
1544 case GI_INFO_TYPE_VFUNC:
1546 case GI_INFO_TYPE_PROPERTY:
1548 case GI_INFO_TYPE_FIELD:
1550 case GI_INFO_TYPE_ARG:
1552 case GI_INFO_TYPE_TYPE:
1554 case GI_INFO_TYPE_UNRESOLVED:
1555 return "unresolved";