| 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 |
#pragma once |
| 10 |
|
| 11 |
#include "profiles/Profile.h" |
| 12 |
#include "settings/lib/ISettingCallback.h" |
| 13 |
#include "settings/lib/ISettingsHandler.h" |
| 14 |
#include "threads/CriticalSection.h" |
| 15 |
|
| 16 |
#include <memory> |
| 17 |
#include <stdint.h> |
| 18 |
#include <vector> |
| 19 |
|
| 20 |
class CEventLog; |
| 21 |
class CEventLogManager; |
| 22 |
class CSettings; |
| 23 |
class TiXmlNode; |
| 24 |
|
| 25 |
class CProfileManager : protected ISettingsHandler, |
| 26 |
protected ISettingCallback |
| 27 |
{ |
| 28 |
public: |
| 29 |
CProfileManager(); |
| 30 |
CProfileManager(const CProfileManager&) = delete; |
| 31 |
CProfileManager& operator=(CProfileManager const&) = delete; |
| 32 |
~CProfileManager() override; |
| 33 |
|
| 34 |
void Initialize(const std::shared_ptr<CSettings>& settings); |
| 35 |
void Uninitialize(); |
| 36 |
|
| 37 |
void OnSettingsLoaded() override; |
| 38 |
void OnSettingsSaved() const override; |
| 39 |
void OnSettingsCleared() override; |
| 40 |
|
| 41 |
bool Load(); |
| 42 |
/*! \brief Load the user profile information from disk |
| 43 |
Loads the profiles.xml file and creates the list of profiles. |
| 44 |
If no profiles exist, a master user is created. Should be called |
| 45 |
after special://masterprofile/ has been defined. |
| 46 |
\param file XML file to load. |
| 47 |
*/ |
| 48 |
|
| 49 |
bool Save() const; |
| 50 |
/*! \brief Save the user profile information to disk |
| 51 |
Saves the list of profiles to the profiles.xml file. |
| 52 |
\param file XML file to save. |
| 53 |
\return true on success, false on failure to save |
| 54 |
*/ |
| 55 |
|
| 56 |
void Clear(); |
| 57 |
|
| 58 |
bool LoadProfile(unsigned int index); |
| 59 |
void LogOff(); |
| 60 |
|
| 61 |
bool DeleteProfile(unsigned int index); |
| 62 |
|
| 63 |
void CreateProfileFolders(); |
| 64 |
|
| 65 |
/*! \brief Retrieve the master profile |
| 66 |
\return const reference to the master profile |
| 67 |
*/ |
| 68 |
const CProfile& GetMasterProfile() const; |
| 69 |
|
| 70 |
/*! \brief Retrieve the current profile |
| 71 |
\return const reference to the current profile |
| 72 |
*/ |
| 73 |
const CProfile& GetCurrentProfile() const; |
| 74 |
|
| 75 |
/*! \brief Retrieve the profile from an index |
| 76 |
\param unsigned index of the profile to retrieve |
| 77 |
\return const pointer to the profile, NULL if the index is invalid |
| 78 |
*/ |
| 79 |
const CProfile* GetProfile(unsigned int index) const; |
| 80 |
|
| 81 |
/*! \brief Retrieve the profile from an index |
| 82 |
\param unsigned index of the profile to retrieve |
| 83 |
\return pointer to the profile, NULL if the index is invalid |
| 84 |
*/ |
| 85 |
CProfile* GetProfile(unsigned int index); |
| 86 |
|
| 87 |
/*! \brief Retrieve index of a particular profile by name |
| 88 |
\param name name of the profile index to retrieve |
| 89 |
\return index of this profile, -1 if invalid. |
| 90 |
*/ |
| 91 |
int GetProfileIndex(const std::string &name) const; |
| 92 |
|
| 93 |
/*! \brief Retrieve the number of profiles |
| 94 |
\return number of profiles |
| 95 |
*/ |
| 96 |
size_t GetNumberOfProfiles() const { return m_profiles.size(); } |
| 97 |
|
| 98 |
/*! \brief Add a new profile |
| 99 |
\param profile CProfile to add |
| 100 |
*/ |
| 101 |
void AddProfile(const CProfile &profile); |
| 102 |
|
| 103 |
/*! \brief Are we using the login screen? |
| 104 |
\return true if we're using the login screen, false otherwise |
| 105 |
*/ |
| 106 |
bool UsingLoginScreen() const { return m_usingLoginScreen; } |
| 107 |
|
| 108 |
/*! \brief Toggle login screen use on and off |
| 109 |
Toggles the login screen state |
| 110 |
*/ |
| 111 |
void ToggleLoginScreen() |
| 112 |
{ |
| 113 |
m_usingLoginScreen = !m_usingLoginScreen; |
| 114 |
Save(); |
| 115 |
} |
| 116 |
|
| 117 |
/*! \brief Are we the master user? |
| 118 |
\return true if the current profile is the master user, false otherwise |
| 119 |
*/ |
| 120 |
bool IsMasterProfile() const { return m_currentProfile == 0; } |
| 121 |
|
| 122 |
/*! \brief Update the date of the current profile |
| 123 |
*/ |
| 124 |
void UpdateCurrentProfileDate(); |
| 125 |
|
| 126 |
/*! \brief Load the master user for the purposes of logging in |
| 127 |
Loads the master user. Identical to LoadProfile(0) but doesn't |
| 128 |
update the last logged in details |
| 129 |
*/ |
| 130 |
void LoadMasterProfileForLogin(); |
| 131 |
|
| 132 |
/*! \brief Retrieve the last used profile index |
| 133 |
\return the last used profile that logged in. Does not count the |
| 134 |
master user during login. |
| 135 |
*/ |
| 136 |
uint32_t GetLastUsedProfileIndex() const { return m_lastUsedProfile; } |
| 137 |
|
| 138 |
/*! \brief Retrieve the current profile index |
| 139 |
\return the index of the currently logged in profile. |
| 140 |
*/ |
| 141 |
uint32_t GetCurrentProfileIndex() const { return m_currentProfile; } |
| 142 |
|
| 143 |
/*! \brief Retrieve the next id to use for a new profile |
| 144 |
\return the unique <id> to be used when creating a new profile |
| 145 |
*/ |
| 146 |
int GetNextProfileId() const { return m_nextProfileId; } |
| 147 |
|
| 148 |
int GetCurrentProfileId() const { return GetCurrentProfile().getId(); } |
| 149 |
|
| 150 |
/*! \brief Retrieve the autologin profile id |
| 151 |
Retrieves the autologin profile id. When set to -1, then the last |
| 152 |
used profile will be loaded |
| 153 |
\return the id to the autologin profile |
| 154 |
*/ |
| 155 |
int GetAutoLoginProfileId() const { return m_autoLoginProfile; } |
| 156 |
|
| 157 |
/*! \brief Retrieve the autologin profile id |
| 158 |
Retrieves the autologin profile id. When set to -1, then the last |
| 159 |
used profile will be loaded |
| 160 |
\return the id to the autologin profile |
| 161 |
*/ |
| 162 |
void SetAutoLoginProfileId(const int profileId) |
| 163 |
{ |
| 164 |
m_autoLoginProfile = profileId; |
| 165 |
Save(); |
| 166 |
} |
| 167 |
|
| 168 |
/*! \brief Retrieve the name of a particular profile by index |
| 169 |
\param profileId profile index for which to retrieve the name |
| 170 |
\param name will hold the name of the profile when a valid profile index has been provided |
| 171 |
\return false if profileId is an invalid index, true if the name parameter is set |
| 172 |
*/ |
| 173 |
bool GetProfileName(const unsigned int profileId, std::string& name) const; |
| 174 |
|
| 175 |
std::string GetUserDataFolder() const; |
| 176 |
std::string GetProfileUserDataFolder() const; |
| 177 |
std::string GetDatabaseFolder() const; |
| 178 |
std::string GetCDDBFolder() const; |
| 179 |
std::string GetThumbnailsFolder() const; |
| 180 |
std::string GetVideoThumbFolder() const; |
| 181 |
std::string GetBookmarksThumbFolder() const; |
| 182 |
std::string GetLibraryFolder() const; |
| 183 |
std::string GetSavestatesFolder() const; |
| 184 |
std::string GetSettingsFile() const; |
| 185 |
|
| 186 |
// uses HasSlashAtEnd to determine if a directory or file was meant |
| 187 |
std::string GetUserDataItem(const std::string& strFile) const; |
| 188 |
|
| 189 |
// Event log access |
| 190 |
CEventLog &GetEventLog(); |
| 191 |
|
| 192 |
protected: |
| 193 |
// implementation of ISettingCallback |
| 194 |
void OnSettingAction(const std::shared_ptr<const CSetting>& setting) override; |
| 195 |
|
| 196 |
private: |
| 197 |
/*! \brief Set the current profile id and update the special://profile path |
| 198 |
\param profileId profile index |
| 199 |
*/ |
| 200 |
void SetCurrentProfileId(unsigned int profileId); |
| 201 |
|
| 202 |
void PrepareLoadProfile(unsigned int profileIndex); |
| 203 |
void FinalizeLoadProfile(); |
| 204 |
|
| 205 |
// Construction parameters |
| 206 |
std::shared_ptr<CSettings> m_settings; |
| 207 |
|
| 208 |
std::vector<CProfile> m_profiles; |
| 209 |
bool m_usingLoginScreen = false; |
| 210 |
bool m_profileLoadedForLogin = false; |
| 211 |
bool m_previousProfileLoadedForLogin = false; |
| 212 |
int m_autoLoginProfile = -1; |
| 213 |
unsigned int m_lastUsedProfile = 0; |
| 214 |
unsigned int m_currentProfile = |
| 215 |
0; // do not modify directly, use SetCurrentProfileId() function instead |
| 216 |
int m_nextProfileId = |
| 217 |
0; // for tracking the next available id to give to a new profile to ensure id's are not re-used |
| 218 |
mutable CCriticalSection m_critical; |
| 219 |
|
| 220 |
// Event properties |
| 221 |
std::unique_ptr<CEventLogManager> m_eventLogs; |
| 222 |
}; |