gedit r6232 - branches/jessevdk-plugins/gedit
- From: jessevdk svn gnome org
- To: svn-commits-list gnome org
- Subject: gedit r6232 - branches/jessevdk-plugins/gedit
- Date: Sun, 6 Apr 2008 23:31:26 +0100 (BST)
Author: jessevdk
Date: Sun Apr 6 23:31:26 2008
New Revision: 6232
URL: http://svn.gnome.org/viewvc/gedit?rev=6232&view=rev
Log:
* gedit/gedit-plugins-engine.[ch]: added get_plugin function to be used
by plugins
* gedit/gedit-python-module.c:
* gedit/gedit-python-plugin.c:
* gedit/gedit-python-plugin.h:
* gedit/gedit-plugins-engine.c:
* gedit/gedit-plugins-engine.h:
Changed GeditPythonPlugin to a full GObject subclass of GeditPlugin.
New GTypes are no longer created for every python plugin, python plugins
should now inherit from GeditPythonPlugin instead which together with
the python bindings provides all the necessary glue. This means that
python plugins are now first class citizens and only carry one GObject
type. Updated GeditPluginsEngine accordingly
Modified:
branches/jessevdk-plugins/gedit/gedit-plugins-engine.c
branches/jessevdk-plugins/gedit/gedit-plugins-engine.h
branches/jessevdk-plugins/gedit/gedit-python-module.c
branches/jessevdk-plugins/gedit/gedit-python-plugin.c
branches/jessevdk-plugins/gedit/gedit-python-plugin.h
Modified: branches/jessevdk-plugins/gedit/gedit-plugins-engine.c
==============================================================================
--- branches/jessevdk-plugins/gedit/gedit-plugins-engine.c (original)
+++ branches/jessevdk-plugins/gedit/gedit-plugins-engine.c Sun Apr 6 23:31:26 2008
@@ -137,7 +137,7 @@
"Only the first will be considered.\n",
info->module_name);
- _gedit_plugin_info_unref (info);
+ gedit_plugin_info_free (info);
continue;
}
@@ -264,7 +264,7 @@
g_return_if_fail (engine->priv->gconf_client != NULL);
g_list_foreach (engine->priv->plugin_list,
- (GFunc) _gedit_plugin_info_unref, NULL);
+ (GFunc) gedit_plugin_info_free, NULL);
g_list_free (engine->priv->plugin_list);
g_object_unref (engine->priv->gconf_client);
@@ -325,6 +325,38 @@
return engine->priv->plugin_list;
}
+static gint
+compare_plugin_name (GeditPluginInfo *info,
+ const gchar *name)
+{
+ return strcmp(gedit_plugin_info_get_module_name(info), name);
+}
+
+GeditPlugin *
+gedit_plugins_engine_get_plugin (GeditPluginsEngine *engine,
+ const gchar *name)
+{
+ GList *item;
+ GeditPluginInfo *info;
+
+ gedit_debug(DEBUG_PLUGINS);
+
+ item = g_list_find_custom(engine->priv->plugin_list,
+ name,
+ (GCompareFunc)compare_plugin_name);
+
+ if (!item)
+ return NULL;
+
+ info = GEDIT_PLUGIN_INFO(item->data);
+
+ /* CHECK: is this really what we want? */
+ if (!gedit_plugin_info_is_active(info))
+ return NULL;
+
+ return gedit_plugin_info_get_plugin(info);
+}
+
static gboolean
load_plugin_module (GeditPluginInfo *info)
{
Modified: branches/jessevdk-plugins/gedit/gedit-plugins-engine.h
==============================================================================
--- branches/jessevdk-plugins/gedit/gedit-plugins-engine.h (original)
+++ branches/jessevdk-plugins/gedit/gedit-plugins-engine.h Sun Apr 6 23:31:26 2008
@@ -84,6 +84,8 @@
GeditPluginInfo *info,
GtkWindow *parent);
+GeditPlugin *gedit_plugins_engine_get_plugin (GeditPluginsEngine *engine,
+ const gchar *name);
/*
* new_window == TRUE if this function is called because a new top window
* has been created
Modified: branches/jessevdk-plugins/gedit/gedit-python-module.c
==============================================================================
--- branches/jessevdk-plugins/gedit/gedit-python-module.c (original)
+++ branches/jessevdk-plugins/gedit/gedit-python-module.c Sun Apr 6 23:31:26 2008
@@ -49,7 +49,7 @@
{
gchar *module;
gchar *path;
- GType type;
+ PyObject *type;
};
enum
@@ -72,8 +72,12 @@
void pygeditcommands_register_classes (PyObject *d);
extern PyMethodDef pygeditcommands_functions[];
+/* Exported by pygeditplugins module */
+void pygeditplugins_register_classes (PyObject *d);
+extern PyMethodDef pygeditplugins_functions[];
+
/* We retreive this to check for correct class hierarchy */
-static PyTypeObject *PyGeditPlugin_Type;
+static PyTypeObject *PyGeditPythonPlugin_Type;
G_DEFINE_TYPE (GeditPythonModule, gedit_python_module, G_TYPE_TYPE_MODULE)
@@ -125,9 +129,11 @@
if (!PyType_Check(value))
continue;
- if (PyObject_IsSubclass (value, (PyObject*) PyGeditPlugin_Type))
+ if (PyObject_IsSubclass (value, (PyObject*) PyGeditPythonPlugin_Type))
{
- priv->type = gedit_python_object_get_type (gmodule, value);
+ /* Store the class as the type */
+ Py_INCREF(value);
+ priv->type = value;
return TRUE;
}
}
@@ -138,22 +144,44 @@
static void
gedit_python_module_unload (GTypeModule *module)
{
- GeditPythonModulePrivate *priv = GEDIT_PYTHON_MODULE_GET_PRIVATE (module);
gedit_debug_message (DEBUG_PLUGINS, "Unloading Python module");
-
- priv->type = 0;
+}
+
+static void
+destroy_pyobject (gpointer data,
+ GeditPythonPlugin *plugin,
+ gboolean is_last_ref)
+{
+ if (is_last_ref)
+ _gedit_python_plugin_destroy (plugin);
}
GObject *
gedit_python_module_new_object (GeditPythonModule *module)
{
GeditPythonModulePrivate *priv = GEDIT_PYTHON_MODULE_GET_PRIVATE (module);
- gedit_debug_message (DEBUG_PLUGINS, "Creating object of type %s", g_type_name (priv->type));
-
+ GObject *object;
+
if (priv->type == 0)
return NULL;
- return g_object_new (priv->type, NULL);
+ PyObject *str = PyObject_Str (priv->type);
+ gedit_debug_message (DEBUG_PLUGINS, "Creating object of type %s", PyString_AsString(str));
+ Py_DECREF(str);
+
+ /* Create new python object, which inherits from GeditPythonPlugin,
+ then get the actual gobject instance back */
+ PyObject *instance = PyObject_CallObject (priv->type, NULL);
+
+ gedit_debug_message (DEBUG_PLUGINS, "Created %d", instance);
+ /* CHECKME: not sure if we want to decrease the reference here, but
+ should be OK since GeditPythonPlugin adds a reference */
+ Py_XDECREF(instance);
+
+ object = pygobject_get(instance);
+
+ g_object_add_toggle_ref (object, (GToggleNotify)destroy_pyobject, NULL);
+ return pygobject_get(instance);
}
static void
@@ -166,7 +194,15 @@
gedit_python_module_finalize (GObject *object)
{
GeditPythonModulePrivate *priv = GEDIT_PYTHON_MODULE_GET_PRIVATE (object);
- gedit_debug_message (DEBUG_PLUGINS, "Finalizing Python module %s", g_type_name (priv->type));
+
+ if (priv->type)
+ {
+ PyObject *str = PyObject_Str (priv->type);
+ gedit_debug_message (DEBUG_PLUGINS, "Finalizing Python module %s", PyString_AsString(str));
+
+ Py_DECREF(str);
+ Py_DECREF(priv->type);
+ }
g_free (priv->module);
g_free (priv->path);
@@ -394,7 +430,7 @@
gedit_python_init (void)
{
PyObject *mdict, *tuple;
- PyObject *gedit, *geditutils, *geditcommands;
+ PyObject *gedit, *geditutils, *geditcommands, *geditplugins;
PyObject *gettext, *install, *gettext_args;
struct sigaction old_sigint;
gint res;
@@ -504,8 +540,8 @@
Py_DECREF(tuple);
/* Retrieve the Python type for gedit.Plugin */
- PyGeditPlugin_Type = (PyTypeObject *) PyDict_GetItemString (mdict, "Plugin");
- if (PyGeditPlugin_Type == NULL)
+ PyGeditPythonPlugin_Type = (PyTypeObject *) PyDict_GetItemString (mdict, "Plugin");
+ if (PyGeditPythonPlugin_Type == NULL)
{
PyErr_Print ();
@@ -519,12 +555,19 @@
/* import gedit.commands */
geditcommands = Py_InitModule ("gedit.commands", pygeditcommands_functions);
PyDict_SetItemString (mdict, "commands", geditcommands);
+
+ /* import gedit.plugins */
+ geditplugins = Py_InitModule ("gedit.plugins", pygeditplugins_functions);
+ PyDict_SetItemString (mdict, "plugins", geditplugins);
mdict = PyModule_GetDict (geditutils);
pygeditutils_register_classes (mdict);
mdict = PyModule_GetDict (geditcommands);
pygeditcommands_register_classes (mdict);
+
+ mdict = PyModule_GetDict (geditplugins);
+ pygeditplugins_register_classes (mdict);
/* i18n support */
gettext = PyImport_ImportModule ("gettext");
Modified: branches/jessevdk-plugins/gedit/gedit-python-plugin.c
==============================================================================
--- branches/jessevdk-plugins/gedit/gedit-python-plugin.c (original)
+++ branches/jessevdk-plugins/gedit/gedit-python-plugin.c Sun Apr 6 23:31:26 2008
@@ -3,6 +3,7 @@
* This file is part of gedit
*
* Copyright (C) 2005 Raphael Slinckx
+ * 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
@@ -29,26 +30,38 @@
#include <pygobject.h>
#include <string.h>
+#define GEDIT_PYTHON_PLUGIN_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), GEDIT_TYPE_PYTHON_PLUGIN, GeditPythonPluginPrivate))
+
static GObjectClass *parent_class;
+struct _GeditPythonPluginPrivate
+{
+ PyObject *instance;
+};
+
+static void gedit_python_plugin_class_init (GeditPythonPluginClass *klass);
+static void gedit_python_plugin_init (GeditPythonPlugin *plugin);
+
+G_DEFINE_TYPE (GeditPythonPlugin, gedit_python_plugin, GEDIT_TYPE_PLUGIN)
+
static PyObject *
-call_python_method (GeditPythonObject *object,
- GeditWindow *window,
- gchar *method)
+call_python_method (GeditPythonPluginPrivate *priv,
+ GeditWindow *window,
+ gchar *method)
{
PyObject *py_ret = NULL;
- g_return_val_if_fail (PyObject_HasAttrString (object->instance, method), NULL);
+ g_return_val_if_fail (PyObject_HasAttrString (priv->instance, method), NULL);
if (window == NULL)
{
- py_ret = PyObject_CallMethod (object->instance,
+ py_ret = PyObject_CallMethod (priv->instance,
method,
NULL);
}
else
{
- py_ret = PyObject_CallMethod (object->instance,
+ py_ret = PyObject_CallMethod (priv->instance,
method,
"(N)",
pygobject_new (G_OBJECT (window)));
@@ -92,11 +105,13 @@
GeditWindow *window)
{
PyGILState_STATE state = pyg_gil_state_ensure ();
- GeditPythonObject *object = (GeditPythonObject *)plugin;
+ GeditPythonPluginPrivate *priv = GEDIT_PYTHON_PLUGIN_GET_PRIVATE(plugin);
- if (PyObject_HasAttrString (object->instance, "update_ui"))
+ gedit_debug_message (DEBUG_PLUGINS, "Update ui %d", priv->instance);
+
+ if (PyObject_HasAttrString (priv->instance, "update_ui"))
{
- PyObject *py_ret = call_python_method (object, window, "update_ui");
+ PyObject *py_ret = call_python_method (priv, window, "update_ui");
if (py_ret)
{
@@ -114,11 +129,13 @@
GeditWindow *window)
{
PyGILState_STATE state = pyg_gil_state_ensure ();
- GeditPythonObject *object = (GeditPythonObject *)plugin;
+ GeditPythonPluginPrivate *priv = GEDIT_PYTHON_PLUGIN_GET_PRIVATE(plugin);
+
+ gedit_debug_message (DEBUG_PLUGINS, "Deactivate %d", priv->instance);
- if (PyObject_HasAttrString (object->instance, "deactivate"))
+ if (PyObject_HasAttrString (priv->instance, "deactivate"))
{
- PyObject *py_ret = call_python_method (object, window, "deactivate");
+ PyObject *py_ret = call_python_method (priv, window, "deactivate");
if (py_ret)
{
@@ -136,11 +153,13 @@
GeditWindow *window)
{
PyGILState_STATE state = pyg_gil_state_ensure ();
- GeditPythonObject *object = (GeditPythonObject *)plugin;
+ GeditPythonPluginPrivate *priv = GEDIT_PYTHON_PLUGIN_GET_PRIVATE(plugin);
- if (PyObject_HasAttrString (object->instance, "activate"))
+ gedit_debug_message (DEBUG_PLUGINS, "Activate %d", priv->instance);
+
+ if (PyObject_HasAttrString (priv->instance, "activate"))
{
- PyObject *py_ret = call_python_method (object, window, "activate");
+ PyObject *py_ret = call_python_method (priv, window, "activate");
if (py_ret)
{
@@ -157,12 +176,14 @@
impl_create_configure_dialog (GeditPlugin *plugin)
{
PyGILState_STATE state = pyg_gil_state_ensure ();
- GeditPythonObject *object = (GeditPythonObject *)plugin;
+ GeditPythonPluginPrivate *priv = GEDIT_PYTHON_PLUGIN_GET_PRIVATE(plugin);
GtkWidget *ret = NULL;
- if (PyObject_HasAttrString (object->instance, "create_configure_dialog"))
+ gedit_debug_message (DEBUG_PLUGINS, "Configure dialog %d", priv->instance);
+
+ if (PyObject_HasAttrString (priv->instance, "create_configure_dialog"))
{
- PyObject *py_ret = call_python_method (object, NULL, "create_configure_dialog");
+ PyObject *py_ret = call_python_method (priv, NULL, "create_configure_dialog");
if (py_ret)
{
@@ -184,6 +205,7 @@
ret = GEDIT_PLUGIN_CLASS (parent_class)->create_configure_dialog (plugin);
pyg_gil_state_release (state);
+
return ret;
}
@@ -191,8 +213,8 @@
impl_is_configurable (GeditPlugin *plugin)
{
PyGILState_STATE state = pyg_gil_state_ensure ();
- GeditPythonObject *object = (GeditPythonObject *) plugin;
- PyObject *dict = object->instance->ob_type->tp_dict;
+ GeditPythonPluginPrivate *priv = GEDIT_PYTHON_PLUGIN_GET_PRIVATE(plugin);
+ PyObject *dict = priv->instance->ob_type->tp_dict;
gboolean result;
if (dict == NULL)
@@ -206,42 +228,76 @@
return result;
}
-
-static void
-gedit_python_object_init (GeditPythonObject *object)
+
+void
+_gedit_python_plugin_set_instance (GeditPythonPlugin *plugin,
+ PyObject *instance)
{
- GeditPythonObjectClass *class;
+ PyGILState_STATE state = pyg_gil_state_ensure ();
+ GeditPythonPluginPrivate *priv = GEDIT_PYTHON_PLUGIN_GET_PRIVATE(plugin);
+
+ Py_XDECREF(priv->instance);
- gedit_debug_message (DEBUG_PLUGINS, "Creating Python plugin instance");
+ gedit_debug_message (DEBUG_PLUGINS, "Setting Python plugin PyObject instance %d (%d)", instance);
+
+ /* CHECK: is the increment actually needed? */
+ Py_INCREF(instance);
+ GEDIT_PYTHON_PLUGIN_GET_PRIVATE(plugin)->instance = instance;
+ pyg_gil_state_release (state);
+}
- class = (GeditPythonObjectClass*) (((GTypeInstance*) object)->g_class);
+void
+_gedit_python_plugin_destroy (GeditPythonPlugin *plugin)
+{
+ PyGILState_STATE state;
+ PyObject *instance;
+ GeditPythonPluginPrivate *priv = GEDIT_PYTHON_PLUGIN_GET_PRIVATE (plugin);
+
+ if (priv->instance)
+ {
+ state = pyg_gil_state_ensure ();
+
+ instance = priv->instance;
+ priv->instance = 0;
- object->instance = PyObject_CallObject (class->type, NULL);
- if (object->instance == NULL)
- PyErr_Print();
+ Py_XDECREF (instance);
+ pyg_gil_state_release (state);
+ }
+ else
+ g_object_unref (plugin);
}
+
static void
-gedit_python_object_finalize (GObject *object)
+gedit_python_plugin_init (GeditPythonPlugin *plugin)
{
- gedit_debug_message (DEBUG_PLUGINS, "Finalizing Python plugin instance");
+ GeditPythonPluginPrivate *priv = GEDIT_PYTHON_PLUGIN_GET_PRIVATE(plugin);
+
+ gedit_debug_message (DEBUG_PLUGINS, "Creating Python plugin instance");
+ priv->instance = 0;
+}
- Py_DECREF (((GeditPythonObject *) object)->instance);
+static void
+gedit_python_plugin_finalize (GObject *object)
+{
+ PyGILState_STATE state = pyg_gil_state_ensure ();
+ GeditPythonPluginPrivate *priv = GEDIT_PYTHON_PLUGIN_GET_PRIVATE (object);
+
+ gedit_debug_message (DEBUG_PLUGINS, "Finalizing Python plugin instance");
+ Py_XDECREF (GEDIT_PYTHON_PLUGIN_GET_PRIVATE(object)->instance);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
-gedit_python_object_class_init (GeditPythonObjectClass *klass,
- gpointer class_data)
+gedit_python_plugin_class_init (GeditPythonPluginClass *klass)
{
GeditPluginClass *plugin_class = GEDIT_PLUGIN_CLASS (klass);
parent_class = g_type_class_peek_parent (klass);
- klass->type = (PyObject*) class_data;
-
- G_OBJECT_CLASS (klass)->finalize = gedit_python_object_finalize;
+ g_type_class_add_private (klass, sizeof (GeditPythonPluginPrivate));
+ G_OBJECT_CLASS (klass)->finalize = gedit_python_plugin_finalize;
plugin_class->activate = impl_activate;
plugin_class->deactivate = impl_deactivate;
@@ -249,37 +305,3 @@
plugin_class->create_configure_dialog = impl_create_configure_dialog;
plugin_class->is_configurable = impl_is_configurable;
}
-
-GType
-gedit_python_object_get_type (GTypeModule *module,
- PyObject *type)
-{
- GType gtype;
- gchar *type_name;
-
- GTypeInfo info = {
- sizeof (GeditPythonObjectClass),
- NULL, /* base_init */
- NULL, /* base_finalize */
- (GClassInitFunc) gedit_python_object_class_init,
- NULL, /* class_finalize */
- type, /* class_data */
- sizeof (GeditPythonObject),
- 0, /* n_preallocs */
- (GInstanceInitFunc) gedit_python_object_init,
- };
-
- Py_INCREF (type);
-
- type_name = g_strdup_printf ("%s+GeditPythonPlugin",
- PyString_AsString (PyObject_GetAttrString (type, "__name__")));
-
- gedit_debug_message (DEBUG_PLUGINS, "Registering Python plugin instance: %s", type_name);
- gtype = g_type_module_register_type (module,
- GEDIT_TYPE_PLUGIN,
- type_name,
- &info, 0);
- g_free (type_name);
-
- return gtype;
-}
Modified: branches/jessevdk-plugins/gedit/gedit-python-plugin.h
==============================================================================
--- branches/jessevdk-plugins/gedit/gedit-python-plugin.h (original)
+++ branches/jessevdk-plugins/gedit/gedit-python-plugin.h Sun Apr 6 23:31:26 2008
@@ -1,8 +1,9 @@
/*
- * gedit-python-plugin.h
+ * gedit-plugin.h
* This file is part of gedit
*
- * Copyright (C) 2005 Raphael Slinckx
+ * Copyright (C) 2005 - Raphael Slinckx
+ * Copyright (C) 2007 - 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
@@ -19,32 +20,65 @@
* Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*/
-#ifndef GEDIT_PYTHON_OBJECT_H
-#define GEDIT_PYTHON_OBJECT_H
-#include <Python.h>
+#ifndef __GEDIT_PYTHON_PLUGIN_H__
+#define __GEDIT_PYTHON_PLUGIN_H__
+
#include <glib-object.h>
-#include "gedit-plugin.h"
+#include <pygobject.h>
+
+#include <gedit/gedit-plugin.h>
G_BEGIN_DECLS
-typedef struct _GeditPythonObject GeditPythonObject;
-typedef struct _GeditPythonObjectClass GeditPythonObjectClass;
+/*
+ * Type checking and casting macros
+ */
+#define GEDIT_TYPE_PYTHON_PLUGIN (gedit_python_plugin_get_type())
+#define GEDIT_PYTHON_PLUGIN(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GEDIT_TYPE_PYTHON_PLUGIN, GeditPythonPlugin))
+#define GEDIT_PYTHON_PLUGIN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GEDIT_TYPE_PYTHON_PLUGIN, GeditPythonPluginClass))
+#define GEDIT_IS_PYTHON_PLUGIN(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GEDIT_TYPE_PYTHON_PLUGIN))
+#define GEDIT_IS_PYTHON_PLUGIN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GEDIT_TYPE_PYTHON_PLUGIN))
+#define GEDIT_PYTHON_PLUGIN_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GEDIT_TYPE_PYTHON_PLUGIN, GeditPythonPluginClass))
+
+/* Private structure type */
+typedef struct _GeditPythonPluginPrivate GeditPythonPluginPrivate;
+
+/*
+ * Main object structure
+ */
+typedef struct _GeditPythonPlugin GeditPythonPlugin;
-struct _GeditPythonObject
+struct _GeditPythonPlugin
{
- GeditPlugin parent_slot;
- PyObject *instance;
+ GeditPlugin parent;
+
+ /*< private > */
+ GeditPythonPluginPrivate *priv;
};
-struct _GeditPythonObjectClass
+/*
+ * Class definition
+ */
+typedef struct _GeditPythonPluginClass GeditPythonPluginClass;
+
+struct _GeditPythonPluginClass
{
- GeditPluginClass parent_slot;
- PyObject *type;
+ GeditPluginClass parent_class;
};
-GType gedit_python_object_get_type (GTypeModule *module, PyObject *type);
+/*
+ * Public methods
+ */
+GType gedit_python_plugin_get_type (void) G_GNUC_CONST;
+
+/*
+ * Private methods
+ */
+void _gedit_python_plugin_set_instance (GeditPythonPlugin *plugin,
+ PyObject *instance);
+void _gedit_python_plugin_destroy (GeditPythonPlugin *plugin);
G_END_DECLS
-#endif
+#endif /* __GEDIT_PYTHON_PLUGIN_H__ */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]