libfreebusy_conv.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #include <kopano/memory.hpp>
  18. #include "libfreebusy_conv.h"
  19. static PyObject *PyTypeFreeBusyBlock;
  20. void InitFreebusy() {
  21. PyObject *lpMAPIStruct = PyImport_ImportModule("MAPI.Struct");
  22. if(!lpMAPIStruct) {
  23. PyErr_SetString(PyExc_RuntimeError, "Unable to import MAPI.Struct");
  24. return;
  25. }
  26. PyTypeFreeBusyBlock = PyObject_GetAttrString(lpMAPIStruct, "FreeBusyBlock");
  27. }
  28. LPFBUser List_to_p_FBUser(PyObject *list, ULONG *cValues) {
  29. KCHL::memory_ptr<FBUser> lpFbUsers;
  30. LPENTRYID entryid = nullptr;
  31. PyObject *iter = NULL;
  32. PyObject *elem = NULL;
  33. char *buf = 0 ;
  34. int len;
  35. size_t size;
  36. int i = 0;
  37. if (list == Py_None)
  38. goto exit;
  39. iter = PyObject_GetIter(list);
  40. if (!iter)
  41. goto exit;
  42. len = PyObject_Length(list);
  43. if (MAPIAllocateBuffer(len * sizeof(FBUser), &~lpFbUsers) != hrSuccess)
  44. goto exit;
  45. while ((elem = PyIter_Next(iter))) {
  46. if (PyBytes_AsStringAndSize(elem, &buf, (Py_ssize_t *)&size) == -1) {
  47. PyErr_SetString(PyExc_RuntimeError, "Entryid is missing");
  48. goto exit;
  49. }
  50. entryid = reinterpret_cast< LPENTRYID >(buf);
  51. lpFbUsers[i].m_cbEid = size;
  52. lpFbUsers[i].m_lpEid = entryid;
  53. ++i;
  54. Py_XDECREF(elem);
  55. elem = nullptr;
  56. }
  57. *cValues = i;
  58. exit:
  59. Py_XDECREF(elem);
  60. Py_XDECREF(iter);
  61. if (PyErr_Occurred() && lpFbUsers != nullptr)
  62. return nullptr;
  63. return lpFbUsers.release();
  64. }
  65. LPFBBlock_1 List_to_p_FBBlock_1(PyObject *list, ULONG *nBlocks) {
  66. KCHL::memory_ptr<FBBlock_1> lpFBBlocks;
  67. PyObject *iter = nullptr, *elem = nullptr;
  68. PyObject *start, *end, *status;
  69. size_t i, len;
  70. if (list == Py_None)
  71. goto exit;
  72. iter = PyObject_GetIter(list);
  73. if (!iter)
  74. goto exit;
  75. len = PyObject_Length(list);
  76. if (MAPIAllocateBuffer(len * sizeof(FBBlock_1), &~lpFBBlocks) != hrSuccess)
  77. goto exit;
  78. i=0;
  79. while ((elem = PyIter_Next(iter))) {
  80. start = PyObject_GetAttrString(elem, "start");
  81. end = PyObject_GetAttrString(elem, "end");
  82. status = PyObject_GetAttrString(elem, "status");
  83. lpFBBlocks[i].m_tmStart = PyLong_AsLong(start);
  84. lpFBBlocks[i].m_tmEnd = PyLong_AsLong(end);
  85. lpFBBlocks[i].m_fbstatus = FBStatus(PyLong_AsLong(status));
  86. i++;
  87. Py_XDECREF(elem);
  88. Py_XDECREF(start);
  89. Py_XDECREF(end);
  90. Py_XDECREF(status);
  91. elem = nullptr;
  92. }
  93. *nBlocks = i;
  94. exit:
  95. Py_XDECREF(elem);
  96. Py_XDECREF(iter);
  97. if (PyErr_Occurred() && lpFBBlocks != nullptr)
  98. return nullptr;
  99. return lpFBBlocks.release();
  100. }
  101. PyObject* Object_from_FBBlock_1(FBBlock_1 const& sFBBlock) {
  102. PyObject *start = nullptr, *end = nullptr,
  103. *status = nullptr, *object = nullptr;
  104. start = PyLong_FromLong(sFBBlock.m_tmStart);
  105. if (PyErr_Occurred())
  106. goto exit;
  107. end = PyLong_FromLong(sFBBlock.m_tmEnd);
  108. if (PyErr_Occurred())
  109. goto exit;
  110. status = PyLong_FromLong(sFBBlock.m_fbstatus);
  111. if (PyErr_Occurred())
  112. goto exit;
  113. object = PyObject_CallFunction(PyTypeFreeBusyBlock, "(OOO)", start, end, status);
  114. exit:
  115. Py_XDECREF(start);
  116. Py_XDECREF(end);
  117. Py_XDECREF(status);
  118. if (PyErr_Occurred()) {
  119. Py_XDECREF(object);
  120. object = nullptr;
  121. }
  122. return object;
  123. }
  124. PyObject* List_from_FBBlock_1(LPFBBlock_1 lpFBBlocks, LONG* nBlocks) {
  125. PyObject *elem = nullptr;
  126. PyObject *list = PyList_New(0);
  127. for (size_t i = 0; i < *nBlocks; ++i) {
  128. elem = Object_from_FBBlock_1(lpFBBlocks[i]);
  129. if(PyErr_Occurred())
  130. goto exit;
  131. PyList_Append(list, elem);
  132. Py_XDECREF(elem);
  133. elem = nullptr;
  134. }
  135. exit:
  136. Py_XDECREF(elem);
  137. if (PyErr_Occurred()) {
  138. Py_XDECREF(list);
  139. list = nullptr;
  140. }
  141. return list;
  142. }