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"
37 static GStaticMutex globals_lock = G_STATIC_MUTEX_INIT;
38 static GIRepository *default_repository = NULL;
39 static GSList *search_path = NULL;
40 static GSList *override_search_path = NULL;
42 struct _GIRepositoryPrivate
44 GHashTable *typelibs; /* (string) namespace -> GTypelib */
45 GHashTable *lazy_typelibs; /* (string) namespace-version -> GTypelib */
46 GHashTable *info_by_gtype; /* GType -> GIBaseInfo */
49 G_DEFINE_TYPE (GIRepository, g_irepository, G_TYPE_OBJECT);
52 g_irepository_init (GIRepository *repository)
54 repository->priv = G_TYPE_INSTANCE_GET_PRIVATE (repository, G_TYPE_IREPOSITORY,
56 repository->priv->typelibs
57 = g_hash_table_new_full (g_str_hash, g_str_equal,
58 (GDestroyNotify) NULL,
59 (GDestroyNotify) g_typelib_free);
60 repository->priv->lazy_typelibs
61 = g_hash_table_new (g_str_hash, g_str_equal);
62 repository->priv->info_by_gtype
63 = g_hash_table_new_full (g_direct_hash, g_direct_equal,
64 (GDestroyNotify) NULL,
65 (GDestroyNotify) g_base_info_unref);
69 g_irepository_finalize (GObject *object)
71 GIRepository *repository = G_IREPOSITORY (object);
73 g_hash_table_destroy (repository->priv->typelibs);
74 g_hash_table_destroy (repository->priv->lazy_typelibs);
75 g_hash_table_destroy (repository->priv->info_by_gtype);
77 (* G_OBJECT_CLASS (g_irepository_parent_class)->finalize) (G_OBJECT (repository));
81 g_irepository_class_init (GIRepositoryClass *class)
83 GObjectClass *gobject_class;
85 gobject_class = G_OBJECT_CLASS (class);
87 gobject_class->finalize = g_irepository_finalize;
89 g_type_class_add_private (class, sizeof (GIRepositoryPrivate));
95 g_static_mutex_lock (&globals_lock);
97 if (default_repository == NULL)
99 default_repository = g_object_new (G_TYPE_IREPOSITORY, NULL);
102 if (search_path == NULL)
106 const gchar *type_lib_path_env;
108 /* This variable is intended to take precedence over both the default
109 * search path, as well as anything written into code with g_irepository_prepend_search_path.
111 type_lib_path_env = g_getenv ("GI_TYPELIB_PATH");
114 override_search_path = NULL;
115 if (type_lib_path_env)
120 custom_dirs = g_strsplit (type_lib_path_env, G_SEARCHPATH_SEPARATOR_S, 0);
125 override_search_path = g_slist_prepend (override_search_path, *d);
129 /* ownership of the array content was passed to the list */
130 g_free (custom_dirs);
133 libdir = GOBJECT_INTROSPECTION_LIBDIR;
135 typelib_dir = g_build_filename (libdir, "girepository-1.0", NULL);
137 search_path = g_slist_prepend (search_path, typelib_dir);
139 search_path = g_slist_reverse (search_path);
142 g_static_mutex_unlock (&globals_lock);
146 g_irepository_prepend_search_path (const char *directory)
149 search_path = g_slist_prepend (search_path, g_strdup (directory));
153 * g_irepository_get_search_path:
155 * Returns the search path the GIRepository will use when looking for typelibs.
156 * The string is internal to GIRespository and should not be freed, nor should
159 * Return value: (element-type filename) (transfer none): list of strings
162 g_irepository_get_search_path (void)
169 build_search_path_with_overrides (void)
172 if (override_search_path != NULL)
174 result = g_slist_copy (override_search_path);
175 g_slist_last (result)->next = g_slist_copy (search_path);
178 result = g_slist_copy (search_path);
183 build_typelib_key (const char *name, const char *source)
185 GString *str = g_string_new (name);
186 g_string_append_c (str, '\0');
187 g_string_append (str, source);
188 return g_string_free (str, FALSE);
192 get_typelib_dependencies (GTypelib *typelib)
195 const char *dependencies_glob;
197 header = (Header *)typelib->data;
199 if (header->dependencies == 0)
202 dependencies_glob = g_typelib_get_string (typelib, header->dependencies);
203 return g_strsplit (dependencies_glob, "|", 0);
206 static GIRepository *
207 get_repository (GIRepository *repository)
211 if (repository != NULL)
214 return default_repository;
218 check_version_conflict (GTypelib *typelib,
219 const gchar *namespace,
220 const gchar *expected_version,
221 char **version_conflict)
224 const char *loaded_version;
226 if (expected_version == NULL)
228 if (version_conflict)
229 *version_conflict = NULL;
233 header = (Header*)typelib->data;
234 loaded_version = g_typelib_get_string (typelib, header->nsversion);
235 g_assert (loaded_version != NULL);
237 if (strcmp (expected_version, loaded_version) != 0)
239 if (version_conflict)
240 *version_conflict = (char*)loaded_version;
243 if (version_conflict)
244 *version_conflict = NULL;
249 get_registered_status (GIRepository *repository,
250 const char *namespace,
253 gboolean *lazy_status,
254 char **version_conflict)
257 repository = get_repository (repository);
259 *lazy_status = FALSE;
260 typelib = g_hash_table_lookup (repository->priv->typelibs, namespace);
262 return check_version_conflict (typelib, namespace, version, version_conflict);
263 typelib = g_hash_table_lookup (repository->priv->lazy_typelibs, namespace);
270 return check_version_conflict (typelib, namespace, version, version_conflict);
274 get_registered (GIRepository *repository,
275 const char *namespace,
278 return get_registered_status (repository, namespace, version, TRUE, NULL, NULL);
282 load_dependencies_recurse (GIRepository *repository,
288 dependencies = get_typelib_dependencies (typelib);
290 if (dependencies != NULL)
294 for (i = 0; dependencies[i]; i++)
296 char *dependency = dependencies[i];
297 const char *last_dash;
298 char *dependency_namespace;
299 const char *dependency_version;
301 last_dash = strrchr (dependency, '-');
302 dependency_namespace = g_strndup (dependency, last_dash - dependency);
303 dependency_version = last_dash+1;
305 if (!g_irepository_require (repository, dependency_namespace, dependency_version,
308 g_free (dependency_namespace);
309 g_strfreev (dependencies);
312 g_free (dependency_namespace);
314 g_strfreev (dependencies);
320 register_internal (GIRepository *repository,
327 const gchar *namespace;
328 const gchar *version;
330 g_return_val_if_fail (typelib != NULL, FALSE);
332 header = (Header *)typelib->data;
334 g_return_val_if_fail (header != NULL, FALSE);
336 namespace = g_typelib_get_string (typelib, header->namespace);
337 version = g_typelib_get_string (typelib, header->nsversion);
341 g_assert (!g_hash_table_lookup (repository->priv->lazy_typelibs,
343 g_hash_table_insert (repository->priv->lazy_typelibs,
344 build_typelib_key (namespace, source), (void *)typelib);
351 /* First, try loading all the dependencies */
352 if (!load_dependencies_recurse (repository, typelib, error))
355 /* Check if we are transitioning from lazily loaded state */
356 if (g_hash_table_lookup_extended (repository->priv->lazy_typelibs,
358 (gpointer)&key, &value))
359 g_hash_table_remove (repository->priv->lazy_typelibs, key);
361 key = build_typelib_key (namespace, source);
363 g_hash_table_insert (repository->priv->typelibs, key, (void *)typelib);
370 * g_irepository_get_dependencies:
371 * @repository: A #GIRepository, may be %NULL for the default
372 * @namespace_: Namespace of interest
374 * Return an array of all (transitive) dependencies for namespace
375 * @namespace_, including version. The returned strings are of the
376 * form <code>namespace-version</code>.
378 * Note: The namespace must have already been loaded using a function
379 * such as #g_irepository_require before calling this function.
381 * Returns: Zero-terminated string array of versioned dependencies
384 g_irepository_get_dependencies (GIRepository *repository,
385 const char *namespace)
389 g_return_val_if_fail (namespace != NULL, NULL);
391 repository = get_repository (repository);
393 typelib = get_registered (repository, namespace, NULL);
394 g_return_val_if_fail (typelib != NULL, NULL);
396 return get_typelib_dependencies (typelib);
400 g_irepository_load_typelib (GIRepository *repository,
402 GIRepositoryLoadFlags flags,
406 const char *namespace;
407 const char *nsversion;
408 gboolean allow_lazy = flags & G_IREPOSITORY_LOAD_FLAG_LAZY;
410 char *version_conflict;
412 repository = get_repository (repository);
414 header = (Header *) typelib->data;
415 namespace = g_typelib_get_string (typelib, header->namespace);
416 nsversion = g_typelib_get_string (typelib, header->nsversion);
418 if (get_registered_status (repository, namespace, nsversion, allow_lazy,
419 &is_lazy, &version_conflict))
421 if (version_conflict != NULL)
423 g_set_error (error, G_IREPOSITORY_ERROR,
424 G_IREPOSITORY_ERROR_NAMESPACE_VERSION_CONFLICT,
425 "Attempting to load namespace '%s', version '%s', but '%s' is already loaded",
426 namespace, nsversion, version_conflict);
431 return register_internal (repository, "<builtin>",
432 allow_lazy, typelib, error);
436 * g_irepository_is_registered:
437 * @repository: A #GIRepository, may be %NULL for the default
438 * @namespace_: Namespace of interest
439 * @version: (allow-none): Required version, may be %NULL for latest
441 * Check whether a particular namespace (and optionally, a specific
442 * version thereof) is currently loaded. This function is likely to
443 * only be useful in unusual circumstances; in order to act upon
444 * metadata in the namespace, you should call #g_irepository_require
445 * instead which will ensure the namespace is loaded, and return as
446 * quickly as this function will if it has already been loaded.
448 * Returns: %TRUE if namespace-version is loaded, %FALSE otherwise
451 g_irepository_is_registered (GIRepository *repository,
452 const gchar *namespace,
453 const gchar *version)
455 repository = get_repository (repository);
456 return get_registered (repository, namespace, version) != NULL;
460 * g_irepository_get_default:
462 * Returns the singleton process-global default #GIRepository. It is
463 * not currently supported to have multiple repositories in a
464 * particular process, but this function is provided in the unlikely
465 * eventuality that it would become possible, and as a convenience for
466 * higher level language bindings to conform to the GObject method
469 * All methods on #GIRepository also accept %NULL as an instance
470 * parameter to mean this default repository, which is usually more
473 * Returns: (transfer none): The global singleton #GIRepository
476 g_irepository_get_default (void)
478 return get_repository (NULL);
482 * g_irepository_get_n_infos:
483 * @repository: A #GIRepository, may be %NULL for the default
484 * @namespace_: Namespace to inspect
486 * This function returns the number of metadata entries in
487 * given namespace @namespace_. The namespace must have
488 * already been loaded before calling this function.
490 * Returns: number of metadata entries
493 g_irepository_get_n_infos (GIRepository *repository,
494 const gchar *namespace)
497 gint n_interfaces = 0;
499 g_return_val_if_fail (namespace != NULL, -1);
501 repository = get_repository (repository);
503 typelib = get_registered (repository, namespace, NULL);
505 g_return_val_if_fail (typelib != NULL, -1);
507 n_interfaces = ((Header *)typelib->data)->n_local_entries;
517 gboolean type_firstpass;
523 find_interface (gpointer key,
528 GTypelib *typelib = (GTypelib *)value;
529 Header *header = (Header *) typelib->data;
530 IfaceData *iface_data = (IfaceData *)data;
538 n_entries = ((Header *)typelib->data)->n_local_entries;
540 if (iface_data->name)
542 for (i = 1; i <= n_entries; i++)
544 entry = g_typelib_get_dir_entry (typelib, i);
545 name = g_typelib_get_string (typelib, entry->name);
546 if (strcmp (name, iface_data->name) == 0)
553 else if (iface_data->type)
555 const char *c_prefix;
556 /* Inside each typelib, we include the "C prefix" which acts as
557 * a namespace mechanism. For GtkTreeView, the C prefix is Gtk.
558 * Given the assumption that GTypes for a library also use the
559 * C prefix, we know we can skip examining a typelib if our
560 * target type does not have this typelib's C prefix.
562 * However, not every class library necessarily conforms to this,
563 * e.g. Clutter has Cogl inside it. So, we split this into two
564 * passes. First we try a lookup, skipping things which don't
565 * have the prefix. If that fails then we try a global lookup,
566 * ignoring the prefix.
568 * See http://bugzilla.gnome.org/show_bug.cgi?id=564016
570 c_prefix = g_typelib_get_string (typelib, header->c_prefix);
571 if (iface_data->type_firstpass && c_prefix != NULL)
573 if (g_ascii_strncasecmp (c_prefix, iface_data->type, strlen (c_prefix)) != 0)
577 for (i = 1; i <= n_entries; i++)
579 RegisteredTypeBlob *blob;
581 entry = g_typelib_get_dir_entry (typelib, i);
582 if (!BLOB_IS_REGISTERED_TYPE (entry))
585 blob = (RegisteredTypeBlob *)(&typelib->data[entry->offset]);
586 if (!blob->gtype_name)
589 type = g_typelib_get_string (typelib, blob->gtype_name);
590 if (strcmp (type, iface_data->type) == 0)
597 else if (iface_data->index > n_entries)
598 iface_data->index -= n_entries;
599 else if (iface_data->index > 0)
601 index = iface_data->index;
602 iface_data->index = 0;
607 entry = g_typelib_get_dir_entry (typelib, index);
608 iface_data->iface = g_info_new_full (entry->blob_type,
610 NULL, typelib, entry->offset);
615 * g_irepository_get_info:
616 * @repository: A #GIRepository, may be %NULL for the default
617 * @namespace_: Namespace to inspect
618 * @index: Offset into namespace metadata for entry
620 * This function returns a particular metadata entry in the
621 * given namespace @namespace_. The namespace must have
622 * already been loaded before calling this function.
624 * Returns: #GIBaseInfo containing metadata
627 g_irepository_get_info (GIRepository *repository,
628 const gchar *namespace,
634 g_return_val_if_fail (namespace != NULL, NULL);
636 repository = get_repository (repository);
638 data.repo = repository;
641 data.index = index + 1;
644 typelib = get_registered (repository, namespace, NULL);
646 g_return_val_if_fail (typelib != NULL, NULL);
648 find_interface ((void *)namespace, typelib, &data);
654 * g_irepository_find_by_gtype:
655 * @repository: A #GIRepository, may be %NULL for the default
656 * @gtype: GType to search for
658 * Searches all loaded namespaces for a particular #GType. Note that
659 * in order to locate the metadata, the namespace corresponding to
660 * the type must first have been loaded. There is currently no
661 * mechanism for determining the namespace which corresponds to an
662 * arbitrary GType - thus, this function will operate most reliably
663 * when you know the GType to originate from be from a loaded namespace.
665 * Returns: #GIBaseInfo representing metadata about @type, or %NULL
668 g_irepository_find_by_gtype (GIRepository *repository,
673 repository = get_repository (repository);
675 data.iface = g_hash_table_lookup (repository->priv->info_by_gtype,
679 return g_base_info_ref (data.iface);
681 data.repo = repository;
683 data.type_firstpass = TRUE;
684 data.type = g_type_name (gtype);
688 g_hash_table_foreach (repository->priv->typelibs, find_interface, &data);
689 g_hash_table_foreach (repository->priv->lazy_typelibs, find_interface, &data);
691 /* We do two passes; see comment in find_interface */
694 data.type_firstpass = FALSE;
695 g_hash_table_foreach (repository->priv->typelibs, find_interface, &data);
696 g_hash_table_foreach (repository->priv->lazy_typelibs, find_interface, &data);
700 g_hash_table_insert (repository->priv->info_by_gtype,
702 g_base_info_ref (data.iface));
708 * g_irepository_find_by_name:
709 * @repository: A #GIRepository, may be %NULL for the default
710 * @namespace_: Namespace which will be searched
711 * @name: Entry name to find
713 * Searches for a particular entry in a namespace. Before calling
714 * this function for a particular namespace, you must call
715 * #g_irepository_require once to load the namespace, or otherwise
716 * ensure the namespace has already been loaded.
718 * Returns: #GIBaseInfo representing metadata about @name, or %NULL
721 g_irepository_find_by_name (GIRepository *repository,
722 const gchar *namespace,
728 g_return_val_if_fail (namespace != NULL, NULL);
730 repository = get_repository (repository);
732 data.repo = repository;
738 typelib = get_registered (repository, namespace, NULL);
740 g_return_val_if_fail (typelib != NULL, NULL);
742 find_interface ((void *)namespace, typelib, &data);
748 collect_namespaces (gpointer key,
754 *list = g_list_append (*list, key);
758 * g_irepository_get_namespaces:
759 * @repository: A #GIRepository, may be %NULL for the default
761 * Return the list of currently loaded namespaces.
763 * Returns: (utf8) (transfer full): List of namespaces
766 g_irepository_get_loaded_namespaces (GIRepository *repository)
768 GList *l, *list = NULL;
772 repository = get_repository (repository);
774 g_hash_table_foreach (repository->priv->typelibs, collect_namespaces, &list);
775 g_hash_table_foreach (repository->priv->lazy_typelibs, collect_namespaces, &list);
777 names = g_malloc0 (sizeof (gchar *) * (g_list_length (list) + 1));
779 for (l = list; l; l = l->next)
780 names[i++] = g_strdup (l->data);
787 * g_irepository_get_version:
788 * @repository: A #GIRepository, may be %NULL for the default
789 * @namespace_: Namespace to inspect
791 * This function returns the loaded version associated with the given
792 * namespace @namespace_.
794 * Note: The namespace must have already been loaded using a function
795 * such as #g_irepository_require before calling this function.
797 * Returns: Loaded version
800 g_irepository_get_version (GIRepository *repository,
801 const gchar *namespace)
806 g_return_val_if_fail (namespace != NULL, NULL);
808 repository = get_repository (repository);
810 typelib = get_registered (repository, namespace, NULL);
812 g_return_val_if_fail (typelib != NULL, NULL);
814 header = (Header *) typelib->data;
815 return g_typelib_get_string (typelib, header->nsversion);
819 * g_irepository_get_shared_library:
820 * @repository: A #GIRepository, may be %NULL for the default
821 * @namespace_: Namespace to inspect
823 * This function returns the full path to the shared C library
824 * associated with the given namespace @namespace_. There may be no
825 * shared library path associated, in which case this function will
828 * Note: The namespace must have already been loaded using a function
829 * such as #g_irepository_require before calling this function.
831 * Returns: Full path to shared library, or %NULL if none associated
834 g_irepository_get_shared_library (GIRepository *repository,
835 const gchar *namespace)
840 g_return_val_if_fail (namespace != NULL, NULL);
842 repository = get_repository (repository);
844 typelib = get_registered (repository, namespace, NULL);
846 g_return_val_if_fail (typelib != NULL, NULL);
848 header = (Header *) typelib->data;
849 if (header->shared_library)
850 return g_typelib_get_string (typelib, header->shared_library);
856 * g_irepository_get_c_prefix
857 * @repository: A #GIRepository, may be %NULL for the default
858 * @namespace: Namespace to inspect
860 * This function returns the "C prefix", or the C level namespace
861 * associated with the given introspection namespace. Each C symbol
862 * starts with this prefix, as well each #GType in the library.
864 * Note: The namespace must have already been loaded using a function
865 * such as #g_irepository_require before calling this function.
867 * Returns: C namespace prefix, or %NULL if none associated
870 g_irepository_get_c_prefix (GIRepository *repository,
871 const gchar *namespace_)
876 g_return_val_if_fail (namespace_ != NULL, NULL);
878 repository = get_repository (repository);
880 typelib = get_registered (repository, namespace_, NULL);
882 g_return_val_if_fail (typelib != NULL, NULL);
884 header = (Header *) typelib->data;
885 if (header->shared_library)
886 return g_typelib_get_string (typelib, header->c_prefix);
892 * g_irepository_get_typelib_path
893 * @repository: Repository, may be %NULL for the default
894 * @namespace_: GI namespace to use, e.g. "Gtk"
896 * If namespace @namespace_ is loaded, return the full path to the
897 * .typelib file it was loaded from. If the typelib for
898 * namespace @namespace_ was included in a shared library, return
899 * the special string "$lt;builtin$gt;".
901 * Returns: Filesystem path (or $lt;builtin$gt;) if successful, %NULL if namespace is not loaded
905 g_irepository_get_typelib_path (GIRepository *repository,
906 const gchar *namespace)
908 gpointer orig_key, value;
910 repository = get_repository (repository);
912 if (!g_hash_table_lookup_extended (repository->priv->typelibs, namespace,
915 if (!g_hash_table_lookup_extended (repository->priv->lazy_typelibs, namespace,
920 return ((char*)orig_key) + strlen ((char *) orig_key) + 1;
923 /* This simple search function looks for a specified namespace-version;
924 it's faster than the full directory listing required for latest version. */
926 find_namespace_version (const gchar *namespace,
927 const gchar *version,
932 GError *error = NULL;
933 GMappedFile *mfile = NULL;
936 fname = g_strdup_printf ("%s-%s.typelib", namespace, version);
938 tmp_path = build_search_path_with_overrides ();
939 for (ldir = tmp_path; ldir; ldir = ldir->next)
941 char *path = g_build_filename (ldir->data, fname, NULL);
943 mfile = g_mapped_file_new (path, FALSE, &error);
947 g_clear_error (&error);
954 g_slist_free (tmp_path);
959 parse_version (const char *version,
966 *major = strtol (version, &end, 10);
967 dot = strchr (version, '.');
975 *minor = strtol (dot+1, &end, 10);
976 if (end != (version + strlen (version)))
982 compare_version (const char *v1,
986 int v1_major, v1_minor;
987 int v2_major, v2_minor;
989 success = parse_version (v1, &v1_major, &v1_minor);
992 success = parse_version (v2, &v2_major, &v2_minor);
995 if (v1_major > v2_major)
997 else if (v2_major > v1_major)
999 else if (v1_minor > v2_minor)
1001 else if (v2_minor > v1_minor)
1006 struct NamespaceVersionCandidadate
1015 compare_candidate_reverse (struct NamespaceVersionCandidadate *c1,
1016 struct NamespaceVersionCandidadate *c2)
1018 int result = compare_version (c1->version, c2->version);
1019 /* First, check the version */
1022 else if (result < 0)
1026 /* Now check the path index, which says how early in the search path
1027 * we found it. This ensures that of equal version targets, we
1028 * pick the earlier one.
1030 if (c1->path_index == c2->path_index)
1032 else if (c1->path_index > c2->path_index)
1040 free_candidate (struct NamespaceVersionCandidadate *candidate)
1042 g_mapped_file_free (candidate->mfile);
1043 g_free (candidate->path);
1044 g_free (candidate->version);
1048 static GMappedFile *
1049 find_namespace_latest (const gchar *namespace,
1050 gchar **version_ret,
1055 GError *error = NULL;
1056 char *namespace_dash;
1057 char *namespace_typelib;
1058 GSList *candidates = NULL;
1059 GMappedFile *result = NULL;
1062 *version_ret = NULL;
1065 namespace_dash = g_strdup_printf ("%s-", namespace);
1066 namespace_typelib = g_strdup_printf ("%s.typelib", namespace);
1069 tmp_path = build_search_path_with_overrides ();
1070 for (ldir = tmp_path; ldir; ldir = ldir->next)
1073 const char *dirname;
1076 dirname = (const char*)ldir->data;
1077 dir = g_dir_open (dirname, 0, NULL);
1080 while ((entry = g_dir_read_name (dir)) != NULL)
1083 char *path, *version;
1084 struct NamespaceVersionCandidadate *candidate;
1086 if (!g_str_has_suffix (entry, ".typelib"))
1089 if (g_str_has_prefix (entry, namespace_dash))
1091 const char *last_dash;
1092 const char *name_end;
1095 name_end = strrchr (entry, '.');
1096 last_dash = strrchr (entry, '-');
1097 version = g_strndup (last_dash+1, name_end-(last_dash+1));
1098 if (!parse_version (version, &major, &minor))
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_new0 (struct NamespaceVersionCandidadate, 1);
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 if (candidates != NULL)
1126 struct NamespaceVersionCandidadate *elected;
1127 candidates = g_slist_sort (candidates, (GCompareFunc) compare_candidate_reverse);
1129 elected = (struct NamespaceVersionCandidadate *) candidates->data;
1130 /* Remove the elected one so we don't try to free its contents */
1131 candidates = g_slist_delete_link (candidates, candidates);
1133 result = elected->mfile;
1134 *path_ret = elected->path;
1135 *version_ret = elected->version;
1136 g_free (elected); /* just free the container */
1137 g_slist_foreach (candidates, (GFunc) free_candidate, NULL);
1138 g_slist_free (candidates);
1140 g_free (namespace_dash);
1141 g_free (namespace_typelib);
1142 g_slist_free (tmp_path);
1147 * g_irepository_require:
1148 * @repository: (allow-none): Repository, may be %NULL for the default
1149 * @namespace_: GI namespace to use, e.g. "Gtk"
1150 * @version: (allow-none): Version of namespace, may be %NULL for latest
1151 * @flags: Set of %GIRepositoryLoadFlags, may be %0
1152 * @error: a #GError.
1154 * Force the namespace @namespace_ to be loaded if it isn't already.
1155 * If @namespace_ is not loaded, this function will search for a
1156 * ".typelib" file using the repository search path. In addition, a
1157 * version @version of namespace may be specified. If @version is
1158 * not specified, the latest will be used.
1160 * Returns: a pointer to the #GTypelib if successful, %NULL otherwise
1163 g_irepository_require (GIRepository *repository,
1164 const gchar *namespace,
1165 const gchar *version,
1166 GIRepositoryLoadFlags flags,
1170 GTypelib *ret = NULL;
1172 GTypelib *typelib = NULL;
1173 const gchar *typelib_namespace, *typelib_version;
1174 gboolean allow_lazy = (flags & G_IREPOSITORY_LOAD_FLAG_LAZY) > 0;
1176 char *version_conflict = NULL;
1178 char *tmp_version = NULL;
1180 g_return_val_if_fail (namespace != NULL, FALSE);
1182 repository = get_repository (repository);
1184 typelib = get_registered_status (repository, namespace, version, allow_lazy,
1185 &is_lazy, &version_conflict);
1189 if (version_conflict != NULL)
1191 g_set_error (error, G_IREPOSITORY_ERROR,
1192 G_IREPOSITORY_ERROR_NAMESPACE_VERSION_CONFLICT,
1193 "Requiring namespace '%s' version '%s', but '%s' is already loaded",
1194 namespace, version, version_conflict);
1198 if (version != NULL)
1200 mfile = find_namespace_version (namespace, version, &path);
1201 tmp_version = g_strdup (version);
1205 mfile = find_namespace_latest (namespace, &tmp_version, &path);
1210 if (version != NULL)
1211 g_set_error (error, G_IREPOSITORY_ERROR,
1212 G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
1213 "Typelib file for namespace '%s', version '%s' not found",
1214 namespace, version);
1216 g_set_error (error, G_IREPOSITORY_ERROR,
1217 G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
1218 "Typelib file for namespace '%s' (any version) not found",
1223 typelib = g_typelib_new_from_mapped_file (mfile);
1224 header = (Header *) typelib->data;
1225 typelib_namespace = g_typelib_get_string (typelib, header->namespace);
1226 typelib_version = g_typelib_get_string (typelib, header->nsversion);
1228 if (strcmp (typelib_namespace, namespace) != 0)
1230 g_set_error (error, G_IREPOSITORY_ERROR,
1231 G_IREPOSITORY_ERROR_NAMESPACE_MISMATCH,
1232 "Typelib file %s for namespace '%s' contains "
1233 "namespace '%s' which doesn't match the file name",
1234 path, namespace, typelib_namespace);
1237 if (version != NULL && strcmp (typelib_version, version) != 0)
1239 g_set_error (error, G_IREPOSITORY_ERROR,
1240 G_IREPOSITORY_ERROR_NAMESPACE_MISMATCH,
1241 "Typelib file %s for namespace '%s' contains "
1242 "version '%s' which doesn't match the expected version '%s'",
1243 path, namespace, typelib_version, version);
1247 if (!register_internal (repository, path, allow_lazy,
1250 g_typelib_free (typelib);
1255 g_free (tmp_version);
1261 g_irepository_introspect_cb (const char *option_name,
1266 gboolean ret = g_irepository_dump (value, error);
1270 static const GOptionEntry introspection_args[] = {
1271 { "introspect-dump", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK,
1272 g_irepository_introspect_cb, "Dump introspection information",
1273 "infile.txt,outfile.xml" },
1278 g_irepository_get_option_group (void)
1280 GOptionGroup *group;
1281 group = g_option_group_new ("girepository", "Introspection Options", "Show Introspection Options", NULL, NULL);
1283 g_option_group_add_entries (group, introspection_args);
1288 g_irepository_error_quark (void)
1290 static GQuark quark = 0;
1292 quark = g_quark_from_static_string ("g-irepository-error-quark");
1297 g_type_tag_to_string (GITypeTag type)
1301 case GI_TYPE_TAG_VOID:
1303 case GI_TYPE_TAG_BOOLEAN:
1305 case GI_TYPE_TAG_INT8:
1307 case GI_TYPE_TAG_UINT8:
1309 case GI_TYPE_TAG_INT16:
1311 case GI_TYPE_TAG_UINT16:
1313 case GI_TYPE_TAG_INT32:
1315 case GI_TYPE_TAG_UINT32:
1317 case GI_TYPE_TAG_INT64:
1319 case GI_TYPE_TAG_UINT64:
1321 case GI_TYPE_TAG_SHORT:
1323 case GI_TYPE_TAG_USHORT:
1325 case GI_TYPE_TAG_INT:
1327 case GI_TYPE_TAG_UINT:
1329 case GI_TYPE_TAG_LONG:
1331 case GI_TYPE_TAG_ULONG:
1333 case GI_TYPE_TAG_SSIZE:
1335 case GI_TYPE_TAG_SIZE:
1337 case GI_TYPE_TAG_FLOAT:
1339 case GI_TYPE_TAG_DOUBLE:
1341 case GI_TYPE_TAG_TIME_T:
1343 case GI_TYPE_TAG_GTYPE:
1345 case GI_TYPE_TAG_UTF8:
1347 case GI_TYPE_TAG_FILENAME:
1349 case GI_TYPE_TAG_ARRAY:
1351 case GI_TYPE_TAG_INTERFACE:
1353 case GI_TYPE_TAG_GLIST:
1355 case GI_TYPE_TAG_GSLIST:
1357 case GI_TYPE_TAG_GHASH:
1359 case GI_TYPE_TAG_ERROR: