[Xfce4-commits] <thunarx-python:master> Implemented the do_save and do_load ThunarxRenamer proxy methods and cleaned things up a bit

Adam Plumb noreply at xfce.org
Thu May 27 18:12:40 CEST 2010


Updating branch refs/heads/master
         to adee09c9457f447e7a5ab73babf336c51901401f (commit)
       from 35310096812bbe7b7e21d25c61cb8791c15b9c39 (commit)

commit adee09c9457f447e7a5ab73babf336c51901401f
Author: Adam Plumb <adamplumb at gmail.com>
Date:   Wed Jan 13 16:22:22 2010 -0500

    Implemented the do_save and do_load ThunarxRenamer proxy methods and cleaned things up a bit

 examples/thunarx-renamer-plugin.py |   29 +++++-
 src/thunarx.override               |  182 +++++++++++++++++++++++++++++-------
 2 files changed, 175 insertions(+), 36 deletions(-)

diff --git a/examples/thunarx-renamer-plugin.py b/examples/thunarx-renamer-plugin.py
index 669e141..4dcc500 100644
--- a/examples/thunarx-renamer-plugin.py
+++ b/examples/thunarx-renamer-plugin.py
@@ -1,15 +1,20 @@
 import thunarx
 import gtk
+import gobject
 
 class ThunarxPythonRenamer(thunarx.Renamer):
     __gtype_name__ = "ThunarxPythonRenamer"
+    test = gobject.property(type=str)
     
     def __init__(self):
         thunarx.Renamer.__init__(self)
+
+        # Set properties to be saved in the settings files
+        self.set_property("test", "blah")
         
         self.set_name("Adam's Renamer")
         self.set_help_url("http://www.google.com")
-        
+     
         hbox = gtk.HBox(0, False)
         
         label = gtk.Label("Setting 1:")
@@ -22,10 +27,13 @@ class ThunarxPythonRenamer(thunarx.Renamer):
         label.show()
         entry.show()
         self.add(hbox)
-        hbox.show()        
+        hbox.show()
     
     def do_process(self, file, text, index):
-        return "__" + text
+        if text.startswith("__"):
+            return text.replace("__", "")
+        else:
+            return "__" + text
 
     def do_changed(self, widget):
         pass
@@ -33,6 +41,21 @@ class ThunarxPythonRenamer(thunarx.Renamer):
     def do_get_actions(self, window, files):
         return [gtk.Action("TPR:SomeAction", "Some Action", None, gtk.STOCK_OPEN)]
 
+    def do_load(self, settings):
+        """
+        Loads settings saved in ~/.config/Thunar/renamerrc
+        """
+        print "loading settings"
+
+    def do_save(self, settings):
+        """
+        If do_save is overriden, you must rebuild the settings dictionary and then
+        return it.
+        """
+        settings["test"] = "blah"
+        
+        return settings
+
 class ThunarxRenamerPlugin(thunarx.RenamerProvider):
     def __init__(self):
         pass
diff --git a/src/thunarx.override b/src/thunarx.override
index e022f60..e2e2d23 100644
--- a/src/thunarx.override
+++ b/src/thunarx.override
@@ -25,7 +25,7 @@ headers
 #include <thunarx/thunarx.h>
 
 static PyObject *
-pylt_wrap_gobj_list(GList *list)
+_glist_to_pyobject(GList *list)
 {
 	GList *l;
 	PyObject *item, *ret;
@@ -53,7 +53,7 @@ pylt_wrap_gobj_list(GList *list)
 }
 
 GList *
-pylt_unwrap_gobj_list(PyObject *py_items, PyTypeObject *type, gboolean *ok)
+_pyobject_to_glist(PyObject *py_items, PyTypeObject *type, gboolean *ok)
 {
 	int len, i;
 	GList *items;
@@ -84,6 +84,59 @@ pylt_unwrap_gobj_list(PyObject *py_items, PyTypeObject *type, gboolean *ok)
 	return items;
 }
 
+GHashTable *
+_pyobject_to_ghashtable(PyObject *py_dict, GHashTable *hashtable)
+{
+    Py_ssize_t pos = 0;
+    PyObject *py_key, *py_value;
+    
+    g_return_if_fail(py_dict != NULL);
+
+    while(PyDict_Next(py_dict, &pos, &py_key, &py_value))
+    {
+        if(PyString_Check(py_key) < 0)
+        {
+            PyErr_SetString (PyExc_TypeError, "key must be a string");
+            continue;
+        }
+        if(PyString_Check(py_value) < 0)
+        {
+            PyErr_SetString (PyExc_TypeError, "value must be a string");
+            continue;
+        }
+        
+        g_hash_table_replace(hashtable, g_strdup(PyString_AsString(py_key)), g_strdup(PyString_AsString(py_value)));
+    }
+    
+    return hashtable;
+}
+
+static PyObject*
+_ghashtable_to_pyobject(GHashTable *hashtable)
+{
+    GList *keys;
+    PyObject *dict;
+    gpointer *value;
+
+    dict = PyDict_New();
+
+    keys = g_hash_table_get_keys(hashtable);
+    for (; keys; keys = g_list_next(keys))
+    {        
+        value = g_hash_table_lookup(hashtable, keys->data);
+        if (value)
+        {
+            PyDict_SetItemString(dict, g_strdup((char*)keys->data), PyString_FromString((char *)value));
+        }
+        else
+        { 
+            PyErr_Warn(PyExc_RuntimeWarning, "hashtable contains a null value");
+        }
+    }
+    g_list_free(keys);
+
+    return dict;
+}
 
 %%
 modulename thunarx
@@ -197,40 +250,103 @@ _wrap_ThunarxRenamer__proxy_do_get_actions(ThunarxRenamer *self, GtkWindow *wind
     return retval;
 }
 %%
-define ThunarxRenamer.do_get_actions kwargs classmethod
-static PyObject *
-_wrap_thunarx_renamer_do_get_actions(PyObject *cls, PyObject *args, PyObject *kwargs)
+override ThunarxRenamer__proxy_do_load noargs
+static void
+_wrap_ThunarxRenamer__proxy_do_load(ThunarxRenamer *self, GHashTable *settings)
+{
+    PyGILState_STATE __py_state;
+    PyObject *py_self;
+    PyObject *py_settings;
+    PyObject *py_args;
+    PyObject *py_method;
+
+    __py_state = pyg_gil_state_ensure();
+    py_self = pygobject_new((GObject *) self);
+    if (!py_self) {
+        if (PyErr_Occurred())
+            PyErr_Print();
+        pyg_gil_state_release(__py_state);
+        return;
+    }
+
+    py_settings = _ghashtable_to_pyobject(settings);
+
+    py_args = PyTuple_New(1);
+    PyTuple_SET_ITEM(py_args, 0, py_settings);
+ 
+    py_method = PyObject_GetAttrString(py_self, "do_load");
+    if (!py_method) {
+        if (PyErr_Occurred())
+            PyErr_Print();
+        Py_DECREF(py_args);
+        Py_DECREF(py_self);
+        pyg_gil_state_release(__py_state);
+        return;
+    }
+ 
+    PyObject_CallObject(py_method, py_args);
+  
+    Py_DECREF(py_settings);
+    Py_DECREF(py_method);
+    Py_DECREF(py_args);
+    Py_DECREF(py_self);
+    pyg_gil_state_release(__py_state);
+}
+%%
+override ThunarxRenamer__proxy_do_save noargs
+static void
+_wrap_ThunarxRenamer__proxy_do_save(ThunarxRenamer *self, GHashTable *settings)
 {
-    gpointer klass;
-    static char *kwlist[] = { "self", "window", "files", NULL };
-    PyGObject *self;
-    PyObject *window, *py_files;
-    GList *files;
-    GList *ret;
-    gboolean ok;
-
-    if (!PyArg_ParseTupleAndKeywords(args, kwargs,"O!O!O!:ThunarxRenamer.get_actions", kwlist, &PyThunarxRenamer_Type, &self, &PyGtkWidget_Type, &window, &PyList_Type, &py_files))
-      return NULL;
-
-    files = pylt_unwrap_gobj_list(py_files, &PyGtkAction_Type, &ok);
+    PyGILState_STATE __py_state;
+    PyObject *py_self;
+    PyObject *py_settings;
+    PyObject *py_args;
+    PyObject *py_method;
+    PyObject *py_ret;
     
-    if (!ok)
-      return NULL;
+    __py_state = pyg_gil_state_ensure();
+    py_self = pygobject_new((GObject *) self);
+    if (!py_self) {
+        if (PyErr_Occurred())
+            PyErr_Print();
+        pyg_gil_state_release(__py_state);
+        return;
+    }
 
-    klass = g_type_class_ref(pyg_type_from_object(cls));
-    if (THUNARX_RENAMER_CLASS(klass)->get_actions)
-        ret = THUNARX_RENAMER_CLASS(klass)->get_actions(THUNARX_RENAMER(self->obj), GTK_WINDOW(window), files);
-    else {
-        PyErr_SetString(PyExc_NotImplementedError, "virtual method ThunarxRenamer.get_actions not implemented");
-        g_type_class_unref(klass);
-        return NULL;
+    py_settings = _ghashtable_to_pyobject(settings);
+
+    py_args = PyTuple_New(1);
+    PyTuple_SET_ITEM(py_args, 0, py_settings);
+    
+    py_method = PyObject_GetAttrString(py_self, "do_save");
+    if (!py_method) {
+        if (PyErr_Occurred())
+            PyErr_Print();
+        Py_DECREF(py_settings);
+        Py_DECREF(py_args);
+        Py_DECREF(py_self);
+        pyg_gil_state_release(__py_state);
+        return;
     }
-    g_type_class_unref(klass);
-    if (ret) {
-        PyObject *py_ret = pylt_wrap_gobj_list(ret);
-        g_free(ret);
-        return py_ret;
+
+    py_ret = PyObject_CallObject(py_method, py_args);
+    if (!py_ret) {
+        if (PyErr_Occurred())
+            PyErr_Print();
+        Py_DECREF(py_method);
+        Py_DECREF(py_settings);
+        Py_DECREF(py_args);
+        Py_DECREF(py_self);
+        pyg_gil_state_release(__py_state);
+        return;
     }
-    Py_INCREF(Py_None);
-    return Py_None;
+    
+    settings = _pyobject_to_ghashtable(py_ret, settings);
+    
+    Py_DECREF(py_ret);
+    Py_DECREF(py_method);
+    Py_DECREF(py_settings);
+    Py_DECREF(py_args);
+    Py_DECREF(py_self);
+    pyg_gil_state_release(__py_state);
 }



More information about the Xfce4-commits mailing list