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