summaryrefslogtreecommitdiffstats
path: root/xbmc/settings/SettingsBase.h
blob: 209b6cc13a300726814819db045f18507b6601a5 (plain)
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
274
275
276
277
278
279
/*
 *  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.
 */

#pragma once

#include "settings/lib/ISettingCallback.h"
#include "threads/CriticalSection.h"

#include <set>
#include <string>
#include <vector>

class CSetting;
class CSettingSection;
class CSettingsManager;
class CVariant;
class CXBMCTinyXML;
class TiXmlElement;

/*!
 \brief Basic wrapper around CSettingsManager providing the framework for
 properly setting up the settings manager and registering all the callbacks,
 handlers and custom setting types.
 \sa CSettingsManager
 */
class CSettingsBase
{
public:
  virtual ~CSettingsBase();

  CSettingsManager* GetSettingsManager() const { return m_settingsManager; }

  /*!
   \brief Initializes the setting system with the generic
   settings definition and platform specific setting definitions.

   \return True if the initialization was successful, false otherwise
   */
  virtual bool Initialize();
  /*!
   \brief Returns whether the settings system has been initialized or not.
  */
  virtual bool IsInitialized() const;
  /*!
   \brief Loads the setting values.

   \return True if the setting values are successfully loaded, false otherwise
   */
  virtual bool Load() = 0;
  /*!
   \brief Tells the settings system that all setting values
   have been loaded.

   This manual trigger is necessary to enable the ISettingCallback methods
   being executed.
   */
  virtual void SetLoaded();
  /*!
   \brief Returns whether the settings system has been loaded or not.
   */
  virtual bool IsLoaded() const;
  /*!
   \brief Saves the setting values.

   \return True if the setting values were successfully saved, false otherwise
   */
  virtual bool Save() = 0;
  /*!
   \brief Unloads the previously loaded setting values.

   The values of all the settings are reset to their default values.
   */
  virtual void Unload();
  /*!
   \brief Uninitializes the settings system.

   Unregisters all previously registered callbacks and destroys all setting
   objects.
   */
  virtual void Uninitialize();

  /*!
   \brief Registers the given ISettingCallback implementation for the given
   set of settings.

   \param callback ISettingCallback implementation
   \param settingList List of setting identifiers for which the given callback shall be triggered
   */
  void RegisterCallback(ISettingCallback* callback, const std::set<std::string>& settingList);
  /*!
   \brief Unregisters the given ISettingCallback implementation.

   \param callback ISettingCallback implementation
   */
  void UnregisterCallback(ISettingCallback* callback);

  /*!
   \brief Gets the setting with the given identifier.

   \param id Setting identifier
   \return Setting object with the given identifier or NULL if the identifier is unknown
   */
  std::shared_ptr<CSetting> GetSetting(const std::string& id) const;
  /*!
   \brief Gets the full list of setting sections.

   \return List of setting sections
   */
  std::vector<std::shared_ptr<CSettingSection>> GetSections() const;
  /*!
   \brief Gets the setting section with the given identifier.

   \param section Setting section identifier
   \return Setting section with the given identifier or NULL if the identifier is unknown
   */
  std::shared_ptr<CSettingSection> GetSection(const std::string& section) const;

  /*!
   \brief Gets the boolean value of the setting with the given identifier.

   \param id Setting identifier
   \return Boolean value of the setting with the given identifier
   */
  bool GetBool(const std::string& id) const;
  /*!
   \brief Gets the integer value of the setting with the given identifier.

   \param id Setting identifier
   \return Integer value of the setting with the given identifier
   */
  int GetInt(const std::string& id) const;
  /*!
   \brief Gets the real number value of the setting with the given identifier.

   \param id Setting identifier
   \return Real number value of the setting with the given identifier
   */
  double GetNumber(const std::string& id) const;
  /*!
   \brief Gets the string value of the setting with the given identifier.

   \param id Setting identifier
   \return String value of the setting with the given identifier
   */
  std::string GetString(const std::string& id) const;
  /*!
   \brief Gets the values of the list setting with the given identifier.

   \param id Setting identifier
   \return List of values of the setting with the given identifier
   */
  std::vector<CVariant> GetList(const std::string& id) const;

  /*!
   \brief Sets the boolean value of the setting with the given identifier.

   \param id Setting identifier
   \param value Boolean value to set
   \return True if setting the value was successful, false otherwise
   */
  bool SetBool(const std::string& id, bool value);
  /*!
   \brief Toggles the boolean value of the setting with the given identifier.

   \param id Setting identifier
   \return True if toggling the boolean value was successful, false otherwise
   */
  bool ToggleBool(const std::string& id);
  /*!
   \brief Sets the integer value of the setting with the given identifier.

   \param id Setting identifier
   \param value Integer value to set
   \return True if setting the value was successful, false otherwise
   */
  bool SetInt(const std::string& id, int value);
  /*!
   \brief Sets the real number value of the setting with the given identifier.

   \param id Setting identifier
   \param value Real number value to set
   \return True if setting the value was successful, false otherwise
   */
  bool SetNumber(const std::string& id, double value);
  /*!
   \brief Sets the string value of the setting with the given identifier.

   \param id Setting identifier
   \param value String value to set
   \return True if setting the value was successful, false otherwise
   */
  bool SetString(const std::string& id, const std::string& value);
  /*!
   \brief Sets the values of the list setting with the given identifier.

   \param id Setting identifier
   \param value Values to set
   \return True if setting the values was successful, false otherwise
   */
  bool SetList(const std::string& id, const std::vector<CVariant>& value);

  /*!
  \brief Sets the value of the setting with the given identifier to its default.

  \param id Setting identifier
  \return True if setting the value to its default was successful, false otherwise
  */
  bool SetDefault(const std::string &id);
  /*!
  \brief Sets the value of all settings to their default.
  */
  void SetDefaults();

protected:
  CSettingsBase();

  virtual void InitializeSettingTypes() { }
  virtual void InitializeControls() { }
  virtual void InitializeOptionFillers() { }
  virtual void UninitializeOptionFillers() { }
  virtual void InitializeConditions() { }
  virtual void UninitializeConditions() { }
  virtual bool InitializeDefinitions() = 0;
  virtual void InitializeVisibility() { }
  virtual void InitializeDefaults() { }
  virtual void InitializeISettingsHandlers() { }
  virtual void UninitializeISettingsHandlers() { }
  virtual void InitializeISubSettings() { }
  virtual void UninitializeISubSettings() { }
  virtual void InitializeISettingCallbacks() { }
  virtual void UninitializeISettingCallbacks() { }

  bool InitializeDefinitionsFromXml(const CXBMCTinyXML& xml);

  /*!
  \brief Loads setting values from the given document in XML format

  \param xml Document in XML format from which the settings are loaded
  \param updated Output parameter indicating whether setting values had to be updated
  \return True if the setting values were successfully loaded, false otherwise
  */
  bool LoadValuesFromXml(const CXBMCTinyXML& xml, bool& updated);
  /*!
  \brief Saves the setting values in XML format to the given document.

  \param xml Document to save the setting values in XML format into
  \return True if the setting values were successfully saved, false otherwise
  */
  bool SaveValuesToXml(CXBMCTinyXML& xml) const;

  /*!
  \brief Loads setting values from the given XML element.

  \param root XML element containing setting values
  \param updated Output parameter indicating whether setting values had to be updated
  \return True if the setting values were successfully loaded, false otherwise
  */
  bool LoadValuesFromXml(const TiXmlElement* root, bool& updated);

  /*!
  \brief Loads hidden setting values from the given XML element.

  \param root XML element containing setting values
  \return True if the setting values were successfully loaded, false otherwise
  */
  bool LoadHiddenValuesFromXml(const TiXmlElement* root);

  bool m_initialized = false;
  CSettingsManager* m_settingsManager;
  mutable CCriticalSection m_critical;
private:
  CSettingsBase(const CSettingsBase&) = delete;
  CSettingsBase& operator=(const CSettingsBase&) = delete;
};