-/* -*- Mode: C; c-file-style: "gnu"; -*- */
-/* GObject introspection: Repository implementation
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ * GObject introspection: Repository implementation
*
* Copyright (C) 2005 Matthias Clasen
* Copyright (C) 2008 Colin Walters <walters@verbum.org>
* Boston, MA 02111-1307, USA.
*/
+#include "config.h"
+
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <glib/gprintf.h>
#include <gmodule.h>
#include "girepository.h"
-#include "gtypelib.h"
-#include "ginfo.h"
-#include "glib-compat.h"
+#include "gitypelib-internal.h"
+#include "girepository-private.h"
+
+/**
+ * SECTION:girepository
+ * @short_description: GObject Introspection repository manager
+ * @include: girepository.h
+ *
+ * #GIRepository is used to manage repositories of namespaces. Namespaces
+ * are represented on disk by type libraries (.typelib files).
+ */
-#include "config.h"
-static GStaticMutex globals_lock = G_STATIC_MUTEX_INIT;
static GIRepository *default_repository = NULL;
static GSList *search_path = NULL;
-static GSList *override_search_path = NULL;
struct _GIRepositoryPrivate
{
- GHashTable *typelibs; /* (string) namespace -> GTypelib */
- GHashTable *lazy_typelibs; /* (string) namespace-version -> GTypelib */
+ GHashTable *typelibs; /* (string) namespace -> GITypelib */
+ GHashTable *lazy_typelibs; /* (string) namespace-version -> GITypelib */
GHashTable *info_by_gtype; /* GType -> GIBaseInfo */
+ GHashTable *info_by_error_domain; /* GQuark -> GIBaseInfo */
};
G_DEFINE_TYPE (GIRepository, g_irepository, G_TYPE_OBJECT);
+#ifdef G_PLATFORM_WIN32
+
+#include <windows.h>
+
+static HMODULE girepository_dll = NULL;
+
+#ifdef DLL_EXPORT
+
+BOOL WINAPI
+DllMain (HINSTANCE hinstDLL,
+ DWORD fdwReason,
+ LPVOID lpvReserved)
+{
+ if (fdwReason == DLL_PROCESS_ATTACH)
+ girepository_dll = hinstDLL;
+
+ return TRUE;
+}
+
+#endif
+
+#undef GOBJECT_INTROSPECTION_LIBDIR
+
+/* GOBJECT_INTROSPECTION_LIBDIR is used only in code called just once,
+ * so no problem leaking this
+ */
+#define GOBJECT_INTROSPECTION_LIBDIR \
+ g_build_filename (g_win32_get_package_installation_directory_of_module (girepository_dll), \
+ "lib", \
+ NULL)
+
+#endif
+
static void
g_irepository_init (GIRepository *repository)
{
(GDestroyNotify) NULL,
(GDestroyNotify) g_typelib_free);
repository->priv->lazy_typelibs
- = g_hash_table_new (g_str_hash, g_str_equal);
+ = g_hash_table_new_full (g_str_hash, g_str_equal,
+ (GDestroyNotify) g_free,
+ (GDestroyNotify) NULL);
repository->priv->info_by_gtype
= g_hash_table_new_full (g_direct_hash, g_direct_equal,
(GDestroyNotify) NULL,
(GDestroyNotify) g_base_info_unref);
+ repository->priv->info_by_error_domain
+ = g_hash_table_new_full (g_direct_hash, g_direct_equal,
+ (GDestroyNotify) NULL,
+ (GDestroyNotify) g_base_info_unref);
}
static void
g_hash_table_destroy (repository->priv->typelibs);
g_hash_table_destroy (repository->priv->lazy_typelibs);
g_hash_table_destroy (repository->priv->info_by_gtype);
+ g_hash_table_destroy (repository->priv->info_by_error_domain);
(* G_OBJECT_CLASS (g_irepository_parent_class)->finalize) (G_OBJECT (repository));
}
static void
init_globals (void)
{
- g_static_mutex_lock (&globals_lock);
+ static gsize initialized = 0;
+
+ if (!g_once_init_enter (&initialized))
+ return;
if (default_repository == NULL)
- {
- default_repository = g_object_new (G_TYPE_IREPOSITORY, NULL);
- }
+ default_repository = g_object_new (G_TYPE_IREPOSITORY, NULL);
if (search_path == NULL)
{
char *typelib_dir;
const gchar *type_lib_path_env;
- /* This variable is intended to take precedence over both the default
- * search path, as well as anything written into code with g_irepository_prepend_search_path.
+ /* This variable is intended to take precedence over both:
+ * - the default search path;
+ * - all g_irepository_prepend_search_path() calls.
*/
type_lib_path_env = g_getenv ("GI_TYPELIB_PATH");
search_path = NULL;
- override_search_path = NULL;
if (type_lib_path_env)
{
gchar **custom_dirs;
d = custom_dirs;
while (*d)
{
- override_search_path = g_slist_prepend (override_search_path, *d);
+ search_path = g_slist_prepend (search_path, *d);
d++;
}
search_path = g_slist_reverse (search_path);
}
- g_static_mutex_unlock (&globals_lock);
+ g_once_init_leave (&initialized, 1);
}
+/**
+ * g_irepository_prepend_search_path:
+ * @directory: (type filename): directory name to prepend to the typelib
+ * search path
+ *
+ * Prepends @directory to the typelib search path.
+ * See g_irepository_get_search_path().
+ */
void
g_irepository_prepend_search_path (const char *directory)
{
/**
* g_irepository_get_search_path:
*
- * Returns the search path the GIRepository will use when looking for typelibs.
- * The string is internal to GIRespository and should not be freed, nor should
- * the elements.
+ * Returns the current search path #GIRepository will use when loading
+ * typelib files. The list is internal to #GIRespository and should not
+ * be freed, nor should its string elements.
*
- * Return value: (element-type filename) (transfer none): list of strings
+ * Returns: (element-type filename) (transfer none): #GSList of strings
*/
GSList *
g_irepository_get_search_path (void)
return search_path;
}
-static
-GSList *
-build_search_path_with_overrides (void)
-{
- GSList *result;
- if (override_search_path != NULL)
- {
- result = g_slist_copy (override_search_path);
- g_slist_last (result)->next = g_slist_copy (search_path);
- }
- else
- result = g_slist_copy (search_path);
- return result;
-}
-
static char *
build_typelib_key (const char *name, const char *source)
{
return g_string_free (str, FALSE);
}
+/* Note: Returns %NULL (not an empty %NULL-terminated array) if there are no
+ * dependencies. */
static char **
-get_typelib_dependencies (GTypelib *typelib)
+get_typelib_dependencies (GITypelib *typelib)
{
Header *header;
const char *dependencies_glob;
return default_repository;
}
-static GTypelib *
-check_version_conflict (GTypelib *typelib,
+static GITypelib *
+check_version_conflict (GITypelib *typelib,
const gchar *namespace,
const gchar *expected_version,
char **version_conflict)
return typelib;
}
-static GTypelib *
+static GITypelib *
get_registered_status (GIRepository *repository,
const char *namespace,
const char *version,
gboolean *lazy_status,
char **version_conflict)
{
- GTypelib *typelib;
+ GITypelib *typelib;
repository = get_repository (repository);
if (lazy_status)
*lazy_status = FALSE;
return check_version_conflict (typelib, namespace, version, version_conflict);
}
-static GTypelib *
+static GITypelib *
get_registered (GIRepository *repository,
const char *namespace,
const char *version)
static gboolean
load_dependencies_recurse (GIRepository *repository,
- GTypelib *typelib,
+ GITypelib *typelib,
GError **error)
{
char **dependencies;
register_internal (GIRepository *repository,
const char *source,
gboolean lazy,
- GTypelib *typelib,
+ GITypelib *typelib,
GError **error)
{
Header *header;
const gchar *namespace;
- const gchar *version;
g_return_val_if_fail (typelib != NULL, FALSE);
g_return_val_if_fail (header != NULL, FALSE);
namespace = g_typelib_get_string (typelib, header->namespace);
- version = g_typelib_get_string (typelib, header->nsversion);
if (lazy)
{
}
/**
+ * g_irepository_get_immediate_dependencies:
+ * @repository: (nullable): A #GIRepository or %NULL for the singleton
+ * process-global default #GIRepository
+ * @namespace_: Namespace of interest
+ *
+ * Return an array of the immediate versioned dependencies for @namespace_.
+ * Returned strings are of the form <code>namespace-version</code>.
+ *
+ * Note: @namespace_ must have already been loaded using a function
+ * such as g_irepository_require() before calling this function.
+ *
+ * To get the transitive closure of dependencies for @namespace_, use
+ * g_irepository_get_dependencies().
+ *
+ * Returns: (transfer full): Zero-terminated string array of immediate versioned
+ * dependencies
+ *
+ * Since: 1.44
+ */
+char **
+g_irepository_get_immediate_dependencies (GIRepository *repository,
+ const char *namespace)
+{
+ GITypelib *typelib;
+ gchar **deps;
+
+ g_return_val_if_fail (namespace != NULL, NULL);
+
+ repository = get_repository (repository);
+
+ typelib = get_registered (repository, namespace, NULL);
+ g_return_val_if_fail (typelib != NULL, NULL);
+
+ /* Ensure we always return a non-%NULL vector. */
+ deps = get_typelib_dependencies (typelib);
+ if (deps == NULL)
+ deps = g_strsplit ("", "|", 0);
+
+ return deps;
+}
+
+/* Load the transitive closure of dependency namespace-version strings for the
+ * given @typelib. @repository must be non-%NULL. @transitive_dependencies must
+ * be a pre-existing GHashTable<owned utf8, owned utf8> set for storing the
+ * dependencies. */
+static void
+get_typelib_dependencies_transitive (GIRepository *repository,
+ GITypelib *typelib,
+ GHashTable *transitive_dependencies)
+{
+ gchar **immediate_dependencies;
+ guint i;
+
+ immediate_dependencies = get_typelib_dependencies (typelib);
+
+ for (i = 0; immediate_dependencies != NULL && immediate_dependencies[i]; i++)
+ {
+ gchar *dependency;
+ const gchar *last_dash;
+ gchar *dependency_namespace;
+
+ dependency = immediate_dependencies[i];
+
+ /* Steal from the strv. */
+ g_hash_table_add (transitive_dependencies, dependency);
+ immediate_dependencies[i] = NULL;
+
+ /* Recurse for this namespace. */
+ last_dash = strrchr (dependency, '-');
+ dependency_namespace = g_strndup (dependency, last_dash - dependency);
+
+ typelib = get_registered (repository, dependency_namespace, NULL);
+ g_return_if_fail (typelib != NULL);
+ get_typelib_dependencies_transitive (repository, typelib,
+ transitive_dependencies);
+
+ g_free (dependency_namespace);
+ }
+
+ g_free (immediate_dependencies);
+}
+
+/**
* g_irepository_get_dependencies:
- * @repository: A #GIRepository, may be %NULL for the default
+ * @repository: (allow-none): A #GIRepository or %NULL for the singleton
+ * process-global default #GIRepository
* @namespace_: Namespace of interest
*
- * Return an array of all (transitive) dependencies for namespace
- * @namespace_, including version. The returned strings are of the
- * form <code>namespace-version</code>.
+ * Return an array of all (transitive) versioned dependencies for
+ * @namespace_. Returned strings are of the form
+ * <code>namespace-version</code>.
*
- * Note: The namespace must have already been loaded using a function
- * such as #g_irepository_require before calling this function.
+ * Note: @namespace_ must have already been loaded using a function
+ * such as g_irepository_require() before calling this function.
+ *
+ * To get only the immediate dependencies for @namespace_, use
+ * g_irepository_get_immediate_dependencies().
*
- * Returns: Zero-terminated string array of versioned dependencies
+ * Returns: (transfer full): Zero-terminated string array of all versioned
+ * dependencies
*/
char **
g_irepository_get_dependencies (GIRepository *repository,
const char *namespace)
{
- GTypelib *typelib;
+ GITypelib *typelib;
+ GHashTable *transitive_dependencies; /* set of owned utf8 */
+ GHashTableIter iter;
+ gchar *dependency;
+ GPtrArray *out; /* owned utf8 elements */
g_return_val_if_fail (namespace != NULL, NULL);
typelib = get_registered (repository, namespace, NULL);
g_return_val_if_fail (typelib != NULL, NULL);
- return get_typelib_dependencies (typelib);
+ /* Load the dependencies. */
+ transitive_dependencies = g_hash_table_new (g_str_hash, g_str_equal);
+ get_typelib_dependencies_transitive (repository, typelib,
+ transitive_dependencies);
+
+ /* Convert to a string array. */
+ out = g_ptr_array_new_full (g_hash_table_size (transitive_dependencies),
+ g_free);
+ g_hash_table_iter_init (&iter, transitive_dependencies);
+
+ while (g_hash_table_iter_next (&iter, (gpointer) &dependency, NULL))
+ {
+ g_ptr_array_add (out, dependency);
+ g_hash_table_iter_steal (&iter);
+ }
+
+ g_hash_table_unref (transitive_dependencies);
+
+ /* Add a NULL terminator. */
+ g_ptr_array_add (out, NULL);
+
+ return (gchar **) g_ptr_array_free (out, FALSE);
}
+/**
+ * g_irepository_load_typelib:
+ * @repository: (allow-none): A #GIRepository or %NULL for the singleton
+ * process-global default #GIRepository
+ * @typelib: TODO
+ * @flags: TODO
+ * @error: TODO
+ *
+ * TODO
+ */
const char *
g_irepository_load_typelib (GIRepository *repository,
- GTypelib *typelib,
+ GITypelib *typelib,
GIRepositoryLoadFlags flags,
GError **error)
{
/**
* g_irepository_is_registered:
- * @repository: A #GIRepository, may be %NULL for the default
+ * @repository: (allow-none): A #GIRepository or %NULL for the singleton
+ * process-global default #GIRepository
* @namespace_: Namespace of interest
* @version: (allow-none): Required version, may be %NULL for latest
*
* Check whether a particular namespace (and optionally, a specific
* version thereof) is currently loaded. This function is likely to
* only be useful in unusual circumstances; in order to act upon
- * metadata in the namespace, you should call #g_irepository_require
+ * metadata in the namespace, you should call g_irepository_require()
* instead which will ensure the namespace is loaded, and return as
* quickly as this function will if it has already been loaded.
*
/**
* g_irepository_get_default:
*
- * Returns the singleton process-global default #GIRepository. It is
+ * Returns the singleton process-global default #GIRepository. It is
* not currently supported to have multiple repositories in a
* particular process, but this function is provided in the unlikely
* eventuality that it would become possible, and as a convenience for
* higher level language bindings to conform to the GObject method
* call conventions.
-
+ *
* All methods on #GIRepository also accept %NULL as an instance
* parameter to mean this default repository, which is usually more
* convenient for C.
/**
* g_irepository_get_n_infos:
- * @repository: A #GIRepository, may be %NULL for the default
+ * @repository: (allow-none): A #GIRepository or %NULL for the singleton
+ * process-global default #GIRepository
* @namespace_: Namespace to inspect
*
* This function returns the number of metadata entries in
g_irepository_get_n_infos (GIRepository *repository,
const gchar *namespace)
{
- GTypelib *typelib;
+ GITypelib *typelib;
gint n_interfaces = 0;
g_return_val_if_fail (namespace != NULL, -1);
return n_interfaces;
}
-typedef struct
-{
- GIRepository *repo;
- gint index;
- const gchar *name;
- gboolean type_firstpass;
- const gchar *type;
- GIBaseInfo *iface;
-} IfaceData;
-
-static void
-find_interface (gpointer key,
- gpointer value,
- gpointer data)
-{
- gint i;
- GTypelib *typelib = (GTypelib *)value;
- Header *header = (Header *) typelib->data;
- IfaceData *iface_data = (IfaceData *)data;
- gint index;
- gint n_entries;
- const gchar *name;
- const gchar *type;
- DirEntry *entry;
-
- index = 0;
- n_entries = ((Header *)typelib->data)->n_local_entries;
-
- if (iface_data->name)
- {
- for (i = 1; i <= n_entries; i++)
- {
- entry = g_typelib_get_dir_entry (typelib, i);
- name = g_typelib_get_string (typelib, entry->name);
- if (strcmp (name, iface_data->name) == 0)
- {
- index = i;
- break;
- }
- }
- }
- else if (iface_data->type)
- {
- const char *c_prefix;
- /* Inside each typelib, we include the "C prefix" which acts as
- * a namespace mechanism. For GtkTreeView, the C prefix is Gtk.
- * Given the assumption that GTypes for a library also use the
- * C prefix, we know we can skip examining a typelib if our
- * target type does not have this typelib's C prefix.
- *
- * However, not every class library necessarily conforms to this,
- * e.g. Clutter has Cogl inside it. So, we split this into two
- * passes. First we try a lookup, skipping things which don't
- * have the prefix. If that fails then we try a global lookup,
- * ignoring the prefix.
- *
- * See http://bugzilla.gnome.org/show_bug.cgi?id=564016
- */
- c_prefix = g_typelib_get_string (typelib, header->c_prefix);
- if (iface_data->type_firstpass && c_prefix != NULL)
- {
- if (g_ascii_strncasecmp (c_prefix, iface_data->type, strlen (c_prefix)) != 0)
- return;
- }
-
- for (i = 1; i <= n_entries; i++)
- {
- RegisteredTypeBlob *blob;
-
- entry = g_typelib_get_dir_entry (typelib, i);
- if (!BLOB_IS_REGISTERED_TYPE (entry))
- continue;
-
- blob = (RegisteredTypeBlob *)(&typelib->data[entry->offset]);
- if (!blob->gtype_name)
- continue;
-
- type = g_typelib_get_string (typelib, blob->gtype_name);
- if (strcmp (type, iface_data->type) == 0)
- {
- index = i;
- break;
- }
- }
- }
- else if (iface_data->index > n_entries)
- iface_data->index -= n_entries;
- else if (iface_data->index > 0)
- {
- index = iface_data->index;
- iface_data->index = 0;
- }
-
- if (index != 0)
- {
- entry = g_typelib_get_dir_entry (typelib, index);
- iface_data->iface = g_info_new_full (entry->blob_type,
- iface_data->repo,
- NULL, typelib, entry->offset);
- }
-}
-
/**
* g_irepository_get_info:
- * @repository: A #GIRepository, may be %NULL for the default
+ * @repository: (allow-none): A #GIRepository or %NULL for the singleton
+ * process-global default #GIRepository
* @namespace_: Namespace to inspect
- * @index: Offset into namespace metadata for entry
+ * @index: 0-based offset into namespace metadata for entry
*
* This function returns a particular metadata entry in the
* given namespace @namespace_. The namespace must have
* already been loaded before calling this function.
+ * See g_irepository_get_n_infos() to find the maximum number of
+ * entries.
*
- * Returns: #GIBaseInfo containing metadata
+ * Returns: (transfer full): #GIBaseInfo containing metadata
*/
GIBaseInfo *
g_irepository_get_info (GIRepository *repository,
const gchar *namespace,
gint index)
{
- IfaceData data;
- GTypelib *typelib;
+ GITypelib *typelib;
+ DirEntry *entry;
g_return_val_if_fail (namespace != NULL, NULL);
repository = get_repository (repository);
- data.repo = repository;
- data.name = NULL;
- data.type = NULL;
- data.index = index + 1;
- data.iface = NULL;
-
typelib = get_registered (repository, namespace, NULL);
g_return_val_if_fail (typelib != NULL, NULL);
- find_interface ((void *)namespace, typelib, &data);
+ entry = g_typelib_get_dir_entry (typelib, index + 1);
+ if (entry == NULL)
+ return NULL;
+ return _g_info_new_full (entry->blob_type,
+ repository,
+ NULL, typelib, entry->offset);
+}
+
+typedef struct {
+ const gchar *gtype_name;
+ GITypelib *result_typelib;
+} FindByGTypeData;
+
+static DirEntry *
+find_by_gtype (GHashTable *table, FindByGTypeData *data, gboolean check_prefix)
+{
+ GHashTableIter iter;
+ gpointer key, value;
+ DirEntry *ret;
+
+ g_hash_table_iter_init (&iter, table);
+ while (g_hash_table_iter_next (&iter, &key, &value))
+ {
+ GITypelib *typelib = (GITypelib*)value;
+ if (check_prefix)
+ {
+ if (!g_typelib_matches_gtype_name_prefix (typelib, data->gtype_name))
+ continue;
+ }
+
+ ret = g_typelib_get_dir_entry_by_gtype_name (typelib, data->gtype_name);
+ if (ret)
+ {
+ data->result_typelib = typelib;
+ return ret;
+ }
+ }
- return data.iface;
+ return NULL;
}
/**
* g_irepository_find_by_gtype:
- * @repository: A #GIRepository, may be %NULL for the default
+ * @repository: (allow-none): A #GIRepository or %NULL for the singleton
+ * process-global default #GIRepository
* @gtype: GType to search for
*
* Searches all loaded namespaces for a particular #GType. Note that
* arbitrary GType - thus, this function will operate most reliably
* when you know the GType to originate from be from a loaded namespace.
*
- * Returns: #GIBaseInfo representing metadata about @type, or %NULL
+ * Returns: (transfer full): #GIBaseInfo representing metadata about @type, or %NULL
*/
GIBaseInfo *
g_irepository_find_by_gtype (GIRepository *repository,
GType gtype)
{
- IfaceData data;
+ FindByGTypeData data;
+ GIBaseInfo *cached;
+ DirEntry *entry;
repository = get_repository (repository);
- data.iface = g_hash_table_lookup (repository->priv->info_by_gtype,
- (gpointer)gtype);
-
- if (data.iface)
- return g_base_info_ref (data.iface);
-
- data.repo = repository;
- data.name = NULL;
- data.type_firstpass = TRUE;
- data.type = g_type_name (gtype);
- data.index = -1;
- data.iface = NULL;
-
- g_hash_table_foreach (repository->priv->typelibs, find_interface, &data);
- g_hash_table_foreach (repository->priv->lazy_typelibs, find_interface, &data);
-
- /* We do two passes; see comment in find_interface */
- if (!data.iface)
+ cached = g_hash_table_lookup (repository->priv->info_by_gtype,
+ (gpointer)gtype);
+
+ if (cached != NULL)
+ return g_base_info_ref (cached);
+
+ data.gtype_name = g_type_name (gtype);
+ data.result_typelib = NULL;
+
+ /* Inside each typelib, we include the "C prefix" which acts as
+ * a namespace mechanism. For GtkTreeView, the C prefix is Gtk.
+ * Given the assumption that GTypes for a library also use the
+ * C prefix, we know we can skip examining a typelib if our
+ * target type does not have this typelib's C prefix. Use this
+ * assumption as our first attempt at locating the DirEntry.
+ */
+ entry = find_by_gtype (repository->priv->typelibs, &data, TRUE);
+ if (entry == NULL)
+ entry = find_by_gtype (repository->priv->lazy_typelibs, &data, TRUE);
+
+ /* Not ever class library necessarily specifies a correct c_prefix,
+ * so take a second pass. This time we will try a global lookup,
+ * ignoring prefixes.
+ * See http://bugzilla.gnome.org/show_bug.cgi?id=564016
+ */
+ if (entry == NULL)
+ entry = find_by_gtype (repository->priv->typelibs, &data, FALSE);
+ if (entry == NULL)
+ entry = find_by_gtype (repository->priv->lazy_typelibs, &data, FALSE);
+
+ if (entry != NULL)
{
- data.type_firstpass = FALSE;
- g_hash_table_foreach (repository->priv->typelibs, find_interface, &data);
- g_hash_table_foreach (repository->priv->lazy_typelibs, find_interface, &data);
+ cached = _g_info_new_full (entry->blob_type,
+ repository,
+ NULL, data.result_typelib, entry->offset);
+
+ g_hash_table_insert (repository->priv->info_by_gtype,
+ (gpointer) gtype,
+ g_base_info_ref (cached));
+ return cached;
}
-
- if (data.iface)
- g_hash_table_insert (repository->priv->info_by_gtype,
- (gpointer) gtype,
- g_base_info_ref (data.iface));
-
- return data.iface;
+ return NULL;
}
/**
* g_irepository_find_by_name:
- * @repository: A #GIRepository, may be %NULL for the default
+ * @repository: (allow-none): A #GIRepository or %NULL for the singleton
+ * process-global default #GIRepository
* @namespace_: Namespace which will be searched
* @name: Entry name to find
*
* Searches for a particular entry in a namespace. Before calling
* this function for a particular namespace, you must call
- * #g_irepository_require once to load the namespace, or otherwise
+ * g_irepository_require() once to load the namespace, or otherwise
* ensure the namespace has already been loaded.
*
- * Returns: #GIBaseInfo representing metadata about @name, or %NULL
+ * Returns: (transfer full): #GIBaseInfo representing metadata about @name, or %NULL
*/
GIBaseInfo *
g_irepository_find_by_name (GIRepository *repository,
const gchar *namespace,
const gchar *name)
{
- IfaceData data;
- GTypelib *typelib;
+ GITypelib *typelib;
+ DirEntry *entry;
g_return_val_if_fail (namespace != NULL, NULL);
repository = get_repository (repository);
+ typelib = get_registered (repository, namespace, NULL);
+ g_return_val_if_fail (typelib != NULL, NULL);
- data.repo = repository;
- data.name = name;
- data.type = NULL;
- data.index = -1;
- data.iface = NULL;
+ entry = g_typelib_get_dir_entry_by_name (typelib, name);
+ if (entry == NULL)
+ return NULL;
+ return _g_info_new_full (entry->blob_type,
+ repository,
+ NULL, typelib, entry->offset);
+}
- typelib = get_registered (repository, namespace, NULL);
+typedef struct {
+ GIRepository *repository;
+ GQuark domain;
- g_return_val_if_fail (typelib != NULL, NULL);
+ GITypelib *result_typelib;
+ DirEntry *result;
+} FindByErrorDomainData;
- find_interface ((void *)namespace, typelib, &data);
+static void
+find_by_error_domain_foreach (gpointer key,
+ gpointer value,
+ gpointer datap)
+{
+ GITypelib *typelib = (GITypelib*)value;
+ FindByErrorDomainData *data = datap;
- return data.iface;
+ if (data->result != NULL)
+ return;
+
+ data->result = g_typelib_get_dir_entry_by_error_domain (typelib, data->domain);
+ if (data->result)
+ data->result_typelib = typelib;
+}
+
+/**
+ * g_irepository_find_by_error_domain:
+ * @repository: (allow-none): A #GIRepository or %NULL for the singleton
+ * process-global default #GIRepository
+ * @domain: a #GError domain
+ *
+ * Searches for the enum type corresponding to the given #GError
+ * domain. Before calling this function for a particular namespace,
+ * you must call g_irepository_require() once to load the namespace, or
+ * otherwise ensure the namespace has already been loaded.
+ *
+ * Returns: (transfer full): #GIEnumInfo representing metadata about @domain's
+ * enum type, or %NULL
+ * Since: 1.29.17
+ */
+GIEnumInfo *
+g_irepository_find_by_error_domain (GIRepository *repository,
+ GQuark domain)
+{
+ FindByErrorDomainData data;
+ GIEnumInfo *cached;
+
+ repository = get_repository (repository);
+
+ cached = g_hash_table_lookup (repository->priv->info_by_error_domain,
+ GUINT_TO_POINTER (domain));
+
+ if (cached != NULL)
+ return g_base_info_ref ((GIBaseInfo *)cached);
+
+ data.repository = repository;
+ data.domain = domain;
+ data.result_typelib = NULL;
+ data.result = NULL;
+
+ g_hash_table_foreach (repository->priv->typelibs, find_by_error_domain_foreach, &data);
+ if (data.result == NULL)
+ g_hash_table_foreach (repository->priv->lazy_typelibs, find_by_error_domain_foreach, &data);
+
+ if (data.result != NULL)
+ {
+ cached = _g_info_new_full (data.result->blob_type,
+ repository,
+ NULL, data.result_typelib, data.result->offset);
+
+ g_hash_table_insert (repository->priv->info_by_error_domain,
+ GUINT_TO_POINTER (domain),
+ g_base_info_ref (cached));
+ return cached;
+ }
+ return NULL;
}
static void
}
/**
- * g_irepository_get_namespaces:
- * @repository: A #GIRepository, may be %NULL for the default
+ * g_irepository_get_loaded_namespaces:
+ * @repository: (allow-none): A #GIRepository or %NULL for the singleton
+ * process-global default #GIRepository
*
* Return the list of currently loaded namespaces.
*
- * Returns: (utf8) (transfer full): List of namespaces
+ * Returns: (element-type utf8) (transfer full): List of namespaces
*/
gchar **
g_irepository_get_loaded_namespaces (GIRepository *repository)
/**
* g_irepository_get_version:
- * @repository: A #GIRepository, may be %NULL for the default
+ * @repository: (allow-none): A #GIRepository or %NULL for the singleton
+ * process-global default #GIRepository
* @namespace_: Namespace to inspect
*
* This function returns the loaded version associated with the given
* namespace @namespace_.
*
* Note: The namespace must have already been loaded using a function
- * such as #g_irepository_require before calling this function.
+ * such as g_irepository_require() before calling this function.
*
* Returns: Loaded version
*/
g_irepository_get_version (GIRepository *repository,
const gchar *namespace)
{
- GTypelib *typelib;
+ GITypelib *typelib;
Header *header;
g_return_val_if_fail (namespace != NULL, NULL);
/**
* g_irepository_get_shared_library:
- * @repository: A #GIRepository, may be %NULL for the default
+ * @repository: (allow-none): A #GIRepository or %NULL for the singleton
+ * process-global default #GIRepository
* @namespace_: Namespace to inspect
*
- * This function returns the full path to the shared C library
- * associated with the given namespace @namespace_. There may be no
- * shared library path associated, in which case this function will
- * return %NULL.
+ * This function returns a comma-separated list of paths to the
+ * shared C libraries associated with the given namespace @namespace_.
+ * There may be no shared library path associated, in which case this
+ * function will return %NULL.
*
* Note: The namespace must have already been loaded using a function
- * such as #g_irepository_require before calling this function.
+ * such as g_irepository_require() before calling this function.
*
- * Returns: Full path to shared library, or %NULL if none associated
+ * Returns: Comma-separated list of paths to shared libraries,
+ * or %NULL if none are associated
*/
const gchar *
g_irepository_get_shared_library (GIRepository *repository,
const gchar *namespace)
{
- GTypelib *typelib;
+ GITypelib *typelib;
Header *header;
g_return_val_if_fail (namespace != NULL, NULL);
}
/**
- * g_irepository_get_c_prefix
- * @repository: A #GIRepository, may be %NULL for the default
+ * g_irepository_get_c_prefix:
+ * @repository: (allow-none): A #GIRepository or %NULL for the singleton
+ * process-global default #GIRepository
* @namespace_: Namespace to inspect
*
* This function returns the "C prefix", or the C level namespace
* starts with this prefix, as well each #GType in the library.
*
* Note: The namespace must have already been loaded using a function
- * such as #g_irepository_require before calling this function.
+ * such as g_irepository_require() before calling this function.
*
* Returns: C namespace prefix, or %NULL if none associated
*/
g_irepository_get_c_prefix (GIRepository *repository,
const gchar *namespace_)
{
- GTypelib *typelib;
+ GITypelib *typelib;
Header *header;
g_return_val_if_fail (namespace_ != NULL, NULL);
g_return_val_if_fail (typelib != NULL, NULL);
header = (Header *) typelib->data;
- if (header->shared_library)
+ if (header->c_prefix)
return g_typelib_get_string (typelib, header->c_prefix);
else
return NULL;
}
/**
- * g_irepository_get_typelib_path
- * @repository: Repository, may be %NULL for the default
+ * g_irepository_get_typelib_path:
+ * @repository: (allow-none): A #GIRepository or %NULL for the singleton
+ * process-global default #GIRepository
* @namespace_: GI namespace to use, e.g. "Gtk"
*
* If namespace @namespace_ is loaded, return the full path to the
* .typelib file it was loaded from. If the typelib for
* namespace @namespace_ was included in a shared library, return
- * the special string "$lt;builtin$gt;".
+ * the special string "<builtin>".
*
* Returns: Filesystem path (or $lt;builtin$gt;) if successful, %NULL if namespace is not loaded
*/
static GMappedFile *
find_namespace_version (const gchar *namespace,
const gchar *version,
+ GSList *search_path,
gchar **path_ret)
{
- GSList *tmp_path;
GSList *ldir;
GError *error = NULL;
GMappedFile *mfile = NULL;
fname = g_strdup_printf ("%s-%s.typelib", namespace, version);
- tmp_path = build_search_path_with_overrides ();
- for (ldir = tmp_path; ldir; ldir = ldir->next)
+ for (ldir = search_path; ldir; ldir = ldir->next)
{
char *path = g_build_filename (ldir->data, fname, NULL);
break;
}
g_free (fname);
- g_slist_free (tmp_path);
return mfile;
}
g_mapped_file_unref (candidate->mfile);
g_free (candidate->path);
g_free (candidate->version);
- g_free (candidate);
+ g_slice_free (struct NamespaceVersionCandidadate, candidate);
}
-static GMappedFile *
-find_namespace_latest (const gchar *namespace,
- gchar **version_ret,
- gchar **path_ret)
+static GSList *
+enumerate_namespace_versions (const gchar *namespace,
+ GSList *search_path)
{
- GSList *tmp_path;
- GSList *ldir;
- GError *error = NULL;
+ GSList *candidates = NULL;
+ GHashTable *found_versions = g_hash_table_new (g_str_hash, g_str_equal);
char *namespace_dash;
char *namespace_typelib;
- GSList *candidates = NULL;
- GMappedFile *result = NULL;
+ GSList *ldir;
+ GError *error = NULL;
int index;
- *version_ret = NULL;
- *path_ret = NULL;
-
namespace_dash = g_strdup_printf ("%s-", namespace);
namespace_typelib = g_strdup_printf ("%s.typelib", namespace);
index = 0;
- tmp_path = build_search_path_with_overrides ();
- for (ldir = tmp_path; ldir; ldir = ldir->next)
+ for (ldir = search_path; ldir; ldir = ldir->next)
{
GDir *dir;
const char *dirname;
last_dash = strrchr (entry, '-');
version = g_strndup (last_dash+1, name_end-(last_dash+1));
if (!parse_version (version, &major, &minor))
- continue;
+ {
+ g_free (version);
+ continue;
+ }
}
else
continue;
+ if (g_hash_table_lookup (found_versions, version) != NULL)
+ {
+ g_free (version);
+ continue;
+ }
+ g_hash_table_insert (found_versions, version, version);
+
path = g_build_filename (dirname, entry, NULL);
mfile = g_mapped_file_new (path, FALSE, &error);
if (mfile == NULL)
g_clear_error (&error);
continue;
}
- candidate = g_new0 (struct NamespaceVersionCandidadate, 1);
+ candidate = g_slice_new0 (struct NamespaceVersionCandidadate);
candidate->mfile = mfile;
candidate->path_index = index;
candidate->path = path;
index++;
}
+ g_free (namespace_dash);
+ g_free (namespace_typelib);
+ g_hash_table_destroy (found_versions);
+
+ return candidates;
+}
+
+static GMappedFile *
+find_namespace_latest (const gchar *namespace,
+ GSList *search_path,
+ gchar **version_ret,
+ gchar **path_ret)
+{
+ GSList *candidates;
+ GMappedFile *result = NULL;
+
+ *version_ret = NULL;
+ *path_ret = NULL;
+
+ candidates = enumerate_namespace_versions (namespace, search_path);
+
if (candidates != NULL)
{
struct NamespaceVersionCandidadate *elected;
result = elected->mfile;
*path_ret = elected->path;
*version_ret = elected->version;
- g_free (elected); /* just free the container */
+ g_slice_free (struct NamespaceVersionCandidadate, elected); /* just free the container */
g_slist_foreach (candidates, (GFunc) free_candidate, NULL);
g_slist_free (candidates);
}
- g_free (namespace_dash);
- g_free (namespace_typelib);
- g_slist_free (tmp_path);
return result;
}
/**
- * g_irepository_require:
- * @repository: (allow-none): Repository, may be %NULL for the default
- * @namespace_: GI namespace to use, e.g. "Gtk"
- * @version: (allow-none): Version of namespace, may be %NULL for latest
- * @flags: Set of %GIRepositoryLoadFlags, may be 0
- * @error: a #GError.
+ * g_irepository_enumerate_versions:
+ * @repository: (allow-none): A #GIRepository or %NULL for the singleton
+ * process-global default #GIRepository
+ * @namespace_: GI namespace, e.g. "Gtk"
*
- * Force the namespace @namespace_ to be loaded if it isn't already.
- * If @namespace_ is not loaded, this function will search for a
- * ".typelib" file using the repository search path. In addition, a
- * version @version of namespace may be specified. If @version is
- * not specified, the latest will be used.
+ * Obtain an unordered list of versions (either currently loaded or
+ * available) for @namespace_ in this @repository.
*
- * Returns: a pointer to the #GTypelib if successful, %NULL otherwise
+ * Returns: (element-type utf8) (transfer full): the array of versions.
*/
-GTypelib *
-g_irepository_require (GIRepository *repository,
- const gchar *namespace,
- const gchar *version,
- GIRepositoryLoadFlags flags,
- GError **error)
+GList *
+g_irepository_enumerate_versions (GIRepository *repository,
+ const gchar *namespace_)
+{
+ GList *ret = NULL;
+ GSList *candidates, *link;
+ const gchar *loaded_version;
+
+ init_globals ();
+ candidates = enumerate_namespace_versions (namespace_, search_path);
+
+ for (link = candidates; link; link = link->next)
+ {
+ struct NamespaceVersionCandidadate *candidate = link->data;
+ ret = g_list_prepend (ret, g_strdup (candidate->version));
+ free_candidate (candidate);
+ }
+ g_slist_free (candidates);
+
+ /* The currently loaded version of a namespace is also part of the
+ * available versions, as it could have been loaded using
+ * require_private().
+ */
+ if (g_irepository_is_registered (repository, namespace_, NULL))
+ {
+ loaded_version = g_irepository_get_version (repository, namespace_);
+ if (loaded_version && !g_list_find_custom (ret, loaded_version, g_str_equal))
+ ret = g_list_prepend (ret, g_strdup (loaded_version));
+ }
+
+ return ret;
+}
+
+static GITypelib *
+require_internal (GIRepository *repository,
+ const gchar *namespace,
+ const gchar *version,
+ GIRepositoryLoadFlags flags,
+ GSList *search_path,
+ GError **error)
{
GMappedFile *mfile;
- GTypelib *ret = NULL;
+ GITypelib *ret = NULL;
Header *header;
- GTypelib *typelib = NULL;
+ GITypelib *typelib = NULL;
const gchar *typelib_namespace, *typelib_version;
gboolean allow_lazy = (flags & G_IREPOSITORY_LOAD_FLAG_LAZY) > 0;
gboolean is_lazy;
if (version != NULL)
{
- mfile = find_namespace_version (namespace, version, &path);
+ mfile = find_namespace_version (namespace, version,
+ search_path, &path);
tmp_version = g_strdup (version);
}
else
{
- mfile = find_namespace_latest (namespace, &tmp_version, &path);
+ mfile = find_namespace_latest (namespace, search_path,
+ &tmp_version, &path);
}
if (mfile == NULL)
goto out;
}
- typelib = g_typelib_new_from_mapped_file (mfile);
+ {
+ GError *temp_error = NULL;
+ typelib = g_typelib_new_from_mapped_file (mfile, &temp_error);
+ if (!typelib)
+ {
+ g_set_error (error, G_IREPOSITORY_ERROR,
+ G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
+ "Failed to load typelib file '%s' for namespace '%s': %s",
+ path, namespace, temp_error->message);
+ g_clear_error (&temp_error);
+ goto out;
+ }
+ }
header = (Header *) typelib->data;
typelib_namespace = g_typelib_get_string (typelib, header->namespace);
typelib_version = g_typelib_get_string (typelib, header->nsversion);
"Typelib file %s for namespace '%s' contains "
"namespace '%s' which doesn't match the file name",
path, namespace, typelib_namespace);
+ g_typelib_free (typelib);
goto out;
}
if (version != NULL && strcmp (typelib_version, version) != 0)
"Typelib file %s for namespace '%s' contains "
"version '%s' which doesn't match the expected version '%s'",
path, namespace, typelib_version, version);
+ g_typelib_free (typelib);
goto out;
}
return ret;
}
+/**
+ * g_irepository_require:
+ * @repository: (allow-none): A #GIRepository or %NULL for the singleton
+ * process-global default #GIRepository
+ * @namespace_: GI namespace to use, e.g. "Gtk"
+ * @version: (allow-none): Version of namespace, may be %NULL for latest
+ * @flags: Set of %GIRepositoryLoadFlags, may be 0
+ * @error: a #GError.
+ *
+ * Force the namespace @namespace_ to be loaded if it isn't already.
+ * If @namespace_ is not loaded, this function will search for a
+ * ".typelib" file using the repository search path. In addition, a
+ * version @version of namespace may be specified. If @version is
+ * not specified, the latest will be used.
+ *
+ * Returns: (transfer none): a pointer to the #GITypelib if successful, %NULL otherwise
+ */
+GITypelib *
+g_irepository_require (GIRepository *repository,
+ const gchar *namespace,
+ const gchar *version,
+ GIRepositoryLoadFlags flags,
+ GError **error)
+{
+ GITypelib *typelib;
+
+ init_globals ();
+ typelib = require_internal (repository, namespace, version, flags,
+ search_path, error);
+
+ return typelib;
+}
+
+/**
+ * g_irepository_require_private:
+ * @repository: (allow-none): A #GIRepository or %NULL for the singleton
+ * process-global default #GIRepository
+ * @typelib_dir: Private directory where to find the requested typelib
+ * @namespace_: GI namespace to use, e.g. "Gtk"
+ * @version: (allow-none): Version of namespace, may be %NULL for latest
+ * @flags: Set of %GIRepositoryLoadFlags, may be 0
+ * @error: a #GError.
+ *
+ * Force the namespace @namespace_ to be loaded if it isn't already.
+ * If @namespace_ is not loaded, this function will search for a
+ * ".typelib" file within the private directory only. In addition, a
+ * version @version of namespace should be specified. If @version is
+ * not specified, the latest will be used.
+ *
+ * Returns: (transfer none): a pointer to the #GITypelib if successful, %NULL otherwise
+ */
+GITypelib *
+g_irepository_require_private (GIRepository *repository,
+ const gchar *typelib_dir,
+ const gchar *namespace,
+ const gchar *version,
+ GIRepositoryLoadFlags flags,
+ GError **error)
+{
+ GSList search_path = { (gpointer) typelib_dir, NULL };
+
+ return require_internal (repository, namespace, version, flags,
+ &search_path, error);
+}
+
static gboolean
g_irepository_introspect_cb (const char *option_name,
const char *value,
gpointer data,
GError **error)
{
- gboolean ret = g_irepository_dump (value, error);
- exit (ret ? 0 : 1);
+ GError *tmp_error = NULL;
+ gboolean ret = g_irepository_dump (value, &tmp_error);
+ if (!ret)
+ {
+ g_error ("Failed to extract GType data: %s",
+ tmp_error->message);
+ exit (1);
+ }
+ exit (0);
}
static const GOptionEntry introspection_args[] = {
{ NULL }
};
+/**
+ * g_irepository_get_option_group: (skip)
+ *
+ * Obtain the option group for girepository, it's used
+ * by the dumper and for programs that wants to provide
+ * introspection information
+ *
+ * Returns: (transfer full): the option group
+ */
GOptionGroup *
g_irepository_get_option_group (void)
{
return quark;
}
+/**
+ * g_type_tag_to_string:
+ * @type: the type_tag
+ *
+ * Obtain a string representation of @type
+ *
+ * Returns: the string
+ */
const gchar*
g_type_tag_to_string (GITypeTag type)
{
case GI_TYPE_TAG_VOID:
return "void";
case GI_TYPE_TAG_BOOLEAN:
- return "boolean";
+ return "gboolean";
case GI_TYPE_TAG_INT8:
- return "int8";
+ return "gint8";
case GI_TYPE_TAG_UINT8:
- return "uint8";
+ return "guint8";
case GI_TYPE_TAG_INT16:
- return "int16";
+ return "gint16";
case GI_TYPE_TAG_UINT16:
- return "uint16";
+ return "guint16";
case GI_TYPE_TAG_INT32:
- return "int32";
+ return "gint32";
case GI_TYPE_TAG_UINT32:
- return "uint32";
+ return "guint32";
case GI_TYPE_TAG_INT64:
- return "int64";
+ return "gint64";
case GI_TYPE_TAG_UINT64:
- return "uint64";
- case GI_TYPE_TAG_SHORT:
- return "short";
- case GI_TYPE_TAG_USHORT:
- return "ushort";
- case GI_TYPE_TAG_INT:
- return "int";
- case GI_TYPE_TAG_UINT:
- return "uint";
- case GI_TYPE_TAG_LONG:
- return "long";
- case GI_TYPE_TAG_ULONG:
- return "ulong";
- case GI_TYPE_TAG_SSIZE:
- return "ssize";
- case GI_TYPE_TAG_SIZE:
- return "size";
+ return "guint64";
case GI_TYPE_TAG_FLOAT:
- return "float";
+ return "gfloat";
case GI_TYPE_TAG_DOUBLE:
- return "double";
- case GI_TYPE_TAG_TIME_T:
- return "time_t";
+ return "gdouble";
+ case GI_TYPE_TAG_UNICHAR:
+ return "gunichar";
case GI_TYPE_TAG_GTYPE:
return "GType";
case GI_TYPE_TAG_UTF8:
return "unknown";
}
}
+
+/**
+ * g_info_type_to_string:
+ * @type: the info type
+ *
+ * Obtain a string representation of @type
+ *
+ * Returns: the string
+ */
+const gchar*
+g_info_type_to_string (GIInfoType type)
+{
+ switch (type)
+ {
+ case GI_INFO_TYPE_INVALID:
+ return "invalid";
+ case GI_INFO_TYPE_FUNCTION:
+ return "function";
+ case GI_INFO_TYPE_CALLBACK:
+ return "callback";
+ case GI_INFO_TYPE_STRUCT:
+ return "struct";
+ case GI_INFO_TYPE_BOXED:
+ return "boxed";
+ case GI_INFO_TYPE_ENUM:
+ return "enum";
+ case GI_INFO_TYPE_FLAGS:
+ return "flags";
+ case GI_INFO_TYPE_OBJECT:
+ return "object";
+ case GI_INFO_TYPE_INTERFACE:
+ return "interface";
+ case GI_INFO_TYPE_CONSTANT:
+ return "constant";
+ case GI_INFO_TYPE_UNION:
+ return "union";
+ case GI_INFO_TYPE_VALUE:
+ return "value";
+ case GI_INFO_TYPE_SIGNAL:
+ return "signal";
+ case GI_INFO_TYPE_VFUNC:
+ return "vfunc";
+ case GI_INFO_TYPE_PROPERTY:
+ return "property";
+ case GI_INFO_TYPE_FIELD:
+ return "field";
+ case GI_INFO_TYPE_ARG:
+ return "arg";
+ case GI_INFO_TYPE_TYPE:
+ return "type";
+ case GI_INFO_TYPE_UNRESOLVED:
+ return "unresolved";
+ default:
+ return "unknown";
+ }
+}