1 /* Python plugin for Claws-Mail
2 * Copyright (C) 2013 Holger Berndt <hb@claws-mail.org>
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.
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.
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/>.
20 # include "claws-features.h"
23 #include "accounttype.h"
25 #include <structmember.h>
29 PyObject *account_name;
31 PrefsAccount *account;
32 } clawsmail_AccountObject;
34 static int Account_init(clawsmail_AccountObject *self, PyObject *args, PyObject *kwds)
37 self->account_name = Py_None;
40 self->address = Py_None;
47 static void Account_dealloc(clawsmail_AccountObject* self)
49 Py_XDECREF(self->account_name);
50 Py_XDECREF(self->address);
52 self->ob_type->tp_free((PyObject*)self);
55 static PyObject* Account_str(PyObject *self)
58 str = PyString_FromString("Account: ");
61 PyString_ConcatAndDel(&str, PyObject_GetAttrString(self, "account_name"));
66 static PyObject* get_account_name(clawsmail_AccountObject *self, void *closure)
68 Py_INCREF(self->account_name);
69 return self->account_name;
72 static PyObject* get_address(clawsmail_AccountObject *self, void *closure)
74 Py_INCREF(self->address);
78 static PyMethodDef Account_methods[] = {
82 static PyGetSetDef Account_getset[] = {
83 {"account_name", (getter)get_account_name, (setter)NULL,
84 "account_name - name of the account", NULL},
86 {"address", (getter)get_address, (setter)NULL,
87 "address - address of the account", NULL},
92 static PyTypeObject clawsmail_AccountType = {
93 PyObject_HEAD_INIT(NULL)
95 "clawsmail.Account", /* tp_name*/
96 sizeof(clawsmail_AccountObject), /* tp_basicsize*/
98 (destructor)Account_dealloc, /* tp_dealloc*/
105 0, /* tp_as_sequence*/
106 0, /* tp_as_mapping*/
109 Account_str, /* tp_str*/
113 Py_TPFLAGS_DEFAULT, /* tp_flags*/
114 "Account objects.\n\n" /* tp_doc */
115 "Do not construct objects of this type yourself.",
118 0, /* tp_richcompare */
119 0, /* tp_weaklistoffset */
122 Account_methods, /* tp_methods */
124 Account_getset, /* tp_getset */
127 0, /* tp_descr_get */
128 0, /* tp_descr_set */
129 0, /* tp_dictoffset */
130 (initproc)Account_init, /* tp_init */
136 gboolean cmpy_add_account(PyObject *module)
138 clawsmail_AccountType.tp_new = PyType_GenericNew;
139 if(PyType_Ready(&clawsmail_AccountType) < 0)
142 Py_INCREF(&clawsmail_AccountType);
143 return (PyModule_AddObject(module, "Account", (PyObject*)&clawsmail_AccountType) == 0);
146 static gboolean update_members(clawsmail_AccountObject *self, PrefsAccount *account)
148 if(account->account_name) {
149 Py_XDECREF(self->account_name);
150 self->account_name = PyString_FromString(account->account_name);
151 if(!self->account_name)
155 if(account->address) {
156 Py_XDECREF(self->address);
157 self->address = PyString_FromString(account->address);
162 self->account = account;
166 Py_XDECREF(self->account_name);
167 Py_XDECREF(self->address);
171 PyObject* clawsmail_account_new(PrefsAccount *account)
173 clawsmail_AccountObject *ff;
178 ff = (clawsmail_AccountObject*) PyObject_CallObject((PyObject*) &clawsmail_AccountType, NULL);
182 if(update_members(ff, account))
183 return (PyObject*)ff;