ECChangeAdvisor.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #ifndef ECCHANGEADVISOR_H
  18. #define ECCHANGEADVISOR_H
  19. #include <kopano/zcdefs.h>
  20. #include <mutex>
  21. #include <mapidefs.h>
  22. #include <mapispi.h>
  23. #include <kopano/ECUnknown.h>
  24. #include <IECChangeAdvisor.h>
  25. #include "ics_client.hpp"
  26. #include <kopano/kcodes.h>
  27. #include <map>
  28. namespace KC {
  29. class ECLogger;
  30. }
  31. class ECMsgStore;
  32. /**
  33. * ECChangeAdvisor: Implementation IECChangeAdvisor, which allows one to register for
  34. * change notifications on folders.
  35. */
  36. class ECChangeAdvisor _kc_final : public ECUnknown {
  37. protected:
  38. /**
  39. * Construct the ChangeAdvisor.
  40. *
  41. * @param[in] lpMsgStore
  42. * The message store that contains the folder to be registered for
  43. * change notifications.
  44. */
  45. ECChangeAdvisor(ECMsgStore *lpMsgStore);
  46. /**
  47. * Destructor.
  48. */
  49. virtual ~ECChangeAdvisor();
  50. public:
  51. /**
  52. * Construct the ChangeAdvisor.
  53. *
  54. * @param[in] lpMsgStore
  55. * The message store that contains the folder to be registered for
  56. * change notifications.
  57. * @param[out] lppChangeAdvisor
  58. * The new change advisor.
  59. */
  60. static HRESULT Create(ECMsgStore *lpMsgStore, ECChangeAdvisor **lppChangeAdvisor);
  61. /**
  62. * Get an alternate interface to the same object.
  63. *
  64. * @param[in] refiid
  65. * A reference to the IID of the requested interface.
  66. * Supported interfaces:
  67. * - IID_ECChangeAdvisor
  68. * - IID_IECChangeAdvisor
  69. * @param[out] lpvoid
  70. * Pointer to a pointer of the requested type, casted to a void pointer.
  71. */
  72. virtual HRESULT QueryInterface(REFIID refiid, void **lpvoid) _kc_override;
  73. // IECChangeAdvisor
  74. virtual HRESULT GetLastError(HRESULT hResult, ULONG ulFlags, LPMAPIERROR *lppMAPIError);
  75. virtual HRESULT Config(LPSTREAM lpStream, LPGUID lpGUID, IECChangeAdviseSink *lpAdviseSink, ULONG ulFlags);
  76. virtual HRESULT UpdateState(LPSTREAM lpStream);
  77. virtual HRESULT AddKeys(LPENTRYLIST lpEntryList);
  78. virtual HRESULT RemoveKeys(LPENTRYLIST lpEntryList);
  79. virtual HRESULT IsMonitoringSyncId(syncid_t ulSyncId);
  80. virtual HRESULT UpdateSyncState(syncid_t ulSyncId, changeid_t ulChangeId);
  81. private:
  82. typedef std::map<syncid_t, connection_t> ConnectionMap;
  83. typedef std::map<syncid_t, changeid_t> SyncStateMap;
  84. /**
  85. * Get the sync id from a ConnectionMap entry.
  86. *
  87. * @param[in] sConnection
  88. * The ConnectionMap entry from which to extract the sync id.
  89. * @return The sync id extracted from the the ConnectionMap entry.
  90. */
  91. static ULONG GetSyncId(const ConnectionMap::value_type &sConnection);
  92. /**
  93. * Create a SyncStateMap entry from an SSyncState structure.
  94. *
  95. * @param[in] sSyncState
  96. * The SSyncState structure to be converted to a SyncStateMap entry.
  97. * @return A new SyncStateMap entry.
  98. */
  99. static SyncStateMap::value_type ConvertSyncState(const SSyncState &sSyncState);
  100. static SSyncState ConvertSyncStateMapEntry(const SyncStateMap::value_type &sMapEntry);
  101. /**
  102. * Compare the sync ids from a ConnectionMap entry and a SyncStateMap entry.
  103. *
  104. * @param[in] sConnection
  105. * The ConnectionMap entry to compare the sync id with.
  106. * @param[in] sSyncState
  107. * The SyncStateMap entry to compare the sync id with.
  108. * @return true if the sync ids are equal, false otherwise.
  109. */
  110. static bool CompareSyncId(const ConnectionMap::value_type &sConnection, const SyncStateMap::value_type &sSyncState);
  111. /**
  112. * Reload the change notifications.
  113. *
  114. * @param[in] lpParam
  115. * The parameter passed to AddSessionReloadCallback.
  116. * @param[in] newSessionId
  117. * The sessionid of the new session.
  118. */
  119. static HRESULT Reload(void *lpParam, ECSESSIONID newSessionId);
  120. /**
  121. * Purge all unused connections from advisor.
  122. */
  123. HRESULT PurgeStates();
  124. class xECChangeAdvisor _kc_final : public IECChangeAdvisor {
  125. #include <kopano/xclsfrag/IUnknown.hpp>
  126. // <kopano/xclsfrag/IECChangeAdvisor.hpp>
  127. virtual HRESULT __stdcall GetLastError(HRESULT hResult, ULONG flags, LPMAPIERROR *lppMAPIError) _kc_override;
  128. virtual HRESULT __stdcall Config(LPSTREAM lpStream, LPGUID lpGUID, IECChangeAdviseSink *lpAdviseSink, ULONG flags) _kc_override;
  129. virtual HRESULT __stdcall UpdateState(LPSTREAM lpStream) _kc_override;
  130. virtual HRESULT __stdcall AddKeys(LPENTRYLIST lpEntryList) _kc_override;
  131. virtual HRESULT __stdcall RemoveKeys(LPENTRYLIST lpEntryList) _kc_override;
  132. virtual HRESULT __stdcall IsMonitoringSyncId(ULONG ulSyncId) _kc_override;
  133. virtual HRESULT __stdcall UpdateSyncState(ULONG ulSyncId, ULONG ulChangeId) _kc_override;
  134. } m_xECChangeAdvisor;
  135. ECMsgStore *m_lpMsgStore;
  136. IECChangeAdviseSink *m_lpChangeAdviseSink = nullptr;
  137. ULONG m_ulFlags = 0, m_ulReloadId = 0;
  138. std::recursive_mutex m_hConnectionLock;
  139. ConnectionMap m_mapConnections;
  140. SyncStateMap m_mapSyncStates;
  141. ECLogger *m_lpLogger;
  142. };
  143. #endif // ndef ECCHANGEADVISOR_H