ArchiverImpl.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. /* ArchiverImpl.cpp
  18. * Definition of class ArchiverImpl
  19. */
  20. #include <kopano/platform.h>
  21. #include <kopano/automapi.hpp>
  22. #include <kopano/memory.hpp>
  23. #include "ArchiverImpl.h"
  24. #include "ArchiveControlImpl.h"
  25. #include "ArchiveManageImpl.h"
  26. #include "ArchiveStateCollector.h"
  27. #include "ArchiveStateUpdater.h"
  28. #include "ArchiverSession.h"
  29. #include <kopano/ECConfig.h>
  30. namespace KC {
  31. ArchiverImpl::~ArchiverImpl()
  32. {
  33. if (m_lpLogger)
  34. m_lpLogger->Release();
  35. if (m_lpLogLogger)
  36. m_lpLogLogger->Release();
  37. delete m_lpsConfig;
  38. delete[] m_lpDefaults;
  39. }
  40. eResult ArchiverImpl::Init(const char *lpszAppName, const char *lpszConfig, const configsetting_t *lpExtraSettings, unsigned int ulFlags)
  41. {
  42. MAPIINIT_0 sMapiInit = {MAPI_INIT_VERSION, MAPI_MULTITHREAD_NOTIFICATIONS};
  43. if (lpExtraSettings == NULL)
  44. m_lpsConfig = ECConfig::Create(Archiver::GetConfigDefaults());
  45. else {
  46. m_lpDefaults = ConcatSettings(Archiver::GetConfigDefaults(), lpExtraSettings);
  47. m_lpsConfig = ECConfig::Create(m_lpDefaults);
  48. }
  49. if (!m_lpsConfig->LoadSettings(lpszConfig) && (ulFlags & RequireConfig))
  50. return FileNotFound;
  51. if (!m_lpsConfig->LoadSettings(lpszConfig)) {
  52. if ((ulFlags & RequireConfig))
  53. return FileNotFound;
  54. } else if (m_lpsConfig->HasErrors()) {
  55. if (!(ulFlags & InhibitErrorLogging)) {
  56. KCHL::object_ptr<ECLogger> lpLogger(new ECLogger_File(EC_LOGLEVEL_FATAL, 0, "-", false));
  57. ec_log_set(lpLogger);
  58. LogConfigErrors(m_lpsConfig);
  59. }
  60. return InvalidConfig;
  61. }
  62. m_lpLogLogger = CreateLogger(m_lpsConfig, (char*)lpszAppName, "");
  63. if (ulFlags & InhibitErrorLogging) {
  64. // We need to check if we're logging to stderr. If so we'll replace
  65. // the logger with a NULL logger.
  66. auto lpFileLogger = dynamic_cast<ECLogger_File *>(m_lpLogLogger);
  67. if (lpFileLogger && lpFileLogger->IsStdErr()) {
  68. m_lpLogLogger->Release();
  69. m_lpLogLogger = new ECLogger_Null();
  70. }
  71. if (m_lpLogger != NULL)
  72. m_lpLogger->Release();
  73. m_lpLogger = m_lpLogLogger;
  74. m_lpLogger->AddRef();
  75. } else if (ulFlags & AttachStdErr) {
  76. // We need to check if the current logger isn't logging to the console
  77. // as that would give duplicate messages
  78. auto lpFileLogger = dynamic_cast<ECLogger_File *>(m_lpLogLogger);
  79. if (lpFileLogger == NULL || !lpFileLogger->IsStdErr()) {
  80. auto lpTeeLogger = new ECLogger_Tee();
  81. lpTeeLogger->AddLogger(m_lpLogLogger);
  82. KCHL::object_ptr<ECLogger_File> lpConsoleLogger(new ECLogger_File(EC_LOGLEVEL_ERROR, 0, "-", false));
  83. lpTeeLogger->AddLogger(lpConsoleLogger);
  84. m_lpLogger = lpTeeLogger;
  85. } else {
  86. m_lpLogger = m_lpLogLogger;
  87. m_lpLogger->AddRef();
  88. }
  89. } else {
  90. m_lpLogger = m_lpLogLogger;
  91. m_lpLogger->AddRef();
  92. }
  93. ec_log_set(m_lpLogger);
  94. if (m_lpsConfig->HasWarnings())
  95. LogConfigErrors(m_lpsConfig);
  96. if (m_MAPI.Initialize(&sMapiInit) != hrSuccess)
  97. return Failure;
  98. if (ArchiverSession::Create(m_lpsConfig, m_lpLogger, &m_ptrSession) != hrSuccess)
  99. return Failure;
  100. return Success;
  101. }
  102. eResult ArchiverImpl::GetControl(ArchiveControlPtr *lpptrControl, bool bForceCleanup)
  103. {
  104. m_lpLogger->Log(EC_LOGLEVEL_DEBUG, "ArchiverImpl::GetControl() function entry");
  105. if (!m_MAPI.IsInitialized())
  106. return Uninitialized;
  107. m_lpLogger->Log(EC_LOGLEVEL_DEBUG, "ArchiverImpl::GetControl(): about to create an ArchiveControlImpl object");
  108. return MAPIErrorToArchiveError(ArchiveControlImpl::Create(m_ptrSession, m_lpsConfig, m_lpLogger, bForceCleanup, lpptrControl));
  109. }
  110. eResult ArchiverImpl::GetManage(const TCHAR *lpszUser, ArchiveManagePtr *lpptrManage)
  111. {
  112. if (!m_MAPI.IsInitialized())
  113. return Uninitialized;
  114. return MAPIErrorToArchiveError(ArchiveManageImpl::Create(m_ptrSession, m_lpsConfig, lpszUser, m_lpLogger, lpptrManage));
  115. }
  116. eResult ArchiverImpl::AutoAttach(unsigned int ulFlags)
  117. {
  118. HRESULT hr;
  119. m_lpLogger->Log(EC_LOGLEVEL_DEBUG, "ArchiverImpl::AutoAttach() function entry");
  120. ArchiveStateCollectorPtr ptrArchiveStateCollector;
  121. ArchiveStateUpdaterPtr ptrArchiveStateUpdater;
  122. if (ulFlags != ArchiveManage::Writable && ulFlags != ArchiveManage::ReadOnly && ulFlags != 0)
  123. return MAPIErrorToArchiveError(MAPI_E_INVALID_PARAMETER);
  124. m_lpLogger->Log(EC_LOGLEVEL_DEBUG, "ArchiverImpl::AutoAttach() about to create collector");
  125. hr = ArchiveStateCollector::Create(m_ptrSession, m_lpLogger, &ptrArchiveStateCollector);
  126. if (hr != hrSuccess)
  127. return MAPIErrorToArchiveError(hr);
  128. m_lpLogger->Log(EC_LOGLEVEL_DEBUG, "ArchiverImpl::AutoAttach() about to get state updater");
  129. hr = ptrArchiveStateCollector->GetArchiveStateUpdater(&ptrArchiveStateUpdater);
  130. if (hr != hrSuccess)
  131. return MAPIErrorToArchiveError(hr);
  132. if (ulFlags == 0) {
  133. if (parseBool(m_lpsConfig->GetSetting("auto_attach_writable")))
  134. ulFlags = ArchiveManage::Writable;
  135. else
  136. ulFlags = ArchiveManage::ReadOnly;
  137. }
  138. m_lpLogger->Log(EC_LOGLEVEL_DEBUG, "ArchiverImpl::AutoAttach() about to call update all");
  139. return MAPIErrorToArchiveError(ptrArchiveStateUpdater->UpdateAll(ulFlags));
  140. }
  141. ECLogger* ArchiverImpl::GetLogger(eLogType which) const
  142. {
  143. ECLogger* retval = NULL;
  144. switch (which) {
  145. case DefaultLog:
  146. retval = ec_log_get();
  147. break;
  148. case LogOnly:
  149. retval = m_lpLogLogger;
  150. }
  151. return retval;
  152. }
  153. configsetting_t* ArchiverImpl::ConcatSettings(const configsetting_t *lpSettings1, const configsetting_t *lpSettings2)
  154. {
  155. configsetting_t *lpMergedSettings = NULL;
  156. unsigned ulSettings = 0;
  157. unsigned ulIndex = 0;
  158. ulSettings = CountSettings(lpSettings1) + CountSettings(lpSettings2);
  159. lpMergedSettings = new configsetting_t[ulSettings + 1];
  160. while (lpSettings1->szName != NULL)
  161. lpMergedSettings[ulIndex++] = *lpSettings1++;
  162. while (lpSettings2->szName != NULL)
  163. lpMergedSettings[ulIndex++] = *lpSettings2++;
  164. memset(&lpMergedSettings[ulIndex], 0, sizeof(lpMergedSettings[ulIndex]));
  165. return lpMergedSettings;
  166. }
  167. unsigned ArchiverImpl::CountSettings(const configsetting_t *lpSettings)
  168. {
  169. unsigned ulSettings = 0;
  170. while ((lpSettings++)->szName != NULL)
  171. ++ulSettings;
  172. return ulSettings;
  173. }
  174. } /* namespace */