Python plugin: Remove unused account methods
[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 PyObject* get_account_name(clawsmail_AccountObject *self, void *closure)
67 {
68   Py_INCREF(self->account_name);
69   return self->account_name;
70 }
71
72 static PyObject* get_address(clawsmail_AccountObject *self, void *closure)
73 {
74   Py_INCREF(self->address);
75   return self->address;
76 }
77
78 static PyGetSetDef Account_getset[] = {
79     {"account_name", (getter)get_account_name, (setter)NULL,
80       "account_name - name of the account", NULL},
81
82     {"address", (getter)get_address, (setter)NULL,
83      "address - address of the account", NULL},
84
85     {NULL}
86 };
87
88 static PyTypeObject clawsmail_AccountType = {
89     PyObject_HEAD_INIT(NULL)
90     0,                         /* ob_size*/
91     "clawsmail.Account",       /* tp_name*/
92     sizeof(clawsmail_AccountObject), /* tp_basicsize*/
93     0,                         /* tp_itemsize*/
94     (destructor)Account_dealloc, /* tp_dealloc*/
95     0,                         /* tp_print*/
96     0,                         /* tp_getattr*/
97     0,                         /* tp_setattr*/
98     0,                         /* tp_compare*/
99     0,                         /* tp_repr*/
100     0,                         /* tp_as_number*/
101     0,                         /* tp_as_sequence*/
102     0,                         /* tp_as_mapping*/
103     0,                         /* tp_hash */
104     0,                         /* tp_call*/
105     Account_str,               /* tp_str*/
106     0,                         /* tp_getattro*/
107     0,                         /* tp_setattro*/
108     0,                         /* tp_as_buffer*/
109     Py_TPFLAGS_DEFAULT,        /* tp_flags*/
110     "Account objects.\n\n"     /* tp_doc */
111     "Do not construct objects of this type yourself.",
112     0,                         /* tp_traverse */
113     0,                         /* tp_clear */
114     0,                         /* tp_richcompare */
115     0,                         /* tp_weaklistoffset */
116     0,                         /* tp_iter */
117     0,                         /* tp_iternext */
118     0,                         /* tp_methods */
119     0,                         /* tp_members */
120     Account_getset,            /* tp_getset */
121     0,                         /* tp_base */
122     0,                         /* tp_dict */
123     0,                         /* tp_descr_get */
124     0,                         /* tp_descr_set */
125     0,                         /* tp_dictoffset */
126     (initproc)Account_init,    /* tp_init */
127     0,                         /* tp_alloc */
128     0,                         /* tp_new */
129 };
130
131
132 gboolean cmpy_add_account(PyObject *module)
133 {
134   clawsmail_AccountType.tp_new = PyType_GenericNew;
135   if(PyType_Ready(&clawsmail_AccountType) < 0)
136     return FALSE;
137
138   Py_INCREF(&clawsmail_AccountType);
139   return (PyModule_AddObject(module, "Account", (PyObject*)&clawsmail_AccountType) == 0);
140 }
141
142 static gboolean update_members(clawsmail_AccountObject *self, PrefsAccount *account)
143 {
144   if(account->account_name) {
145     Py_XDECREF(self->account_name);
146     self->account_name = PyString_FromString(account->account_name);
147     if(!self->account_name)
148       goto err;
149   }
150
151   if(account->address) {
152     Py_XDECREF(self->address);
153     self->address = PyString_FromString(account->address);
154     if(!self->address)
155       goto err;
156   }
157
158   self->account = account;
159
160   return TRUE;
161 err:
162   Py_XDECREF(self->account_name);
163   Py_XDECREF(self->address);
164   return FALSE;
165 }
166
167 PyObject* clawsmail_account_new(PrefsAccount *account)
168 {
169   clawsmail_AccountObject *ff;
170
171   if(!account)
172     return NULL;
173
174   ff = (clawsmail_AccountObject*) PyObject_CallObject((PyObject*) &clawsmail_AccountType, NULL);
175   if(!ff)
176     return NULL;
177
178   if(update_members(ff, account))
179     return (PyObject*)ff;
180   else {
181     Py_XDECREF(ff);
182     return NULL;
183   }
184 }