xbmc
SettingsManager.h
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 "ISettingCallback.h"
12 #include "ISettingControlCreator.h"
13 #include "ISettingCreator.h"
14 #include "ISettingsHandler.h"
15 #include "ISettingsValueSerializer.h"
16 #include "Setting.h"
17 #include "SettingConditions.h"
18 #include "SettingDefinitions.h"
19 #include "SettingDependency.h"
20 #include "threads/SharedSection.h"
21 #include "utils/logtypes.h"
22 
23 #include <map>
24 #include <set>
25 #include <unordered_set>
26 #include <vector>
27 
28 class CSettingCategory;
29 class CSettingGroup;
30 class CSettingSection;
31 class CSettingUpdate;
32 
33 class TiXmlElement;
34 class TiXmlNode;
35 
43  private ISettingCallback,
44  private ISettingsHandler
45 {
46 public:
51  ~CSettingsManager() override;
52 
53  static const uint32_t Version;
54  static const uint32_t MinimumSupportedVersion;
55 
56  // implementation of ISettingCreator
57  std::shared_ptr<CSetting> CreateSetting(const std::string &settingType, const std::string &settingId, CSettingsManager *settingsManager = nullptr) const override;
58 
59  // implementation of ISettingControlCreator
60  std::shared_ptr<ISettingControl> CreateControl(const std::string &controlType) const override;
61 
62  uint32_t GetVersion() const { return Version; }
63  uint32_t GetMinimumSupportedVersion() const { return MinimumSupportedVersion; }
64 
71  uint32_t ParseVersion(const TiXmlElement* root) const;
72 
80  bool Initialize(const TiXmlElement *root);
90  bool Load(const TiXmlElement *root, bool &updated, bool triggerEvents = true, std::map<std::string, std::shared_ptr<CSetting>> *loadedSettings = nullptr);
97  bool Save(const ISettingsValueSerializer* serializer, std::string& serializedValues) const;
103  void Unload();
111  void Clear();
112 
121  bool LoadSetting(const TiXmlNode *node, const std::string &settingId);
122 
132  bool LoadSetting(const TiXmlNode *node, const std::string &settingId, bool &updated);
133 
140  void SetInitialized();
144  bool IsInitialized() const { return m_initialized; }
152  void SetLoaded() { m_loaded = true; }
156  bool IsLoaded() const { return m_loaded; }
157 
164  void AddSection(const std::shared_ptr<CSettingSection>& section);
165 
184  bool AddSetting(const std::shared_ptr<CSetting>& setting,
185  const std::shared_ptr<CSettingSection>& section,
186  const std::shared_ptr<CSettingCategory>& category,
187  const std::shared_ptr<CSettingGroup>& group);
188 
196  void RegisterCallback(ISettingCallback *callback, const std::set<std::string> &settingList);
202  void UnregisterCallback(ISettingCallback *callback);
203 
215  void RegisterSettingType(const std::string &settingType, ISettingCreator *settingCreator);
216 
228  void RegisterSettingControl(const std::string &controlType, ISettingControlCreator *settingControlCreator);
229 
236  void RegisterSettingsHandler(ISettingsHandler *settingsHandler, bool bFront = false);
242  void UnregisterSettingsHandler(ISettingsHandler *settingsHandler);
243 
250  void RegisterSettingOptionsFiller(const std::string &identifier, IntegerSettingOptionsFiller optionsFiller);
257  void RegisterSettingOptionsFiller(const std::string &identifier, StringSettingOptionsFiller optionsFiller);
263  void UnregisterSettingOptionsFiller(const std::string &identifier);
271  void* GetSettingOptionsFiller(const std::shared_ptr<const CSetting>& setting);
272 
277  bool HasSettings() const;
278 
285  std::shared_ptr<CSetting> GetSetting(const std::string &id) const;
291  std::vector<std::shared_ptr<CSettingSection>> GetSections() const;
298  std::shared_ptr<CSettingSection> GetSection(std::string section) const;
311  SettingDependencyMap GetDependencies(const std::string &id) const;
323  SettingDependencyMap GetDependencies(const std::shared_ptr<const CSetting>& setting) const;
324 
331  bool GetBool(const std::string &id) const;
338  int GetInt(const std::string &id) const;
345  double GetNumber(const std::string &id) const;
352  std::string GetString(const std::string &id) const;
359  std::vector< std::shared_ptr<CSetting> > GetList(const std::string &id) const;
360 
368  bool SetBool(const std::string &id, bool value);
375  bool ToggleBool(const std::string &id);
383  bool SetInt(const std::string &id, int value);
391  bool SetNumber(const std::string &id, double value);
399  bool SetString(const std::string &id, const std::string &value);
407  bool SetList(const std::string &id, const std::vector< std::shared_ptr<CSetting> > &value);
408 
415  bool SetDefault(const std::string &id);
419  void SetDefaults();
420 
426  const CSettingConditionsManager& GetConditions() const { return m_conditions; }
435  void AddCondition(const std::string &condition);
446  void AddDynamicCondition(const std::string &identifier, SettingConditionCheck condition, void *data = nullptr);
447 
453  void RemoveDynamicCondition(const std::string &identifier);
454 
455 private:
456  // implementation of ISettingCallback
457  bool OnSettingChanging(const std::shared_ptr<const CSetting>& setting) override;
458  void OnSettingChanged(const std::shared_ptr<const CSetting>& setting) override;
459  void OnSettingAction(const std::shared_ptr<const CSetting>& setting) override;
460  bool OnSettingUpdate(const std::shared_ptr<CSetting>& setting,
461  const char* oldSettingId,
462  const TiXmlNode* oldSettingNode) override;
463  void OnSettingPropertyChanged(const std::shared_ptr<const CSetting>& setting,
464  const char* propertyName) override;
465 
466  // implementation of ISettingsHandler
467  bool OnSettingsLoading() override;
468  void OnSettingsLoaded() override;
469  void OnSettingsUnloaded() override;
470  bool OnSettingsSaving() const override;
471  void OnSettingsSaved() const override;
472  void OnSettingsCleared() override;
473 
474  bool Serialize(TiXmlNode *parent) const;
475  bool Deserialize(const TiXmlNode *node, bool &updated, std::map<std::string, std::shared_ptr<CSetting>> *loadedSettings = nullptr);
476 
477  bool LoadSetting(const TiXmlNode* node, const std::shared_ptr<CSetting>& setting, bool& updated);
478  bool UpdateSetting(const TiXmlNode* node,
479  const std::shared_ptr<CSetting>& setting,
480  const CSettingUpdate& update);
481  void UpdateSettingByDependency(const std::string &settingId, const CSettingDependency &dependency);
482  void UpdateSettingByDependency(const std::string &settingId, SettingDependencyType dependencyType);
483 
484  void AddSetting(const std::shared_ptr<CSetting>& setting);
485 
486  void ResolveReferenceSettings(const std::shared_ptr<CSettingSection>& section);
487  void CleanupIncompleteSettings();
488 
489  enum class SettingOptionsFillerType {
490  Unknown = 0,
491  Integer,
492  String
493  };
494 
495  void RegisterSettingOptionsFiller(const std::string &identifier, void *filler, SettingOptionsFillerType type);
496 
497  using CallbackSet = std::set<ISettingCallback *>;
498  struct Setting {
499  std::shared_ptr<CSetting> setting;
500  SettingDependencyMap dependencies;
501  std::set<std::string> children;
502  CallbackSet callbacks;
503  std::unordered_set<std::string> references;
504  };
505 
506  using SettingMap = std::map<std::string, Setting>;
507 
508  void ResolveSettingDependencies(const std::shared_ptr<CSetting>& setting);
509  void ResolveSettingDependencies(const Setting& setting);
510 
511  SettingMap::const_iterator FindSetting(std::string settingId) const;
512  SettingMap::iterator FindSetting(std::string settingId);
513  std::pair<SettingMap::iterator, bool> InsertSetting(std::string settingId, const Setting& setting);
514 
515  bool m_initialized = false;
516  bool m_loaded = false;
517 
518  SettingMap m_settings;
519  using SettingSectionMap = std::map<std::string, std::shared_ptr<CSettingSection>>;
520  SettingSectionMap m_sections;
521 
522  using SettingCreatorMap = std::map<std::string, ISettingCreator*>;
523  SettingCreatorMap m_settingCreators;
524 
525  using SettingControlCreatorMap = std::map<std::string, ISettingControlCreator*>;
526  SettingControlCreatorMap m_settingControlCreators;
527 
528  using SettingsHandlers = std::vector<ISettingsHandler*>;
529  SettingsHandlers m_settingsHandlers;
530 
531  CSettingConditionsManager m_conditions;
532 
533  struct SettingOptionsFiller {
534  void *filler;
535  SettingOptionsFillerType type;
536  };
537  using SettingOptionsFillerMap = std::map<std::string, SettingOptionsFiller>;
538  SettingOptionsFillerMap m_optionsFillers;
539 
540  mutable CSharedSection m_critical;
541  mutable CSharedSection m_settingsCritical;
542 
543  Logger m_logger;
544 };
void UnregisterSettingOptionsFiller(const std::string &identifier)
Unregisters the setting options filler registered under the given identifier.
Definition: SettingsManager.cpp:460
Category of groups of settings being part of a section.
Definition: SettingSection.h:85
std::shared_ptr< CSettingSection > GetSection(std::string section) const
Gets the setting section with the given identifier.
Definition: SettingsManager.cpp:549
std::shared_ptr< ISettingControl > CreateControl(const std::string &controlType) const override
Creates a new setting control of the given custom setting control type.
Definition: SettingsManager.cpp:986
bool IsLoaded() const
Returns whether the settings system has been loaded or not.
Definition: SettingsManager.h:156
void RegisterCallback(ISettingCallback *callback, const std::set< std::string > &settingList)
Registers the given ISettingCallback implementation to be triggered for the given list of settings...
Definition: SettingsManager.cpp:366
bool SetBool(const std::string &id, bool value)
Sets the boolean value of the setting with the given identifier.
Definition: SettingsManager.cpp:593
const CSettingConditionsManager & GetConditions() const
Gets the setting conditions manager used by the settings manager.
Definition: SettingsManager.h:426
CSettingsManager()
Creates a new (uninitialized) settings manager.
Definition: SettingsManager.cpp:56
bool GetBool(const std::string &id) const
Gets the boolean value of the setting with the given identifier.
Definition: SettingsManager.cpp:583
bool HasSettings() const
Checks whether any settings have been initialized.
Definition: SettingsManager.cpp:516
Definition: ISettingCallback.h:16
void RemoveDynamicCondition(const std::string &identifier)
Removes the given dynamic condition.
Definition: SettingsManager.cpp:729
bool AddSetting(const std::shared_ptr< CSetting > &setting, const std::shared_ptr< CSettingSection > &section, const std::shared_ptr< CSettingCategory > &category, const std::shared_ptr< CSettingGroup > &group)
Adds the given setting to the given group in the given category in the given section;.
Definition: SettingsManager.cpp:310
bool Initialize(const TiXmlElement *root)
Initializes the settings manager using the setting definitions represented by the given XML element...
Definition: SettingsManager.cpp:81
Interface for creating a new setting control of a custom setting control type.
Definition: ISettingControlCreator.h:20
bool SetInt(const std::string &id, int value)
Sets the integer value of the setting with the given identifier.
Definition: SettingsManager.cpp:623
Section of setting categories.
Definition: SettingSection.h:144
Definition: SettingConditions.h:82
Interface for creating a new setting of a custom setting type.
Definition: ISettingCreator.h:21
void Unload()
Unloads the previously loaded setting values.
Definition: SettingsManager.cpp:199
std::string GetString(const std::string &id) const
Gets the string value of the setting with the given identifier.
Definition: SettingsManager.cpp:653
void SetDefaults()
Sets the value of all settings to their default.
Definition: SettingsManager.cpp:704
void UnregisterSettingsHandler(ISettingsHandler *settingsHandler)
Unregisters the given ISettingsHandler implementation.
Definition: SettingsManager.cpp:433
void Clear()
Clears the complete settings manager.
Definition: SettingsManager.cpp:215
void RegisterSettingOptionsFiller(const std::string &identifier, IntegerSettingOptionsFiller optionsFiller)
Registers the given integer setting options filler under the given identifier.
Definition: SettingsManager.cpp:444
bool Save(const ISettingsValueSerializer *serializer, std::string &serializedValues) const
Saves the setting values using the given serializer.
Definition: SettingsManager.cpp:178
void UnregisterCallback(ISettingCallback *callback)
Unregisters the given ISettingCallback implementation.
Definition: SettingsManager.cpp:389
void * GetSettingOptionsFiller(const std::shared_ptr< const CSetting > &setting)
Gets the implementation of the setting options filler used by the given setting.
Definition: SettingsManager.cpp:466
bool SetString(const std::string &id, const std::string &value)
Sets the string value of the setting with the given identifier.
Definition: SettingsManager.cpp:663
void RegisterSettingsHandler(ISettingsHandler *settingsHandler, bool bFront=false)
Registers the given ISettingsHandler implementation.
Definition: SettingsManager.cpp:418
double GetNumber(const std::string &id) const
Gets the real number value of the setting with the given identifier.
Definition: SettingsManager.cpp:633
Definition: SettingUpdate.h:23
Definition: ISettingsValueSerializer.h:15
Definition: SmartPlayList.cpp:137
Interface defining methods being called by the settings system if an action is performed on multiple/...
Definition: ISettingsHandler.h:16
bool SetDefault(const std::string &id)
Sets the value of the setting to its default.
Definition: SettingsManager.cpp:693
void AddCondition(const std::string &condition)
Adds the given static condition.
Definition: SettingsManager.cpp:711
void AddDynamicCondition(const std::string &identifier, SettingConditionCheck condition, void *data=nullptr)
Adds the given dynamic condition.
Definition: SettingsManager.cpp:720
int GetInt(const std::string &id) const
Gets the integer value of the setting with the given identifier.
Definition: SettingsManager.cpp:613
A CSharedSection is a mutex that satisfies the Shared Lockable concept (see Lockables.h).
Definition: SharedSection.h:19
Settings manager responsible for initializing, loading and handling all settings. ...
Definition: SettingsManager.h:41
void RegisterSettingControl(const std::string &controlType, ISettingControlCreator *settingControlCreator)
Registers a custom setting control type and its ISettingControlCreator implementation.
Definition: SettingsManager.cpp:407
bool SetNumber(const std::string &id, double value)
Sets the real number value of the setting with the given identifier.
Definition: SettingsManager.cpp:643
Group of settings being part of a category.
Definition: SettingSection.h:28
std::shared_ptr< CSetting > GetSetting(const std::string &id) const
Gets the setting with the given identifier.
Definition: SettingsManager.cpp:521
bool IsInitialized() const
Returns whether the settings system has been initialized or not.
Definition: SettingsManager.h:144
std::shared_ptr< CSetting > CreateSetting(const std::string &settingType, const std::string &settingId, CSettingsManager *settingsManager=nullptr) const override
Creates a new setting of the given custom setting type.
Definition: SettingsManager.cpp:956
bool ToggleBool(const std::string &id)
Toggles the boolean value of the setting with the given identifier.
Definition: SettingsManager.cpp:603
Definition: SettingDependency.h:111
void SetInitialized()
Tells the settings system that the initialization is complete.
Definition: SettingsManager.cpp:248
uint32_t ParseVersion(const TiXmlElement *root) const
Try to get the version of the setting definitions/values represented by the given XML element...
Definition: SettingsManager.cpp:72
bool LoadSetting(const TiXmlNode *node, const std::string &settingId)
Loads the setting being represented by the given XML node with the given identifier.
Definition: SettingsManager.cpp:228
SettingDependencyMap GetDependencies(const std::string &id) const
Gets a map of settings (and their dependencies) which depend on the setting with the given identifier...
Definition: SettingsManager.cpp:565
std::vector< std::shared_ptr< CSettingSection > > GetSections() const
Gets the full list of setting sections.
Definition: SettingsManager.cpp:539
std::vector< std::shared_ptr< CSetting > > GetList(const std::string &id) const
Gets the values of the list setting with the given identifier.
Definition: SettingsManager.cpp:673
void SetLoaded()
Tells the settings system that all setting values have been loaded.
Definition: SettingsManager.h:152
bool SetList(const std::string &id, const std::vector< std::shared_ptr< CSetting > > &value)
Sets the values of the list setting with the given identifier.
Definition: SettingsManager.cpp:683
void RegisterSettingType(const std::string &settingType, ISettingCreator *settingCreator)
Registers a custom setting type and its ISettingCreator implementation.
Definition: SettingsManager.cpp:396
void AddSection(const std::shared_ptr< CSettingSection > &section)
Adds the given section, its categories, groups and settings.
Definition: SettingsManager.cpp:268
bool Load(const TiXmlElement *root, bool &updated, bool triggerEvents=true, std::map< std::string, std::shared_ptr< CSetting >> *loadedSettings=nullptr)
Loads setting values from the given XML element.
Definition: SettingsManager.cpp:141