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: (allow-none): 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: (allow-none): 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: (allow-none): 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;
519 * g_irepository_get_info:
520 * @repository: (allow-none): A #GIRepository, may be %NULL for the default
521 * @namespace_: Namespace to inspect
522 * @index: Offset into namespace metadata for entry
524 * This function returns a particular metadata entry in the
525 * given namespace @namespace_. The namespace must have
526 * already been loaded before calling this function.
528 * Returns: (transfer full): #GIBaseInfo containing metadata
531 g_irepository_get_info (GIRepository *repository,
532 const gchar *namespace,
538 g_return_val_if_fail (namespace != NULL, NULL);
540 repository = get_repository (repository);
542 typelib = get_registered (repository, namespace, NULL);
544 g_return_val_if_fail (typelib != NULL, NULL);
546 entry = g_typelib_get_dir_entry (typelib, index);
549 return _g_info_new_full (entry->blob_type,
551 NULL, typelib, entry->offset);
555 GIRepository *repository;
559 GITypelib *result_typelib;
564 find_by_gtype_foreach (gpointer key,
568 GITypelib *typelib = (GITypelib*)value;
569 FindByGTypeData *data = datap;
571 if (data->result != NULL)
574 data->result = g_typelib_get_dir_entry_by_gtype (typelib, data->fastpass, data->type);
576 data->result_typelib = typelib;
580 * g_irepository_find_by_gtype:
581 * @repository: (allow-none): A #GIRepository, may be %NULL for the default
582 * @gtype: GType to search for
584 * Searches all loaded namespaces for a particular #GType. Note that
585 * in order to locate the metadata, the namespace corresponding to
586 * the type must first have been loaded. There is currently no
587 * mechanism for determining the namespace which corresponds to an
588 * arbitrary GType - thus, this function will operate most reliably
589 * when you know the GType to originate from be from a loaded namespace.
591 * Returns: (transfer full): #GIBaseInfo representing metadata about @type, or %NULL
594 g_irepository_find_by_gtype (GIRepository *repository,
597 FindByGTypeData data;
600 repository = get_repository (repository);
602 cached = g_hash_table_lookup (repository->priv->info_by_gtype,
606 return g_base_info_ref (cached);
608 data.repository = repository;
609 data.fastpass = TRUE;
611 data.result_typelib = NULL;
614 g_hash_table_foreach (repository->priv->typelibs, find_by_gtype_foreach, &data);
615 if (data.result == NULL)
616 g_hash_table_foreach (repository->priv->lazy_typelibs, find_by_gtype_foreach, &data);
618 /* We do two passes; see comment in find_interface */
619 if (data.result == NULL)
621 data.fastpass = FALSE;
622 g_hash_table_foreach (repository->priv->typelibs, find_by_gtype_foreach, &data);
624 if (data.result == NULL)
625 g_hash_table_foreach (repository->priv->lazy_typelibs, find_by_gtype_foreach, &data);
627 if (data.result != NULL)
629 cached = _g_info_new_full (data.result->blob_type,
631 NULL, data.result_typelib, data.result->offset);
633 g_hash_table_insert (repository->priv->info_by_gtype,
635 g_base_info_ref (cached));
642 * g_irepository_find_by_name:
643 * @repository: (allow-none): A #GIRepository, may be %NULL for the default
644 * @namespace_: Namespace which will be searched
645 * @name: Entry name to find
647 * Searches for a particular entry in a namespace. Before calling
648 * this function for a particular namespace, you must call
649 * #g_irepository_require once to load the namespace, or otherwise
650 * ensure the namespace has already been loaded.
652 * Returns: (transfer full): #GIBaseInfo representing metadata about @name, or %NULL
655 g_irepository_find_by_name (GIRepository *repository,
656 const gchar *namespace,
662 g_return_val_if_fail (namespace != NULL, NULL);
664 repository = get_repository (repository);
665 typelib = get_registered (repository, namespace, NULL);
666 g_return_val_if_fail (typelib != NULL, NULL);
668 entry = g_typelib_get_dir_entry_by_name (typelib, name);
671 return _g_info_new_full (entry->blob_type,
673 NULL, typelib, entry->offset);
677 collect_namespaces (gpointer key,
683 *list = g_list_append (*list, key);
687 * g_irepository_get_loaded_namespaces:
688 * @repository: (allow-none): A #GIRepository, may be %NULL for the default
690 * Return the list of currently loaded namespaces.
692 * Returns: (utf8) (transfer full): List of namespaces
695 g_irepository_get_loaded_namespaces (GIRepository *repository)
697 GList *l, *list = NULL;
701 repository = get_repository (repository);
703 g_hash_table_foreach (repository->priv->typelibs, collect_namespaces, &list);
704 g_hash_table_foreach (repository->priv->lazy_typelibs, collect_namespaces, &list);
706 names = g_malloc0 (sizeof (gchar *) * (g_list_length (list) + 1));
708 for (l = list; l; l = l->next)
709 names[i++] = g_strdup (l->data);
716 * g_irepository_get_version:
717 * @repository: (allow-none): A #GIRepository, may be %NULL for the default
718 * @namespace_: Namespace to inspect
720 * This function returns the loaded version associated with the given
721 * namespace @namespace_.
723 * Note: The namespace must have already been loaded using a function
724 * such as #g_irepository_require before calling this function.
726 * Returns: Loaded version
729 g_irepository_get_version (GIRepository *repository,
730 const gchar *namespace)
735 g_return_val_if_fail (namespace != NULL, NULL);
737 repository = get_repository (repository);
739 typelib = get_registered (repository, namespace, NULL);
741 g_return_val_if_fail (typelib != NULL, NULL);
743 header = (Header *) typelib->data;
744 return g_typelib_get_string (typelib, header->nsversion);
748 * g_irepository_get_shared_library:
749 * @repository: (allow-none): A #GIRepository, may be %NULL for the default
750 * @namespace_: Namespace to inspect
752 * This function returns the full path to the shared C library
753 * associated with the given namespace @namespace_. There may be no
754 * shared library path associated, in which case this function will
757 * Note: The namespace must have already been loaded using a function
758 * such as #g_irepository_require before calling this function.
760 * Returns: Full path to shared library, or %NULL if none associated
763 g_irepository_get_shared_library (GIRepository *repository,
764 const gchar *namespace)
769 g_return_val_if_fail (namespace != NULL, NULL);
771 repository = get_repository (repository);
773 typelib = get_registered (repository, namespace, NULL);
775 g_return_val_if_fail (typelib != NULL, NULL);
777 header = (Header *) typelib->data;
778 if (header->shared_library)
779 return g_typelib_get_string (typelib, header->shared_library);
785 * g_irepository_get_c_prefix
786 * @repository: (allow-none): A #GIRepository, may be %NULL for the default
787 * @namespace_: Namespace to inspect
789 * This function returns the "C prefix", or the C level namespace
790 * associated with the given introspection namespace. Each C symbol
791 * starts with this prefix, as well each #GType in the library.
793 * Note: The namespace must have already been loaded using a function
794 * such as #g_irepository_require before calling this function.
796 * Returns: C namespace prefix, or %NULL if none associated
799 g_irepository_get_c_prefix (GIRepository *repository,
800 const gchar *namespace_)
805 g_return_val_if_fail (namespace_ != NULL, NULL);
807 repository = get_repository (repository);
809 typelib = get_registered (repository, namespace_, NULL);
811 g_return_val_if_fail (typelib != NULL, NULL);
813 header = (Header *) typelib->data;
814 if (header->shared_library)
815 return g_typelib_get_string (typelib, header->c_prefix);
821 * g_irepository_get_typelib_path
822 * @repository: (allow-none): Repository, may be %NULL for the default
823 * @namespace_: GI namespace to use, e.g. "Gtk"
825 * If namespace @namespace_ is loaded, return the full path to the
826 * .typelib file it was loaded from. If the typelib for
827 * namespace @namespace_ was included in a shared library, return
828 * the special string "$lt;builtin$gt;".
830 * Returns: Filesystem path (or $lt;builtin$gt;) if successful, %NULL if namespace is not loaded
834 g_irepository_get_typelib_path (GIRepository *repository,
835 const gchar *namespace)
837 gpointer orig_key, value;
839 repository = get_repository (repository);
841 if (!g_hash_table_lookup_extended (repository->priv->typelibs, namespace,
844 if (!g_hash_table_lookup_extended (repository->priv->lazy_typelibs, namespace,
849 return ((char*)orig_key) + strlen ((char *) orig_key) + 1;
852 /* This simple search function looks for a specified namespace-version;
853 it's faster than the full directory listing required for latest version. */
855 find_namespace_version (const gchar *namespace,
856 const gchar *version,
861 GError *error = NULL;
862 GMappedFile *mfile = NULL;
865 fname = g_strdup_printf ("%s-%s.typelib", namespace, version);
867 for (ldir = search_path; ldir; ldir = ldir->next)
869 char *path = g_build_filename (ldir->data, fname, NULL);
871 mfile = g_mapped_file_new (path, FALSE, &error);
875 g_clear_error (&error);
886 parse_version (const char *version,
893 *major = strtol (version, &end, 10);
894 dot = strchr (version, '.');
902 *minor = strtol (dot+1, &end, 10);
903 if (end != (version + strlen (version)))
909 compare_version (const char *v1,
913 int v1_major, v1_minor;
914 int v2_major, v2_minor;
916 success = parse_version (v1, &v1_major, &v1_minor);
919 success = parse_version (v2, &v2_major, &v2_minor);
922 if (v1_major > v2_major)
924 else if (v2_major > v1_major)
926 else if (v1_minor > v2_minor)
928 else if (v2_minor > v1_minor)
933 struct NamespaceVersionCandidadate
942 compare_candidate_reverse (struct NamespaceVersionCandidadate *c1,
943 struct NamespaceVersionCandidadate *c2)
945 int result = compare_version (c1->version, c2->version);
946 /* First, check the version */
953 /* Now check the path index, which says how early in the search path
954 * we found it. This ensures that of equal version targets, we
955 * pick the earlier one.
957 if (c1->path_index == c2->path_index)
959 else if (c1->path_index > c2->path_index)
967 free_candidate (struct NamespaceVersionCandidadate *candidate)
969 g_mapped_file_unref (candidate->mfile);
970 g_free (candidate->path);
971 g_free (candidate->version);
972 g_slice_free (struct NamespaceVersionCandidadate, candidate);
976 enumerate_namespace_versions (const gchar *namespace,
979 GSList *candidates = NULL;
980 GHashTable *found_versions = g_hash_table_new (g_str_hash, g_str_equal);
981 char *namespace_dash;
982 char *namespace_typelib;
984 GError *error = NULL;
987 namespace_dash = g_strdup_printf ("%s-", namespace);
988 namespace_typelib = g_strdup_printf ("%s.typelib", namespace);
991 for (ldir = search_path; ldir; ldir = ldir->next)
997 dirname = (const char*)ldir->data;
998 dir = g_dir_open (dirname, 0, NULL);
1001 while ((entry = g_dir_read_name (dir)) != NULL)
1004 char *path, *version;
1005 struct NamespaceVersionCandidadate *candidate;
1007 if (!g_str_has_suffix (entry, ".typelib"))
1010 if (g_str_has_prefix (entry, namespace_dash))
1012 const char *last_dash;
1013 const char *name_end;
1016 name_end = strrchr (entry, '.');
1017 last_dash = strrchr (entry, '-');
1018 version = g_strndup (last_dash+1, name_end-(last_dash+1));
1019 if (!parse_version (version, &major, &minor))
1028 if (g_hash_table_lookup (found_versions, version) != NULL)
1033 g_hash_table_insert (found_versions, version, version);
1035 path = g_build_filename (dirname, entry, NULL);
1036 mfile = g_mapped_file_new (path, FALSE, &error);
1041 g_clear_error (&error);
1044 candidate = g_slice_new0 (struct NamespaceVersionCandidadate);
1045 candidate->mfile = mfile;
1046 candidate->path_index = index;
1047 candidate->path = path;
1048 candidate->version = version;
1049 candidates = g_slist_prepend (candidates, candidate);
1055 g_free (namespace_dash);
1056 g_free (namespace_typelib);
1057 g_hash_table_destroy (found_versions);
1062 static GMappedFile *
1063 find_namespace_latest (const gchar *namespace,
1064 GSList *search_path,
1065 gchar **version_ret,
1069 GMappedFile *result = NULL;
1071 *version_ret = NULL;
1074 candidates = enumerate_namespace_versions (namespace, search_path);
1076 if (candidates != NULL)
1078 struct NamespaceVersionCandidadate *elected;
1079 candidates = g_slist_sort (candidates, (GCompareFunc) compare_candidate_reverse);
1081 elected = (struct NamespaceVersionCandidadate *) candidates->data;
1082 /* Remove the elected one so we don't try to free its contents */
1083 candidates = g_slist_delete_link (candidates, candidates);
1085 result = elected->mfile;
1086 *path_ret = elected->path;
1087 *version_ret = elected->version;
1088 g_slice_free (struct NamespaceVersionCandidadate, elected); /* just free the container */
1089 g_slist_foreach (candidates, (GFunc) free_candidate, NULL);
1090 g_slist_free (candidates);
1096 * g_irepository_enumerate_versions:
1097 * @repository: (allow-none): the repository
1098 * @namespace_: GI namespace, e.g. "Gtk"
1100 * Obtain an unordered list of versions (either currently loaded or
1101 * available) for @namespace_ in this @repository.
1103 * Returns: (element-type utf8) (transfer full): the array of versions.
1106 g_irepository_enumerate_versions (GIRepository *repository,
1107 const gchar *namespace_)
1110 GSList *search_path;
1111 GSList *candidates, *link;
1112 const gchar *loaded_version;
1114 search_path = build_search_path_with_overrides ();
1115 candidates = enumerate_namespace_versions (namespace_, search_path);
1116 g_slist_free (search_path);
1118 for (link = candidates; link; link = link->next)
1120 struct NamespaceVersionCandidadate *candidate = link->data;
1121 ret = g_list_prepend (ret, g_strdup (candidate->version));
1122 free_candidate (candidate);
1124 g_slist_free (candidates);
1126 /* The currently loaded version of a namespace is also part of the
1127 * available versions, as it could have been loaded using
1128 * require_private().
1130 if (g_irepository_is_registered (repository, namespace_, NULL))
1132 loaded_version = g_irepository_get_version (repository, namespace_);
1133 if (loaded_version && !g_list_find_custom (ret, loaded_version, g_str_equal))
1134 ret = g_list_prepend (ret, g_strdup (loaded_version));
1141 require_internal (GIRepository *repository,
1142 const gchar *namespace,
1143 const gchar *version,
1144 GIRepositoryLoadFlags flags,
1145 GSList *search_path,
1149 GITypelib *ret = NULL;
1151 GITypelib *typelib = NULL;
1152 const gchar *typelib_namespace, *typelib_version;
1153 gboolean allow_lazy = (flags & G_IREPOSITORY_LOAD_FLAG_LAZY) > 0;
1155 char *version_conflict = NULL;
1157 char *tmp_version = NULL;
1159 g_return_val_if_fail (namespace != NULL, FALSE);
1161 repository = get_repository (repository);
1163 typelib = get_registered_status (repository, namespace, version, allow_lazy,
1164 &is_lazy, &version_conflict);
1168 if (version_conflict != NULL)
1170 g_set_error (error, G_IREPOSITORY_ERROR,
1171 G_IREPOSITORY_ERROR_NAMESPACE_VERSION_CONFLICT,
1172 "Requiring namespace '%s' version '%s', but '%s' is already loaded",
1173 namespace, version, version_conflict);
1177 if (version != NULL)
1179 mfile = find_namespace_version (namespace, version,
1180 search_path, &path);
1181 tmp_version = g_strdup (version);
1185 mfile = find_namespace_latest (namespace, search_path,
1186 &tmp_version, &path);
1191 if (version != NULL)
1192 g_set_error (error, G_IREPOSITORY_ERROR,
1193 G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
1194 "Typelib file for namespace '%s', version '%s' not found",
1195 namespace, version);
1197 g_set_error (error, G_IREPOSITORY_ERROR,
1198 G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
1199 "Typelib file for namespace '%s' (any version) not found",
1205 GError *temp_error = NULL;
1206 typelib = g_typelib_new_from_mapped_file (mfile, &temp_error);
1209 g_set_error (error, G_IREPOSITORY_ERROR,
1210 G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
1211 "Failed to load typelib file '%s' for namespace '%s': %s",
1212 path, namespace, temp_error->message);
1213 g_clear_error (&temp_error);
1217 header = (Header *) typelib->data;
1218 typelib_namespace = g_typelib_get_string (typelib, header->namespace);
1219 typelib_version = g_typelib_get_string (typelib, header->nsversion);
1221 if (strcmp (typelib_namespace, namespace) != 0)
1223 g_set_error (error, G_IREPOSITORY_ERROR,
1224 G_IREPOSITORY_ERROR_NAMESPACE_MISMATCH,
1225 "Typelib file %s for namespace '%s' contains "
1226 "namespace '%s' which doesn't match the file name",
1227 path, namespace, typelib_namespace);
1228 g_typelib_free (typelib);
1231 if (version != NULL && strcmp (typelib_version, version) != 0)
1233 g_set_error (error, G_IREPOSITORY_ERROR,
1234 G_IREPOSITORY_ERROR_NAMESPACE_MISMATCH,
1235 "Typelib file %s for namespace '%s' contains "
1236 "version '%s' which doesn't match the expected version '%s'",
1237 path, namespace, typelib_version, version);
1238 g_typelib_free (typelib);
1242 if (!register_internal (repository, path, allow_lazy,
1245 g_typelib_free (typelib);
1250 g_free (tmp_version);
1256 * g_irepository_require:
1257 * @repository: (allow-none): Repository, may be %NULL for the default
1258 * @namespace_: GI namespace to use, e.g. "Gtk"
1259 * @version: (allow-none): Version of namespace, may be %NULL for latest
1260 * @flags: Set of %GIRepositoryLoadFlags, may be 0
1261 * @error: a #GError.
1263 * Force the namespace @namespace_ to be loaded if it isn't already.
1264 * If @namespace_ is not loaded, this function will search for a
1265 * ".typelib" file using the repository search path. In addition, a
1266 * version @version of namespace may be specified. If @version is
1267 * not specified, the latest will be used.
1269 * Returns: (transfer none): a pointer to the #GITypelib if successful, %NULL otherwise
1272 g_irepository_require (GIRepository *repository,
1273 const gchar *namespace,
1274 const gchar *version,
1275 GIRepositoryLoadFlags flags,
1278 GSList *search_path;
1281 search_path = build_search_path_with_overrides ();
1282 typelib = require_internal (repository, namespace, version, flags,
1283 search_path, error);
1284 g_slist_free (search_path);
1290 * g_irepository_require_private:
1291 * @repository: (allow-none): Repository, may be %NULL for the default
1292 * @typelib_dir: Private directory where to find the requested typelib
1293 * @namespace_: GI namespace to use, e.g. "Gtk"
1294 * @version: (allow-none): Version of namespace, may be %NULL for latest
1295 * @flags: Set of %GIRepositoryLoadFlags, may be 0
1296 * @error: a #GError.
1298 * Force the namespace @namespace_ to be loaded if it isn't already.
1299 * If @namespace_ is not loaded, this function will search for a
1300 * ".typelib" file within the private directory only. In addition, a
1301 * version @version of namespace should be specified. If @version is
1302 * not specified, the latest will be used.
1304 * Returns: (transfer none): a pointer to the #GITypelib if successful, %NULL otherwise
1307 g_irepository_require_private (GIRepository *repository,
1308 const gchar *typelib_dir,
1309 const gchar *namespace,
1310 const gchar *version,
1311 GIRepositoryLoadFlags flags,
1314 GSList search_path = { (gpointer) typelib_dir, NULL };
1316 return require_internal (repository, namespace, version, flags,
1317 &search_path, error);
1321 g_irepository_introspect_cb (const char *option_name,
1326 GError *tmp_error = NULL;
1327 gboolean ret = g_irepository_dump (value, &tmp_error);
1330 g_error ("Failed to extract GType data: %s",
1331 tmp_error->message);
1337 static const GOptionEntry introspection_args[] = {
1338 { "introspect-dump", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK,
1339 g_irepository_introspect_cb, "Dump introspection information",
1340 "infile.txt,outfile.xml" },
1345 * g_irepository_get_option_group: (skip)
1347 * Obtain the option group for girepository, it's used
1348 * by the dumper and for programs that wants to provide
1349 * introspection information
1351 * Returns: (transfer full): the option group
1354 g_irepository_get_option_group (void)
1356 GOptionGroup *group;
1357 group = g_option_group_new ("girepository", "Introspection Options", "Show Introspection Options", NULL, NULL);
1359 g_option_group_add_entries (group, introspection_args);
1364 g_irepository_error_quark (void)
1366 static GQuark quark = 0;
1368 quark = g_quark_from_static_string ("g-irepository-error-quark");
1373 * g_type_tag_to_string:
1374 * @type: the type_tag
1376 * Obtain a string representation of @type
1378 * Returns: the string
1381 g_type_tag_to_string (GITypeTag type)
1385 case GI_TYPE_TAG_VOID:
1387 case GI_TYPE_TAG_BOOLEAN:
1389 case GI_TYPE_TAG_INT8:
1391 case GI_TYPE_TAG_UINT8:
1393 case GI_TYPE_TAG_INT16:
1395 case GI_TYPE_TAG_UINT16:
1397 case GI_TYPE_TAG_INT32:
1399 case GI_TYPE_TAG_UINT32:
1401 case GI_TYPE_TAG_INT64:
1403 case GI_TYPE_TAG_UINT64:
1405 case GI_TYPE_TAG_FLOAT:
1407 case GI_TYPE_TAG_DOUBLE:
1409 case GI_TYPE_TAG_GTYPE:
1411 case GI_TYPE_TAG_UTF8:
1413 case GI_TYPE_TAG_FILENAME:
1415 case GI_TYPE_TAG_ARRAY:
1417 case GI_TYPE_TAG_INTERFACE:
1419 case GI_TYPE_TAG_GLIST:
1421 case GI_TYPE_TAG_GSLIST:
1423 case GI_TYPE_TAG_GHASH:
1425 case GI_TYPE_TAG_ERROR:
1433 * g_info_type_to_string:
1434 * @type: the info type
1436 * Obtain a string representation of @type
1438 * Returns: the string
1441 g_info_type_to_string (GIInfoType type)
1445 case GI_INFO_TYPE_INVALID:
1447 case GI_INFO_TYPE_FUNCTION:
1449 case GI_INFO_TYPE_CALLBACK:
1451 case GI_INFO_TYPE_STRUCT:
1453 case GI_INFO_TYPE_BOXED:
1455 case GI_INFO_TYPE_ENUM:
1457 case GI_INFO_TYPE_FLAGS:
1459 case GI_INFO_TYPE_OBJECT:
1461 case GI_INFO_TYPE_INTERFACE:
1463 case GI_INFO_TYPE_CONSTANT:
1465 case GI_INFO_TYPE_ERROR_DOMAIN:
1466 return "error domain";
1467 case GI_INFO_TYPE_UNION:
1469 case GI_INFO_TYPE_VALUE:
1471 case GI_INFO_TYPE_SIGNAL:
1473 case GI_INFO_TYPE_VFUNC:
1475 case GI_INFO_TYPE_PROPERTY:
1477 case GI_INFO_TYPE_FIELD:
1479 case GI_INFO_TYPE_ARG:
1481 case GI_INFO_TYPE_TYPE:
1483 case GI_INFO_TYPE_UNRESOLVED:
1484 return "unresolved";