Python plugin: Make 'account' property of ComposeWindow read/write
[claws.git] / src / plugins / python / accounttype.c
1 /* Python plugin for Claws-Mail
2  * Copyright (C) 2013 Holger Berndt <hb@claws-mail.org>
3  *
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 3 of the License, or
7  * (at your option) any later version.
8  *
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.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #ifdef HAVE_CONFIG_H
19 #  include "config.h"
20 #  include "claws-features.h"
21 #endif
22
23 #include "accounttype.h"
24
25 #include <structmember.h>
26
27 typedef struct {
28     PyObject_HEAD
29     PrefsAccount *account;
30 } clawsmail_AccountObject;
31
32 static int Account_init(clawsmail_AccountObject *self, PyObject *args, PyObject *kwds)
33 {
34   self->account = NULL;
35   return 0;
36 }
37
38
39 static void Account_dealloc(clawsmail_AccountObject* self)
40 {
41   self->ob_type->tp_free((PyObject*)self);
42 }
43
44 static int Account_compare(clawsmail_AccountObject *obj1, clawsmail_AccountObject *obj2)
45 {
46   if(obj1->account->account_id < obj2->account->account_id)
47     return -1;
48   else if(obj1->account->account_id > obj2->account->account_id)
49     return 1;
50   else
51     return 0;
52 }
53
54 static PyObject* Account_str(clawsmail_AccountObject *self)
55 {
56   if(self->account && self->account->account_name)
57     return PyString_FromFormat("Account: %s", self->account->account_name);
58   Py_RETURN_NONE;
59 }
60
61 static PyObject* get_account_name(clawsmail_AccountObject *self, void *closure)
62 {
63   if(self->account && self->account->account_name)
64     return PyString_FromString(self->account->account_name);
65   Py_RETURN_NONE;
66 }
67
68 static PyObject* get_address(clawsmail_AccountObject *self, void *closure)
69 {
70   if(self->account && self->account->address)
71     return PyString_FromString(self->account->address);
72   Py_RETURN_NONE;
73 }
74
75 static PyObject* get_is_default(clawsmail_AccountObject *self, void *closure)
76 {
77   if(self->account->is_default)
78     Py_RETURN_TRUE;
79   Py_RETURN_FALSE;
80 }
81
82 static PyGetSetDef Account_getset[] = {
83     {"account_name", (getter)get_account_name, (setter)NULL,
84       "account_name - name of the account", NULL},
85
86     {"address", (getter)get_address, (setter)NULL,
87      "address - address of the account", NULL},
88
89     {"is_default", (getter)get_is_default, (setter)NULL,
90      "is_default - whether this account is the default account", NULL},
91
92     {NULL}
93 };
94
95 static PyTypeObject clawsmail_AccountType = {
96     PyObject_HEAD_INIT(NULL)
97     0,                         /* ob_size*/
98     "clawsmail.Account",       /* tp_name*/
99     sizeof(clawsmail_AccountObject), /* tp_basicsize*/
100     0,                         /* tp_itemsize*/
101     (destructor)Account_dealloc, /* tp_dealloc*/
102     0,                         /* tp_print*/
103     0,                         /* tp_getattr*/
104     0,                         /* tp_setattr*/
105     (cmpfunc)Account_compare,  /* tp_compare*/
106     0,                         /* tp_repr*/
107     0,                         /* tp_as_number*/
108     0,                         /* tp_as_sequence*/
109     0,                         /* tp_as_mapping*/
110     0,                         /* tp_hash */
111     0,                         /* tp_call*/
112     (reprfunc)Account_str,     /* tp_str*/
113     0,                         /* tp_getattro*/
114     0,                         /* tp_setattro*/
115     0,                         /* tp_as_buffer*/
116     Py_TPFLAGS_DEFAULT,        /* tp_flags*/
117     "Account objects.\n\n"     /* tp_doc */
118     "Do not construct objects of this type yourself.",
119     0,                         /* tp_traverse */
120     0,                         /* tp_clear */
121     0,                         /* tp_richcompare */
122     0,                         /* tp_weaklistoffset */
123     0,                         /* tp_iter */
124     0,                         /* tp_iternext */
125     0,                         /* tp_methods */
126     0,                         /* tp_members */
127     Account_getset,            /* tp_getset */
128     0,                         /* tp_base */
129     0,                         /* tp_dict */
130     0,                         /* tp_descr_get */
131     0,                         /* tp_descr_set */
132     0,                         /* tp_dictoffset */
133     (initproc)Account_init,    /* tp_init */
134     0,                         /* tp_alloc */
135     0,                         /* tp_new */
136 };
137
138
139 gboolean cmpy_add_account(PyObject *module)
140 {
141   clawsmail_AccountType.tp_new = PyType_GenericNew;
142   if(PyType_Ready(&clawsmail_AccountType) < 0)
143     return FALSE;
144
145   Py_INCREF(&clawsmail_AccountType);
146   return (PyModule_AddObject(module, "Account", (PyObject*)&clawsmail_AccountType) == 0);
147 }
148
149 PyObject* clawsmail_account_new(PrefsAccount *account)
150 {
151   clawsmail_AccountObject *ff;
152
153   if(!account)
154     return NULL;
155
156   ff = (clawsmail_AccountObject*) PyObject_CallObject((PyObject*) &clawsmail_AccountType, NULL);
157   if(!ff)
158     return NULL;
159
160   ff->account = account;
161   return (PyObject*)ff;
162 }
163
164 gboolean clawsmail_account_check(PyObject *self)
165 {
166   return (PyObject_TypeCheck(self, &clawsmail_AccountType) != 0);
167 }
168
169 PrefsAccount* clawsmail_account_get_account(PyObject *self)
170 {
171   g_return_val_if_fail(clawsmail_account_check(self), NULL);
172
173   return ((clawsmail_AccountObject*)self)->account;
174 }