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: 0-based 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.
527 * See g_irepository_get_n_infos() to find the maximum number of
530 * Returns: (transfer full): #GIBaseInfo containing metadata
533 g_irepository_get_info (GIRepository *repository,
534 const gchar *namespace,
540 g_return_val_if_fail (namespace != NULL, NULL);
542 repository = get_repository (repository);
544 typelib = get_registered (repository, namespace, NULL);
546 g_return_val_if_fail (typelib != NULL, NULL);
548 entry = g_typelib_get_dir_entry (typelib, index + 1);
551 return _g_info_new_full (entry->blob_type,
553 NULL, typelib, entry->offset);
557 GIRepository *repository;
561 GITypelib *result_typelib;
566 find_by_gtype_foreach (gpointer key,
570 GITypelib *typelib = (GITypelib*)value;
571 FindByGTypeData *data = datap;
573 if (data->result != NULL)
576 data->result = g_typelib_get_dir_entry_by_gtype (typelib, data->fastpass, data->type);
578 data->result_typelib = typelib;
582 * g_irepository_find_by_gtype:
583 * @repository: (allow-none): A #GIRepository, may be %NULL for the default
584 * @gtype: GType to search for
586 * Searches all loaded namespaces for a particular #GType. Note that
587 * in order to locate the metadata, the namespace corresponding to
588 * the type must first have been loaded. There is currently no
589 * mechanism for determining the namespace which corresponds to an
590 * arbitrary GType - thus, this function will operate most reliably
591 * when you know the GType to originate from be from a loaded namespace.
593 * Returns: (transfer full): #GIBaseInfo representing metadata about @type, or %NULL
596 g_irepository_find_by_gtype (GIRepository *repository,
599 FindByGTypeData data;
602 repository = get_repository (repository);
604 cached = g_hash_table_lookup (repository->priv->info_by_gtype,
608 return g_base_info_ref (cached);
610 data.repository = repository;
611 data.fastpass = TRUE;
613 data.result_typelib = NULL;
616 g_hash_table_foreach (repository->priv->typelibs, find_by_gtype_foreach, &data);
617 if (data.result == NULL)
618 g_hash_table_foreach (repository->priv->lazy_typelibs, find_by_gtype_foreach, &data);
620 /* We do two passes; see comment in find_interface */
621 if (data.result == NULL)
623 data.fastpass = FALSE;
624 g_hash_table_foreach (repository->priv->typelibs, find_by_gtype_foreach, &data);
626 if (data.result == NULL)
627 g_hash_table_foreach (repository->priv->lazy_typelibs, find_by_gtype_foreach, &data);
629 if (data.result != NULL)
631 cached = _g_info_new_full (data.result->blob_type,
633 NULL, data.result_typelib, data.result->offset);
635 g_hash_table_insert (repository->priv->info_by_gtype,
637 g_base_info_ref (cached));
644 * g_irepository_find_by_name:
645 * @repository: (allow-none): A #GIRepository, may be %NULL for the default
646 * @namespace_: Namespace which will be searched
647 * @name: Entry name to find
649 * Searches for a particular entry in a namespace. Before calling
650 * this function for a particular namespace, you must call
651 * g_irepository_require() once to load the namespace, or otherwise
652 * ensure the namespace has already been loaded.
654 * Returns: (transfer full): #GIBaseInfo representing metadata about @name, or %NULL
657 g_irepository_find_by_name (GIRepository *repository,
658 const gchar *namespace,
664 g_return_val_if_fail (namespace != NULL, NULL);
666 repository = get_repository (repository);
667 typelib = get_registered (repository, namespace, NULL);
668 g_return_val_if_fail (typelib != NULL, NULL);
670 entry = g_typelib_get_dir_entry_by_name (typelib, name);
673 return _g_info_new_full (entry->blob_type,
675 NULL, typelib, entry->offset);
679 collect_namespaces (gpointer key,
685 *list = g_list_append (*list, key);
689 * g_irepository_get_loaded_namespaces:
690 * @repository: (allow-none): A #GIRepository, may be %NULL for the default
692 * Return the list of currently loaded namespaces.
694 * Returns: (utf8) (transfer full): List of namespaces
697 g_irepository_get_loaded_namespaces (GIRepository *repository)
699 GList *l, *list = NULL;
703 repository = get_repository (repository);
705 g_hash_table_foreach (repository->priv->typelibs, collect_namespaces, &list);
706 g_hash_table_foreach (repository->priv->lazy_typelibs, collect_namespaces, &list);
708 names = g_malloc0 (sizeof (gchar *) * (g_list_length (list) + 1));
710 for (l = list; l; l = l->next)
711 names[i++] = g_strdup (l->data);
718 * g_irepository_get_version:
719 * @repository: (allow-none): A #GIRepository, may be %NULL for the default
720 * @namespace_: Namespace to inspect
722 * This function returns the loaded version associated with the given
723 * namespace @namespace_.
725 * Note: The namespace must have already been loaded using a function
726 * such as g_irepository_require() before calling this function.
728 * Returns: Loaded version
731 g_irepository_get_version (GIRepository *repository,
732 const gchar *namespace)
737 g_return_val_if_fail (namespace != NULL, NULL);
739 repository = get_repository (repository);
741 typelib = get_registered (repository, namespace, NULL);
743 g_return_val_if_fail (typelib != NULL, NULL);
745 header = (Header *) typelib->data;
746 return g_typelib_get_string (typelib, header->nsversion);
750 * g_irepository_get_shared_library:
751 * @repository: (allow-none): A #GIRepository, may be %NULL for the default
752 * @namespace_: Namespace to inspect
754 * This function returns the full path to the shared C library
755 * associated with the given namespace @namespace_. There may be no
756 * shared library path associated, in which case this function will
759 * Note: The namespace must have already been loaded using a function
760 * such as g_irepository_require() before calling this function.
762 * Returns: Full path to shared library, or %NULL if none associated
765 g_irepository_get_shared_library (GIRepository *repository,
766 const gchar *namespace)
771 g_return_val_if_fail (namespace != NULL, NULL);
773 repository = get_repository (repository);
775 typelib = get_registered (repository, namespace, NULL);
777 g_return_val_if_fail (typelib != NULL, NULL);
779 header = (Header *) typelib->data;
780 if (header->shared_library)
781 return g_typelib_get_string (typelib, header->shared_library);
787 * g_irepository_get_c_prefix
788 * @repository: (allow-none): A #GIRepository, may be %NULL for the default
789 * @namespace_: Namespace to inspect
791 * This function returns the "C prefix", or the C level namespace
792 * associated with the given introspection namespace. Each C symbol
793 * starts with this prefix, as well each #GType in the library.
795 * Note: The namespace must have already been loaded using a function
796 * such as g_irepository_require() before calling this function.
798 * Returns: C namespace prefix, or %NULL if none associated
801 g_irepository_get_c_prefix (GIRepository *repository,
802 const gchar *namespace_)
807 g_return_val_if_fail (namespace_ != NULL, NULL);
809 repository = get_repository (repository);
811 typelib = get_registered (repository, namespace_, NULL);
813 g_return_val_if_fail (typelib != NULL, NULL);
815 header = (Header *) typelib->data;
816 if (header->shared_library)
817 return g_typelib_get_string (typelib, header->c_prefix);
823 * g_irepository_get_typelib_path
824 * @repository: (allow-none): Repository, may be %NULL for the default
825 * @namespace_: GI namespace to use, e.g. "Gtk"
827 * If namespace @namespace_ is loaded, return the full path to the
828 * .typelib file it was loaded from. If the typelib for
829 * namespace @namespace_ was included in a shared library, return
830 * the special string "$lt;builtin$gt;".
832 * Returns: Filesystem path (or $lt;builtin$gt;) if successful, %NULL if namespace is not loaded
836 g_irepository_get_typelib_path (GIRepository *repository,
837 const gchar *namespace)
839 gpointer orig_key, value;
841 repository = get_repository (repository);
843 if (!g_hash_table_lookup_extended (repository->priv->typelibs, namespace,
846 if (!g_hash_table_lookup_extended (repository->priv->lazy_typelibs, namespace,
851 return ((char*)orig_key) + strlen ((char *) orig_key) + 1;
854 /* This simple search function looks for a specified namespace-version;
855 it's faster than the full directory listing required for latest version. */
857 find_namespace_version (const gchar *namespace,
858 const gchar *version,
863 GError *error = NULL;
864 GMappedFile *mfile = NULL;
867 fname = g_strdup_printf ("%s-%s.typelib", namespace, version);
869 for (ldir = search_path; ldir; ldir = ldir->next)
871 char *path = g_build_filename (ldir->data, fname, NULL);
873 mfile = g_mapped_file_new (path, FALSE, &error);
877 g_clear_error (&error);
888 parse_version (const char *version,
895 *major = strtol (version, &end, 10);
896 dot = strchr (version, '.');
904 *minor = strtol (dot+1, &end, 10);
905 if (end != (version + strlen (version)))
911 compare_version (const char *v1,
915 int v1_major, v1_minor;
916 int v2_major, v2_minor;
918 success = parse_version (v1, &v1_major, &v1_minor);
921 success = parse_version (v2, &v2_major, &v2_minor);
924 if (v1_major > v2_major)
926 else if (v2_major > v1_major)
928 else if (v1_minor > v2_minor)
930 else if (v2_minor > v1_minor)
935 struct NamespaceVersionCandidadate
944 compare_candidate_reverse (struct NamespaceVersionCandidadate *c1,
945 struct NamespaceVersionCandidadate *c2)
947 int result = compare_version (c1->version, c2->version);
948 /* First, check the version */
955 /* Now check the path index, which says how early in the search path
956 * we found it. This ensures that of equal version targets, we
957 * pick the earlier one.
959 if (c1->path_index == c2->path_index)
961 else if (c1->path_index > c2->path_index)
969 free_candidate (struct NamespaceVersionCandidadate *candidate)
971 g_mapped_file_unref (candidate->mfile);
972 g_free (candidate->path);
973 g_free (candidate->version);
974 g_slice_free (struct NamespaceVersionCandidadate, candidate);
978 enumerate_namespace_versions (const gchar *namespace,
981 GSList *candidates = NULL;
982 GHashTable *found_versions = g_hash_table_new (g_str_hash, g_str_equal);
983 char *namespace_dash;
984 char *namespace_typelib;
986 GError *error = NULL;
989 namespace_dash = g_strdup_printf ("%s-", namespace);
990 namespace_typelib = g_strdup_printf ("%s.typelib", namespace);
993 for (ldir = search_path; ldir; ldir = ldir->next)
999 dirname = (const char*)ldir->data;
1000 dir = g_dir_open (dirname, 0, NULL);
1003 while ((entry = g_dir_read_name (dir)) != NULL)
1006 char *path, *version;
1007 struct NamespaceVersionCandidadate *candidate;
1009 if (!g_str_has_suffix (entry, ".typelib"))
1012 if (g_str_has_prefix (entry, namespace_dash))
1014 const char *last_dash;
1015 const char *name_end;
1018 name_end = strrchr (entry, '.');
1019 last_dash = strrchr (entry, '-');
1020 version = g_strndup (last_dash+1, name_end-(last_dash+1));
1021 if (!parse_version (version, &major, &minor))
1030 if (g_hash_table_lookup (found_versions, version) != NULL)
1035 g_hash_table_insert (found_versions, version, version);
1037 path = g_build_filename (dirname, entry, NULL);
1038 mfile = g_mapped_file_new (path, FALSE, &error);
1043 g_clear_error (&error);
1046 candidate = g_slice_new0 (struct NamespaceVersionCandidadate);
1047 candidate->mfile = mfile;
1048 candidate->path_index = index;
1049 candidate->path = path;
1050 candidate->version = version;
1051 candidates = g_slist_prepend (candidates, candidate);
1057 g_free (namespace_dash);
1058 g_free (namespace_typelib);
1059 g_hash_table_destroy (found_versions);
1064 static GMappedFile *
1065 find_namespace_latest (const gchar *namespace,
1066 GSList *search_path,
1067 gchar **version_ret,
1071 GMappedFile *result = NULL;
1073 *version_ret = NULL;
1076 candidates = enumerate_namespace_versions (namespace, search_path);
1078 if (candidates != NULL)
1080 struct NamespaceVersionCandidadate *elected;
1081 candidates = g_slist_sort (candidates, (GCompareFunc) compare_candidate_reverse);
1083 elected = (struct NamespaceVersionCandidadate *) candidates->data;
1084 /* Remove the elected one so we don't try to free its contents */
1085 candidates = g_slist_delete_link (candidates, candidates);
1087 result = elected->mfile;
1088 *path_ret = elected->path;
1089 *version_ret = elected->version;
1090 g_slice_free (struct NamespaceVersionCandidadate, elected); /* just free the container */
1091 g_slist_foreach (candidates, (GFunc) free_candidate, NULL);
1092 g_slist_free (candidates);
1098 * g_irepository_enumerate_versions:
1099 * @repository: (allow-none): the repository
1100 * @namespace_: GI namespace, e.g. "Gtk"
1102 * Obtain an unordered list of versions (either currently loaded or
1103 * available) for @namespace_ in this @repository.
1105 * Returns: (element-type utf8) (transfer full): the array of versions.
1108 g_irepository_enumerate_versions (GIRepository *repository,
1109 const gchar *namespace_)
1112 GSList *search_path;
1113 GSList *candidates, *link;
1114 const gchar *loaded_version;
1116 search_path = build_search_path_with_overrides ();
1117 candidates = enumerate_namespace_versions (namespace_, search_path);
1118 g_slist_free (search_path);
1120 for (link = candidates; link; link = link->next)
1122 struct NamespaceVersionCandidadate *candidate = link->data;
1123 ret = g_list_prepend (ret, g_strdup (candidate->version));
1124 free_candidate (candidate);
1126 g_slist_free (candidates);
1128 /* The currently loaded version of a namespace is also part of the
1129 * available versions, as it could have been loaded using
1130 * require_private().
1132 if (g_irepository_is_registered (repository, namespace_, NULL))
1134 loaded_version = g_irepository_get_version (repository, namespace_);
1135 if (loaded_version && !g_list_find_custom (ret, loaded_version, g_str_equal))
1136 ret = g_list_prepend (ret, g_strdup (loaded_version));
1143 require_internal (GIRepository *repository,
1144 const gchar *namespace,
1145 const gchar *version,
1146 GIRepositoryLoadFlags flags,
1147 GSList *search_path,
1151 GITypelib *ret = NULL;
1153 GITypelib *typelib = NULL;
1154 const gchar *typelib_namespace, *typelib_version;
1155 gboolean allow_lazy = (flags & G_IREPOSITORY_LOAD_FLAG_LAZY) > 0;
1157 char *version_conflict = NULL;
1159 char *tmp_version = NULL;
1161 g_return_val_if_fail (namespace != NULL, FALSE);
1163 repository = get_repository (repository);
1165 typelib = get_registered_status (repository, namespace, version, allow_lazy,
1166 &is_lazy, &version_conflict);
1170 if (version_conflict != NULL)
1172 g_set_error (error, G_IREPOSITORY_ERROR,
1173 G_IREPOSITORY_ERROR_NAMESPACE_VERSION_CONFLICT,
1174 "Requiring namespace '%s' version '%s', but '%s' is already loaded",
1175 namespace, version, version_conflict);
1179 if (version != NULL)
1181 mfile = find_namespace_version (namespace, version,
1182 search_path, &path);
1183 tmp_version = g_strdup (version);
1187 mfile = find_namespace_latest (namespace, search_path,
1188 &tmp_version, &path);
1193 if (version != NULL)
1194 g_set_error (error, G_IREPOSITORY_ERROR,
1195 G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
1196 "Typelib file for namespace '%s', version '%s' not found",
1197 namespace, version);
1199 g_set_error (error, G_IREPOSITORY_ERROR,
1200 G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
1201 "Typelib file for namespace '%s' (any version) not found",
1207 GError *temp_error = NULL;
1208 typelib = g_typelib_new_from_mapped_file (mfile, &temp_error);
1211 g_set_error (error, G_IREPOSITORY_ERROR,
1212 G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
1213 "Failed to load typelib file '%s' for namespace '%s': %s",
1214 path, namespace, temp_error->message);
1215 g_clear_error (&temp_error);
1219 header = (Header *) typelib->data;
1220 typelib_namespace = g_typelib_get_string (typelib, header->namespace);
1221 typelib_version = g_typelib_get_string (typelib, header->nsversion);
1223 if (strcmp (typelib_namespace, namespace) != 0)
1225 g_set_error (error, G_IREPOSITORY_ERROR,
1226 G_IREPOSITORY_ERROR_NAMESPACE_MISMATCH,
1227 "Typelib file %s for namespace '%s' contains "
1228 "namespace '%s' which doesn't match the file name",
1229 path, namespace, typelib_namespace);
1230 g_typelib_free (typelib);
1233 if (version != NULL && strcmp (typelib_version, version) != 0)
1235 g_set_error (error, G_IREPOSITORY_ERROR,
1236 G_IREPOSITORY_ERROR_NAMESPACE_MISMATCH,
1237 "Typelib file %s for namespace '%s' contains "
1238 "version '%s' which doesn't match the expected version '%s'",
1239 path, namespace, typelib_version, version);
1240 g_typelib_free (typelib);
1244 if (!register_internal (repository, path, allow_lazy,
1247 g_typelib_free (typelib);
1252 g_free (tmp_version);
1258 * g_irepository_require:
1259 * @repository: (allow-none): Repository, may be %NULL for the default
1260 * @namespace_: GI namespace to use, e.g. "Gtk"
1261 * @version: (allow-none): Version of namespace, may be %NULL for latest
1262 * @flags: Set of %GIRepositoryLoadFlags, may be 0
1263 * @error: a #GError.
1265 * Force the namespace @namespace_ to be loaded if it isn't already.
1266 * If @namespace_ is not loaded, this function will search for a
1267 * ".typelib" file using the repository search path. In addition, a
1268 * version @version of namespace may be specified. If @version is
1269 * not specified, the latest will be used.
1271 * Returns: (transfer none): a pointer to the #GITypelib if successful, %NULL otherwise
1274 g_irepository_require (GIRepository *repository,
1275 const gchar *namespace,
1276 const gchar *version,
1277 GIRepositoryLoadFlags flags,
1280 GSList *search_path;
1283 search_path = build_search_path_with_overrides ();
1284 typelib = require_internal (repository, namespace, version, flags,
1285 search_path, error);
1286 g_slist_free (search_path);
1292 * g_irepository_require_private:
1293 * @repository: (allow-none): Repository, may be %NULL for the default
1294 * @typelib_dir: Private directory where to find the requested typelib
1295 * @namespace_: GI namespace to use, e.g. "Gtk"
1296 * @version: (allow-none): Version of namespace, may be %NULL for latest
1297 * @flags: Set of %GIRepositoryLoadFlags, may be 0
1298 * @error: a #GError.
1300 * Force the namespace @namespace_ to be loaded if it isn't already.
1301 * If @namespace_ is not loaded, this function will search for a
1302 * ".typelib" file within the private directory only. In addition, a
1303 * version @version of namespace should be specified. If @version is
1304 * not specified, the latest will be used.
1306 * Returns: (transfer none): a pointer to the #GITypelib if successful, %NULL otherwise
1309 g_irepository_require_private (GIRepository *repository,
1310 const gchar *typelib_dir,
1311 const gchar *namespace,
1312 const gchar *version,
1313 GIRepositoryLoadFlags flags,
1316 GSList search_path = { (gpointer) typelib_dir, NULL };
1318 return require_internal (repository, namespace, version, flags,
1319 &search_path, error);
1323 g_irepository_introspect_cb (const char *option_name,
1328 GError *tmp_error = NULL;
1329 gboolean ret = g_irepository_dump (value, &tmp_error);
1332 g_error ("Failed to extract GType data: %s",
1333 tmp_error->message);
1339 static const GOptionEntry introspection_args[] = {
1340 { "introspect-dump", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK,
1341 g_irepository_introspect_cb, "Dump introspection information",
1342 "infile.txt,outfile.xml" },
1347 * g_irepository_get_option_group: (skip)
1349 * Obtain the option group for girepository, it's used
1350 * by the dumper and for programs that wants to provide
1351 * introspection information
1353 * Returns: (transfer full): the option group
1356 g_irepository_get_option_group (void)
1358 GOptionGroup *group;
1359 group = g_option_group_new ("girepository", "Introspection Options", "Show Introspection Options", NULL, NULL);
1361 g_option_group_add_entries (group, introspection_args);
1366 g_irepository_error_quark (void)
1368 static GQuark quark = 0;
1370 quark = g_quark_from_static_string ("g-irepository-error-quark");
1375 * g_type_tag_to_string:
1376 * @type: the type_tag
1378 * Obtain a string representation of @type
1380 * Returns: the string
1383 g_type_tag_to_string (GITypeTag type)
1387 case GI_TYPE_TAG_VOID:
1389 case GI_TYPE_TAG_BOOLEAN:
1391 case GI_TYPE_TAG_INT8:
1393 case GI_TYPE_TAG_UINT8:
1395 case GI_TYPE_TAG_INT16:
1397 case GI_TYPE_TAG_UINT16:
1399 case GI_TYPE_TAG_INT32:
1401 case GI_TYPE_TAG_UINT32:
1403 case GI_TYPE_TAG_INT64:
1405 case GI_TYPE_TAG_UINT64:
1407 case GI_TYPE_TAG_FLOAT:
1409 case GI_TYPE_TAG_DOUBLE:
1411 case GI_TYPE_TAG_UNICHAR:
1413 case GI_TYPE_TAG_GTYPE:
1415 case GI_TYPE_TAG_UTF8:
1417 case GI_TYPE_TAG_FILENAME:
1419 case GI_TYPE_TAG_ARRAY:
1421 case GI_TYPE_TAG_INTERFACE:
1423 case GI_TYPE_TAG_GLIST:
1425 case GI_TYPE_TAG_GSLIST:
1427 case GI_TYPE_TAG_GHASH:
1429 case GI_TYPE_TAG_ERROR:
1437 * g_info_type_to_string:
1438 * @type: the info type
1440 * Obtain a string representation of @type
1442 * Returns: the string
1445 g_info_type_to_string (GIInfoType type)
1449 case GI_INFO_TYPE_INVALID:
1451 case GI_INFO_TYPE_FUNCTION:
1453 case GI_INFO_TYPE_CALLBACK:
1455 case GI_INFO_TYPE_STRUCT:
1457 case GI_INFO_TYPE_BOXED:
1459 case GI_INFO_TYPE_ENUM:
1461 case GI_INFO_TYPE_FLAGS:
1463 case GI_INFO_TYPE_OBJECT:
1465 case GI_INFO_TYPE_INTERFACE:
1467 case GI_INFO_TYPE_CONSTANT:
1469 case GI_INFO_TYPE_ERROR_DOMAIN:
1470 return "error domain";
1471 case GI_INFO_TYPE_UNION:
1473 case GI_INFO_TYPE_VALUE:
1475 case GI_INFO_TYPE_SIGNAL:
1477 case GI_INFO_TYPE_VFUNC:
1479 case GI_INFO_TYPE_PROPERTY:
1481 case GI_INFO_TYPE_FIELD:
1483 case GI_INFO_TYPE_ARG:
1485 case GI_INFO_TYPE_TYPE:
1487 case GI_INFO_TYPE_UNRESOLVED:
1488 return "unresolved";