1 /* Python plugin for Claws-Mail
2 * Copyright (C) 2009 Holger Berndt
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "claws-features.h"
23 #include <glib/gi18n.h>
25 #include "foldertype.h"
26 #include "folderpropertiestype.h"
27 #include "messageinfotype.h"
29 #include <structmember.h>
36 PyObject *mailbox_name;
38 FolderItem *folderitem;
39 } clawsmail_FolderObject;
42 static void Folder_dealloc(clawsmail_FolderObject* self)
44 Py_XDECREF(self->name);
45 Py_XDECREF(self->path);
46 Py_XDECREF(self->mailbox_name);
47 Py_XDECREF(self->properties);
48 self->ob_type->tp_free((PyObject*)self);
51 #define FOLDERITEM_STRING_TO_PYTHON_FOLDER_MEMBER(self,fis, pms) \
55 str = PyString_FromString(fis); \
58 retval = PyObject_SetAttrString((PyObject*)self, pms, str); \
66 static int Folder_init(clawsmail_FolderObject *self, PyObject *args, PyObject *kwds)
68 const char *ss = NULL;
69 FolderItem *folderitem = NULL;
72 /* optional constructor argument: folderitem id string */
73 if(!PyArg_ParseTuple(args, "|sb", &ss, &create))
83 self->mailbox_name = Py_None;
87 folderitem = folder_find_item_from_identifier(ss);
89 PyErr_SetString(PyExc_ValueError, "A folder with that path does not exist, and the create parameter was False.");
94 folderitem = folder_get_item_from_identifier(ss);
96 PyErr_SetString(PyExc_IOError, "A folder with that path does not exist, and could not be created.");
103 FOLDERITEM_STRING_TO_PYTHON_FOLDER_MEMBER(self, folderitem->name, "name");
104 FOLDERITEM_STRING_TO_PYTHON_FOLDER_MEMBER(self, folderitem->path, "path");
105 FOLDERITEM_STRING_TO_PYTHON_FOLDER_MEMBER(self, folderitem->folder->name, "mailbox_name");
106 self->folderitem = folderitem;
107 self->properties = clawsmail_folderproperties_new(folderitem->prefs);
111 self->properties = Py_None;
120 static PyObject* Folder_str(PyObject *self)
123 str = PyString_FromString("Folder: ");
124 PyString_ConcatAndDel(&str, PyObject_GetAttrString(self, "name"));
128 static PyObject* Folder_get_identifier(clawsmail_FolderObject *self, PyObject *args)
132 if(!self->folderitem)
134 id = folder_item_get_identifier(self->folderitem);
135 obj = Py_BuildValue("s", id);
140 static PyObject* Folder_get_messages(clawsmail_FolderObject *self, PyObject *args)
142 GSList *msglist, *walk;
146 if(!self->folderitem)
149 msglist = folder_item_get_msg_list(self->folderitem);
150 retval = PyTuple_New(g_slist_length(msglist));
152 procmsg_msg_list_free(msglist);
157 for(pos = 0, walk = msglist; walk; walk = walk->next, ++pos) {
159 msg = clawsmail_messageinfo_new(walk->data);
160 PyTuple_SET_ITEM(retval, pos, msg);
162 procmsg_msg_list_free(msglist);
167 static PyObject* get_properties(clawsmail_FolderObject *self, void *closure)
169 Py_INCREF(self->properties);
170 return self->properties;
173 static PyMethodDef Folder_methods[] = {
174 {"get_identifier", (PyCFunction)Folder_get_identifier, METH_NOARGS,
175 "get_identifier() - get identifier\n"
177 "Get identifier for folder as a string (e.g. #mh/foo/bar)."},
178 {"get_messages", (PyCFunction)Folder_get_messages, METH_NOARGS,
179 "get_messages() - get a tuple of messages in folder\n"
181 "Get a tuple of MessageInfos for the folder."},
185 static PyMemberDef Folder_members[] = {
186 {"name", T_OBJECT_EX, offsetof(clawsmail_FolderObject, name), 0,
187 "name - name of folder"},
189 {"path", T_OBJECT_EX, offsetof(clawsmail_FolderObject, path), 0,
190 "path - path of folder"},
192 {"mailbox_name", T_OBJECT_EX, offsetof(clawsmail_FolderObject, mailbox_name), 0,
193 "mailbox_name - name of the corresponding mailbox"},
198 static PyGetSetDef Folder_getset[] = {
199 {"properties", (getter)get_properties, (setter)NULL,
200 "properties - folder properties object", NULL},
206 static PyTypeObject clawsmail_FolderType = {
207 PyObject_HEAD_INIT(NULL)
209 "clawsmail.Folder", /* tp_name*/
210 sizeof(clawsmail_FolderObject), /* tp_basicsize*/
212 (destructor)Folder_dealloc, /* tp_dealloc*/
219 0, /* tp_as_sequence*/
220 0, /* tp_as_mapping*/
223 Folder_str, /* tp_str*/
227 Py_TPFLAGS_DEFAULT, /* tp_flags*/
228 "Folder objects.\n\n" /* tp_doc */
229 "The __init__ function takes two optional arguments:\n"
230 "folder = Folder(identifier, [create_if_not_existing=False])\n"
231 "The identifier is an id string (e.g. '#mh/Mail/foo/bar'),"
232 "create_if_not_existing is a boolean expression.",
235 0, /* tp_richcompare */
236 0, /* tp_weaklistoffset */
239 Folder_methods, /* tp_methods */
240 Folder_members, /* tp_members */
241 Folder_getset, /* tp_getset */
244 0, /* tp_descr_get */
245 0, /* tp_descr_set */
246 0, /* tp_dictoffset */
247 (initproc)Folder_init, /* tp_init */
252 gboolean cmpy_add_folder(PyObject *module)
254 clawsmail_FolderType.tp_new = PyType_GenericNew;
255 if(PyType_Ready(&clawsmail_FolderType) < 0)
258 Py_INCREF(&clawsmail_FolderType);
259 return (PyModule_AddObject(module, "Folder", (PyObject*)&clawsmail_FolderType) == 0);
262 PyObject* clawsmail_folder_new(FolderItem *folderitem)
264 clawsmail_FolderObject *ff;
271 id = folder_item_get_identifier(folderitem);
272 arglist = Py_BuildValue("(s)", id);
274 ff = (clawsmail_FolderObject*) PyObject_CallObject((PyObject*) &clawsmail_FolderType, arglist);
276 return (PyObject*)ff;
279 FolderItem* clawsmail_folder_get_item(PyObject *self)
281 return ((clawsmail_FolderObject*)self)->folderitem;
284 PyTypeObject* clawsmail_folder_get_type_object()
286 return &clawsmail_FolderType;
289 gboolean clawsmail_folder_check(PyObject *self)
291 return (PyObject_TypeCheck(self, &clawsmail_FolderType) != 0);