Python plugin: Add Mailbox type
authorHolger Berndt <hb@claws-mail.org>
Sat, 10 Aug 2013 12:56:28 +0000 (14:56 +0200)
committerHolger Berndt <hb@claws-mail.org>
Sun, 18 Aug 2013 12:32:50 +0000 (14:32 +0200)
src/plugins/python/Makefile.am
src/plugins/python/clawsmailmodule.c
src/plugins/python/mailboxtype.c [new file with mode: 0644]
src/plugins/python/mailboxtype.h [new file with mode: 0644]

index 4f72a91..dcade0b 100644 (file)
@@ -17,6 +17,8 @@ python_la_SOURCES = \
        folderpropertiestype.h \
        foldertype.c \
        foldertype.h \
+       mailboxtype.c \
+       mailboxtype.h \
        messageinfotype.c \
        messageinfotype.h \
        nodetype.c \
index ec1f223..6323e25 100644 (file)
@@ -31,6 +31,7 @@
 #include "foldertype.h"
 #include "messageinfotype.h"
 #include "accounttype.h"
+#include "mailboxtype.h"
 
 #include <pygobject.h>
 #include <pygtk/pygtk.h>
@@ -106,10 +107,33 @@ static PyObject *get_folderview_selected_folder(PyObject *self, PyObject *args)
     if(item)
       return clawsmail_folder_new(item);
   }
-  Py_INCREF(Py_None);
-  return Py_None;
+  Py_RETURN_NONE;
+}
+
+static PyObject *get_folderview_selected_mailbox(PyObject *self, PyObject *args)
+{
+  MainWindow *mainwin;
+
+  mainwin =  mainwindow_get_mainwindow();
+  if(mainwin && mainwin->folderview) {
+    FolderItem *item;
+    item = folderview_get_selected_item(mainwin->folderview);
+    if(item) {
+      gchar *id;
+      id = folder_item_get_identifier(item);
+      /* If there is an id, it's a folder, not a mailbox */
+      if(id) {
+        g_free(id);
+        Py_RETURN_NONE;
+      }
+      else
+        return clawsmail_mailbox_new(item->folder);
+    }
+  }
+  Py_RETURN_NONE;
 }
 
+
 static PyObject *folderview_select_folder(PyObject *self, PyObject *args)
 {
   MainWindow *mainwin;
@@ -464,6 +488,34 @@ static PyObject* get_accounts(PyObject *self, PyObject *args)
   return accounts_tuple;
 }
 
+static PyObject* get_mailboxes(PyObject *self, PyObject *args)
+{
+  PyObject *mailboxes_tuple;
+  GList *mailboxes_list;
+  GList *walk;
+
+  mailboxes_list = folder_get_list();
+
+  mailboxes_tuple = PyTuple_New(g_list_length(mailboxes_list));
+  if(mailboxes_tuple) {
+    PyObject *mailbox_object;
+    Py_ssize_t iMailbox;
+
+    iMailbox = 0;
+    for(walk = mailboxes_list; walk; walk = walk->next) {
+      mailbox_object = clawsmail_mailbox_new(walk->data);
+      if(mailbox_object == NULL) {
+        Py_DECREF(mailboxes_tuple);
+        return NULL;
+      }
+      PyTuple_SET_ITEM(mailboxes_tuple, iMailbox++, mailbox_object);
+    }
+  }
+
+  return mailboxes_tuple;
+}
+
+
 static PyObject* make_sure_tag_exists(PyObject *self, PyObject *args)
 {
   int retval;
@@ -676,12 +728,17 @@ static PyMethodDef ClawsMailMethods[] = {
     {"get_folderview_selected_folder",  get_folderview_selected_folder, METH_NOARGS,
      "get_folderview_selected_folder() - get selected folder in folderview\n"
      "\n"
-     "Returns the currently selected folder as a clawsmail.Folder."},
+     "Returns the currently selected folder as a clawsmail.Folder or None if no folder is selected."},
     {"folderview_select_folder",  folderview_select_folder, METH_VARARGS,
      "folderview_select_folder(folder) - select folder in folderview\n"
      "\n"
      "Takes an argument of type clawsmail.Folder, and selects the corresponding folder."},
 
+    {"get_folderview_selected_mailbox",  get_folderview_selected_mailbox, METH_NOARGS,
+     "get_folderview_selected_mailbox() - get selected mailbox in folderview\n"
+     "\n"
+     "Returns the currently selected mailbox as a clawsmail.Mailbox or None if no mailbox is selected."},
+
     {"quicksearch_search", quicksearch_search, METH_VARARGS,
      "quicksearch_search(string [, type]) - perform a quicksearch\n"
      "\n"
@@ -764,7 +821,12 @@ static PyMethodDef ClawsMailMethods[] = {
       "\n"
       "Return the object representing the default account."},
 
-      /* private */
+     {"get_mailboxes", get_mailboxes, METH_NOARGS,
+      "get_mailboxes() - get a tuple of all mailboxes that Claws Mail knows about\n"
+      "\n"
+      "Get a tuple of Mailbox objects representing all mailboxes 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"
@@ -821,6 +883,7 @@ PyMODINIT_FUNC initclawsmail(void)
   ok = ok && cmpy_add_messageinfo(cm_module);
   ok = ok && cmpy_add_account(cm_module);
   ok = ok && cmpy_add_folderproperties(cm_module);
+  ok = ok && cmpy_add_mailbox(cm_module);
 
   /* initialize misc things */
   if(ok)
diff --git a/src/plugins/python/mailboxtype.c b/src/plugins/python/mailboxtype.c
new file mode 100644 (file)
index 0000000..ffb42f0
--- /dev/null
@@ -0,0 +1,135 @@
+/* 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 <glib.h>
+#include <glib/gi18n.h>
+
+#include "mailboxtype.h"
+
+#include <structmember.h>
+
+typedef struct {
+    PyObject_HEAD
+    Folder *folder;
+} clawsmail_MailboxObject;
+
+static int Mailbox_init(clawsmail_MailboxObject *self, PyObject *args, PyObject *kwds)
+{
+  self->folder = NULL;
+  return 0;
+}
+
+static void Mailbox_dealloc(clawsmail_MailboxObject* self)
+{
+  self->folder = NULL;
+  self->ob_type->tp_free((PyObject*)self);
+}
+
+static PyObject* Mailbox_str(clawsmail_MailboxObject *self)
+{
+  if(self->folder && self->folder->name)
+    return PyString_FromFormat("Mailbox: %s", self->folder->name);
+  Py_RETURN_NONE;
+}
+
+static PyObject* get_name(clawsmail_MailboxObject *self, void *closure)
+{
+  if(self->folder && self->folder->name)
+    return PyString_FromString(self->folder->name);
+  Py_RETURN_NONE;
+}
+
+static PyGetSetDef Mailbox_getset[] = {
+   {"name", (getter)get_name, (setter)NULL,
+    "name - name of the mailbox", NULL},
+
+   {NULL}
+};
+
+static PyTypeObject clawsmail_MailboxType = {
+    PyObject_HEAD_INIT(NULL)
+    0,                         /* ob_size*/
+    "clawsmail.Mailbox",       /* tp_name*/
+    sizeof(clawsmail_MailboxObject), /* tp_basicsize*/
+    0,                         /* tp_itemsize*/
+    (destructor)Mailbox_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*/
+    (reprfunc)Mailbox_str,     /* tp_str*/
+    0,                         /* tp_getattro*/
+    0,                         /* tp_setattro*/
+    0,                         /* tp_as_buffer*/
+    Py_TPFLAGS_DEFAULT,        /* tp_flags*/
+    "Mailbox 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 */
+    0,                         /* tp_methods */
+    0,                         /* tp_members */
+    Mailbox_getset,            /* tp_getset */
+    0,                         /* tp_base */
+    0,                         /* tp_dict */
+    0,                         /* tp_descr_get */
+    0,                         /* tp_descr_set */
+    0,                         /* tp_dictoffset */
+    (initproc)Mailbox_init,    /* tp_init */
+    0,                         /* tp_alloc */
+    0,                         /* tp_new */
+};
+
+
+gboolean cmpy_add_mailbox(PyObject *module)
+{
+  clawsmail_MailboxType.tp_new = PyType_GenericNew;
+  if(PyType_Ready(&clawsmail_MailboxType) < 0)
+    return FALSE;
+
+  Py_INCREF(&clawsmail_MailboxType);
+  return (PyModule_AddObject(module, "Mailbox", (PyObject*)&clawsmail_MailboxType) == 0);
+}
+
+PyObject* clawsmail_mailbox_new(Folder *folder)
+{
+  clawsmail_MailboxObject *ff;
+
+  if(!folder)
+    return NULL;
+
+  ff = (clawsmail_MailboxObject*) PyObject_CallObject((PyObject*) &clawsmail_MailboxType, NULL);
+  if(!ff)
+    return NULL;
+
+  ff->folder = folder;
+  return (PyObject*)ff;
+}
diff --git a/src/plugins/python/mailboxtype.h b/src/plugins/python/mailboxtype.h
new file mode 100644 (file)
index 0000000..61337f1
--- /dev/null
@@ -0,0 +1,30 @@
+/* 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 MAILBOXTYPE_H
+#define MAILBOXTYPE_H
+
+#include <glib.h>
+#include <Python.h>
+
+#include "folder.h"
+
+gboolean cmpy_add_mailbox(PyObject *module);
+
+PyObject* clawsmail_mailbox_new(Folder *folder);
+
+#endif /* MAILBOXTYPE_H */