Python plugin: Add account address
[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     PyObject *account_name;
30     PyObject *address;
31     PrefsAccount *account;
32 } clawsmail_AccountObject;
33
34 static int Account_init(clawsmail_AccountObject *self, PyObject *args, PyObject *kwds)
35 {
36   Py_INCREF(Py_None);
37   self->account_name = Py_None;
38
39   Py_INCREF(Py_None);
40   self->address = Py_None;
41
42   self->account = NULL;
43   return 0;
44 }
45
46
47 static void Account_dealloc(clawsmail_AccountObject* self)
48 {
49   Py_XDECREF(self->account_name);
50   Py_XDECREF(self->address);
51
52   self->ob_type->tp_free((PyObject*)self);
53 }
54
55 static PyObject* Account_str(PyObject *self)
56 {
57   PyObject *str;
58   str = PyString_FromString("Account: ");
59   if(str == NULL)
60     return NULL;
61   PyString_ConcatAndDel(&str, PyObject_GetAttrString(self, "account_name"));
62
63   return str;
64 }
65
66 static PyMethodDef Account_methods[] = {
67     {NULL}
68 };
69
70 static PyMemberDef Account_members[] = {
71     {"account_name", T_OBJECT_EX, offsetof(clawsmail_AccountObject, account_name), 0,
72      "account name - name of the account"},
73
74      {"address", T_OBJECT_EX, offsetof(clawsmail_AccountObject, address), 0,
75       "address - address of the account"},
76
77     {NULL}
78 };
79
80 static PyTypeObject clawsmail_AccountType = {
81     PyObject_HEAD_INIT(NULL)
82     0,                         /* ob_size*/
83     "clawsmail.Account",       /* tp_name*/
84     sizeof(clawsmail_AccountObject), /* tp_basicsize*/
85     0,                         /* tp_itemsize*/
86     (destructor)Account_dealloc, /* tp_dealloc*/
87     0,                         /* tp_print*/
88     0,                         /* tp_getattr*/
89     0,                         /* tp_setattr*/
90     0,                         /* tp_compare*/
91     0,                         /* tp_repr*/
92     0,                         /* tp_as_number*/
93     0,                         /* tp_as_sequence*/
94     0,                         /* tp_as_mapping*/
95     0,                         /* tp_hash */
96     0,                         /* tp_call*/
97     Account_str,               /* tp_str*/
98     0,                         /* tp_getattro*/
99     0,                         /* tp_setattro*/
100     0,                         /* tp_as_buffer*/
101     Py_TPFLAGS_DEFAULT,        /* tp_flags*/
102     "Account objects.\n\n"     /* tp_doc */
103     "Do not construct objects of this type yourself.",
104     0,                         /* tp_traverse */
105     0,                         /* tp_clear */
106     0,                         /* tp_richcompare */
107     0,                         /* tp_weaklistoffset */
108     0,                         /* tp_iter */
109     0,                         /* tp_iternext */
110     Account_methods,           /* tp_methods */
111     Account_members,           /* tp_members */
112     0,                         /* tp_getset */
113     0,                         /* tp_base */
114     0,                         /* tp_dict */
115     0,                         /* tp_descr_get */
116     0,                         /* tp_descr_set */
117     0,                         /* tp_dictoffset */
118     (initproc)Account_init,    /* tp_init */
119     0,                         /* tp_alloc */
120     0,                         /* tp_new */
121 };
122
123
124 gboolean cmpy_add_account(PyObject *module)
125 {
126   clawsmail_AccountType.tp_new = PyType_GenericNew;
127   if(PyType_Ready(&clawsmail_AccountType) < 0)
128     return FALSE;
129
130   Py_INCREF(&clawsmail_AccountType);
131   return (PyModule_AddObject(module, "Account", (PyObject*)&clawsmail_AccountType) == 0);
132 }
133
134 static gboolean update_members(clawsmail_AccountObject *self, PrefsAccount *account)
135 {
136   if(account->account_name) {
137     Py_XDECREF(self->account_name);
138     self->account_name = PyString_FromString(account->account_name);
139     if(!self->account_name)
140       goto err;
141   }
142
143   if(account->address) {
144     Py_XDECREF(self->address);
145     self->address = PyString_FromString(account->address);
146     if(!self->address)
147       goto err;
148   }
149
150   self->account = account;
151
152   return TRUE;
153 err:
154   Py_XDECREF(self->account_name);
155   Py_XDECREF(self->address);
156   return FALSE;
157 }
158
159 PyObject* clawsmail_account_new(PrefsAccount *account)
160 {
161   clawsmail_AccountObject *ff;
162
163   if(!account)
164     return NULL;
165
166   ff = (clawsmail_AccountObject*) PyObject_CallObject((PyObject*) &clawsmail_AccountType, NULL);
167   if(!ff)
168     return NULL;
169
170   if(update_members(ff, account))
171     return (PyObject*)ff;
172   else {
173     Py_XDECREF(ff);
174     return NULL;
175   }
176 }