From 6c1ce8e65407452ce82f412fdd26d1367bf3ea38 Mon Sep 17 00:00:00 2001 From: Dorian Zedler Date: Fri, 10 Jul 2020 15:20:13 +0200 Subject: [PATCH] completed new settings structure and added class to manage remote settings --- ScStwLibraries/ScStwLibraries.pri | 2 + ScStwLibraries/headers/ScStw.hpp | 3 +- ScStwLibraries/headers/client/scstwclient.h | 4 +- .../headers/client/scstwremotesettings.h | 35 ++++++++++++ ScStwLibraries/headers/scstwsettings.h | 19 ++++--- ScStwLibraries/sources/client/scstwclient.cpp | 10 ++-- .../sources/client/scstwremotesettings.cpp | 53 +++++++++++++++++++ ScStwLibraries/sources/scstwlibraries.cpp | 3 +- ScStwLibraries/sources/scstwsettings.cpp | 17 ++++-- 9 files changed, 125 insertions(+), 21 deletions(-) create mode 100644 ScStwLibraries/headers/client/scstwremotesettings.h create mode 100644 ScStwLibraries/sources/client/scstwremotesettings.cpp diff --git a/ScStwLibraries/ScStwLibraries.pri b/ScStwLibraries/ScStwLibraries.pri index fbec086..a168177 100644 --- a/ScStwLibraries/ScStwLibraries.pri +++ b/ScStwLibraries/ScStwLibraries.pri @@ -19,6 +19,7 @@ INCLUDEPATH += $$PWD/headers $$PWD SOURCES += \ $$PWD/sources/ScStw.cpp \ + $$PWD/sources/client/scstwremotesettings.cpp \ $$PWD/sources/scstwsettings.cpp \ $$PWD/sources/scstwsoundplayer.cpp \ $$PWD/sources/scstwlibraries.cpp \ @@ -28,6 +29,7 @@ SOURCES += \ HEADERS += \ $$PWD/headers/ScStw.hpp \ $$PWD/headers/ScStwLibraries_global.h \ + $$PWD/headers/client/scstwremotesettings.h \ $$PWD/headers/scstwlibraries.h \ $$PWD/headers/scstwrace.h \ $$PWD/headers/scstwsettings.h \ diff --git a/ScStwLibraries/headers/ScStw.hpp b/ScStwLibraries/headers/ScStw.hpp index 497cfc0..11e5a4c 100644 --- a/ScStwLibraries/headers/ScStw.hpp +++ b/ScStwLibraries/headers/ScStw.hpp @@ -61,7 +61,8 @@ public: RaceStateChanged = 9000, TimersChanged = 9001, ExtensionsChanged = 9002, - NextStartActionChanged = 9003 /*, ProfilesChanged*/ + NextStartActionChanged = 9003, /*, ProfilesChanged*/ + SettingChanged = 9004 }; Q_ENUM(SignalKey) diff --git a/ScStwLibraries/headers/client/scstwclient.h b/ScStwLibraries/headers/client/scstwclient.h index df9c5f8..d3a3946 100644 --- a/ScStwLibraries/headers/client/scstwclient.h +++ b/ScStwLibraries/headers/client/scstwclient.h @@ -140,14 +140,14 @@ public slots: * \param value the value to write to * \return the status code returned by the command */ - ScStw::StatusCode writeRemoteSetting(ScStwSettings::BaseStationSetting key, QString value); + ScStw::StatusCode writeRemoteSetting(ScStwSettings::BaseStationSetting key, QVariant value); /*! * \brief Function to read a setting on the base station * \param key the key to read from * \return the value of the key or "false" if the key is not found or an error occured */ - QString readRemoteSetting(ScStwSettings::BaseStationSetting key); + QVariant readRemoteSetting(ScStwSettings::BaseStationSetting key); /*! Getter fuctions */ diff --git a/ScStwLibraries/headers/client/scstwremotesettings.h b/ScStwLibraries/headers/client/scstwremotesettings.h new file mode 100644 index 0000000..7f02d6e --- /dev/null +++ b/ScStwLibraries/headers/client/scstwremotesettings.h @@ -0,0 +1,35 @@ +#ifndef SCSTWREMOTESETTINGS_H +#define SCSTWREMOTESETTINGS_H + +#include +#include "scstwsettings.h" +#include "scstwclient.h" +#include + +class ScStwRemoteSettings : public ScStwSettings +{ + Q_OBJECT +public: + ScStwRemoteSettings(ScStwClient * scStwClient, QObject * parent = nullptr); + + enum SettingsMode { + LOCAL, + REMOTE + }; + +protected: + QVariant readSetting(QString key, int keyInt, int keyLevel); + bool writeSetting(QString key, QVariant value, int keyInt,int keyLevel = -1); + void setDefaultSetting(QString key, QVariant defaultVariant, int keyInt,int keyLevel = -1); + +private: + ScStwClient * scStwClient; + + SettingsMode getMode(); + +private slots: + void handleClientStateChange(); + void handleBaseStationSignal(ScStw::SignalKey key, QVariant data); +}; + +#endif // SCSTWREMOTESETTINGS_H diff --git a/ScStwLibraries/headers/scstwsettings.h b/ScStwLibraries/headers/scstwsettings.h index 8c1c655..7fca825 100644 --- a/ScStwLibraries/headers/scstwsettings.h +++ b/ScStwLibraries/headers/scstwsettings.h @@ -9,6 +9,7 @@ #include #include #include +#include class ScStwSettings : public QObject { @@ -32,12 +33,16 @@ public: AtYourMarksSoundDelaySetting, SoundVolumeSetting }; - Q_ENUM(BaseStationSetting) - QVariant readSetting(BaseStationSetting key); - bool writeSetting(BaseStationSetting key, QVariant value); - void setDefaultSetting(BaseStationSetting key, QVariant defaultVariant); + enum KeyLevelEnum { + KeyLevel = 0 + }; + Q_ENUM(KeyLevelEnum) + + Q_INVOKABLE QVariant readSetting(BaseStationSetting key); + Q_INVOKABLE bool writeSetting(BaseStationSetting key, QVariant value); + Q_INVOKABLE void setDefaultSetting(BaseStationSetting key, QVariant defaultVariant); static BaseStationSetting keyFromInt(int i) { QMetaEnum enumeration = QMetaEnum::fromType(); @@ -45,9 +50,9 @@ public: } protected: - QVariant readSetting(QString key); - bool writeSetting(QString key, QVariant value, int keyInt,int keyLevel = -1); - void setDefaultSetting(QString key, QVariant defaultVariant, int keyInt,int keyLevel = -1); + 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 void setDefaultSetting(QString key, QVariant defaultVariant, int keyInt,int keyLevel = -1); private: QFile * settingsFile; diff --git a/ScStwLibraries/sources/client/scstwclient.cpp b/ScStwLibraries/sources/client/scstwclient.cpp index 795ce4a..9e7d93c 100644 --- a/ScStwLibraries/sources/client/scstwclient.cpp +++ b/ScStwLibraries/sources/client/scstwclient.cpp @@ -392,15 +392,15 @@ void ScStwClient::handleSignal(QVariantMap data) { // --- helper functions --- // ------------------------ -ScStw::StatusCode ScStwClient::writeRemoteSetting(ScStwSettings::BaseStationSetting key, QString value) { +ScStw::StatusCode ScStwClient::writeRemoteSetting(ScStwSettings::BaseStationSetting key, QVariant value) { QJsonArray requestData; - requestData.append(key); - requestData.append(value); + requestData.append(int(key)); + requestData.append(QJsonValue::fromVariant(value)); return ScStw::StatusCode(this->sendCommand(3000, requestData)["status"].toInt()); } -QString ScStwClient::readRemoteSetting(ScStwSettings::BaseStationSetting key) { - QVariantMap reply = this->sendCommand(3001, key); +QVariant ScStwClient::readRemoteSetting(ScStwSettings::BaseStationSetting key) { + QVariantMap reply = this->sendCommand(3001, int(key)); if(reply["status"] != 200){ return "false"; } diff --git a/ScStwLibraries/sources/client/scstwremotesettings.cpp b/ScStwLibraries/sources/client/scstwremotesettings.cpp new file mode 100644 index 0000000..8879dea --- /dev/null +++ b/ScStwLibraries/sources/client/scstwremotesettings.cpp @@ -0,0 +1,53 @@ +#include "../../headers/client/scstwremotesettings.h" + +ScStwRemoteSettings::ScStwRemoteSettings(ScStwClient * scStwClient, QObject * parent) : ScStwSettings(parent) +{ + this->scStwClient = scStwClient; + + connect(this->scStwClient, &ScStwClient::gotSignal, this, &ScStwRemoteSettings::handleBaseStationSignal); +} + +ScStwRemoteSettings::SettingsMode ScStwRemoteSettings::getMode() { + if(this->scStwClient->getState() == ScStwClient::CONNECTED) + return ScStwRemoteSettings::REMOTE; + else + return ScStwRemoteSettings::LOCAL; +} + +QVariant ScStwRemoteSettings::readSetting(QString key, int keyInt, int keyLevel) { + if(this->getMode() == LOCAL || keyLevel > 0) + return ScStwSettings::readSetting(key, keyInt, keyLevel); + + return this->scStwClient->readRemoteSetting(ScStwSettings::BaseStationSetting(keyInt)); +} + +bool ScStwRemoteSettings::writeSetting(QString key, QVariant value, int keyInt, int keyLevel) { + if(this->getMode() == LOCAL || keyLevel > 0) + return ScStwSettings::writeSetting(key, value, keyInt, keyLevel); + + this->scStwClient->writeRemoteSetting(ScStwSettings::BaseStationSetting(keyInt), value); + emit this->settingChanged(keyInt, keyLevel); + + return true; +} + +void ScStwRemoteSettings::setDefaultSetting(QString key, QVariant defaultVariant, int keyInt, int keyLevel) { + if(this->getMode() == LOCAL || keyLevel > 0) + return ScStwSettings::setDefaultSetting(key, defaultVariant, keyInt, keyLevel); + + return; +} + +void ScStwRemoteSettings::handleClientStateChange() { + if(this->scStwClient->getState() == ScStwClient::DISCONNECTED) + emit this->settingChanged(-1, 0); +} + +void ScStwRemoteSettings::handleBaseStationSignal(ScStw::SignalKey key, QVariant data) { + switch (key) { + case ScStw::SettingChanged: + emit this->settingChanged(data.toMap()["key"].toInt(), 0); + default: + break; +} +} diff --git a/ScStwLibraries/sources/scstwlibraries.cpp b/ScStwLibraries/sources/scstwlibraries.cpp index af0228d..4470c31 100644 --- a/ScStwLibraries/sources/scstwlibraries.cpp +++ b/ScStwLibraries/sources/scstwlibraries.cpp @@ -26,7 +26,7 @@ ScStwLibraries::ScStwLibraries(QObject *parent) : QObject(parent) void ScStwLibraries::init() { #ifdef ScStwLibraries_QML qmlRegisterType("de.itsblue.ScStw", 2, 0, "ScStw"); - qRegisterMetaType("ScStw::BaseStationSetting"); + qRegisterMetaType("ScStwSettings::BaseStationSetting"); qRegisterMetaType("ScStw::SocketCommand"); qmlRegisterType("de.itsblue.ScStw", 2, 0, "ScStwRace"); @@ -34,6 +34,7 @@ void ScStwLibraries::init() { #ifdef ScStwLibraries_ClientLibs qmlRegisterType("de.itsblue.ScStw", 2, 0, "ScStwClient"); + qmlRegisterType("de.itsblue.ScStw", 2, 0, "ScStwSettings"); #endif #endif } diff --git a/ScStwLibraries/sources/scstwsettings.cpp b/ScStwLibraries/sources/scstwsettings.cpp index 5e33941..47a779e 100644 --- a/ScStwLibraries/sources/scstwsettings.cpp +++ b/ScStwLibraries/sources/scstwsettings.cpp @@ -2,10 +2,15 @@ ScStwSettings::ScStwSettings(QObject *parent) : QObject(parent) { - QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/settings.json"; - this->settingsFile = new QFile(path); + QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); + QDir dir(path); + this->settingsFile = new QFile(path+ "/settings.json"); + if(!this->settingsFile->exists()) + if(!dir.mkpath(path)) + qFatal("[FATAL] Couldn't create settings dir %s", qPrintable(path)); + if(!this->settingsFile->open(QFile::ReadWrite)) - qFatal("[FATAL] Couldn't open settings file %s", qPrintable(path)); + qFatal("[FATAL] Couldn't open settings file %s", qPrintable(path + "/settings.json")); if(!this->loadSettingsFromFile() && this->settingsFile->size() != 0) qFatal("[FATAL] Settings file (%s) is of invalid format!", qPrintable(path)); @@ -14,7 +19,7 @@ ScStwSettings::ScStwSettings(QObject *parent) : QObject(parent) } QVariant ScStwSettings::readSetting(BaseStationSetting key) { - return this->readSetting(QMetaEnum::fromType().valueToKey(int(key))); + return this->readSetting(QMetaEnum::fromType().valueToKey(int(key)), int(key), 0); } bool ScStwSettings::writeSetting(BaseStationSetting key, QVariant value) { @@ -25,7 +30,9 @@ void ScStwSettings::setDefaultSetting(BaseStationSetting key, QVariant defaultVa this->setDefaultSetting(QMetaEnum::fromType().valueToKey(int(key)), defaultVariant, int(key), 0); } -QVariant ScStwSettings::readSetting(QString key) { +QVariant ScStwSettings::readSetting(QString key, int keyInt, int keyLevel) { + Q_UNUSED(keyInt) + Q_UNUSED(keyLevel) return this->settingsCache[key]; }