2 OpenChange MAPI implementation.
4 Python interface to mapistore context
6 Copyright (C) Julien Kerihuel 2010-2011.
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.
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.
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/>.
23 #include "pyopenchange/mapistore/pymapistore.h"
25 static void py_MAPIStoreContext_dealloc(PyObject *_self)
27 PyMAPIStoreContextObject *self = (PyMAPIStoreContextObject *) _self;
29 mapistore_del_context(self->mstore_ctx, self->context_id);
30 Py_XDECREF(self->parent);
34 static PyObject *py_MAPIStoreContext_open(PyMAPIStoreContextObject *self, PyObject *args)
36 PyMAPIStoreFolderObject *folder;
38 folder = PyObject_New(PyMAPIStoreFolderObject, &PyMAPIStoreFolder);
40 folder->context = self;
41 folder->folder_object = NULL;
42 folder->fid = self->fid;
45 return (PyObject *)folder;
48 static PyObject *py_MAPIStoreContext_register_subscription(PyMAPIStoreContextObject *self, PyObject *args)
51 struct mapistore_mgmt_notif n;
52 const char *mapistoreURI;
54 uint16_t NotificationFlags;
57 struct mapistore_subscription_list *subscription_list;
58 struct mapistore_subscription *subscription;
59 struct mapistore_object_subscription_parameters subscription_params;
62 if (!PyArg_ParseTuple(args, "sbh", &mapistoreURI, &WholeStore, &NotificationFlags)) {
66 n.WholeStore = WholeStore;
67 n.NotificationFlags = NotificationFlags;
69 if (WholeStore == true) {
72 n.MAPIStoreURI = NULL;
74 /* Retrieve folderID from mapistoreURI in openchange.ldb */
75 ret = openchangedb_get_fid(self->parent->ocdb_ctx, mapistoreURI, &FolderID);
76 if (ret != MAPISTORE_SUCCESS) {
77 /* Try to retrieve URI from user indexing.tdb */
78 ret = mapistore_indexing_record_get_fmid(self->mstore_ctx,
79 self->mstore_ctx->conn_info->username,
80 mapistoreURI, false, &FolderID, &softdeleted);
81 if (ret != MAPISTORE_SUCCESS || softdeleted == true) {
82 return PyBool_FromLong(false);
86 n.FolderID = FolderID;
88 n.MAPIStoreURI = mapistoreURI;
92 ret = mapistore_mgmt_interface_register_subscription(self->mstore_ctx->conn_info, &n);
94 /* Upon success attach subscription to session object using
95 * existing mapistore_notification.c implementation */
96 if (ret == MAPISTORE_SUCCESS) {
97 subscription_list = talloc_zero(self->mstore_ctx, struct mapistore_subscription_list);
98 DLIST_ADD(self->mstore_ctx->subscriptions, subscription_list);
100 subscription_params.folder_id = n.FolderID;
101 subscription_params.object_id = n.MessageID;
102 subscription_params.whole_store = n.WholeStore;
104 /* In OpenChange server, we use handle_id of the
105 * object, just use rand for now in bindings */
108 subscription = mapistore_new_subscription(subscription_list,
110 self->mstore_ctx->conn_info->username,
111 random_int, n.NotificationFlags,
112 &subscription_params);
113 subscription_list->subscription = subscription;
116 return PyInt_FromLong(!ret ? random_int : -1);
119 static PyObject *py_MAPIStoreContext_unregister_subscription(PyMAPIStoreContextObject *self, PyObject *args)
122 struct mapistore_mgmt_notif n;
123 const char *mapistoreURI;
125 uint16_t NotificationFlags;
129 if (!PyArg_ParseTuple(args, "sbhi", &mapistoreURI, &WholeStore, &NotificationFlags, &identifier)) {
133 n.WholeStore = WholeStore;
134 n.NotificationFlags = NotificationFlags;
136 if (WholeStore == true) {
139 n.MAPIStoreURI = NULL;
141 /* Retrieve folderID from mapistoreURI in openchange.ldb */
142 ret = openchangedb_get_fid(self->parent->ocdb_ctx, mapistoreURI, &FolderID);
143 if (ret != MAPISTORE_SUCCESS) {
144 /* Try to retrieve URI from user indexing.tdb */
147 n.FolderID = FolderID;
149 n.MAPIStoreURI = mapistoreURI;
153 ret = mapistore_mgmt_interface_unregister_subscription(self->mstore_ctx->conn_info, &n);
155 /* Remove matching notifications from mapistore_notification system */
156 if (ret == MAPISTORE_SUCCESS) {
157 ret = mapistore_delete_subscription(self->mstore_ctx, identifier, NotificationFlags);
160 return PyBool_FromLong(!ret ? true : false);
163 static PyObject *py_MAPIStoreContext_get_notifications(PyMAPIStoreContextObject *self, PyObject *args)
166 struct mapistore_subscription_list *sel;
167 struct mapistore_notification_list *nlist;
169 for (sel = self->mstore_ctx->subscriptions; sel; sel = sel->next) {
170 ret = mapistore_get_queued_notifications(self->mstore_ctx, sel->subscription, &nlist);
171 if (ret == MAPISTORE_SUCCESS) {
173 printf("notification FolderID: 0x%llx\n",
174 nlist->notification->parameters.object_parameters.folder_id);
175 printf("notification MessageID: 0x%llx\n",
176 nlist->notification->parameters.object_parameters.object_id);
183 if (ret != MAPISTORE_SUCCESS) {
189 static PyMethodDef mapistore_context_methods[] = {
190 { "open", (PyCFunction)py_MAPIStoreContext_open, METH_VARARGS },
191 { "add_subscription", (PyCFunction)py_MAPIStoreContext_register_subscription, METH_VARARGS },
192 { "delete_subscription", (PyCFunction)py_MAPIStoreContext_unregister_subscription, METH_VARARGS },
193 { "get_notifications", (PyCFunction)py_MAPIStoreContext_get_notifications, METH_VARARGS },
197 static PyGetSetDef mapistore_context_getsetters[] = {
201 PyTypeObject PyMAPIStoreContext = {
202 PyObject_HEAD_INIT(NULL) 0,
203 .tp_name = "mapistore context",
204 .tp_basicsize = sizeof (PyMAPIStoreContextObject),
205 .tp_methods = mapistore_context_methods,
206 .tp_getset = mapistore_context_getsetters,
207 .tp_doc = "mapistore context object",
208 .tp_dealloc = (destructor)py_MAPIStoreContext_dealloc,
209 .tp_flags = Py_TPFLAGS_DEFAULT,