xed/xedit/xedit-plugin.c

335 lines
8.2 KiB
C
Raw Normal View History

2011-11-07 13:46:58 -06:00
/*
2016-01-25 08:13:49 -06:00
* xedit-plugin.h
* This file is part of xedit
2011-11-07 13:46:58 -06:00
*
2011-11-07 18:10:16 -06:00
* Copyright (C) 2002-2005 Paolo Maggi
2011-11-07 13:46:58 -06:00
*
* 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
*/
2011-11-07 18:10:16 -06:00
2011-11-07 13:46:58 -06:00
/*
2016-01-25 08:13:49 -06:00
* Modified by the xedit Team, 2002-2005. See the AUTHORS file for a
* list of people on the xedit Team.
2011-11-07 18:10:16 -06:00
* See the ChangeLog files for a list of changes.
2011-11-07 13:46:58 -06:00
*
* $Id$
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
2016-01-25 08:13:49 -06:00
#include "xedit-plugin.h"
#include "xedit-dirs.h"
2011-11-07 13:46:58 -06:00
/* properties */
enum {
PROP_0,
PROP_INSTALL_DIR,
PROP_DATA_DIR_NAME,
PROP_DATA_DIR
};
2016-01-25 08:13:49 -06:00
typedef struct _XeditPluginPrivate XeditPluginPrivate;
2011-11-07 13:46:58 -06:00
2016-01-25 08:13:49 -06:00
struct _XeditPluginPrivate
2011-11-07 13:46:58 -06:00
{
gchar *install_dir;
gchar *data_dir_name;
};
2016-01-25 08:13:49 -06:00
#define XEDIT_PLUGIN_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), XEDIT_TYPE_PLUGIN, XeditPluginPrivate))
2011-11-07 13:46:58 -06:00
2016-01-25 08:13:49 -06:00
G_DEFINE_TYPE(XeditPlugin, xedit_plugin, G_TYPE_OBJECT)
2011-11-07 13:46:58 -06:00
static void
2016-01-25 08:13:49 -06:00
dummy (XeditPlugin *plugin, XeditWindow *window)
2011-11-07 13:46:58 -06:00
{
/* Empty */
}
static GtkWidget *
2016-01-25 08:13:49 -06:00
create_configure_dialog (XeditPlugin *plugin)
2011-11-07 13:46:58 -06:00
{
return NULL;
}
static gboolean
2016-01-25 08:13:49 -06:00
is_configurable (XeditPlugin *plugin)
2011-11-07 13:46:58 -06:00
{
2016-01-25 08:13:49 -06:00
return (XEDIT_PLUGIN_GET_CLASS (plugin)->create_configure_dialog !=
2011-11-07 13:46:58 -06:00
create_configure_dialog);
}
static void
2016-01-25 08:13:49 -06:00
xedit_plugin_get_property (GObject *object,
2011-11-07 13:46:58 -06:00
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
switch (prop_id)
{
case PROP_INSTALL_DIR:
2016-01-25 08:13:49 -06:00
g_value_take_string (value, xedit_plugin_get_install_dir (XEDIT_PLUGIN (object)));
2011-11-07 13:46:58 -06:00
break;
case PROP_DATA_DIR:
2016-01-25 08:13:49 -06:00
g_value_take_string (value, xedit_plugin_get_data_dir (XEDIT_PLUGIN (object)));
2011-11-07 13:46:58 -06:00
break;
default:
g_return_if_reached ();
}
}
static void
2016-01-25 08:13:49 -06:00
xedit_plugin_set_property (GObject *object,
2011-11-07 13:46:58 -06:00
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
2016-01-25 08:13:49 -06:00
XeditPluginPrivate *priv = XEDIT_PLUGIN_GET_PRIVATE (object);
2011-11-07 13:46:58 -06:00
switch (prop_id)
{
case PROP_INSTALL_DIR:
priv->install_dir = g_value_dup_string (value);
break;
case PROP_DATA_DIR_NAME:
priv->data_dir_name = g_value_dup_string (value);
break;
default:
g_return_if_reached ();
}
}
static void
2016-01-25 08:13:49 -06:00
xedit_plugin_finalize (GObject *object)
2011-11-07 13:46:58 -06:00
{
2016-01-25 08:13:49 -06:00
XeditPluginPrivate *priv = XEDIT_PLUGIN_GET_PRIVATE (object);
2011-11-07 13:46:58 -06:00
g_free (priv->install_dir);
g_free (priv->data_dir_name);
2016-01-25 08:13:49 -06:00
G_OBJECT_CLASS (xedit_plugin_parent_class)->finalize (object);
2011-11-07 13:46:58 -06:00
}
2011-11-07 18:10:16 -06:00
static void
2016-01-25 08:13:49 -06:00
xedit_plugin_class_init (XeditPluginClass *klass)
2011-11-07 13:46:58 -06:00
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
klass->activate = dummy;
klass->deactivate = dummy;
klass->update_ui = dummy;
2011-11-07 18:10:16 -06:00
2011-11-07 13:46:58 -06:00
klass->create_configure_dialog = create_configure_dialog;
klass->is_configurable = is_configurable;
2016-01-25 08:13:49 -06:00
object_class->get_property = xedit_plugin_get_property;
object_class->set_property = xedit_plugin_set_property;
object_class->finalize = xedit_plugin_finalize;
2011-11-07 13:46:58 -06:00
g_object_class_install_property (object_class,
PROP_INSTALL_DIR,
g_param_spec_string ("install-dir",
"Install Directory",
"The directory where the plugin is installed",
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
/* the basename of the data dir is set at construction time by the plugin loader
* while the full path is constructed on the fly to take into account relocability
* that's why we have a writeonly prop and a readonly prop */
g_object_class_install_property (object_class,
PROP_DATA_DIR_NAME,
g_param_spec_string ("data-dir-name",
"Basename of the data directory",
"The basename of the directory where the plugin should look for its data files",
NULL,
G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class,
PROP_DATA_DIR,
g_param_spec_string ("data-dir",
"Data Directory",
"The full path of the directory where the plugin should look for its data files",
NULL,
G_PARAM_READABLE));
2016-01-25 08:13:49 -06:00
g_type_class_add_private (klass, sizeof (XeditPluginPrivate));
2011-11-07 13:46:58 -06:00
}
static void
2016-01-25 08:13:49 -06:00
xedit_plugin_init (XeditPlugin *plugin)
2011-11-07 13:46:58 -06:00
{
/* Empty */
}
/**
2016-01-25 08:13:49 -06:00
* xedit_plugin_get_install_dir:
* @plugin: a #XeditPlugin
2011-11-07 13:46:58 -06:00
*
* Get the path of the directory where the plugin is installed.
*
* Return value: a newly allocated string with the path of the
* directory where the plugin is installed
*/
gchar *
2016-01-25 08:13:49 -06:00
xedit_plugin_get_install_dir (XeditPlugin *plugin)
2011-11-07 13:46:58 -06:00
{
2016-01-25 08:13:49 -06:00
g_return_val_if_fail (XEDIT_IS_PLUGIN (plugin), NULL);
2011-11-07 13:46:58 -06:00
2016-01-25 08:13:49 -06:00
return g_strdup (XEDIT_PLUGIN_GET_PRIVATE (plugin)->install_dir);
2011-11-07 13:46:58 -06:00
}
/**
2016-01-25 08:13:49 -06:00
* xedit_plugin_get_data_dir:
* @plugin: a #XeditPlugin
2011-11-07 13:46:58 -06:00
*
* Get the path of the directory where the plugin should look for
* its data files.
*
* Return value: a newly allocated string with the path of the
* directory where the plugin should look for its data files
*/
gchar *
2016-01-25 08:13:49 -06:00
xedit_plugin_get_data_dir (XeditPlugin *plugin)
2011-11-07 13:46:58 -06:00
{
2016-01-25 08:13:49 -06:00
XeditPluginPrivate *priv;
gchar *xedit_lib_dir;
2011-11-07 13:46:58 -06:00
gchar *data_dir;
2016-01-25 08:13:49 -06:00
g_return_val_if_fail (XEDIT_IS_PLUGIN (plugin), NULL);
2011-11-07 13:46:58 -06:00
2016-01-25 08:13:49 -06:00
priv = XEDIT_PLUGIN_GET_PRIVATE (plugin);
2011-11-07 13:46:58 -06:00
/* If it's a "user" plugin the data dir is
* install_dir/data_dir_name if instead it's a
2016-01-25 08:13:49 -06:00
* "system" plugin the data dir is under xedit_data_dir,
* so it's under $prefix/share/xedit/plugins/data_dir_name
2011-11-07 13:46:58 -06:00
* where data_dir_name usually it's the name of the plugin
*/
2016-01-25 08:13:49 -06:00
xedit_lib_dir = xedit_dirs_get_xedit_lib_dir ();
2011-11-07 13:46:58 -06:00
/* CHECK: is checking the prefix enough or should we be more
* careful about normalizing paths etc? */
2016-01-25 08:13:49 -06:00
if (g_str_has_prefix (priv->install_dir, xedit_lib_dir))
2011-11-07 13:46:58 -06:00
{
2016-01-25 08:13:49 -06:00
gchar *xedit_data_dir;
2011-11-07 13:46:58 -06:00
2016-01-25 08:13:49 -06:00
xedit_data_dir = xedit_dirs_get_xedit_data_dir ();
2011-11-07 13:46:58 -06:00
2016-01-25 08:13:49 -06:00
data_dir = g_build_filename (xedit_data_dir,
2011-11-07 13:46:58 -06:00
"plugins",
priv->data_dir_name,
NULL);
2016-01-25 08:13:49 -06:00
g_free (xedit_data_dir);
2011-11-07 13:46:58 -06:00
}
else
{
data_dir = g_build_filename (priv->install_dir,
priv->data_dir_name,
NULL);
}
2016-01-25 08:13:49 -06:00
g_free (xedit_lib_dir);
2011-11-07 13:46:58 -06:00
return data_dir;
}
/**
2016-01-25 08:13:49 -06:00
* xedit_plugin_activate:
* @plugin: a #XeditPlugin
* @window: a #XeditWindow
2011-11-07 18:10:16 -06:00
*
2011-11-07 13:46:58 -06:00
* Activates the plugin.
*/
void
2016-01-25 08:13:49 -06:00
xedit_plugin_activate (XeditPlugin *plugin,
XeditWindow *window)
2011-11-07 13:46:58 -06:00
{
2016-01-25 08:13:49 -06:00
g_return_if_fail (XEDIT_IS_PLUGIN (plugin));
g_return_if_fail (XEDIT_IS_WINDOW (window));
2011-11-07 18:10:16 -06:00
2016-01-25 08:13:49 -06:00
XEDIT_PLUGIN_GET_CLASS (plugin)->activate (plugin, window);
2011-11-07 13:46:58 -06:00
}
/**
2016-01-25 08:13:49 -06:00
* xedit_plugin_deactivate:
* @plugin: a #XeditPlugin
* @window: a #XeditWindow
2011-11-07 18:10:16 -06:00
*
2011-11-07 13:46:58 -06:00
* Deactivates the plugin.
*/
void
2016-01-25 08:13:49 -06:00
xedit_plugin_deactivate (XeditPlugin *plugin,
XeditWindow *window)
2011-11-07 13:46:58 -06:00
{
2016-01-25 08:13:49 -06:00
g_return_if_fail (XEDIT_IS_PLUGIN (plugin));
g_return_if_fail (XEDIT_IS_WINDOW (window));
2011-11-07 13:46:58 -06:00
2016-01-25 08:13:49 -06:00
XEDIT_PLUGIN_GET_CLASS (plugin)->deactivate (plugin, window);
2011-11-07 13:46:58 -06:00
}
/**
2016-01-25 08:13:49 -06:00
* xedit_plugin_update_ui:
* @plugin: a #XeditPlugin
* @window: a #XeditWindow
2011-11-07 13:46:58 -06:00
*
* Triggers an update of the user interface to take into account state changes
* caused by the plugin.
2011-11-07 18:10:16 -06:00
*/
2011-11-07 13:46:58 -06:00
void
2016-01-25 08:13:49 -06:00
xedit_plugin_update_ui (XeditPlugin *plugin,
XeditWindow *window)
2011-11-07 13:46:58 -06:00
{
2016-01-25 08:13:49 -06:00
g_return_if_fail (XEDIT_IS_PLUGIN (plugin));
g_return_if_fail (XEDIT_IS_WINDOW (window));
2011-11-07 13:46:58 -06:00
2016-01-25 08:13:49 -06:00
XEDIT_PLUGIN_GET_CLASS (plugin)->update_ui (plugin, window);
2011-11-07 13:46:58 -06:00
}
/**
2016-01-25 08:13:49 -06:00
* xedit_plugin_is_configurable:
* @plugin: a #XeditPlugin
2011-11-07 13:46:58 -06:00
*
* Whether the plugin is configurable.
*
* Returns: TRUE if the plugin is configurable:
*/
gboolean
2016-01-25 08:13:49 -06:00
xedit_plugin_is_configurable (XeditPlugin *plugin)
2011-11-07 13:46:58 -06:00
{
2016-01-25 08:13:49 -06:00
g_return_val_if_fail (XEDIT_IS_PLUGIN (plugin), FALSE);
2011-11-07 13:46:58 -06:00
2016-01-25 08:13:49 -06:00
return XEDIT_PLUGIN_GET_CLASS (plugin)->is_configurable (plugin);
2011-11-07 13:46:58 -06:00
}
/**
2016-01-25 08:13:49 -06:00
* xedit_plugin_create_configure_dialog:
* @plugin: a #XeditPlugin
2011-11-07 13:46:58 -06:00
*
* Creates the configure dialog widget for the plugin.
*
* Returns: the configure dialog widget for the plugin.
*/
GtkWidget *
2016-01-25 08:13:49 -06:00
xedit_plugin_create_configure_dialog (XeditPlugin *plugin)
2011-11-07 13:46:58 -06:00
{
2016-01-25 08:13:49 -06:00
g_return_val_if_fail (XEDIT_IS_PLUGIN (plugin), NULL);
2011-11-07 18:10:16 -06:00
2016-01-25 08:13:49 -06:00
return XEDIT_PLUGIN_GET_CLASS (plugin)->create_configure_dialog (plugin);
2011-11-07 13:46:58 -06:00
}