icalrecurrence.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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 <memory>
  19. #include "icalrecurrence.h"
  20. #include "vconverter.h"
  21. #include "nameids.h"
  22. #include "valarm.h"
  23. #include <mapicode.h>
  24. #include <kopano/mapiext.h>
  25. #include <kopano/memory.hpp>
  26. #include <mapix.h>
  27. #include <mapiutil.h>
  28. #include <cmath>
  29. #include <algorithm>
  30. #include "freebusy.h"
  31. using namespace KCHL;
  32. static bool operator ==(const SPropValue &spv, ULONG ulPropTag)
  33. {
  34. return spv.ulPropTag == ulPropTag;
  35. }
  36. namespace KC {
  37. /**
  38. * Parses ical RRULE and convert it to mapi recurrence
  39. *
  40. * @param[in] sTimeZone Timezone structure
  41. * @param[in] lpicRootEvent Ical VCALENDAR component
  42. * @param[in] lpicEvent Ical VEVENT component containing the RRULE
  43. * @param[in] bIsAllday Allday status of the event
  44. * @param[in] lpNamedProps Named property tag array
  45. * @param[in] lpIcalItem Structure in which mapi properties are set
  46. * @return MAPI error code
  47. * @retval MAPI_E_NOT_FOUND Start or end time not found in ical data
  48. */
  49. HRESULT ICalRecurrence::HrParseICalRecurrenceRule(TIMEZONE_STRUCT sTimeZone, icalcomponent *lpicRootEvent, icalcomponent *lpicEvent,
  50. bool bIsAllday, LPSPropTagArray lpNamedProps, icalitem *lpIcalItem)
  51. {
  52. HRESULT hr = hrSuccess;
  53. std::unique_ptr<recurrence> lpRec;
  54. icalproperty *lpicProp = NULL;
  55. icalrecurrencetype icRRule;
  56. int i = 0;
  57. ULONG ulWeekDays = 0;
  58. time_t dtUTCStart = 0;
  59. time_t dtLocalStart = 0;
  60. time_t dtUTCEnd = 0;
  61. time_t dtUTCUntil = 0;
  62. time_t exUTCDate = 0;
  63. time_t exLocalDate = 0;
  64. SPropValue sPropVal = {0};
  65. struct tm tm = {0};
  66. lpicProp = icalcomponent_get_first_property(lpicEvent, ICAL_RRULE_PROPERTY);
  67. if (lpicProp == NULL)
  68. return hr;
  69. icRRule = icalproperty_get_rrule(lpicProp);
  70. lpicProp = icalcomponent_get_first_property(lpicEvent, ICAL_DTSTART_PROPERTY);
  71. if (lpicProp == nullptr)
  72. return MAPI_E_NOT_FOUND;
  73. // use localtime for calculating weekday as in UTC time
  74. // the weekday can change to previous day for time 00:00 am
  75. dtLocalStart = icaltime_as_timet(icalproperty_get_dtstart(lpicProp));
  76. gmtime_safe(&dtLocalStart, &tm);
  77. dtUTCStart = ICalTimeTypeToUTC(lpicRootEvent, lpicProp);
  78. lpicProp = icalcomponent_get_first_property(lpicEvent, ICAL_DTEND_PROPERTY);
  79. if (!lpicProp) {
  80. // check for Task's DUE property.
  81. lpicProp = icalcomponent_get_first_property(lpicEvent, ICAL_DUE_PROPERTY);
  82. }
  83. if (!lpicProp)
  84. {
  85. // check for duration property
  86. lpicProp = icalcomponent_get_first_property(lpicEvent, ICAL_DURATION_PROPERTY);
  87. if (lpicProp == nullptr)
  88. return MAPI_E_NOT_FOUND;
  89. dtUTCEnd = dtUTCStart + icaldurationtype_as_int(icalproperty_get_duration(lpicProp));
  90. } else {
  91. dtUTCEnd = ICalTimeTypeToUTC(lpicRootEvent, lpicProp);
  92. }
  93. lpRec.reset(new recurrence);
  94. // recurrence class contains LOCAL times only, so convert UTC -> LOCAL
  95. lpRec->setStartDateTime(dtLocalStart);
  96. // default 1st day of week is sunday, except in weekly recurrences
  97. lpRec->setFirstDOW(0);
  98. sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_RECURRENCETYPE], PT_LONG);
  99. switch (icRRule.freq) {
  100. case ICAL_DAILY_RECURRENCE:
  101. sPropVal.Value.ul = 1;
  102. lpRec->setFrequency(recurrence::DAILY);
  103. break;
  104. case ICAL_WEEKLY_RECURRENCE:
  105. sPropVal.Value.ul = 2;
  106. lpRec->setFrequency(recurrence::WEEKLY);
  107. // assume this weekly item is exactly on the start time day
  108. lpRec->setWeekDays(1 << tm.tm_wday);
  109. // Strange little thing for the recurrence type "every workday"
  110. lpRec->setFirstDOW(1);
  111. break;
  112. case ICAL_MONTHLY_RECURRENCE:
  113. sPropVal.Value.ul = 3;
  114. lpRec->setFrequency(recurrence::MONTHLY);
  115. break;
  116. case ICAL_YEARLY_RECURRENCE:
  117. sPropVal.Value.ul = 4;
  118. lpRec->setFrequency(recurrence::YEARLY);
  119. lpRec->setDayOfMonth(tm.tm_mday);
  120. if (icRRule.by_month[0] != ICAL_RECURRENCE_ARRAY_MAX)
  121. lpRec->setMonth(icRRule.by_month[0]);
  122. break;
  123. default:
  124. return MAPI_E_INVALID_PARAMETER;
  125. };
  126. lpIcalItem->lstMsgProps.push_back(sPropVal);
  127. // since we know the frequency, this value can be set correctly
  128. lpRec->setInterval(icRRule.interval);
  129. // ulWeekDays, ulWeekNumber (monthly)
  130. if (icRRule.by_day[i] != ICAL_RECURRENCE_ARRAY_MAX) {
  131. ulWeekDays = 0;
  132. if (icRRule.by_day[0] < 0) {
  133. // outlook can only have _one_ last day of the month/year (not daily or weekly here!)
  134. ulWeekDays |= (1 << (abs(icRRule.by_day[0]%8) - 1));
  135. // next call also changes pattern to 3!
  136. lpRec->setWeekNumber(5);
  137. } else if (icRRule.by_day[0] >= 1 && icRRule.by_day[0] <= 7) {
  138. // weekly, normal days
  139. i = 0;
  140. while (icRRule.by_day[i] != ICAL_RECURRENCE_ARRAY_MAX) {
  141. ulWeekDays |= (1 << (icRRule.by_day[i] - 1));
  142. ++i;
  143. }
  144. // handle the BYSETPOS value
  145. if (icRRule.by_set_pos[0] != ICAL_RECURRENCE_ARRAY_MAX) {
  146. // Monthly every Nth [mo/to/we/th/fr/sa/su] day
  147. lpRec->setWeekNumber(icRRule.by_set_pos[0]);
  148. } else if (lpRec->getFrequency() == recurrence::MONTHLY) {
  149. // A monthly every [mo/tu/we/th/fr/sa/su]day is not supported in outlook. but this is the same as
  150. // weekly x day so convert this item to weekly x day
  151. lpRec->setFrequency(recurrence::WEEKLY);
  152. // assume this weekly item is exactly on the start time day
  153. lpRec->setWeekDays(1 << tm.tm_wday);
  154. // Strange little thing for the recurrence type "every workday"
  155. lpRec->setFirstDOW(1);
  156. sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_RECURRENCETYPE], PT_LONG);
  157. sPropVal.Value.ul = 2;
  158. lpIcalItem->lstMsgProps.push_back(sPropVal);
  159. } else if (lpRec->getFrequency() == recurrence::YEARLY) {
  160. return MAPI_E_NO_SUPPORT;
  161. }
  162. } else {
  163. // monthly, first sunday: 9, monday: 10
  164. ulWeekDays |= (1 << ((icRRule.by_day[0]%8) - 1));
  165. lpRec->setWeekNumber((int)(icRRule.by_day[0]/8)); // 1..4
  166. }
  167. lpRec->setWeekDays(ulWeekDays);
  168. } else if (icRRule.by_month_day[0] != ICAL_RECURRENCE_ARRAY_MAX) {
  169. // outlook can only have _one_ day in the month/year, while ical can do multiple
  170. // do we need to create multiple items here ?? :(
  171. lpRec->setDayOfMonth(icRRule.by_month_day[0]);
  172. }else if (icRRule.freq == ICAL_MONTHLY_RECURRENCE || icRRule.freq == ICAL_YEARLY_RECURRENCE) {
  173. // When RRULE:FREQ=MONTHLY;INTERVAL=1 is set then day of the month is set from the start date.
  174. lpRec->setDayOfMonth(tm.tm_mday);
  175. }
  176. lpRec->setStartDateTime(lpRec->calcStartDate());
  177. if (icRRule.count != 0) {
  178. // count limit
  179. lpRec->setEndType(recurrence::NUMBER);
  180. lpRec->setCount(icRRule.count);
  181. // calculate end
  182. lpRec->setEndDate(lpRec->calcEndDate());
  183. dtUTCUntil = lpRec->getEndDate();
  184. } else if (icRRule.until.year != 0) {
  185. // date limit
  186. lpRec->setEndType(recurrence::DATE);
  187. // enddate is the date/time of the LAST occurrence, do not add duration
  188. dtUTCUntil = icaltime_as_timet_with_zone(icRRule.until, NULL);
  189. lpRec->setEndDate(UTCToLocal(dtUTCUntil, sTimeZone));
  190. // calculate number
  191. lpRec->setCount(lpRec->calcCount());
  192. } else {
  193. // never ending story
  194. lpRec->setEndType(recurrence::NEVER);
  195. // outlook also sets 10, so this get's displayed in the recurrence dialog window
  196. lpRec->setCount(10);
  197. // 1. dtUTCEnd -> end time offset (set later)
  198. // (date will overridden by recurrence.cpp when writing the blob)
  199. // 2. dtUTCUntil = clipend == start of month of item in 4500
  200. dtUTCUntil = 0x7FFFFFFF; // incorrect, but good enough
  201. }
  202. // offset in minutes after midnight of the start time
  203. lpRec->setEndTimeOffset((lpRec->getStartTimeOffset() + dtUTCEnd - dtUTCStart) / 60);
  204. // Set 0x8236, also known as ClipEnd in OutlookSpy
  205. UnixTimeToFileTime(dtUTCUntil, &sPropVal.Value.ft);
  206. sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_RECURRENCE_END], PT_SYSTIME);
  207. lpIcalItem->lstMsgProps.push_back(sPropVal);
  208. // find EXDATE properties, add to delete exceptions
  209. lpicProp = icalcomponent_get_first_property(lpicEvent, ICAL_EXDATE_PROPERTY);
  210. while (lpicProp != NULL)
  211. {
  212. exUTCDate = ICalTimeTypeToUTC(lpicRootEvent, lpicProp);
  213. exLocalDate = UTCToLocal(exUTCDate, sTimeZone);
  214. lpRec->addDeletedException(exLocalDate);
  215. lpicProp = icalcomponent_get_next_property(lpicEvent, ICAL_EXDATE_PROPERTY);
  216. }
  217. // now that we have a full recurrence object, recalculate the end time, see ZCP-9143
  218. if (lpRec->getEndType() == recurrence::DATE)
  219. {
  220. memory_ptr<OccrInfo> lpOccrInfo;
  221. ULONG cValues = 0;
  222. hr = lpRec->HrGetItems(dtUTCStart, dtUTCUntil, sTimeZone, 0, &~lpOccrInfo, &cValues, true);
  223. if (hr == hrSuccess && cValues > 0) {
  224. dtUTCUntil = lpOccrInfo[cValues-1].tBaseDate;
  225. lpRec->setEndDate(UTCToLocal(dtUTCUntil, sTimeZone));
  226. }
  227. }
  228. // set named prop 0x8510 to 353, needed for Outlook to ask for single or total recurrence when deleting
  229. sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_SIDEEFFECT], PT_LONG);
  230. sPropVal.Value.ul = 353;
  231. lpIcalItem->lstMsgProps.push_back(sPropVal);
  232. // Set 0x8235, also known as ClipStart in OutlookSpy
  233. UnixTimeToFileTime(LocalToUTC(recurrence::StartOfDay(lpRec->getStartDateTime()), sTimeZone), &sPropVal.Value.ft);
  234. sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_RECURRENCE_START], PT_SYSTIME);
  235. lpIcalItem->lstMsgProps.push_back(sPropVal);
  236. lpicProp = icalcomponent_get_first_property(lpicEvent, ICAL_RECURRENCEID_PROPERTY);
  237. if(!lpicProp) {
  238. UnixTimeToFileTime(LocalToUTC(lpRec->getStartDateTime(), sTimeZone), &sPropVal.Value.ft);
  239. // Set 0x820D / ApptStartWhole
  240. sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_APPTSTARTWHOLE], PT_SYSTIME);
  241. lpIcalItem->lstMsgProps.push_back(sPropVal);
  242. // Set 0x8516 / CommonStart
  243. sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_COMMONSTART], PT_SYSTIME);
  244. lpIcalItem->lstMsgProps.push_back(sPropVal);
  245. UnixTimeToFileTime(LocalToUTC(lpRec->getStartDateTime() + (dtUTCEnd - dtUTCStart), sTimeZone), &sPropVal.Value.ft);
  246. // Set 0x820E / ApptEndWhole
  247. sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_APPTENDWHOLE], PT_SYSTIME);
  248. lpIcalItem->lstMsgProps.push_back(sPropVal);
  249. // Set CommonEnd
  250. sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_COMMONEND], PT_SYSTIME);
  251. lpIcalItem->lstMsgProps.push_back(sPropVal);
  252. }
  253. lpIcalItem->lpRecurrence = lpRec.release();
  254. return hr;
  255. }
  256. /**
  257. * Parses ical exception and sets Mapi propertis in icalitem::exception structure
  258. *
  259. * @param[in] lpEventRoot ical VCALENDAR component
  260. * @param[in] lpicEvent ical VEVENT component
  261. * @param[in,out] lpIcalItem icalitem in which recurrence structure is set
  262. * @param[in] bIsAllDay Allday status of the exception
  263. * @param[in] lpNamedProps named property tag array
  264. * @param[in] strCharset charset to convert to
  265. * @param[out] lpEx exception structure in which mapi properties are set
  266. *
  267. * @return MAPI error code
  268. * @retval MAPI_E_NOT_FOUND start time or end time of event is not set in ical data
  269. */
  270. HRESULT ICalRecurrence::HrMakeMAPIException(icalcomponent *lpEventRoot, icalcomponent *lpicEvent, icalitem *lpIcalItem, bool bIsAllDay, LPSPropTagArray lpNamedProps, std::string& strCharset, icalitem::exception *lpEx)
  271. {
  272. HRESULT hr;
  273. icalproperty *lpicProp = NULL;
  274. time_t ttStartLocalTime = 0;
  275. time_t ttEndLocalTime = 0;
  276. time_t ttStartUtcTime = 0;
  277. time_t ttEndUtcTime = 0;
  278. time_t ttOriginalUtcTime = 0;
  279. time_t ttOriginalLocalTime = 0;
  280. ULONG ulId = 0;
  281. ULONG i = 0;
  282. SPropValue sPropVal;
  283. std::wstring strIcalProp;
  284. bool bXMS = false;
  285. icalcomponent *lpicAlarm = NULL;
  286. LONG ulRemindBefore = 0;
  287. time_t ttReminderTime = 0;
  288. bool bReminderSet = false;
  289. convert_context converter;
  290. const char *lpszProp;
  291. bool abOldPresent[8] = {false};
  292. bool abNewPresent[8] = {false};
  293. SizedSPropTagArray(8, sptaCopy) = { 8, {
  294. PR_SUBJECT,
  295. CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_LOCATION], PT_STRING8),
  296. PR_BODY,
  297. CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_LABEL], PT_LONG),
  298. CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_REMINDERSET], PT_BOOLEAN),
  299. CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_REMINDERMINUTESBEFORESTART], PT_LONG),
  300. CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_ALLDAYEVENT], PT_BOOLEAN),
  301. CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_BUSYSTATUS], PT_LONG)
  302. } };
  303. bool bOldIsAllDay = false;
  304. lpicProp = icalcomponent_get_first_property(lpicEvent, ICAL_RECURRENCEID_PROPERTY);
  305. if (lpicProp == NULL)
  306. // you tricked me! it's not an exception at all!
  307. return MAPI_E_NOT_FOUND;
  308. ttOriginalUtcTime = ICalTimeTypeToUTC(lpEventRoot, lpicProp);
  309. ttOriginalLocalTime = icaltime_as_timet(icalvalue_get_datetime(icalproperty_get_value(lpicProp)));
  310. lpEx->tBaseDate = ttOriginalUtcTime;
  311. lpicProp = icalcomponent_get_first_property(lpicEvent, ICAL_DTSTART_PROPERTY);
  312. if (lpicProp == NULL)
  313. return MAPI_E_NOT_FOUND;
  314. ttStartUtcTime = ICalTimeTypeToUTC(lpEventRoot, lpicProp);
  315. ttStartLocalTime = icaltime_as_timet(icalvalue_get_datetime(icalproperty_get_value(lpicProp)));
  316. lpEx->tStartDate = ttStartUtcTime;
  317. lpicProp = icalcomponent_get_first_property(lpicEvent, ICAL_DTEND_PROPERTY);
  318. if (lpicProp == NULL)
  319. return MAPI_E_NOT_FOUND;
  320. ttEndUtcTime = ICalTimeTypeToUTC(lpEventRoot, lpicProp);
  321. ttEndLocalTime = icaltime_as_timet(icalvalue_get_datetime(icalproperty_get_value(lpicProp)));
  322. hr = lpIcalItem->lpRecurrence->addModifiedException(ttStartLocalTime, ttEndLocalTime, ttOriginalLocalTime, &ulId);
  323. if (hr != hrSuccess)
  324. return hr;
  325. sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_RECURRINGBASE], PT_SYSTIME);
  326. UnixTimeToFileTime(ttOriginalUtcTime, &sPropVal.Value.ft);
  327. lpEx->lstMsgProps.push_back(sPropVal);
  328. sPropVal.ulPropTag = PR_EXCEPTION_STARTTIME;
  329. UnixTimeToFileTime(ttStartLocalTime, &sPropVal.Value.ft);
  330. lpEx->lstAttachProps.push_back(sPropVal);
  331. sPropVal.ulPropTag = PR_EXCEPTION_ENDTIME;
  332. UnixTimeToFileTime(ttEndLocalTime, &sPropVal.Value.ft);
  333. lpEx->lstAttachProps.push_back(sPropVal);
  334. sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_APPTSTARTWHOLE], PT_SYSTIME);
  335. UnixTimeToFileTime(ttStartUtcTime, &sPropVal.Value.ft);
  336. lpEx->lstMsgProps.push_back(sPropVal);
  337. sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_COMMONSTART], PT_SYSTIME);
  338. lpEx->lstMsgProps.push_back(sPropVal);
  339. sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_RECURRENCE_START], PT_SYSTIME);
  340. lpEx->lstMsgProps.push_back(sPropVal);
  341. sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_APPTENDWHOLE], PT_SYSTIME);
  342. UnixTimeToFileTime(ttEndUtcTime, &sPropVal.Value.ft);
  343. lpEx->lstMsgProps.push_back(sPropVal);
  344. sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_COMMONEND], PT_SYSTIME);
  345. lpEx->lstMsgProps.push_back(sPropVal);
  346. sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_RECURRENCE_END], PT_SYSTIME);
  347. lpEx->lstMsgProps.push_back(sPropVal);
  348. sPropVal.ulPropTag = PR_EXCEPTION_ENDTIME;
  349. UnixTimeToFileTime(ttEndLocalTime, &sPropVal.Value.ft);
  350. lpEx->lstAttachProps.push_back(sPropVal);
  351. sPropVal.ulPropTag = PR_DISPLAY_NAME_W;
  352. HrCopyString(lpIcalItem->base, L"Untitled", &sPropVal.Value.lpszW);
  353. lpEx->lstAttachProps.push_back(sPropVal);
  354. sPropVal.ulPropTag = PR_ATTACH_METHOD;
  355. sPropVal.Value.ul = ATTACH_EMBEDDED_MSG;
  356. lpEx->lstAttachProps.push_back(sPropVal);
  357. sPropVal.ulPropTag = PR_ATTACH_FLAGS;
  358. sPropVal.Value.ul = 0;
  359. lpEx->lstAttachProps.push_back(sPropVal);
  360. sPropVal.ulPropTag = PR_ATTACHMENT_LINKID;
  361. sPropVal.Value.ul = 0;
  362. lpEx->lstAttachProps.push_back(sPropVal);
  363. sPropVal.ulPropTag = PR_ATTACHMENT_HIDDEN;
  364. sPropVal.Value.b = TRUE;
  365. lpEx->lstAttachProps.push_back(sPropVal);
  366. sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_HIDE_ATTACH], PT_BOOLEAN);;
  367. sPropVal.Value.b = TRUE;
  368. lpIcalItem->lstMsgProps.push_back(sPropVal);
  369. sPropVal.ulPropTag = PR_ATTACHMENT_FLAGS;
  370. sPropVal.Value.ul = 2;
  371. lpEx->lstAttachProps.push_back(sPropVal);
  372. sPropVal.ulPropTag = PR_MESSAGE_CLASS_W;
  373. HrCopyString(lpIcalItem->base, L"IPM.OLE.CLASS.{00061055-0000-0000-C000-000000000046}", &sPropVal.Value.lpszW);
  374. lpEx->lstMsgProps.push_back(sPropVal);
  375. // copy properties to exception and test if changed
  376. for (const auto &prop : lpIcalItem->lstMsgProps)
  377. for (i = 0; i < sptaCopy.cValues; ++i) {
  378. if (sptaCopy.aulPropTag[i] == prop.ulPropTag) {
  379. abOldPresent[i] = true;
  380. if (sptaCopy.aulPropTag[i] != PR_BODY) // no need to copy body
  381. lpEx->lstMsgProps.push_back(prop);
  382. if (sptaCopy.aulPropTag[i] == CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_ALLDAYEVENT], PT_BOOLEAN))
  383. bOldIsAllDay = prop.Value.b; // remember allday event status
  384. break;
  385. }
  386. }
  387. // find exceptional properties
  388. // TODO: should actually look at original message, and check for differences
  389. lpicProp = icalcomponent_get_first_property(lpicEvent, ICAL_ANY_PROPERTY);
  390. while (lpicProp) {
  391. switch (icalproperty_isa(lpicProp)) {
  392. case ICAL_SUMMARY_PROPERTY:
  393. lpszProp = icalproperty_get_summary(lpicProp);
  394. strIcalProp = converter.convert_to<std::wstring>(lpszProp, rawsize(lpszProp), strCharset.c_str());
  395. hr = lpIcalItem->lpRecurrence->setModifiedSubject(ulId, strIcalProp.c_str());
  396. if (hr != hrSuccess)
  397. return hr;
  398. sPropVal.ulPropTag = PR_SUBJECT_W;
  399. HrCopyString(lpIcalItem->base, strIcalProp.c_str(), &sPropVal.Value.lpszW);
  400. lpEx->lstMsgProps.push_back(sPropVal);
  401. abNewPresent[0] = true;
  402. break;
  403. case ICAL_LOCATION_PROPERTY:
  404. lpszProp = icalproperty_get_location(lpicProp);
  405. strIcalProp = converter.convert_to<std::wstring>(lpszProp, rawsize(lpszProp), strCharset.c_str());
  406. hr = lpIcalItem->lpRecurrence->setModifiedLocation(ulId, strIcalProp.c_str());
  407. if (hr != hrSuccess)
  408. return hr;
  409. sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_LOCATION], PT_UNICODE);
  410. HrCopyString(lpIcalItem->base, strIcalProp.c_str(), &sPropVal.Value.lpszW);
  411. lpEx->lstMsgProps.push_back(sPropVal);
  412. abNewPresent[1] = true;
  413. break;
  414. case ICAL_TRANSP_PROPERTY:
  415. if (!bXMS) {
  416. ULONG ulBusyStatus = 2; // default busy
  417. switch(icalproperty_get_transp(lpicProp)){
  418. case ICAL_TRANSP_TRANSPARENT:
  419. ulBusyStatus = 0;
  420. break;
  421. case ICAL_TRANSP_OPAQUE:
  422. ulBusyStatus = 2;
  423. break;
  424. default:
  425. break;
  426. }
  427. hr = lpIcalItem->lpRecurrence->setModifiedBusyStatus(ulId, ulBusyStatus);
  428. if (hr != hrSuccess)
  429. return hr;
  430. sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_BUSYSTATUS], PT_LONG);
  431. sPropVal.Value.ul = ulBusyStatus;
  432. lpEx->lstMsgProps.push_back(sPropVal);
  433. abNewPresent[7] = true;
  434. }
  435. break;
  436. case ICAL_X_PROPERTY:
  437. if (strcmp(icalproperty_get_x_name(lpicProp), "X-MICROSOFT-CDO-BUSYSTATUS") == 0) {
  438. ULONG ulBusyStatus = 2; // default busy
  439. const char *lpszIcalProp = icalproperty_get_x(lpicProp);
  440. if (strcmp(lpszIcalProp, "FREE") == 0)
  441. ulBusyStatus = 0;
  442. else if (strcmp(lpszIcalProp, "TENTATIVE") == 0)
  443. ulBusyStatus = 1;
  444. else if (strcmp(lpszIcalProp, "OOF") == 0)
  445. ulBusyStatus = 3;
  446. hr = lpIcalItem->lpRecurrence->setModifiedBusyStatus(ulId, ulBusyStatus);
  447. if (hr != hrSuccess)
  448. return hr;
  449. sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_BUSYSTATUS], PT_LONG);
  450. sPropVal.Value.ul = ulBusyStatus;
  451. lpEx->lstMsgProps.push_back(sPropVal);
  452. bXMS = true;
  453. abNewPresent[7] = true;
  454. }
  455. break;
  456. case ICAL_DESCRIPTION_PROPERTY:
  457. lpszProp = icalproperty_get_description(lpicProp);
  458. strIcalProp = converter.convert_to<std::wstring>(lpszProp, rawsize(lpszProp), strCharset.c_str());
  459. hr = lpIcalItem->lpRecurrence->setModifiedBody(ulId);
  460. if (hr != hrSuccess)
  461. return hr;
  462. sPropVal.ulPropTag = PR_BODY_W;
  463. HrCopyString(lpIcalItem->base, strIcalProp.c_str(), &sPropVal.Value.lpszW);
  464. lpEx->lstMsgProps.push_back(sPropVal);
  465. abNewPresent[2] = true;
  466. break;
  467. default:
  468. // ignore property
  469. break;
  470. };
  471. lpicProp = icalcomponent_get_next_property(lpicEvent, ICAL_ANY_PROPERTY);
  472. }
  473. // make sure these are not removed :| (body, label, reminderset, reminder minutes)
  474. abNewPresent[2] = abNewPresent[3] = abNewPresent[4] = abNewPresent[5] = true;
  475. // test if properties were just removed
  476. for (i = 0; i < sptaCopy.cValues; ++i) {
  477. if (abOldPresent[i] == true && abNewPresent[i] == false) {
  478. auto iProp = find(lpEx->lstMsgProps.begin(), lpEx->lstMsgProps.end(), sptaCopy.aulPropTag[i]);
  479. if (iProp != lpEx->lstMsgProps.cend()) {
  480. lpEx->lstMsgProps.erase(iProp);
  481. switch (i) {
  482. case 0:
  483. // subject
  484. hr = lpIcalItem->lpRecurrence->setModifiedSubject(ulId, std::wstring());
  485. if (hr != hrSuccess)
  486. return hr;
  487. sPropVal.ulPropTag = PR_SUBJECT_W;
  488. sPropVal.Value.lpszW = const_cast<wchar_t *>(L"");
  489. lpEx->lstMsgProps.push_back(sPropVal);
  490. break;
  491. case 1:
  492. // location
  493. hr = lpIcalItem->lpRecurrence->setModifiedLocation(ulId, std::wstring());
  494. if (hr != hrSuccess)
  495. return hr;
  496. sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_LOCATION], PT_UNICODE);
  497. sPropVal.Value.lpszW = const_cast<wchar_t *>(L"");
  498. lpEx->lstMsgProps.push_back(sPropVal);
  499. break;
  500. case 2:
  501. // body, ignore!
  502. break;
  503. case 3:
  504. // label, ignore
  505. break;
  506. case 4:
  507. // reminder set, ignore
  508. break;
  509. case 5:
  510. // reminder minutes, ignore
  511. break;
  512. case 6:
  513. // allday event
  514. if (bIsAllDay != bOldIsAllDay) {
  515. // flip all day status
  516. hr = lpIcalItem->lpRecurrence->setModifiedSubType(ulId, 1);
  517. if (hr != hrSuccess)
  518. return hr;
  519. sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_ALLDAYEVENT], PT_BOOLEAN);
  520. sPropVal.Value.ul = !bOldIsAllDay;
  521. lpEx->lstMsgProps.push_back(sPropVal);
  522. }
  523. break;
  524. case 7:
  525. // busy status, default: busy
  526. hr = lpIcalItem->lpRecurrence->setModifiedBusyStatus(ulId, 1);
  527. if (hr != hrSuccess)
  528. return hr;
  529. sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_BUSYSTATUS], PT_LONG);
  530. sPropVal.Value.ul = 1;
  531. lpEx->lstMsgProps.push_back(sPropVal);
  532. break;
  533. };
  534. }
  535. }
  536. }
  537. // reminderset (flip reminder on/off) and reminderdelta (offset time change) in vevent component of this item
  538. lpicAlarm = icalcomponent_get_first_component(lpicEvent, ICAL_VALARM_COMPONENT);
  539. if (lpicAlarm) {
  540. hr = HrParseVAlarm(lpicAlarm, &ulRemindBefore, &ttReminderTime, &bReminderSet);
  541. if (hr == hrSuccess) {
  542. // abOldPresent[4] == reminderset marker in original message
  543. if (bReminderSet != abOldPresent[4]) {
  544. hr = lpIcalItem->lpRecurrence->setModifiedReminder(ulId, bReminderSet);
  545. if (hr != hrSuccess)
  546. return hr;
  547. sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_REMINDERSET], PT_BOOLEAN);
  548. sPropVal.Value.b = bReminderSet;
  549. lpEx->lstMsgProps.push_back(sPropVal);
  550. if (ttReminderTime == 0)
  551. ttReminderTime = ttStartLocalTime;
  552. sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_REMINDERTIME], PT_SYSTIME);
  553. UnixTimeToFileTime(ttReminderTime, &sPropVal.Value.ft);
  554. lpEx->lstMsgProps.push_back(sPropVal);
  555. }
  556. hr = lpIcalItem->lpRecurrence->setModifiedReminderDelta(ulId, ulRemindBefore);
  557. if (hr != hrSuccess)
  558. return hr;
  559. sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_REMINDERMINUTESBEFORESTART], PT_LONG);
  560. sPropVal.Value.ul = ulRemindBefore;
  561. lpEx->lstMsgProps.push_back(sPropVal);
  562. }
  563. } else if (abOldPresent[4]) {
  564. // disable reminder in attachment
  565. hr = lpIcalItem->lpRecurrence->setModifiedReminder(ulId, 0);
  566. if (hr != hrSuccess)
  567. return hr;
  568. sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_REMINDERSET], PT_BOOLEAN);
  569. sPropVal.Value.b = FALSE;
  570. lpEx->lstMsgProps.push_back(sPropVal);
  571. }
  572. return hr;
  573. }
  574. /**
  575. * Finalizes recurrence struct with exceptions into the mapi message
  576. *
  577. * @param[in] lpRecurrence recurrence object
  578. * @param[in] lpNamedProps Named property tag array
  579. * @param[in,out] lpMessage The mapi message in which the recurrence is stored
  580. * @return MAPI error code
  581. * @retval MAPI_E_CORRUPT_DATA Invalid recurrence state
  582. */
  583. HRESULT ICalRecurrence::HrMakeMAPIRecurrence(recurrence *lpRecurrence, LPSPropTagArray lpNamedProps, LPMESSAGE lpMessage)
  584. {
  585. HRESULT hr = hrSuccess;
  586. memory_ptr<char> lpRecBlob;
  587. unsigned int ulRecBlob = 0;
  588. memory_ptr<SPropValue> lpPropVal, lpsPropRecPattern;
  589. std::string strHRS;
  590. ULONG i = 0;
  591. hr = lpRecurrence->HrGetRecurrenceState(&~lpRecBlob, &ulRecBlob);
  592. if (hr != hrSuccess)
  593. return hr;
  594. hr = lpRecurrence->HrGetHumanReadableString(&strHRS);
  595. if (hr != hrSuccess)
  596. return hr;
  597. // adjust number of props
  598. hr = MAPIAllocateBuffer(sizeof(SPropValue) * 4, &~lpPropVal);
  599. if (hr != hrSuccess)
  600. return hr;
  601. lpPropVal[i].ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_RECURRING], PT_BOOLEAN);
  602. lpPropVal[i].Value.b = TRUE;
  603. ++i;
  604. // TODO: combine with icon index in vevent .. the item may be a meeting request (meeting+recurring==1027)
  605. lpPropVal[i].ulPropTag = PR_ICON_INDEX;
  606. lpPropVal[i].Value.ul = ICON_APPT_RECURRING;
  607. ++i;
  608. lpPropVal[i].ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_RECURRENCESTATE], PT_BINARY);
  609. lpPropVal[i].Value.bin.lpb = reinterpret_cast<BYTE *>(lpRecBlob.get());
  610. lpPropVal[i].Value.bin.cb = ulRecBlob;
  611. ++i;
  612. hr = HrGetOneProp(lpMessage, CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_RECURRENCEPATTERN], PT_STRING8), &~lpsPropRecPattern);
  613. if(hr != hrSuccess)
  614. {
  615. lpPropVal[i].ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_RECURRENCEPATTERN], PT_STRING8);
  616. lpPropVal[i].Value.lpszA = (char*)strHRS.c_str();
  617. ++i;
  618. }
  619. return lpMessage->SetProps(i, lpPropVal, NULL);
  620. }
  621. /**
  622. * Check if the exception is valid and according to outlook behaviour
  623. *
  624. * @param[in] lpItem structure containing mapi properties
  625. * @param[in] lpEx Structure containing mapi properties of exception
  626. * @return True if the occurrence is valid else false
  627. */
  628. bool ICalRecurrence::HrValidateOccurrence(icalitem *lpItem, icalitem::exception lpEx)
  629. {
  630. HRESULT hr = hrSuccess;
  631. memory_ptr<OccrInfo> lpFBBlocksAll;
  632. ULONG cValues = 0;
  633. time_t tBaseDateStart = LocalToUTC(lpItem->lpRecurrence->StartOfDay(UTCToLocal(lpEx.tBaseDate, lpItem->tTZinfo)), lpItem->tTZinfo);
  634. time_t tStartDateStart = LocalToUTC(lpItem->lpRecurrence->StartOfDay(UTCToLocal(lpEx.tStartDate, lpItem->tTZinfo)), lpItem->tTZinfo);
  635. if (tBaseDateStart < tStartDateStart)
  636. hr = lpItem->lpRecurrence->HrGetItems(tBaseDateStart, tStartDateStart + 1439 * 60, lpItem->tTZinfo, lpItem->ulFbStatus, &~lpFBBlocksAll, &cValues);
  637. else
  638. hr = lpItem->lpRecurrence->HrGetItems(tStartDateStart, tBaseDateStart + 1439 * 60, lpItem->tTZinfo, lpItem->ulFbStatus, &~lpFBBlocksAll, &cValues);
  639. if (hr != hrSuccess)
  640. return false;
  641. return cValues == 1;
  642. }
  643. /**
  644. * Sets recurrence in ical data and exdate for exceptions
  645. *
  646. * @param[in] sTimezone timezone structure
  647. * @param[in] bIsAllDay boolean to specify if event is allday or not
  648. * @param[in] lpRecurrence Mapi recurrence strucuture
  649. * @param[in,out] lpicEvent ical component to which recurrence and exdates are added
  650. * @return MAPI error code
  651. * @retval MAPI_E_INVALID_PARAMETER Invalid recurrence type is set in mapi recurrence
  652. */
  653. HRESULT ICalRecurrence::HrCreateICalRecurrence(TIMEZONE_STRUCT sTimeZone, bool bIsAllDay, recurrence *lpRecurrence, icalcomponent *lpicEvent)
  654. {
  655. icalrecurrencetype icRRule;
  656. std::list<time_t> lstExceptions;
  657. icaltimetype ittExDate;
  658. TIMEZONE_STRUCT sTZgmt = {0};
  659. HRESULT hr = HrCreateICalRecurrenceType(sTimeZone, bIsAllDay, lpRecurrence, &icRRule);
  660. if (hr != hrSuccess)
  661. return hr;
  662. icalcomponent_add_property(lpicEvent, icalproperty_new_rrule(icRRule));
  663. // all delete exceptions are in the delete list,
  664. lstExceptions = lpRecurrence->getDeletedExceptions();
  665. if (!lstExceptions.empty()) {
  666. // add EXDATE props
  667. for (const auto &exc : lstExceptions) {
  668. if(bIsAllDay)
  669. ittExDate = icaltime_from_timet_with_zone(LocalToUTC(exc, sTZgmt), bIsAllDay, nullptr);
  670. else
  671. ittExDate = icaltime_from_timet_with_zone(LocalToUTC(exc, sTimeZone), 0, nullptr);
  672. ittExDate.is_utc = 1;
  673. icalcomponent_add_property(lpicEvent, icalproperty_new_exdate(ittExDate));
  674. }
  675. }
  676. // modified exceptions are done by the caller because of the attachments with info
  677. return hrSuccess;
  678. }
  679. /**
  680. * Creates ical recurrence structure from mapi recurrence.
  681. *
  682. * @param[in] sTimeZone Timezone structure, unused currently
  683. * @param[in] bIsAllday Flag that specifies if the recurrence is all day
  684. * @param[in] lpRecurrence mapi recurrence structure to be converted
  685. * @param[out] lpicRRule ical recurrence structure to be returned
  686. * @return MAPI error code
  687. * @retval MAPI_E_INVALID_PARAMETER invalid recurrence type is set in mapi recurrence structure
  688. */
  689. HRESULT ICalRecurrence::HrCreateICalRecurrenceType(TIMEZONE_STRUCT sTimeZone, bool bIsAllday, recurrence *lpRecurrence, icalrecurrencetype *lpicRRule)
  690. {
  691. struct icalrecurrencetype icRec;
  692. icalrecurrencetype_clear(&icRec);
  693. switch (lpRecurrence->getFrequency()) {
  694. case recurrence::DAILY:
  695. icRec.freq = ICAL_DAILY_RECURRENCE;
  696. // only weekdays selected in outlook
  697. if (lpRecurrence->getWeekDays()) {
  698. // iCal.app does not have daily-weekday type of recurrence
  699. // so Daily-weekdays is converted to weekly recurrence
  700. icRec.freq = ICAL_WEEKLY_RECURRENCE;
  701. WeekDaysToICalArray(lpRecurrence->getWeekDays(), &icRec);
  702. }
  703. break;
  704. case recurrence::WEEKLY:
  705. icRec.freq = ICAL_WEEKLY_RECURRENCE;
  706. WeekDaysToICalArray(lpRecurrence->getWeekDays(), &icRec);
  707. break;
  708. case recurrence::MONTHLY:
  709. icRec.freq = ICAL_MONTHLY_RECURRENCE;
  710. if (lpRecurrence->getWeekNumber() == 0) {
  711. // mapi patterntype == 2
  712. icRec.by_month_day[0] = lpRecurrence->getDayOfMonth();
  713. icRec.by_month_day[1] = ICAL_RECURRENCE_ARRAY_MAX;
  714. break;
  715. }
  716. // mapi patterntype == 3
  717. // only 1 day should be set!
  718. if (lpRecurrence->getWeekDays() == 127) {
  719. // All Weekdays are set for recurrence type "second" "day" of month.
  720. // SU,MO,TU,WE,TH,FR,SA -> weekdays = 127.
  721. icRec.by_month_day[0] = lpRecurrence->getWeekNumber() == 5 ? -1 : lpRecurrence->getWeekNumber(); // hack to handle nth day month type of rec.
  722. icRec.by_month_day[1] = ICAL_RECURRENCE_ARRAY_MAX;
  723. break;
  724. } else if (lpRecurrence->getWeekDays() == 62 || lpRecurrence->getWeekDays() == 65) {
  725. // Recurrence of type '3rd weekday'/'last weekend'
  726. // MO,TU,WE,TH,FR -> 62 and SU,SA -> 65
  727. icRec.by_set_pos[0] = lpRecurrence->getWeekNumber();
  728. icRec.by_set_pos[1] = ICAL_RECURRENCE_ARRAY_MAX;
  729. WeekDaysToICalArray(lpRecurrence->getWeekDays(), &icRec);
  730. } else if (lpRecurrence->getWeekNumber() == 5) {
  731. icRec.by_day[0] = (round(log((double)lpRecurrence->getWeekDays()) / log(2.0)) + 8 + 1) * -1; // corrected last weekday
  732. icRec.by_day[1] = ICAL_RECURRENCE_ARRAY_MAX;
  733. } else {
  734. icRec.by_day[0] = round(log((double)lpRecurrence->getWeekDays()) / log(2.0)) + (8 * lpRecurrence->getWeekNumber()) + 1; // +1 because outlook starts on sunday
  735. icRec.by_day[1] = ICAL_RECURRENCE_ARRAY_MAX;
  736. }
  737. break;
  738. case recurrence::YEARLY:
  739. icRec.freq = ICAL_YEARLY_RECURRENCE;
  740. if (lpRecurrence->getWeekNumber() == 0) {
  741. // mapi patterntype == 2
  742. icRec.by_month_day[0] = lpRecurrence->getDayOfMonth();
  743. icRec.by_month_day[1] = ICAL_RECURRENCE_ARRAY_MAX;
  744. icRec.by_month[0] = lpRecurrence->getMonth();
  745. icRec.by_month[1] = ICAL_RECURRENCE_ARRAY_MAX;
  746. break;
  747. }
  748. // mapi patterntype == 3
  749. // only 1 day should be set!
  750. if (lpRecurrence->getWeekNumber() == 5)
  751. icRec.by_day[0] = ((log((double)lpRecurrence->getWeekDays())/log(2.0)) + 8 + 1 ) * -1;
  752. else
  753. icRec.by_day[0] = (int)(log((double)lpRecurrence->getWeekDays())/log(2.0)) + (8 * lpRecurrence->getWeekNumber() ) +1; // +1 because outlook starts on sunday
  754. icRec.by_day[1] = ICAL_RECURRENCE_ARRAY_MAX;
  755. icRec.by_month[0] = lpRecurrence->getMonth();
  756. icRec.by_month[1] = ICAL_RECURRENCE_ARRAY_MAX;
  757. break;
  758. default:
  759. return MAPI_E_INVALID_PARAMETER;
  760. }
  761. icRec.interval = lpRecurrence->getInterval();
  762. switch (lpRecurrence->getEndType()) {
  763. case recurrence::DATE:
  764. /* From the RFC:
  765. The UNTIL rule part defines a date-time value which bounds the
  766. recurrence rule in an inclusive manner. If the value specified by
  767. UNTIL is synchronized with the specified recurrence, this date or
  768. date-time becomes the last instance of the recurrence. If specified
  769. as a date-time value, then it MUST be specified in an UTC time
  770. format.
  771. */
  772. icRec.count = 0;
  773. // if untiltime is saved as UTC it breaks last occurrence.
  774. icRec.until = icaltime_from_timet_with_zone(lpRecurrence->getEndDate() + lpRecurrence->getStartTimeOffset(), bIsAllday, nullptr);
  775. icRec.until.is_utc = 0;
  776. break;
  777. case recurrence::NUMBER:
  778. icRec.count = lpRecurrence->getCount();
  779. icRec.until = icaltime_null_time();
  780. break;
  781. case recurrence::NEVER:
  782. icRec.count = 0;
  783. icRec.until = icaltime_null_time();
  784. break;
  785. };
  786. *lpicRRule = icRec;
  787. return hrSuccess;
  788. }
  789. /**
  790. * Sets weekdays in ical recurrence from mapi weekdays
  791. *
  792. * e.g input: 0x3E == 011 1110, output: MO,TU,WE,TH,FR
  793. * @param[in] ulWeekDays mapi weekdays
  794. * @param[in,out] lpRec ical recurrence in which weekday are set
  795. * @return Always returns hrSuccess
  796. */
  797. HRESULT ICalRecurrence::WeekDaysToICalArray(ULONG ulWeekDays, struct icalrecurrencetype *lpRec)
  798. {
  799. int i = 0, j = 0;
  800. for (i = 0; i < 7; ++i)
  801. if ((ulWeekDays >> i) & 1)
  802. lpRec->by_day[j++] = i+1;
  803. lpRec->by_day[j] = ICAL_RECURRENCE_ARRAY_MAX;
  804. return hrSuccess;
  805. }
  806. /**
  807. * Clones the ical component and removes the properties that differ in exception
  808. *
  809. * @param[in] lpicEvent The ical component
  810. * @param[out] lppicException The cloned component with some properties removed
  811. * @return Always returns hrSuccess
  812. */
  813. HRESULT ICalRecurrence::HrMakeICalException(icalcomponent *lpicEvent, icalcomponent **lppicException)
  814. {
  815. HRESULT hr = hrSuccess;
  816. icalcomponent *lpicException = NULL;
  817. icalproperty *lpicProp = NULL;
  818. lpicException = icalcomponent_new_clone(lpicEvent);
  819. // these are always different in an exception
  820. lpicProp = icalcomponent_get_first_property(lpicException, ICAL_DTSTART_PROPERTY);
  821. if (lpicProp) {
  822. icalcomponent_remove_property(lpicException, lpicProp);
  823. icalproperty_free(lpicProp);
  824. }
  825. lpicProp = icalcomponent_get_first_property(lpicException, ICAL_DTEND_PROPERTY);
  826. if (lpicProp) {
  827. icalcomponent_remove_property(lpicException, lpicProp);
  828. icalproperty_free(lpicProp);
  829. }
  830. // exceptions don't have the rrule again
  831. lpicProp = icalcomponent_get_first_property(lpicException, ICAL_RRULE_PROPERTY);
  832. if (lpicProp) {
  833. icalcomponent_remove_property(lpicException, lpicProp);
  834. icalproperty_free(lpicProp);
  835. }
  836. // exceptions don't have the deleted exceptions anymore
  837. while ((lpicProp = icalcomponent_get_first_property(lpicException, ICAL_EXDATE_PROPERTY)) != NULL) {
  838. icalcomponent_remove_property(lpicException, lpicProp);
  839. icalproperty_free(lpicProp);
  840. }
  841. // exceptions can't be different in privacy
  842. lpicProp = icalcomponent_get_first_property(lpicException, ICAL_CLASS_PROPERTY);
  843. if (lpicProp) {
  844. icalcomponent_remove_property(lpicException, lpicProp);
  845. icalproperty_free(lpicProp);
  846. }
  847. *lppicException = lpicException;
  848. return hr;
  849. }
  850. } /* namespace */