Python plugin: Add accessor to accounts, and an Account object
authorHolger Berndt <hb@claws-mail.org>
Sun, 21 Jul 2013 12:10:46 +0000 (14:10 +0200)
committerHolger Berndt <hb@claws-mail.org>
Sat, 27 Jul 2013 14:50:09 +0000 (16:50 +0200)
src/plugins/python/Makefile.am
src/plugins/python/accounttype.c [new file with mode: 0644]
src/plugins/python/accounttype.h [new file with mode: 0644]
src/plugins/python/clawsmailmodule.c

index bee41ec581878ef4302ad88f9c7c9ce9d7bfa18a..bb213fc734d19bfc60839a4ea257728d5112d056 100644 (file)
@@ -7,6 +7,8 @@ plugin_LTLIBRARIES = python.la
 endif
 
 python_la_SOURCES = \
+       accounttype.c \
+       accounttype.h \
        clawsmailmodule.c \
        clawsmailmodule.h \
        composewindowtype.c \
diff --git a/src/plugins/python/accounttype.c b/src/plugins/python/accounttype.c
new file mode 100644 (file)
index 0000000..2163cf3
--- /dev/null
@@ -0,0 +1,160 @@
+/* Python plugin for Claws-Mail
+ * Copyright (C) 2013 Holger Berndt <hb@claws-mail.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#  include "claws-features.h"
+#endif
+
+#include "accounttype.h"
+
+#include <structmember.h>
+
+typedef struct {
+    PyObject_HEAD
+    PyObject *account_name;
+    PrefsAccount *account;
+} clawsmail_AccountObject;
+
+static int Account_init(clawsmail_AccountObject *self, PyObject *args, PyObject *kwds)
+{
+  Py_INCREF(Py_None);
+  self->account_name = Py_None;
+
+  self->account = NULL;
+  return 0;
+}
+
+
+static void Account_dealloc(clawsmail_AccountObject* self)
+{
+  Py_XDECREF(self->account_name);
+
+  self->ob_type->tp_free((PyObject*)self);
+}
+
+static PyObject* Account_str(PyObject *self)
+{
+  PyObject *str;
+  str = PyString_FromString("Account: ");
+  if(str == NULL)
+    return NULL;
+  PyString_ConcatAndDel(&str, PyObject_GetAttrString(self, "account_name"));
+
+  return str;
+}
+
+static PyMethodDef Account_methods[] = {
+    {NULL}
+};
+
+static PyMemberDef Account_members[] = {
+    {"account_name", T_OBJECT_EX, offsetof(clawsmail_AccountObject, account_name), 0,
+     "account name - name of the corresponding account"},
+
+    {NULL}
+};
+
+static PyTypeObject clawsmail_AccountType = {
+    PyObject_HEAD_INIT(NULL)
+    0,                         /* ob_size*/
+    "clawsmail.Account",       /* tp_name*/
+    sizeof(clawsmail_AccountObject), /* tp_basicsize*/
+    0,                         /* tp_itemsize*/
+    (destructor)Account_dealloc, /* tp_dealloc*/
+    0,                         /* tp_print*/
+    0,                         /* tp_getattr*/
+    0,                         /* tp_setattr*/
+    0,                         /* 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*/
+    0,                         /* tp_getattro*/
+    0,                         /* tp_setattro*/
+    0,                         /* tp_as_buffer*/
+    Py_TPFLAGS_DEFAULT,        /* tp_flags*/
+    "Account objects.\n\n"     /* tp_doc */
+    "Do not construct objects of this type yourself.",
+    0,                         /* tp_traverse */
+    0,                         /* tp_clear */
+    0,                         /* tp_richcompare */
+    0,                         /* tp_weaklistoffset */
+    0,                         /* tp_iter */
+    0,                         /* tp_iternext */
+    Account_methods,           /* tp_methods */
+    Account_members,           /* tp_members */
+    0,                         /* tp_getset */
+    0,                         /* tp_base */
+    0,                         /* tp_dict */
+    0,                         /* tp_descr_get */
+    0,                         /* tp_descr_set */
+    0,                         /* tp_dictoffset */
+    (initproc)Account_init,    /* tp_init */
+    0,                         /* tp_alloc */
+    0,                         /* tp_new */
+};
+
+
+gboolean cmpy_add_account(PyObject *module)
+{
+  clawsmail_AccountType.tp_new = PyType_GenericNew;
+  if(PyType_Ready(&clawsmail_AccountType) < 0)
+    return FALSE;
+
+  Py_INCREF(&clawsmail_AccountType);
+  return (PyModule_AddObject(module, "Account", (PyObject*)&clawsmail_AccountType) == 0);
+}
+
+static gboolean update_members(clawsmail_AccountObject *self, PrefsAccount *account)
+{
+  PyObject *str;
+
+  str = PyString_FromString(account->account_name);
+  if(!str)
+    goto err;
+  self->account_name = str;
+
+  self->account = account;
+
+  return TRUE;
+err:
+  Py_XDECREF(self->account_name);
+  return FALSE;
+}
+
+PyObject* clawsmail_account_new(PrefsAccount *account)
+{
+  clawsmail_AccountObject *ff;
+
+  if(!account)
+    return NULL;
+
+  ff = (clawsmail_AccountObject*) PyObject_CallObject((PyObject*) &clawsmail_AccountType, NULL);
+  if(!ff)
+    return NULL;
+
+  if(update_members(ff, account))
+    return (PyObject*)ff;
+  else {
+    Py_XDECREF(ff);
+    return NULL;
+  }
+}
diff --git a/src/plugins/python/accounttype.h b/src/plugins/python/accounttype.h
new file mode 100644 (file)
index 0000000..808faba
--- /dev/null
@@ -0,0 +1,31 @@
+/* Python plugin for Claws-Mail
+ * Copyright (C) 2013 Holger Berndt <hb@claws-mail.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef ACCOUNTTYPE_H
+#define ACCOUNTTYPE_H
+
+#include <glib.h>
+#include <Python.h>
+
+#include "account.h"
+
+gboolean cmpy_add_account(PyObject *module);
+
+PyObject* clawsmail_account_new(PrefsAccount *account);
+
+
+#endif /* ACCOUNTTYPE_H */
index 0efc52e8b767dcc87739f56de7685cbffad55d2b..e21f28053f12fad38f24ff238557c9c041bc02f8 100644 (file)
@@ -29,6 +29,7 @@
 #include "composewindowtype.h"
 #include "foldertype.h"
 #include "messageinfotype.h"
+#include "accounttype.h"
 
 #include <pygobject.h>
 #include <pygtk/pygtk.h>
@@ -40,6 +41,7 @@
 #include "toolbar.h"
 #include "prefs_common.h"
 #include "common/tags.h"
+#include "account.h"
 
 #include <glib.h>
 
@@ -434,6 +436,33 @@ static PyObject* get_tags(PyObject *self, PyObject *args)
   return tags_tuple;
 }
 
+static PyObject* get_accounts(PyObject *self, PyObject *args)
+{
+  PyObject *accounts_tuple;
+  GList *accounts_list;
+  GList *walk;
+
+  accounts_list = account_get_list();
+
+  accounts_tuple = PyTuple_New(g_list_length(accounts_list));
+  if(accounts_tuple) {
+    PyObject *account_object;
+    Py_ssize_t iAccount;
+
+    iAccount = 0;
+    for(walk = accounts_list; walk; walk = walk->next) {
+      account_object = clawsmail_account_new(walk->data);
+      if(account_object == NULL) {
+        Py_DECREF(accounts_tuple);
+        return NULL;
+      }
+      PyTuple_SET_ITEM(accounts_tuple, iAccount++, account_object);
+    }
+  }
+
+  return accounts_tuple;
+}
+
 static PyObject* make_sure_tag_exists(PyObject *self, PyObject *args)
 {
   int retval;
@@ -696,7 +725,12 @@ static PyMethodDef ClawsMailMethods[] = {
      "Raises a KeyError exception if the tag does not exist.\n"
      "Raises a ValueError exception if the old or new tag name is a reserved name."},
 
-     /* private */
+     {"get_accounts", get_accounts, METH_NOARGS,
+      "get_accounts() - get a tuple of all accounts that Claws Mail knows about\n"
+      "\n"
+      "Get a tuple of Account objects representing all accounts that are defined in Claws Mail."},
+
+      /* private */
     {"__gobj", private_wrap_gobj, METH_VARARGS,
      "__gobj(ptr) - transforms a C GObject pointer into a PyGObject\n"
      "\n"
@@ -751,6 +785,7 @@ PyMODINIT_FUNC initclawsmail(void)
   ok = ok && cmpy_add_composewindow(cm_module);
   ok = ok && cmpy_add_folder(cm_module);
   ok = ok && cmpy_add_messageinfo(cm_module);
+  ok = ok && cmpy_add_account(cm_module);
 
   /* initialize misc things */
   if(ok)