#6538 Added ConfigWriter to centralized config file interaction

This commit is contained in:
Jamie Newbon 2020-04-21 15:53:53 +01:00
parent d061eac117
commit 10f558d863
3 changed files with 178 additions and 1 deletions

View file

@ -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

View file

@ -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<typename T>
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();
}
}

104
src/gui/src/ConfigWriter.h Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef SYNERGY_CORE_CONFIGWRITER_H
#define SYNERGY_CORE_CONFIGWRITER_H
#include <QVariant>
#include <QSettings>
///@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 <typename T>
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<ConfigBase*> 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<class T> static inline void destroy(T*& p) { delete p; p = 0; }
};
#endif //SYNERGY_CORE_CONFIGWRITER_H