From 615ac508d65afa9625b9fe0acae84f781979597a Mon Sep 17 00:00:00 2001 From: Dorian Zedler Date: Fri, 24 Jul 2020 13:07:27 +0200 Subject: [PATCH] added type security to settings --- ScStwLibraries/headers/scstwsettings.h | 19 ++++++++++++++++++- ScStwLibraries/sources/scstwsettings.cpp | 11 ++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/ScStwLibraries/headers/scstwsettings.h b/ScStwLibraries/headers/scstwsettings.h index 71787a3..be0afb6 100644 --- a/ScStwLibraries/headers/scstwsettings.h +++ b/ScStwLibraries/headers/scstwsettings.h @@ -37,6 +37,7 @@ public: explicit ScStwSettings(QObject *parent = nullptr); typedef QString(*keyToStringConverter)(int); + typedef QVariant::Type(*keyToTypeConverter)(int); /*! * \brief The BaseStationSetting enum contains all settings of the base station that can be changed by a client @@ -79,11 +80,26 @@ public: return QMetaEnum::fromType().valueToKey(key); } + static QVariant::Type keyToType(int key) { + QMap types = { + {ReadySoundEnableSetting, QVariant::Bool}, + {ReadySoundDelaySetting, QVariant::Double}, + {AtYourMarksSoundEnableSetting, QVariant::Bool}, + {AtYourMarksSoundDelaySetting, QVariant::Double}, + {SoundVolumeSetting, QVariant::Double} + }; + + if(types.contains(BaseStationSetting(key))) + return types[BaseStationSetting(key)]; + + return QVariant::Invalid; + } + protected: virtual QVariant readSetting(QString key, int keyInt = -1, int keyLevel = -1); virtual bool writeSetting(QString key, QVariant value, int keyInt = -1,int keyLevel = -1); virtual bool setDefaultSetting(QString key, QVariant defaultValue, int keyInt,int keyLevel = -1); - bool registerKeyLevelHandler(int keyLevel, keyToStringConverter); + bool registerKeyLevelConverters(int keyLevel, keyToStringConverter, keyToTypeConverter); private: QFile * settingsFile; @@ -113,6 +129,7 @@ private: }; QMap keyToStringConverters; + QMap keyToTypeConverters; QMap> internalSettingHandlers; private slots: diff --git a/ScStwLibraries/sources/scstwsettings.cpp b/ScStwLibraries/sources/scstwsettings.cpp index 45844a0..81c843c 100644 --- a/ScStwLibraries/sources/scstwsettings.cpp +++ b/ScStwLibraries/sources/scstwsettings.cpp @@ -35,7 +35,7 @@ ScStwSettings::ScStwSettings(QObject *parent) : QObject(parent) connect(this, &ScStwSettings::settingChanged, this, &ScStwSettings::writeSettingsToFile); - this->registerKeyLevelHandler(ScStwSettings::KeyLevel, &ScStwSettings::keyToString); + this->registerKeyLevelConverters(ScStwSettings::KeyLevel, &ScStwSettings::keyToString, &ScStwSettings::keyToType); } QVariant ScStwSettings::readSetting(BaseStationSetting key) { @@ -78,6 +78,10 @@ QVariant ScStwSettings::readSetting(QString key, int keyInt, int keyLevel) { } bool ScStwSettings::writeSetting(QString key, QVariant value, int keyInt, int keyLevel) { + if(!this->keyToTypeConverters.contains(keyInt) || !value.convert(this->keyToTypeConverters[keyLevel](keyInt)) || value.type() == QVariant::Invalid) { + return false; + } + if(!this->settingsCache.contains(key)) this->settingsCache.insert(key, value); else if (this->settingsCache[key] == value) @@ -106,11 +110,12 @@ ScStwSetting * ScStwSettings::getSetting(int key, int keyLevel) { return this->internalSettingHandlers[keyLevel][key]; } -bool ScStwSettings::registerKeyLevelHandler(int keyLevel, keyToStringConverter converterFunction) { +bool ScStwSettings::registerKeyLevelConverters(int keyLevel, keyToStringConverter keyToStringConverterFunction, keyToTypeConverter keyToTypeConverterFunction) { if(this->keyToStringConverters.contains(keyLevel)) return false; - this->keyToStringConverters.insert(keyLevel, converterFunction); + this->keyToStringConverters.insert(keyLevel, keyToStringConverterFunction); + this->keyToTypeConverters.insert(keyLevel, keyToTypeConverterFunction); return true; }