typedef struct {
PyObject_HEAD
- PyObject *account_name;
- PyObject *address;
PrefsAccount *account;
} clawsmail_AccountObject;
static int Account_init(clawsmail_AccountObject *self, PyObject *args, PyObject *kwds)
{
- Py_INCREF(Py_None);
- self->account_name = Py_None;
-
- Py_INCREF(Py_None);
- self->address = Py_None;
-
self->account = NULL;
return 0;
}
static void Account_dealloc(clawsmail_AccountObject* self)
{
- Py_XDECREF(self->account_name);
- Py_XDECREF(self->address);
-
self->ob_type->tp_free((PyObject*)self);
}
-static PyObject* Account_str(PyObject *self)
+static int Account_compare(clawsmail_AccountObject *obj1, clawsmail_AccountObject *obj2)
{
- PyObject *str;
- str = PyString_FromString("Account: ");
- if(str == NULL)
- return NULL;
- PyString_ConcatAndDel(&str, PyObject_GetAttrString(self, "account_name"));
+ if(obj1->account->account_id < obj2->account->account_id)
+ return -1;
+ else if(obj1->account->account_id > obj2->account->account_id)
+ return 1;
+ else
+ return 0;
+}
- return str;
+static PyObject* Account_str(clawsmail_AccountObject *self)
+{
+ if(self->account && self->account->account_name)
+ return PyString_FromFormat("Account: %s", self->account->account_name);
+ Py_RETURN_NONE;
}
static PyObject* get_account_name(clawsmail_AccountObject *self, void *closure)
{
- Py_INCREF(self->account_name);
- return self->account_name;
+ if(self->account && self->account->account_name)
+ return PyString_FromString(self->account->account_name);
+ Py_RETURN_NONE;
}
static PyObject* get_address(clawsmail_AccountObject *self, void *closure)
{
- Py_INCREF(self->address);
- return self->address;
+ if(self->account && self->account->address)
+ return PyString_FromString(self->account->address);
+ Py_RETURN_NONE;
}
static PyObject* get_is_default(clawsmail_AccountObject *self, void *closure)
{
if(self->account->is_default)
Py_RETURN_TRUE;
- else
- Py_RETURN_FALSE;
+ Py_RETURN_FALSE;
}
static PyGetSetDef Account_getset[] = {
0, /* tp_print*/
0, /* tp_getattr*/
0, /* tp_setattr*/
- 0, /* tp_compare*/
+ (cmpfunc)Account_compare, /* tp_compare*/
0, /* tp_repr*/
0, /* tp_as_number*/
0, /* tp_as_sequence*/
0, /* tp_as_mapping*/
0, /* tp_hash */
0, /* tp_call*/
- Account_str, /* tp_str*/
+ (reprfunc)Account_str, /* tp_str*/
0, /* tp_getattro*/
0, /* tp_setattro*/
0, /* tp_as_buffer*/
return (PyModule_AddObject(module, "Account", (PyObject*)&clawsmail_AccountType) == 0);
}
-static gboolean update_members(clawsmail_AccountObject *self, PrefsAccount *account)
-{
- if(account->account_name) {
- Py_XDECREF(self->account_name);
- self->account_name = PyString_FromString(account->account_name);
- if(!self->account_name)
- goto err;
- }
-
- if(account->address) {
- Py_XDECREF(self->address);
- self->address = PyString_FromString(account->address);
- if(!self->address)
- goto err;
- }
-
- self->account = account;
-
- return TRUE;
-err:
- Py_XDECREF(self->account_name);
- Py_XDECREF(self->address);
- return FALSE;
-}
-
PyObject* clawsmail_account_new(PrefsAccount *account)
{
clawsmail_AccountObject *ff;
if(!ff)
return NULL;
- if(update_members(ff, account))
- return (PyObject*)ff;
- else {
- Py_XDECREF(ff);
- return NULL;
- }
+ ff->account = account;
+ return (PyObject*)ff;
+}
+
+gboolean clawsmail_account_check(PyObject *self)
+{
+ return (PyObject_TypeCheck(self, &clawsmail_AccountType) != 0);
+}
+
+PrefsAccount* clawsmail_account_get_account(PyObject *self)
+{
+ g_return_val_if_fail(clawsmail_account_check(self), NULL);
+
+ return ((clawsmail_AccountObject*)self)->account;
}