123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971 |
- #include <kopano/platform.h>
- #include <memory>
- #include "icalrecurrence.h"
- #include "vconverter.h"
- #include "nameids.h"
- #include "valarm.h"
- #include <mapicode.h>
- #include <kopano/mapiext.h>
- #include <kopano/memory.hpp>
- #include <mapix.h>
- #include <mapiutil.h>
- #include <cmath>
- #include <algorithm>
- #include "freebusy.h"
- using namespace KCHL;
- static bool operator ==(const SPropValue &spv, ULONG ulPropTag)
- {
- return spv.ulPropTag == ulPropTag;
- }
- namespace KC {
- HRESULT ICalRecurrence::HrParseICalRecurrenceRule(TIMEZONE_STRUCT sTimeZone, icalcomponent *lpicRootEvent, icalcomponent *lpicEvent,
- bool bIsAllday, LPSPropTagArray lpNamedProps, icalitem *lpIcalItem)
- {
- HRESULT hr = hrSuccess;
- std::unique_ptr<recurrence> lpRec;
- icalproperty *lpicProp = NULL;
- icalrecurrencetype icRRule;
- int i = 0;
- ULONG ulWeekDays = 0;
- time_t dtUTCStart = 0;
- time_t dtLocalStart = 0;
- time_t dtUTCEnd = 0;
- time_t dtUTCUntil = 0;
- time_t exUTCDate = 0;
- time_t exLocalDate = 0;
- SPropValue sPropVal = {0};
- struct tm tm = {0};
- lpicProp = icalcomponent_get_first_property(lpicEvent, ICAL_RRULE_PROPERTY);
- if (lpicProp == NULL)
- return hr;
- icRRule = icalproperty_get_rrule(lpicProp);
- lpicProp = icalcomponent_get_first_property(lpicEvent, ICAL_DTSTART_PROPERTY);
- if (lpicProp == nullptr)
- return MAPI_E_NOT_FOUND;
-
-
-
- dtLocalStart = icaltime_as_timet(icalproperty_get_dtstart(lpicProp));
- gmtime_safe(&dtLocalStart, &tm);
-
- dtUTCStart = ICalTimeTypeToUTC(lpicRootEvent, lpicProp);
- lpicProp = icalcomponent_get_first_property(lpicEvent, ICAL_DTEND_PROPERTY);
- if (!lpicProp) {
-
- lpicProp = icalcomponent_get_first_property(lpicEvent, ICAL_DUE_PROPERTY);
- }
- if (!lpicProp)
- {
-
- lpicProp = icalcomponent_get_first_property(lpicEvent, ICAL_DURATION_PROPERTY);
- if (lpicProp == nullptr)
- return MAPI_E_NOT_FOUND;
- dtUTCEnd = dtUTCStart + icaldurationtype_as_int(icalproperty_get_duration(lpicProp));
- } else {
- dtUTCEnd = ICalTimeTypeToUTC(lpicRootEvent, lpicProp);
- }
- lpRec.reset(new recurrence);
-
- lpRec->setStartDateTime(dtLocalStart);
-
- lpRec->setFirstDOW(0);
- sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_RECURRENCETYPE], PT_LONG);
- switch (icRRule.freq) {
- case ICAL_DAILY_RECURRENCE:
- sPropVal.Value.ul = 1;
- lpRec->setFrequency(recurrence::DAILY);
- break;
- case ICAL_WEEKLY_RECURRENCE:
- sPropVal.Value.ul = 2;
- lpRec->setFrequency(recurrence::WEEKLY);
-
- lpRec->setWeekDays(1 << tm.tm_wday);
-
- lpRec->setFirstDOW(1);
- break;
- case ICAL_MONTHLY_RECURRENCE:
- sPropVal.Value.ul = 3;
- lpRec->setFrequency(recurrence::MONTHLY);
- break;
- case ICAL_YEARLY_RECURRENCE:
- sPropVal.Value.ul = 4;
- lpRec->setFrequency(recurrence::YEARLY);
- lpRec->setDayOfMonth(tm.tm_mday);
- if (icRRule.by_month[0] != ICAL_RECURRENCE_ARRAY_MAX)
- lpRec->setMonth(icRRule.by_month[0]);
- break;
- default:
- return MAPI_E_INVALID_PARAMETER;
- };
- lpIcalItem->lstMsgProps.push_back(sPropVal);
-
- lpRec->setInterval(icRRule.interval);
-
- if (icRRule.by_day[i] != ICAL_RECURRENCE_ARRAY_MAX) {
- ulWeekDays = 0;
- if (icRRule.by_day[0] < 0) {
-
- ulWeekDays |= (1 << (abs(icRRule.by_day[0]%8) - 1));
-
- lpRec->setWeekNumber(5);
- } else if (icRRule.by_day[0] >= 1 && icRRule.by_day[0] <= 7) {
-
- i = 0;
- while (icRRule.by_day[i] != ICAL_RECURRENCE_ARRAY_MAX) {
- ulWeekDays |= (1 << (icRRule.by_day[i] - 1));
- ++i;
- }
-
- if (icRRule.by_set_pos[0] != ICAL_RECURRENCE_ARRAY_MAX) {
-
- lpRec->setWeekNumber(icRRule.by_set_pos[0]);
- } else if (lpRec->getFrequency() == recurrence::MONTHLY) {
-
-
- lpRec->setFrequency(recurrence::WEEKLY);
-
- lpRec->setWeekDays(1 << tm.tm_wday);
-
- lpRec->setFirstDOW(1);
- sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_RECURRENCETYPE], PT_LONG);
- sPropVal.Value.ul = 2;
- lpIcalItem->lstMsgProps.push_back(sPropVal);
- } else if (lpRec->getFrequency() == recurrence::YEARLY) {
- return MAPI_E_NO_SUPPORT;
- }
- } else {
-
- ulWeekDays |= (1 << ((icRRule.by_day[0]%8) - 1));
- lpRec->setWeekNumber((int)(icRRule.by_day[0]/8));
- }
- lpRec->setWeekDays(ulWeekDays);
- } else if (icRRule.by_month_day[0] != ICAL_RECURRENCE_ARRAY_MAX) {
-
-
- lpRec->setDayOfMonth(icRRule.by_month_day[0]);
- }else if (icRRule.freq == ICAL_MONTHLY_RECURRENCE || icRRule.freq == ICAL_YEARLY_RECURRENCE) {
-
- lpRec->setDayOfMonth(tm.tm_mday);
- }
- lpRec->setStartDateTime(lpRec->calcStartDate());
- if (icRRule.count != 0) {
-
- lpRec->setEndType(recurrence::NUMBER);
- lpRec->setCount(icRRule.count);
-
- lpRec->setEndDate(lpRec->calcEndDate());
- dtUTCUntil = lpRec->getEndDate();
- } else if (icRRule.until.year != 0) {
-
- lpRec->setEndType(recurrence::DATE);
-
- dtUTCUntil = icaltime_as_timet_with_zone(icRRule.until, NULL);
- lpRec->setEndDate(UTCToLocal(dtUTCUntil, sTimeZone));
-
- lpRec->setCount(lpRec->calcCount());
- } else {
-
- lpRec->setEndType(recurrence::NEVER);
-
- lpRec->setCount(10);
-
-
-
- dtUTCUntil = 0x7FFFFFFF;
- }
-
- lpRec->setEndTimeOffset((lpRec->getStartTimeOffset() + dtUTCEnd - dtUTCStart) / 60);
-
- UnixTimeToFileTime(dtUTCUntil, &sPropVal.Value.ft);
- sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_RECURRENCE_END], PT_SYSTIME);
- lpIcalItem->lstMsgProps.push_back(sPropVal);
-
- lpicProp = icalcomponent_get_first_property(lpicEvent, ICAL_EXDATE_PROPERTY);
- while (lpicProp != NULL)
- {
- exUTCDate = ICalTimeTypeToUTC(lpicRootEvent, lpicProp);
- exLocalDate = UTCToLocal(exUTCDate, sTimeZone);
- lpRec->addDeletedException(exLocalDate);
- lpicProp = icalcomponent_get_next_property(lpicEvent, ICAL_EXDATE_PROPERTY);
- }
-
- if (lpRec->getEndType() == recurrence::DATE)
- {
- memory_ptr<OccrInfo> lpOccrInfo;
- ULONG cValues = 0;
- hr = lpRec->HrGetItems(dtUTCStart, dtUTCUntil, sTimeZone, 0, &~lpOccrInfo, &cValues, true);
- if (hr == hrSuccess && cValues > 0) {
- dtUTCUntil = lpOccrInfo[cValues-1].tBaseDate;
- lpRec->setEndDate(UTCToLocal(dtUTCUntil, sTimeZone));
- }
- }
-
- sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_SIDEEFFECT], PT_LONG);
- sPropVal.Value.ul = 353;
- lpIcalItem->lstMsgProps.push_back(sPropVal);
-
- UnixTimeToFileTime(LocalToUTC(recurrence::StartOfDay(lpRec->getStartDateTime()), sTimeZone), &sPropVal.Value.ft);
- sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_RECURRENCE_START], PT_SYSTIME);
- lpIcalItem->lstMsgProps.push_back(sPropVal);
- lpicProp = icalcomponent_get_first_property(lpicEvent, ICAL_RECURRENCEID_PROPERTY);
- if(!lpicProp) {
- UnixTimeToFileTime(LocalToUTC(lpRec->getStartDateTime(), sTimeZone), &sPropVal.Value.ft);
-
- sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_APPTSTARTWHOLE], PT_SYSTIME);
- lpIcalItem->lstMsgProps.push_back(sPropVal);
-
- sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_COMMONSTART], PT_SYSTIME);
- lpIcalItem->lstMsgProps.push_back(sPropVal);
- UnixTimeToFileTime(LocalToUTC(lpRec->getStartDateTime() + (dtUTCEnd - dtUTCStart), sTimeZone), &sPropVal.Value.ft);
-
- sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_APPTENDWHOLE], PT_SYSTIME);
- lpIcalItem->lstMsgProps.push_back(sPropVal);
-
- sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_COMMONEND], PT_SYSTIME);
- lpIcalItem->lstMsgProps.push_back(sPropVal);
- }
- lpIcalItem->lpRecurrence = lpRec.release();
- return hr;
- }
- HRESULT ICalRecurrence::HrMakeMAPIException(icalcomponent *lpEventRoot, icalcomponent *lpicEvent, icalitem *lpIcalItem, bool bIsAllDay, LPSPropTagArray lpNamedProps, std::string& strCharset, icalitem::exception *lpEx)
- {
- HRESULT hr;
- icalproperty *lpicProp = NULL;
- time_t ttStartLocalTime = 0;
- time_t ttEndLocalTime = 0;
- time_t ttStartUtcTime = 0;
- time_t ttEndUtcTime = 0;
- time_t ttOriginalUtcTime = 0;
- time_t ttOriginalLocalTime = 0;
- ULONG ulId = 0;
- ULONG i = 0;
- SPropValue sPropVal;
- std::wstring strIcalProp;
- bool bXMS = false;
- icalcomponent *lpicAlarm = NULL;
- LONG ulRemindBefore = 0;
- time_t ttReminderTime = 0;
- bool bReminderSet = false;
- convert_context converter;
- const char *lpszProp;
- bool abOldPresent[8] = {false};
- bool abNewPresent[8] = {false};
- SizedSPropTagArray(8, sptaCopy) = { 8, {
- PR_SUBJECT,
- CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_LOCATION], PT_STRING8),
- PR_BODY,
- CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_LABEL], PT_LONG),
- CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_REMINDERSET], PT_BOOLEAN),
- CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_REMINDERMINUTESBEFORESTART], PT_LONG),
- CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_ALLDAYEVENT], PT_BOOLEAN),
- CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_BUSYSTATUS], PT_LONG)
- } };
- bool bOldIsAllDay = false;
- lpicProp = icalcomponent_get_first_property(lpicEvent, ICAL_RECURRENCEID_PROPERTY);
- if (lpicProp == NULL)
-
- return MAPI_E_NOT_FOUND;
- ttOriginalUtcTime = ICalTimeTypeToUTC(lpEventRoot, lpicProp);
- ttOriginalLocalTime = icaltime_as_timet(icalvalue_get_datetime(icalproperty_get_value(lpicProp)));
-
- lpEx->tBaseDate = ttOriginalUtcTime;
- lpicProp = icalcomponent_get_first_property(lpicEvent, ICAL_DTSTART_PROPERTY);
- if (lpicProp == NULL)
- return MAPI_E_NOT_FOUND;
- ttStartUtcTime = ICalTimeTypeToUTC(lpEventRoot, lpicProp);
- ttStartLocalTime = icaltime_as_timet(icalvalue_get_datetime(icalproperty_get_value(lpicProp)));
- lpEx->tStartDate = ttStartUtcTime;
- lpicProp = icalcomponent_get_first_property(lpicEvent, ICAL_DTEND_PROPERTY);
- if (lpicProp == NULL)
- return MAPI_E_NOT_FOUND;
- ttEndUtcTime = ICalTimeTypeToUTC(lpEventRoot, lpicProp);
- ttEndLocalTime = icaltime_as_timet(icalvalue_get_datetime(icalproperty_get_value(lpicProp)));
- hr = lpIcalItem->lpRecurrence->addModifiedException(ttStartLocalTime, ttEndLocalTime, ttOriginalLocalTime, &ulId);
- if (hr != hrSuccess)
- return hr;
- sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_RECURRINGBASE], PT_SYSTIME);
- UnixTimeToFileTime(ttOriginalUtcTime, &sPropVal.Value.ft);
- lpEx->lstMsgProps.push_back(sPropVal);
- sPropVal.ulPropTag = PR_EXCEPTION_STARTTIME;
- UnixTimeToFileTime(ttStartLocalTime, &sPropVal.Value.ft);
- lpEx->lstAttachProps.push_back(sPropVal);
- sPropVal.ulPropTag = PR_EXCEPTION_ENDTIME;
- UnixTimeToFileTime(ttEndLocalTime, &sPropVal.Value.ft);
- lpEx->lstAttachProps.push_back(sPropVal);
- sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_APPTSTARTWHOLE], PT_SYSTIME);
- UnixTimeToFileTime(ttStartUtcTime, &sPropVal.Value.ft);
- lpEx->lstMsgProps.push_back(sPropVal);
-
- sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_COMMONSTART], PT_SYSTIME);
- lpEx->lstMsgProps.push_back(sPropVal);
- sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_RECURRENCE_START], PT_SYSTIME);
- lpEx->lstMsgProps.push_back(sPropVal);
- sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_APPTENDWHOLE], PT_SYSTIME);
- UnixTimeToFileTime(ttEndUtcTime, &sPropVal.Value.ft);
- lpEx->lstMsgProps.push_back(sPropVal);
- sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_COMMONEND], PT_SYSTIME);
- lpEx->lstMsgProps.push_back(sPropVal);
- sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_RECURRENCE_END], PT_SYSTIME);
- lpEx->lstMsgProps.push_back(sPropVal);
- sPropVal.ulPropTag = PR_EXCEPTION_ENDTIME;
- UnixTimeToFileTime(ttEndLocalTime, &sPropVal.Value.ft);
- lpEx->lstAttachProps.push_back(sPropVal);
- sPropVal.ulPropTag = PR_DISPLAY_NAME_W;
- HrCopyString(lpIcalItem->base, L"Untitled", &sPropVal.Value.lpszW);
- lpEx->lstAttachProps.push_back(sPropVal);
- sPropVal.ulPropTag = PR_ATTACH_METHOD;
- sPropVal.Value.ul = ATTACH_EMBEDDED_MSG;
- lpEx->lstAttachProps.push_back(sPropVal);
- sPropVal.ulPropTag = PR_ATTACH_FLAGS;
- sPropVal.Value.ul = 0;
- lpEx->lstAttachProps.push_back(sPropVal);
- sPropVal.ulPropTag = PR_ATTACHMENT_LINKID;
- sPropVal.Value.ul = 0;
- lpEx->lstAttachProps.push_back(sPropVal);
- sPropVal.ulPropTag = PR_ATTACHMENT_HIDDEN;
- sPropVal.Value.b = TRUE;
- lpEx->lstAttachProps.push_back(sPropVal);
-
- sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_HIDE_ATTACH], PT_BOOLEAN);;
- sPropVal.Value.b = TRUE;
- lpIcalItem->lstMsgProps.push_back(sPropVal);
-
- sPropVal.ulPropTag = PR_ATTACHMENT_FLAGS;
- sPropVal.Value.ul = 2;
- lpEx->lstAttachProps.push_back(sPropVal);
- sPropVal.ulPropTag = PR_MESSAGE_CLASS_W;
- HrCopyString(lpIcalItem->base, L"IPM.OLE.CLASS.{00061055-0000-0000-C000-000000000046}", &sPropVal.Value.lpszW);
- lpEx->lstMsgProps.push_back(sPropVal);
-
- for (const auto &prop : lpIcalItem->lstMsgProps)
- for (i = 0; i < sptaCopy.cValues; ++i) {
- if (sptaCopy.aulPropTag[i] == prop.ulPropTag) {
- abOldPresent[i] = true;
- if (sptaCopy.aulPropTag[i] != PR_BODY)
- lpEx->lstMsgProps.push_back(prop);
- if (sptaCopy.aulPropTag[i] == CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_ALLDAYEVENT], PT_BOOLEAN))
- bOldIsAllDay = prop.Value.b;
- break;
- }
- }
-
-
- lpicProp = icalcomponent_get_first_property(lpicEvent, ICAL_ANY_PROPERTY);
- while (lpicProp) {
- switch (icalproperty_isa(lpicProp)) {
- case ICAL_SUMMARY_PROPERTY:
- lpszProp = icalproperty_get_summary(lpicProp);
- strIcalProp = converter.convert_to<std::wstring>(lpszProp, rawsize(lpszProp), strCharset.c_str());
- hr = lpIcalItem->lpRecurrence->setModifiedSubject(ulId, strIcalProp.c_str());
- if (hr != hrSuccess)
- return hr;
- sPropVal.ulPropTag = PR_SUBJECT_W;
- HrCopyString(lpIcalItem->base, strIcalProp.c_str(), &sPropVal.Value.lpszW);
- lpEx->lstMsgProps.push_back(sPropVal);
- abNewPresent[0] = true;
- break;
- case ICAL_LOCATION_PROPERTY:
- lpszProp = icalproperty_get_location(lpicProp);
- strIcalProp = converter.convert_to<std::wstring>(lpszProp, rawsize(lpszProp), strCharset.c_str());
- hr = lpIcalItem->lpRecurrence->setModifiedLocation(ulId, strIcalProp.c_str());
- if (hr != hrSuccess)
- return hr;
- sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_LOCATION], PT_UNICODE);
- HrCopyString(lpIcalItem->base, strIcalProp.c_str(), &sPropVal.Value.lpszW);
- lpEx->lstMsgProps.push_back(sPropVal);
- abNewPresent[1] = true;
- break;
- case ICAL_TRANSP_PROPERTY:
- if (!bXMS) {
- ULONG ulBusyStatus = 2;
- switch(icalproperty_get_transp(lpicProp)){
- case ICAL_TRANSP_TRANSPARENT:
- ulBusyStatus = 0;
- break;
- case ICAL_TRANSP_OPAQUE:
- ulBusyStatus = 2;
- break;
- default:
- break;
- }
- hr = lpIcalItem->lpRecurrence->setModifiedBusyStatus(ulId, ulBusyStatus);
- if (hr != hrSuccess)
- return hr;
- sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_BUSYSTATUS], PT_LONG);
- sPropVal.Value.ul = ulBusyStatus;
- lpEx->lstMsgProps.push_back(sPropVal);
- abNewPresent[7] = true;
- }
- break;
- case ICAL_X_PROPERTY:
- if (strcmp(icalproperty_get_x_name(lpicProp), "X-MICROSOFT-CDO-BUSYSTATUS") == 0) {
- ULONG ulBusyStatus = 2;
- const char *lpszIcalProp = icalproperty_get_x(lpicProp);
- if (strcmp(lpszIcalProp, "FREE") == 0)
- ulBusyStatus = 0;
- else if (strcmp(lpszIcalProp, "TENTATIVE") == 0)
- ulBusyStatus = 1;
- else if (strcmp(lpszIcalProp, "OOF") == 0)
- ulBusyStatus = 3;
- hr = lpIcalItem->lpRecurrence->setModifiedBusyStatus(ulId, ulBusyStatus);
- if (hr != hrSuccess)
- return hr;
- sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_BUSYSTATUS], PT_LONG);
- sPropVal.Value.ul = ulBusyStatus;
- lpEx->lstMsgProps.push_back(sPropVal);
- bXMS = true;
- abNewPresent[7] = true;
- }
- break;
- case ICAL_DESCRIPTION_PROPERTY:
- lpszProp = icalproperty_get_description(lpicProp);
- strIcalProp = converter.convert_to<std::wstring>(lpszProp, rawsize(lpszProp), strCharset.c_str());
- hr = lpIcalItem->lpRecurrence->setModifiedBody(ulId);
- if (hr != hrSuccess)
- return hr;
- sPropVal.ulPropTag = PR_BODY_W;
- HrCopyString(lpIcalItem->base, strIcalProp.c_str(), &sPropVal.Value.lpszW);
- lpEx->lstMsgProps.push_back(sPropVal);
- abNewPresent[2] = true;
- break;
- default:
-
- break;
- };
- lpicProp = icalcomponent_get_next_property(lpicEvent, ICAL_ANY_PROPERTY);
- }
-
- abNewPresent[2] = abNewPresent[3] = abNewPresent[4] = abNewPresent[5] = true;
-
- for (i = 0; i < sptaCopy.cValues; ++i) {
- if (abOldPresent[i] == true && abNewPresent[i] == false) {
- auto iProp = find(lpEx->lstMsgProps.begin(), lpEx->lstMsgProps.end(), sptaCopy.aulPropTag[i]);
- if (iProp != lpEx->lstMsgProps.cend()) {
- lpEx->lstMsgProps.erase(iProp);
- switch (i) {
- case 0:
-
- hr = lpIcalItem->lpRecurrence->setModifiedSubject(ulId, std::wstring());
- if (hr != hrSuccess)
- return hr;
- sPropVal.ulPropTag = PR_SUBJECT_W;
- sPropVal.Value.lpszW = const_cast<wchar_t *>(L"");
- lpEx->lstMsgProps.push_back(sPropVal);
- break;
- case 1:
-
- hr = lpIcalItem->lpRecurrence->setModifiedLocation(ulId, std::wstring());
- if (hr != hrSuccess)
- return hr;
- sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_LOCATION], PT_UNICODE);
- sPropVal.Value.lpszW = const_cast<wchar_t *>(L"");
- lpEx->lstMsgProps.push_back(sPropVal);
- break;
- case 2:
-
- break;
- case 3:
-
- break;
- case 4:
-
- break;
- case 5:
-
- break;
- case 6:
-
- if (bIsAllDay != bOldIsAllDay) {
-
- hr = lpIcalItem->lpRecurrence->setModifiedSubType(ulId, 1);
- if (hr != hrSuccess)
- return hr;
- sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_ALLDAYEVENT], PT_BOOLEAN);
- sPropVal.Value.ul = !bOldIsAllDay;
- lpEx->lstMsgProps.push_back(sPropVal);
- }
- break;
- case 7:
-
- hr = lpIcalItem->lpRecurrence->setModifiedBusyStatus(ulId, 1);
- if (hr != hrSuccess)
- return hr;
- sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_BUSYSTATUS], PT_LONG);
- sPropVal.Value.ul = 1;
- lpEx->lstMsgProps.push_back(sPropVal);
- break;
- };
- }
- }
- }
-
-
- lpicAlarm = icalcomponent_get_first_component(lpicEvent, ICAL_VALARM_COMPONENT);
- if (lpicAlarm) {
- hr = HrParseVAlarm(lpicAlarm, &ulRemindBefore, &ttReminderTime, &bReminderSet);
- if (hr == hrSuccess) {
-
- if (bReminderSet != abOldPresent[4]) {
- hr = lpIcalItem->lpRecurrence->setModifiedReminder(ulId, bReminderSet);
- if (hr != hrSuccess)
- return hr;
- sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_REMINDERSET], PT_BOOLEAN);
- sPropVal.Value.b = bReminderSet;
- lpEx->lstMsgProps.push_back(sPropVal);
- if (ttReminderTime == 0)
- ttReminderTime = ttStartLocalTime;
- sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_REMINDERTIME], PT_SYSTIME);
- UnixTimeToFileTime(ttReminderTime, &sPropVal.Value.ft);
- lpEx->lstMsgProps.push_back(sPropVal);
- }
- hr = lpIcalItem->lpRecurrence->setModifiedReminderDelta(ulId, ulRemindBefore);
- if (hr != hrSuccess)
- return hr;
- sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_REMINDERMINUTESBEFORESTART], PT_LONG);
- sPropVal.Value.ul = ulRemindBefore;
- lpEx->lstMsgProps.push_back(sPropVal);
- }
- } else if (abOldPresent[4]) {
-
- hr = lpIcalItem->lpRecurrence->setModifiedReminder(ulId, 0);
- if (hr != hrSuccess)
- return hr;
- sPropVal.ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_REMINDERSET], PT_BOOLEAN);
- sPropVal.Value.b = FALSE;
- lpEx->lstMsgProps.push_back(sPropVal);
- }
- return hr;
- }
- HRESULT ICalRecurrence::HrMakeMAPIRecurrence(recurrence *lpRecurrence, LPSPropTagArray lpNamedProps, LPMESSAGE lpMessage)
- {
- HRESULT hr = hrSuccess;
- memory_ptr<char> lpRecBlob;
- unsigned int ulRecBlob = 0;
- memory_ptr<SPropValue> lpPropVal, lpsPropRecPattern;
- std::string strHRS;
- ULONG i = 0;
- hr = lpRecurrence->HrGetRecurrenceState(&~lpRecBlob, &ulRecBlob);
- if (hr != hrSuccess)
- return hr;
- hr = lpRecurrence->HrGetHumanReadableString(&strHRS);
- if (hr != hrSuccess)
- return hr;
-
- hr = MAPIAllocateBuffer(sizeof(SPropValue) * 4, &~lpPropVal);
- if (hr != hrSuccess)
- return hr;
- lpPropVal[i].ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_RECURRING], PT_BOOLEAN);
- lpPropVal[i].Value.b = TRUE;
- ++i;
-
- lpPropVal[i].ulPropTag = PR_ICON_INDEX;
- lpPropVal[i].Value.ul = ICON_APPT_RECURRING;
- ++i;
- lpPropVal[i].ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_RECURRENCESTATE], PT_BINARY);
- lpPropVal[i].Value.bin.lpb = reinterpret_cast<BYTE *>(lpRecBlob.get());
- lpPropVal[i].Value.bin.cb = ulRecBlob;
- ++i;
- hr = HrGetOneProp(lpMessage, CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_RECURRENCEPATTERN], PT_STRING8), &~lpsPropRecPattern);
- if(hr != hrSuccess)
- {
- lpPropVal[i].ulPropTag = CHANGE_PROP_TYPE(lpNamedProps->aulPropTag[PROP_RECURRENCEPATTERN], PT_STRING8);
- lpPropVal[i].Value.lpszA = (char*)strHRS.c_str();
- ++i;
- }
- return lpMessage->SetProps(i, lpPropVal, NULL);
- }
- bool ICalRecurrence::HrValidateOccurrence(icalitem *lpItem, icalitem::exception lpEx)
- {
- HRESULT hr = hrSuccess;
- memory_ptr<OccrInfo> lpFBBlocksAll;
- ULONG cValues = 0;
- time_t tBaseDateStart = LocalToUTC(lpItem->lpRecurrence->StartOfDay(UTCToLocal(lpEx.tBaseDate, lpItem->tTZinfo)), lpItem->tTZinfo);
- time_t tStartDateStart = LocalToUTC(lpItem->lpRecurrence->StartOfDay(UTCToLocal(lpEx.tStartDate, lpItem->tTZinfo)), lpItem->tTZinfo);
- if (tBaseDateStart < tStartDateStart)
- hr = lpItem->lpRecurrence->HrGetItems(tBaseDateStart, tStartDateStart + 1439 * 60, lpItem->tTZinfo, lpItem->ulFbStatus, &~lpFBBlocksAll, &cValues);
- else
- hr = lpItem->lpRecurrence->HrGetItems(tStartDateStart, tBaseDateStart + 1439 * 60, lpItem->tTZinfo, lpItem->ulFbStatus, &~lpFBBlocksAll, &cValues);
- if (hr != hrSuccess)
- return false;
- return cValues == 1;
- }
- HRESULT ICalRecurrence::HrCreateICalRecurrence(TIMEZONE_STRUCT sTimeZone, bool bIsAllDay, recurrence *lpRecurrence, icalcomponent *lpicEvent)
- {
- icalrecurrencetype icRRule;
- std::list<time_t> lstExceptions;
- icaltimetype ittExDate;
- TIMEZONE_STRUCT sTZgmt = {0};
- HRESULT hr = HrCreateICalRecurrenceType(sTimeZone, bIsAllDay, lpRecurrence, &icRRule);
- if (hr != hrSuccess)
- return hr;
- icalcomponent_add_property(lpicEvent, icalproperty_new_rrule(icRRule));
-
- lstExceptions = lpRecurrence->getDeletedExceptions();
- if (!lstExceptions.empty()) {
-
- for (const auto &exc : lstExceptions) {
- if(bIsAllDay)
- ittExDate = icaltime_from_timet_with_zone(LocalToUTC(exc, sTZgmt), bIsAllDay, nullptr);
- else
- ittExDate = icaltime_from_timet_with_zone(LocalToUTC(exc, sTimeZone), 0, nullptr);
- ittExDate.is_utc = 1;
- icalcomponent_add_property(lpicEvent, icalproperty_new_exdate(ittExDate));
- }
- }
-
- return hrSuccess;
- }
- HRESULT ICalRecurrence::HrCreateICalRecurrenceType(TIMEZONE_STRUCT sTimeZone, bool bIsAllday, recurrence *lpRecurrence, icalrecurrencetype *lpicRRule)
- {
- struct icalrecurrencetype icRec;
- icalrecurrencetype_clear(&icRec);
- switch (lpRecurrence->getFrequency()) {
- case recurrence::DAILY:
- icRec.freq = ICAL_DAILY_RECURRENCE;
-
- if (lpRecurrence->getWeekDays()) {
-
-
- icRec.freq = ICAL_WEEKLY_RECURRENCE;
- WeekDaysToICalArray(lpRecurrence->getWeekDays(), &icRec);
- }
- break;
- case recurrence::WEEKLY:
- icRec.freq = ICAL_WEEKLY_RECURRENCE;
- WeekDaysToICalArray(lpRecurrence->getWeekDays(), &icRec);
- break;
- case recurrence::MONTHLY:
- icRec.freq = ICAL_MONTHLY_RECURRENCE;
- if (lpRecurrence->getWeekNumber() == 0) {
-
- icRec.by_month_day[0] = lpRecurrence->getDayOfMonth();
- icRec.by_month_day[1] = ICAL_RECURRENCE_ARRAY_MAX;
- break;
- }
-
-
- if (lpRecurrence->getWeekDays() == 127) {
-
-
- icRec.by_month_day[0] = lpRecurrence->getWeekNumber() == 5 ? -1 : lpRecurrence->getWeekNumber();
- icRec.by_month_day[1] = ICAL_RECURRENCE_ARRAY_MAX;
- break;
- } else if (lpRecurrence->getWeekDays() == 62 || lpRecurrence->getWeekDays() == 65) {
-
-
- icRec.by_set_pos[0] = lpRecurrence->getWeekNumber();
- icRec.by_set_pos[1] = ICAL_RECURRENCE_ARRAY_MAX;
- WeekDaysToICalArray(lpRecurrence->getWeekDays(), &icRec);
- } else if (lpRecurrence->getWeekNumber() == 5) {
- icRec.by_day[0] = (round(log((double)lpRecurrence->getWeekDays()) / log(2.0)) + 8 + 1) * -1;
- icRec.by_day[1] = ICAL_RECURRENCE_ARRAY_MAX;
- } else {
- icRec.by_day[0] = round(log((double)lpRecurrence->getWeekDays()) / log(2.0)) + (8 * lpRecurrence->getWeekNumber()) + 1;
- icRec.by_day[1] = ICAL_RECURRENCE_ARRAY_MAX;
- }
- break;
- case recurrence::YEARLY:
- icRec.freq = ICAL_YEARLY_RECURRENCE;
- if (lpRecurrence->getWeekNumber() == 0) {
-
- icRec.by_month_day[0] = lpRecurrence->getDayOfMonth();
- icRec.by_month_day[1] = ICAL_RECURRENCE_ARRAY_MAX;
- icRec.by_month[0] = lpRecurrence->getMonth();
- icRec.by_month[1] = ICAL_RECURRENCE_ARRAY_MAX;
- break;
- }
-
-
- if (lpRecurrence->getWeekNumber() == 5)
- icRec.by_day[0] = ((log((double)lpRecurrence->getWeekDays())/log(2.0)) + 8 + 1 ) * -1;
- else
- icRec.by_day[0] = (int)(log((double)lpRecurrence->getWeekDays())/log(2.0)) + (8 * lpRecurrence->getWeekNumber() ) +1;
- icRec.by_day[1] = ICAL_RECURRENCE_ARRAY_MAX;
- icRec.by_month[0] = lpRecurrence->getMonth();
- icRec.by_month[1] = ICAL_RECURRENCE_ARRAY_MAX;
- break;
- default:
- return MAPI_E_INVALID_PARAMETER;
- }
- icRec.interval = lpRecurrence->getInterval();
- switch (lpRecurrence->getEndType()) {
- case recurrence::DATE:
-
- icRec.count = 0;
-
- icRec.until = icaltime_from_timet_with_zone(lpRecurrence->getEndDate() + lpRecurrence->getStartTimeOffset(), bIsAllday, nullptr);
- icRec.until.is_utc = 0;
- break;
- case recurrence::NUMBER:
- icRec.count = lpRecurrence->getCount();
- icRec.until = icaltime_null_time();
- break;
- case recurrence::NEVER:
- icRec.count = 0;
- icRec.until = icaltime_null_time();
- break;
- };
- *lpicRRule = icRec;
- return hrSuccess;
- }
- HRESULT ICalRecurrence::WeekDaysToICalArray(ULONG ulWeekDays, struct icalrecurrencetype *lpRec)
- {
- int i = 0, j = 0;
- for (i = 0; i < 7; ++i)
- if ((ulWeekDays >> i) & 1)
- lpRec->by_day[j++] = i+1;
- lpRec->by_day[j] = ICAL_RECURRENCE_ARRAY_MAX;
- return hrSuccess;
- }
- HRESULT ICalRecurrence::HrMakeICalException(icalcomponent *lpicEvent, icalcomponent **lppicException)
- {
- HRESULT hr = hrSuccess;
- icalcomponent *lpicException = NULL;
- icalproperty *lpicProp = NULL;
-
- lpicException = icalcomponent_new_clone(lpicEvent);
-
- lpicProp = icalcomponent_get_first_property(lpicException, ICAL_DTSTART_PROPERTY);
- if (lpicProp) {
- icalcomponent_remove_property(lpicException, lpicProp);
- icalproperty_free(lpicProp);
- }
- lpicProp = icalcomponent_get_first_property(lpicException, ICAL_DTEND_PROPERTY);
- if (lpicProp) {
- icalcomponent_remove_property(lpicException, lpicProp);
- icalproperty_free(lpicProp);
- }
-
- lpicProp = icalcomponent_get_first_property(lpicException, ICAL_RRULE_PROPERTY);
- if (lpicProp) {
- icalcomponent_remove_property(lpicException, lpicProp);
- icalproperty_free(lpicProp);
- }
-
- while ((lpicProp = icalcomponent_get_first_property(lpicException, ICAL_EXDATE_PROPERTY)) != NULL) {
- icalcomponent_remove_property(lpicException, lpicProp);
- icalproperty_free(lpicProp);
- }
-
- lpicProp = icalcomponent_get_first_property(lpicException, ICAL_CLASS_PROPERTY);
- if (lpicProp) {
- icalcomponent_remove_property(lpicException, lpicProp);
- icalproperty_free(lpicProp);
- }
- *lppicException = lpicException;
- return hr;
- }
- }
|