[videodb] Remove nested transaction when saving state after stopping PVR playback
[xbmc.git] / xbmc / ServiceManager.cpp
blob9af3405f7dccd3a81860a9a9124702f6437423b9
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "ServiceManager.h"
11 #include "ContextMenuManager.h"
12 #include "DatabaseManager.h"
13 #include "PlayListPlayer.h"
14 #include "addons/AddonManager.h"
15 #include "addons/BinaryAddonCache.h"
16 #include "addons/ExtsMimeSupportList.h"
17 #include "addons/RepositoryUpdater.h"
18 #include "addons/Service.h"
19 #include "addons/VFSEntry.h"
20 #include "addons/binary-addons/BinaryAddonManager.h"
21 #include "cores/DataCacheCore.h"
22 #include "cores/RetroPlayer/guibridge/GUIGameRenderManager.h"
23 #include "cores/playercorefactory/PlayerCoreFactory.h"
24 #include "favourites/FavouritesService.h"
25 #include "games/GameServices.h"
26 #include "games/controllers/ControllerManager.h"
27 #include "input/InputManager.h"
28 #include "interfaces/generic/ScriptInvocationManager.h"
29 #include "interfaces/python/XBPython.h"
30 #include "network/Network.h"
31 #include "peripherals/Peripherals.h"
32 #if defined(HAS_FILESYSTEM_SMB)
33 #include "network/IWSDiscovery.h"
34 #if defined(TARGET_WINDOWS)
35 #include "platform/win32/network/WSDiscoveryWin32.h"
36 #else // !defined(TARGET_WINDOWS)
37 #include "platform/posix/filesystem/SMBWSDiscovery.h"
38 #endif // defined(TARGET_WINDOWS)
39 #endif // HAS_FILESYSTEM_SMB
40 #include "powermanagement/PowerManager.h"
41 #include "profiles/ProfileManager.h"
42 #include "pvr/PVRManager.h"
43 #if !defined(TARGET_WINDOWS) && defined(HAS_OPTICAL_DRIVE)
44 #include "storage/DetectDVDType.h"
45 #endif
46 #include "pictures/SlideShowDelegator.h"
47 #include "storage/MediaManager.h"
48 #include "utils/FileExtensionProvider.h"
49 #include "utils/log.h"
50 #include "weather/WeatherManager.h"
52 #include <memory>
54 using namespace KODI;
56 CServiceManager::CServiceManager() = default;
58 CServiceManager::~CServiceManager()
60 if (init_level > 2)
61 DeinitStageThree();
62 if (init_level > 1)
63 DeinitStageTwo();
64 if (init_level > 0)
65 DeinitStageOne();
68 bool CServiceManager::InitForTesting()
70 m_network = CNetworkBase::GetNetwork();
72 m_databaseManager = std::make_unique<CDatabaseManager>();
74 m_binaryAddonManager = std::make_unique<ADDON::CBinaryAddonManager>();
75 m_addonMgr = std::make_unique<ADDON::CAddonMgr>();
76 if (!m_addonMgr->Init())
78 CLog::Log(LOGFATAL, "CServiceManager::{}: Unable to start CAddonMgr", __FUNCTION__);
79 return false;
82 m_extsMimeSupportList = std::make_unique<ADDONS::CExtsMimeSupportList>(*m_addonMgr);
83 m_fileExtensionProvider = std::make_unique<CFileExtensionProvider>(*m_addonMgr);
85 init_level = 1;
86 return true;
89 void CServiceManager::DeinitTesting()
91 init_level = 0;
92 m_fileExtensionProvider.reset();
93 m_extsMimeSupportList.reset();
94 m_binaryAddonManager.reset();
95 m_addonMgr.reset();
96 m_databaseManager.reset();
97 m_network.reset();
100 bool CServiceManager::InitStageOne()
102 m_Platform.reset(CPlatform::CreateInstance());
103 if (!m_Platform->InitStageOne())
104 return false;
106 #ifdef HAS_PYTHON
107 m_XBPython = std::make_unique<XBPython>();
108 CScriptInvocationManager::GetInstance().RegisterLanguageInvocationHandler(m_XBPython.get(),
109 ".py");
110 #endif
112 m_playlistPlayer = std::make_unique<PLAYLIST::CPlayListPlayer>();
113 m_slideShowDelegator = std::make_unique<CSlideShowDelegator>();
115 m_network = CNetworkBase::GetNetwork();
117 init_level = 1;
118 return true;
121 bool CServiceManager::InitStageTwo(const std::string& profilesUserDataFolder)
123 // Initialize the addon database (must be before the addon manager is init'd)
124 m_databaseManager = std::make_unique<CDatabaseManager>();
126 m_binaryAddonManager = std::make_unique<
127 ADDON::
128 CBinaryAddonManager>(); /* Need to constructed before, GetRunningInstance() of binary CAddonDll need to call them */
129 m_addonMgr = std::make_unique<ADDON::CAddonMgr>();
130 if (!m_addonMgr->Init())
132 CLog::Log(LOGFATAL, "CServiceManager::{}: Unable to start CAddonMgr", __FUNCTION__);
133 return false;
136 m_repositoryUpdater = std::make_unique<ADDON::CRepositoryUpdater>(*m_addonMgr);
138 m_extsMimeSupportList = std::make_unique<ADDONS::CExtsMimeSupportList>(*m_addonMgr);
140 m_vfsAddonCache = std::make_unique<ADDON::CVFSAddonCache>();
141 m_vfsAddonCache->Init();
143 m_PVRManager = std::make_unique<PVR::CPVRManager>();
145 m_dataCacheCore = std::make_unique<CDataCacheCore>();
147 m_binaryAddonCache = std::make_unique<ADDON::CBinaryAddonCache>();
148 m_binaryAddonCache->Init();
150 m_favouritesService = std::make_unique<CFavouritesService>(profilesUserDataFolder);
152 m_serviceAddons = std::make_unique<ADDON::CServiceAddonManager>(*m_addonMgr);
154 m_contextMenuManager = std::make_unique<CContextMenuManager>(*m_addonMgr);
156 m_gameControllerManager = std::make_unique<GAME::CControllerManager>(*m_addonMgr);
157 m_inputManager = std::make_unique<CInputManager>();
158 m_inputManager->InitializeInputs();
160 m_peripherals =
161 std::make_unique<PERIPHERALS::CPeripherals>(*m_inputManager, *m_gameControllerManager);
163 m_gameRenderManager = std::make_unique<RETRO::CGUIGameRenderManager>();
165 m_fileExtensionProvider = std::make_unique<CFileExtensionProvider>(*m_addonMgr);
167 m_powerManager = std::make_unique<CPowerManager>();
168 m_powerManager->Initialize();
169 m_powerManager->SetDefaults();
171 m_weatherManager = std::make_unique<CWeatherManager>();
173 m_mediaManager = std::make_unique<CMediaManager>();
174 m_mediaManager->Initialize();
176 #if !defined(TARGET_WINDOWS) && defined(HAS_OPTICAL_DRIVE)
177 m_DetectDVDType = std::make_unique<MEDIA_DETECT::CDetectDVDMedia>();
178 #endif
180 #if defined(HAS_FILESYSTEM_SMB)
181 m_WSDiscovery = WSDiscovery::IWSDiscovery::GetInstance();
182 #endif
184 if (!m_Platform->InitStageTwo())
185 return false;
187 init_level = 2;
188 return true;
191 // stage 3 is called after successful initialization of WindowManager
192 bool CServiceManager::InitStageThree(const std::shared_ptr<CProfileManager>& profileManager)
194 #if !defined(TARGET_WINDOWS) && defined(HAS_OPTICAL_DRIVE)
195 // Start Thread for DVD Mediatype detection
196 CLog::Log(LOGINFO, "[Media Detection] starting service for optical media detection");
197 m_DetectDVDType->Create(false);
198 #endif
200 // Peripherals depends on strings being loaded before stage 3
201 m_peripherals->Initialise();
203 m_gameServices =
204 std::make_unique<GAME::CGameServices>(*m_gameControllerManager, *m_gameRenderManager,
205 *m_peripherals, *profileManager, *m_inputManager);
207 m_contextMenuManager->Init();
209 // Init PVR manager after login, not already on login screen
210 if (!profileManager->UsingLoginScreen())
211 m_PVRManager->Init();
213 m_playerCoreFactory = std::make_unique<CPlayerCoreFactory>(*profileManager);
215 if (!m_Platform->InitStageThree())
216 return false;
218 init_level = 3;
219 return true;
222 void CServiceManager::DeinitStageThree()
224 init_level = 2;
225 #if !defined(TARGET_WINDOWS) && defined(HAS_OPTICAL_DRIVE)
226 m_DetectDVDType->StopThread();
227 m_DetectDVDType.reset();
228 #endif
229 m_playerCoreFactory.reset();
230 m_PVRManager->Deinit();
231 m_contextMenuManager->Deinit();
232 m_gameServices.reset();
233 m_peripherals->Clear();
235 m_Platform->DeinitStageThree();
238 void CServiceManager::DeinitStageTwo()
240 init_level = 1;
242 #if defined(HAS_FILESYSTEM_SMB)
243 m_WSDiscovery.reset();
244 #endif
246 m_weatherManager.reset();
247 m_powerManager.reset();
248 m_fileExtensionProvider.reset();
249 m_gameRenderManager.reset();
250 m_peripherals.reset();
251 m_inputManager.reset();
252 m_gameControllerManager.reset();
253 m_contextMenuManager.reset();
254 m_serviceAddons.reset();
255 m_favouritesService.reset();
256 m_binaryAddonCache.reset();
257 m_dataCacheCore.reset();
258 m_PVRManager.reset();
259 m_extsMimeSupportList.reset();
260 m_vfsAddonCache.reset();
261 m_repositoryUpdater.reset();
262 m_binaryAddonManager.reset();
263 m_addonMgr.reset();
264 m_databaseManager.reset();
266 m_mediaManager->Stop();
267 m_mediaManager.reset();
269 m_Platform->DeinitStageTwo();
272 void CServiceManager::DeinitStageOne()
274 init_level = 0;
276 m_network.reset();
277 m_playlistPlayer.reset();
278 m_slideShowDelegator.reset();
279 #ifdef HAS_PYTHON
280 CScriptInvocationManager::GetInstance().UnregisterLanguageInvocationHandler(m_XBPython.get());
281 m_XBPython.reset();
282 #endif
284 m_Platform->DeinitStageOne();
285 m_Platform.reset();
288 #if defined(HAS_FILESYSTEM_SMB)
289 WSDiscovery::IWSDiscovery& CServiceManager::GetWSDiscovery()
291 return *m_WSDiscovery;
293 #endif
295 ADDON::CAddonMgr& CServiceManager::GetAddonMgr()
297 return *m_addonMgr;
300 ADDONS::CExtsMimeSupportList& CServiceManager::GetExtsMimeSupportList()
302 return *m_extsMimeSupportList;
305 ADDON::CBinaryAddonCache& CServiceManager::GetBinaryAddonCache()
307 return *m_binaryAddonCache;
310 ADDON::CBinaryAddonManager& CServiceManager::GetBinaryAddonManager()
312 return *m_binaryAddonManager;
315 ADDON::CVFSAddonCache& CServiceManager::GetVFSAddonCache()
317 return *m_vfsAddonCache;
320 ADDON::CServiceAddonManager& CServiceManager::GetServiceAddons()
322 return *m_serviceAddons;
325 ADDON::CRepositoryUpdater& CServiceManager::GetRepositoryUpdater()
327 return *m_repositoryUpdater;
330 #ifdef HAS_PYTHON
331 XBPython& CServiceManager::GetXBPython()
333 return *m_XBPython;
335 #endif
337 #if !defined(TARGET_WINDOWS) && defined(HAS_OPTICAL_DRIVE)
338 MEDIA_DETECT::CDetectDVDMedia& CServiceManager::GetDetectDVDMedia()
340 return *m_DetectDVDType;
342 #endif
344 PVR::CPVRManager& CServiceManager::GetPVRManager()
346 return *m_PVRManager;
349 CContextMenuManager& CServiceManager::GetContextMenuManager()
351 return *m_contextMenuManager;
354 CDataCacheCore& CServiceManager::GetDataCacheCore()
356 return *m_dataCacheCore;
359 CPlatform& CServiceManager::GetPlatform()
361 return *m_Platform;
364 PLAYLIST::CPlayListPlayer& CServiceManager::GetPlaylistPlayer()
366 return *m_playlistPlayer;
369 GAME::CControllerManager& CServiceManager::GetGameControllerManager()
371 return *m_gameControllerManager;
374 GAME::CGameServices& CServiceManager::GetGameServices()
376 return *m_gameServices;
379 KODI::RETRO::CGUIGameRenderManager& CServiceManager::GetGameRenderManager()
381 return *m_gameRenderManager;
384 PERIPHERALS::CPeripherals& CServiceManager::GetPeripherals()
386 return *m_peripherals;
389 CFavouritesService& CServiceManager::GetFavouritesService()
391 return *m_favouritesService;
394 CInputManager& CServiceManager::GetInputManager()
396 return *m_inputManager;
399 CFileExtensionProvider& CServiceManager::GetFileExtensionProvider()
401 return *m_fileExtensionProvider;
404 CPowerManager& CServiceManager::GetPowerManager()
406 return *m_powerManager;
409 CNetworkBase& CServiceManager::GetNetwork()
411 return *m_network;
414 CWeatherManager& CServiceManager::GetWeatherManager()
416 return *m_weatherManager;
419 CPlayerCoreFactory& CServiceManager::GetPlayerCoreFactory()
421 return *m_playerCoreFactory;
424 CDatabaseManager& CServiceManager::GetDatabaseManager()
426 return *m_databaseManager;
429 CMediaManager& CServiceManager::GetMediaManager()
431 return *m_mediaManager;
434 CSlideShowDelegator& CServiceManager::GetSlideShowDelegator()
436 return *m_slideShowDelegator;