xed/pluma/pluma-object-module.c

344 lines
8.8 KiB
C
Raw Normal View History

2011-11-07 13:46:58 -06:00
/*
2011-11-07 16:52:18 -06:00
* pluma-object-module.c
* This file is part of pluma
2011-11-07 13:46:58 -06:00
*
* Copyright (C) 2005 - Paolo Maggi
* Copyright (C) 2008 - Jesse van den Kieboom
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
2012-11-18 19:54:49 -06:00
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
2011-11-07 13:46:58 -06:00
*/
/* This is a modified version of ephy-module.c from Epiphany source code.
* Here the original copyright assignment:
*
* Copyright (C) 2003 Marco Pesenti Gritti
* Copyright (C) 2003, 2004 Christian Persch
*
*/
/*
2011-11-07 16:52:18 -06:00
* Modified by the pluma Team, 2005. See the AUTHORS file for a
* list of people on the pluma Team.
2011-11-07 13:46:58 -06:00
* See the ChangeLog files for a list of changes.
*
2011-11-07 16:52:18 -06:00
* $Id: pluma-module.c 6314 2008-06-05 12:57:53Z pborelli $
2011-11-07 13:46:58 -06:00
*/
#include "config.h"
2011-11-07 16:52:18 -06:00
#include "pluma-object-module.h"
#include "pluma-debug.h"
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
typedef GType (*PlumaObjectModuleRegisterFunc) (GTypeModule *);
2011-11-07 13:46:58 -06:00
enum {
PROP_0,
PROP_MODULE_NAME,
PROP_PATH,
PROP_TYPE_REGISTRATION,
PROP_RESIDENT
};
2011-11-07 16:52:18 -06:00
struct _PlumaObjectModulePrivate
2011-11-07 13:46:58 -06:00
{
GModule *library;
GType type;
gchar *path;
gchar *module_name;
gchar *type_registration;
gboolean resident;
};
2011-11-07 16:52:18 -06:00
G_DEFINE_TYPE (PlumaObjectModule, pluma_object_module, G_TYPE_TYPE_MODULE);
2011-11-07 13:46:58 -06:00
static gboolean
2011-11-07 16:52:18 -06:00
pluma_object_module_load (GTypeModule *gmodule)
2011-11-07 13:46:58 -06:00
{
2011-11-07 16:52:18 -06:00
PlumaObjectModule *module = PLUMA_OBJECT_MODULE (gmodule);
PlumaObjectModuleRegisterFunc register_func;
2011-11-07 13:46:58 -06:00
gchar *path;
2011-11-07 16:52:18 -06:00
pluma_debug_message (DEBUG_PLUGINS, "Loading %s module from %s",
2011-11-07 13:46:58 -06:00
module->priv->module_name, module->priv->path);
path = g_module_build_path (module->priv->path, module->priv->module_name);
g_return_val_if_fail (path != NULL, FALSE);
2011-11-07 16:52:18 -06:00
pluma_debug_message (DEBUG_PLUGINS, "Module filename: %s", path);
2011-11-07 13:46:58 -06:00
module->priv->library = g_module_open (path,
G_MODULE_BIND_LAZY);
g_free (path);
if (module->priv->library == NULL)
{
g_warning ("%s: %s", module->priv->module_name, g_module_error());
return FALSE;
}
/* extract symbols from the lib */
if (!g_module_symbol (module->priv->library, module->priv->type_registration,
(void *) &register_func))
{
g_warning ("%s: %s", module->priv->module_name, g_module_error());
g_module_close (module->priv->library);
return FALSE;
}
/* symbol can still be NULL even though g_module_symbol
* returned TRUE */
if (register_func == NULL)
{
g_warning ("Symbol '%s' should not be NULL", module->priv->type_registration);
g_module_close (module->priv->library);
return FALSE;
}
module->priv->type = register_func (gmodule);
if (module->priv->type == 0)
{
g_warning ("Invalid object contained by module %s", module->priv->module_name);
return FALSE;
}
if (module->priv->resident)
{
g_module_make_resident (module->priv->library);
}
return TRUE;
}
static void
2011-11-07 16:52:18 -06:00
pluma_object_module_unload (GTypeModule *gmodule)
2011-11-07 13:46:58 -06:00
{
2011-11-07 16:52:18 -06:00
PlumaObjectModule *module = PLUMA_OBJECT_MODULE (gmodule);
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
pluma_debug_message (DEBUG_PLUGINS, "Unloading %s", module->priv->path);
2011-11-07 13:46:58 -06:00
g_module_close (module->priv->library);
module->priv->library = NULL;
module->priv->type = 0;
}
static void
2011-11-07 16:52:18 -06:00
pluma_object_module_init (PlumaObjectModule *module)
2011-11-07 13:46:58 -06:00
{
2011-11-07 16:52:18 -06:00
pluma_debug_message (DEBUG_PLUGINS, "PlumaObjectModule %p initialising", module);
2011-11-07 13:46:58 -06:00
module->priv = G_TYPE_INSTANCE_GET_PRIVATE (module,
2011-11-07 16:52:18 -06:00
PLUMA_TYPE_OBJECT_MODULE,
PlumaObjectModulePrivate);
2011-11-07 13:46:58 -06:00
}
static void
2011-11-07 16:52:18 -06:00
pluma_object_module_finalize (GObject *object)
2011-11-07 13:46:58 -06:00
{
2011-11-07 16:52:18 -06:00
PlumaObjectModule *module = PLUMA_OBJECT_MODULE (object);
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
pluma_debug_message (DEBUG_PLUGINS, "PlumaObjectModule %p finalising", module);
2011-11-07 13:46:58 -06:00
g_free (module->priv->path);
g_free (module->priv->module_name);
g_free (module->priv->type_registration);
2011-11-07 16:52:18 -06:00
G_OBJECT_CLASS (pluma_object_module_parent_class)->finalize (object);
2011-11-07 13:46:58 -06:00
}
static void
2011-11-07 16:52:18 -06:00
pluma_object_module_get_property (GObject *object,
2011-11-07 13:46:58 -06:00
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
2011-11-07 16:52:18 -06:00
PlumaObjectModule *module = PLUMA_OBJECT_MODULE (object);
2011-11-07 13:46:58 -06:00
switch (prop_id)
{
case PROP_MODULE_NAME:
g_value_set_string (value, module->priv->module_name);
break;
case PROP_PATH:
g_value_set_string (value, module->priv->path);
break;
case PROP_TYPE_REGISTRATION:
g_value_set_string (value, module->priv->type_registration);
break;
case PROP_RESIDENT:
g_value_set_boolean (value, module->priv->resident);
break;
default:
g_return_if_reached ();
}
}
static void
2011-11-07 16:52:18 -06:00
pluma_object_module_set_property (GObject *object,
2011-11-07 13:46:58 -06:00
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
2011-11-07 16:52:18 -06:00
PlumaObjectModule *module = PLUMA_OBJECT_MODULE (object);
2011-11-07 13:46:58 -06:00
switch (prop_id)
{
case PROP_MODULE_NAME:
module->priv->module_name = g_value_dup_string (value);
g_type_module_set_name (G_TYPE_MODULE (object),
module->priv->module_name);
break;
case PROP_PATH:
module->priv->path = g_value_dup_string (value);
break;
case PROP_TYPE_REGISTRATION:
module->priv->type_registration = g_value_dup_string (value);
break;
case PROP_RESIDENT:
module->priv->resident = g_value_get_boolean (value);
break;
default:
g_return_if_reached ();
}
}
static void
2011-11-07 16:52:18 -06:00
pluma_object_module_class_init (PlumaObjectModuleClass *klass)
2011-11-07 13:46:58 -06:00
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (klass);
2011-11-07 16:52:18 -06:00
object_class->set_property = pluma_object_module_set_property;
object_class->get_property = pluma_object_module_get_property;
object_class->finalize = pluma_object_module_finalize;
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
module_class->load = pluma_object_module_load;
module_class->unload = pluma_object_module_unload;
2011-11-07 13:46:58 -06:00
g_object_class_install_property (object_class,
PROP_MODULE_NAME,
g_param_spec_string ("module-name",
"Module Name",
"The module to load for this object",
NULL,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class,
PROP_PATH,
g_param_spec_string ("path",
"Path",
"The path to use when loading this module",
NULL,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class,
PROP_TYPE_REGISTRATION,
g_param_spec_string ("type-registration",
"Type Registration",
"The name of the type registration function",
NULL,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class,
PROP_RESIDENT,
g_param_spec_boolean ("resident",
"Resident",
"Whether the module is resident",
FALSE,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY));
2011-11-07 16:52:18 -06:00
g_type_class_add_private (klass, sizeof (PlumaObjectModulePrivate));
2011-11-07 13:46:58 -06:00
}
2011-11-07 16:52:18 -06:00
PlumaObjectModule *
pluma_object_module_new (const gchar *module_name,
2011-11-07 13:46:58 -06:00
const gchar *path,
const gchar *type_registration,
gboolean resident)
{
2011-11-07 16:52:18 -06:00
return (PlumaObjectModule *)g_object_new (PLUMA_TYPE_OBJECT_MODULE,
2011-11-07 13:46:58 -06:00
"module-name",
module_name,
"path",
path,
"type-registration",
type_registration,
"resident",
resident,
NULL);
}
GObject *
2011-11-07 16:52:18 -06:00
pluma_object_module_new_object (PlumaObjectModule *module,
2011-11-07 13:46:58 -06:00
const gchar *first_property_name,
...)
{
va_list var_args;
GObject *result;
g_return_val_if_fail (module->priv->type != 0, NULL);
2011-11-07 16:52:18 -06:00
pluma_debug_message (DEBUG_PLUGINS, "Creating object of type %s",
2011-11-07 13:46:58 -06:00
g_type_name (module->priv->type));
va_start (var_args, first_property_name);
result = g_object_new_valist (module->priv->type, first_property_name, var_args);
va_end (var_args);
return result;
}
const gchar *
2011-11-07 16:52:18 -06:00
pluma_object_module_get_path (PlumaObjectModule *module)
2011-11-07 13:46:58 -06:00
{
2011-11-07 16:52:18 -06:00
g_return_val_if_fail (PLUMA_IS_OBJECT_MODULE (module), NULL);
2011-11-07 13:46:58 -06:00
return module->priv->path;
}
const gchar *
2011-11-07 16:52:18 -06:00
pluma_object_module_get_module_name (PlumaObjectModule *module)
2011-11-07 13:46:58 -06:00
{
2011-11-07 16:52:18 -06:00
g_return_val_if_fail (PLUMA_IS_OBJECT_MODULE (module), NULL);
2011-11-07 13:46:58 -06:00
return module->priv->module_name;
}
const gchar *
2011-11-07 16:52:18 -06:00
pluma_object_module_get_type_registration (PlumaObjectModule *module)
2011-11-07 13:46:58 -06:00
{
2011-11-07 16:52:18 -06:00
g_return_val_if_fail (PLUMA_IS_OBJECT_MODULE (module), NULL);
2011-11-07 13:46:58 -06:00
return module->priv->type_registration;
}
GType
2011-11-07 16:52:18 -06:00
pluma_object_module_get_object_type (PlumaObjectModule *module)
2011-11-07 13:46:58 -06:00
{
2011-11-07 16:52:18 -06:00
g_return_val_if_fail (PLUMA_IS_OBJECT_MODULE (module), 0);
2011-11-07 13:46:58 -06:00
return module->priv->type;
}