From 10f558d86395db2e4cef1ec220a6896630e9515e Mon Sep 17 00:00:00 2001 From: Jamie Newbon Date: Tue, 21 Apr 2020 15:53:53 +0100 Subject: [PATCH] #6538 Added ConfigWriter to centralized config file interaction --- src/gui/CMakeLists.txt | 2 +- src/gui/src/ConfigWriter.cpp | 73 ++++++++++++++++++++++++ src/gui/src/ConfigWriter.h | 104 +++++++++++++++++++++++++++++++++++ 3 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 src/gui/src/ConfigWriter.cpp create mode 100644 src/gui/src/ConfigWriter.h diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index bdb4d1f9d..2ea13189f 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -30,7 +30,7 @@ if (SYNERGY_ENTERPRISE) endif () if (WIN32) - set (LEGACY_GUI_RC_FILES res/win/Synergy.rc) + set (LEGACY_GUI_RC_FILES res/win/Synergy.rc src/ConfigWriter.cpp src/ConfigWriter.h) endif() add_executable (synergy WIN32 diff --git a/src/gui/src/ConfigWriter.cpp b/src/gui/src/ConfigWriter.cpp new file mode 100644 index 000000000..165c0fc7f --- /dev/null +++ b/src/gui/src/ConfigWriter.cpp @@ -0,0 +1,73 @@ +// +// Created by jamie on 21/04/2020. +// + +#include "ConfigWriter.h" + +ConfigWriter* ConfigWriter::s_pConfiguration = nullptr; + + +ConfigWriter *ConfigWriter::make() { + // Only one ConfigWriter can exist at any one time (Singolton) + if (!s_pConfiguration) { + s_pConfiguration = new ConfigWriter(); + } + return s_pConfiguration; +} + +ConfigWriter::ConfigWriter() { + +} + +void ConfigWriter::destroy() { + destroy(s_pConfiguration); +} + +ConfigWriter::~ConfigWriter() { + destroy(m_pSettingsCurrent); + destroy(m_pSettingsSystem); + destroy(m_pSettingsUser); +} + +template +void ConfigWriter::setSetting(const char *name, T value) { + +} + +QVariant ConfigWriter::loadSetting(const char *name, const QVariant &defaultValue) { + return QVariant(); +} + + +void ConfigWriter::setScope(ConfigWriter::Scope scope) { + if (m_CurrentScope != scope) + { + m_CurrentScope = scope; + switch (scope) + { + case User: + m_pSettingsCurrent = m_pSettingsUser; + break; + case System: + m_pSettingsCurrent = m_pSettingsSystem; + break; + } + } +} + +ConfigWriter::Scope ConfigWriter::getScope() const { + return m_CurrentScope; +} + +void ConfigWriter::gloablLoad() { + for(auto & i : m_pCallerList) { + i->loadSettings(); + } +} + +void ConfigWriter::globalSave() { + for(auto & i : m_pCallerList) { + i->saveSettings(); + } +} + diff --git a/src/gui/src/ConfigWriter.h b/src/gui/src/ConfigWriter.h new file mode 100644 index 000000000..bcf659b6b --- /dev/null +++ b/src/gui/src/ConfigWriter.h @@ -0,0 +1,104 @@ +/* + * synergy -- mouse and keyboard sharing utility + * Copyright (C) 2020 - 2020 Symless Ltd. + * + * This package is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * found in the file LICENSE that should have accompanied this file. + * + * This package is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef SYNERGY_CORE_CONFIGWRITER_H +#define SYNERGY_CORE_CONFIGWRITER_H + + +#include +#include + +///@brief This abstract class will be used by all classes that use the ConfigWriter +/// to allow global saving and loading +class ConfigBase { +public : + ConfigBase() = default; + virtual ~ConfigBase() = default; + + /// @brief The function that is called when the settings need to be loaded from file + virtual void loadSettings() = 0; + + /// @brief The function that is called when the settings need to be saved to file + virtual void saveSettings() = 0; +}; + +class ConfigWriter { + +public: + + /// @brief the public way to construct the configuration calls + static ConfigWriter* make(); + + /// @brief the public way to destroy the configuration class + static void destroy(); + + ~ConfigWriter(); + + ///@brief An Enumeration of all the scopes available + enum Scope { System, User}; + + /// @brief Sets the value of a setting + /// @param [in] name The Setting to be saved + /// @param [in] value The Value to be saved + template + void setSetting(const char* name, T value); + + /// @brief Loads a setting + /// @param [in] name The setting to be loaded + /// @param [in] defaultValue The default value of the setting + QVariant loadSetting(const char* name, const QVariant& defaultValue = QVariant()); + + /// @brief Changes the setting save and load location between System and User scope + /// @param [in] scope The scope to set + void setScope(Scope scope = User); + + /// @brief Get the current scope the settings are loading and save from. + /// @return Scope An enum defining the current scope + Scope getScope() const; + + /// @brief trigger a config load across all registered classes + void gloablLoad(); + + /// @brief trigger a config save across all registered classes + void globalSave(); + +protected: + + Scope m_CurrentScope = User; /// @brief The current scope of the settings + + QSettings* m_pSettingsCurrent = nullptr; /// @brief The currently active settings + QSettings* m_pSettingsUser = nullptr; /// @brief The user specific settings + QSettings* m_pSettingsSystem = nullptr; /// @brief The system wide settings + +private: + + /// @brief Contains a list all all classes that hook into the writer. + /// This allows all classes that save settings to be called an updated + /// on a save and reload by any other class + std::list m_pCallerList; + + /// @brief The constructor, as this is a singolton we want to control who can call the constructor + ConfigWriter(); + + /// @brief the pointer of the + static ConfigWriter* s_pConfiguration; + + /// @brief deletes pointers and sets the value to null + template static inline void destroy(T*& p) { delete p; p = 0; } +}; + + +#endif //SYNERGY_CORE_CONFIGWRITER_H