Python plugin: Folder: Create Python objects for members on the fly
[claws.git] / src / plugins / python / foldertype.c
1 /* Python plugin for Claws-Mail
2  * Copyright (C) 2009 Holger Berndt
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 #ifdef HAVE_CONFIG_H
19 #  include "config.h"
20 #include "claws-features.h"
21 #endif
22
23 #include <glib/gi18n.h>
24
25 #include "foldertype.h"
26 #include "folderpropertiestype.h"
27 #include "messageinfotype.h"
28
29 #include <structmember.h>
30
31
32 typedef struct {
33     PyObject_HEAD
34     PyObject *properties;
35     FolderItem *folderitem;
36 } clawsmail_FolderObject;
37
38
39 static void Folder_dealloc(clawsmail_FolderObject* self)
40 {
41   Py_XDECREF(self->properties);
42   self->ob_type->tp_free((PyObject*)self);
43 }
44
45 static int Folder_init(clawsmail_FolderObject *self, PyObject *args, PyObject *kwds)
46 {
47   const char *ss = NULL;
48   FolderItem *folderitem = NULL;
49   char create = 0;
50
51   /* optional constructor argument: folderitem id string */
52   if(!PyArg_ParseTuple(args, "|sb", &ss, &create))
53     return -1;
54
55   if(ss) {
56     if(create == 0) {
57       folderitem = folder_find_item_from_identifier(ss);
58       if(!folderitem) {
59         PyErr_SetString(PyExc_ValueError, "A folder with that path does not exist, and the create parameter was False.");
60         return -1;
61       }
62     }
63     else {
64       folderitem = folder_get_item_from_identifier(ss);
65       if(!folderitem) {
66         PyErr_SetString(PyExc_IOError, "A folder with that path does not exist, and could not be created.");
67         return -1;
68       }
69     }
70   }
71
72   self->folderitem = folderitem;
73   if(folderitem)
74     self->properties = clawsmail_folderproperties_new(folderitem->prefs);
75   else {
76     Py_INCREF(Py_None);
77     self->properties = Py_None;
78   }
79
80   return 0;
81 }
82
83 static PyObject* Folder_str(clawsmail_FolderObject *self)
84 {
85   return PyString_FromFormat("Folder: %s", self->folderitem->name);
86 }
87
88 static PyObject* Folder_get_identifier(clawsmail_FolderObject *self, PyObject *args)
89 {
90   PyObject *obj;
91   gchar *id;
92   if(!self->folderitem)
93     return NULL;
94   id = folder_item_get_identifier(self->folderitem);
95   obj = Py_BuildValue("s", id);
96   g_free(id);
97   return obj;
98 }
99
100 static PyObject* Folder_get_messages(clawsmail_FolderObject *self, PyObject *args)
101 {
102   GSList *msglist, *walk;
103   PyObject *retval;
104   Py_ssize_t pos;
105
106   if(!self->folderitem)
107     return NULL;
108
109   msglist = folder_item_get_msg_list(self->folderitem);
110   retval = PyTuple_New(g_slist_length(msglist));
111   if(!retval) {
112     procmsg_msg_list_free(msglist);
113     Py_INCREF(Py_None);
114     return Py_None;
115   }
116
117   for(pos = 0, walk = msglist; walk; walk = walk->next, ++pos) {
118     PyObject *msg;
119     msg = clawsmail_messageinfo_new(walk->data);
120     PyTuple_SET_ITEM(retval, pos, msg);
121   }
122   procmsg_msg_list_free(msglist);
123
124   return retval;
125 }
126
127 static PyObject* get_name(clawsmail_FolderObject *self, void *closure)
128 {
129   if(self->folderitem && self->folderitem->name)
130     return PyString_FromString(self->folderitem->name);
131   Py_RETURN_NONE;
132 }
133
134 static PyObject* get_path(clawsmail_FolderObject *self, void *closure)
135 {
136   if(self->folderitem && self->folderitem->path)
137     return PyString_FromString(self->folderitem->path);
138   Py_RETURN_NONE;
139 }
140
141 static PyObject* get_mailbox_name(clawsmail_FolderObject *self, void *closure)
142 {
143   if(self->folderitem && self->folderitem->folder && self->folderitem->folder->name)
144     return PyString_FromString(self->folderitem->folder->name);
145   Py_RETURN_NONE;
146 }
147
148 static PyObject* get_properties(clawsmail_FolderObject *self, void *closure)
149 {
150   Py_INCREF(self->properties);
151   return self->properties;
152 }
153
154 static PyMethodDef Folder_methods[] = {
155     {"get_identifier", (PyCFunction)Folder_get_identifier, METH_NOARGS,
156      "get_identifier() - get identifier\n"
157      "\n"
158      "Get identifier for folder as a string (e.g. #mh/foo/bar)."},
159     {"get_messages", (PyCFunction)Folder_get_messages, METH_NOARGS,
160      "get_messages() - get a tuple of messages in folder\n"
161      "\n"
162      "Get a tuple of MessageInfos for the folder."},
163     {NULL}
164 };
165
166 static PyGetSetDef Folder_getset[] = {
167     {"name", (getter)get_name, (setter)NULL,
168      "name - name of folder", NULL},
169
170     {"path", (getter)get_path, (setter)NULL,
171      "path - path of folder", NULL},
172
173     {"mailbox_name", (getter)get_mailbox_name, (setter)NULL,
174      "mailbox_name - name of the corresponding mailbox", NULL},
175
176     {"properties", (getter)get_properties, (setter)NULL,
177      "properties - folder properties object", NULL},
178
179     {NULL}
180 };
181
182
183 static PyTypeObject clawsmail_FolderType = {
184     PyObject_HEAD_INIT(NULL)
185     0,                         /* ob_size*/
186     "clawsmail.Folder",        /* tp_name*/
187     sizeof(clawsmail_FolderObject), /* tp_basicsize*/
188     0,                         /* tp_itemsize*/
189     (destructor)Folder_dealloc, /* tp_dealloc*/
190     0,                         /* tp_print*/
191     0,                         /* tp_getattr*/
192     0,                         /* tp_setattr*/
193     0,                         /* tp_compare*/
194     0,                         /* tp_repr*/
195     0,                         /* tp_as_number*/
196     0,                         /* tp_as_sequence*/
197     0,                         /* tp_as_mapping*/
198     0,                         /* tp_hash */
199     0,                         /* tp_call*/
200     (reprfunc)Folder_str,      /* tp_str*/
201     0,                         /* tp_getattro*/
202     0,                         /* tp_setattro*/
203     0,                         /* tp_as_buffer*/
204     Py_TPFLAGS_DEFAULT,        /* tp_flags*/
205     "Folder objects.\n\n"      /* tp_doc */
206     "The __init__ function takes two optional arguments:\n"
207     "folder = Folder(identifier, [create_if_not_existing=False])\n"
208     "The identifier is an id string (e.g. '#mh/Mail/foo/bar'),"
209     "create_if_not_existing is a boolean expression.",
210     0,                         /* tp_traverse */
211     0,                         /* tp_clear */
212     0,                         /* tp_richcompare */
213     0,                         /* tp_weaklistoffset */
214     0,                         /* tp_iter */
215     0,                         /* tp_iternext */
216     Folder_methods,            /* tp_methods */
217     0,                         /* tp_members */
218     Folder_getset,             /* tp_getset */
219     0,                         /* tp_base */
220     0,                         /* tp_dict */
221     0,                         /* tp_descr_get */
222     0,                         /* tp_descr_set */
223     0,                         /* tp_dictoffset */
224     (initproc)Folder_init,     /* tp_init */
225     0,                         /* tp_alloc */
226     0,                         /* tp_new */
227 };
228
229 gboolean cmpy_add_folder(PyObject *module)
230 {
231   clawsmail_FolderType.tp_new = PyType_GenericNew;
232   if(PyType_Ready(&clawsmail_FolderType) < 0)
233     return FALSE;
234
235   Py_INCREF(&clawsmail_FolderType);
236   return (PyModule_AddObject(module, "Folder", (PyObject*)&clawsmail_FolderType) == 0);
237 }
238
239 PyObject* clawsmail_folder_new(FolderItem *folderitem)
240 {
241   clawsmail_FolderObject *ff;
242   PyObject *arglist;
243   gchar *id;
244
245   if(!folderitem)
246     return NULL;
247
248   id = folder_item_get_identifier(folderitem);
249   arglist = Py_BuildValue("(s)", id);
250   g_free(id);
251   ff = (clawsmail_FolderObject*) PyObject_CallObject((PyObject*) &clawsmail_FolderType, arglist);
252   Py_DECREF(arglist);
253   return (PyObject*)ff;
254 }
255
256 FolderItem* clawsmail_folder_get_item(PyObject *self)
257 {
258   return ((clawsmail_FolderObject*)self)->folderitem;
259 }
260
261 PyTypeObject* clawsmail_folder_get_type_object()
262 {
263   return &clawsmail_FolderType;
264 }
265
266 gboolean clawsmail_folder_check(PyObject *self)
267 {
268   return (PyObject_TypeCheck(self, &clawsmail_FolderType) != 0);
269 }