settings: add wrapper for GetList()/SetList() to CSettings
[vuplus_xbmc] / xbmc / settings / Settings.h
1 #pragma once
2 /*
3  *      Copyright (C) 2005-2013 Team XBMC
4  *      http://xbmc.org
5  *
6  *  This Program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2, or (at your option)
9  *  any later version.
10  *
11  *  This Program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with XBMC; see the file COPYING.  If not, see
18  *  <http://www.gnu.org/licenses/>.
19  *
20  */
21
22 #include <set>
23 #include <string>
24
25 #include "settings/ISettingCallback.h"
26 #include "settings/ISettingControlCreator.h"
27 #include "settings/ISettingCreator.h"
28 #include "threads/CriticalSection.h"
29 #include "utils/Variant.h"
30
31 class CSetting;
32 class CSettingSection;
33 class CSettingsManager;
34 class TiXmlElement;
35 class TiXmlNode;
36
37 /*!
38  \brief Wrapper around CSettingsManager responsible for properly setting up
39  the settings manager and registering all the callbacks, handlers and custom
40  setting types.
41  \sa CSettingsManager
42  */
43 class CSettings : public ISettingCreator, public ISettingControlCreator
44 {
45 public:
46   /*!
47    \brief Creates a new settings wrapper around a new settings manager.
48
49    For access to the "global" settings wrapper the static Get() method should
50    be used.
51    */
52   CSettings();
53   virtual ~CSettings();
54
55   /*!
56    \brief Returns a "global" settings wrapper which can be used from anywhere.
57
58    \return "global" settings wrapper
59    */
60   static CSettings& Get();
61
62   // implementation of ISettingCreator
63   virtual CSetting* CreateSetting(const std::string &settingType, const std::string &settingId, CSettingsManager *settingsManager = NULL) const;
64
65   // implementation of ISettingControlCreator
66   virtual ISettingControl* CreateControl(const std::string &controlType) const;
67
68   /*!
69    \brief Initializes the setting system with the generic
70    settings definition and platform specific setting definitions.
71
72    \return True if the initialization was successful, false otherwise
73    */
74   bool Initialize();
75   /*!
76    \brief Loads the setting values.
77
78    \return True if the setting values are successfully loaded, false otherwise
79    */
80   bool Load();
81   /*!
82    \brief Loads setting values from the given (XML) file.
83
84    \param file Path to an XML file containing setting values
85    \return True if the setting values were successfully loaded, false otherwise
86    */
87   bool Load(const std::string &file);
88   /*!
89    \brief Loads setting values from the given XML element.
90
91    \param root XML element containing setting values
92    \param hide Whether to hide the loaded settings or not
93    \return True if the setting values were successfully loaded, false otherwise
94    */
95   bool Load(const TiXmlElement *root, bool hide = false);
96   /*!
97    \brief Tells the settings system that all setting values
98    have been loaded.
99
100    This manual trigger is necessary to enable the ISettingCallback methods
101    being executed.
102    */
103   void SetLoaded();
104   /*!
105    \brief Saves the setting values.
106
107    \return True if the setting values were successfully saved, false otherwise
108    */
109   bool Save();
110   /*!
111    \brief Saves the setting values to the given (XML) file.
112
113    \param file Path to an XML file
114    \return True if the setting values were successfully saved, false otherwise
115    */
116   bool Save(const std::string &file);
117   /*!
118    \brief Unloads the previously loaded setting values.
119
120    The values of all the settings are reset to their default values.
121    */
122   void Unload();
123   /*!
124    \brief Uninitializes the settings system.
125
126    Unregisters all previously registered callbacks and destroys all setting
127    objects.
128    */
129   void Uninitialize();
130
131   /*!
132    \brief Registers the given ISettingCallback implementation for the given
133    set of settings.
134
135    \param callback ISettingCallback implementation
136    \param settingList List of setting identifiers for which the given callback shall be triggered
137    */
138   void RegisterCallback(ISettingCallback *callback, const std::set<std::string> &settingList);
139   /*!
140    \brief Unregisters the given ISettingCallback implementation.
141
142    \param callback ISettingCallback implementation
143    */
144   void UnregisterCallback(ISettingCallback *callback);
145
146   /*!
147    \brief Gets the setting with the given identifier.
148
149    \param id Setting identifier
150    \return Setting object with the given identifier or NULL if the identifier is unknown
151    */
152   CSetting* GetSetting(const std::string &id) const;
153   /*!
154    \brief Gets the setting section with the given identifier.
155
156    \param section Setting section identifier
157    \return Setting section with the given identifier or NULL if the identifier is unknown
158    */
159   CSettingSection* GetSection(const std::string &section) const;
160
161   /*!
162    \brief Gets the boolean value of the setting with the given identifier.
163
164    \param id Setting identifier
165    \return Boolean value of the setting with the given identifier
166    */
167   bool GetBool(const std::string &id) const;
168   /*!
169    \brief Gets the integer value of the setting with the given identifier.
170
171    \param id Setting identifier
172    \return Integer value of the setting with the given identifier
173    */
174   int GetInt(const std::string &id) const;
175   /*!
176    \brief Gets the real number value of the setting with the given identifier.
177
178    \param id Setting identifier
179    \return Real number value of the setting with the given identifier
180    */
181   double GetNumber(const std::string &id) const;
182   /*!
183    \brief Gets the string value of the setting with the given identifier.
184
185    \param id Setting identifier
186    \return String value of the setting with the given identifier
187    */
188   std::string GetString(const std::string &id) const;
189   /*!
190    \brief Gets the values of the list setting with the given identifier.
191
192    \param id Setting identifier
193    \return List of values of the setting with the given identifier
194    */
195   std::vector<CVariant> GetList(const std::string &id) const;
196
197   /*!
198    \brief Sets the boolean value of the setting with the given identifier.
199
200    \param id Setting identifier
201    \param value Boolean value to set
202    \return True if setting the value was successful, false otherwise
203    */
204   bool SetBool(const std::string &id, bool value);
205   /*!
206    \brief Toggles the boolean value of the setting with the given identifier.
207
208    \param id Setting identifier
209    \return True if toggling the boolean value was successful, false otherwise
210    */
211   bool ToggleBool(const std::string &id);
212   /*!
213    \brief Sets the integer value of the setting with the given identifier.
214
215    \param id Setting identifier
216    \param value Integer value to set
217    \return True if setting the value was successful, false otherwise
218    */
219   bool SetInt(const std::string &id, int value);
220   /*!
221    \brief Sets the real number value of the setting with the given identifier.
222
223    \param id Setting identifier
224    \param value Real number value to set
225    \return True if setting the value was successful, false otherwise
226    */
227   bool SetNumber(const std::string &id, double value);
228   /*!
229    \brief Sets the string value of the setting with the given identifier.
230
231    \param id Setting identifier
232    \param value String value to set
233    \return True if setting the value was successful, false otherwise
234    */
235   bool SetString(const std::string &id, const std::string &value);
236   /*!
237    \brief Sets the values of the list setting with the given identifier.
238
239    \param id Setting identifier
240    \param value Values to set
241    \return True if setting the values was successful, false otherwise
242    */
243   bool SetList(const std::string &id, const std::vector<CVariant> &value);
244
245   /*!
246    \brief Loads the setting being represented by the given XML node with the
247    given identifier.
248
249    \param node XML node representing the setting to load
250    \param settingId Setting identifier
251    \return True if the setting was successfully loaded from the given XML node, false otherwise
252    */
253   bool LoadSetting(const TiXmlNode *node, const std::string &settingId);
254
255 private:
256   CSettings(const CSettings&);
257   CSettings const& operator=(CSettings const&);
258
259   bool Initialize(const std::string &file);
260   bool InitializeDefinitions();
261   void InitializeSettingTypes();
262   void InitializeControls();
263   void InitializeVisibility();
264   void InitializeDefaults();
265   void InitializeOptionFillers();
266   void InitializeConditions();
267   void InitializeISettingsHandlers();
268   void InitializeISubSettings();
269   void InitializeISettingCallbacks();
270   bool Reset();
271
272   bool m_initialized;
273   CSettingsManager *m_settingsManager;
274   CCriticalSection m_critical;
275 };