2008-02-08 16:31:03 +01:00
|
|
|
/* -*- Mode: C; c-file-style: "gnu"; -*- */
|
|
|
|
/* GObject introspection: Repository implementation
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Matthias Clasen
|
2008-10-12 06:51:48 +02:00
|
|
|
* Copyright (C) 2008 Colin Walters <walters@verbum.org>
|
|
|
|
* Copyright (C) 2008 Red Hat, Inc.
|
2008-02-08 16:31:03 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
#include <glib/gprintf.h>
|
|
|
|
#include <gmodule.h>
|
|
|
|
#include "girepository.h"
|
2008-06-08 16:37:30 +02:00
|
|
|
#include "gtypelib.h"
|
2008-02-08 16:31:03 +01:00
|
|
|
|
2008-08-21 01:56:40 +02:00
|
|
|
static GStaticMutex globals_lock = G_STATIC_MUTEX_INIT;
|
2008-02-08 16:31:03 +01:00
|
|
|
static GIRepository *default_repository = NULL;
|
|
|
|
static GSList *search_path = NULL;
|
|
|
|
|
|
|
|
struct _GIRepositoryPrivate
|
|
|
|
{
|
2008-08-30 22:31:07 +02:00
|
|
|
GHashTable *typelibs; /* (string) namespace -> GTypelib */
|
2008-10-12 06:51:48 +02:00
|
|
|
GHashTable *lazy_typelibs; /* (string) namespace-version -> GTypelib */
|
2008-02-08 16:31:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GIRepository, g_irepository, G_TYPE_OBJECT);
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_irepository_init (GIRepository *repository)
|
|
|
|
{
|
|
|
|
repository->priv = G_TYPE_INSTANCE_GET_PRIVATE (repository, G_TYPE_IREPOSITORY,
|
|
|
|
GIRepositoryPrivate);
|
2008-08-30 22:31:12 +02:00
|
|
|
repository->priv->typelibs
|
|
|
|
= g_hash_table_new_full (g_str_hash, g_str_equal,
|
|
|
|
(GDestroyNotify) NULL,
|
|
|
|
(GDestroyNotify) g_typelib_free);
|
|
|
|
repository->priv->lazy_typelibs
|
|
|
|
= g_hash_table_new (g_str_hash, g_str_equal);
|
2008-02-08 16:31:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_irepository_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
GIRepository *repository = G_IREPOSITORY (object);
|
|
|
|
|
2008-08-30 22:31:07 +02:00
|
|
|
g_hash_table_destroy (repository->priv->typelibs);
|
2008-10-12 06:51:48 +02:00
|
|
|
g_hash_table_destroy (repository->priv->lazy_typelibs);
|
2008-02-08 16:31:03 +01:00
|
|
|
|
|
|
|
(* G_OBJECT_CLASS (g_irepository_parent_class)->finalize) (G_OBJECT (repository));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_irepository_class_init (GIRepositoryClass *class)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
|
|
|
|
gobject_class = G_OBJECT_CLASS (class);
|
|
|
|
|
|
|
|
gobject_class->finalize = g_irepository_finalize;
|
|
|
|
|
|
|
|
g_type_class_add_private (class, sizeof (GIRepositoryPrivate));
|
|
|
|
}
|
|
|
|
|
2008-08-21 01:56:40 +02:00
|
|
|
static void
|
|
|
|
init_globals ()
|
|
|
|
{
|
|
|
|
g_static_mutex_lock (&globals_lock);
|
|
|
|
|
|
|
|
if (default_repository == NULL)
|
|
|
|
{
|
|
|
|
default_repository = g_object_new (G_TYPE_IREPOSITORY, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (search_path == NULL)
|
|
|
|
{
|
|
|
|
const gchar *const *datadirs;
|
|
|
|
const gchar *const *dir;
|
|
|
|
|
|
|
|
datadirs = g_get_system_data_dirs ();
|
|
|
|
|
|
|
|
search_path = NULL;
|
|
|
|
for (dir = datadirs; *dir; dir++) {
|
2008-08-26 23:55:42 +02:00
|
|
|
char *path = g_build_filename (*dir, "girepository", NULL);
|
2008-08-21 01:56:40 +02:00
|
|
|
search_path = g_slist_prepend (search_path, path);
|
|
|
|
}
|
|
|
|
search_path = g_slist_reverse (search_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_static_mutex_unlock (&globals_lock);
|
|
|
|
}
|
|
|
|
|
2008-08-30 22:31:07 +02:00
|
|
|
void
|
|
|
|
g_irepository_prepend_search_path (const char *directory)
|
|
|
|
{
|
|
|
|
init_globals ();
|
|
|
|
search_path = g_slist_prepend (search_path, g_strdup (directory));
|
|
|
|
}
|
|
|
|
|
2008-08-21 05:06:13 +02:00
|
|
|
static char *
|
|
|
|
build_typelib_key (const char *name, const char *source)
|
|
|
|
{
|
|
|
|
GString *str = g_string_new (name);
|
|
|
|
g_string_append_c (str, '\0');
|
|
|
|
g_string_append (str, source);
|
|
|
|
return g_string_free (str, FALSE);
|
|
|
|
}
|
|
|
|
|
2008-08-30 22:31:07 +02:00
|
|
|
static char **
|
|
|
|
get_typelib_dependencies (GTypelib *typelib)
|
2008-02-08 16:31:03 +01:00
|
|
|
{
|
|
|
|
Header *header;
|
2008-08-30 22:31:07 +02:00
|
|
|
const char *dependencies_glob;
|
|
|
|
|
2008-08-09 14:55:32 +02:00
|
|
|
header = (Header *)typelib->data;
|
2008-02-08 16:31:03 +01:00
|
|
|
|
2008-08-30 22:31:07 +02:00
|
|
|
if (header->dependencies == 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
dependencies_glob = g_typelib_get_string (typelib, header->dependencies);
|
|
|
|
return g_strsplit (dependencies_glob, "|", 0);
|
|
|
|
}
|
2008-02-08 16:31:03 +01:00
|
|
|
|
2008-08-30 22:31:07 +02:00
|
|
|
static GIRepository *
|
|
|
|
get_repository (GIRepository *repository)
|
|
|
|
{
|
2008-02-08 16:31:03 +01:00
|
|
|
if (repository != NULL)
|
2008-08-30 22:31:12 +02:00
|
|
|
return repository;
|
2008-02-08 16:31:03 +01:00
|
|
|
else
|
|
|
|
{
|
2008-08-21 01:56:40 +02:00
|
|
|
init_globals ();
|
2008-08-30 22:31:07 +02:00
|
|
|
return default_repository;
|
2008-02-08 16:31:03 +01:00
|
|
|
}
|
2008-08-30 22:31:07 +02:00
|
|
|
}
|
|
|
|
|
2008-10-12 06:51:48 +02:00
|
|
|
static GTypelib *
|
|
|
|
check_version_conflict (GTypelib *typelib,
|
|
|
|
const gchar *namespace,
|
|
|
|
const gchar *expected_version,
|
|
|
|
char **version_conflict)
|
|
|
|
{
|
|
|
|
Header *header;
|
|
|
|
const char *loaded_version;
|
|
|
|
|
|
|
|
if (expected_version == NULL)
|
|
|
|
{
|
|
|
|
if (version_conflict)
|
|
|
|
*version_conflict = NULL;
|
|
|
|
return typelib;
|
|
|
|
}
|
|
|
|
|
|
|
|
header = (Header*)typelib->data;
|
|
|
|
loaded_version = g_typelib_get_string (typelib, header->nsversion);
|
|
|
|
g_assert (loaded_version != NULL);
|
|
|
|
|
|
|
|
if (strcmp (expected_version, loaded_version) != 0)
|
|
|
|
{
|
|
|
|
if (version_conflict)
|
|
|
|
*version_conflict = (char*)loaded_version;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (version_conflict)
|
|
|
|
*version_conflict = NULL;
|
|
|
|
return typelib;
|
|
|
|
}
|
|
|
|
|
2008-08-30 22:31:12 +02:00
|
|
|
static GTypelib *
|
|
|
|
get_registered_status (GIRepository *repository,
|
|
|
|
const char *namespace,
|
2008-10-12 06:51:48 +02:00
|
|
|
const char *version,
|
2008-08-30 22:31:12 +02:00
|
|
|
gboolean allow_lazy,
|
2008-10-12 06:51:48 +02:00
|
|
|
gboolean *lazy_status,
|
|
|
|
char **version_conflict)
|
2008-08-30 22:31:07 +02:00
|
|
|
{
|
2008-08-30 22:31:12 +02:00
|
|
|
GTypelib *typelib;
|
|
|
|
repository = get_repository (repository);
|
|
|
|
if (lazy_status)
|
|
|
|
*lazy_status = FALSE;
|
|
|
|
typelib = g_hash_table_lookup (repository->priv->typelibs, namespace);
|
2008-10-12 06:51:48 +02:00
|
|
|
if (typelib)
|
|
|
|
return check_version_conflict (typelib, namespace, version, version_conflict);
|
2008-08-30 22:31:12 +02:00
|
|
|
typelib = g_hash_table_lookup (repository->priv->lazy_typelibs, namespace);
|
|
|
|
if (!typelib)
|
|
|
|
return NULL;
|
|
|
|
if (lazy_status)
|
|
|
|
*lazy_status = TRUE;
|
|
|
|
if (!allow_lazy)
|
|
|
|
return NULL;
|
2008-10-12 06:51:48 +02:00
|
|
|
return check_version_conflict (typelib, namespace, version, version_conflict);
|
2008-08-30 22:31:12 +02:00
|
|
|
}
|
2008-08-30 22:31:07 +02:00
|
|
|
|
2008-08-30 22:31:12 +02:00
|
|
|
static GTypelib *
|
|
|
|
get_registered (GIRepository *repository,
|
2008-10-12 06:51:48 +02:00
|
|
|
const char *namespace,
|
|
|
|
const char *version)
|
2008-08-30 22:31:12 +02:00
|
|
|
{
|
2008-10-12 06:51:48 +02:00
|
|
|
return get_registered_status (repository, namespace, version, TRUE, NULL, NULL);
|
2008-08-30 22:31:12 +02:00
|
|
|
}
|
2008-02-08 16:31:03 +01:00
|
|
|
|
2008-08-30 22:31:12 +02:00
|
|
|
static gboolean
|
|
|
|
load_dependencies_recurse (GIRepository *repository,
|
|
|
|
GTypelib *typelib,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
char **dependencies;
|
2008-02-08 16:31:03 +01:00
|
|
|
|
2008-08-30 22:31:07 +02:00
|
|
|
dependencies = get_typelib_dependencies (typelib);
|
2008-08-30 22:31:12 +02:00
|
|
|
|
2008-08-30 22:31:07 +02:00
|
|
|
if (dependencies != NULL)
|
2008-02-08 16:31:03 +01:00
|
|
|
{
|
2008-08-30 22:31:07 +02:00
|
|
|
int i;
|
2008-08-30 22:31:12 +02:00
|
|
|
|
2008-08-30 22:31:07 +02:00
|
|
|
for (i = 0; dependencies[i]; i++)
|
|
|
|
{
|
|
|
|
char *dependency = dependencies[i];
|
2008-10-12 06:51:48 +02:00
|
|
|
const char *last_dash;
|
|
|
|
char *dependency_namespace;
|
|
|
|
const char *dependency_version;
|
|
|
|
|
|
|
|
last_dash = strrchr (dependency, '-');
|
|
|
|
dependency_namespace = g_strndup (dependency, last_dash - dependency);
|
|
|
|
dependency_version = last_dash+1;
|
2008-08-30 22:31:12 +02:00
|
|
|
|
2008-10-12 06:51:48 +02:00
|
|
|
if (!g_irepository_require (repository, dependency_namespace, dependency_version,
|
2008-08-30 22:31:12 +02:00
|
|
|
0, error))
|
2008-08-30 22:31:07 +02:00
|
|
|
{
|
2008-10-12 06:51:48 +02:00
|
|
|
g_free (dependency_namespace);
|
2008-08-30 22:31:07 +02:00
|
|
|
g_strfreev (dependencies);
|
2008-08-30 22:31:12 +02:00
|
|
|
return FALSE;
|
2008-08-30 22:31:07 +02:00
|
|
|
}
|
2008-10-12 06:51:48 +02:00
|
|
|
g_free (dependency_namespace);
|
2008-08-30 22:31:07 +02:00
|
|
|
}
|
|
|
|
g_strfreev (dependencies);
|
2008-02-08 16:31:03 +01:00
|
|
|
}
|
2008-08-30 22:31:12 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
2008-10-12 06:51:48 +02:00
|
|
|
|
2008-08-30 22:31:12 +02:00
|
|
|
static const char *
|
|
|
|
register_internal (GIRepository *repository,
|
|
|
|
const char *source,
|
|
|
|
gboolean lazy,
|
|
|
|
GTypelib *typelib,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
Header *header;
|
|
|
|
const gchar *namespace;
|
2008-10-12 06:51:48 +02:00
|
|
|
const gchar *version;
|
2008-08-30 22:31:12 +02:00
|
|
|
gboolean was_loaded;
|
|
|
|
gboolean currently_lazy;
|
|
|
|
|
|
|
|
g_return_val_if_fail (typelib != NULL, FALSE);
|
|
|
|
|
|
|
|
header = (Header *)typelib->data;
|
|
|
|
|
|
|
|
g_return_val_if_fail (header != NULL, FALSE);
|
|
|
|
|
|
|
|
namespace = g_typelib_get_string (typelib, header->namespace);
|
2008-10-12 06:51:48 +02:00
|
|
|
version = g_typelib_get_string (typelib, header->nsversion);
|
2008-08-30 22:31:12 +02:00
|
|
|
|
|
|
|
if (lazy)
|
|
|
|
{
|
|
|
|
g_assert (!g_hash_table_lookup (repository->priv->lazy_typelibs,
|
|
|
|
namespace));
|
|
|
|
g_hash_table_insert (repository->priv->lazy_typelibs,
|
|
|
|
build_typelib_key (namespace, source), (void *)typelib);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gpointer value;
|
|
|
|
char *key;
|
2008-08-30 22:31:07 +02:00
|
|
|
|
2008-08-30 22:31:12 +02:00
|
|
|
/* First, try loading all the dependencies */
|
|
|
|
if (!load_dependencies_recurse (repository, typelib, error))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Check if we are transitioning from lazily loaded state */
|
|
|
|
if (g_hash_table_lookup_extended (repository->priv->lazy_typelibs,
|
|
|
|
namespace,
|
|
|
|
(gpointer)&key, &value))
|
|
|
|
g_hash_table_remove (repository->priv->lazy_typelibs, key);
|
|
|
|
else
|
|
|
|
key = build_typelib_key (namespace, source);
|
|
|
|
|
|
|
|
g_hash_table_insert (repository->priv->typelibs, key, (void *)typelib);
|
|
|
|
}
|
2008-02-08 16:31:03 +01:00
|
|
|
|
2008-10-09 18:44:11 +02:00
|
|
|
if (typelib->modules == NULL)
|
|
|
|
typelib->modules = g_list_append(typelib->modules, g_module_open (NULL, 0));
|
|
|
|
|
2008-08-30 22:31:12 +02:00
|
|
|
return namespace;
|
2008-02-08 16:31:03 +01:00
|
|
|
}
|
|
|
|
|
2008-08-30 22:31:07 +02:00
|
|
|
char **
|
|
|
|
g_irepository_get_dependencies (GIRepository *repository,
|
|
|
|
const char *namespace)
|
|
|
|
{
|
|
|
|
GTypelib *typelib;
|
|
|
|
|
|
|
|
g_return_val_if_fail (namespace != NULL, NULL);
|
|
|
|
|
|
|
|
repository = get_repository (repository);
|
|
|
|
|
2008-10-12 06:51:48 +02:00
|
|
|
typelib = get_registered (repository, namespace, NULL);
|
2008-08-30 22:31:07 +02:00
|
|
|
g_return_val_if_fail (typelib != NULL, NULL);
|
|
|
|
|
|
|
|
return get_typelib_dependencies (typelib);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
g_irepository_load_typelib (GIRepository *repository,
|
|
|
|
GTypelib *typelib,
|
2008-08-30 22:31:12 +02:00
|
|
|
GIRepositoryLoadFlags flags,
|
2008-08-30 22:31:07 +02:00
|
|
|
GError **error)
|
2008-08-21 05:06:13 +02:00
|
|
|
{
|
2008-08-30 22:31:07 +02:00
|
|
|
Header *header;
|
|
|
|
const char *namespace;
|
2008-10-12 06:51:48 +02:00
|
|
|
const char *nsversion;
|
2008-08-30 22:31:12 +02:00
|
|
|
gboolean allow_lazy = flags & G_IREPOSITORY_LOAD_FLAG_LAZY;
|
|
|
|
gboolean is_lazy;
|
2008-10-12 06:51:48 +02:00
|
|
|
char *version_conflict;
|
2008-08-30 22:31:07 +02:00
|
|
|
|
|
|
|
repository = get_repository (repository);
|
|
|
|
|
|
|
|
header = (Header *) typelib->data;
|
|
|
|
namespace = g_typelib_get_string (typelib, header->namespace);
|
2008-10-12 06:51:48 +02:00
|
|
|
nsversion = g_typelib_get_string (typelib, header->nsversion);
|
2008-08-30 22:31:07 +02:00
|
|
|
|
2008-10-12 06:51:48 +02:00
|
|
|
if (get_registered_status (repository, namespace, nsversion, allow_lazy,
|
|
|
|
&is_lazy, &version_conflict))
|
|
|
|
{
|
|
|
|
if (version_conflict != NULL)
|
|
|
|
{
|
|
|
|
g_set_error (error, G_IREPOSITORY_ERROR,
|
|
|
|
G_IREPOSITORY_ERROR_NAMESPACE_VERSION_CONFLICT,
|
|
|
|
"Attempting to load namespace '%s', version '%s', but '%s' is already loaded",
|
|
|
|
namespace, nsversion, version_conflict);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return namespace;
|
|
|
|
}
|
2008-08-30 22:31:12 +02:00
|
|
|
return register_internal (repository, "<builtin>",
|
|
|
|
allow_lazy, typelib, error);
|
2008-02-08 16:31:03 +01:00
|
|
|
}
|
|
|
|
|
2008-10-12 06:51:48 +02:00
|
|
|
/**
|
|
|
|
* g_irepository_is_registered
|
|
|
|
* @repository: A #GIRepository, may be %NULL for the default
|
|
|
|
* @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
|
|
|
|
* instead which will ensure the namespace is loaded, and return as
|
|
|
|
* quickly as this function will if it has already been loaded.
|
|
|
|
*
|
|
|
|
* Returns: %TRUE if namespace-version is loaded, %FALSE otherwise
|
|
|
|
*/
|
2008-02-08 16:31:03 +01:00
|
|
|
gboolean
|
|
|
|
g_irepository_is_registered (GIRepository *repository,
|
2008-10-12 06:51:48 +02:00
|
|
|
const gchar *namespace,
|
|
|
|
const gchar *version)
|
2008-02-08 16:31:03 +01:00
|
|
|
{
|
2008-08-30 22:31:07 +02:00
|
|
|
repository = get_repository (repository);
|
2008-10-12 06:51:48 +02:00
|
|
|
return get_registered (repository, namespace, version) != NULL;
|
2008-02-08 16:31:03 +01:00
|
|
|
}
|
|
|
|
|
2008-10-12 06:51:48 +02:00
|
|
|
/**
|
|
|
|
* g_irepository_get_default
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Returns: The global singleton #GIRepository
|
|
|
|
*/
|
2008-02-08 16:31:03 +01:00
|
|
|
GIRepository *
|
|
|
|
g_irepository_get_default (void)
|
|
|
|
{
|
2008-08-30 22:31:07 +02:00
|
|
|
return get_repository (NULL);
|
2008-02-08 16:31:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
count_interfaces (gpointer key,
|
|
|
|
gpointer value,
|
|
|
|
gpointer data)
|
|
|
|
{
|
2008-08-09 14:55:32 +02:00
|
|
|
guchar *typelib = ((GTypelib *) value)->data;
|
2008-02-08 16:31:03 +01:00
|
|
|
gint *n_interfaces = (gint *)data;
|
|
|
|
|
2008-08-09 14:55:32 +02:00
|
|
|
*n_interfaces += ((Header *)typelib)->n_local_entries;
|
2008-02-08 16:31:03 +01:00
|
|
|
}
|
|
|
|
|
2008-10-12 06:51:48 +02:00
|
|
|
/**
|
|
|
|
* g_irepository_get_n_infos
|
|
|
|
* @repository: A #GIRepository, may be %NULL for the default
|
|
|
|
* @namespace: Namespace to inspect
|
|
|
|
*
|
|
|
|
* This function returns the number of metadata entries in
|
|
|
|
* given namespace @namespace. The namespace must have
|
|
|
|
* already been loaded before calling this function.
|
|
|
|
*
|
|
|
|
* Returns: number of metadata entries
|
|
|
|
*/
|
2008-02-08 16:31:03 +01:00
|
|
|
gint
|
|
|
|
g_irepository_get_n_infos (GIRepository *repository,
|
|
|
|
const gchar *namespace)
|
|
|
|
{
|
2008-10-12 06:51:48 +02:00
|
|
|
GTypelib *typelib;
|
2008-02-08 16:31:03 +01:00
|
|
|
gint n_interfaces = 0;
|
2008-08-30 22:31:07 +02:00
|
|
|
|
2008-10-12 06:51:48 +02:00
|
|
|
g_return_val_if_fail (namespace != NULL, -1);
|
|
|
|
|
2008-08-30 22:31:07 +02:00
|
|
|
repository = get_repository (repository);
|
2008-02-08 16:31:03 +01:00
|
|
|
|
2008-10-12 06:51:48 +02:00
|
|
|
typelib = get_registered (repository, namespace, NULL);
|
2008-02-08 16:31:03 +01:00
|
|
|
|
2008-10-12 06:51:48 +02:00
|
|
|
g_return_val_if_fail (typelib != NULL, -1);
|
2008-02-08 16:31:03 +01:00
|
|
|
|
2008-10-12 06:51:48 +02:00
|
|
|
n_interfaces = ((Header *)typelib->data)->n_local_entries;
|
2008-02-08 16:31:03 +01:00
|
|
|
|
|
|
|
return n_interfaces;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
gint index;
|
|
|
|
const gchar *name;
|
|
|
|
const gchar *type;
|
|
|
|
GIBaseInfo *iface;
|
|
|
|
} IfaceData;
|
|
|
|
|
|
|
|
static void
|
|
|
|
find_interface (gpointer key,
|
|
|
|
gpointer value,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
gint i;
|
2008-08-09 14:55:32 +02:00
|
|
|
GTypelib *typelib = (GTypelib *)value;
|
2008-02-08 16:31:03 +01:00
|
|
|
IfaceData *iface_data = (IfaceData *)data;
|
|
|
|
gint index;
|
|
|
|
gint n_entries;
|
|
|
|
guint32 offset;
|
|
|
|
const gchar *name;
|
|
|
|
const gchar *type;
|
|
|
|
DirEntry *entry;
|
|
|
|
|
|
|
|
index = 0;
|
2008-08-09 14:55:32 +02:00
|
|
|
n_entries = ((Header *)typelib->data)->n_local_entries;
|
2008-02-08 16:31:03 +01:00
|
|
|
|
|
|
|
if (iface_data->name)
|
|
|
|
{
|
|
|
|
for (i = 1; i <= n_entries; i++)
|
|
|
|
{
|
2008-08-09 14:55:32 +02:00
|
|
|
entry = g_typelib_get_dir_entry (typelib, i);
|
|
|
|
name = g_typelib_get_string (typelib, entry->name);
|
2008-02-08 16:31:03 +01:00
|
|
|
if (strcmp (name, iface_data->name) == 0)
|
|
|
|
{
|
|
|
|
index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (iface_data->type)
|
|
|
|
{
|
|
|
|
for (i = 1; i <= n_entries; i++)
|
|
|
|
{
|
2008-10-12 22:53:26 +02:00
|
|
|
RegisteredTypeBlob *blob;
|
|
|
|
|
2008-08-09 14:55:32 +02:00
|
|
|
entry = g_typelib_get_dir_entry (typelib, i);
|
2008-10-12 22:53:26 +02:00
|
|
|
if (!BLOB_IS_REGISTERED_TYPE (entry))
|
2008-02-08 16:31:03 +01:00
|
|
|
continue;
|
2008-10-12 22:53:26 +02:00
|
|
|
|
2008-10-12 23:22:44 +02:00
|
|
|
blob = (RegisteredTypeBlob *)(&typelib->data[entry->offset]);
|
2008-10-12 22:53:26 +02:00
|
|
|
if (!blob->gtype_name)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
type = g_typelib_get_string (typelib, blob->gtype_name);
|
2008-02-08 16:31:03 +01:00
|
|
|
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)
|
|
|
|
{
|
2008-08-09 14:55:32 +02:00
|
|
|
entry = g_typelib_get_dir_entry (typelib, index);
|
2008-02-08 16:31:03 +01:00
|
|
|
iface_data->iface = g_info_new (entry->blob_type, NULL,
|
2008-08-09 14:55:32 +02:00
|
|
|
typelib, entry->offset);
|
2008-02-08 16:31:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-12 06:51:48 +02:00
|
|
|
/**
|
|
|
|
* g_irepository_get_info
|
|
|
|
* @repository: A #GIRepository, may be %NULL for the default
|
|
|
|
* @namespace: Namespace to inspect
|
|
|
|
* @index: 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.
|
|
|
|
*
|
|
|
|
* Returns: #GIBaseInfo containing metadata
|
|
|
|
*/
|
2008-02-08 16:31:03 +01:00
|
|
|
GIBaseInfo *
|
|
|
|
g_irepository_get_info (GIRepository *repository,
|
|
|
|
const gchar *namespace,
|
|
|
|
gint index)
|
|
|
|
{
|
|
|
|
IfaceData data;
|
2008-10-12 06:51:48 +02:00
|
|
|
GTypelib *typelib;
|
|
|
|
|
|
|
|
g_return_val_if_fail (namespace != NULL, NULL);
|
2008-02-08 16:31:03 +01:00
|
|
|
|
2008-08-30 22:31:07 +02:00
|
|
|
repository = get_repository (repository);
|
|
|
|
|
2008-02-08 16:31:03 +01:00
|
|
|
data.name = NULL;
|
|
|
|
data.type = NULL;
|
|
|
|
data.index = index + 1;
|
|
|
|
data.iface = NULL;
|
|
|
|
|
2008-10-12 06:51:48 +02:00
|
|
|
typelib = get_registered (repository, namespace, NULL);
|
|
|
|
|
|
|
|
g_return_val_if_fail (typelib != NULL, NULL);
|
|
|
|
|
|
|
|
find_interface ((void *)namespace, typelib, &data);
|
2008-02-08 16:31:03 +01:00
|
|
|
|
|
|
|
return data.iface;
|
|
|
|
}
|
|
|
|
|
2008-10-12 06:51:48 +02:00
|
|
|
/**
|
|
|
|
* g_irepository_find_by_gtype
|
|
|
|
* @repository: A #GIRepository, may be %NULL for the default
|
|
|
|
* @type: GType to search for
|
|
|
|
*
|
|
|
|
* Searches all loaded namespaces for a particular #GType. Note that
|
|
|
|
* in order to locate the metadata, the namespace corresponding to
|
|
|
|
* the type must first have been loaded. There is currently no
|
|
|
|
* mechanism for determining the namespace which corresponds to an
|
|
|
|
* arbitrary GType - thus, this function will function most reliably
|
|
|
|
* when you have expect the GType to be from a known namespace.
|
|
|
|
*
|
|
|
|
* Returns: #GIBaseInfo representing metadata about @type, or %NULL
|
|
|
|
*/
|
2008-02-08 16:31:03 +01:00
|
|
|
GIBaseInfo *
|
|
|
|
g_irepository_find_by_gtype (GIRepository *repository,
|
|
|
|
GType type)
|
|
|
|
{
|
|
|
|
IfaceData data;
|
|
|
|
|
2008-08-30 22:31:07 +02:00
|
|
|
repository = get_repository (repository);
|
|
|
|
|
2008-02-08 16:31:03 +01:00
|
|
|
data.name = NULL;
|
|
|
|
data.type = g_type_name (type);
|
|
|
|
data.index = -1;
|
|
|
|
data.iface = NULL;
|
|
|
|
|
2008-08-30 22:31:07 +02:00
|
|
|
g_hash_table_foreach (repository->priv->typelibs, find_interface, &data);
|
2008-08-30 22:31:12 +02:00
|
|
|
g_hash_table_foreach (repository->priv->lazy_typelibs, find_interface, &data);
|
2008-02-08 16:31:03 +01:00
|
|
|
|
|
|
|
return data.iface;
|
|
|
|
}
|
|
|
|
|
2008-08-21 01:56:40 +02:00
|
|
|
/**
|
|
|
|
* g_irepository_find_by_name
|
|
|
|
* @repository: A #GIRepository, may be %NULL for the default
|
2008-10-12 06:51:48 +02:00
|
|
|
* @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
|
|
|
|
* ensure the namespace has already been loaded.
|
2008-08-21 01:56:40 +02:00
|
|
|
*
|
|
|
|
* Returns: #GIBaseInfo representing metadata about @name, or %NULL
|
|
|
|
*/
|
2008-02-08 16:31:03 +01:00
|
|
|
GIBaseInfo *
|
|
|
|
g_irepository_find_by_name (GIRepository *repository,
|
|
|
|
const gchar *namespace,
|
|
|
|
const gchar *name)
|
|
|
|
{
|
|
|
|
IfaceData data;
|
2008-10-12 06:51:48 +02:00
|
|
|
GTypelib *typelib;
|
|
|
|
|
|
|
|
g_return_val_if_fail (namespace != NULL, NULL);
|
2008-02-08 16:31:03 +01:00
|
|
|
|
2008-08-30 22:31:07 +02:00
|
|
|
repository = get_repository (repository);
|
|
|
|
|
2008-02-08 16:31:03 +01:00
|
|
|
data.name = name;
|
|
|
|
data.type = NULL;
|
|
|
|
data.index = -1;
|
|
|
|
data.iface = NULL;
|
|
|
|
|
2008-10-12 06:51:48 +02:00
|
|
|
typelib = get_registered (repository, namespace, NULL);
|
|
|
|
|
|
|
|
g_return_val_if_fail (typelib != NULL, NULL);
|
|
|
|
|
|
|
|
find_interface ((void *)namespace, typelib, &data);
|
2008-02-08 16:31:03 +01:00
|
|
|
|
|
|
|
return data.iface;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
collect_namespaces (gpointer key,
|
|
|
|
gpointer value,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GList **list = data;
|
|
|
|
|
|
|
|
*list = g_list_append (*list, key);
|
|
|
|
}
|
|
|
|
|
2008-08-21 01:56:40 +02:00
|
|
|
/**
|
|
|
|
* g_irepository_get_namespaces
|
|
|
|
* @repository: A #GIRepository, may be %NULL for the default
|
|
|
|
*
|
2008-10-12 06:51:48 +02:00
|
|
|
* Return the list of currently loaded namespaces.
|
|
|
|
*
|
|
|
|
* Returns: <utf8,transfer>: List of namespaces
|
2008-08-21 01:56:40 +02:00
|
|
|
*/
|
2008-02-08 16:31:03 +01:00
|
|
|
gchar **
|
2008-10-12 06:51:48 +02:00
|
|
|
g_irepository_get_loaded_namespaces (GIRepository *repository)
|
2008-02-08 16:31:03 +01:00
|
|
|
{
|
|
|
|
GList *l, *list = NULL;
|
|
|
|
gchar **names;
|
|
|
|
gint i;
|
|
|
|
|
2008-08-30 22:31:07 +02:00
|
|
|
repository = get_repository (repository);
|
|
|
|
|
|
|
|
g_hash_table_foreach (repository->priv->typelibs, collect_namespaces, &list);
|
2008-08-30 22:31:12 +02:00
|
|
|
g_hash_table_foreach (repository->priv->lazy_typelibs, collect_namespaces, &list);
|
2008-02-08 16:31:03 +01:00
|
|
|
|
|
|
|
names = g_malloc0 (sizeof (gchar *) * (g_list_length (list) + 1));
|
|
|
|
i = 0;
|
|
|
|
for (l = list; l; l = l->next)
|
|
|
|
names[i++] = g_strdup (l->data);
|
|
|
|
g_list_free (list);
|
|
|
|
|
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
2008-10-12 06:51:48 +02:00
|
|
|
/**
|
|
|
|
* g_irepository_get_version
|
|
|
|
* @repository: A #GIRepository, may be %NULL for the default
|
|
|
|
* @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.
|
|
|
|
*
|
|
|
|
* Returns: Loaded version
|
|
|
|
*/
|
|
|
|
const gchar *
|
|
|
|
g_irepository_get_version (GIRepository *repository,
|
|
|
|
const gchar *namespace)
|
|
|
|
{
|
|
|
|
GTypelib *typelib;
|
|
|
|
Header *header;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
header = (Header *) typelib->data;
|
|
|
|
return g_typelib_get_string (typelib, header->nsversion);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_irepository_get_shared_library
|
|
|
|
* @repository: A #GIRepository, may be %NULL for the default
|
|
|
|
* @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.
|
|
|
|
*
|
|
|
|
* Note: The namespace must have already been loaded using a function
|
|
|
|
* such as #g_irepository_require before calling this function.
|
|
|
|
*
|
|
|
|
* Returns: Full path to shared library, or %NULL if none associated
|
|
|
|
*/
|
2008-02-08 16:31:03 +01:00
|
|
|
const gchar *
|
|
|
|
g_irepository_get_shared_library (GIRepository *repository,
|
2008-10-12 06:51:48 +02:00
|
|
|
const gchar *namespace)
|
2008-02-08 16:31:03 +01:00
|
|
|
{
|
2008-08-09 14:55:32 +02:00
|
|
|
GTypelib *typelib;
|
2008-02-08 16:31:03 +01:00
|
|
|
Header *header;
|
|
|
|
|
2008-10-12 06:51:48 +02:00
|
|
|
g_return_val_if_fail (namespace != NULL, NULL);
|
|
|
|
|
2008-08-30 22:31:07 +02:00
|
|
|
repository = get_repository (repository);
|
|
|
|
|
2008-10-12 06:51:48 +02:00
|
|
|
typelib = get_registered (repository, namespace, NULL);
|
|
|
|
|
|
|
|
g_return_val_if_fail (typelib != NULL, NULL);
|
|
|
|
|
2008-08-09 14:55:32 +02:00
|
|
|
header = (Header *) typelib->data;
|
2008-02-08 16:31:03 +01:00
|
|
|
if (header->shared_library)
|
2008-08-09 14:55:32 +02:00
|
|
|
return g_typelib_get_string (typelib, header->shared_library);
|
2008-02-08 16:31:03 +01:00
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-08-21 05:06:13 +02:00
|
|
|
/**
|
|
|
|
* g_irepository_get_typelib_path
|
|
|
|
* @repository: Repository, may be %NULL for the default
|
|
|
|
* @namespace: GI namespace to use, e.g. "Gtk"
|
2008-10-12 06:51:48 +02:00
|
|
|
* @version: <allow-none>: Version of namespace to use, e.g. "0.8", may be %NULL
|
2008-08-21 05:06:13 +02:00
|
|
|
*
|
|
|
|
* 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 "<builtin>".
|
|
|
|
*
|
2008-10-12 06:51:48 +02:00
|
|
|
* Returns: Filesystem path (or <builtin>) if successful, %NULL if namespace is not loaded
|
2008-08-21 05:06:13 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
const gchar *
|
|
|
|
g_irepository_get_typelib_path (GIRepository *repository,
|
|
|
|
const gchar *namespace)
|
2008-02-08 16:31:03 +01:00
|
|
|
{
|
2008-08-21 05:06:13 +02:00
|
|
|
gpointer orig_key, value;
|
|
|
|
|
2008-08-30 22:31:07 +02:00
|
|
|
repository = get_repository (repository);
|
|
|
|
|
|
|
|
if (!g_hash_table_lookup_extended (repository->priv->typelibs, namespace,
|
2008-08-21 05:06:13 +02:00
|
|
|
&orig_key, &value))
|
2008-08-30 22:31:12 +02:00
|
|
|
{
|
|
|
|
if (!g_hash_table_lookup_extended (repository->priv->lazy_typelibs, namespace,
|
|
|
|
&orig_key, &value))
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-08-21 05:06:13 +02:00
|
|
|
return ((char*)orig_key) + strlen ((char *) orig_key) + 1;
|
2008-02-08 16:31:03 +01:00
|
|
|
}
|
|
|
|
|
2008-10-12 06:51:48 +02:00
|
|
|
/* This simple search function looks for a specified namespace-version;
|
|
|
|
it's faster than the full directory listing required for latest version. */
|
|
|
|
static GMappedFile *
|
|
|
|
find_namespace_version (const gchar *namespace,
|
|
|
|
const gchar *version,
|
|
|
|
gchar **path_ret)
|
|
|
|
{
|
|
|
|
GSList *ldir;
|
|
|
|
GError *error = NULL;
|
|
|
|
GMappedFile *mfile = NULL;
|
|
|
|
char *fname;
|
|
|
|
|
|
|
|
fname = g_strdup_printf ("%s-%s.typelib", namespace, version);
|
|
|
|
|
|
|
|
for (ldir = search_path; ldir; ldir = ldir->next)
|
|
|
|
{
|
|
|
|
Header *header;
|
|
|
|
char *path = g_build_filename (ldir->data, fname, NULL);
|
|
|
|
|
|
|
|
mfile = g_mapped_file_new (path, FALSE, &error);
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
g_free (path);
|
|
|
|
g_clear_error (&error);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*path_ret = path;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
g_free (fname);
|
|
|
|
return mfile;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
parse_version (const char *version,
|
|
|
|
int *major,
|
|
|
|
int *minor)
|
|
|
|
{
|
|
|
|
const char *dot;
|
|
|
|
const char *end;
|
|
|
|
|
|
|
|
*major = strtol (version, &end, 10);
|
|
|
|
dot = strchr (version, '.');
|
|
|
|
if (dot == NULL)
|
|
|
|
{
|
|
|
|
*minor = 0;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
if (dot != end)
|
|
|
|
return FALSE;
|
|
|
|
*minor = strtol (dot+1, &end, 10);
|
|
|
|
if (end != (version + strlen (version)))
|
|
|
|
return FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
compare_version (const char *v1,
|
|
|
|
const char *v2)
|
|
|
|
{
|
2008-10-12 23:03:41 +02:00
|
|
|
gboolean success;
|
2008-10-12 06:51:48 +02:00
|
|
|
int v1_major, v1_minor;
|
|
|
|
int v2_major, v2_minor;
|
|
|
|
|
2008-10-12 23:03:41 +02:00
|
|
|
success = parse_version (v1, &v1_major, &v1_minor);
|
|
|
|
g_assert (success);
|
|
|
|
|
|
|
|
success = parse_version (v2, &v2_major, &v2_minor);
|
|
|
|
g_assert (success);
|
2008-10-12 06:51:48 +02:00
|
|
|
|
|
|
|
if (v1_major > v2_major)
|
|
|
|
return 1;
|
|
|
|
else if (v2_major > v1_major)
|
|
|
|
return -1;
|
|
|
|
else if (v1_minor > v2_minor)
|
|
|
|
return 1;
|
|
|
|
else if (v2_minor > v1_minor)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct NamespaceVersionCandidadate
|
|
|
|
{
|
|
|
|
GMappedFile *mfile;
|
|
|
|
char *path;
|
|
|
|
char *version;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
compare_candidate_reverse (struct NamespaceVersionCandidadate *c1,
|
|
|
|
struct NamespaceVersionCandidadate *c2)
|
|
|
|
{
|
|
|
|
int result = compare_version (c1->version, c2->version);
|
|
|
|
if (result > 0)
|
|
|
|
return -1;
|
|
|
|
else if (result < 0)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_candidate (struct NamespaceVersionCandidadate *candidate)
|
|
|
|
{
|
|
|
|
g_mapped_file_free (candidate->mfile);
|
|
|
|
g_free (candidate->path);
|
|
|
|
g_free (candidate->version);
|
|
|
|
g_free (candidate);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GMappedFile *
|
|
|
|
find_namespace_latest (const gchar *namespace,
|
|
|
|
gchar **version_ret,
|
|
|
|
gchar **path_ret)
|
|
|
|
{
|
|
|
|
GSList *ldir;
|
|
|
|
GError *error = NULL;
|
|
|
|
char *namespace_dash;
|
|
|
|
char *namespace_typelib;
|
|
|
|
GSList *candidates = NULL;
|
|
|
|
GMappedFile *result = NULL;
|
|
|
|
|
|
|
|
*version_ret = NULL;
|
|
|
|
*path_ret = NULL;
|
|
|
|
|
|
|
|
namespace_dash = g_strdup_printf ("%s-", namespace);
|
|
|
|
namespace_typelib = g_strdup_printf ("%s.typelib", namespace);
|
|
|
|
|
|
|
|
for (ldir = search_path; ldir; ldir = ldir->next)
|
|
|
|
{
|
|
|
|
GDir *dir;
|
|
|
|
const char *dirname;
|
|
|
|
const char *entry;
|
|
|
|
|
|
|
|
dirname = (const char*)ldir->data;
|
|
|
|
dir = g_dir_open (dirname, 0, NULL);
|
|
|
|
if (dir == NULL)
|
|
|
|
continue;
|
|
|
|
while ((entry = g_dir_read_name (dir)) != NULL)
|
|
|
|
{
|
|
|
|
GMappedFile *mfile;
|
|
|
|
char *path, *version;
|
|
|
|
struct NamespaceVersionCandidadate *candidate;
|
|
|
|
|
|
|
|
if (!g_str_has_suffix (entry, ".typelib"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (g_str_has_prefix (entry, namespace_dash))
|
|
|
|
{
|
|
|
|
const char *last_dash;
|
|
|
|
const char *name_end;
|
|
|
|
int major, minor;
|
|
|
|
|
|
|
|
name_end = strrchr (entry, '.');
|
|
|
|
last_dash = strrchr (entry, '-');
|
|
|
|
version = g_strndup (last_dash+1, name_end-(last_dash+1));
|
|
|
|
if (!parse_version (version, &major, &minor))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
|
|
|
path = g_build_filename (dirname, entry, NULL);
|
|
|
|
mfile = g_mapped_file_new (path, FALSE, &error);
|
|
|
|
if (mfile == NULL)
|
|
|
|
{
|
|
|
|
g_free (path);
|
|
|
|
g_free (version);
|
|
|
|
g_clear_error (&error);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
candidate = g_new0 (struct NamespaceVersionCandidadate, 1);
|
|
|
|
candidate->mfile = mfile;
|
|
|
|
candidate->path = path;
|
|
|
|
candidate->version = version;
|
|
|
|
candidates = g_slist_prepend (candidates, candidate);
|
|
|
|
}
|
|
|
|
g_dir_close (dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (candidates != NULL)
|
|
|
|
{
|
|
|
|
struct NamespaceVersionCandidadate *elected;
|
|
|
|
candidates = g_slist_sort (candidates, (GCompareFunc) compare_candidate_reverse);
|
|
|
|
|
|
|
|
elected = (struct NamespaceVersionCandidadate *) candidates->data;
|
|
|
|
/* Remove the elected one so we don't try to free it */
|
|
|
|
candidates = g_slist_delete_link (candidates, candidates);
|
|
|
|
|
|
|
|
result = elected->mfile;
|
|
|
|
*path_ret = elected->path;
|
|
|
|
*version_ret = elected->version;
|
|
|
|
g_slist_foreach (candidates, (GFunc) free_candidate, NULL);
|
|
|
|
g_slist_free (candidates);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (namespace_dash);
|
|
|
|
g_free (namespace_typelib);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-08-21 01:56:40 +02:00
|
|
|
/**
|
|
|
|
* g_irepository_require
|
2008-10-12 06:51:48 +02:00
|
|
|
* @repository: <allow-none>: Repository, may be %NULL for the default
|
2008-08-21 01:56:40 +02:00
|
|
|
* @namespace: GI namespace to use, e.g. "Gtk"
|
2008-10-12 06:51:48 +02:00
|
|
|
* @version: <allow-none>: Version of namespace, may be %NULL for latest
|
2008-08-30 22:31:12 +02:00
|
|
|
* @flags: Set of %GIRepositoryLoadFlags, may be %0
|
2008-08-21 01:56:40 +02:00
|
|
|
* @error: a #GError.
|
|
|
|
*
|
2008-10-12 06:51:48 +02:00
|
|
|
* 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.
|
2008-08-21 01:56:40 +02:00
|
|
|
*
|
2008-08-30 22:31:07 +02:00
|
|
|
* Returns: %TRUE if successful, %NULL otherwise
|
2008-08-21 01:56:40 +02:00
|
|
|
*/
|
2008-08-30 22:31:07 +02:00
|
|
|
gboolean
|
2008-08-21 01:56:40 +02:00
|
|
|
g_irepository_require (GIRepository *repository,
|
|
|
|
const gchar *namespace,
|
2008-10-12 06:51:48 +02:00
|
|
|
const gchar *version,
|
2008-08-30 22:31:12 +02:00
|
|
|
GIRepositoryLoadFlags flags,
|
2008-08-21 01:56:40 +02:00
|
|
|
GError **error)
|
2008-02-08 16:31:03 +01:00
|
|
|
{
|
|
|
|
const char *dir;
|
|
|
|
GMappedFile *mfile;
|
2008-10-12 06:51:48 +02:00
|
|
|
gboolean ret = FALSE;
|
2008-02-08 16:31:03 +01:00
|
|
|
GError *error1 = NULL;
|
2008-10-12 06:51:48 +02:00
|
|
|
Header *header;
|
2008-08-09 14:55:32 +02:00
|
|
|
GTypelib *typelib = NULL;
|
2008-10-12 06:51:48 +02:00
|
|
|
const gchar *typelib_namespace, *typelib_version, *shlib_fname;
|
2008-02-08 16:31:03 +01:00
|
|
|
GModule *module;
|
|
|
|
guint32 shlib;
|
2008-10-12 06:51:48 +02:00
|
|
|
gboolean allow_lazy = (flags & G_IREPOSITORY_LOAD_FLAG_LAZY) > 0;
|
2008-08-30 22:31:12 +02:00
|
|
|
gboolean is_lazy;
|
2008-10-12 06:51:48 +02:00
|
|
|
char *version_conflict = NULL;
|
|
|
|
char *path = NULL;
|
|
|
|
char *tmp_version = NULL;
|
|
|
|
|
|
|
|
g_return_val_if_fail (namespace != NULL, FALSE);
|
2008-02-08 16:31:03 +01:00
|
|
|
|
2008-08-30 22:31:07 +02:00
|
|
|
repository = get_repository (repository);
|
2008-02-08 16:31:03 +01:00
|
|
|
|
2008-10-12 06:51:48 +02:00
|
|
|
if (get_registered_status (repository, namespace, version, allow_lazy,
|
|
|
|
&is_lazy, &version_conflict))
|
2008-08-30 22:31:07 +02:00
|
|
|
return TRUE;
|
2008-02-08 16:31:03 +01:00
|
|
|
|
2008-10-12 06:51:48 +02:00
|
|
|
if (version_conflict != NULL)
|
2008-08-21 08:47:49 +02:00
|
|
|
{
|
2008-10-12 06:51:48 +02:00
|
|
|
g_set_error (error, G_IREPOSITORY_ERROR,
|
|
|
|
G_IREPOSITORY_ERROR_NAMESPACE_VERSION_CONFLICT,
|
|
|
|
"Requiring namespace '%s' version '%s', but '%s' is already loaded",
|
|
|
|
namespace, version, version_conflict);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2008-08-21 08:47:49 +02:00
|
|
|
|
2008-10-12 06:51:48 +02:00
|
|
|
if (version != NULL)
|
|
|
|
{
|
|
|
|
mfile = find_namespace_version (namespace, version, &path);
|
|
|
|
tmp_version = g_strdup (version);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mfile = find_namespace_latest (namespace, &tmp_version, &path);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mfile == NULL)
|
|
|
|
{
|
|
|
|
const char *error_fmt;
|
|
|
|
if (version != NULL)
|
|
|
|
g_set_error (error, G_IREPOSITORY_ERROR,
|
|
|
|
G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
|
|
|
|
"Typelib file %s for namespace '%s', version '%s' not found",
|
|
|
|
namespace, version);
|
|
|
|
else
|
|
|
|
g_set_error (error, G_IREPOSITORY_ERROR,
|
|
|
|
G_IREPOSITORY_ERROR_TYPELIB_NOT_FOUND,
|
|
|
|
"Typelib file for namespace '%s' (any version) not found",
|
|
|
|
namespace);
|
|
|
|
goto out;
|
|
|
|
}
|
2008-08-21 08:47:49 +02:00
|
|
|
|
2008-10-12 06:51:48 +02:00
|
|
|
typelib = g_typelib_new_from_mapped_file (mfile);
|
|
|
|
header = (Header *) typelib->data;
|
|
|
|
typelib_namespace = g_typelib_get_string (typelib, header->namespace);
|
|
|
|
typelib_version = g_typelib_get_string (typelib, header->nsversion);
|
|
|
|
|
|
|
|
if (strcmp (typelib_namespace, namespace) != 0)
|
2008-08-21 08:47:49 +02:00
|
|
|
{
|
2008-02-08 16:31:03 +01:00
|
|
|
g_set_error (error, G_IREPOSITORY_ERROR,
|
2008-10-12 06:51:48 +02:00
|
|
|
G_IREPOSITORY_ERROR_NAMESPACE_MISMATCH,
|
|
|
|
"Typelib file %s for namespace '%s' contains "
|
|
|
|
"namespace '%s' which doesn't match the file name",
|
|
|
|
path, namespace, typelib_namespace);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (version != NULL && strcmp (typelib_version, version) != 0)
|
|
|
|
{
|
|
|
|
g_set_error (error, G_IREPOSITORY_ERROR,
|
|
|
|
G_IREPOSITORY_ERROR_NAMESPACE_MISMATCH,
|
|
|
|
"Typelib file %s for namespace '%s' contains "
|
|
|
|
"version '%s' which doesn't match the expected version '%s'",
|
|
|
|
path, namespace, typelib_version, version);
|
|
|
|
goto out;
|
2008-02-08 16:31:03 +01:00
|
|
|
}
|
2008-08-21 08:47:49 +02:00
|
|
|
|
2008-10-12 06:51:48 +02:00
|
|
|
if (!register_internal (repository, path, allow_lazy,
|
2008-08-30 22:31:12 +02:00
|
|
|
typelib, error))
|
2008-08-30 22:31:07 +02:00
|
|
|
{
|
|
|
|
g_typelib_free (typelib);
|
2008-10-12 06:51:48 +02:00
|
|
|
goto out;
|
2008-08-30 22:31:07 +02:00
|
|
|
}
|
2008-10-12 06:51:48 +02:00
|
|
|
ret = TRUE;
|
|
|
|
out:
|
|
|
|
g_free (tmp_version);
|
|
|
|
g_free (path);
|
|
|
|
return ret;
|
2008-02-08 16:31:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GQuark
|
|
|
|
g_irepository_error_quark (void)
|
|
|
|
{
|
|
|
|
static GQuark quark = 0;
|
|
|
|
if (quark == 0)
|
|
|
|
quark = g_quark_from_static_string ("g-irepository-error-quark");
|
|
|
|
return quark;
|
|
|
|
}
|
2008-08-18 10:52:47 +02:00
|
|
|
|
|
|
|
const gchar*
|
|
|
|
g_type_tag_to_string (GITypeTag type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case GI_TYPE_TAG_VOID:
|
|
|
|
return "void";
|
|
|
|
case GI_TYPE_TAG_BOOLEAN:
|
|
|
|
return "boolean";
|
|
|
|
case GI_TYPE_TAG_INT8:
|
|
|
|
return "int8";
|
|
|
|
case GI_TYPE_TAG_UINT8:
|
|
|
|
return "uint8";
|
|
|
|
case GI_TYPE_TAG_INT16:
|
|
|
|
return "int16";
|
|
|
|
case GI_TYPE_TAG_UINT16:
|
|
|
|
return "uint16";
|
|
|
|
case GI_TYPE_TAG_INT32:
|
|
|
|
return "int32";
|
|
|
|
case GI_TYPE_TAG_UINT32:
|
|
|
|
return "uint32";
|
|
|
|
case GI_TYPE_TAG_INT64:
|
|
|
|
return "int64";
|
|
|
|
case GI_TYPE_TAG_UINT64:
|
|
|
|
return "uint64";
|
|
|
|
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";
|
|
|
|
case GI_TYPE_TAG_FLOAT:
|
|
|
|
return "float";
|
|
|
|
case GI_TYPE_TAG_DOUBLE:
|
|
|
|
return "double";
|
2008-09-23 20:20:25 +02:00
|
|
|
case GI_TYPE_TAG_TIME_T:
|
|
|
|
return "time_t";
|
2008-10-02 15:25:46 +02:00
|
|
|
case GI_TYPE_TAG_GTYPE:
|
|
|
|
return "GType";
|
2008-08-18 10:52:47 +02:00
|
|
|
case GI_TYPE_TAG_UTF8:
|
|
|
|
return "utf8";
|
|
|
|
case GI_TYPE_TAG_FILENAME:
|
|
|
|
return "filename";
|
|
|
|
case GI_TYPE_TAG_ARRAY:
|
|
|
|
return "array";
|
|
|
|
case GI_TYPE_TAG_INTERFACE:
|
|
|
|
return "interface";
|
|
|
|
case GI_TYPE_TAG_GLIST:
|
|
|
|
return "glist";
|
|
|
|
case GI_TYPE_TAG_GSLIST:
|
|
|
|
return "gslist";
|
|
|
|
case GI_TYPE_TAG_GHASH:
|
|
|
|
return "ghash";
|
|
|
|
case GI_TYPE_TAG_ERROR:
|
|
|
|
return "error";
|
|
|
|
default:
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
}
|