This repository has been archived on 2024-06-03. You can view files and clone it, but cannot push or open issues or pull requests.
shared-libraries/ScStwLibraries/sources/client/scstwremotesettings.cpp

105 lines
3.9 KiB
C++
Raw Permalink Normal View History

2020-07-11 21:03:58 +02:00
/****************************************************************************
** ScStw Libraries
** Copyright (C) 2020 Itsblue development
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program 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/>.
****************************************************************************/
#include "../../headers/client/scstwremotesettings.h"
2020-10-09 15:18:56 +02:00
ScStwRemoteSettings::ScStwRemoteSettings(QObject* parent) : ScStwSettings(parent)
{
2020-10-09 15:18:56 +02:00
this->scStwClient = nullptr;
}
2020-10-09 15:18:56 +02:00
ScStwRemoteSettings::ScStwRemoteSettings(ScStwClient * scStwClient, QObject * parent) : ScStwSettings(parent)
{
this->setScStwClient(scStwClient);
}
ScStwRemoteSettings::SettingsMode ScStwRemoteSettings::getMode() {
2020-10-09 15:18:56 +02:00
if(this->scStwClient == nullptr || this->scStwClient->getState() != ScStwClient::CONNECTED)
return LOCAL;
else
2020-10-09 15:18:56 +02:00
return REMOTE;
}
QVariant ScStwRemoteSettings::readSetting(QString key, int keyInt, int keyLevel) {
if(this->getMode() == LOCAL || keyLevel > ScStwSettings::KeyLevel)
return ScStwSettings::readSetting(key, keyInt, keyLevel);
qDebug() << "Setting read: keyLevel: " << keyLevel << " key: " << key;
2021-01-09 23:12:28 +01:00
ScStw::StatusCode status;
QVariant value = this->scStwClient->readRemoteSetting(ScStwSettings::BaseStationSetting(keyInt), &status);
if(status == ScStw::Success)
return value;
else
return QVariant();
}
bool ScStwRemoteSettings::writeSetting(QString key, QVariant value, int keyInt, int keyLevel) {
if(this->getMode() == LOCAL || keyLevel > ScStwSettings::KeyLevel)
return ScStwSettings::writeSetting(key, value, keyInt, keyLevel);
qDebug() << "Setting write: keyLevel: " << keyLevel << " key: " << key << " value: " << value;
ScStw::StatusCode res = this->scStwClient->writeRemoteSetting(ScStwSettings::BaseStationSetting(keyInt), value);
return res == ScStw::Success;
}
bool ScStwRemoteSettings::setDefaultSetting(QString key, QVariant defaultVariant, int keyInt, int keyLevel) {
if(this->getMode() == LOCAL || keyLevel > ScStwSettings::KeyLevel)
return ScStwSettings::setDefaultSetting(key, defaultVariant, keyInt, keyLevel);
return false;
}
void ScStwRemoteSettings::handleClientStateChange() {
if(this->scStwClient->getState() == ScStwClient::DISCONNECTED)
emit this->settingChanged(-1, ScStwSettings::KeyLevel, QVariant());
2021-01-09 23:12:28 +01:00
// Dont't need to do that when changing to connected,
// as the basestation emits a wildcrd setting changed on connect anyway
}
void ScStwRemoteSettings::handleBaseStationSignal(ScStw::SignalKey key, QVariant data) {
switch (key) {
case ScStw::SettingChanged:
emit this->settingChanged(data.toMap()["key"].toInt(), ScStwSettings::KeyLevel, data.toMap()["value"]);
default:
break;
}
}
2020-10-09 15:18:56 +02:00
ScStwClient* ScStwRemoteSettings::getScStwClient() {
return this->scStwClient;
}
void ScStwRemoteSettings::setScStwClient(ScStwClient* client) {
if(client == this->scStwClient)
return;
this->scStwClient = client;
if(this->scStwClient != nullptr) {
this->scStwClient->addSignalSubscription(ScStw::SettingChanged);
connect(this->scStwClient, &ScStwClient::stateChanged, this, &ScStwRemoteSettings::handleClientStateChange);
2020-10-09 15:18:56 +02:00
connect(this->scStwClient, &ScStwClient::gotSignal, this, &ScStwRemoteSettings::handleBaseStationSignal);
}
}