Updated Finnish translation
[rhythmbox.git] / plugins / rb-python-plugin.c
bloba128325af8c771541c46c21a0132f3e2b37cc69a
1 /*
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.
20 #include <config.h>
22 #include "rb-python-plugin.h"
23 #include "rb-plugin.h"
24 #include "rb-debug.h"
26 #include <pygobject.h>
27 #include <string.h>
29 static GObjectClass *parent_class;
31 static PyObject *
32 call_python_method (RBPythonObject *object,
33 RBShell *shell,
34 gchar *method)
36 PyObject *py_ret = NULL;
38 g_return_val_if_fail (PyObject_HasAttrString (object->instance, method), NULL);
40 if (shell == NULL)
42 py_ret = PyObject_CallMethod (object->instance,
43 method,
44 NULL);
46 else
48 py_ret = PyObject_CallMethod (object->instance,
49 method,
50 "(N)",
51 pygobject_new (G_OBJECT (shell)));
54 if (!py_ret)
55 PyErr_Print ();
57 return py_ret;
60 static gboolean
61 check_py_object_is_gtk_widget (PyObject *py_obj)
63 static PyTypeObject *_PyGtkWidget_Type = NULL;
65 if (_PyGtkWidget_Type == NULL)
67 PyObject *module;
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");
78 PyErr_Print();
80 return FALSE;
84 return PyObject_TypeCheck (py_obj, _PyGtkWidget_Type) ? TRUE : FALSE;
87 static void
88 impl_deactivate (RBPlugin *plugin,
89 RBShell *shell)
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");
98 if (py_ret)
100 Py_XDECREF (py_ret);
103 else
104 RB_PLUGIN_CLASS (parent_class)->deactivate (plugin, shell);
106 pyg_gil_state_release (state);
109 static void
110 impl_activate (RBPlugin *plugin,
111 RBShell *shell)
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");
120 if (py_ret)
122 Py_XDECREF (py_ret);
125 else
126 RB_PLUGIN_CLASS (parent_class)->activate (plugin, shell);
128 pyg_gil_state_release (state);
131 static GtkWidget *
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");
142 if (py_ret)
144 if (check_py_object_is_gtk_widget (py_ret))
146 ret = GTK_WIDGET (pygobject_get (py_ret));
147 g_object_ref (ret);
149 else
151 PyErr_SetString(PyExc_TypeError, "return value for create_configure_dialog is not a GtkWidget");
152 PyErr_Print();
155 Py_DECREF (py_ret);
158 else
159 ret = RB_PLUGIN_CLASS (parent_class)->create_configure_dialog (plugin);
161 pyg_gil_state_release (state);
162 return ret;
165 static gboolean
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;
171 gboolean result;
173 if (dict == NULL)
174 result = FALSE;
175 else if (!PyDict_Check(dict))
176 result = FALSE;
177 else
178 result = PyDict_GetItemString(dict, "create_configure_dialog") != NULL;
180 pyg_gil_state_release (state);
182 return result;
185 static void
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)
196 PyErr_Print();
199 static void
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);
211 static void
212 rb_python_object_class_init (RBPythonObjectClass *klass,
213 gpointer class_data)
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;
230 GType
231 rb_python_object_get_type (GTypeModule *module,
232 PyObject *type)
234 GType gtype;
235 gchar *type_name;
237 GTypeInfo info = {
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),
245 0, /* n_preallocs */
246 (GInstanceInitFunc) rb_python_object_init,
249 Py_INCREF (type);
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,
256 RB_TYPE_PLUGIN,
257 type_name,
258 &info, 0);
259 g_free (type_name);
261 return gtype;