| 1 |
/* |
| 2 |
* Copyright (C) 2013-2018 Team Kodi |
| 3 |
* This file is part of Kodi - https://kodi.tv |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSES/README.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "ProfileManager.h" |
| 10 |
|
| 11 |
#include "ContextMenuManager.h" //! @todo Remove me |
| 12 |
#include "DatabaseManager.h" |
| 13 |
#include "FileItem.h" |
| 14 |
#include "GUIInfoManager.h" |
| 15 |
#include "GUIPassword.h" |
| 16 |
#include "PasswordManager.h" |
| 17 |
#include "PlayListPlayer.h" //! @todo Remove me |
| 18 |
#include "ServiceBroker.h" |
| 19 |
#include "Util.h" |
| 20 |
#include "addons/AddonManager.h" //! @todo Remove me |
| 21 |
#include "addons/Service.h" //! @todo Remove me |
| 22 |
#include "addons/Skin.h" |
| 23 |
#include "application/Application.h" //! @todo Remove me |
| 24 |
#include "application/ApplicationComponents.h" |
| 25 |
#include "application/ApplicationPowerHandling.h" |
| 26 |
#include "dialogs/GUIDialogKaiToast.h" |
| 27 |
#include "dialogs/GUIDialogYesNo.h" |
| 28 |
#include "events/EventLog.h" |
| 29 |
#include "events/EventLogManager.h" |
| 30 |
#include "favourites/FavouritesService.h" //! @todo Remove me |
| 31 |
#include "filesystem/Directory.h" |
| 32 |
#include "filesystem/DirectoryCache.h" |
| 33 |
#include "filesystem/File.h" |
| 34 |
#include "filesystem/SpecialProtocol.h" |
| 35 |
#include "guilib/GUIComponent.h" |
| 36 |
#include "guilib/GUIWindowManager.h" |
| 37 |
#include "guilib/LocalizeStrings.h" |
| 38 |
#include "guilib/StereoscopicsManager.h" //! @todo Remove me |
| 39 |
#include "input/InputManager.h" |
| 40 |
#include "interfaces/json-rpc/JSONRPC.h" //! @todo Remove me |
| 41 |
#include "music/MusicLibraryQueue.h" |
| 42 |
#include "network/Network.h" //! @todo Remove me |
| 43 |
#include "network/NetworkServices.h" //! @todo Remove me |
| 44 |
#include "pvr/PVRManager.h" //! @todo Remove me |
| 45 |
#include "settings/Settings.h" |
| 46 |
#include "settings/SettingsComponent.h" |
| 47 |
#include "settings/lib/SettingsManager.h" |
| 48 |
#if !defined(TARGET_WINDOWS) && defined(HAS_OPTICAL_DRIVE) |
| 49 |
#include "storage/DetectDVDType.h" |
| 50 |
#endif |
| 51 |
#include "threads/SingleLock.h" |
| 52 |
#include "utils/FileUtils.h" |
| 53 |
#include "utils/StringUtils.h" |
| 54 |
#include "utils/URIUtils.h" |
| 55 |
#include "utils/Variant.h" |
| 56 |
#include "utils/XMLUtils.h" |
| 57 |
#include "utils/log.h" |
| 58 |
#include "video/VideoLibraryQueue.h" //! @todo Remove me |
| 59 |
#include "weather/WeatherManager.h" //! @todo Remove me |
| 60 |
|
| 61 |
#include <algorithm> |
| 62 |
#include <memory> |
| 63 |
#include <mutex> |
| 64 |
#include <string> |
| 65 |
#include <vector> |
| 66 |
|
| 67 |
//! @todo |
| 68 |
//! eventually the profile should dictate where special://masterprofile/ is |
| 69 |
//! but for now it makes sense to leave all the profile settings in a user |
| 70 |
//! writeable location like special://masterprofile/ |
| 71 |
#define PROFILES_FILE "special://masterprofile/profiles.xml" |
| 72 |
|
| 73 |
#define XML_PROFILES "profiles" |
| 74 |
#define XML_AUTO_LOGIN "autologin" |
| 75 |
#define XML_LAST_LOADED "lastloaded" |
| 76 |
#define XML_LOGIN_SCREEN "useloginscreen" |
| 77 |
#define XML_NEXTID "nextIdProfile" |
| 78 |
#define XML_PROFILE "profile" |
| 79 |
|
| 80 |
using namespace XFILE; |
| 81 |
|
| 82 |
static CProfile EmptyProfile; |
| 83 |
|
| 84 |
CProfileManager::CProfileManager() : m_eventLogs(new CEventLogManager) |
| 85 |
{ |
| 86 |
} |
| 87 |
|
| 88 |
CProfileManager::~CProfileManager() = default; |
| 89 |
|
| 90 |
void CProfileManager::Initialize(const std::shared_ptr<CSettings>& settings) |
| 91 |
{ |
| 92 |
m_settings = settings; |
| 93 |
|
| 94 |
if (m_settings->IsLoaded()) |
| 95 |
OnSettingsLoaded(); |
| 96 |
|
| 97 |
m_settings->GetSettingsManager()->RegisterSettingsHandler(this); |
| 98 |
|
| 99 |
std::set<std::string> settingSet = { |
| 100 |
CSettings::SETTING_EVENTLOG_SHOW |
| 101 |
}; |
| 102 |
|
| 103 |
m_settings->GetSettingsManager()->RegisterCallback(this, settingSet); |
| 104 |
} |
| 105 |
|
| 106 |
void CProfileManager::Uninitialize() |
| 107 |
{ |
| 108 |
m_settings->GetSettingsManager()->UnregisterCallback(this); |
| 109 |
m_settings->GetSettingsManager()->UnregisterSettingsHandler(this); |
| 110 |
} |
| 111 |
|
| 112 |
void CProfileManager::OnSettingsLoaded() |
| 113 |
{ |
| 114 |
// check them all |
| 115 |
std::string strDir = m_settings->GetString(CSettings::SETTING_SYSTEM_PLAYLISTSPATH); |
| 116 |
if (strDir == "set default" || strDir.empty()) |
| 117 |
{ |
| 118 |
strDir = "special://profile/playlists/"; |
| 119 |
m_settings->SetString(CSettings::SETTING_SYSTEM_PLAYLISTSPATH, strDir); |
| 120 |
} |
| 121 |
|
| 122 |
CDirectory::Create(strDir); |
| 123 |
CDirectory::Create(URIUtils::AddFileToFolder(strDir,"music")); |
| 124 |
CDirectory::Create(URIUtils::AddFileToFolder(strDir,"video")); |
| 125 |
CDirectory::Create(URIUtils::AddFileToFolder(strDir,"mixed")); |
| 126 |
} |
| 127 |
|
| 128 |
void CProfileManager::OnSettingsSaved() const |
| 129 |
{ |
| 130 |
// save mastercode |
| 131 |
Save(); |
| 132 |
} |
| 133 |
|
| 134 |
void CProfileManager::OnSettingsCleared() |
| 135 |
{ |
| 136 |
Clear(); |
| 137 |
} |
| 138 |
|
| 139 |
bool CProfileManager::Load() |
| 140 |
{ |
| 141 |
bool ret = true; |
| 142 |
const std::string file = PROFILES_FILE; |
| 143 |
|
| 144 |
std::unique_lock<CCriticalSection> lock(m_critical); |
| 145 |
|
| 146 |
// clear out our profiles |
| 147 |
m_profiles.clear(); |
| 148 |
|
| 149 |
if (CFile::Exists(file)) |
| 150 |
{ |
| 151 |
CXBMCTinyXML profilesDoc; |
| 152 |
if (profilesDoc.LoadFile(file)) |
| 153 |
{ |
| 154 |
const TiXmlElement *rootElement = profilesDoc.RootElement(); |
| 155 |
if (rootElement && StringUtils::EqualsNoCase(rootElement->Value(), XML_PROFILES)) |
| 156 |
{ |
| 157 |
XMLUtils::GetUInt(rootElement, XML_LAST_LOADED, m_lastUsedProfile); |
| 158 |
XMLUtils::GetBoolean(rootElement, XML_LOGIN_SCREEN, m_usingLoginScreen); |
| 159 |
XMLUtils::GetInt(rootElement, XML_AUTO_LOGIN, m_autoLoginProfile); |
| 160 |
XMLUtils::GetInt(rootElement, XML_NEXTID, m_nextProfileId); |
| 161 |
|
| 162 |
std::string defaultDir("special://home/userdata"); |
| 163 |
if (!CDirectory::Exists(defaultDir)) |
| 164 |
defaultDir = "special://xbmc/userdata"; |
| 165 |
|
| 166 |
const TiXmlElement* pProfile = rootElement->FirstChildElement(XML_PROFILE); |
| 167 |
while (pProfile) |
| 168 |
{ |
| 169 |
CProfile profile(defaultDir); |
| 170 |
profile.Load(pProfile, GetNextProfileId()); |
| 171 |
AddProfile(profile); |
| 172 |
|
| 173 |
pProfile = pProfile->NextSiblingElement(XML_PROFILE); |
| 174 |
} |
| 175 |
} |
| 176 |
else |
| 177 |
{ |
| 178 |
CLog::Log(LOGERROR, "CProfileManager: error loading {}, no <profiles> node", file); |
| 179 |
ret = false; |
| 180 |
} |
| 181 |
} |
| 182 |
else |
| 183 |
{ |
| 184 |
CLog::Log(LOGERROR, "CProfileManager: error loading {}, Line {}\n{}", file, |
| 185 |
profilesDoc.ErrorRow(), profilesDoc.ErrorDesc()); |
| 186 |
ret = false; |
| 187 |
} |
| 188 |
} |
| 189 |
if (!ret) |
| 190 |
{ |
| 191 |
CLog::Log(LOGERROR, |
| 192 |
"Failed to load profile - might be corrupted - falling back to master profile"); |
| 193 |
m_profiles.clear(); |
| 194 |
CFile::Delete(file); |
| 195 |
|
| 196 |
ret = true; |
| 197 |
} |
| 198 |
|
| 199 |
if (m_profiles.empty()) |
| 200 |
{ // add the master user |
| 201 |
CProfile profile("special://masterprofile/", "Master user", 0); |
| 202 |
AddProfile(profile); |
| 203 |
} |
| 204 |
|
| 205 |
// check the validity of the previous profile index |
| 206 |
if (m_lastUsedProfile >= m_profiles.size()) |
| 207 |
m_lastUsedProfile = 0; |
| 208 |
|
| 209 |
SetCurrentProfileId(m_lastUsedProfile); |
| 210 |
|
| 211 |
// check the validity of the auto login profile index |
| 212 |
if (m_autoLoginProfile < -1 || m_autoLoginProfile >= (int)m_profiles.size()) |
| 213 |
m_autoLoginProfile = -1; |
| 214 |
else if (m_autoLoginProfile >= 0) |
| 215 |
SetCurrentProfileId(m_autoLoginProfile); |
| 216 |
|
| 217 |
// the login screen runs as the master profile, so if we're using this, we need to ensure |
| 218 |
// we switch to the master profile |
| 219 |
if (m_usingLoginScreen) |
| 220 |
SetCurrentProfileId(0); |
| 221 |
|
| 222 |
return ret; |
| 223 |
} |
| 224 |
|
| 225 |
bool CProfileManager::Save() const |
| 226 |
{ |
| 227 |
const std::string file = PROFILES_FILE; |
| 228 |
|
| 229 |
std::unique_lock<CCriticalSection> lock(m_critical); |
| 230 |
|
| 231 |
CXBMCTinyXML xmlDoc; |
| 232 |
TiXmlElement xmlRootElement(XML_PROFILES); |
| 233 |
TiXmlNode *pRoot = xmlDoc.InsertEndChild(xmlRootElement); |
| 234 |
if (pRoot == nullptr) |
| 235 |
return false; |
| 236 |
|
| 237 |
XMLUtils::SetInt(pRoot, XML_LAST_LOADED, m_currentProfile); |
| 238 |
XMLUtils::SetBoolean(pRoot, XML_LOGIN_SCREEN, m_usingLoginScreen); |
| 239 |
XMLUtils::SetInt(pRoot, XML_AUTO_LOGIN, m_autoLoginProfile); |
| 240 |
XMLUtils::SetInt(pRoot, XML_NEXTID, m_nextProfileId); |
| 241 |
|
| 242 |
for (const auto& profile : m_profiles) |
| 243 |
profile.Save(pRoot); |
| 244 |
|
| 245 |
// save the file |
| 246 |
return xmlDoc.SaveFile(file); |
| 247 |
} |
| 248 |
|
| 249 |
void CProfileManager::Clear() |
| 250 |
{ |
| 251 |
std::unique_lock<CCriticalSection> lock(m_critical); |
| 252 |
m_usingLoginScreen = false; |
| 253 |
m_profileLoadedForLogin = false; |
| 254 |
m_previousProfileLoadedForLogin = false; |
| 255 |
m_lastUsedProfile = 0; |
| 256 |
m_nextProfileId = 0; |
| 257 |
SetCurrentProfileId(0); |
| 258 |
m_profiles.clear(); |
| 259 |
} |
| 260 |
|
| 261 |
void CProfileManager::PrepareLoadProfile(unsigned int profileIndex) |
| 262 |
{ |
| 263 |
CContextMenuManager &contextMenuManager = CServiceBroker::GetContextMenuManager(); |
| 264 |
ADDON::CServiceAddonManager &serviceAddons = CServiceBroker::GetServiceAddons(); |
| 265 |
PVR::CPVRManager &pvrManager = CServiceBroker::GetPVRManager(); |
| 266 |
CNetworkBase &networkManager = CServiceBroker::GetNetwork(); |
| 267 |
|
| 268 |
contextMenuManager.Deinit(); |
| 269 |
|
| 270 |
serviceAddons.Stop(); |
| 271 |
|
| 272 |
// stop PVR related services |
| 273 |
pvrManager.Stop(); |
| 274 |
|
| 275 |
if (profileIndex != 0 || !IsMasterProfile()) |
| 276 |
networkManager.NetworkMessage(CNetworkBase::SERVICES_DOWN, 1); |
| 277 |
} |
| 278 |
|
| 279 |
bool CProfileManager::LoadProfile(unsigned int index) |
| 280 |
{ |
| 281 |
PrepareLoadProfile(index); |
| 282 |
|
| 283 |
if (index == 0 && IsMasterProfile()) |
| 284 |
{ |
| 285 |
CGUIWindow* pWindow = CServiceBroker::GetGUI()->GetWindowManager().GetWindow(WINDOW_HOME); |
| 286 |
if (pWindow) |
| 287 |
pWindow->ResetControlStates(); |
| 288 |
|
| 289 |
UpdateCurrentProfileDate(); |
| 290 |
FinalizeLoadProfile(); |
| 291 |
|
| 292 |
return true; |
| 293 |
} |
| 294 |
|
| 295 |
std::unique_lock<CCriticalSection> lock(m_critical); |
| 296 |
// check if the index is valid or not |
| 297 |
if (index >= m_profiles.size()) |
| 298 |
return false; |
| 299 |
|
| 300 |
// check if the profile is already active |
| 301 |
if (m_currentProfile == index) |
| 302 |
return true; |
| 303 |
|
| 304 |
// save any settings of the currently used skin but only if the (master) |
| 305 |
// profile hasn't just been loaded as a temporary profile for login |
| 306 |
if (g_SkinInfo != nullptr && !m_previousProfileLoadedForLogin) |
| 307 |
g_SkinInfo->SaveSettings(); |
| 308 |
|
| 309 |
// @todo: why is m_settings not used here? |
| 310 |
const std::shared_ptr<CSettings> settings = CServiceBroker::GetSettingsComponent()->GetSettings(); |
| 311 |
|
| 312 |
// unload any old settings |
| 313 |
settings->Unload(); |
| 314 |
|
| 315 |
SetCurrentProfileId(index); |
| 316 |
m_previousProfileLoadedForLogin = false; |
| 317 |
|
| 318 |
// load the new settings |
| 319 |
if (!settings->Load()) |
| 320 |
{ |
| 321 |
CLog::Log(LOGFATAL, "CProfileManager: unable to load settings for profile \"{}\"", |
| 322 |
m_profiles.at(index).getName()); |
| 323 |
return false; |
| 324 |
} |
| 325 |
settings->SetLoaded(); |
| 326 |
|
| 327 |
CreateProfileFolders(); |
| 328 |
|
| 329 |
CServiceBroker::GetDatabaseManager().Initialize(); |
| 330 |
CServiceBroker::GetInputManager().LoadKeymaps(); |
| 331 |
|
| 332 |
CServiceBroker::GetInputManager().SetMouseEnabled(settings->GetBool(CSettings::SETTING_INPUT_ENABLEMOUSE)); |
| 333 |
|
| 334 |
CGUIComponent* gui = CServiceBroker::GetGUI(); |
| 335 |
if (gui) |
| 336 |
{ |
| 337 |
CGUIInfoManager& infoMgr = gui->GetInfoManager(); |
| 338 |
infoMgr.ResetCache(); |
| 339 |
infoMgr.GetInfoProviders().GetGUIControlsInfoProvider().ResetContainerMovingCache(); |
| 340 |
infoMgr.GetInfoProviders().GetLibraryInfoProvider().ResetLibraryBools(); |
| 341 |
} |
| 342 |
|
| 343 |
if (m_currentProfile != 0) |
| 344 |
{ |
| 345 |
CXBMCTinyXML doc; |
| 346 |
if (doc.LoadFile(URIUtils::AddFileToFolder(GetUserDataFolder(), "guisettings.xml"))) |
| 347 |
{ |
| 348 |
settings->LoadSetting(doc.RootElement(), CSettings::SETTING_MASTERLOCK_MAXRETRIES); |
| 349 |
settings->LoadSetting(doc.RootElement(), CSettings::SETTING_MASTERLOCK_STARTUPLOCK); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
CPasswordManager::GetInstance().Clear(); |
| 354 |
|
| 355 |
// to set labels - shares are reloaded |
| 356 |
#if !defined(TARGET_WINDOWS) && defined(HAS_OPTICAL_DRIVE) |
| 357 |
MEDIA_DETECT::CDetectDVDMedia::UpdateState(); |
| 358 |
#endif |
| 359 |
|
| 360 |
// init windows |
| 361 |
CGUIMessage msg(GUI_MSG_NOTIFY_ALL, 0, 0, GUI_MSG_WINDOW_RESET); |
| 362 |
CServiceBroker::GetGUI()->GetWindowManager().SendMessage(msg); |
| 363 |
|
| 364 |
CUtil::DeleteDirectoryCache(); |
| 365 |
g_directoryCache.Clear(); |
| 366 |
|
| 367 |
lock.unlock(); |
| 368 |
|
| 369 |
UpdateCurrentProfileDate(); |
| 370 |
FinalizeLoadProfile(); |
| 371 |
|
| 372 |
m_profileLoadedForLogin = false; |
| 373 |
|
| 374 |
return true; |
| 375 |
} |
| 376 |
|
| 377 |
void CProfileManager::FinalizeLoadProfile() |
| 378 |
{ |
| 379 |
CContextMenuManager &contextMenuManager = CServiceBroker::GetContextMenuManager(); |
| 380 |
ADDON::CServiceAddonManager &serviceAddons = CServiceBroker::GetServiceAddons(); |
| 381 |
PVR::CPVRManager &pvrManager = CServiceBroker::GetPVRManager(); |
| 382 |
CNetworkBase &networkManager = CServiceBroker::GetNetwork(); |
| 383 |
ADDON::CAddonMgr &addonManager = CServiceBroker::GetAddonMgr(); |
| 384 |
CWeatherManager &weatherManager = CServiceBroker::GetWeatherManager(); |
| 385 |
CFavouritesService &favouritesManager = CServiceBroker::GetFavouritesService(); |
| 386 |
PLAYLIST::CPlayListPlayer &playlistManager = CServiceBroker::GetPlaylistPlayer(); |
| 387 |
CStereoscopicsManager &stereoscopicsManager = CServiceBroker::GetGUI()->GetStereoscopicsManager(); |
| 388 |
|
| 389 |
if (m_lastUsedProfile != m_currentProfile) |
| 390 |
{ |
| 391 |
playlistManager.ClearPlaylist(PLAYLIST::TYPE_VIDEO); |
| 392 |
playlistManager.ClearPlaylist(PLAYLIST::TYPE_MUSIC); |
| 393 |
playlistManager.SetCurrentPlaylist(PLAYLIST::TYPE_NONE); |
| 394 |
} |
| 395 |
|
| 396 |
networkManager.NetworkMessage(CNetworkBase::SERVICES_UP, 1); |
| 397 |
|
| 398 |
// reload the add-ons, or we will first load all add-ons from the master account without checking disabled status |
| 399 |
addonManager.ReInit(); |
| 400 |
|
| 401 |
// let CApplication know that we are logging into a new profile |
| 402 |
g_application.SetLoggingIn(true); |
| 403 |
|
| 404 |
if (!g_application.LoadLanguage(true)) |
| 405 |
{ |
| 406 |
CLog::Log(LOGFATAL, "Unable to load language for profile \"{}\"", |
| 407 |
GetCurrentProfile().getName()); |
| 408 |
return; |
| 409 |
} |
| 410 |
|
| 411 |
weatherManager.Refresh(); |
| 412 |
|
| 413 |
JSONRPC::CJSONRPC::Initialize(); |
| 414 |
|
| 415 |
// Restart context menu manager |
| 416 |
contextMenuManager.Init(); |
| 417 |
|
| 418 |
// Restart PVR services if we are not just loading the master profile for the login screen |
| 419 |
if (m_previousProfileLoadedForLogin || m_currentProfile != 0 || m_lastUsedProfile == 0) |
| 420 |
pvrManager.Init(); |
| 421 |
|
| 422 |
favouritesManager.ReInit(GetProfileUserDataFolder()); |
| 423 |
|
| 424 |
// Start these operations only when a profile is loaded, not on the login screen |
| 425 |
if (!m_profileLoadedForLogin || (m_profileLoadedForLogin && m_lastUsedProfile == 0)) |
| 426 |
{ |
| 427 |
serviceAddons.Start(); |
| 428 |
g_application.UpdateLibraries(); |
| 429 |
} |
| 430 |
|
| 431 |
stereoscopicsManager.Initialize(); |
| 432 |
|
| 433 |
// Load initial window |
| 434 |
int firstWindow = g_SkinInfo->GetFirstWindow(); |
| 435 |
|
| 436 |
CServiceBroker::GetGUI()->GetWindowManager().ChangeActiveWindow(firstWindow); |
| 437 |
|
| 438 |
//the user interfaces has been fully initialized, let everyone know |
| 439 |
CGUIMessage msg(GUI_MSG_NOTIFY_ALL, WINDOW_SETTINGS_PROFILES, 0, GUI_MSG_UI_READY); |
| 440 |
CServiceBroker::GetGUI()->GetWindowManager().SendThreadMessage(msg); |
| 441 |
} |
| 442 |
|
| 443 |
void CProfileManager::LogOff() |
| 444 |
{ |
| 445 |
CNetworkBase &networkManager = CServiceBroker::GetNetwork(); |
| 446 |
|
| 447 |
g_application.StopPlaying(); |
| 448 |
|
| 449 |
if (CMusicLibraryQueue::GetInstance().IsScanningLibrary()) |
| 450 |
CMusicLibraryQueue::GetInstance().StopLibraryScanning(); |
| 451 |
|
| 452 |
if (CVideoLibraryQueue::GetInstance().IsRunning()) |
| 453 |
CVideoLibraryQueue::GetInstance().CancelAllJobs(); |
| 454 |
|
| 455 |
// Stop PVR services |
| 456 |
CServiceBroker::GetPVRManager().Stop(); |
| 457 |
|
| 458 |
networkManager.NetworkMessage(CNetworkBase::SERVICES_DOWN, 1); |
| 459 |
|
| 460 |
LoadMasterProfileForLogin(); |
| 461 |
|
| 462 |
g_passwordManager.bMasterUser = false; |
| 463 |
|
| 464 |
auto& components = CServiceBroker::GetAppComponents(); |
| 465 |
const auto appPower = components.GetComponent<CApplicationPowerHandling>(); |
| 466 |
appPower->WakeUpScreenSaverAndDPMS(); |
| 467 |
CServiceBroker::GetGUI()->GetWindowManager().ActivateWindow(WINDOW_LOGIN_SCREEN, {}, false); |
| 468 |
|
| 469 |
if (!CServiceBroker::GetNetwork().GetServices().StartEventServer()) // event server could be needed in some situations |
| 470 |
CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Warning, g_localizeStrings.Get(33102), g_localizeStrings.Get(33100)); |
| 471 |
} |
| 472 |
|
| 473 |
bool CProfileManager::DeleteProfile(unsigned int index) |
| 474 |
{ |
| 475 |
std::unique_lock<CCriticalSection> lock(m_critical); |
| 476 |
const CProfile *profile = GetProfile(index); |
| 477 |
if (profile == NULL) |
| 478 |
return false; |
| 479 |
|
| 480 |
CGUIDialogYesNo* dlgYesNo = CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogYesNo>(WINDOW_DIALOG_YES_NO); |
| 481 |
if (dlgYesNo == NULL) |
| 482 |
return false; |
| 483 |
|
| 484 |
const std::string& str = g_localizeStrings.Get(13201); |
| 485 |
dlgYesNo->SetHeading(CVariant{13200}); |
| 486 |
dlgYesNo->SetLine(0, CVariant{StringUtils::Format(str, profile->getName())}); |
| 487 |
dlgYesNo->SetLine(1, CVariant{""}); |
| 488 |
dlgYesNo->SetLine(2, CVariant{""}); |
| 489 |
dlgYesNo->Open(); |
| 490 |
|
| 491 |
if (!dlgYesNo->IsConfirmed()) |
| 492 |
return false; |
| 493 |
|
| 494 |
// fall back to master profile if necessary |
| 495 |
if ((int)index == m_autoLoginProfile) |
| 496 |
m_autoLoginProfile = 0; |
| 497 |
|
| 498 |
// delete profile |
| 499 |
std::string strDirectory = profile->getDirectory(); |
| 500 |
m_profiles.erase(m_profiles.begin() + index); |
| 501 |
|
| 502 |
// fall back to master profile if necessary |
| 503 |
if (index == m_currentProfile) |
| 504 |
{ |
| 505 |
LoadProfile(0); |
| 506 |
m_settings->Save(); |
| 507 |
} |
| 508 |
|
| 509 |
CFileItemPtr item = |
| 510 |
std::make_shared<CFileItem>(URIUtils::AddFileToFolder(GetUserDataFolder(), strDirectory)); |
| 511 |
item->SetPath(URIUtils::AddFileToFolder(GetUserDataFolder(), strDirectory + "/")); |
| 512 |
item->m_bIsFolder = true; |
| 513 |
item->Select(true); |
| 514 |
|
| 515 |
CGUIComponent *gui = CServiceBroker::GetGUI(); |
| 516 |
if (gui && gui->ConfirmDelete(item->GetPath())) |
| 517 |
CFileUtils::DeleteItem(item); |
| 518 |
|
| 519 |
return Save(); |
| 520 |
} |
| 521 |
|
| 522 |
void CProfileManager::CreateProfileFolders() |
| 523 |
{ |
| 524 |
CDirectory::Create(GetDatabaseFolder()); |
| 525 |
CDirectory::Create(GetCDDBFolder()); |
| 526 |
CDirectory::Create(GetLibraryFolder()); |
| 527 |
|
| 528 |
// create Thumbnails/* |
| 529 |
CDirectory::Create(GetThumbnailsFolder()); |
| 530 |
CDirectory::Create(GetVideoThumbFolder()); |
| 531 |
CDirectory::Create(GetBookmarksThumbFolder()); |
| 532 |
CDirectory::Create(GetSavestatesFolder()); |
| 533 |
for (size_t hex = 0; hex < 16; hex++) |
| 534 |
CDirectory::Create( |
| 535 |
URIUtils::AddFileToFolder(GetThumbnailsFolder(), StringUtils::Format("{:x}", hex))); |
| 536 |
|
| 537 |
CDirectory::Create("special://profile/addon_data"); |
| 538 |
CDirectory::Create("special://profile/keymaps"); |
| 539 |
} |
| 540 |
|
| 541 |
const CProfile& CProfileManager::GetMasterProfile() const |
| 542 |
{ |
| 543 |
std::unique_lock<CCriticalSection> lock(m_critical); |
| 544 |
if (!m_profiles.empty()) |
| 545 |
return m_profiles[0]; |
| 546 |
|
| 547 |
CLog::Log(LOGERROR, "{}: master profile doesn't exist", __FUNCTION__); |
| 548 |
return EmptyProfile; |
| 549 |
} |
| 550 |
|
| 551 |
const CProfile& CProfileManager::GetCurrentProfile() const |
| 552 |
{ |
| 553 |
std::unique_lock<CCriticalSection> lock(m_critical); |
| 554 |
if (m_currentProfile < m_profiles.size()) |
| 555 |
return m_profiles[m_currentProfile]; |
| 556 |
|
| 557 |
CLog::Log(LOGERROR, "CProfileManager: current profile index ({0}) is outside of the valid range ({1})", m_currentProfile, m_profiles.size()); |
| 558 |
return EmptyProfile; |
| 559 |
} |
| 560 |
|
| 561 |
const CProfile* CProfileManager::GetProfile(unsigned int index) const |
| 562 |
{ |
| 563 |
std::unique_lock<CCriticalSection> lock(m_critical); |
| 564 |
if (index < m_profiles.size()) |
| 565 |
return &m_profiles[index]; |
| 566 |
|
| 567 |
return NULL; |
| 568 |
} |
| 569 |
|
| 570 |
CProfile* CProfileManager::GetProfile(unsigned int index) |
| 571 |
{ |
| 572 |
std::unique_lock<CCriticalSection> lock(m_critical); |
| 573 |
if (index < m_profiles.size()) |
| 574 |
return &m_profiles[index]; |
| 575 |
|
| 576 |
return NULL; |
| 577 |
} |
| 578 |
|
| 579 |
int CProfileManager::GetProfileIndex(const std::string &name) const |
| 580 |
{ |
| 581 |
std::unique_lock<CCriticalSection> lock(m_critical); |
| 582 |
for (int i = 0; i < static_cast<int>(m_profiles.size()); i++) |
| 583 |
{ |
| 584 |
if (StringUtils::EqualsNoCase(m_profiles[i].getName(), name)) |
| 585 |
return i; |
| 586 |
} |
| 587 |
|
| 588 |
return -1; |
| 589 |
} |
| 590 |
|
| 591 |
void CProfileManager::AddProfile(const CProfile &profile) |
| 592 |
{ |
| 593 |
{ |
| 594 |
std::unique_lock<CCriticalSection> lock(m_critical); |
| 595 |
// data integrity check - covers off migration from old profiles.xml, |
| 596 |
// incrementing of the m_nextIdProfile,and bad data coming in |
| 597 |
m_nextProfileId = std::max(m_nextProfileId, profile.getId() + 1); |
| 598 |
|
| 599 |
m_profiles.push_back(profile); |
| 600 |
} |
| 601 |
Save(); |
| 602 |
} |
| 603 |
|
| 604 |
void CProfileManager::UpdateCurrentProfileDate() |
| 605 |
{ |
| 606 |
std::unique_lock<CCriticalSection> lock(m_critical); |
| 607 |
if (m_currentProfile < m_profiles.size()) |
| 608 |
{ |
| 609 |
m_profiles[m_currentProfile].setDate(); |
| 610 |
CSingleExit exit(m_critical); |
| 611 |
Save(); |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
void CProfileManager::LoadMasterProfileForLogin() |
| 616 |
{ |
| 617 |
std::unique_lock<CCriticalSection> lock(m_critical); |
| 618 |
// save the previous user |
| 619 |
m_lastUsedProfile = m_currentProfile; |
| 620 |
if (m_currentProfile != 0) |
| 621 |
{ |
| 622 |
// determines that the (master) profile has only been loaded for login |
| 623 |
m_profileLoadedForLogin = true; |
| 624 |
|
| 625 |
LoadProfile(0); |
| 626 |
|
| 627 |
// remember that the (master) profile has only been loaded for login |
| 628 |
m_previousProfileLoadedForLogin = true; |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
bool CProfileManager::GetProfileName(const unsigned int profileId, std::string& name) const |
| 633 |
{ |
| 634 |
std::unique_lock<CCriticalSection> lock(m_critical); |
| 635 |
const CProfile *profile = GetProfile(profileId); |
| 636 |
if (!profile) |
| 637 |
return false; |
| 638 |
|
| 639 |
name = profile->getName(); |
| 640 |
return true; |
| 641 |
} |
| 642 |
|
| 643 |
std::string CProfileManager::GetUserDataFolder() const |
| 644 |
{ |
| 645 |
return GetMasterProfile().getDirectory(); |
| 646 |
} |
| 647 |
|
| 648 |
std::string CProfileManager::GetProfileUserDataFolder() const |
| 649 |
{ |
| 650 |
if (m_currentProfile == 0) |
| 651 |
return GetUserDataFolder(); |
| 652 |
|
| 653 |
return URIUtils::AddFileToFolder(GetUserDataFolder(), GetCurrentProfile().getDirectory()); |
| 654 |
} |
| 655 |
|
| 656 |
std::string CProfileManager::GetDatabaseFolder() const |
| 657 |
{ |
| 658 |
if (GetCurrentProfile().hasDatabases()) |
| 659 |
return URIUtils::AddFileToFolder(GetProfileUserDataFolder(), "Database"); |
| 660 |
|
| 661 |
return URIUtils::AddFileToFolder(GetUserDataFolder(), "Database"); |
| 662 |
} |
| 663 |
|
| 664 |
std::string CProfileManager::GetCDDBFolder() const |
| 665 |
{ |
| 666 |
return URIUtils::AddFileToFolder(GetDatabaseFolder(), "CDDB"); |
| 667 |
} |
| 668 |
|
| 669 |
std::string CProfileManager::GetThumbnailsFolder() const |
| 670 |
{ |
| 671 |
if (GetCurrentProfile().hasDatabases()) |
| 672 |
return URIUtils::AddFileToFolder(GetProfileUserDataFolder(), "Thumbnails"); |
| 673 |
|
| 674 |
return URIUtils::AddFileToFolder(GetUserDataFolder(), "Thumbnails"); |
| 675 |
} |
| 676 |
|
| 677 |
std::string CProfileManager::GetVideoThumbFolder() const |
| 678 |
{ |
| 679 |
return URIUtils::AddFileToFolder(GetThumbnailsFolder(), "Video"); |
| 680 |
} |
| 681 |
|
| 682 |
std::string CProfileManager::GetBookmarksThumbFolder() const |
| 683 |
{ |
| 684 |
return URIUtils::AddFileToFolder(GetVideoThumbFolder(), "Bookmarks"); |
| 685 |
} |
| 686 |
|
| 687 |
std::string CProfileManager::GetLibraryFolder() const |
| 688 |
{ |
| 689 |
if (GetCurrentProfile().hasDatabases()) |
| 690 |
return URIUtils::AddFileToFolder(GetProfileUserDataFolder(), "library"); |
| 691 |
|
| 692 |
return URIUtils::AddFileToFolder(GetUserDataFolder(), "library"); |
| 693 |
} |
| 694 |
|
| 695 |
std::string CProfileManager::GetSavestatesFolder() const |
| 696 |
{ |
| 697 |
if (GetCurrentProfile().hasDatabases()) |
| 698 |
return URIUtils::AddFileToFolder(GetProfileUserDataFolder(), "Savestates"); |
| 699 |
|
| 700 |
return URIUtils::AddFileToFolder(GetUserDataFolder(), "Savestates"); |
| 701 |
} |
| 702 |
|
| 703 |
std::string CProfileManager::GetSettingsFile() const |
| 704 |
{ |
| 705 |
if (m_currentProfile == 0) |
| 706 |
return "special://masterprofile/guisettings.xml"; |
| 707 |
|
| 708 |
return "special://profile/guisettings.xml"; |
| 709 |
} |
| 710 |
|
| 711 |
std::string CProfileManager::GetUserDataItem(const std::string& strFile) const |
| 712 |
{ |
| 713 |
std::string path; |
| 714 |
path = "special://profile/" + strFile; |
| 715 |
|
| 716 |
// check if item exists in the profile (either for folder or |
| 717 |
// for a file (depending on slashAtEnd of strFile) otherwise |
| 718 |
// return path to masterprofile |
| 719 |
if ((URIUtils::HasSlashAtEnd(path) && !CDirectory::Exists(path)) || !CFile::Exists(path)) |
| 720 |
path = "special://masterprofile/" + strFile; |
| 721 |
|
| 722 |
return path; |
| 723 |
} |
| 724 |
|
| 725 |
CEventLog& CProfileManager::GetEventLog() |
| 726 |
{ |
| 727 |
return m_eventLogs->GetEventLog(GetCurrentProfileId()); |
| 728 |
} |
| 729 |
|
| 730 |
void CProfileManager::OnSettingAction(const std::shared_ptr<const CSetting>& setting) |
| 731 |
{ |
| 732 |
if (setting == nullptr) |
| 733 |
return; |
| 734 |
|
| 735 |
const std::string& settingId = setting->GetId(); |
| 736 |
if (settingId == CSettings::SETTING_EVENTLOG_SHOW) |
| 737 |
GetEventLog().ShowFullEventLog(); |
| 738 |
} |
| 739 |
|
| 740 |
void CProfileManager::SetCurrentProfileId(unsigned int profileId) |
| 741 |
{ |
| 742 |
{ |
| 743 |
std::unique_lock<CCriticalSection> lock(m_critical); |
| 744 |
m_currentProfile = profileId; |
| 745 |
CSpecialProtocol::SetProfilePath(GetProfileUserDataFolder()); |
| 746 |
} |
| 747 |
Save(); |
| 748 |
} |