recurrence.cpp 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879
  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 "recurrence.h"
  20. #include <cmath>
  21. #include <kopano/ECLogger.h>
  22. #include <mapicode.h>
  23. #include <kopano/stringutil.h>
  24. #include <kopano/ECIConv.h>
  25. #include <ctime>
  26. #include <kopano/CommonUtil.h>
  27. #include <mapiutil.h>
  28. #include <kopano/mapiguidext.h>
  29. #include <kopano/namedprops.h>
  30. #include <iostream>
  31. #include <algorithm>
  32. using namespace std;
  33. namespace KC {
  34. recurrence::recurrence() {
  35. m_ulMonth = 0x0;
  36. }
  37. /**
  38. * Load recurrence from blob property data
  39. *
  40. * @param[in] lpData Data from named property RecurrenceState
  41. * @param[in] ulLen Length of lpData
  42. * @param[in] ulFlags RECURRENCE_STATE_TASKS if the recurrence is from a task
  43. */
  44. HRESULT recurrence::HrLoadRecurrenceState(const char *lpData,
  45. unsigned int ulLen, ULONG ulFlags)
  46. {
  47. return m_sRecState.ParseBlob(lpData, ulLen, ulFlags);
  48. }
  49. /**
  50. * Write the new recurrence blob.
  51. *
  52. * @param[out] lppData The blob will be returned in this pointer.
  53. * @param[out] lpulLen Length of returned data in lppData.
  54. * @param[in] base base pointer to MAPIAllocateMore() for lppData allocation. NULL to use MAPIAllocateBuffer().
  55. */
  56. HRESULT recurrence::HrGetRecurrenceState(char **lppData, unsigned int *lpulLen, void *base)
  57. {
  58. time_t tStart;
  59. LONG rStart;
  60. struct tm tm;
  61. // VALIDATION ONLY, not auto-correcting .. you should enter data correctly!
  62. if (m_sRecState.ulRecurFrequency != RF_DAILY &&
  63. m_sRecState.ulRecurFrequency != RF_WEEKLY &&
  64. m_sRecState.ulRecurFrequency != RF_MONTHLY &&
  65. m_sRecState.ulRecurFrequency != RF_YEARLY)
  66. return MAPI_E_CORRUPT_DATA;
  67. if (m_sRecState.ulPatternType != PT_DAY &&
  68. m_sRecState.ulPatternType != PT_WEEK &&
  69. m_sRecState.ulPatternType != PT_MONTH &&
  70. m_sRecState.ulPatternType != PT_MONTH_NTH &&
  71. m_sRecState.ulPatternType != PT_MONTH_END &&
  72. m_sRecState.ulPatternType != PT_HJ_MONTH &&
  73. m_sRecState.ulPatternType != PT_HJ_MONTH_NTH &&
  74. m_sRecState.ulPatternType != PT_HJ_MONTH_END)
  75. return MAPI_E_CORRUPT_DATA;
  76. if (m_sRecState.ulEndType != ET_DATE &&
  77. m_sRecState.ulEndType != ET_NUMBER &&
  78. m_sRecState.ulEndType != ET_NEVER)
  79. return MAPI_E_CORRUPT_DATA;
  80. // calculate ulFirstDateTime
  81. switch (m_sRecState.ulRecurFrequency) {
  82. case RF_DAILY:
  83. if (m_sRecState.ulPatternType == PT_WEEK)
  84. m_sRecState.ulFirstDateTime = 6 * 24 * 60;
  85. else
  86. m_sRecState.ulFirstDateTime = m_sRecState.ulStartDate % m_sRecState.ulPeriod;
  87. break;
  88. case RF_WEEKLY:
  89. int daycount, dayskip;
  90. int weekskip;
  91. tStart = getStartDate();
  92. gmtime_safe(&tStart, &tm);
  93. daycount = 0;
  94. dayskip = -1;
  95. for (int j = 0; j < 7; ++j) {
  96. if (m_sRecState.ulWeekDays & (1<<((tm.tm_wday + j)%7))) {
  97. if (dayskip == -1)
  98. dayskip = j;
  99. ++daycount;
  100. }
  101. }
  102. // dayskip is the number of days to skip from the startdate until the first occurrence
  103. // daycount is the number of days per week that an occurrence occurs
  104. weekskip = 0;
  105. if ((tm.tm_wday < (int)m_sRecState.ulFirstDOW && dayskip > 0) || (tm.tm_wday+dayskip) > 6)
  106. weekskip = 1;
  107. // weekskip is the amount of weeks to skip from the startdate before the first occurence
  108. // The real start is start + dayskip + weekskip-1 (since dayskip will already bring us into the next week)
  109. tStart = tStart + (dayskip * 24*60*60) + (weekskip * (m_sRecState.ulPeriod-1) * 7 * 24*60*60);
  110. gmtime_safe(&tStart, &tm);
  111. UnixTimeToRTime(tStart, &rStart);
  112. m_sRecState.ulFirstDateTime = rStart % (m_sRecState.ulPeriod*7*24*60);
  113. m_sRecState.ulFirstDateTime -= ((tm.tm_wday-1) * 24 * 60); // php says -1, but it's already 0..6 ... err?
  114. break;
  115. case RF_MONTHLY:
  116. case RF_YEARLY:
  117. tStart = getStartDate();
  118. gmtime_safe(&tStart, &tm);
  119. rStart = ((((12%m_sRecState.ulPeriod) * (( ((tm.tm_year + 1900)) - 1601)%m_sRecState.ulPeriod)) % m_sRecState.ulPeriod) + ( ((tm.tm_mon)) ))%m_sRecState.ulPeriod;
  120. m_sRecState.ulFirstDateTime = 0;
  121. for (int i = 0; i < rStart; ++i)
  122. m_sRecState.ulFirstDateTime += MonthInSeconds(1601 + (i/12), (i%12)+1) / 60;
  123. break;
  124. }
  125. // it's not really possible to set this from a program, so we fix this here
  126. if (m_sRecState.ulEndType == ET_NEVER)
  127. m_sRecState.ulEndDate = 0x5AE980DF;
  128. // lstModified must be sorted and unique
  129. sort(m_sRecState.lstModifiedInstanceDates.begin(), m_sRecState.lstModifiedInstanceDates.end());
  130. unique(m_sRecState.lstModifiedInstanceDates.begin(), m_sRecState.lstModifiedInstanceDates.end());
  131. m_sRecState.ulModifiedInstanceCount = m_sRecState.lstModifiedInstanceDates.size();
  132. // lstDeleted must be sorted and unique
  133. sort(m_sRecState.lstDeletedInstanceDates.begin(), m_sRecState.lstDeletedInstanceDates.end());
  134. unique(m_sRecState.lstDeletedInstanceDates.begin(), m_sRecState.lstDeletedInstanceDates.end());
  135. m_sRecState.ulDeletedInstanceCount = m_sRecState.lstDeletedInstanceDates.size();
  136. // exception info count is same as number of modified items
  137. m_sRecState.ulExceptionCount = m_sRecState.lstModifiedInstanceDates.size();
  138. return m_sRecState.GetBlob(lppData, lpulLen, base);
  139. }
  140. HRESULT recurrence::HrGetHumanReadableString(std::string *lpstrHRS)
  141. {
  142. HRESULT hr = S_OK;
  143. std::string strHRS;
  144. strHRS = "This item is recurring";
  145. // @todo: make strings like outlook does, and probably make it std::wstring
  146. *lpstrHRS = std::move(strHRS);
  147. return hr;
  148. }
  149. recurrence::freq_type recurrence::getFrequency()
  150. {
  151. switch (m_sRecState.ulRecurFrequency) {
  152. case RF_DAILY:
  153. return DAILY;
  154. case RF_WEEKLY:
  155. return WEEKLY;
  156. case RF_MONTHLY:
  157. return MONTHLY;
  158. case RF_YEARLY:
  159. return YEARLY;
  160. }
  161. return DAILY;
  162. }
  163. HRESULT recurrence::setFrequency(freq_type ft)
  164. {
  165. HRESULT hr = S_OK;
  166. switch (ft) {
  167. case DAILY:
  168. m_sRecState.ulRecurFrequency = RF_DAILY;
  169. m_sRecState.ulPatternType = PT_DAY;
  170. m_sRecState.ulPeriod = 60*24; // stored in minutes
  171. break;
  172. case WEEKLY:
  173. m_sRecState.ulRecurFrequency = RF_WEEKLY;
  174. m_sRecState.ulPatternType = PT_WEEK;
  175. m_sRecState.ulPeriod = 1;
  176. break;
  177. case MONTHLY:
  178. m_sRecState.ulRecurFrequency = RF_MONTHLY;
  179. m_sRecState.ulPatternType = PT_MONTH;
  180. m_sRecState.ulPeriod = 1;
  181. break;
  182. case YEARLY:
  183. m_sRecState.ulRecurFrequency = RF_YEARLY;
  184. m_sRecState.ulPatternType = PT_MONTH; // every Nth month
  185. m_sRecState.ulPeriod = 12;
  186. break;
  187. default:
  188. hr = E_INVALIDARG;
  189. break;
  190. }
  191. return hr;
  192. }
  193. time_t recurrence::getStartDate()
  194. {
  195. time_t tStart = 0;
  196. RTimeToUnixTime(m_sRecState.ulStartDate, &tStart);
  197. return tStart;
  198. }
  199. HRESULT recurrence::setStartDate(time_t tStart)
  200. {
  201. return UnixTimeToRTime(StartOfDay(tStart),
  202. reinterpret_cast<LONG *>(&m_sRecState.ulStartDate));
  203. }
  204. time_t recurrence::getEndDate()
  205. {
  206. time_t tEnd;
  207. RTimeToUnixTime(m_sRecState.ulEndDate, &tEnd);
  208. return tEnd;
  209. }
  210. HRESULT recurrence::setEndDate(time_t tEnd)
  211. {
  212. return UnixTimeToRTime(StartOfDay(tEnd),
  213. reinterpret_cast<LONG *>(&m_sRecState.ulEndDate));
  214. }
  215. ULONG recurrence::getStartTimeOffset()
  216. {
  217. return m_sRecState.ulStartTimeOffset*60;
  218. }
  219. HRESULT recurrence::setStartTimeOffset(ULONG ulMinutesSinceMidnight)
  220. {
  221. if (ulMinutesSinceMidnight >= 24 * 60)
  222. return E_INVALIDARG;
  223. m_sRecState.ulStartTimeOffset = ulMinutesSinceMidnight;
  224. return S_OK;
  225. }
  226. ULONG recurrence::getEndTimeOffset()
  227. {
  228. return m_sRecState.ulEndTimeOffset*60;
  229. }
  230. HRESULT recurrence::setEndTimeOffset(ULONG ulMinutesSinceMidnight)
  231. {
  232. m_sRecState.ulEndTimeOffset = ulMinutesSinceMidnight;
  233. return S_OK;
  234. }
  235. time_t recurrence::getStartDateTime()
  236. {
  237. time_t tStart;
  238. RTimeToUnixTime(m_sRecState.ulStartDate, &tStart);
  239. return tStart + (m_sRecState.ulStartTimeOffset*60);
  240. }
  241. HRESULT recurrence::setStartDateTime(time_t t)
  242. {
  243. HRESULT hr;
  244. time_t startDate = StartOfDay(t);
  245. hr = UnixTimeToRTime(startDate, (LONG*)&m_sRecState.ulStartDate);
  246. m_sRecState.ulStartTimeOffset = (t - startDate)/60;
  247. return hr;
  248. }
  249. time_t recurrence::getEndDateTime()
  250. {
  251. time_t tStart;
  252. RTimeToUnixTime(m_sRecState.ulEndDate, &tStart);
  253. return tStart + (m_sRecState.ulEndTimeOffset*60);
  254. }
  255. HRESULT recurrence::setEndDateTime(time_t t)
  256. {
  257. HRESULT hr;
  258. hr = UnixTimeToRTime(StartOfDay(t), (LONG*)&m_sRecState.ulEndDate);
  259. // end time in minutes since midnight of start of item
  260. m_sRecState.ulEndTimeOffset = (t - getStartDate())/60;
  261. return hr;
  262. }
  263. ULONG recurrence::getCount()
  264. {
  265. return m_sRecState.ulOccurrenceCount;
  266. }
  267. HRESULT recurrence::setCount(ULONG ulCount)
  268. {
  269. m_sRecState.ulOccurrenceCount = ulCount;
  270. return S_OK;
  271. }
  272. recurrence::term_type recurrence::getEndType()
  273. {
  274. if (m_sRecState.ulEndType == ET_DATE)
  275. return DATE;
  276. else if (m_sRecState.ulEndType == ET_NUMBER)
  277. return NUMBER;
  278. return NEVER;
  279. }
  280. HRESULT recurrence::setEndType(term_type t)
  281. {
  282. switch (t) {
  283. case DATE:
  284. m_sRecState.ulEndType = ET_DATE;
  285. break;
  286. case NUMBER:
  287. m_sRecState.ulEndType = ET_NUMBER;
  288. break;
  289. case NEVER:
  290. default:
  291. m_sRecState.ulEndType = ET_NEVER;
  292. break;
  293. };
  294. return S_OK;
  295. }
  296. ULONG recurrence::getInterval()
  297. {
  298. ULONG rv;
  299. if (m_sRecState.ulPatternType == PT_DAY)
  300. // day pattern type, period stored in minutes per day
  301. rv = m_sRecState.ulPeriod / (60*24);
  302. else if (getFrequency() == recurrence::YEARLY)
  303. // yearly stored in months
  304. rv = m_sRecState.ulPeriod / 12;
  305. else
  306. // either weeks or months, no conversion required
  307. rv = m_sRecState.ulPeriod;
  308. return rv;
  309. }
  310. // Note: Frequency must be set before the interval!
  311. HRESULT recurrence::setInterval(ULONG i)
  312. {
  313. if (i == 0)
  314. return E_INVALIDARG;
  315. m_sRecState.ulPeriod = m_sRecState.ulPeriod * i; // in setFrequency(), ulPeriod is set to "1" for each type
  316. return S_OK;
  317. }
  318. HRESULT recurrence::setSlidingFlag(ULONG s)
  319. {
  320. m_sRecState.ulSlidingFlag = s;
  321. return S_OK;
  322. }
  323. ULONG recurrence::getFirstDOW()
  324. {
  325. return m_sRecState.ulFirstDOW;
  326. }
  327. HRESULT recurrence::setFirstDOW(ULONG ulFirstDOW)
  328. {
  329. m_sRecState.ulFirstDOW = ulFirstDOW;
  330. return S_OK;
  331. }
  332. UCHAR recurrence::getWeekDays()
  333. {
  334. // valid ulPatternTypes: 1 2 4 a c
  335. if (m_sRecState.ulPatternType == PT_DAY)
  336. return 0;
  337. return m_sRecState.ulWeekDays;
  338. }
  339. HRESULT recurrence::setWeekDays(UCHAR d)
  340. {
  341. // if setWeekDays is called on a daily event, update the pattern type
  342. if (m_sRecState.ulPatternType == PT_DAY) {
  343. m_sRecState.ulPatternType = PT_WEEK;
  344. m_sRecState.ulPeriod = m_sRecState.ulPeriod / (24*60); // convert period from daily to "weekly"
  345. }
  346. m_sRecState.ulWeekDays = d & WD_MASK;
  347. return S_OK;
  348. }
  349. UCHAR recurrence::getDayOfMonth()
  350. {
  351. if (m_sRecState.ulRecurFrequency != RF_YEARLY &&
  352. m_sRecState.ulRecurFrequency != RF_MONTHLY &&
  353. (m_sRecState.ulPatternType != PT_MONTH ||
  354. m_sRecState.ulPatternType != PT_MONTH_END))
  355. return 0;
  356. return m_sRecState.ulDayOfMonth;
  357. }
  358. HRESULT recurrence::setDayOfMonth(UCHAR d)
  359. {
  360. m_sRecState.ulDayOfMonth = d;
  361. return S_OK;
  362. }
  363. /**
  364. * Get the month between 1...12
  365. * 1 = jan
  366. */
  367. UCHAR recurrence::getMonth()
  368. {
  369. if (m_ulMonth > 0 && m_ulMonth < 13)
  370. return m_ulMonth;
  371. struct tm tmMonth;
  372. time_t tStart = getStartDate();
  373. gmtime_safe(&tStart, &tmMonth);
  374. return tmMonth.tm_mon+1;
  375. }
  376. HRESULT recurrence::setMonth(UCHAR m)
  377. {
  378. if(m < 1 || m > 12)
  379. return MAPI_E_CALL_FAILED;
  380. m_ulMonth = m;
  381. return hrSuccess;
  382. }
  383. // only valid in monthly type 0x3 and 0xb
  384. UCHAR recurrence::getWeekNumber()
  385. {
  386. if (m_sRecState.ulPatternType != PT_MONTH_NTH &&
  387. m_sRecState.ulPatternType != PT_HJ_MONTH_NTH)
  388. return 0;
  389. return m_sRecState.ulWeekNumber;
  390. }
  391. HRESULT recurrence::setWeekNumber(UCHAR s)
  392. {
  393. // we should be handling monthly recurrence items here, calendar type 0xB (hijri) is not supported
  394. m_sRecState.ulPatternType = PT_MONTH_NTH;
  395. m_sRecState.ulWeekNumber = s;
  396. return S_OK;
  397. }
  398. // ------------
  399. // handle exceptions
  400. // ------------
  401. HRESULT recurrence::addDeletedException(time_t tDelete)
  402. {
  403. HRESULT hr = S_OK;
  404. time_t tDayDelete = StartOfDay(tDelete);
  405. ULONG rtime;
  406. hr = UnixTimeToRTime(tDayDelete, (LONG*)&rtime);
  407. m_sRecState.lstDeletedInstanceDates.push_back(rtime);
  408. return hr;
  409. }
  410. std::list<time_t> recurrence::getDeletedExceptions() {
  411. time_t tDayDelete;
  412. time_t offset = getStartTimeOffset();
  413. std::list<time_t> lstDeletes;
  414. std::vector<unsigned int> lstDeletedInstanceDates;
  415. // make copy of struct info
  416. lstDeletedInstanceDates = m_sRecState.lstDeletedInstanceDates;
  417. for (const auto &exc : m_sRecState.lstExceptions) {
  418. // if startofday(exception.basedata) == present in lstDeletes, that's a move, so remove from deletes list
  419. auto d = find(lstDeletedInstanceDates.begin(),
  420. lstDeletedInstanceDates.end(),
  421. exc.ulOriginalStartDate - (exc.ulOriginalStartDate % 1440));
  422. if (d != lstDeletedInstanceDates.end())
  423. lstDeletedInstanceDates.erase(d);
  424. }
  425. for (const auto &d : lstDeletedInstanceDates) {
  426. RTimeToUnixTime(d, &tDayDelete);
  427. lstDeletes.push_back(tDayDelete + offset);
  428. }
  429. return lstDeletes;
  430. }
  431. std::list<time_t> recurrence::getModifiedOccurrences() {
  432. time_t tDayModified;
  433. std::list<time_t> lstModified;
  434. for (const auto &exc : m_sRecState.lstExceptions) {
  435. RTimeToUnixTime(exc.ulOriginalStartDate, &tDayModified);
  436. lstModified.push_back(tDayModified);
  437. }
  438. return lstModified;
  439. }
  440. ULONG recurrence::getModifiedCount()
  441. {
  442. return m_sRecState.ulModifiedInstanceCount;
  443. }
  444. ULONG recurrence::getModifiedFlags(ULONG id)
  445. {
  446. if (id >= m_sRecState.ulModifiedInstanceCount)
  447. return 0;
  448. return m_sRecState.lstExceptions[id].ulOverrideFlags;
  449. }
  450. time_t recurrence::getModifiedStartDateTime(ULONG id)
  451. {
  452. time_t tDayModified = 0;
  453. if (id >= m_sRecState.ulModifiedInstanceCount)
  454. return 0;
  455. RTimeToUnixTime(m_sRecState.lstExceptions[id].ulStartDateTime, &tDayModified);
  456. return tDayModified;
  457. }
  458. time_t recurrence::getModifiedEndDateTime(ULONG id)
  459. {
  460. time_t tDayModified = 0;
  461. if (id >= m_sRecState.ulModifiedInstanceCount)
  462. return 0;
  463. RTimeToUnixTime(m_sRecState.lstExceptions[id].ulEndDateTime, &tDayModified);
  464. return tDayModified;
  465. }
  466. time_t recurrence::getModifiedOriginalDateTime(ULONG id)
  467. {
  468. time_t tDayModified = 0;
  469. if (id >= m_sRecState.ulModifiedInstanceCount)
  470. return 0;
  471. RTimeToUnixTime(m_sRecState.lstExceptions[id].ulOriginalStartDate, &tDayModified);
  472. return tDayModified;
  473. }
  474. std::wstring recurrence::getModifiedSubject(ULONG id)
  475. {
  476. if (id >= m_sRecState.ulModifiedInstanceCount)
  477. return wstring();
  478. return m_sRecState.lstExtendedExceptions[id].strWideCharSubject;
  479. }
  480. ULONG recurrence::getModifiedMeetingType(ULONG id)
  481. {
  482. if (id >= m_sRecState.ulModifiedInstanceCount)
  483. return 0;
  484. return m_sRecState.lstExceptions[id].ulApptStateFlags;
  485. }
  486. LONG recurrence::getModifiedReminderDelta(ULONG id)
  487. {
  488. if (id >= m_sRecState.ulModifiedInstanceCount)
  489. return 0;
  490. return m_sRecState.lstExceptions[id].ulReminderDelta;
  491. }
  492. ULONG recurrence::getModifiedReminder(ULONG id)
  493. {
  494. if (id >= m_sRecState.ulModifiedInstanceCount)
  495. return 0;
  496. return m_sRecState.lstExceptions[id].ulReminderSet;
  497. }
  498. std::wstring recurrence::getModifiedLocation(ULONG id)
  499. {
  500. if (id >= m_sRecState.ulModifiedInstanceCount)
  501. return wstring();
  502. return m_sRecState.lstExtendedExceptions[id].strWideCharLocation;
  503. }
  504. ULONG recurrence::getModifiedBusyStatus(ULONG id)
  505. {
  506. if (id >= m_sRecState.ulModifiedInstanceCount)
  507. return 0;
  508. return m_sRecState.lstExceptions[id].ulBusyStatus;
  509. }
  510. ULONG recurrence::getModifiedAttachment(ULONG id)
  511. {
  512. if (id >= m_sRecState.ulModifiedInstanceCount)
  513. return 0;
  514. return m_sRecState.lstExceptions[id].ulAttachment;
  515. }
  516. ULONG recurrence::getModifiedSubType(ULONG id)
  517. {
  518. if (id >= m_sRecState.ulModifiedInstanceCount)
  519. return 0;
  520. return m_sRecState.lstExceptions[id].ulSubType;
  521. }
  522. HRESULT recurrence::addModifiedException(time_t tStart, time_t tEnd, time_t tOriginalStart, ULONG *lpid)
  523. {
  524. HRESULT hr = S_OK;
  525. LONG rStart, rEnd, rOrig, rDayStart;
  526. ULONG id = 0;
  527. RecurrenceState::Exception sException = {0};
  528. RecurrenceState::ExtendedException sExtException = {0};
  529. UnixTimeToRTime(tStart, &rStart);
  530. UnixTimeToRTime(tEnd, &rEnd);
  531. UnixTimeToRTime(tOriginalStart, &rOrig);
  532. // this is not thread safe, but since this code is not (yet)
  533. // called in a thread unsafe manner I could not care less at
  534. // the moment
  535. id = m_sRecState.lstModifiedInstanceDates.size();
  536. // move is the exception day start
  537. UnixTimeToRTime(StartOfDay(tStart), &rDayStart);
  538. m_sRecState.lstModifiedInstanceDates.push_back(rDayStart);
  539. // every modify is also a delete in the blob
  540. // delete is the original start
  541. UnixTimeToRTime(StartOfDay(tOriginalStart), &rDayStart);
  542. m_sRecState.lstDeletedInstanceDates.push_back(rDayStart);
  543. sExtException.ulStartDateTime = sException.ulStartDateTime = rStart;
  544. sExtException.ulEndDateTime = sException.ulEndDateTime = rEnd;
  545. sExtException.ulOriginalStartDate = sException.ulOriginalStartDate = rOrig;
  546. sExtException.ulChangeHighlightValue = 0;
  547. m_sRecState.lstExceptions.push_back(std::move(sException));
  548. m_sRecState.lstExtendedExceptions.push_back(std::move(sExtException));
  549. *lpid = id;
  550. return hr;
  551. }
  552. HRESULT recurrence::setModifiedSubject(ULONG id, const std::wstring &strSubject)
  553. {
  554. if (id >= m_sRecState.lstExceptions.size())
  555. return S_FALSE;
  556. m_sRecState.lstExceptions[id].ulOverrideFlags |= ARO_SUBJECT;
  557. m_sRecState.lstExceptions[id].strSubject = convert_to<string>(strSubject);
  558. m_sRecState.lstExtendedExceptions[id].strWideCharSubject = strSubject;
  559. return S_OK;
  560. }
  561. HRESULT recurrence::setModifiedMeetingType(ULONG id, ULONG type)
  562. {
  563. if (id >= m_sRecState.lstExceptions.size())
  564. return S_FALSE;
  565. m_sRecState.lstExceptions[id].ulOverrideFlags |= ARO_MEETINGTYPE;
  566. m_sRecState.lstExceptions[id].ulApptStateFlags = type;
  567. return S_OK;
  568. }
  569. HRESULT recurrence::setModifiedReminderDelta(ULONG id, LONG delta)
  570. {
  571. if (id >= m_sRecState.lstExceptions.size())
  572. return S_FALSE;
  573. m_sRecState.lstExceptions[id].ulOverrideFlags |= ARO_REMINDERDELTA;
  574. m_sRecState.lstExceptions[id].ulReminderDelta = delta;
  575. return S_OK;
  576. }
  577. HRESULT recurrence::setModifiedReminder(ULONG id, ULONG set)
  578. {
  579. if (id >= m_sRecState.lstExceptions.size())
  580. return S_FALSE;
  581. m_sRecState.lstExceptions[id].ulOverrideFlags |= ARO_REMINDERSET;
  582. m_sRecState.lstExceptions[id].ulReminderSet = set;
  583. return S_OK;
  584. }
  585. HRESULT recurrence::setModifiedLocation(ULONG id,
  586. const std::wstring &strLocation)
  587. {
  588. if (id >= m_sRecState.lstExceptions.size())
  589. return S_FALSE;
  590. m_sRecState.lstExceptions[id].ulOverrideFlags |= ARO_LOCATION;
  591. m_sRecState.lstExceptions[id].strLocation = convert_to<string>(strLocation);
  592. m_sRecState.lstExtendedExceptions[id].strWideCharLocation = strLocation;
  593. return S_OK;
  594. }
  595. HRESULT recurrence::setModifiedBusyStatus(ULONG id, ULONG status)
  596. {
  597. if (id >= m_sRecState.lstExceptions.size())
  598. return S_FALSE;
  599. m_sRecState.lstExceptions[id].ulOverrideFlags |= ARO_BUSYSTATUS;
  600. m_sRecState.lstExceptions[id].ulBusyStatus = status;
  601. return S_OK;
  602. }
  603. HRESULT recurrence::setModifiedAttachment(ULONG id)
  604. {
  605. if (id >= m_sRecState.lstExceptions.size())
  606. return S_FALSE;
  607. m_sRecState.lstExceptions[id].ulOverrideFlags |= ARO_ATTACHMENT;
  608. return S_OK;
  609. }
  610. HRESULT recurrence::setModifiedSubType(ULONG id, ULONG subtype)
  611. {
  612. if (id >= m_sRecState.lstExceptions.size())
  613. return S_FALSE;
  614. m_sRecState.lstExceptions[id].ulOverrideFlags |= ARO_SUBTYPE;
  615. m_sRecState.lstExceptions[id].ulSubType = subtype;
  616. return S_OK;
  617. }
  618. HRESULT recurrence::setModifiedApptColor(ULONG id, ULONG color)
  619. {
  620. if (id >= m_sRecState.lstExceptions.size())
  621. return S_FALSE;
  622. m_sRecState.lstExceptions[id].ulOverrideFlags |= ARO_APPTCOLOR;
  623. m_sRecState.lstExceptions[id].ulAppointmentColor = color;
  624. return S_OK;
  625. }
  626. HRESULT recurrence::setModifiedBody(ULONG id)
  627. {
  628. if (id >= m_sRecState.lstExceptions.size())
  629. return S_FALSE;
  630. m_sRecState.lstExceptions[id].ulOverrideFlags |= ARO_EXCEPTIONAL_BODY;
  631. return S_OK;
  632. }
  633. /*
  634. HRESULT recurrence::setDeletedOccurrence(time_t t)
  635. {
  636. if (isException(t) || !isOccurrence(t))
  637. return S_FALSE;
  638. // FIXME: should only do deleted_list handling .. only the blob is weird with del/mod
  639. exceptions.push_back(DayStartOf(t));
  640. deleted_occurrences.push_back(DayStartOf(t));
  641. return S_OK;
  642. }
  643. HRESULT recurrence::removeDeletedOccurrence(time_t t)
  644. {
  645. if (!isDeletedOccurrence(t))
  646. return S_FALSE;
  647. // FIXME: should only do deleted_list handling .. only the blob is weird with del/mod
  648. exceptions.remove(DayStartOf(t));
  649. deleted_occurrences.remove(DayStartOf(t));
  650. return S_OK;
  651. }
  652. std::list<time_t> recurrence::getDeletedOccurrences()
  653. {
  654. return deleted_occurrences;
  655. }
  656. // ?
  657. HRESULT recurrence::getChangedOccurrence(time_t t, changed_occurrence_type *c)
  658. {
  659. for (const auto i : changed_occurrences)
  660. if (DayStartOf(i.basedate) == DayStartOf(t)) {
  661. *c = *i;
  662. return S_OK;
  663. }
  664. return S_FALSE;
  665. }
  666. HRESULT recurrence::setChangedOccurrence(changed_occurrence_type c){
  667. if(isException(c.basedate) || !isOccurrence(c.basedate))
  668. return MAPI_E_CALL_FAILED;
  669. exceptions.push_back(DayStartOf(c.basedate));
  670. changed_occurrences.push_back(c);
  671. return S_OK;
  672. }
  673. HRESULT recurrence::removeChangedOccurrence(time_t t){
  674. if(!isChangedOccurrence(t))
  675. return MAPI_E_CALL_FAILED;
  676. for (auto i = changed_occurrences.begin();
  677. i != changed_occurrences.end(); ++i)
  678. if(DayStartOf(i->basedate) == DayStartOf(t)){
  679. changed_occurrences.erase(i);
  680. return S_OK;
  681. }
  682. return MAPI_E_NOT_FOUND;
  683. }
  684. list<recurrence::changed_occurrence_type> recurrence::getChangedOccurrences(){
  685. return changed_occurrences;
  686. }
  687. bool recurrence::isException(time_t t){
  688. for (const auto i : exceptions)
  689. if (DayStartOf(i) == DayStartOf(t))
  690. return true;
  691. return false;
  692. }
  693. list<time_t> recurrence::getExceptions(){
  694. return exceptions;
  695. }
  696. bool recurrence::isOccurrence(time_t time){
  697. calcEndDate();
  698. if(this->term != NEVER && time > (this->enddate + this->starttime*60))
  699. return false;
  700. if(isDeletedOccurrence(time))
  701. return false;
  702. //loop through changed occurrences
  703. for (const auto change : changed_occurrences)
  704. if (change.startdate == time)
  705. return true;
  706. return isRuleOccurrence(time);
  707. }
  708. bool recurrence::isRuleOccurrence(time_t time){
  709. time_t ulStart;
  710. ULONGLONG diff; //number of days between start and occurrence
  711. ULONG ulUlPeriod;
  712. ULONG ulMonth;
  713. if(time < this->startdate)
  714. return false;
  715. calcEndDate();
  716. if(this->term != NEVER && time > (this->enddate + this->starttime*60))
  717. return false;
  718. if(this->freq == DAILY){
  719. diff = (time - this->startdate)/(24*60*60);
  720. return diff % m_sRecState.ulPeriod == 0;
  721. }else if(this->freq == WEEKLY){
  722. int a = WeekDayFromTime(time);
  723. if(!(((1 << WeekDayFromTime(time)) & this->weekdays)))
  724. return false;
  725. ulStart = WeekStartOf(this->startdate);
  726. diff = (time - this->startdate)/(24*60*60);
  727. return (((ULONGLONG)(diff / 7)) % m_sRecState.ulPeriod == 0);
  728. }else{ //monthly or yearly
  729. ulUlPeriod = m_sRecState.ulPeriod;
  730. if(this->freq == YEARLY)
  731. ulUlPeriod *= 12;
  732. if(this->nday>0){
  733. if(!(((1 << WeekDayFromTime(time)) & this->weekdays)))
  734. return false;
  735. diff = AllMonthsFromTime(time) - AllMonthsFromTime(this->startdate);
  736. if(diff % ulUlPeriod != 0)
  737. return false;
  738. ulMonth = MonthFromTime(time);
  739. if(this->nday == 5 && ulMonth != MonthFromTime(time + 7*24*60*60))
  740. return true;
  741. if(ulMonth != MonthFromTime(time - 7*24*60*60))
  742. return this->nday == 1;
  743. if(ulMonth != MonthFromTime(time - 14*24*60*60))
  744. return this->nday == 2;
  745. if(ulMonth != MonthFromTime(time - 21*24*60*60))
  746. return this->nday == 3;
  747. if(ulMonth != MonthFromTime(time - 28*24*60*60))
  748. return this->nday == 4;
  749. return false;
  750. }else{
  751. ULONG ulDays = DaysInMonth(MonthFromTime(time));
  752. if(MonthDayFromTime(time) != this->monthday && !(this->monthday > ulDays && MonthDayFromTime(time) == ulDays))
  753. return false; //the day of the month is wrong
  754. diff = AllMonthsFromTime(time) - AllMonthsFromTime(this->startdate);
  755. return diff % ulUlPeriod == 0;
  756. }
  757. }
  758. return false;
  759. }
  760. bool recurrence::isDeletedOccurrence(time_t t){
  761. for (const auto i : deleted_occurrences)
  762. if (DayStartOf(i) == DayStartOf(t))
  763. return true;
  764. return false;
  765. }
  766. bool recurrence::isChangedOccurrence(time_t t){
  767. for (const auto i : changed_occurrences)
  768. if (DayStartOf(i.basedate) == DayStartOf(t))
  769. return true;
  770. return false;
  771. }
  772. bool recurrence::isAfter(time_t tStamp)
  773. {
  774. struct tm tmStamp;
  775. gmtime_safe(&tStamp, &tmStamp);
  776. switch (getFrequency()) {
  777. case DAILY:
  778. break;
  779. case WEEKLY:
  780. for (char i = 0; i < 7; ++i) {
  781. if (m_sRecState.ulWeekDays & (1<<i))
  782. {
  783. if (tmStamp.tm_wday < i)
  784. return true;
  785. if (tmStamp.tm_wday > i)
  786. return false;
  787. break; // break? what about other days? think!
  788. }
  789. }
  790. break;
  791. case YEARLY:
  792. if (tmStamp.tm_mon < getMonth())
  793. return true;
  794. if (tmStamp.tm_mon > getMonth())
  795. return false;
  796. break; // added
  797. case MONTHLY:
  798. if (getWeekNumber() == 5)
  799. {
  800. char d = DaysInMonth(tmStamp.tm_mon + 1, tmStamp.tm_year + 1900) - tmStamp.tm_mday;
  801. if (d >= 7)
  802. return true;
  803. char i = tmStamp.tm_wday - (6 - d);
  804. if (i < 0)
  805. i+=7;
  806. while (i != tmStamp.tm_wday) {
  807. if (getWeekDays() & (1<<i))
  808. return false;
  809. i = (i + 1) % 7;
  810. }
  811. if (!(getWeekDays() & (1 << tmStamp.tm_wday)))
  812. return true;
  813. }
  814. else if (getWeekNumber() > 0)
  815. {
  816. if (getWeekNumber() < tmStamp.tm_mday / 7)
  817. return true;
  818. if (getWeekNumber() < tmStamp.tm_mday / 7)
  819. return false;
  820. char i = tmStamp.tm_wday - ((tmStamp.tm_mday - 1) % 7);
  821. if (i < 0)
  822. i += 7;
  823. while (i != tmStamp.tm_wday) {
  824. if (getWeekDays() & (1<<i))
  825. return false;
  826. i = (i + 1) % 7;
  827. }
  828. if (!(getWeekDays() & (1 << tmStamp.tm_wday)))
  829. return true;
  830. }
  831. else
  832. {
  833. if (tmStamp.tm_mday < getDayOfMonth())
  834. return true;
  835. if (tmStamp.tm_mday > getDayOfMonth())
  836. return true;
  837. }
  838. }
  839. if (tmStamp.tm_hour * 60 + tmStamp.tm_min < getStartDateTime())
  840. return true;
  841. return false;
  842. }
  843. list<time_t> recurrence::getOccurrencesBetween(time_t begin, time_t end){
  844. list<time_t> occurrences;
  845. if(begin > end)
  846. return occurrences;
  847. //first add all changed_occurrences with startdate between begin and end
  848. for (const auto changed : changed_occurrences)
  849. if (changed.startdate > begin && changed.startdate < end)
  850. occurrences.push_back(changed.startdate);
  851. //check startdate & enddate
  852. if(end < this->startdate)
  853. return occurrences;
  854. if(this->term != NEVER && this->enddate < begin)
  855. return occurrences;
  856. //TODO: loop through occurrences and add every occurrence that's not an exception
  857. return occurrences;
  858. }
  859. */
  860. time_t recurrence::calcStartDate()
  861. {
  862. time_t tStart = getStartDateTime();
  863. struct tm tm;
  864. switch (m_sRecState.ulRecurFrequency) {
  865. case RF_DAILY:
  866. // Use the default start date.
  867. break;
  868. case RF_WEEKLY:
  869. int daycount, dayskip;
  870. int weekskip;
  871. gmtime_safe(&tStart, &tm);
  872. daycount = 0;
  873. dayskip = -1;
  874. for (int j = 0; j < 7; ++j) {
  875. if (m_sRecState.ulWeekDays & (1<<((tm.tm_wday + j)%7))) {
  876. if (dayskip == -1)
  877. dayskip = j;
  878. ++daycount;
  879. }
  880. }
  881. // dayskip is the number of days to skip from the startdate until the first occurrence
  882. // daycount is the number of days per week that an occurrence occurs
  883. weekskip = 0;
  884. if ((tm.tm_wday < (int)m_sRecState.ulFirstDOW && dayskip > 0) || (tm.tm_wday+dayskip) > 6)
  885. weekskip = 1;
  886. // weekskip is the amount of weeks to skip from the startdate before the first occurence
  887. // The real start is start + dayskip + weekskip-1 (since dayskip will already bring us into the next week)
  888. tStart = tStart + (dayskip * 24*60*60) + (weekskip * (m_sRecState.ulPeriod-1) * 7 * 24*60*60);
  889. gmtime_safe(&tStart, &tm);
  890. break;
  891. case RF_MONTHLY:
  892. case RF_YEARLY:
  893. gmtime_safe(&tStart, &tm);
  894. if (m_sRecState.ulPatternType == PT_MONTH) {
  895. unsigned int count = 0;
  896. // Go the beginning of the month
  897. tStart -= (tm.tm_mday-1) * 24*60*60;
  898. // Go the the correct month day
  899. tStart += (m_sRecState.ulDayOfMonth-1) * 24*60*60;
  900. // If the previous calculation gave us a start date *before* the original start date, then we need to skip to the next occurrence
  901. if (m_sRecState.ulRecurFrequency == RF_MONTHLY &&
  902. static_cast<int>(m_sRecState.ulDayOfMonth) < tm.tm_mday) {
  903. // Monthly, go to next occurrence in 'everyn' months
  904. count = m_sRecState.ulPeriod;
  905. } else if (m_sRecState.ulRecurFrequency == RF_YEARLY) {
  906. if (getMonth() - 1 < tm.tm_mon || (getMonth() - 1 == tm.tm_mon && static_cast<int>(m_sRecState.ulDayOfMonth) < tm.tm_mday))
  907. // Yearly, go to next occurrence in 'everyn' months minus difference in first occurence and original date
  908. count = (m_sRecState.ulPeriod - (tm.tm_mon - (getMonth()-1)));
  909. else if (getMonth()-1 > tm.tm_mon)
  910. count = (getMonth()-1) - tm.tm_mon;
  911. }
  912. int curmonth = tm.tm_mon + 1;
  913. int curyear = tm.tm_year + 1900;
  914. for (unsigned int i = 0; i < count; ++i) {
  915. tStart += MonthInSeconds(curyear, curmonth);
  916. if (curmonth == 12) {
  917. curmonth = 0;
  918. ++curyear;
  919. }
  920. ++curmonth;
  921. }
  922. // "start" is now pointing to the first occurrence, except that it will overshoot if the
  923. // month in which it occurs has less days than specified as the day of the month. So 31st
  924. // of each month will overshoot in february (29 days). We compensate for that by checking
  925. // if the day of the month we got is wrong, and then back up to the last day of the previous
  926. // month.
  927. if ( m_sRecState.ulDayOfMonth >= 28 && m_sRecState.ulDayOfMonth <=31) {
  928. gmtime_safe(&tStart, &tm);
  929. if (tm.tm_mday < static_cast<int>(m_sRecState.ulDayOfMonth))
  930. tStart -= tm.tm_mday * 24 * 60 *60;
  931. }
  932. } else if (m_sRecState.ulPatternType == PT_MONTH_NTH) {
  933. // seek to the begin of the month
  934. tStart -= (tm.tm_mday-1) * 24*60*60;
  935. // See to the end of the month when every last n Day of the month
  936. if (m_sRecState.ulWeekNumber == 5)
  937. tStart += MonthInSeconds(tm.tm_year + 1900, tm.tm_mon+1) - (24*60*60);
  938. // Find the first valid day (from the original start date)
  939. int day = -1;
  940. bool bMoveMonth = false;
  941. for (int i = 0; i < 7; ++i) {
  942. if (m_sRecState.ulWeekNumber == 5 && (1<< (tm.tm_wday - i)%7) & m_sRecState.ulWeekDays) {
  943. day = DaysInMonth(tm.tm_year+1900, tm.tm_mon+1) - i;
  944. if (day < tm.tm_mday)
  945. bMoveMonth = true;
  946. break;
  947. } else if (m_sRecState.ulWeekNumber != 5 && (1<< (tm.tm_wday + i)%7) & m_sRecState.ulWeekDays) {
  948. int maxweekday = m_sRecState.ulWeekNumber * 7;
  949. day = tm.tm_mday+i;
  950. if (day > maxweekday)
  951. bMoveMonth = true;
  952. break;
  953. }
  954. }
  955. // Move to the right month
  956. if (m_sRecState.ulRecurFrequency == RF_YEARLY) {
  957. unsigned int count = 0;
  958. if (getMonth() - 1 < tm.tm_mon || (getMonth() - 1 == tm.tm_mon && bMoveMonth))
  959. count = 12 - tm.tm_mon + (getMonth()-1);
  960. else
  961. count = (getMonth()-1) - tm.tm_mon;
  962. int curmonth = tm.tm_mon + 1;
  963. int curyear = tm.tm_year + 1900;
  964. for (unsigned int i = 0; i < count; ++i) {
  965. tStart += MonthInSeconds(curyear, curmonth);
  966. if (curmonth == 12) {
  967. curmonth = 0;
  968. ++curyear;
  969. }
  970. ++curmonth;
  971. }
  972. } else {
  973. // Check you exist in the right month
  974. if(bMoveMonth) {
  975. int curmonth = tm.tm_mon + 1;
  976. int curyear = tm.tm_year + 1900;
  977. if (m_sRecState.ulWeekNumber == 5) {
  978. if (curmonth == 12) {
  979. curmonth = 0;
  980. ++curyear;
  981. }
  982. ++curmonth;
  983. }
  984. for (unsigned int i = 0; i < m_sRecState.ulPeriod; ++i) {
  985. tStart += MonthInSeconds(curyear, curmonth);
  986. if (curmonth == 12) {
  987. curmonth = 0;
  988. ++curyear;
  989. }
  990. ++curmonth;
  991. }
  992. }
  993. }
  994. // Seek to the right day (tStart should be the first day or the last day of the month.
  995. gmtime_safe(&tStart, &tm);
  996. for (int i = 0; i < 7; ++i) {
  997. if (m_sRecState.ulWeekNumber == 5 && (1<< (tm.tm_wday - i)%7) & m_sRecState.ulWeekDays) {
  998. tStart -= i * 24 * 60 *60;
  999. break;
  1000. } else if (m_sRecState.ulWeekNumber != 5 && (1<< (tm.tm_wday + i)%7) & m_sRecState.ulWeekDays) {
  1001. tStart += (((m_sRecState.ulWeekNumber-1) * 7 + (i+1))- 1) * 24 * 60 *60;
  1002. break;
  1003. }
  1004. }
  1005. }
  1006. break;
  1007. }
  1008. return tStart;
  1009. }
  1010. time_t recurrence::calcEndDate()
  1011. {
  1012. time_t tStart, tEnd;
  1013. struct tm tm;
  1014. if (m_sRecState.ulEndType != ET_NUMBER)
  1015. return getEndDateTime();
  1016. tStart = getStartDateTime();
  1017. tEnd = tStart;
  1018. int fwd;
  1019. int rest;
  1020. int daycount;
  1021. switch (m_sRecState.ulRecurFrequency) {
  1022. case RF_DAILY:
  1023. if (m_sRecState.ulPatternType == PT_DAY) {
  1024. // really daily, not every weekday
  1025. // -1 because the first day already counts (from 1-1-1980 to 1-1-1980 is 1 occurrence)
  1026. tEnd += ((m_sRecState.ulPeriod * 60) * (m_sRecState.ulOccurrenceCount - 1) );
  1027. break;
  1028. }
  1029. case RF_WEEKLY:
  1030. // $forwardcount is the maximum number of week occurrences we can go ahead after the first occurrence that
  1031. // is still inside the recurrence. We subtract one to make sure that the last week is never forwarded over
  1032. // (eg when numoccur = 2, and daycount = 1)
  1033. daycount = calcBits(m_sRecState.ulWeekDays);
  1034. if (daycount == 0) {
  1035. /*
  1036. * If the recurrence never occurs, the event
  1037. * will never finish. Just abort it.
  1038. */
  1039. tEnd = tStart;
  1040. break;
  1041. }
  1042. fwd = floor((double)(m_sRecState.ulOccurrenceCount-1) / daycount);
  1043. rest = m_sRecState.ulOccurrenceCount - (fwd*daycount) - 1;
  1044. fwd *= m_sRecState.ulPeriod;
  1045. tEnd += fwd * 7 * 24 * 60 * 60;
  1046. gmtime_safe(&tEnd, &tm);
  1047. for (int j = 1; m_sRecState.ulWeekDays != 0 && rest > 0; ++j) {
  1048. if ((tm.tm_wday + j)%7 == (int)getFirstDOW())
  1049. tEnd += (m_sRecState.ulPeriod-1) * 7 *24*60*60;
  1050. // If this is a matching day, once less occurrence to process
  1051. if(m_sRecState.ulWeekDays & (1<<((tm.tm_wday+j)%7)) )
  1052. --rest;
  1053. // next day
  1054. tEnd += 24*60*60;
  1055. }
  1056. break;
  1057. case RF_MONTHLY:
  1058. case RF_YEARLY:
  1059. gmtime_safe(&tStart, &tm);
  1060. tm.tm_year += 1900; // 1900 based
  1061. ++tm.tm_mon; // 1-based
  1062. fwd = (m_sRecState.ulOccurrenceCount-1) * m_sRecState.ulPeriod;
  1063. while (fwd > 0) {
  1064. tEnd += MonthInSeconds(tm.tm_year, tm.tm_mon);
  1065. if (tm.tm_mon%12 == 0) {
  1066. ++tm.tm_year;
  1067. tm.tm_mon = 1;
  1068. } else {
  1069. ++tm.tm_mon;
  1070. }
  1071. --fwd;
  1072. }
  1073. gmtime_safe(&tEnd, &tm);
  1074. tm.tm_year += 1900;
  1075. ++tm.tm_mon;
  1076. if (m_sRecState.ulPatternType == PT_MONTH) {
  1077. // month, (monthend?)
  1078. // compensation between 28 and 31
  1079. if (m_sRecState.ulDayOfMonth >= 28 && m_sRecState.ulDayOfMonth <= 31 && tm.tm_mday < (int)m_sRecState.ulDayOfMonth) {
  1080. if (tm.tm_mday < 28)
  1081. tEnd -= tm.tm_mday * 24 * 60 * 60;
  1082. else
  1083. tEnd += (DaysInMonth(tm.tm_year, tm.tm_mon) - tm.tm_mday) * 24 * 60 * 60;
  1084. }
  1085. } else if (m_sRecState.ulPatternType == PT_MONTH_NTH) {
  1086. // month Nth
  1087. if (m_sRecState.ulWeekNumber == 5)
  1088. // last day of month
  1089. tEnd += (DaysInMonth(tm.tm_year, tm.tm_mon) - tm.tm_mday) * 24 * 60 * 60;
  1090. else
  1091. tEnd -= (tm.tm_mday-1) * 24 * 60 * 60;
  1092. for (daycount = 0; daycount < 7; ++daycount) {
  1093. gmtime_safe(&tEnd, &tm);
  1094. tm.tm_year += 1900;
  1095. ++tm.tm_mon;
  1096. if (m_sRecState.ulWeekNumber == 5 && (1 << (tm.tm_wday - daycount) % 7) & m_sRecState.ulWeekDays)
  1097. tEnd -= tm.tm_mday * 24 * 60 * 60;
  1098. else if (m_sRecState.ulWeekNumber != 5 && (1 << (tm.tm_wday + daycount) % 7) & m_sRecState.ulWeekDays)
  1099. tEnd += (daycount + ((m_sRecState.ulWeekNumber-1)*7)) * 24 * 60 * 60;
  1100. }
  1101. }
  1102. break;
  1103. }
  1104. return tEnd;
  1105. }
  1106. ULONG recurrence::calcBits(ULONG x)
  1107. {
  1108. ULONG n = 0;
  1109. while (x) {
  1110. n += x&1;
  1111. x >>= 1;
  1112. }
  1113. return n;
  1114. }
  1115. /**
  1116. * Returns the number of occurrences in this DATE ending recurring
  1117. * item. It doesn't really matter what is returned, since this value
  1118. * is only used for display in a Recurrence Dialog window.
  1119. *
  1120. * @return number of occurrences
  1121. */
  1122. ULONG recurrence::calcCount()
  1123. {
  1124. ULONG ulCount = 0;
  1125. if (m_sRecState.ulEndType != ET_DATE)
  1126. return m_sRecState.ulOccurrenceCount;
  1127. if (m_sRecState.ulPeriod == 0)
  1128. return 0;
  1129. switch (m_sRecState.ulRecurFrequency) {
  1130. case RF_DAILY:
  1131. if (m_sRecState.ulPatternType == PT_WEEK) {
  1132. // every weekday item
  1133. // ulPeriod is stored in days (so this is always 1, right?)
  1134. ulCount = (getEndDate() - getStartDate()) / (24*60*60) / m_sRecState.ulPeriod;
  1135. // this one isn't one short?
  1136. --ulCount;
  1137. } else {
  1138. // ulPeriod is stored in minutes
  1139. ulCount = (getEndDate() - getStartDate()) / 60 / m_sRecState.ulPeriod;
  1140. }
  1141. break;
  1142. case RF_WEEKLY:
  1143. ulCount = (getEndDate() - getStartDate()) / (7*24*60*60) / m_sRecState.ulPeriod * calcBits(m_sRecState.ulWeekDays);
  1144. break;
  1145. case RF_MONTHLY:
  1146. ulCount = (AllMonthsFromTime(getEndDate()) - AllMonthsFromTime(getStartDate())) / m_sRecState.ulPeriod;
  1147. break;
  1148. case RF_YEARLY:
  1149. // ulPeriod is stored in months
  1150. ulCount = (AllMonthsFromTime(getEndDate()) - AllMonthsFromTime(getStartDate())) / m_sRecState.ulPeriod;
  1151. break;
  1152. }
  1153. // FIXME: some seem 1 short ??
  1154. return ulCount +1;
  1155. }
  1156. time_t recurrence::MonthInSeconds(ULONG year, ULONG month)
  1157. {
  1158. return DaysInMonth(year, month) * 24 * 60 * 60;
  1159. }
  1160. time_t recurrence::MonthsInSeconds(ULONG months)
  1161. {
  1162. ULONG year = 1601;
  1163. ULONG days = 0;
  1164. for (ULONG m = 0; m < months; ++m) {
  1165. days += DaysInMonth((m+1)%12, year);
  1166. if (m%12)
  1167. ++year;
  1168. }
  1169. return days * 24 * 60 * 60;
  1170. }
  1171. time_t recurrence::Minutes2Time(ULONG minutes)
  1172. {
  1173. return (minutes - NANOSECS_BETWEEN_EPOCHS/600000000) * 60;
  1174. }
  1175. ULONG recurrence::Time2Minutes(time_t time)
  1176. {
  1177. return (time / 60) + (NANOSECS_BETWEEN_EPOCHS/600000000);
  1178. }
  1179. ULONG recurrence::Minutes2Month(ULONG minutes)
  1180. {
  1181. return (ULONG)ceil((double)minutes / (31.0*24.0*60.0)) + 1;
  1182. }
  1183. time_t recurrence::StartOfDay(time_t t)
  1184. {
  1185. struct tm sTM;
  1186. gmtime_safe(&t, &sTM);
  1187. return t - (sTM.tm_hour * 60 * 60 + sTM.tm_min * 60 + sTM.tm_sec);
  1188. }
  1189. time_t recurrence::StartOfWeek(time_t t)
  1190. {
  1191. struct tm sTM;
  1192. gmtime_safe(&t, &sTM);
  1193. return t - (sTM.tm_wday * 24 * 60 * 60 + sTM.tm_hour * 60 * 60 + sTM.tm_min * 60 + sTM.tm_sec);
  1194. }
  1195. time_t recurrence::StartOfMonth(time_t t)
  1196. {
  1197. struct tm sTM;
  1198. gmtime_safe(&t, &sTM);
  1199. return t - ((sTM.tm_mday -1) * 24 * 60 * 60 + sTM.tm_hour * 60 * 60 + sTM.tm_min * 60 + sTM.tm_sec);
  1200. }
  1201. time_t recurrence::StartOfYear(time_t t)
  1202. {
  1203. struct tm sTM;
  1204. gmtime_safe(&t, &sTM);
  1205. return t - (sTM.tm_yday * 24 * 60 * 60 + sTM.tm_hour * 60 * 60 + sTM.tm_min * 60 + sTM.tm_sec);
  1206. }
  1207. bool recurrence::isLeapYear(ULONG year)
  1208. {
  1209. return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
  1210. }
  1211. ULONG recurrence::DaysInYear(ULONG year)
  1212. {
  1213. return isLeapYear(year) ? 366 : 365;
  1214. }
  1215. ULONG recurrence::DaysInMonth(ULONG month)
  1216. {
  1217. UCHAR days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
  1218. if (month>12)
  1219. return 0;
  1220. return days[month];
  1221. }
  1222. ULONG recurrence::DaysInMonth(ULONG year, ULONG month)
  1223. {
  1224. return DaysInMonth(month) + (month == 2 && isLeapYear(year) ? 1 : 0);
  1225. }
  1226. ULONG recurrence::MonthFromTime(time_t t)
  1227. {
  1228. struct tm lpT;
  1229. gmtime_safe(&t, &lpT);
  1230. return lpT.tm_mon + 1;
  1231. }
  1232. ULONG recurrence::YearFromTime(time_t t)
  1233. {
  1234. struct tm lpT;
  1235. gmtime_safe(&t, &lpT);
  1236. return lpT.tm_year + 1900;
  1237. }
  1238. ULONG recurrence::AllMonthsFromTime(time_t t)
  1239. {
  1240. struct tm lpT;
  1241. gmtime_safe(&t, &lpT);
  1242. return lpT.tm_mon + (lpT.tm_year + 1900 - 1601) * 12;
  1243. }
  1244. ULONG recurrence::WeekDayFromTime(time_t t)
  1245. {
  1246. struct tm lpT;
  1247. gmtime_safe(&t, &lpT);
  1248. return lpT.tm_wday;
  1249. }
  1250. ULONG recurrence::MonthDayFromTime(time_t t)
  1251. {
  1252. struct tm lpT;
  1253. gmtime_safe(&t, &lpT);
  1254. return lpT.tm_mday;
  1255. }
  1256. bool recurrence::CheckAddValidOccr(time_t tsNow, time_t tsStart, time_t tsEnd,
  1257. TIMEZONE_STRUCT ttZinfo, ULONG ulBusyStatus, OccrInfo **lppOccrInfoAll,
  1258. ULONG *lpcValues)
  1259. {
  1260. time_t tsOccStart = 0;
  1261. time_t tsOccEnd = 0;
  1262. ec_log_debug("Testing match: %lu ==> %s", tsNow, ctime(&tsNow));
  1263. if (!isOccurrenceValid(UTCToLocal(tsStart, ttZinfo), UTCToLocal(tsEnd, ttZinfo), tsNow + getStartTimeOffset())) {
  1264. ec_log_debug("Skipping match: %lu ==> %s", tsNow, ctime(&tsNow));
  1265. return false;
  1266. }
  1267. tsOccStart = LocalToUTC(tsNow + getStartTimeOffset(), ttZinfo);
  1268. tsOccEnd = LocalToUTC(tsNow + getEndTimeOffset(), ttZinfo);
  1269. ec_log_debug("Adding match: %lu ==> %s", tsOccStart, ctime(&tsOccStart));
  1270. AddValidOccr(tsOccStart, tsOccEnd, ulBusyStatus, lppOccrInfoAll, lpcValues);
  1271. return true;
  1272. }
  1273. /**
  1274. * Calculates occurrences of a recurrence between a specified period
  1275. * @param[in] tsStart starting time of period
  1276. * @param[in] tsEnd ending time of period
  1277. * @param[in] ttZinfo timezone struct of the recurrence
  1278. * @param[in] ulBusyStatus freebusy status of the recurrence
  1279. * @param[in] last only return last occurrence (fast)
  1280. * @param[out] lppOccrInfo array of occurrences
  1281. * @param[out] lpcValues number of occurrences in lppOccrInfo
  1282. *
  1283. * @return HRESULT
  1284. */
  1285. HRESULT recurrence::HrGetItems(time_t tsStart, time_t tsEnd,
  1286. TIMEZONE_STRUCT ttZinfo, ULONG ulBusyStatus, OccrInfo **lppOccrInfo,
  1287. ULONG *lpcValues, bool last)
  1288. {
  1289. HRESULT hr = 0;
  1290. time_t tsNow = 0;
  1291. time_t tsDayNow = 0;
  1292. time_t tsOccStart = 0;
  1293. time_t tsOccEnd = 0;
  1294. time_t tsDayEnd = 0;
  1295. time_t tsDayStart = 0;
  1296. time_t tsRangeEnd = 0;
  1297. time_t remainder = 0;
  1298. ULONG ulWday = 0;
  1299. OccrInfo *lpOccrInfoAll = *lppOccrInfo;
  1300. std::vector<RecurrenceState::Exception> lstExceptions;
  1301. RecurrenceState::Exception lpException;
  1302. tsDayStart = getStartDate();
  1303. if(getEndType() == NEVER || tsEnd < getEndDateTime())
  1304. tsRangeEnd = tsEnd;
  1305. else
  1306. tsRangeEnd = LocalToUTC(getEndDateTime(), ttZinfo);
  1307. tsDayEnd = StartOfDay(UTCToLocal(tsRangeEnd, ttZinfo));
  1308. ec_log_debug("DURATION START TIME: %lu ==> %s", tsStart, ctime(&tsStart));
  1309. ec_log_debug("DURATIION END TIME: %lu ==> %s", tsEnd, ctime(&tsEnd));
  1310. ec_log_debug("Rec Start TIME: %lu ==> %s", tsDayStart, ctime(&tsDayStart));
  1311. ec_log_debug("Rec End TIME: %lu ==> %s", tsDayEnd, ctime(&tsDayEnd));
  1312. switch (getFrequency())
  1313. {
  1314. case DAILY:
  1315. if(m_sRecState.ulPeriod <= 0)
  1316. m_sRecState.ulPeriod = 1440;
  1317. if (m_sRecState.ulPatternType == PT_DAY) {
  1318. if (last) {
  1319. remainder = (tsDayEnd-tsDayStart) % (m_sRecState.ulPeriod * 60);
  1320. for (tsNow = tsDayEnd - remainder; tsNow >= tsDayStart; tsNow -= m_sRecState.ulPeriod * 60)
  1321. if (CheckAddValidOccr(tsNow, tsStart, tsEnd, ttZinfo, ulBusyStatus, &lpOccrInfoAll, lpcValues))
  1322. break;
  1323. } else {
  1324. for (tsNow = tsDayStart; tsNow <= tsDayEnd; tsNow += m_sRecState.ulPeriod * 60)
  1325. CheckAddValidOccr(tsNow, tsStart, tsEnd, ttZinfo, ulBusyStatus, &lpOccrInfoAll, lpcValues);
  1326. }
  1327. break;
  1328. }
  1329. // daily, but every weekday (outlook)
  1330. else if (last) {
  1331. remainder = (tsDayEnd - tsDayStart) % (60 * 1440); // shouldn't this be m_sRecState.ulPeriod * 60? (see above)
  1332. for (tsNow = tsDayEnd - remainder; tsNow >= tsDayStart; tsNow -= 60 * 1440) { //604800 = 60*60*24*7
  1333. tm sTm;
  1334. gmtime_safe(&tsNow, &sTm);
  1335. if (sTm.tm_wday > 0 && sTm.tm_wday < 6 &&
  1336. CheckAddValidOccr(tsNow, tsStart, tsEnd, ttZinfo, ulBusyStatus, &lpOccrInfoAll, lpcValues))
  1337. break;
  1338. }
  1339. break;
  1340. }
  1341. for (tsNow = tsDayStart ;tsNow <= tsDayEnd; tsNow += 60 * 1440) { //604800 = 60*60*24*7
  1342. tm sTm;
  1343. gmtime_safe(&tsNow, &sTm);
  1344. if (sTm.tm_wday > 0 && sTm.tm_wday < 6)
  1345. CheckAddValidOccr(tsNow, tsStart, tsEnd, ttZinfo, ulBusyStatus, &lpOccrInfoAll, lpcValues);
  1346. }
  1347. break;// CASE : DAILY
  1348. case WEEKLY:
  1349. if(m_sRecState.ulPeriod <= 0)
  1350. m_sRecState.ulPeriod = 1;
  1351. if(last) {
  1352. bool found = false;
  1353. remainder = (tsDayEnd-tsDayStart) % (m_sRecState.ulPeriod * 604800);
  1354. for(tsNow = tsDayEnd-remainder; tsNow >= tsDayStart; tsNow -= (m_sRecState.ulPeriod * 604800)) { //604800 = 60*60*24*7
  1355. // Loop through the whole following week to the first occurrence of the week, add each day that is specified
  1356. for (int i = 6; i >= 0; --i) {
  1357. ULONG ulWday = 0;
  1358. tsDayNow = tsNow + i * 1440 * 60; // 60 * 60 * 24 = 1440
  1359. ec_log_debug("Checking for weekly tsDayNow: %s", ctime(&tsDayNow));
  1360. ulWday = WeekDayFromTime(tsDayNow);
  1361. if (m_sRecState.ulWeekDays & (1 << ulWday) &&
  1362. CheckAddValidOccr(tsDayNow, tsStart, tsEnd, ttZinfo, ulBusyStatus, &lpOccrInfoAll, lpcValues)) {
  1363. found = true;
  1364. break;
  1365. }
  1366. }
  1367. if (found)
  1368. break;
  1369. }
  1370. break;
  1371. }
  1372. for (tsNow = tsDayStart; tsNow <= tsDayEnd; tsNow += (m_sRecState.ulPeriod * 604800)) { //604800 = 60*60*24*7
  1373. // Loop through the whole following week to the first occurrence of the week, add each day that is specified
  1374. for (int i = 0; i < 7; ++i) {
  1375. ULONG ulWday = 0;
  1376. tsDayNow = tsNow + i * 1440 * 60; // 60 * 60 * 24 = 1440
  1377. ec_log_debug("Checking for weekly tsDayNow: %s", ctime(&tsDayNow));
  1378. ulWday = WeekDayFromTime(tsDayNow);
  1379. if (m_sRecState.ulWeekDays & (1 << ulWday))
  1380. CheckAddValidOccr(tsDayNow, tsStart, tsEnd, ttZinfo, ulBusyStatus, &lpOccrInfoAll, lpcValues);
  1381. }
  1382. }
  1383. break;// CASE : WEEKLY
  1384. case MONTHLY:
  1385. if(m_sRecState.ulPeriod <= 0)
  1386. m_sRecState.ulPeriod = 1;
  1387. tsNow = StartOfMonth(tsDayStart);
  1388. ec_log_debug("Monthly Recurrence");
  1389. while(tsNow < tsDayEnd) {
  1390. ULONG ulDiffrence = 0;
  1391. ULONG ulDaysOfMonths = 0;
  1392. ULONG ulDayCounter = 0;
  1393. ULONG ulValidDay = 0;
  1394. ulDaysOfMonths = countDaysOfMonth(tsNow);
  1395. if(m_sRecState.ulDayOfMonth != 0) {
  1396. ulDiffrence = 1;
  1397. if(m_sRecState.ulWeekDays == 0 && m_sRecState.ulDayOfMonth > ulDaysOfMonths)
  1398. ulDiffrence = m_sRecState.ulDayOfMonth - ulDaysOfMonths + 1;
  1399. tsDayNow = tsNow + (m_sRecState.ulDayOfMonth - ulDiffrence) *24*60*60;
  1400. } else if( m_sRecState.ulWeekNumber != 0 && m_sRecState.ulWeekDays != 0) {
  1401. if (m_sRecState.ulWeekNumber < 5) {
  1402. ulDayCounter = 0;
  1403. for (ULONG ulDay = 0; ulDay < DaysInMonth(YearFromTime(tsNow), MonthFromTime(tsNow)); ++ulDay) {
  1404. tsDayNow = tsNow + ulDay * 60 * 60 * 24;
  1405. ulWday = WeekDayFromTime(tsDayNow);
  1406. if (m_sRecState.ulWeekDays & ( 1<< ulWday))
  1407. ++ulDayCounter;
  1408. if (m_sRecState.ulWeekNumber == ulDayCounter) {
  1409. ulValidDay = ulDay;
  1410. break;
  1411. }
  1412. }
  1413. tsDayNow = tsNow + ulValidDay * 60 * 60 * 24;
  1414. } else {
  1415. ulDaysOfMonths = DaysInMonth(YearFromTime(tsNow), MonthFromTime(tsNow));
  1416. tsDayNow = tsNow + (ulDaysOfMonths - 1) * 60 * 60 * 24;
  1417. while ((m_sRecState.ulWeekDays & ( 1<< WeekDayFromTime(tsDayNow))) == 0)
  1418. tsDayNow -= 86400; // deduct 1 day
  1419. }
  1420. }
  1421. if(isOccurrenceValid(tsStart, tsEnd, tsDayNow + getStartTimeOffset())){
  1422. tsOccStart = LocalToUTC(tsDayNow + getStartTimeOffset(), ttZinfo);
  1423. tsOccEnd = LocalToUTC(tsDayNow + getEndTimeOffset(), ttZinfo);
  1424. AddValidOccr(tsOccStart, tsOccEnd, ulBusyStatus, &lpOccrInfoAll, lpcValues);
  1425. }
  1426. tsNow += DaysTillMonth(tsNow, m_sRecState.ulPeriod) * 60 * 60 * 24;
  1427. }
  1428. break;// CASE : MONTHLY
  1429. case YEARLY:
  1430. if(m_sRecState.ulPeriod <= 0)
  1431. m_sRecState.ulPeriod = 12;
  1432. tsNow = StartOfYear(tsDayStart);
  1433. ec_log_debug("Recurrence Type Yearly");
  1434. while(tsNow < tsDayEnd) {
  1435. ULONG ulMonthDay = 0;
  1436. time_t tMonthStart = 0;
  1437. time_t tsMonthNow = 0;
  1438. ULONG ulValidDay = 0;
  1439. if(m_sRecState.ulDayOfMonth != 0) {
  1440. ulMonthDay = m_sRecState.ulDayOfMonth;
  1441. tMonthStart = tsNow + DaysTillMonth(tsNow, getMonth()-1) * 24 * 60 *60;
  1442. if( ulMonthDay > DaysInMonth(YearFromTime(tMonthStart),MonthFromTime(tMonthStart)))
  1443. ulMonthDay = DaysInMonth(YearFromTime(tMonthStart),MonthFromTime(tMonthStart));
  1444. tsDayNow = tMonthStart + (ulMonthDay -1) * 24 * 60 * 60;
  1445. } else if( m_sRecState.ulWeekNumber != 0 && m_sRecState.ulWeekDays != 0) {
  1446. tsMonthNow = tsNow + DaysTillMonth(tsNow, getMonth()-1) * 24 * 60 * 60;
  1447. ec_log_debug("Checking yearly nth Weekday Occrrence ulMonthNow: %s", ctime(&tsMonthNow));
  1448. for (int ulDay = 0; ulDay < 7; ++ulDay) {
  1449. tsDayNow = tsMonthNow + ulDay * 60 * 60 * 24;
  1450. ulWday = WeekDayFromTime(tsDayNow);
  1451. if (m_sRecState.ulWeekDays & ( 1<< ulWday)){
  1452. ulValidDay = ulDay;
  1453. break;
  1454. }
  1455. }
  1456. tsDayNow = tsMonthNow + (ulValidDay + (getWeekNumber() - 1) * 7) * 60 * 60 * 24;
  1457. while (StartOfMonth(tsDayNow) != StartOfMonth(tsMonthNow))
  1458. tsDayNow -= 7 * 24 * 60 * 60;
  1459. }
  1460. if(isOccurrenceValid(tsStart, tsEnd, tsDayNow + getStartTimeOffset())){
  1461. tsOccStart = LocalToUTC(tsDayNow + getStartTimeOffset(), ttZinfo);
  1462. tsOccEnd = LocalToUTC(tsDayNow + getEndTimeOffset(), ttZinfo);
  1463. AddValidOccr(tsOccStart, tsOccEnd, ulBusyStatus, &lpOccrInfoAll, lpcValues);
  1464. }
  1465. tsNow += DaysTillMonth(tsNow, m_sRecState.ulPeriod) * 60 * 60 * 24;
  1466. }
  1467. break;
  1468. }
  1469. lstExceptions = m_sRecState.lstExceptions;
  1470. while(lstExceptions.size() != 0)
  1471. {
  1472. OccrInfo sOccrInfo;
  1473. lpException = lstExceptions.back();
  1474. // APPT_STARTWHOLE
  1475. RTimeToUnixTime(lpException.ulStartDateTime, &tsOccStart); // tsOccStart == localtime
  1476. tsOccStart = LocalToUTC(tsOccStart, ttZinfo);
  1477. if(tsOccStart > tsEnd) { // tsStart, tsEnd == gmtime
  1478. ec_log_debug("Skipping exception start match: %lu ==> %s", tsOccStart, ctime(&tsOccStart));
  1479. goto next;
  1480. }
  1481. UnixTimeToRTime(tsOccStart, &sOccrInfo.fbBlock.m_tmStart); // gmtime in rtime, is this correct?
  1482. // APPT_ENDWHOLE
  1483. RTimeToUnixTime(lpException.ulEndDateTime, &tsOccEnd);
  1484. tsOccEnd = LocalToUTC(tsOccEnd, ttZinfo);
  1485. if(tsOccEnd < tsStart) {
  1486. ec_log_debug("Skipping exception end match: %lu ==> %s", tsOccEnd, ctime(&tsOccEnd));
  1487. goto next;
  1488. }
  1489. UnixTimeToRTime(tsOccEnd, &sOccrInfo.fbBlock.m_tmEnd);
  1490. // APPT_FBSTATUS
  1491. sOccrInfo.fbBlock.m_fbstatus = (FBStatus)lpException.ulBusyStatus;
  1492. // Freebusy status
  1493. RTimeToUnixTime(lpException.ulOriginalStartDate, &sOccrInfo.tBaseDate);
  1494. ec_log_debug("Adding exception match: %lu ==> %s", sOccrInfo.tBaseDate, ctime(&sOccrInfo.tBaseDate));
  1495. hr = HrAddFBBlock(sOccrInfo, &lpOccrInfoAll, lpcValues);
  1496. next:
  1497. lstExceptions.pop_back();
  1498. }
  1499. *lppOccrInfo = lpOccrInfoAll;
  1500. return hr;
  1501. }
  1502. HRESULT recurrence::AddValidOccr(time_t tsOccrStart, time_t tsOccrEnd, ULONG ulBusyStatus, OccrInfo **lpFBBlocksAll, ULONG *lpcValues)
  1503. {
  1504. OccrInfo sOccrInfo;
  1505. // APPT_STARTWHOLE
  1506. UnixTimeToRTime(tsOccrStart, &sOccrInfo.fbBlock.m_tmStart);
  1507. sOccrInfo.tBaseDate = tsOccrStart;
  1508. // APPT_ENDWHOLE
  1509. UnixTimeToRTime(tsOccrEnd, &sOccrInfo.fbBlock.m_tmEnd);
  1510. sOccrInfo.fbBlock.m_fbstatus = (FBStatus)ulBusyStatus;
  1511. return HrAddFBBlock(sOccrInfo, lpFBBlocksAll, lpcValues);
  1512. }
  1513. bool recurrence::isOccurrenceValid(time_t tsPeriodStart, time_t tsPeriodEnd, time_t tsNewOcc)
  1514. {
  1515. if (isException(tsNewOcc))
  1516. return false;
  1517. if (tsNewOcc < tsPeriodStart || tsNewOcc > tsPeriodEnd)
  1518. return false;
  1519. if (isDeletedOccurrence(tsNewOcc))
  1520. return false;
  1521. return true;
  1522. }
  1523. /**
  1524. * checks if the Occurrence is deleted.
  1525. * @param tsOccDate Occurrence Unix timestamp
  1526. * @return bool
  1527. */
  1528. bool recurrence::isDeletedOccurrence(time_t tsOccDate)
  1529. {
  1530. for (const auto oc : getDeletedExceptions())
  1531. if (tsOccDate == oc)
  1532. return true;
  1533. return false;
  1534. }
  1535. /**
  1536. * checks if the Occurrence is a modified ocurrence
  1537. * @param tsOccDate Occurrence Unix timestamp
  1538. * @return bool
  1539. */
  1540. bool recurrence::isException(time_t tsOccDate)
  1541. {
  1542. for (const auto oc : getModifiedOccurrences())
  1543. if (StartOfDay(tsOccDate) == StartOfDay(oc))
  1544. return true;
  1545. return false;
  1546. }
  1547. ULONG recurrence::countDaysOfMonth(time_t tsDate)
  1548. {
  1549. ULONG ulYear = 0;
  1550. ULONG ulMonth = 0;
  1551. ULONG ulDays = 0;
  1552. static const ULONG ulDaysArray[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  1553. ulYear = this->YearFromTime(tsDate);
  1554. ulMonth = this->MonthFromTime(tsDate);
  1555. if(this->isLeapYear(ulYear) && ulMonth == 2 )
  1556. ulDays = 29;
  1557. else
  1558. ulDays = ulDaysArray[ulMonth -1];
  1559. return ulDays;
  1560. }
  1561. ULONG recurrence::DaysTillMonth(time_t tsDate, ULONG ulMonth)
  1562. {
  1563. ULONG ulDays = 0;
  1564. for (ULONG ul = MonthFromTime(tsDate);
  1565. ul < ulMonth + MonthFromTime(tsDate); ++ul)
  1566. ulDays += DaysInMonth(YearFromTime(tsDate), ul);
  1567. return ulDays;
  1568. }
  1569. } /* namespace */