Python plugin: Make it possible to get folder tree from a Mailbox
[claws.git] / src / plugins / python / mailboxtype.c
1 /* Python plugin for Claws-Mail
2  * Copyright (C) 2013 Holger Berndt <hb@claws-mail.org>
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.h>
24 #include <glib/gi18n.h>
25
26 #include "mailboxtype.h"
27
28 #include <structmember.h>
29
30 typedef struct {
31     PyObject_HEAD
32     Folder *folder;
33 } clawsmail_MailboxObject;
34
35 static int Mailbox_init(clawsmail_MailboxObject *self, PyObject *args, PyObject *kwds)
36 {
37   self->folder = NULL;
38   return 0;
39 }
40
41 static void Mailbox_dealloc(clawsmail_MailboxObject* self)
42 {
43   self->folder = NULL;
44   self->ob_type->tp_free((PyObject*)self);
45 }
46
47 static PyObject* Mailbox_str(clawsmail_MailboxObject *self)
48 {
49   if(self->folder && self->folder->name)
50     return PyString_FromFormat("Mailbox: %s", self->folder->name);
51   Py_RETURN_NONE;
52 }
53
54 static PyObject* get_name(clawsmail_MailboxObject *self, void *closure)
55 {
56   if(self->folder && self->folder->name)
57     return PyString_FromString(self->folder->name);
58   Py_RETURN_NONE;
59 }
60
61 static PyGetSetDef Mailbox_getset[] = {
62    {"name", (getter)get_name, (setter)NULL,
63     "name - name of the mailbox", NULL},
64
65    {NULL}
66 };
67
68 static PyTypeObject clawsmail_MailboxType = {
69     PyObject_HEAD_INIT(NULL)
70     0,                         /* ob_size*/
71     "clawsmail.Mailbox",       /* tp_name*/
72     sizeof(clawsmail_MailboxObject), /* tp_basicsize*/
73     0,                         /* tp_itemsize*/
74     (destructor)Mailbox_dealloc, /* tp_dealloc*/
75     0,                         /* tp_print*/
76     0,                         /* tp_getattr*/
77     0,                         /* tp_setattr*/
78     0,                         /* tp_compare*/
79     0,                         /* tp_repr*/
80     0,                         /* tp_as_number*/
81     0,                         /* tp_as_sequence*/
82     0,                         /* tp_as_mapping*/
83     0,                         /* tp_hash */
84     0,                         /* tp_call*/
85     (reprfunc)Mailbox_str,     /* tp_str*/
86     0,                         /* tp_getattro*/
87     0,                         /* tp_setattro*/
88     0,                         /* tp_as_buffer*/
89     Py_TPFLAGS_DEFAULT,        /* tp_flags*/
90     "Mailbox objects.\n\n"     /* tp_doc */
91     "Do not construct objects of this type yourself.",
92     0,                         /* tp_traverse */
93     0,                         /* tp_clear */
94     0,                         /* tp_richcompare */
95     0,                         /* tp_weaklistoffset */
96     0,                         /* tp_iter */
97     0,                         /* tp_iternext */
98     0,                         /* tp_methods */
99     0,                         /* tp_members */
100     Mailbox_getset,            /* tp_getset */
101     0,                         /* tp_base */
102     0,                         /* tp_dict */
103     0,                         /* tp_descr_get */
104     0,                         /* tp_descr_set */
105     0,                         /* tp_dictoffset */
106     (initproc)Mailbox_init,    /* tp_init */
107     0,                         /* tp_alloc */
108     0,                         /* tp_new */
109 };
110
111
112 gboolean cmpy_add_mailbox(PyObject *module)
113 {
114   clawsmail_MailboxType.tp_new = PyType_GenericNew;
115   if(PyType_Ready(&clawsmail_MailboxType) < 0)
116     return FALSE;
117
118   Py_INCREF(&clawsmail_MailboxType);
119   return (PyModule_AddObject(module, "Mailbox", (PyObject*)&clawsmail_MailboxType) == 0);
120 }
121
122 PyObject* clawsmail_mailbox_new(Folder *folder)
123 {
124   clawsmail_MailboxObject *ff;
125
126   if(!folder)
127     return NULL;
128
129   ff = (clawsmail_MailboxObject*) PyObject_CallObject((PyObject*) &clawsmail_MailboxType, NULL);
130   if(!ff)
131     return NULL;
132
133   ff->folder = folder;
134   return (PyObject*)ff;
135 }
136
137 Folder* clawsmail_mailbox_get_folder(PyObject *self)
138 {
139   return ((clawsmail_MailboxObject*)self)->folder;
140 }
141
142 PyTypeObject* clawsmail_mailbox_get_type_object()
143 {
144   return &clawsmail_MailboxType;
145 }