[claws-mail-cvs-commit] Makefile.am

Holger Berndt <[email protected]> Wed, 19 Aug 2009 16:49:28 +0000
Newsgroups gmane.mail.sylpheed.claws.cvs
Message-ID <[email protected]>
Update of /srv/cvs/claws-mail/plugins/python/src
In directory ran:/tmp/cvs-serv9552/src

Modified Files:
      Tag: gtk2
	Makefile.am clawsmailmodule.c 
Added Files:
      Tag: gtk2
	foldertype.c foldertype.h nodetype.c nodetype.h 
Log Message:
2009-08-19 [holger]	0.1cvs7

	* .cvsignore
		more files to ignore
	* README
	* src/Makefile.am
	* src/clawsmailmodule.c
	* src/foldertype.c
	* src/foldertype.h
	* src/nodetype.c
	* src/nodetype.h
		Implement a tree widget, and
		provide access to the folderview 

Index: clawsmailmodule.c
===================================================================
RCS file: /srv/cvs/claws-mail/plugins/python/src/Attic/clawsmailmodule.c,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -u -d -r1.1.2.1 -r1.1.2.2
--- clawsmailmodule.c	9 Aug 2009 14:43:37 -0000	1.1.2.1
+++ clawsmailmodule.c	19 Aug 2009 16:49:25 -0000	1.1.2.2
@@ -22,7 +22,9 @@
 #endif
 #include "pluginconfig.h"
 
+#include "nodetype.h"
 #include "composewindowtype.h"
+#include "foldertype.h"
 
 #include <pygobject.h>
 #include <pygtk/pygtk.h>
@@ -30,6 +32,8 @@
 #include "mainwindow.h"
 #include "toolbar.h"
 
+#include <glib.h>
+
 static PyObject *cm_module = NULL;
 
 PyObject* get_gobj_from_address(gpointer addr)
@@ -68,11 +72,204 @@
     return NULL;
 }
 
+static PyObject *get_folderview_selected_folder(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)
+      return clawsmail_folder_new(item);
+  }
+  Py_INCREF(Py_None);
+  return Py_None;
+}
+
+static PyObject *folderview_select_folder(PyObject *self, PyObject *args)
+{
+  MainWindow *mainwin;
+
+  mainwin =  mainwindow_get_mainwindow();
+  if(mainwin && mainwin->folderview) {
+    FolderItem *item;
+    PyObject *folder;
+    folder = PyTuple_GetItem(args, 0);
+    if(!folder)
+      return NULL;
+    Py_INCREF(folder);
+    item = clawsmail_folder_get_item(folder);
+    Py_DECREF(folder);
+    if(item)
+      folderview_select(mainwin->folderview, item);
+  }
+  Py_INCREF(Py_None);
+  return Py_None;
+}
+
+static gboolean setup_folderitem_node(GNode *item_node, GNode *item_parent, PyObject **pyparent)
+{
+  PyObject *pynode, *children;
+  int retval, n_children, i_child;
+  PyObject *folder;
+
+  /* create a python node for the folderitem node */
+  pynode = clawsmail_node_new(cm_module);
+  if(!pynode)
+    return FALSE;
+
+  /* store Folder in pynode */
+  folder = clawsmail_folder_new(item_node->data);
+  retval = PyObject_SetAttrString(pynode, "data", folder);
+  Py_DECREF(folder);
+  if(retval == -1) {
+    Py_DECREF(pynode);
+    return FALSE;
+  }
+
+  if(pyparent && *pyparent) {
+    /* add this node to the parent's childs */
+    children = PyObject_GetAttrString(*pyparent, "children");
+    retval = PyList_Append(children, pynode);
+    Py_DECREF(children);
+
+    if(retval == -1) {
+      Py_DECREF(pynode);
+      return FALSE;
+    }
+  }
+  else if(pyparent) {
+    *pyparent = pynode;
+    Py_INCREF(pynode);
+  }
+
+  /* call this function recursively for all children of the new node */
+  n_children = g_node_n_children(item_node);
+  for(i_child = 0; i_child < n_children; i_child++) {
+    if(!setup_folderitem_node(g_node_nth_child(item_node, i_child), item_node, &pynode)) {
+      Py_DECREF(pynode);
+      return FALSE;
+    }
+  }
+
+  Py_DECREF(pynode);
+  return TRUE;
+}
+
+static PyObject* get_folder_tree_from_account_name(const char *str)
+{
+  PyObject *result;
+  GList *walk;
+
+  result = Py_BuildValue("[]");
+  if(!result)
+    return NULL;
+
+  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);
+          Py_DECREF(result);
+          return NULL;
+        }
+      }
+      retval = PyList_Append(result, root);
+      Py_DECREF(root);
+      if(retval == -1) {
+        Py_DECREF(result);
+        return NULL;
+      }
+    }
+  }
+  return result;
+}
+
+static PyObject* get_folder_tree_from_folderitem(FolderItem *item)
+{
+  PyObject *result;
+  GList *walk;
+
+  for(walk = folder_get_list(); walk; walk = walk->next) {
+    Folder *folder = walk->data;
+    if(folder->node) {
+      GNode *root_node;
+
+      root_node = g_node_find(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, item);
+      if(!root_node)
+        continue;
+
+      result = NULL;
+      if(!setup_folderitem_node(root_node, NULL, &result))
+        return NULL;
+      else
+        return result;
+    }
+  }
+
+  PyErr_SetString(PyExc_LookupError, "Folder not found");
+  return NULL;
+}
+
+static PyObject* get_folder_tree(PyObject *self, PyObject *args)
+{
+  PyObject *arg;
+  PyObject *result;
+  int retval;
+
+  Py_INCREF(Py_None);
+  arg = Py_None;
+  retval = PyArg_ParseTuple(args, "|O", &arg);
+  Py_DECREF(Py_None);
+  if(!retval)
+    return NULL;
+
+  /* calling possibilities:
+   * nothing, the mailbox name in a string, a Folder object */
+
+  /* no arguments: build up a list of folder trees */
+  if(PyTuple_Size(args) == 0) {
+    result = get_folder_tree_from_account_name(NULL);
+  }
+  else if(PyString_Check(arg)){
+    const char *str;
+    str = PyString_AsString(arg);
+    if(!str)
+      return NULL;
+
+    result = get_folder_tree_from_account_name(str);
+  }
+  else if(PyObject_TypeCheck(arg, clawsmail_folder_get_type_object())) {
+    result = get_folder_tree_from_folderitem(clawsmail_folder_get_item(arg));
+  }
+  else {
+    PyErr_SetString(PyExc_TypeError, "Parameter must be nothing, a mailbox string or a Folder object.");
+    return NULL;
+  }
+
+  return result;
+}
+
 
 static PyMethodDef ClawsMailMethods[] = {
     /* public */
-    {"get_mainwindow_action_group",  get_mainwindow_action_group, METH_VARARGS,
-     "Get action group of main window menu."},
+    {"get_mainwindow_action_group",  get_mainwindow_action_group, METH_NOARGS, "Get action group of main window menu."},
+    {"get_folder_tree",  get_folder_tree, METH_VARARGS, "Get folder tree."},
+    {"get_folderview_selected_folder",  get_folderview_selected_folder, METH_NOARGS, "Get selected folder in folderview."},
+    {"folderview_select_folder",  folderview_select_folder, METH_VARARGS, "Select folder in folderview. Takes an argument of type Folder."},
 
      /* private */
      {"__gobj", private_wrap_gobj, METH_VARARGS, "Wraps a C GObject"},
@@ -89,8 +286,9 @@
   cm_module = Py_InitModule("clawsmail", ClawsMailMethods);
 
   /* initialize classes */
-  //initComposeWindow();
+  initnode(cm_module);
   initcomposewindow(cm_module);
+  initfolder(cm_module);
 
   PyRun_SimpleString("import clawsmail\n");
 }

--- NEW FILE: foldertype.h ---
/* Python plugin for Claws-Mail
 * Copyright (C) 2009 Holger Berndt
 *
 * 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 FOLDERTYPE_H
#define FOLDERTYPE_H

#include <Python.h>

#include "folder.h"

#ifndef PyMODINIT_FUNC  /* declarations for DLL import/export */
#define PyMODINIT_FUNC void
#endif

PyMODINIT_FUNC initfolder(PyObject *module);

PyObject* clawsmail_folder_new(FolderItem *folderitem);
FolderItem* clawsmail_folder_get_item(PyObject *self);
PyTypeObject* clawsmail_folder_get_type_object();

#endif /* FOLDERTYPE_H */

Index: Makefile.am
===================================================================
RCS file: /srv/cvs/claws-mail/plugins/python/src/Attic/Makefile.am,v
retrieving revision 1.1.2.2
retrieving revision 1.1.2.3
diff -u -d -r1.1.2.2 -r1.1.2.3
--- Makefile.am	19 Aug 2009 16:11:42 -0000	1.1.2.2
+++ Makefile.am	19 Aug 2009 16:49:25 -0000	1.1.2.3
@@ -7,6 +7,10 @@
 	clawsmailmodule.h \
 	composewindowtype.c \
 	composewindowtype.h \
+	foldertype.c \
+	foldertype.h \
+	nodetype.c \
+	nodetype.h \
 	python_plugin.c \
 	python-hooks.c \
 	python-hooks.h \

--- NEW FILE: nodetype.c ---
/* Python plugin for Claws-Mail
 * Copyright (C) 2009 Holger Berndt
 *
 * 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"
#endif
#include "pluginconfig.h"

#include "nodetype.h"

#include <structmember.h>

PyMODINIT_FUNC initnode(PyObject *module)
{
  PyObject *dict;
  PyObject *res;
  const char *cmd =
      "class Node:\n"
      "    def __init__(self):\n"
      "        self.data = None\n"
      "        self.children = []\n"
      "\n"
      "    def __str__(self):\n"
      "        return '\\n'.join(self.get_str_list(0))\n"
      "\n"
      "    def get_str_list(self, level):\n"
      "        str = []\n"
      "        indent = '  '*level\n"
      "        if self.data:\n"
      "            str.append(indent + self.data.__str__())\n"
      "        else:\n"
      "            str.append(indent + 'None')\n"
      "        for child in self.children:\n"
      "            str.extend(child.get_str_list(level+1))\n"
      "        return str\n"
      "\n";

  dict = PyModule_GetDict(module);
  res = PyRun_String(cmd, Py_file_input, dict, dict);
  Py_XDECREF(res);
}

PyObject* clawsmail_node_new(PyObject *module)
{
  PyObject *class, *dict;

  dict = PyModule_GetDict(module);
  class = PyDict_GetItemString(dict, "Node");
  return PyObject_CallObject(class, NULL);
}

--- NEW FILE: foldertype.c ---
/* Python plugin for Claws-Mail
 * Copyright (C) 2009 Holger Berndt
 *
 * 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"
#endif
#include "pluginconfig.h"

#include "foldertype.h"

#include <structmember.h>


typedef struct {
    PyObject_HEAD
    PyObject *name;
    PyObject *path;
    FolderItem *folderitem;
} clawsmail_FolderObject;


static void Folder_dealloc(clawsmail_FolderObject* self)
{
  Py_XDECREF(self->name);
  Py_XDECREF(self->path);
  self->ob_type->tp_free((PyObject*)self);
}

static int Folder_init(clawsmail_FolderObject *self, PyObject *args, PyObject *kwds)
{
  Py_INCREF(Py_None);
  self->name = Py_None;

  Py_INCREF(Py_None);
  self->path = Py_None;

  return 0;
}

static PyObject* Folder_str(PyObject *self)
{
  PyObject *str;
  str = PyString_FromString("Folder: ");
  PyString_ConcatAndDel(&str, PyObject_GetAttrString(self, "name"));
  return str;
}

static PyMethodDef Folder_methods[] = {
    {NULL}
};

static PyMemberDef Folder_members[] = {
    {"name", T_OBJECT_EX, offsetof(clawsmail_FolderObject, name), 0, "name of folder"},
    {"path", T_OBJECT_EX, offsetof(clawsmail_FolderObject, path), 0, "path of folder"},
    {NULL}
};

static PyTypeObject clawsmail_FolderType = {
    PyObject_HEAD_INIT(NULL)
    0,                         /* ob_size*/
    "clawsmail.Folder",        /* tp_name*/
    sizeof(clawsmail_FolderObject), /* tp_basicsize*/
    0,                         /* tp_itemsize*/
    (destructor)Folder_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*/
    Folder_str,                /* tp_str*/
    0,                         /* tp_getattro*/
    0,                         /* tp_setattro*/
    0,                         /* tp_as_buffer*/
    Py_TPFLAGS_DEFAULT,        /* tp_flags*/
    "Folder objects.",         /* tp_doc */
    0,                         /* tp_traverse */
    0,                         /* tp_clear */
    0,                         /* tp_richcompare */
    0,                         /* tp_weaklistoffset */
    0,                         /* tp_iter */
    0,                         /* tp_iternext */
    Folder_methods,            /* tp_methods */
    Folder_members,            /* tp_members */
    0,                         /* tp_getset */
    0,                         /* tp_base */
    0,                         /* tp_dict */
    0,                         /* tp_descr_get */
    0,                         /* tp_descr_set */
    0,                         /* tp_dictoffset */
    (initproc)Folder_init,     /* tp_init */
    0,                         /* tp_alloc */
    0,                         /* tp_new */
};

PyMODINIT_FUNC initfolder(PyObject *module)
{
    clawsmail_FolderType.tp_new = PyType_GenericNew;
    if(PyType_Ready(&clawsmail_FolderType) < 0)
        return;

    Py_INCREF(&clawsmail_FolderType);
    PyModule_AddObject(module, "Folder", (PyObject*)&clawsmail_FolderType);
}

#define FOLDERITEM_STRING_TO_PYTHON_FOLDER_MEMBER(fis, pms)       \
  do {                                                            \
    if(fis) {                                                     \
      PyObject *str;                                              \
      str = PyString_FromString(fis);                             \
      if(str) {                                                   \
        int retval;                                               \
        retval = PyObject_SetAttrString((PyObject*)ff, pms, str); \
        Py_DECREF(str);                                           \
        if(retval == -1)                                          \
          goto err;                                               \
      }                                                           \
    }                                                             \
  } while(0)

PyObject* clawsmail_folder_new(FolderItem *folderitem)
{
  clawsmail_FolderObject *ff;

  if(!folderitem)
    return NULL;

  ff = (clawsmail_FolderObject*) PyObject_CallObject((PyObject*) &clawsmail_FolderType, NULL);
  if(!ff)
    return NULL;

  FOLDERITEM_STRING_TO_PYTHON_FOLDER_MEMBER(folderitem->name, "name");
  FOLDERITEM_STRING_TO_PYTHON_FOLDER_MEMBER(folderitem->path, "path");

  ff->folderitem = folderitem;
  return (PyObject*)ff;

err:
  Py_XDECREF(ff);
  return NULL;
}

FolderItem* clawsmail_folder_get_item(PyObject *self)
{
  return ((clawsmail_FolderObject*)self)->folderitem;
}

PyTypeObject* clawsmail_folder_get_type_object()
{
  return &clawsmail_FolderType;
}

--- NEW FILE: nodetype.h ---
/* Python plugin for Claws-Mail
 * Copyright (C) 2009 Holger Berndt
 *
 * 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 NODETYPE_H
#define NODETYPE_H

#include <Python.h>

#ifndef PyMODINIT_FUNC  /* declarations for DLL import/export */
#define PyMODINIT_FUNC void
#endif

PyMODINIT_FUNC initnode(PyObject *module);

PyObject* clawsmail_node_new(PyObject *module);

#endif /* NODETYPE_H */