fix CID 1596595: Resource leaks, and CID 1596594: (CHECKED_RETURN)
[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   Py_TYPE(self)->tp_free((PyObject*)self);
42 }
43
44 static PyObject* Account_str(clawsmail_AccountObject *self)
45 {
46   if(self->account && self->account->account_name)
47     return PyUnicode_FromFormat("Account: %s", self->account->account_name);
48   Py_RETURN_NONE;
49 }
50
51 static PyObject* get_account_name(clawsmail_AccountObject *self, void *closure)
52 {
53   if(self->account && self->account->account_name)
54     return PyUnicode_FromString(self->account->account_name);
55   Py_RETURN_NONE;
56 }
57
58 static PyObject* get_address(clawsmail_AccountObject *self, void *closure)
59 {
60   if(self->account && self->account->address)
61     return PyUnicode_FromString(self->account->address);
62   Py_RETURN_NONE;
63 }
64
65 static PyObject* get_is_default(clawsmail_AccountObject *self, void *closure)
66 {
67   if(self->account->is_default)
68     Py_RETURN_TRUE;
69   Py_RETURN_FALSE;
70 }
71
72 static PyGetSetDef Account_getset[] = {
73     {"account_name", (getter)get_account_name, (setter)NULL,
74       "account_name - name of the account", NULL},
75
76     {"address", (getter)get_address, (setter)NULL,
77      "address - address of the account", NULL},
78
79     {"is_default", (getter)get_is_default, (setter)NULL,
80      "is_default - whether this account is the default account", NULL},
81
82     {NULL}
83 };
84
85 static PyTypeObject clawsmail_AccountType = {
86     PyVarObject_HEAD_INIT(NULL, 0)
87     "clawsmail.Account",       /* tp_name*/
88     sizeof(clawsmail_AccountObject), /* tp_basicsize*/
89     0,                         /* tp_itemsize*/
90     (destructor)Account_dealloc, /* tp_dealloc*/
91     0,                         /* tp_print*/
92     0,                         /* tp_getattr*/
93     0,                         /* tp_setattr*/
94     0,                         /* tp_compare*/
95     0,                         /* tp_repr*/
96     0,                         /* tp_as_number*/
97     0,                         /* tp_as_sequence*/
98     0,                         /* tp_as_mapping*/
99     0,                         /* tp_hash */
100     0,                         /* tp_call*/
101     (reprfunc)Account_str,     /* tp_str*/
102     0,                         /* tp_getattro*/
103     0,                         /* tp_setattro*/
104     0,                         /* tp_as_buffer*/
105     Py_TPFLAGS_DEFAULT,        /* tp_flags*/
106     "Account objects.\n\n"     /* tp_doc */
107     "Do not construct objects of this type yourself.",
108     0,                         /* tp_traverse */
109     0,                         /* tp_clear */
110     0,                         /* tp_richcompare */
111     0,                         /* tp_weaklistoffset */
112     0,                         /* tp_iter */
113     0,                         /* tp_iternext */
114     0,                         /* tp_methods */
115     0,                         /* tp_members */
116     Account_getset,            /* tp_getset */
117     0,                         /* tp_base */
118     0,                         /* tp_dict */
119     0,                         /* tp_descr_get */
120     0,                         /* tp_descr_set */
121     0,                         /* tp_dictoffset */
122     (initproc)Account_init,    /* tp_init */
123     0,                         /* tp_alloc */
124     0,                         /* tp_new */
125         0,                         /* tp_free */
126         0,                         /* tp_is_gc */
127         0,                         /* tp_bases */
128         0,                         /* tp_mro */
129         0,                         /* tp_cache */
130         0,                         /* tp_subclasses */
131         0,                         /* tp_weaklist */
132         0,                         /* tp_del */
133 #if ((PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 6) || \
134      (PY_MAJOR_VERSION == 3))
135     0,                         /* tp_version_tag */
136 #endif
137 #if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 4)
138     0,                         /* tp_finalize */
139 #endif
140 };
141
142 gboolean cmpy_add_account(PyObject *module)
143 {
144   clawsmail_AccountType.tp_new = PyType_GenericNew;
145   if(PyType_Ready(&clawsmail_AccountType) < 0)
146     return FALSE;
147
148   Py_INCREF(&clawsmail_AccountType);
149   return (PyModule_AddObject(module, "Account", (PyObject*)&clawsmail_AccountType) == 0);
150 }
151
152 PyObject* clawsmail_account_new(PrefsAccount *account)
153 {
154   clawsmail_AccountObject *ff;
155
156   if(!account)
157     return NULL;
158
159   ff = (clawsmail_AccountObject*) PyObject_CallObject((PyObject*) &clawsmail_AccountType, NULL);
160   if(!ff)
161     return NULL;
162
163   ff->account = account;
164   return (PyObject*)ff;
165 }
166
167 gboolean clawsmail_account_check(PyObject *self)
168 {
169   return (PyObject_TypeCheck(self, &clawsmail_AccountType) != 0);
170 }
171
172 PrefsAccount* clawsmail_account_get_account(PyObject *self)
173 {
174   g_return_val_if_fail(clawsmail_account_check(self), NULL);
175
176   return ((clawsmail_AccountObject*)self)->account;
177 }