2 * Copyright (C) 2005 Raphael Slinckx
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301 USA.
22 #include "rb-python-plugin.h"
23 #include "rb-plugin.h"
26 #include <pygobject.h>
29 static GObjectClass
*parent_class
;
32 call_python_method (RBPythonObject
*object
,
36 PyObject
*py_ret
= NULL
;
38 g_return_val_if_fail (PyObject_HasAttrString (object
->instance
, method
), NULL
);
42 py_ret
= PyObject_CallMethod (object
->instance
,
48 py_ret
= PyObject_CallMethod (object
->instance
,
51 pygobject_new (G_OBJECT (shell
)));
61 check_py_object_is_gtk_widget (PyObject
*py_obj
)
63 static PyTypeObject
*_PyGtkWidget_Type
= NULL
;
65 if (_PyGtkWidget_Type
== NULL
)
69 if ((module
= PyImport_ImportModule ("gtk")))
71 PyObject
*moddict
= PyModule_GetDict (module
);
72 _PyGtkWidget_Type
= (PyTypeObject
*) PyDict_GetItemString (moddict
, "Widget");
75 if (_PyGtkWidget_Type
== NULL
)
77 PyErr_SetString(PyExc_TypeError
, "could not find python gtk widget type");
84 return PyObject_TypeCheck (py_obj
, _PyGtkWidget_Type
) ? TRUE
: FALSE
;
88 impl_deactivate (RBPlugin
*plugin
,
91 PyGILState_STATE state
= pyg_gil_state_ensure ();
92 RBPythonObject
*object
= (RBPythonObject
*)plugin
;
94 if (PyObject_HasAttrString (object
->instance
, "deactivate"))
96 PyObject
*py_ret
= call_python_method (object
, shell
, "deactivate");
104 RB_PLUGIN_CLASS (parent_class
)->deactivate (plugin
, shell
);
106 pyg_gil_state_release (state
);
110 impl_activate (RBPlugin
*plugin
,
113 PyGILState_STATE state
= pyg_gil_state_ensure ();
114 RBPythonObject
*object
= (RBPythonObject
*)plugin
;
116 if (PyObject_HasAttrString (object
->instance
, "activate"))
118 PyObject
*py_ret
= call_python_method (object
, shell
, "activate");
126 RB_PLUGIN_CLASS (parent_class
)->activate (plugin
, shell
);
128 pyg_gil_state_release (state
);
132 impl_create_configure_dialog (RBPlugin
*plugin
)
134 PyGILState_STATE state
= pyg_gil_state_ensure ();
135 RBPythonObject
*object
= (RBPythonObject
*)plugin
;
136 GtkWidget
*ret
= NULL
;
138 if (PyObject_HasAttrString (object
->instance
, "create_configure_dialog"))
140 PyObject
*py_ret
= call_python_method (object
, NULL
, "create_configure_dialog");
144 if (check_py_object_is_gtk_widget (py_ret
))
146 ret
= GTK_WIDGET (pygobject_get (py_ret
));
151 PyErr_SetString(PyExc_TypeError
, "return value for create_configure_dialog is not a GtkWidget");
159 ret
= RB_PLUGIN_CLASS (parent_class
)->create_configure_dialog (plugin
);
161 pyg_gil_state_release (state
);
166 impl_is_configurable (RBPlugin
*plugin
)
168 PyGILState_STATE state
= pyg_gil_state_ensure ();
169 RBPythonObject
*object
= (RBPythonObject
*) plugin
;
170 PyObject
*dict
= object
->instance
->ob_type
->tp_dict
;
175 else if (!PyDict_Check(dict
))
178 result
= PyDict_GetItemString(dict
, "create_configure_dialog") != NULL
;
180 pyg_gil_state_release (state
);
186 rb_python_object_init (RBPythonObject
*object
)
188 RBPythonObjectClass
*class;
190 rb_debug ("Creating python plugin instance");
192 class = (RBPythonObjectClass
*) (((GTypeInstance
*) object
)->g_class
);
194 object
->instance
= PyObject_CallObject (class->type
, NULL
);
195 if (object
->instance
== NULL
)
200 rb_python_object_finalize (GObject
*object
)
202 rb_debug ("Finalizing python plugin instance");
204 if (((RBPythonObject
*) object
)->instance
) {
205 Py_DECREF (((RBPythonObject
*) object
)->instance
);
208 G_OBJECT_CLASS (parent_class
)->finalize (object
);
212 rb_python_object_class_init (RBPythonObjectClass
*klass
,
215 RBPluginClass
*plugin_class
= RB_PLUGIN_CLASS (klass
);
216 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
218 parent_class
= g_type_class_peek_parent (klass
);
220 klass
->type
= (PyObject
*) class_data
;
222 object_class
->finalize
= rb_python_object_finalize
;
224 plugin_class
->activate
= impl_activate
;
225 plugin_class
->deactivate
= impl_deactivate
;
226 plugin_class
->create_configure_dialog
= impl_create_configure_dialog
;
227 plugin_class
->is_configurable
= impl_is_configurable
;
231 rb_python_object_get_type (GTypeModule
*module
,
238 sizeof (RBPythonObjectClass
),
239 NULL
, /* base_init */
240 NULL
, /* base_finalize */
241 (GClassInitFunc
) rb_python_object_class_init
,
242 NULL
, /* class_finalize */
243 type
, /* class_data */
244 sizeof (RBPythonObject
),
246 (GInstanceInitFunc
) rb_python_object_init
,
251 type_name
= g_strdup_printf ("%s+RBPythonPlugin",
252 PyString_AsString (PyObject_GetAttrString (type
, "__name__")));
254 rb_debug ("Registering python plugin instance: %s", type_name
);
255 gtype
= g_type_module_register_type (module
,