2 OpenChange MAPI implementation.
4 Python interface to openchange
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 "libmapi/libmapi.h"
24 #include "pyopenchange/pymapi.h"
28 static PyObject *py_SPropValue_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
31 PySPropValueObject *SPropValue;
33 mem_ctx = talloc_new(NULL);
34 if (mem_ctx == NULL) {
39 SPropValue = PyObject_New(PySPropValueObject, &PySPropValue);
40 SPropValue->mem_ctx = mem_ctx;
41 SPropValue->SPropValue = talloc_array(mem_ctx, struct SPropValue, 2);
42 SPropValue->cValues = 0;
44 return (PyObject *) SPropValue;
47 static void py_SPropValue_dealloc(PyObject *_self)
49 PySPropValueObject *self = (PySPropValueObject *)_self;
51 talloc_free(self->mem_ctx);
55 static PyObject *py_SPropValue_add(PySPropValueObject *self, PyObject *args)
62 if (!PyArg_ParseTuple(args, "lO", &proptag, &data)) {
66 /* Ensure this tag has not already been added to the list */
67 for (i = 0; i < self->cValues; i++) {
68 if (self->SPropValue[i].ulPropTag == proptag) {
73 self->SPropValue = talloc_realloc(self->mem_ctx, self->SPropValue, struct SPropValue, self->cValues + 2);
75 switch (proptag & 0xFFFF) {
77 if (!PyInt_Check(data)) {
78 PyErr_SetString(PyExc_TypeError, "Property Tag requires long");
81 self->SPropValue[self->cValues].value.i = (uint16_t) PyInt_AsLong(data);
84 if (!PyInt_Check(data)) {
85 PyErr_SetString(PyExc_TypeError, "Property Tag requires long");
88 self->SPropValue[self->cValues].value.l = PyInt_AsLong(data);
91 if (!PyFloat_Check(data)) {
92 PyErr_SetString(PyExc_TypeError, "Property Tag requires double");
95 self->SPropValue[self->cValues].value.dbl = PyFloat_AsDouble(data);
98 if (!PyBool_Check(data)) {
99 PyErr_SetString(PyExc_TypeError, "Property Tag requires long");
102 self->SPropValue[self->cValues].value.b = PyInt_AsLong(data);
105 if (!PyInt_Check(data)) {
106 PyErr_SetString(PyExc_TypeError, "Property Tag requires long long int");
109 self->SPropValue[self->cValues].value.d = PyLong_AsLongLong(data);
112 if (!PyString_Check(data)) {
113 PyErr_SetString(PyExc_TypeError, "Property Tag requires string");
116 self->SPropValue[self->cValues].value.lpszA = talloc_strdup(self->mem_ctx, PyString_AsString(data));
119 if (!PyString_Check(data)) {
120 PyErr_SetString(PyExc_TypeError, "Property Tag requires string");
123 self->SPropValue[self->cValues].value.lpszW = talloc_strdup(self->mem_ctx, PyString_AsString(data));
126 if (!PyFloat_Check(data)) {
127 PyErr_SetString(PyExc_TypeError, "Property Tag requires float");
130 unix_to_nt_time(&nt, PyFloat_AsDouble(data));
131 self->SPropValue[self->cValues].value.ft.dwLowDateTime = (nt << 32) >> 32;
132 self->SPropValue[self->cValues].value.ft.dwHighDateTime = nt >> 32;
135 if (!PyInt_Check(data)) {
136 PyErr_SetString(PyExc_TypeError, "Property Tag requires long");
139 self->SPropValue[self->cValues].value.err = PyInt_AsLong(data);
142 printf("Missing support for 0x%.4x type\n", (proptag & 0xFFFF));
146 self->SPropValue[self->cValues].ulPropTag = proptag;
151 static PyObject *py_SPropValue_dump(PySPropValueObject *self, PyObject *args)
156 if (!PyArg_ParseTuple(args, "s", &sep)) {
160 for (i = 0; i < self->cValues; i++) {
161 mapidump_SPropValue(self->SPropValue[i], sep);
167 static PyMethodDef mapi_SPropValue_methods[] = {
168 { "add", (PyCFunction)py_SPropValue_add, METH_VARARGS },
169 { "dump", (PyCFunction)py_SPropValue_dump, METH_VARARGS },
173 static PyGetSetDef mapi_SPropValue_getsetters[] = {
177 PyTypeObject PySPropValue = {
178 PyObject_HEAD_INIT(NULL) 0,
179 .tp_name = "SPropValue",
180 .tp_basicsize = sizeof (PySPropValueObject),
181 .tp_methods = mapi_SPropValue_methods,
182 .tp_getset = mapi_SPropValue_getsetters,
183 .tp_doc = "SPropValue MAPI structure",
184 .tp_new = py_SPropValue_new,
185 .tp_dealloc = (destructor)py_SPropValue_dealloc,
186 .tp_flags = Py_TPFLAGS_DEFAULT,
189 static PyMethodDef py_mapi_global_methods[] = {
197 if (PyType_Ready(&PySPropValue) < 0) {
201 m = Py_InitModule3("mapi", py_mapi_global_methods,
202 "An interface to OpenChange MAPI");
207 /* Add all properties - generated by mparse.pl */
208 pymapi_add_properties(m);
210 Py_INCREF(&PySPropValue);
212 PyModule_AddObject(m, "SPropValue", (PyObject *)&PySPropValue);