From 9166dc63e4d2f3f4d62293b48706cfa9b9a0afc1 Mon Sep 17 00:00:00 2001 From: Holger Berndt Date: Thu, 14 Aug 2014 23:41:00 +0200 Subject: [PATCH] Python plugin: Make 'account' property of ComposeWindow read/write --- src/plugins/python/accounttype.c | 12 +++++++++ src/plugins/python/accounttype.h | 2 ++ src/plugins/python/composewindowtype.c | 35 +++++++++++++++++++++++++- 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/plugins/python/accounttype.c b/src/plugins/python/accounttype.c index 6239eadba..e5cbe327e 100644 --- a/src/plugins/python/accounttype.c +++ b/src/plugins/python/accounttype.c @@ -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; +} diff --git a/src/plugins/python/accounttype.h b/src/plugins/python/accounttype.h index cf5da0af3..07328d691 100644 --- a/src/plugins/python/accounttype.h +++ b/src/plugins/python/accounttype.h @@ -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 */ diff --git a/src/plugins/python/composewindowtype.c b/src/plugins/python/composewindowtype.c index d07a7352a..3a5411603 100644 --- a/src/plugins/python/composewindowtype.c +++ b/src/plugins/python/composewindowtype.c @@ -30,6 +30,7 @@ #include "mainwindow.h" #include "account.h" #include "summaryview.h" +#include "gtk/combobox.h" #include #include @@ -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} -- 2.25.1