ECMessageStreamImporterIStreamAdapter.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 <new>
  19. #include "ECMessageStreamImporterIStreamAdapter.h"
  20. #include <kopano/ECInterfaceDefs.h>
  21. HRESULT ECMessageStreamImporterIStreamAdapter::Create(WSMessageStreamImporter *lpStreamImporter, IStream **lppStream)
  22. {
  23. if (lpStreamImporter == NULL || lppStream == NULL)
  24. return MAPI_E_INVALID_PARAMETER;
  25. ECMessageStreamImporterIStreamAdapterPtr ptrAdapter(new(std::nothrow) ECMessageStreamImporterIStreamAdapter(lpStreamImporter));
  26. if (ptrAdapter == nullptr)
  27. return MAPI_E_NOT_ENOUGH_MEMORY;
  28. return ptrAdapter->QueryInterface(IID_IStream, reinterpret_cast<LPVOID *>(lppStream));
  29. }
  30. HRESULT ECMessageStreamImporterIStreamAdapter::QueryInterface(REFIID refiid, void **lppInterface)
  31. {
  32. REGISTER_INTERFACE2(ECUnknown, this);
  33. REGISTER_INTERFACE2(ISequentialStream, &this->m_xSequentialStream);
  34. REGISTER_INTERFACE2(IStream, &this->m_xStream);
  35. return ECUnknown::QueryInterface(refiid, lppInterface);
  36. }
  37. // ISequentialStream
  38. HRESULT ECMessageStreamImporterIStreamAdapter::Read(void* /*pv*/, ULONG /*cb*/, ULONG* /*pcbRead*/)
  39. {
  40. return MAPI_E_NO_SUPPORT;
  41. }
  42. HRESULT ECMessageStreamImporterIStreamAdapter::Write(const void *pv, ULONG cb, ULONG *pcbWritten)
  43. {
  44. HRESULT hr;
  45. if (!m_ptrSink) {
  46. hr = m_ptrStreamImporter->StartTransfer(&~m_ptrSink);
  47. if (hr != hrSuccess)
  48. return hr;
  49. }
  50. hr = m_ptrSink->Write((LPVOID)pv, cb);
  51. if (hr != hrSuccess)
  52. return hr;
  53. if (pcbWritten)
  54. *pcbWritten = cb;
  55. return hrSuccess;
  56. }
  57. // IStream
  58. HRESULT ECMessageStreamImporterIStreamAdapter::Seek(LARGE_INTEGER /*dlibMove*/, DWORD /*dwOrigin*/, ULARGE_INTEGER* /*plibNewPosition*/)
  59. {
  60. return MAPI_E_NO_SUPPORT;
  61. }
  62. HRESULT ECMessageStreamImporterIStreamAdapter::SetSize(ULARGE_INTEGER /*libNewSize*/)
  63. {
  64. return MAPI_E_NO_SUPPORT;
  65. }
  66. HRESULT ECMessageStreamImporterIStreamAdapter::CopyTo(IStream* /*pstm*/, ULARGE_INTEGER /*cb*/, ULARGE_INTEGER* /*pcbRead*/, ULARGE_INTEGER* /*pcbWritten*/)
  67. {
  68. return MAPI_E_NO_SUPPORT;
  69. }
  70. HRESULT ECMessageStreamImporterIStreamAdapter::Commit(DWORD /*grfCommitFlags*/)
  71. {
  72. HRESULT hr;
  73. HRESULT hrAsync = hrSuccess;
  74. if (m_ptrSink == NULL)
  75. return MAPI_E_UNCONFIGURED;
  76. m_ptrSink.reset();
  77. hr = m_ptrStreamImporter->GetAsyncResult(&hrAsync);
  78. if (hr == hrSuccess)
  79. hr = hrAsync;
  80. return hr;
  81. }
  82. HRESULT ECMessageStreamImporterIStreamAdapter::Revert(void)
  83. {
  84. return MAPI_E_NO_SUPPORT;
  85. }
  86. HRESULT ECMessageStreamImporterIStreamAdapter::LockRegion(ULARGE_INTEGER /*libOffset*/, ULARGE_INTEGER /*cb*/, DWORD /*dwLockType*/)
  87. {
  88. return MAPI_E_NO_SUPPORT;
  89. }
  90. HRESULT ECMessageStreamImporterIStreamAdapter::UnlockRegion(ULARGE_INTEGER /*libOffset*/, ULARGE_INTEGER /*cb*/, DWORD /*dwLockType*/)
  91. {
  92. return MAPI_E_NO_SUPPORT;
  93. }
  94. HRESULT ECMessageStreamImporterIStreamAdapter::Stat(STATSTG* /*pstatstg*/, DWORD /*grfStatFlag*/)
  95. {
  96. return MAPI_E_NO_SUPPORT;
  97. }
  98. HRESULT ECMessageStreamImporterIStreamAdapter::Clone(IStream** /*ppstm*/)
  99. {
  100. return MAPI_E_NO_SUPPORT;
  101. }
  102. ECMessageStreamImporterIStreamAdapter::ECMessageStreamImporterIStreamAdapter(WSMessageStreamImporter *lpStreamImporter)
  103. : m_ptrStreamImporter(lpStreamImporter, true)
  104. { }
  105. ECMessageStreamImporterIStreamAdapter::~ECMessageStreamImporterIStreamAdapter()
  106. {
  107. Commit(0); // This causes us to wait for the async thread.
  108. }
  109. // ISequentialStream proxies
  110. DEF_ULONGMETHOD1(TRACE_MAPI, ECMessageStreamImporterIStreamAdapter, SequentialStream, AddRef, (void))
  111. DEF_ULONGMETHOD1(TRACE_MAPI, ECMessageStreamImporterIStreamAdapter, SequentialStream, Release, (void))
  112. DEF_HRMETHOD(TRACE_MAPI, ECMessageStreamImporterIStreamAdapter, SequentialStream, QueryInterface, (REFIID, refiid), (void **, lppInterface))
  113. DEF_HRMETHOD(TRACE_MAPI, ECMessageStreamImporterIStreamAdapter, SequentialStream, Read, (void *, pv), (ULONG, cb), (ULONG *, pcbRead))
  114. DEF_HRMETHOD(TRACE_MAPI, ECMessageStreamImporterIStreamAdapter, SequentialStream, Write, (const void *, pv), (ULONG, cb), (ULONG *, pcbWritten))
  115. // IStream proxies
  116. DEF_ULONGMETHOD1(TRACE_MAPI, ECMessageStreamImporterIStreamAdapter, Stream, AddRef, (void))
  117. DEF_ULONGMETHOD1(TRACE_MAPI, ECMessageStreamImporterIStreamAdapter, Stream, Release, (void))
  118. DEF_HRMETHOD(TRACE_MAPI, ECMessageStreamImporterIStreamAdapter, Stream, QueryInterface, (REFIID, refiid), (void **, lppInterface))
  119. DEF_HRMETHOD(TRACE_MAPI, ECMessageStreamImporterIStreamAdapter, Stream, Read, (void *, pv), (ULONG, cb), (ULONG *, pcbRead))
  120. DEF_HRMETHOD(TRACE_MAPI, ECMessageStreamImporterIStreamAdapter, Stream, Write, (const void *, pv), (ULONG, cb), (ULONG *, pcbWritten))
  121. DEF_HRMETHOD(TRACE_MAPI, ECMessageStreamImporterIStreamAdapter, Stream, Seek, (LARGE_INTEGER, dlibMove), (DWORD, dwOrigin), (ULARGE_INTEGER *, plibNewPosition))
  122. DEF_HRMETHOD(TRACE_MAPI, ECMessageStreamImporterIStreamAdapter, Stream, SetSize, (ULARGE_INTEGER, libNewSize))
  123. DEF_HRMETHOD(TRACE_MAPI, ECMessageStreamImporterIStreamAdapter, Stream, CopyTo, (IStream *, pstm), (ULARGE_INTEGER, cb), (ULARGE_INTEGER *, pcbRead), (ULARGE_INTEGER *, pcbWritten))
  124. DEF_HRMETHOD(TRACE_MAPI, ECMessageStreamImporterIStreamAdapter, Stream, Commit, (DWORD, grfCommitFlags))
  125. DEF_HRMETHOD(TRACE_MAPI, ECMessageStreamImporterIStreamAdapter, Stream, Revert, (void))
  126. DEF_HRMETHOD(TRACE_MAPI, ECMessageStreamImporterIStreamAdapter, Stream, LockRegion, (ULARGE_INTEGER, libOffset), (ULARGE_INTEGER, cb), (DWORD, dwLockType))
  127. DEF_HRMETHOD(TRACE_MAPI, ECMessageStreamImporterIStreamAdapter, Stream, UnlockRegion, (ULARGE_INTEGER, libOffset), (ULARGE_INTEGER, cb), (DWORD, dwLockType))
  128. DEF_HRMETHOD(TRACE_MAPI, ECMessageStreamImporterIStreamAdapter, Stream, Stat, (STATSTG *, pstatstg), (DWORD, grfStatFlag))
  129. DEF_HRMETHOD(TRACE_MAPI, ECMessageStreamImporterIStreamAdapter, Stream, Clone, (IStream **, ppstm))