SettingsManager.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #pragma once
  2. #include <utils/Logger.h>
  3. #include <utils/settings.h>
  4. #include <utils/version.hpp>
  5. using namespace semver;
  6. // qt includes
  7. #include <QJsonObject>
  8. const int GLOABL_INSTANCE_ID = 255;
  9. class Hyperion;
  10. class SettingsTable;
  11. ///
  12. /// @brief Manage the settings read write from/to configuration file, on settings changed will emit a signal to update components accordingly
  13. ///
  14. class SettingsManager : public QObject
  15. {
  16. Q_OBJECT
  17. public:
  18. ///
  19. /// @brief Construct a settings manager and assign a hyperion instance
  20. /// @params instance Instance index of HyperionInstanceManager
  21. /// @params parent The parent hyperion instance
  22. ///
  23. SettingsManager(quint8 instance, QObject* parent = nullptr, bool readonlyMode = false);
  24. ///
  25. /// @brief Save a complete json configuration
  26. /// @param config The entire config object
  27. /// @param correct If true will correct json against schema before save
  28. /// @return True on success else false
  29. ///
  30. bool saveSettings(QJsonObject config, bool correct = false);
  31. ///
  32. /// @brief Restore a complete json configuration
  33. /// @param config The entire config object
  34. /// @param correct If true will correct json against schema before save
  35. /// @return True on success else false
  36. ///
  37. bool restoreSettings(QJsonObject config, bool correct = false);
  38. ///
  39. /// @brief get a single setting json from configuration
  40. /// @param type The settings::type from enum
  41. /// @return The requested json data as QJsonDocument
  42. ///
  43. QJsonDocument getSetting(settings::type type) const;
  44. ///
  45. /// @brief get the full settings object of this instance (with global settings)
  46. /// @return The requested json
  47. ///
  48. QJsonObject getSettings() const;
  49. signals:
  50. ///
  51. /// @brief Emits whenever a configuration part changed.
  52. /// @param type The settings type from enum
  53. /// @param data The data as QJsonDocument
  54. ///
  55. void settingsChanged(settings::type type, const QJsonDocument& data);
  56. private:
  57. ///
  58. /// @brief Add possible migrations steps for configuration here
  59. /// @param config The configuration object
  60. /// @return True when a migration has been triggered
  61. ///
  62. bool handleConfigUpgrade(QJsonObject& config);
  63. bool resolveConfigVersion(QJsonObject& config);
  64. /// Logger instance
  65. Logger* _log;
  66. /// Hyperion instance
  67. Hyperion* _hyperion;
  68. /// Instance number
  69. quint8 _instance;
  70. /// instance of database table interface
  71. SettingsTable* _sTable;
  72. /// the schema
  73. static QJsonObject schemaJson;
  74. /// the current configuration of this instance
  75. QJsonObject _qconfig;
  76. semver::version _configVersion;
  77. semver::version _previousVersion;
  78. bool _readonlyMode;
  79. };