ECFreeBusyUpdate.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 <new>
  18. #include <kopano/platform.h>
  19. #include <kopano/memory.hpp>
  20. #include <kopano/ECInterfaceDefs.h>
  21. #include "ECFreeBusyUpdate.h"
  22. #include "freebusytags.h"
  23. #include "freebusyutil.h"
  24. using namespace KCHL;
  25. namespace KC {
  26. ECFreeBusyUpdate::ECFreeBusyUpdate(IMessage *lpMessage) :
  27. m_lpMessage(lpMessage)
  28. {
  29. if(m_lpMessage)
  30. m_lpMessage->AddRef();
  31. }
  32. ECFreeBusyUpdate::~ECFreeBusyUpdate(void)
  33. {
  34. if(m_lpMessage)
  35. m_lpMessage->Release();
  36. }
  37. HRESULT ECFreeBusyUpdate::Create(IMessage* lpMessage, ECFreeBusyUpdate **lppECFreeBusyUpdate)
  38. {
  39. HRESULT hr = hrSuccess;
  40. auto lpECFreeBusyUpdate = new(std::nothrow) ECFreeBusyUpdate(lpMessage);
  41. if (lpECFreeBusyUpdate == nullptr)
  42. return MAPI_E_NOT_ENOUGH_MEMORY;
  43. hr = lpECFreeBusyUpdate->QueryInterface(IID_ECFreeBusyUpdate, (void **)lppECFreeBusyUpdate);
  44. if(hr != hrSuccess)
  45. delete lpECFreeBusyUpdate;
  46. return hr;
  47. }
  48. HRESULT ECFreeBusyUpdate::QueryInterface(REFIID refiid, void** lppInterface)
  49. {
  50. REGISTER_INTERFACE2(ECFreeBusyUpdate, this);
  51. REGISTER_INTERFACE2(ECUnknown, this);
  52. REGISTER_INTERFACE2(IFreeBusyUpdate, &this->m_xFreeBusyUpdate);
  53. REGISTER_INTERFACE2(IUnknown, &this->m_xFreeBusyUpdate);
  54. return MAPI_E_INTERFACE_NOT_SUPPORTED;
  55. }
  56. HRESULT ECFreeBusyUpdate::PublishFreeBusy(FBBlock_1 *lpBlocks, ULONG nBlocks)
  57. {
  58. if(nBlocks > 0 && lpBlocks == NULL)
  59. return MAPI_E_INVALID_PARAMETER;
  60. for (ULONG i = 0; i < nBlocks; ++i)
  61. m_fbBlockList.Add(&lpBlocks[i]);
  62. return S_OK;
  63. }
  64. HRESULT ECFreeBusyUpdate::ResetPublishedFreeBusy()
  65. {
  66. m_fbBlockList.Clear();
  67. return S_OK;
  68. }
  69. HRESULT ECFreeBusyUpdate::SaveChanges(FILETIME ftStart, FILETIME ftEnd)
  70. {
  71. HRESULT hr = hrSuccess;
  72. ULONG cValues = 0;
  73. ULONG cProps = 0;
  74. ULONG ulMonths;
  75. memory_ptr<SPropValue> lpPropArray, lpPropFBDataArray;
  76. LONG rtmStart = 0;
  77. LONG rtmEnd = 0;
  78. FILETIME ft;
  79. time_t tmUnixStart;
  80. time_t tmUnixEnd;
  81. struct tm tmStart;
  82. struct tm tmEnd;
  83. static constexpr const SizedSPropTagArray(8, sPropsFBDelete) = {
  84. 8,
  85. {
  86. PR_FREEBUSY_ALL_EVENTS,
  87. PR_FREEBUSY_ALL_MONTHS,
  88. PR_FREEBUSY_BUSY_EVENTS,
  89. PR_FREEBUSY_BUSY_MONTHS,
  90. PR_FREEBUSY_OOF_EVENTS,
  91. PR_FREEBUSY_OOF_MONTHS,
  92. PR_FREEBUSY_TENTATIVE_EVENTS,
  93. PR_FREEBUSY_TENTATIVE_MONTHS
  94. }
  95. };
  96. FileTimeToRTime(&ftStart, &rtmStart);
  97. FileTimeToRTime(&ftEnd, &rtmEnd);
  98. if(m_lpMessage == NULL)
  99. {
  100. hr = MAPI_E_INVALID_OBJECT;
  101. goto exit;
  102. }
  103. if((ULONG)rtmStart > (ULONG)rtmEnd)
  104. {
  105. hr = MAPI_E_BAD_VALUE;
  106. goto exit;
  107. }
  108. GetSystemTimeAsFileTime(&ft);
  109. // Restrict on start and end date
  110. m_fbBlockList.Restrict(rtmStart, rtmEnd);
  111. //Calculate months
  112. RTimeToUnixTime(rtmStart, &tmUnixStart);
  113. RTimeToUnixTime(rtmEnd, &tmUnixEnd);
  114. gmtime_safe(&tmUnixStart, &tmStart);
  115. gmtime_safe(&tmUnixEnd, &tmEnd);
  116. ulMonths = DiffYearMonthToMonth(&tmStart, &tmEnd);
  117. if(ulMonths == 0)
  118. ++ulMonths;
  119. cValues = 9;
  120. cProps = 0;
  121. hr = MAPIAllocateBuffer(sizeof(SPropValue) * cValues, &~lpPropArray);
  122. if (hr != hrSuccess)
  123. goto exit;
  124. lpPropArray[cProps].ulPropTag = PR_FREEBUSY_LAST_MODIFIED;
  125. lpPropArray[cProps++].Value.ft = ft;
  126. lpPropArray[cProps].ulPropTag = PR_FREEBUSY_START_RANGE;
  127. lpPropArray[cProps++].Value.l = rtmStart;
  128. lpPropArray[cProps].ulPropTag = PR_FREEBUSY_END_RANGE;
  129. lpPropArray[cProps++].Value.l = rtmEnd;
  130. lpPropArray[cProps].ulPropTag = PR_FREEBUSY_NUM_MONTHS;
  131. lpPropArray[cProps++].Value.l = ulMonths;
  132. hr = m_lpMessage->SetProps(cProps, lpPropArray, NULL);
  133. if(hr != hrSuccess)
  134. goto exit;
  135. // Delete all free/busy data properties
  136. hr = m_lpMessage->DeleteProps(sPropsFBDelete, NULL);
  137. if(hr != hrSuccess)
  138. goto exit;
  139. if (CreateFBProp(fbKopanoAllBusy, ulMonths, PR_FREEBUSY_ALL_MONTHS,
  140. PR_FREEBUSY_ALL_EVENTS, &m_fbBlockList, &~lpPropFBDataArray) == hrSuccess) {
  141. hr = m_lpMessage->SetProps(2, lpPropFBDataArray, NULL);
  142. if(hr != hrSuccess)
  143. goto exit;
  144. }
  145. if (CreateFBProp(fbBusy, ulMonths, PR_FREEBUSY_BUSY_MONTHS,
  146. PR_FREEBUSY_BUSY_EVENTS, &m_fbBlockList, &~lpPropFBDataArray) == hrSuccess) {
  147. hr = m_lpMessage->SetProps(2, lpPropFBDataArray, NULL);
  148. if(hr != hrSuccess)
  149. goto exit;
  150. }
  151. if (CreateFBProp(fbTentative, ulMonths, PR_FREEBUSY_TENTATIVE_MONTHS,
  152. PR_FREEBUSY_TENTATIVE_EVENTS, &m_fbBlockList, &~lpPropFBDataArray) == hrSuccess) {
  153. hr = m_lpMessage->SetProps(2, lpPropFBDataArray, NULL);
  154. if(hr != hrSuccess)
  155. goto exit;
  156. }
  157. if (CreateFBProp(fbOutOfOffice, ulMonths, PR_FREEBUSY_OOF_MONTHS,
  158. PR_FREEBUSY_OOF_EVENTS, &m_fbBlockList, &~lpPropFBDataArray) == hrSuccess) {
  159. hr = m_lpMessage->SetProps(2, lpPropFBDataArray, NULL);
  160. if(hr != hrSuccess)
  161. goto exit;
  162. }
  163. hr = m_lpMessage->SaveChanges(KEEP_OPEN_READWRITE);
  164. if(hr != hrSuccess)
  165. goto exit;
  166. exit:
  167. m_fbBlockList.Reset();
  168. return hr;
  169. }
  170. DEF_HRMETHOD1(TRACE_MAPI, ECFreeBusyUpdate, FreeBusyUpdate, QueryInterface, (REFIID, refiid), (void**, lppInterface))
  171. DEF_ULONGMETHOD1(TRACE_MAPI, ECFreeBusyUpdate, FreeBusyUpdate, AddRef, (void))
  172. DEF_ULONGMETHOD1(TRACE_MAPI, ECFreeBusyUpdate, FreeBusyUpdate, Release, (void))
  173. DEF_HRMETHOD1(TRACE_MAPI, ECFreeBusyUpdate, FreeBusyUpdate, Reload, (void))
  174. DEF_HRMETHOD1(TRACE_MAPI, ECFreeBusyUpdate, FreeBusyUpdate, PublishFreeBusy, (FBBlock_1 *, lpBlocks), (ULONG, nBlocks))
  175. DEF_HRMETHOD1(TRACE_MAPI, ECFreeBusyUpdate, FreeBusyUpdate, RemoveAppt, (void))
  176. DEF_HRMETHOD1(TRACE_MAPI, ECFreeBusyUpdate, FreeBusyUpdate, ResetPublishedFreeBusy, (void))
  177. DEF_HRMETHOD1(TRACE_MAPI, ECFreeBusyUpdate, FreeBusyUpdate, ChangeAppt, (void))
  178. DEF_HRMETHOD1(TRACE_MAPI, ECFreeBusyUpdate, FreeBusyUpdate, SaveChanges, (FILETIME, ftBegin), (FILETIME, ftEnd))
  179. DEF_HRMETHOD1(TRACE_MAPI, ECFreeBusyUpdate, FreeBusyUpdate, GetFBTimes, (void))
  180. DEF_HRMETHOD1(TRACE_MAPI, ECFreeBusyUpdate, FreeBusyUpdate, Intersect, (void))
  181. } /* namespace */