Merge r3302 from sogo.
[jelmer/openchange.git] / pyopenchange / mapistore / folder.c
1 /*
2    OpenChange MAPI implementation.
3
4    Python interface to mapistore folder
5
6    Copyright (C) Julien Kerihuel 2011.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <Python.h>
23 #include "pyopenchange/mapistore/pymapistore.h"
24 #include "gen_ndr/exchange.h"
25
26 static void py_MAPIStoreFolder_dealloc(PyObject *_self)
27 {
28         PyMAPIStoreFolderObject *self = (PyMAPIStoreFolderObject *)_self;
29
30         Py_DECREF(self->context);
31         PyObject_Del(_self);
32 }
33
34 static PyObject *py_MAPIStoreFolder_create_folder(PyMAPIStoreFolderObject *self, PyObject *args, PyObject *kwargs)
35 {
36         int                     ret;
37         /* PyMAPIStoreFolderObject      *folder; */
38         char                    *kwnames[] = { "name", "description", "foldertype", "flags" };
39         const char              *name;
40         const char              *desc = NULL;
41         uint16_t                foldertype = FOLDER_GENERIC;
42         uint16_t                flags = NONE;
43         uint64_t                fid;
44
45         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|shh", kwnames, &name, &desc, &foldertype, &flags)) {
46                 return NULL;
47         }
48
49         /* Step 1. Check if the folder already exists */
50         ret = mapistore_folder_get_child_fid_by_name(self->context->mstore_ctx,
51                                                      self->context->context_id,
52                                                      self->context->folder_object, 
53                                                      name, &fid);
54         if (ret == MAPISTORE_SUCCESS) {
55                 if (flags != OPEN_IF_EXISTS) {
56                         PyErr_MAPIStore_IS_ERR_RAISE(MAPISTORE_ERR_EXIST);
57                         Py_RETURN_NONE;
58                 }
59         }
60         
61         /* TODO: Complete the implementation */
62
63         return Py_None;
64 }
65
66 static PyObject *py_MAPIStoreFolder_get_fid(PyMAPIStoreFolderObject *self, void *closure)
67 {
68         return PyLong_FromLongLong(self->fid);
69 }
70
71 static PyObject *py_MAPIStoreFolder_get_folder_count(PyMAPIStoreFolderObject *self, void *closure)
72 {
73         uint32_t        RowCount;
74         int             retval;
75
76         retval = mapistore_folder_get_folder_count(self->context->mstore_ctx, self->context->context_id,
77                                                    (self->folder_object ? self->folder_object : 
78                                                     self->context->folder_object), &RowCount);
79         if (retval != MAPISTORE_SUCCESS) {
80                 return PyInt_FromLong(-1);
81         }
82
83         return PyInt_FromLong(RowCount);
84 }
85
86
87 static PyObject *py_MAPIStoreFolder_get_message_count(PyMAPIStoreFolderObject *self, void *closure)
88 {
89         uint32_t        RowCount;
90         int             retval;
91
92         retval = mapistore_folder_get_message_count(self->context->mstore_ctx, self->context->context_id,
93                                                    (self->folder_object ? self->folder_object : 
94                                                     self->context->folder_object), 
95                                                     MAPISTORE_MESSAGE_TABLE, &RowCount);
96         if (retval != MAPISTORE_SUCCESS) {
97                 return PyInt_FromLong(-1);
98         }
99
100         return PyInt_FromLong(RowCount);
101 }
102
103
104 static PyObject *py_MAPIStoreFolder_get_fai_message_count(PyMAPIStoreFolderObject *self, void *closure)
105 {
106         uint32_t        RowCount;
107         int             retval;
108
109         retval = mapistore_folder_get_message_count(self->context->mstore_ctx, self->context->context_id,
110                                                    (self->folder_object ? self->folder_object : 
111                                                     self->context->folder_object), 
112                                                     MAPISTORE_FAI_TABLE, &RowCount);
113         if (retval != MAPISTORE_SUCCESS) {
114                 return PyInt_FromLong(-1);
115         }
116
117         return PyInt_FromLong(RowCount);
118 }
119
120 static PyMethodDef mapistore_folder_methods[] = {
121         { "create_folder", (PyCFunction)py_MAPIStoreFolder_create_folder, METH_VARARGS|METH_KEYWORDS },
122         { NULL },
123 };
124
125 static PyGetSetDef mapistore_folder_getsetters[] = {
126         { (char *)"fid", (getter)py_MAPIStoreFolder_get_fid, NULL, NULL },
127         { (char *)"folder_count", (getter)py_MAPIStoreFolder_get_folder_count, NULL, NULL },
128         { (char *)"message_count", (getter)py_MAPIStoreFolder_get_message_count, NULL, NULL },
129         { (char *)"fai_message_count", (getter)py_MAPIStoreFolder_get_fai_message_count, NULL, NULL },
130         { NULL }
131 };
132
133 PyTypeObject PyMAPIStoreFolder = {
134         PyObject_HEAD_INIT(NULL) 0,
135         .tp_name = "mapistore folder",
136         .tp_basicsize = sizeof (PyMAPIStoreFolderObject),
137         .tp_methods = mapistore_folder_methods,
138         .tp_getset = mapistore_folder_getsetters,
139         .tp_doc = "mapistore folder object",
140         .tp_dealloc = (destructor)py_MAPIStoreFolder_dealloc,
141         .tp_flags = Py_TPFLAGS_DEFAULT,
142 };