Терминальный проект КиберПлат [open source]
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
7.7KB

  1. /* @file Сервис для работы с настройками. */
  2. // Qt
  3. #include <Common/QtHeadersBegin.h>
  4. #include <QtCore/QDir>
  5. #include <Common/QtHeadersEnd.h>
  6. #include <Common/Version.h>
  7. // SDK
  8. #include <SDK/PaymentProcessor/Settings/TerminalSettings.h>
  9. #include <SDK/PaymentProcessor/Settings/DealerSettings.h>
  10. #include <SDK/PaymentProcessor/Settings/Directory.h>
  11. #include <SDK/PaymentProcessor/Settings/UserSettings.h>
  12. #include <SDK/PaymentProcessor/Settings/ExtensionsSettings.h>
  13. // Modules
  14. #include <SettingsManager/SettingsManager.h>
  15. // Project
  16. #include "System/IApplication.h"
  17. #include "System/SettingsConstants.h"
  18. #include "Services/ServiceNames.h"
  19. #include "Services/SettingsService.h"
  20. #include "Services/EventService.h"
  21. namespace PPSDK = SDK::PaymentProcessor;
  22. namespace AdapterNames = PPSDK::CAdapterNames;
  23. //---------------------------------------------------------------------------
  24. SettingsService * SettingsService::instance(IApplication * aApplication)
  25. {
  26. return static_cast<SettingsService *>(aApplication->getCore()->getService(CServices::SettingsService));
  27. }
  28. //---------------------------------------------------------------------------
  29. SettingsService::SettingsService(IApplication * aApplication)
  30. : mApplication(aApplication),
  31. mSettingsManager(nullptr),
  32. mRestoreConfiguration(false)
  33. {
  34. }
  35. //---------------------------------------------------------------------------
  36. SettingsService::~SettingsService()
  37. {
  38. }
  39. //---------------------------------------------------------------------------
  40. bool SettingsService::initialize()
  41. {
  42. mSettingsManager = new SettingsManager(mApplication->getUserDataPath());
  43. mSettingsManager->setLog(mApplication->getLog());
  44. // Регистрируем все конфиги.
  45. QList<SSettingsSource> settingsSources;
  46. settingsSources
  47. << SSettingsSource(mApplication->getWorkingDirectory() + "/data/system.ini", AdapterNames::TerminalAdapter, true)
  48. << SSettingsSource("terminal.ini", AdapterNames::TerminalAdapter, false)
  49. << SSettingsSource("keys.xml", AdapterNames::TerminalAdapter, false)
  50. << SSettingsSource("config.xml", AdapterNames::TerminalAdapter, true)
  51. << SSettingsSource("commissions.xml", AdapterNames::DealerAdapter, true)
  52. << SSettingsSource("commissions.local.xml", AdapterNames::DealerAdapter, true)
  53. << SSettingsSource("config.xml", AdapterNames::DealerAdapter, true)
  54. << SSettingsSource("customers.xml", AdapterNames::DealerAdapter, true)
  55. << SSettingsSource("groups.xml", AdapterNames::DealerAdapter, true)
  56. << SSettingsSource("user.ini", AdapterNames::UserAdapter, true)
  57. << SSettingsSource("numcapacity.xml", AdapterNames::Directory, true)
  58. << SSettingsSource(mApplication->getWorkingDirectory() + "/data/directory.xml", AdapterNames::Directory, true)
  59. << SSettingsSource("config.xml", AdapterNames::Extensions, true)
  60. // Временный конфиг #38560
  61. << SSettingsSource("extensions.xml", AdapterNames::Extensions, true);
  62. // Загрузка всех operators.xml
  63. foreach (auto file, QDir(mApplication->getUserDataPath()).entryInfoList(QStringList() << "operators*.xml", QDir::Files, QDir::Name))
  64. {
  65. // Вставляем в property_tree как ссылку на файл
  66. settingsSources << SSettingsSource(file.filePath(), AdapterNames::DealerAdapter, "operators");
  67. }
  68. // Загружаем настройки.
  69. mSettingsManager->loadSettings(settingsSources);
  70. // Инициализируем все адаптеры настроек.
  71. SDK::PaymentProcessor::TerminalSettings * terminalSettings = new SDK::PaymentProcessor::TerminalSettings(mSettingsManager->getProperties());
  72. terminalSettings->setLog(mApplication->getLog());
  73. terminalSettings->initialize();
  74. // Устанавливаем переменные окружения.
  75. SDK::PaymentProcessor::SAppEnvironment environment;
  76. environment.userDataPath = IApplication::toAbsolutePath(mApplication->getSettings().value(CSettings::UserDataPath).toString());
  77. environment.contentPath = IApplication::toAbsolutePath(mApplication->getSettings().value(CSettings::ContentPath).toString());
  78. environment.interfacePath = IApplication::toAbsolutePath(mApplication->getSettings().value(CSettings::InterfacePath).toString());
  79. environment.adPath = IApplication::toAbsolutePath(mApplication->getSettings().value(CSettings::AdPath).toString());
  80. environment.version = Cyberplat::getVersion();
  81. terminalSettings->setAppEnvironment(environment);
  82. SDK::PaymentProcessor::DealerSettings * dealerSettings = new SDK::PaymentProcessor::DealerSettings(mSettingsManager->getProperties());
  83. dealerSettings->setLog(mApplication->getLog());
  84. dealerSettings->initialize();
  85. SDK::PaymentProcessor::Directory * directory = new SDK::PaymentProcessor::Directory(mSettingsManager->getProperties());
  86. directory->setLog(mApplication->getLog());
  87. SDK::PaymentProcessor::ExtensionsSettings * extensionsSettings = new SDK::PaymentProcessor::ExtensionsSettings(mSettingsManager->getProperties());
  88. extensionsSettings->setLog(mApplication->getLog());
  89. SDK::PaymentProcessor::UserSettings * userSettings = new SDK::PaymentProcessor::UserSettings(mSettingsManager->getProperties());
  90. userSettings->setLog(mApplication->getLog());
  91. mSettingsAdapters.insert(AdapterNames::TerminalAdapter, terminalSettings);
  92. mSettingsAdapters.insert(AdapterNames::DealerAdapter, dealerSettings);
  93. mSettingsAdapters.insert(AdapterNames::UserAdapter, userSettings);
  94. mSettingsAdapters.insert(AdapterNames::Directory, directory);
  95. mSettingsAdapters.insert(AdapterNames::Extensions, extensionsSettings);
  96. return true;
  97. }
  98. //------------------------------------------------------------------------------
  99. void SettingsService::finishInitialize()
  100. {
  101. if (mRestoreConfiguration)
  102. {
  103. EventService::instance(mApplication)->sendEvent(PPSDK::EEventType::RestoreConfiguration, QVariant());
  104. mRestoreConfiguration = false;
  105. }
  106. }
  107. //---------------------------------------------------------------------------
  108. bool SettingsService::canShutdown()
  109. {
  110. return true;
  111. }
  112. //---------------------------------------------------------------------------
  113. bool SettingsService::shutdown()
  114. {
  115. foreach (SDK::PaymentProcessor::ISettingsAdapter * adapter, mSettingsAdapters)
  116. {
  117. delete adapter;
  118. }
  119. mSettingsAdapters.clear();
  120. delete mSettingsManager;
  121. return true;
  122. }
  123. //---------------------------------------------------------------------------
  124. QString SettingsService::getName() const
  125. {
  126. return CServices::SettingsService;
  127. }
  128. //---------------------------------------------------------------------------
  129. const QSet<QString> & SettingsService::getRequiredServices() const
  130. {
  131. static QSet<QString> requiredServices;
  132. return requiredServices;
  133. }
  134. //---------------------------------------------------------------------------
  135. QVariantMap SettingsService::getParameters() const
  136. {
  137. return QVariantMap();
  138. }
  139. //---------------------------------------------------------------------------
  140. void SettingsService::resetParameters(const QSet<QString> &)
  141. {
  142. }
  143. //---------------------------------------------------------------------------
  144. SettingsManager * SettingsService::getSettingsManager() const
  145. {
  146. return mSettingsManager;
  147. }
  148. //---------------------------------------------------------------------------
  149. SDK::PaymentProcessor::ISettingsAdapter * SettingsService::getAdapter(const QString & aAdapterName)
  150. {
  151. return mSettingsAdapters.contains(aAdapterName) ? mSettingsAdapters[aAdapterName] : 0;
  152. }
  153. //---------------------------------------------------------------------------
  154. bool SettingsService::saveConfiguration()
  155. {
  156. return mSettingsManager->saveSettings();
  157. }
  158. //---------------------------------------------------------------------------
  159. QList<SDK::PaymentProcessor::ISettingsAdapter *> SettingsService::enumerateAdapters() const
  160. {
  161. return mSettingsAdapters.values();
  162. }
  163. //---------------------------------------------------------------------------