vfreebusy.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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/platform.h>
  18. #include <utility>
  19. #include "vfreebusy.h"
  20. #include <mapiutil.h>
  21. #include <kopano/mapiext.h>
  22. #include "nameids.h"
  23. using namespace std;
  24. namespace KC {
  25. /**
  26. * Converts a VFREEBUSY object to separate parts
  27. *
  28. * @param[in] lpFbcomp The ical component containing the VFREEBUSY
  29. * @param[out] lptStart Returns the DTSTART property
  30. * @param[out] lptEnd Returns the DTEND property
  31. * @param[out] lpstrUID Returns the UID property
  32. * @param[out] lstUsers Appends any email addresses of ATTENDEEs listed
  33. *
  34. * @return Always hrSuccess
  35. */
  36. HRESULT HrGetFbInfo(icalcomponent *lpFbcomp, time_t *lptStart, time_t *lptEnd, std::string *lpstrUID, std::list<std::string> *lstUsers)
  37. {
  38. HRESULT hr = hrSuccess;
  39. icalproperty *lpicProp = NULL;
  40. std::string strEmail;
  41. // DTSTART
  42. lpicProp = icalcomponent_get_first_property(lpFbcomp, ICAL_DTSTART_PROPERTY);
  43. if (lpicProp)
  44. *lptStart = icaltime_as_timet (icalproperty_get_dtstart (lpicProp));
  45. // DTEND
  46. lpicProp = icalcomponent_get_first_property(lpFbcomp, ICAL_DTEND_PROPERTY);
  47. if (lpicProp)
  48. *lptEnd = icaltime_as_timet (icalproperty_get_dtend (lpicProp));
  49. // UID
  50. lpicProp = icalcomponent_get_first_property(lpFbcomp, ICAL_UID_PROPERTY);
  51. if (lpicProp)
  52. *lpstrUID = icalproperty_get_uid(lpicProp);
  53. // ATTENDEE
  54. lpicProp = icalcomponent_get_first_property(lpFbcomp, ICAL_ATTENDEE_PROPERTY);
  55. while (lpicProp) {
  56. strEmail = icalproperty_get_attendee(lpicProp);
  57. if (strncasecmp(strEmail.c_str(), "mailto:", 7) == 0) {
  58. strEmail.erase(0, 7);
  59. }
  60. lstUsers->push_back(std::move(strEmail));
  61. lpicProp = icalcomponent_get_next_property(lpFbcomp, ICAL_ATTENDEE_PROPERTY);
  62. }
  63. return hr;
  64. }
  65. /**
  66. * Converts a MAPI freebusy block to a VFREEBUSY ical component.
  67. *
  68. * @param[in] lpsFbblk MAPI freebusy info blocks
  69. * @param[in] ulBlocks Number of blocks in lpsFbblk
  70. * @param[in] tDtStart Unix timestamp with the start date
  71. * @param[in] tDtEnd Unix timestamp with the end date
  72. * @param[in] strOrganiser The email address of the organiser
  73. * @param[in] strUser The email address of an attendee
  74. * @param[in] strUID UID of the freebusy data
  75. * @param[out] lpicFbComponent new VFREEBUSY ical component
  76. *
  77. * @return MAPI error code
  78. */
  79. HRESULT HrFbBlock2ICal(FBBlock_1 *lpsFbblk, LONG ulBlocks, time_t tDtStart, time_t tDtEnd, const std::string &strOrganiser, const std::string &strUser, const std::string &strUID, icalcomponent **lpicFbComponent)
  80. {
  81. icalcomponent *lpFbComp = NULL;
  82. icaltimetype ittStamp;
  83. icalproperty *lpicProp = NULL;
  84. icalperiodtype icalPeriod;
  85. icalparameter *icalParam = NULL;
  86. time_t tStart = 0;
  87. time_t tEnd = 0;
  88. std::string strEmail;
  89. lpFbComp = icalcomponent_new(ICAL_VFREEBUSY_COMPONENT);
  90. if (lpFbComp == NULL)
  91. return MAPI_E_INVALID_PARAMETER;
  92. //DTSTART
  93. ittStamp = icaltime_from_timet_with_zone(tDtStart, false, icaltimezone_get_utc_timezone());
  94. lpicProp = icalproperty_new(ICAL_DTSTART_PROPERTY);
  95. if (lpicProp == NULL)
  96. return MAPI_E_NOT_ENOUGH_MEMORY;
  97. icalproperty_set_value(lpicProp, icalvalue_new_datetime(ittStamp));
  98. icalcomponent_add_property(lpFbComp, lpicProp);
  99. //DTEND
  100. ittStamp = icaltime_from_timet_with_zone(tDtEnd, false, icaltimezone_get_utc_timezone());
  101. lpicProp = icalproperty_new(ICAL_DTEND_PROPERTY);
  102. if (lpicProp == NULL)
  103. return MAPI_E_NOT_ENOUGH_MEMORY;
  104. icalproperty_set_value(lpicProp, icalvalue_new_datetime(ittStamp));
  105. icalcomponent_add_property(lpFbComp, lpicProp);
  106. //DTSTAMP
  107. ittStamp = icaltime_from_timet_with_zone(time(NULL), false, icaltimezone_get_utc_timezone());
  108. lpicProp = icalproperty_new(ICAL_DTSTAMP_PROPERTY);
  109. if (lpicProp == NULL)
  110. return MAPI_E_NOT_ENOUGH_MEMORY;
  111. icalproperty_set_value(lpicProp, icalvalue_new_datetime(ittStamp));
  112. icalcomponent_add_property(lpFbComp, lpicProp);
  113. //UID
  114. lpicProp = icalproperty_new(ICAL_UID_PROPERTY);
  115. if (lpicProp == NULL)
  116. return MAPI_E_NOT_ENOUGH_MEMORY;
  117. icalproperty_set_uid(lpicProp, strUID.c_str());
  118. icalcomponent_add_property(lpFbComp, lpicProp);
  119. //ORGANIZER
  120. lpicProp = icalproperty_new(ICAL_ORGANIZER_PROPERTY);
  121. if (lpicProp == NULL)
  122. return MAPI_E_NOT_ENOUGH_MEMORY;
  123. icalproperty_set_organizer(lpicProp, strOrganiser.c_str());
  124. icalcomponent_add_property(lpFbComp, lpicProp);
  125. //ATTENDEE
  126. strEmail = "mailto:" + strUser;
  127. lpicProp = icalproperty_new_attendee(strEmail.c_str());
  128. if (lpicProp == NULL)
  129. return MAPI_E_NOT_ENOUGH_MEMORY;
  130. // param PARTSTAT
  131. icalParam = icalparameter_new_partstat(ICAL_PARTSTAT_ACCEPTED);
  132. icalproperty_add_parameter(lpicProp, icalParam);
  133. // param CUTYPE
  134. icalParam = icalparameter_new_cutype(ICAL_CUTYPE_INDIVIDUAL);
  135. icalproperty_add_parameter(lpicProp, icalParam);
  136. icalcomponent_add_property(lpFbComp, lpicProp);
  137. // add all freebusy blocks
  138. for (int i = 0; i < ulBlocks; ++i) {
  139. // FREEBUSY
  140. lpicProp = icalproperty_new(ICAL_FREEBUSY_PROPERTY);
  141. if (lpicProp == NULL)
  142. return MAPI_E_NOT_ENOUGH_MEMORY;
  143. RTimeToUnixTime(lpsFbblk[i].m_tmStart, &tStart);
  144. RTimeToUnixTime(lpsFbblk[i].m_tmEnd, &tEnd);
  145. icalPeriod.start = icaltime_from_timet_with_zone(tStart, false, icaltimezone_get_utc_timezone());
  146. icalPeriod.end = icaltime_from_timet_with_zone(tEnd, false, icaltimezone_get_utc_timezone());
  147. icalproperty_set_freebusy(lpicProp, icalPeriod);
  148. switch (lpsFbblk[i].m_fbstatus)
  149. {
  150. case fbBusy:
  151. icalParam = icalparameter_new_fbtype (ICAL_FBTYPE_BUSY);
  152. break;
  153. case fbTentative:
  154. icalParam = icalparameter_new_fbtype (ICAL_FBTYPE_BUSYTENTATIVE);
  155. break;
  156. case fbOutOfOffice:
  157. icalParam = icalparameter_new_fbtype (ICAL_FBTYPE_BUSYUNAVAILABLE);
  158. break;
  159. default:
  160. icalParam = icalparameter_new_fbtype (ICAL_FBTYPE_FREE);
  161. break;
  162. }
  163. icalproperty_add_parameter (lpicProp, icalParam);
  164. icalcomponent_add_property(lpFbComp,lpicProp);
  165. }
  166. icalcomponent_end_component(lpFbComp, ICAL_VFREEBUSY_COMPONENT);
  167. *lpicFbComponent = lpFbComp;
  168. return hrSuccess;
  169. }
  170. } /* namespace */