2 OpenChange OCPF (OpenChange Property File) implementation.
4 Python interface to ocpf
6 Copyright (C) Julien Kerihuel 2010.
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/pyocpf.h"
24 #include "pyopenchange/pymapi.h"
25 #include "libocpf/ocpf.h"
27 static PyTypeObject *SPropValue_Type;
31 PyAPI_DATA(PyTypeObject) PyOcpf;
33 static PyObject *py_new_context(PyTypeObject *type, PyObject *args, PyObject *kwargs)
36 PyOcpfObject *ocpf_object;
37 char *kwnames[] = { "filename", "flags", NULL };
43 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sH", kwnames,
48 mem_ctx = talloc_new(NULL);
49 if (mem_ctx == NULL) {
54 printf("filename = %s, flags = 0x%x\n", filename, flags);
55 ret = ocpf_new_context(filename, &context_id, flags);
56 if (ret == OCPF_ERROR) {
60 printf("context_id = %d\n", context_id);
61 ocpf_object = PyObject_New(PyOcpfObject, &PyOcpf);
62 ocpf_object->context_id = context_id;
63 ocpf_object->mem_ctx = mem_ctx;
65 return (PyObject *) ocpf_object;
68 static PyObject *py_ocpf_parse(PyOcpfObject *self)
72 ret = ocpf_parse(self->context_id);
73 return PyInt_FromLong(ret);
76 static PyObject *py_ocpf_set_SPropValue_array(PyOcpfObject *self)
80 ret = ocpf_server_set_SPropValue(self->mem_ctx, self->context_id);
81 return PyInt_FromLong(ret);
84 static PyObject *py_ocpf_get_SPropValue(PyOcpfObject *self)
88 PySPropValueObject *pySPropValue;
89 struct SPropValue *SPropValue;
91 mod_mapi = PyImport_ImportModule("openchange.mapi");
92 if (mod_mapi == NULL) {
93 printf("Can't load module\n");
96 SPropValue_Type = (PyTypeObject *)PyObject_GetAttrString(mod_mapi, "SPropValue");
97 if (SPropValue_Type == NULL)
100 SPropValue = ocpf_get_SPropValue(self->context_id, &cValues);
102 pySPropValue = (PySPropValueObject *)SPropValue_Type->tp_new(NULL, NULL, NULL);
103 talloc_free(pySPropValue->SPropValue);
104 pySPropValue->SPropValue = SPropValue;
105 talloc_steal(pySPropValue->mem_ctx, pySPropValue->SPropValue);
106 pySPropValue->cValues = cValues;
108 return (PyObject *) pySPropValue;
112 static PyObject *py_ocpf_add_SPropValue(PyOcpfObject *self, PyObject *args)
115 PyObject *pySPropValue;
116 PySPropValueObject *SPropValue;
120 mod_mapi = PyImport_ImportModule("openchange.mapi");
121 if (mod_mapi == NULL) {
122 printf("Can't load module\n");
126 SPropValue_Type = (PyTypeObject *)PyObject_GetAttrString(mod_mapi, "SPropValue");
127 if (SPropValue_Type == NULL) {
131 if (!PyArg_ParseTuple(args, "O", &pySPropValue)) {
135 if (!PyObject_TypeCheck(pySPropValue, SPropValue_Type)) {
136 PyErr_SetString(PyExc_TypeError, "Function requires SPropValue obnject");
140 SPropValue = (PySPropValueObject *) pySPropValue;
142 for (i = 0; i < SPropValue->cValues; i++) {
143 ret = ocpf_server_add_SPropValue(self->context_id, &SPropValue->SPropValue[i]);
145 return PyInt_FromLong(ret);
149 return PyInt_FromLong(ret);
153 static PyObject *py_ocpf_dump(PyOcpfObject *self)
155 ocpf_dump(self->context_id);
159 static PyObject *py_ocpf_write_init(PyOcpfObject *self, PyObject *args)
164 if (!PyArg_ParseTuple(args, "K", &folder_id)) {
168 ret = ocpf_write_init(self->context_id, folder_id);
169 return PyInt_FromLong(ret);
172 static PyObject *py_ocpf_write(PyOcpfObject *self, PyObject *args)
175 PyObject *pySPropValue;
176 PySPropValueObject *SPropValue;
177 struct mapi_SPropValue_array mapi_lpProps;
181 mod_mapi = PyImport_ImportModule("openchange.mapi");
182 if (mod_mapi == NULL) {
183 printf("Can't load module\n");
186 SPropValue_Type = (PyTypeObject *)PyObject_GetAttrString(mod_mapi, "SPropValue");
187 if (SPropValue_Type == NULL)
191 if (!PyArg_ParseTuple(args, "O", &pySPropValue)) {
195 if (!PyObject_TypeCheck(pySPropValue, SPropValue_Type)) {
196 PyErr_SetString(PyExc_TypeError, "Function require SPropValue object");
199 SPropValue = (PySPropValueObject *) pySPropValue;
201 mapi_lpProps.cValues = SPropValue->cValues;
202 mapi_lpProps.lpProps = talloc_array(SPropValue->mem_ctx, struct mapi_SPropValue, SPropValue->cValues);
203 for (i = 0; i < SPropValue->cValues; i++) {
204 cast_mapi_SPropValue((TALLOC_CTX *)mapi_lpProps.lpProps,
205 &(mapi_lpProps.lpProps[i]), &(SPropValue->SPropValue[i]));
208 ret = ocpf_write_auto(self->context_id, NULL, &mapi_lpProps);
209 talloc_free(mapi_lpProps.lpProps);
210 return PyInt_FromLong(ret);
213 static PyObject *py_ocpf_write_commit(PyOcpfObject *self)
217 ret = ocpf_write_commit(self->context_id);
218 return PyInt_FromLong(ret);
221 static PyObject *py_ocpf_set_type(PyOcpfObject *self, PyObject *args)
225 if (!PyArg_ParseTuple(args, "s", &type)) {
229 return PyInt_FromLong(ocpf_server_set_type(self->context_id, type));
232 static PyMethodDef py_ocpf_global_methods[] = {
236 static PyMethodDef ocpf_methods[] = {
237 { "parse", (PyCFunction) py_ocpf_parse, METH_NOARGS },
238 { "dump", (PyCFunction) py_ocpf_dump, METH_NOARGS },
239 { "write_init", (PyCFunction) py_ocpf_write_init, METH_VARARGS },
240 { "write", (PyCFunction) py_ocpf_write, METH_VARARGS },
241 { "write_commit", (PyCFunction) py_ocpf_write_commit, METH_NOARGS },
242 { "set_SPropValue_array", (PyCFunction) py_ocpf_set_SPropValue_array, METH_NOARGS },
243 { "get_SPropValue", (PyCFunction) py_ocpf_get_SPropValue, METH_NOARGS },
244 { "add_SPropValue", (PyCFunction) py_ocpf_add_SPropValue, METH_VARARGS },
245 { "set_type", (PyCFunction) py_ocpf_set_type, METH_VARARGS },
249 static PyGetSetDef ocpf_getsetters[] = {
253 static void py_ocpf_dealloc(PyObject *_self)
255 PyOcpfObject *self = (PyOcpfObject *)_self;
257 talloc_free(self->mem_ctx);
259 printf("ocpf_del_context\n");
260 ocpf_del_context(self->context_id);
264 PyTypeObject PyOcpf = {
265 PyObject_HEAD_INIT(NULL) 0,
267 .tp_basicsize = sizeof(PyOcpfObject),
268 .tp_methods = ocpf_methods,
269 .tp_getset = ocpf_getsetters,
270 .tp_doc = "OCPF context",
271 .tp_new = py_new_context,
272 .tp_dealloc = (destructor)py_ocpf_dealloc,
273 .tp_flags = Py_TPFLAGS_DEFAULT,
280 if (PyType_Ready(&PyOcpf) < 0) {
284 m = Py_InitModule3("ocpf", py_ocpf_global_methods,
285 "An interface to OCPF, an OpenChange file format used to represent MAPI messages");
290 PyModule_AddObject(m, "OCPF_FLAGS_RDWR", PyInt_FromLong(OCPF_FLAGS_RDWR));
291 PyModule_AddObject(m, "OCPF_FLAGS_READ", PyInt_FromLong(OCPF_FLAGS_READ));
292 PyModule_AddObject(m, "OCPF_FLAGS_WRITE", PyInt_FromLong(OCPF_FLAGS_WRITE));
293 PyModule_AddObject(m, "OCPF_FLAGS_CREATE", PyInt_FromLong(OCPF_FLAGS_CREATE));
297 PyModule_AddObject(m, "Ocpf", (PyObject *)&PyOcpf);
299 /* Initialize OCPF subsystem */