postsaveiidupdater.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 <kopano/Util.h>
  19. #include "postsaveiidupdater.h"
  20. #include "instanceidmapper.h"
  21. namespace KC { namespace operations {
  22. TaskBase::TaskBase(const AttachPtr &ptrSourceAttach, const MessagePtr &ptrDestMsg, ULONG ulDestAttachIdx)
  23. : m_ptrSourceAttach(ptrSourceAttach)
  24. , m_ptrDestMsg(ptrDestMsg)
  25. , m_ulDestAttachIdx(ulDestAttachIdx)
  26. { }
  27. HRESULT TaskBase::Execute(ULONG ulPropTag, const InstanceIdMapperPtr &ptrMapper) {
  28. HRESULT hr;
  29. SPropValuePtr ptrSourceServerUID;
  30. ULONG cbSourceInstanceID = 0;
  31. EntryIdPtr ptrSourceInstanceID;
  32. MAPITablePtr ptrTable;
  33. SRowSetPtr ptrRows;
  34. AttachPtr ptrAttach;
  35. SPropValuePtr ptrDestServerUID;
  36. ULONG cbDestInstanceID = 0;
  37. EntryIdPtr ptrDestInstanceID;
  38. static constexpr const SizedSPropTagArray(1, sptaTableProps) = {1, {PR_ATTACH_NUM}};
  39. hr = GetUniqueIDs(m_ptrSourceAttach, &~ptrSourceServerUID, &cbSourceInstanceID, &~ptrSourceInstanceID);
  40. if (hr != hrSuccess)
  41. return hr;
  42. hr = m_ptrDestMsg->GetAttachmentTable(MAPI_DEFERRED_ERRORS, &~ptrTable);
  43. if (hr != hrSuccess)
  44. return hr;
  45. hr = ptrTable->SetColumns(sptaTableProps, TBL_BATCH);
  46. if (hr != hrSuccess)
  47. return hr;
  48. hr = ptrTable->SeekRow(BOOKMARK_BEGINNING, m_ulDestAttachIdx, NULL);
  49. if (hr != hrSuccess)
  50. return hr;
  51. hr = ptrTable->QueryRows(1, 0, &ptrRows);
  52. if (hr != hrSuccess)
  53. return hr;
  54. if (ptrRows.empty())
  55. return MAPI_E_NOT_FOUND;
  56. hr = m_ptrDestMsg->OpenAttach(ptrRows[0].lpProps[0].Value.ul, &ptrAttach.iid(), 0, &~ptrAttach);
  57. if (hr != hrSuccess)
  58. return hr;
  59. hr = GetUniqueIDs(ptrAttach, &~ptrDestServerUID, &cbDestInstanceID, &~ptrDestInstanceID);
  60. if (hr != hrSuccess)
  61. return hr;
  62. return DoExecute(ulPropTag, ptrMapper, ptrSourceServerUID->Value.bin,
  63. cbSourceInstanceID, ptrSourceInstanceID,
  64. ptrDestServerUID->Value.bin, cbDestInstanceID,
  65. ptrDestInstanceID);
  66. }
  67. HRESULT TaskBase::GetUniqueIDs(IAttach *lpAttach, LPSPropValue *lppServerUID, ULONG *lpcbInstanceID, LPENTRYID *lppInstanceID)
  68. {
  69. HRESULT hr;
  70. SPropValuePtr ptrServerUID;
  71. ECSingleInstancePtr ptrInstance;
  72. ULONG cbInstanceID = 0;
  73. EntryIdPtr ptrInstanceID;
  74. hr = HrGetOneProp(lpAttach, PR_EC_SERVER_UID, &~ptrServerUID);
  75. if (hr != hrSuccess)
  76. return hr;
  77. hr = lpAttach->QueryInterface(ptrInstance.iid(), &~ptrInstance);
  78. if (hr != hrSuccess)
  79. return hr;
  80. hr = ptrInstance->GetSingleInstanceId(&cbInstanceID, &~ptrInstanceID);
  81. if (hr != hrSuccess)
  82. return hr;
  83. *lppServerUID = ptrServerUID.release();
  84. *lpcbInstanceID = cbInstanceID;
  85. *lppInstanceID = ptrInstanceID.release();
  86. return hrSuccess;
  87. }
  88. TaskMapInstanceId::TaskMapInstanceId(const AttachPtr &ptrSourceAttach, const MessagePtr &ptrDestMsg, ULONG ulDestAttachNum)
  89. : TaskBase(ptrSourceAttach, ptrDestMsg, ulDestAttachNum)
  90. { }
  91. HRESULT TaskMapInstanceId::DoExecute(ULONG ulPropTag, const InstanceIdMapperPtr &ptrMapper, const SBinary &sourceServerUID, ULONG cbSourceInstanceID, LPENTRYID lpSourceInstanceID, const SBinary &destServerUID, ULONG cbDestInstanceID, LPENTRYID lpDestInstanceID) {
  92. return ptrMapper->SetMappedInstances(ulPropTag, sourceServerUID, cbSourceInstanceID, lpSourceInstanceID, destServerUID, cbDestInstanceID, lpDestInstanceID);
  93. }
  94. TaskVerifyAndUpdateInstanceId::TaskVerifyAndUpdateInstanceId(const AttachPtr &ptrSourceAttach, const MessagePtr &ptrDestMsg, ULONG ulDestAttachNum, ULONG cbDestInstanceID, LPENTRYID lpDestInstanceID)
  95. : TaskBase(ptrSourceAttach, ptrDestMsg, ulDestAttachNum)
  96. , m_destInstanceID(cbDestInstanceID, lpDestInstanceID)
  97. { }
  98. HRESULT TaskVerifyAndUpdateInstanceId::DoExecute(ULONG ulPropTag, const InstanceIdMapperPtr &ptrMapper, const SBinary &sourceServerUID, ULONG cbSourceInstanceID, LPENTRYID lpSourceInstanceID, const SBinary &destServerUID, ULONG cbDestInstanceID, LPENTRYID lpDestInstanceID) {
  99. HRESULT hr = hrSuccess;
  100. SBinary lhs, rhs;
  101. lhs.cb = cbDestInstanceID;
  102. lhs.lpb = (LPBYTE)lpDestInstanceID;
  103. rhs.cb = m_destInstanceID.size();
  104. rhs.lpb = m_destInstanceID;
  105. if (Util::CompareSBinary(lhs, rhs) != 0)
  106. hr = ptrMapper->SetMappedInstances(ulPropTag, sourceServerUID, cbSourceInstanceID, lpSourceInstanceID, destServerUID, cbDestInstanceID, lpDestInstanceID);
  107. return hr;
  108. }
  109. PostSaveInstanceIdUpdater::PostSaveInstanceIdUpdater(ULONG ulPropTag, const InstanceIdMapperPtr &ptrMapper, const TaskList &lstDeferred)
  110. : m_ulPropTag(ulPropTag)
  111. , m_ptrMapper(ptrMapper)
  112. , m_lstDeferred(lstDeferred)
  113. { }
  114. HRESULT PostSaveInstanceIdUpdater::Execute()
  115. {
  116. HRESULT hr = hrSuccess;
  117. bool bFailure = false;
  118. for (const auto &i : m_lstDeferred) {
  119. hr = i->Execute(m_ulPropTag, m_ptrMapper);
  120. if (hr != hrSuccess)
  121. bFailure = true;
  122. }
  123. return bFailure ? MAPI_W_ERRORS_RETURNED : hrSuccess;
  124. }
  125. }} /* namespace */