Python plugin: Make 'account' property of ComposeWindow read/write
authorHolger Berndt <hb@claws-mail.org>
Thu, 14 Aug 2014 21:41:00 +0000 (23:41 +0200)
committerHolger Berndt <hb@claws-mail.org>
Thu, 14 Aug 2014 21:41:07 +0000 (23:41 +0200)
src/plugins/python/accounttype.c
src/plugins/python/accounttype.h
src/plugins/python/composewindowtype.c

index 6239ead..e5cbe32 100644 (file)
@@ -160,3 +160,15 @@ PyObject* clawsmail_account_new(PrefsAccount *account)
   ff->account = account;
   return (PyObject*)ff;
 }
+
+gboolean clawsmail_account_check(PyObject *self)
+{
+  return (PyObject_TypeCheck(self, &clawsmail_AccountType) != 0);
+}
+
+PrefsAccount* clawsmail_account_get_account(PyObject *self)
+{
+  g_return_val_if_fail(clawsmail_account_check(self), NULL);
+
+  return ((clawsmail_AccountObject*)self)->account;
+}
index cf5da0a..07328d6 100644 (file)
@@ -27,5 +27,7 @@ gboolean cmpy_add_account(PyObject *module);
 
 PyObject* clawsmail_account_new(PrefsAccount *account);
 
+gboolean clawsmail_account_check(PyObject *self);
+PrefsAccount* clawsmail_account_get_account(PyObject *self);
 
 #endif /* ACCOUNTTYPE_H */
index d07a735..3a54116 100644 (file)
@@ -30,6 +30,7 @@
 #include "mainwindow.h"
 #include "account.h"
 #include "summaryview.h"
+#include "gtk/combobox.h"
 
 #include <glib.h>
 #include <glib/gi18n.h>
@@ -443,6 +444,38 @@ static PyObject* get_account(clawsmail_ComposeWindowObject *self, void *closure)
   Py_RETURN_NONE;
 }
 
+static int set_account(clawsmail_ComposeWindowObject *self, PyObject *value, void *closure)
+{
+  PrefsAccount *target_account;
+
+  if(value == NULL) {
+    PyErr_SetString(PyExc_TypeError, "Cannot delete 'account' attribute");
+    return -1;
+  }
+
+  if(!clawsmail_account_check(value)) {
+    PyErr_SetString(PyExc_TypeError, "ComposeWindow.account: Can only assign an account");
+    return -1;
+  }
+
+
+  target_account = clawsmail_account_get_account(value);
+  if(!target_account) {
+    PyErr_SetString(PyExc_TypeError, "Account value broken");
+    return -1;
+  }
+
+  if(!self->compose || !self->compose->account_combo) {
+    PyErr_SetString(PyExc_RuntimeError, "ComposeWindow: Cannot access account");
+    return -1;
+  }
+
+  combobox_select_by_data(GTK_COMBO_BOX(self->compose->account_combo), target_account->account_id);
+
+  return 0;
+}
+
+
 
 static PyMethodDef ComposeWindow_methods[] = {
     {"set_subject", (PyCFunction)ComposeWindow_set_subject, METH_VARARGS,
@@ -547,7 +580,7 @@ static PyMemberDef ComposeWindow_members[] = {
 };
 
 static PyGetSetDef ComposeWindow_getset[] = {
-    {"account", (getter)get_account, (setter)NULL,
+    {"account", (getter)get_account, (setter)set_account,
       "account - the account corresponding to this compose window", NULL},
 
     {NULL}