1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
/*
* Copyright (C) 2016-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "SettingsBase.h"
#include "settings/SettingUtils.h"
#include "settings/SettingsValueXmlSerializer.h"
#include "settings/lib/Setting.h"
#include "settings/lib/SettingsManager.h"
#include "utils/Variant.h"
#include "utils/XBMCTinyXML.h"
#include <mutex>
#define SETTINGS_XML_ROOT "settings"
CSettingsBase::CSettingsBase()
: m_settingsManager(new CSettingsManager())
{ }
CSettingsBase::~CSettingsBase()
{
Uninitialize();
delete m_settingsManager;
}
bool CSettingsBase::Initialize()
{
std::unique_lock<CCriticalSection> lock(m_critical);
if (m_initialized)
return false;
// register custom setting types
InitializeSettingTypes();
// register custom setting controls
InitializeControls();
// option fillers and conditions need to be
// initialized before the setting definitions
InitializeOptionFillers();
InitializeConditions();
// load the settings definitions
if (!InitializeDefinitions())
return false;
InitializeVisibility();
InitializeDefaults();
m_settingsManager->SetInitialized();
InitializeISettingsHandlers();
InitializeISubSettings();
InitializeISettingCallbacks();
m_initialized = true;
return true;
}
bool CSettingsBase::IsInitialized() const
{
return m_initialized && m_settingsManager->IsInitialized();
}
bool CSettingsBase::LoadValuesFromXml(const CXBMCTinyXML& xml, bool& updated)
{
const TiXmlElement* xmlRoot = xml.RootElement();
if (xmlRoot == nullptr || xmlRoot->ValueStr() != SETTINGS_XML_ROOT)
return false;
return m_settingsManager->Load(xmlRoot, updated);
}
bool CSettingsBase::LoadValuesFromXml(const TiXmlElement* root, bool& updated)
{
if (root == nullptr)
return false;
return m_settingsManager->Load(root, updated);
}
bool CSettingsBase::LoadHiddenValuesFromXml(const TiXmlElement* root)
{
if (root == nullptr)
return false;
std::map<std::string, std::shared_ptr<CSetting>> loadedSettings;
bool updated;
// don't trigger events for hidden settings
bool success = m_settingsManager->Load(root, updated, false, &loadedSettings);
if (success)
{
for(std::map<std::string, std::shared_ptr<CSetting>>::const_iterator setting = loadedSettings.begin(); setting != loadedSettings.end(); ++setting)
setting->second->SetVisible(false);
}
return success;
}
void CSettingsBase::SetLoaded()
{
m_settingsManager->SetLoaded();
}
bool CSettingsBase::IsLoaded() const
{
return m_settingsManager->IsLoaded();
}
bool CSettingsBase::SaveValuesToXml(CXBMCTinyXML& xml) const
{
std::string serializedSettings;
auto xmlSerializer = std::make_unique<CSettingsValueXmlSerializer>();
if (!m_settingsManager->Save(xmlSerializer.get(), serializedSettings))
return false;
if (!xml.Parse(serializedSettings))
return false;
return true;
}
void CSettingsBase::Unload()
{
m_settingsManager->Unload();
}
void CSettingsBase::Uninitialize()
{
std::unique_lock<CCriticalSection> lock(m_critical);
if (!m_initialized)
return;
// unregister setting option fillers
UninitializeOptionFillers();
// unregister setting conditions
UninitializeConditions();
// unregister ISettingCallback implementations
UninitializeISettingCallbacks();
// cleanup the settings manager
m_settingsManager->Clear();
// unregister ISubSettings implementations
UninitializeISubSettings();
// unregister ISettingsHandler implementations
UninitializeISettingsHandlers();
m_initialized = false;
}
void CSettingsBase::RegisterCallback(ISettingCallback* callback, const std::set<std::string>& settingList)
{
m_settingsManager->RegisterCallback(callback, settingList);
}
void CSettingsBase::UnregisterCallback(ISettingCallback* callback)
{
m_settingsManager->UnregisterCallback(callback);
}
SettingPtr CSettingsBase::GetSetting(const std::string& id) const
{
if (id.empty())
return nullptr;
return m_settingsManager->GetSetting(id);
}
std::vector<std::shared_ptr<CSettingSection>> CSettingsBase::GetSections() const
{
return m_settingsManager->GetSections();
}
std::shared_ptr<CSettingSection> CSettingsBase::GetSection(const std::string& section) const
{
if (section.empty())
return nullptr;
return m_settingsManager->GetSection(section);
}
bool CSettingsBase::GetBool(const std::string& id) const
{
return m_settingsManager->GetBool(id);
}
bool CSettingsBase::SetBool(const std::string& id, bool value)
{
return m_settingsManager->SetBool(id, value);
}
bool CSettingsBase::ToggleBool(const std::string& id)
{
return m_settingsManager->ToggleBool(id);
}
int CSettingsBase::GetInt(const std::string& id) const
{
return m_settingsManager->GetInt(id);
}
bool CSettingsBase::SetInt(const std::string& id, int value)
{
return m_settingsManager->SetInt(id, value);
}
double CSettingsBase::GetNumber(const std::string& id) const
{
return m_settingsManager->GetNumber(id);
}
bool CSettingsBase::SetNumber(const std::string& id, double value)
{
return m_settingsManager->SetNumber(id, value);
}
std::string CSettingsBase::GetString(const std::string& id) const
{
return m_settingsManager->GetString(id);
}
bool CSettingsBase::SetString(const std::string& id, const std::string& value)
{
return m_settingsManager->SetString(id, value);
}
std::vector<CVariant> CSettingsBase::GetList(const std::string& id) const
{
std::shared_ptr<CSetting> setting = m_settingsManager->GetSetting(id);
if (setting == nullptr || setting->GetType() != SettingType::List)
return std::vector<CVariant>();
return CSettingUtils::GetList(std::static_pointer_cast<CSettingList>(setting));
}
bool CSettingsBase::SetList(const std::string& id, const std::vector<CVariant>& value)
{
std::shared_ptr<CSetting> setting = m_settingsManager->GetSetting(id);
if (setting == nullptr || setting->GetType() != SettingType::List)
return false;
return CSettingUtils::SetList(std::static_pointer_cast<CSettingList>(setting), value);
}
bool CSettingsBase::SetDefault(const std::string &id)
{
return m_settingsManager->SetDefault(id);
}
void CSettingsBase::SetDefaults()
{
m_settingsManager->SetDefaults();
}
bool CSettingsBase::InitializeDefinitionsFromXml(const CXBMCTinyXML& xml)
{
const TiXmlElement* root = xml.RootElement();
if (root == nullptr)
return false;
return m_settingsManager->Initialize(root);
}
|