[Commits] clawsmailmodule.c 1.1.2.13 1.1.2.14 messageinfotype.c 1.1.2.10 1.1.2.11

holger at claws-mail.org holger at claws-mail.org
Sat Dec 29 01:41:32 CET 2012


Update of /home/claws-mail/plugins/python/src
In directory srv:/tmp/cvs-serv7887/src

Modified Files:
      Tag: gtk2
	clawsmailmodule.c messageinfotype.c 
Log Message:
2012-12-29 [holger]	0.11cvs3

	* src/clawsmailmodule.c
	* src/messageinfotype.c
		Add support for tags 

Index: clawsmailmodule.c
===================================================================
RCS file: /home/claws-mail/plugins/python/src/Attic/clawsmailmodule.c,v
retrieving revision 1.1.2.13
retrieving revision 1.1.2.14
diff -u -d -r1.1.2.13 -r1.1.2.14
--- clawsmailmodule.c	10 Jul 2012 13:36:54 -0000	1.1.2.13
+++ clawsmailmodule.c	29 Dec 2012 00:41:30 -0000	1.1.2.14
@@ -1,5 +1,5 @@
 /* Python plugin for Claws-Mail
- * Copyright (C) 2009 Holger Berndt
+ * Copyright (C) 2009-2012 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
@@ -35,6 +35,7 @@
 #include "quicksearch.h"
 #include "toolbar.h"
 #include "prefs_common.h"
+#include "common/tags.h"
 
 #include <glib.h>
 
@@ -398,6 +399,118 @@
     Py_RETURN_FALSE;
 }
 
+static PyObject* get_tags(PyObject *self, PyObject *args)
+{
+  Py_ssize_t num_tags;
+  PyObject *tags_tuple;
+  GSList *tags_list;
+
+  tags_list = tags_get_list();
+  num_tags = g_slist_length(tags_list);
+
+  tags_tuple = PyTuple_New(num_tags);
+  if(tags_tuple != NULL) {
+    Py_ssize_t iTag;
+    PyObject *tag_object;
+    GSList *walk;
+
+    iTag = 0;
+    for(walk = tags_list; walk; walk = walk->next) {
+      tag_object = Py_BuildValue("s", tags_get_tag(GPOINTER_TO_INT(walk->data)));
+      if(tag_object == NULL) {
+        Py_DECREF(tags_tuple);
+        return NULL;
+      }
+      PyTuple_SET_ITEM(tags_tuple, iTag++, tag_object);
+    }
+  }
+
+  g_slist_free(tags_list);
+
+  return tags_tuple;
+}
+
+static PyObject* make_sure_tag_exists(PyObject *self, PyObject *args)
+{
+  int retval;
+  const char *tag_str;
+
+  retval = PyArg_ParseTuple(args, "s", &tag_str);
+  if(!retval)
+    return NULL;
+
+  if(IS_NOT_RESERVED_TAG(tag_str) == FALSE) {
+    /* tag name is reserved, raise KeyError */
+    PyErr_SetString(PyExc_ValueError, "Tag name is reserved");
+    return NULL;
+  }
+
+  tags_add_tag(tag_str);
+
+  Py_RETURN_NONE;
+}
+
+static PyObject* delete_tag(PyObject *self, PyObject *args)
+{
+  int retval;
+  const char *tag_str;
+  gint tag_id;
+  MainWindow *mainwin;
+
+  retval = PyArg_ParseTuple(args, "s", &tag_str);
+  if(!retval)
+    return NULL;
+
+  tag_id = tags_get_id_for_str(tag_str);
+  if(tag_id == -1) {
+    PyErr_SetString(PyExc_KeyError, "Tag does not exist");
+    return NULL;
+  }
+
+  tags_remove_tag(tag_id);
+
+  /* update display */
+  mainwin = mainwindow_get_mainwindow();
+  if(mainwin)
+    summary_redisplay_msg(mainwin->summaryview);
+
+  Py_RETURN_NONE;
+}
+
+
+static PyObject* rename_tag(PyObject *self, PyObject *args)
+{
+  int retval;
+  const char *old_tag_str;
+  const char *new_tag_str;
+  gint tag_id;
+  MainWindow *mainwin;
+
+  retval = PyArg_ParseTuple(args, "ss", &old_tag_str, &new_tag_str);
+  if(!retval)
+    return NULL;
+
+  if((IS_NOT_RESERVED_TAG(new_tag_str) == FALSE) ||(IS_NOT_RESERVED_TAG(old_tag_str) == FALSE)) {
+    PyErr_SetString(PyExc_ValueError, "Tag name is reserved");
+    return NULL;
+  }
+
+  tag_id = tags_get_id_for_str(old_tag_str);
+  if(tag_id == -1) {
+    PyErr_SetString(PyExc_KeyError, "Tag does not exist");
+    return NULL;
+  }
+
+  tags_update_tag(tag_id, new_tag_str);
+
+  /* update display */
+  mainwin = mainwindow_get_mainwindow();
+  if(mainwin)
+    summary_redisplay_msg(mainwin->summaryview);
+
+  Py_RETURN_NONE;
+}
+
 static gboolean get_message_list_for_move_or_copy(PyObject *messagelist, PyObject *folder, GSList **list)
 {
   Py_ssize_t size, iEl;
@@ -554,11 +667,36 @@
      "Copy a list of clawsmail.MessageInfo objects to a target folder.\n"
      "The target_folder argument has to be a clawsmail.Folder object."},
 
+    {"get_tags", get_tags, METH_NOARGS,
+     "get_tags() - get a tuple of all tags that Claws Mail knows about\n"
+     "\n"
+     "Get a tuple of strings representing all tags that are defined in Claws Mail."},
+
+    {"make_sure_tag_exists", make_sure_tag_exists, METH_VARARGS,
+     "make_sure_tag_exists(tag) - make sure that the specified tag exists\n"
+     "\n"
+     "This function creates the given tag if it does not exist yet.\n"
+     "It is not an error if the tag already exists. In this case, this function does nothing.\n"
+     "However, if a reserved tag name is chosen, a ValueError exception is raised."},
+
+    {"delete_tag", delete_tag, METH_VARARGS,
+     "delete_tag(tag) - delete a tag\n"
+     "\n"
+     "This function deletes an existing tag.\n"
+     "Raises a KeyError exception if the tag does not exist."},
+
+    {"rename_tag", rename_tag, METH_VARARGS,
+     "rename_tag(old_tag, new_tag) - rename tag old_tag to new_tag\n"
+     "\n"
+     "This function renames an existing tag.\n"
+     "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 */
-     {"__gobj", private_wrap_gobj, METH_VARARGS,
-      "__gobj(ptr) - transforms a C GObject pointer into a PyGObject\n"
-      "\n"
-      "For internal usage only."},
+    {"__gobj", private_wrap_gobj, METH_VARARGS,
+     "__gobj(ptr) - transforms a C GObject pointer into a PyGObject\n"
+     "\n"
+     "For internal usage only."},
 
     {NULL, NULL, 0, NULL}
 };

Index: messageinfotype.c
===================================================================
RCS file: /home/claws-mail/plugins/python/src/Attic/messageinfotype.c,v
retrieving revision 1.1.2.10
retrieving revision 1.1.2.11
diff -u -d -r1.1.2.10 -r1.1.2.11
--- messageinfotype.c	10 Jul 2012 13:36:54 -0000	1.1.2.10
+++ messageinfotype.c	29 Dec 2012 00:41:30 -0000	1.1.2.11
@@ -1,5 +1,5 @@
 /* Python plugin for Claws-Mail
- * Copyright (C) 2009 Holger Berndt
+ * Copyright (C) 2009-2012 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
@@ -20,6 +20,11 @@
 #endif
 
 #include "messageinfotype.h"
+
+#include "common/tags.h"
+#include "mainwindow.h"
+#include "summaryview.h"
+
 #include <structmember.h>
 
 
@@ -112,6 +117,88 @@
   return py_boolean_return_value(MSG_IS_FORWARDED(((clawsmail_MessageInfoObject*)self)->msginfo->flags));
 }
 
+static PyObject* get_tags(PyObject *self, PyObject *args)
+{
+  GSList *tags_list;
+  Py_ssize_t num_tags;
+  PyObject *tags_tuple;
+
+  tags_list = ((clawsmail_MessageInfoObject*)self)->msginfo->tags;
+  num_tags = g_slist_length(tags_list);
+
+  tags_tuple = PyTuple_New(num_tags);
+  if(tags_tuple != NULL) {
+    Py_ssize_t iTag;
+    PyObject *tag_object;
+    GSList *walk;
+
+    iTag = 0;
+    for(walk = tags_list; walk; walk = walk->next) {
+      tag_object = Py_BuildValue("s", tags_get_tag(GPOINTER_TO_INT(walk->data)));
+      if(tag_object == NULL) {
+        Py_DECREF(tags_tuple);
+        return NULL;
+      }
+      PyTuple_SET_ITEM(tags_tuple, iTag++, tag_object);
+    }
+  }
+
+  return tags_tuple;
+}
+
+
+static PyObject* add_or_remove_tag(PyObject *self, PyObject *args, gboolean add)
+{
+  int retval;
+  const char *tag_str;
+  gint tag_id;
+  MsgInfo *msginfo;
+  MainWindow *mainwin;
+
+  retval = PyArg_ParseTuple(args, "s", &tag_str);
+  if(!retval)
+    return NULL;
+
+  tag_id = tags_get_id_for_str(tag_str);
+  if(tag_id == -1) {
+    PyErr_SetString(PyExc_ValueError, "Tag does not exist");
+    return NULL;
+  }
+
+  msginfo = ((clawsmail_MessageInfoObject*)self)->msginfo;
+
+  if(!add) {
+    /* raise KeyError if tag is not set */
+    if(!g_slist_find(msginfo->tags, GINT_TO_POINTER(tag_id))) {
+      PyErr_SetString(PyExc_KeyError, "Tag is not set on this message");
+      return NULL;
+    }
+  }
+
+  procmsg_msginfo_update_tags(msginfo, add, tag_id);
+
+  /* update display */
+  mainwin = mainwindow_get_mainwindow();
+  if(mainwin)
+    summary_redisplay_msg(mainwin->summaryview);
+
+  Py_RETURN_NONE;
+}
+
+
+
+static PyObject* add_tag(PyObject *self, PyObject *args)
+{
+  return add_or_remove_tag(self, args, TRUE);
+}
+
+
+static PyObject* remove_tag(PyObject *self, PyObject *args)
+{
+  return add_or_remove_tag(self, args, FALSE);
+}
+
+
 static PyMethodDef MessageInfo_methods[] = {
   {"is_new",  is_new, METH_NOARGS,
    "is_new() - checks if the message is new\n"
@@ -143,6 +230,23 @@
    "\n"
    "Returns True if the forwarded flag of the message is set."},
 
+  {"get_tags",  get_tags, METH_NOARGS,
+   "get_tags() - get message tags\n"
+   "\n"
+   "Returns a tuple of tags that apply to this message."},
+
+  {"add_tag",  add_tag, METH_VARARGS,
+   "add_tag(tag) - add a tag to this message\n"
+   "\n"
+   "Add a tag to this message. If the tag is already set, nothing is done.\n"
+   "If the tag does not exist, a ValueError exception is raised."},
+
+  {"remove_tag",  remove_tag, METH_VARARGS,
+   "remove_tag(tag) - remove a tag from this message\n"
+   "\n"
+   "Remove a tag from this message. If the tag is not set, a KeyError exception is raised.\n"
+   "If the tag does not exist, a ValueError exception is raised."},
+
   {NULL}
 };
 



More information about the Commits mailing list