Python plugin: Make it possible to get folder tree from a Mailbox
[claws.git] / src / plugins / python / clawsmailmodule.c
index d983c6e61cb21483af4ca31ea09a9a2e61317318..53e3b36622ffa5ebaead58cd755927487fa00064 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;
@@ -180,6 +204,29 @@ static gboolean setup_folderitem_node(GNode *item_node, GNode *item_parent, PyOb
   return TRUE;
 }
 
+static PyObject* get_folder_tree_from_folder(Folder *folder)
+{
+  if(folder->node) {
+    PyObject *root;
+    int n_children, i_child;
+
+    /* create root nodes */
+    root = clawsmail_node_new(cm_module);
+    if(!root)
+      return NULL;
+
+    n_children = g_node_n_children(folder->node);
+    for(i_child = 0; i_child < n_children; i_child++) {
+      if(!setup_folderitem_node(g_node_nth_child(folder->node, i_child), folder->node, &root)) {
+        Py_DECREF(root);
+        return NULL;
+      }
+    }
+    return root;
+  }
+  return NULL;
+}
+
 static PyObject* get_folder_tree_from_account_name(const char *str)
 {
   PyObject *result;
@@ -191,28 +238,19 @@ static PyObject* get_folder_tree_from_account_name(const char *str)
 
   for(walk = folder_get_list(); walk; walk = walk->next) {
     Folder *folder = walk->data;
-    if((!str || !g_strcmp0(str, folder->name)) && folder->node) {
-      PyObject *root;
-      int n_children, i_child, retval;
-
-      /* create root nodes */
-      root = clawsmail_node_new(cm_module);
-      if(!root) {
-        Py_DECREF(result);
-        return NULL;
-      }
-
-      n_children = g_node_n_children(folder->node);
-      for(i_child = 0; i_child < n_children; i_child++) {
-        if(!setup_folderitem_node(g_node_nth_child(folder->node, i_child), folder->node, &root)) {
-          Py_DECREF(root);
+    if(!str || !g_strcmp0(str, folder->name)) {
+      PyObject *tree_from_folder;
+      tree_from_folder = get_folder_tree_from_folder(folder);
+      if(tree_from_folder) {
+        int retval;
+        retval = PyList_Append(result, tree_from_folder);
+        Py_DECREF(tree_from_folder);
+        if(retval == -1) {
           Py_DECREF(result);
           return NULL;
         }
       }
-      retval = PyList_Append(result, root);
-      Py_DECREF(root);
-      if(retval == -1) {
+      else {
         Py_DECREF(result);
         return NULL;
       }
@@ -278,8 +316,11 @@ static PyObject* get_folder_tree(PyObject *self, PyObject *args)
   else if(PyObject_TypeCheck(arg, clawsmail_folder_get_type_object())) {
     result = get_folder_tree_from_folderitem(clawsmail_folder_get_item(arg));
   }
+  else if(PyObject_TypeCheck(arg, clawsmail_mailbox_get_type_object())) {
+    result = get_folder_tree_from_folder(clawsmail_mailbox_get_folder(arg));
+  }
   else {
-    PyErr_SetString(PyExc_TypeError, "Parameter must be nothing, a mailbox string or a Folder object.");
+    PyErr_SetString(PyExc_TypeError, "Parameter must be nothing, a Folder object, a Mailbox object, or a mailbox name string.");
     return NULL;
   }
 
@@ -464,6 +505,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;
@@ -625,6 +694,29 @@ static PyObject* copy_messages(PyObject *self, PyObject *args)
   return move_or_copy_messages(self, args, FALSE);
 }
 
+static PyObject* get_current_account(PyObject *self, PyObject *args)
+{
+  PrefsAccount *account;
+  account = account_get_cur_account();
+  if(account) {
+    return clawsmail_account_new(account);
+  }
+  else
+    Py_RETURN_NONE;
+}
+
+static PyObject* get_default_account(PyObject *self, PyObject *args)
+{
+  PrefsAccount *account;
+  account = account_get_default();
+  if(account) {
+    return clawsmail_account_new(account);
+  }
+  else
+    Py_RETURN_NONE;
+}
+
+
 static PyMethodDef ClawsMailMethods[] = {
     /* public */
     {"get_mainwindow_action_group",  get_mainwindow_action_group, METH_NOARGS,
@@ -642,23 +734,31 @@ static PyMethodDef ClawsMailMethods[] = {
      "\n"
      "Without arguments, get a list of folder trees for all mailboxes.\n"
      "\n"
-     "If the optional root argument is a string, it is supposed to be a\n"
-     "mailbox name. The function then returns a tree of folders of that mailbox.\n"
-     "\n"
      "If the optional root argument is a clawsmail.Folder, the function\n"
      "returns a tree of subfolders with the given folder as root element.\n"
      "\n"
+     "If the optional root argument is a clawsmail.Mailbox, the function\n"
+     "returns a tree of folders with the given mailbox as root element.\n"
+     "\n"
+     "If the optional root argument is a string, it is supposed to be a\n"
+     "mailbox name. The function then returns a tree of folders of that mailbox.\n"
+     "\n"
      "In any case, a tree consists of elements of the type clawsmail.Node."},
 
     {"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"
@@ -731,7 +831,22 @@ static PyMethodDef ClawsMailMethods[] = {
       "\n"
       "Get a tuple of Account objects representing all accounts that are defined in Claws Mail."},
 
-      /* private */
+      {"get_current_account", get_current_account, METH_NOARGS,
+       "get_current_account() - get the current account\n"
+       "\n"
+       "Return the object representing the currently selected account."},
+
+     {"get_default_account", get_default_account, METH_NOARGS,
+      "get_default_account() - get the default account\n"
+      "\n"
+      "Return the object representing the default account."},
+
+     {"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"
@@ -788,6 +903,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)