mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-04 08:36:52 +08:00
refs #198, enabled settings context to read / write JSON settings from/to file
This commit is contained in:
@@ -6,15 +6,14 @@
|
||||
#ifndef BLACKCORE_CONTEXTSETTINGS_H
|
||||
#define BLACKCORE_CONTEXTSETTINGS_H
|
||||
|
||||
#include "blackcore/coreruntime.h"
|
||||
#include "blackcore/context.h"
|
||||
#include "blackcore/dbus_server.h"
|
||||
#include "blackcore/keyboard.h"
|
||||
#include "blackmisc/hwkeyboardkeylist.h"
|
||||
#include "blackmisc/statusmessagelist.h"
|
||||
#include "blackmisc/settingutilities.h"
|
||||
#include "blackmisc/setnetwork.h"
|
||||
|
||||
|
||||
#include "blackmisc/dbus.h"
|
||||
#include <QObject>
|
||||
#include <QVariant>
|
||||
|
||||
@@ -46,15 +45,15 @@ namespace BlackCore
|
||||
{}
|
||||
|
||||
public:
|
||||
|
||||
//! Service name
|
||||
static const QString &InterfaceName()
|
||||
{
|
||||
static QString s(BLACKCORE_CONTEXTSETTINGS_INTERFACENAME);
|
||||
return s;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Service path
|
||||
*/
|
||||
//! Service path
|
||||
static const QString &ObjectPath()
|
||||
{
|
||||
static QString s(BLACKCORE_CONTEXTSETTINGS_OBJECTPATH);
|
||||
@@ -94,9 +93,8 @@ namespace BlackCore
|
||||
//! Destructor
|
||||
virtual ~IContextSettings() {}
|
||||
|
||||
|
||||
/*!
|
||||
* \brief Handle value
|
||||
* Handle value
|
||||
* \param path where value belongs to
|
||||
* \param command what to do with value
|
||||
* \param value
|
||||
@@ -108,11 +106,8 @@ namespace BlackCore
|
||||
virtual BlackMisc::CStatusMessageList value(const QString &path, const QString &command, const QVariant &value) = 0;
|
||||
|
||||
signals:
|
||||
//! \brief Settings have been changed
|
||||
void changedSettings(SettingsType type);
|
||||
|
||||
//! \brief Network settings have been changed
|
||||
void changedNetworkSettings();
|
||||
//! Settings have been changed
|
||||
void changedSettings(uint type);
|
||||
|
||||
public slots:
|
||||
|
||||
@@ -121,6 +116,18 @@ namespace BlackCore
|
||||
|
||||
//! Hotkeys
|
||||
virtual BlackMisc::Hardware::CKeyboardKeyList getHotkeys() const = 0;
|
||||
|
||||
//! save settings
|
||||
virtual BlackMisc::CStatusMessage write() const = 0;
|
||||
|
||||
//! Read settings
|
||||
virtual BlackMisc::CStatusMessage read() = 0;
|
||||
|
||||
//! Read settings
|
||||
virtual QString getSettingsFileName() const = 0;
|
||||
|
||||
//! Settings as JSON string
|
||||
virtual QString getSettingsAsJsonString() const = 0;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,9 @@
|
||||
#include "context_runtime.h"
|
||||
|
||||
#include "blackmisc/settingutilities.h"
|
||||
#include "blackmisc/blackmiscfreefunctions.h"
|
||||
#include <QFile>
|
||||
#include <QJsonDocument>
|
||||
|
||||
using namespace BlackMisc;
|
||||
using namespace BlackMisc::Settings;
|
||||
@@ -21,19 +24,107 @@ namespace BlackCore
|
||||
*/
|
||||
CContextSettings::CContextSettings(CRuntimeConfig::ContextMode mode, CRuntime *parent) : IContextSettings(mode, parent) {}
|
||||
|
||||
/*
|
||||
* Read settings
|
||||
*/
|
||||
CStatusMessage CContextSettings::read()
|
||||
{
|
||||
// create some dummy settings
|
||||
// this would actually be reading the settings from disk ..
|
||||
if (!CSettingUtilities::initSettingsDirectory())
|
||||
{
|
||||
return CStatusMessage(CStatusMessage::TypeCore, CStatusMessage::SeverityError,
|
||||
QString("Cannot init directory: %1").arg(this->getSettingsDirectory()));
|
||||
}
|
||||
bool ok = false;
|
||||
QFile jsonFile(this->getSettingsFileName());
|
||||
QJsonObject obj;
|
||||
|
||||
this->m_settingsNetwork.setCurrentNetworkServer(CServer("Testserver", "Client project testserver", "vatsim-germany.org", 6809, CUser("guest", "Guest Client project", "", "guest")));
|
||||
this->m_settingsNetwork.addTrafficNetworkServer(this->m_settingsNetwork.getCurrentTrafficNetworkServer());
|
||||
this->m_settingsNetwork.addTrafficNetworkServer(CServer("Europe C2", "VATSIM Server", "88.198.19.202", 6809, CUser("vatsimid", "Black Client", "", "vatsimpw")));
|
||||
this->m_settingsNetwork.addTrafficNetworkServer(CServer("Europe CC", "VATSIM Server", "5.9.155.43", 6809, CUser("vatsimid", "Black Client", "", "vatsimpw")));
|
||||
this->m_settingsNetwork.addTrafficNetworkServer(CServer("UK", "VATSIM Server", "109.169.48.148", 6809, CUser("vatsimid", "Black Client", "", "vatsimpw")));
|
||||
this->m_settingsNetwork.addTrafficNetworkServer(CServer("USA-W", "VATSIM Server", "64.151.108.52", 6809, CUser("vatsimid", "Black Client", "", "vatsimpw")));
|
||||
if (jsonFile.open(QFile::ReadOnly))
|
||||
{
|
||||
QJsonDocument doc = QJsonDocument::fromJson(jsonFile.readAll());
|
||||
obj = doc.object();
|
||||
ok = true;
|
||||
}
|
||||
jsonFile.close();
|
||||
|
||||
// hotkeys
|
||||
this->m_hotkeys.initAsHotkeyList();
|
||||
// init network
|
||||
if (obj.contains(IContextSettings::PathNetworkSettings()))
|
||||
{
|
||||
this->m_settingsNetwork.fromJson(
|
||||
obj.value(IContextSettings::PathNetworkSettings()).toObject()
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
this->m_settingsNetwork.initDefaultValues();
|
||||
}
|
||||
|
||||
// init own members
|
||||
if (obj.contains(IContextSettings::PathHotkeys()))
|
||||
{
|
||||
this->m_hotkeys.fromJson(
|
||||
obj.value(IContextSettings::PathHotkeys()).toObject()
|
||||
);
|
||||
}
|
||||
this->m_hotkeys.initAsHotkeyList(false); // update missing parts
|
||||
|
||||
if (ok)
|
||||
{
|
||||
return CStatusMessage(CStatusMessage::TypeCore, CStatusMessage::SeverityInfo,
|
||||
QString("Read settings: %1").arg(this->getSettingsFileName()));
|
||||
}
|
||||
else
|
||||
{
|
||||
return CStatusMessage(CStatusMessage::TypeCore, CStatusMessage::SeverityError,
|
||||
QString("Problem reading settings: %1").arg(this->getSettingsFileName()));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Write settings
|
||||
*/
|
||||
CStatusMessage CContextSettings::write() const
|
||||
{
|
||||
if (!CSettingUtilities::initSettingsDirectory())
|
||||
{
|
||||
return CStatusMessage(CStatusMessage::TypeCore, CStatusMessage::SeverityError,
|
||||
QString("Cannot init directory: %1").arg(this->getSettingsDirectory()));
|
||||
}
|
||||
QFile jsonFile(this->getSettingsFileName());
|
||||
bool ok = false;
|
||||
if (jsonFile.open(QFile::WriteOnly))
|
||||
{
|
||||
QJsonDocument doc = this->toJsonDocument();
|
||||
ok = jsonFile.write(doc.toJson(QJsonDocument::Indented)) >= 0;
|
||||
jsonFile.close();
|
||||
}
|
||||
if (ok)
|
||||
{
|
||||
return CStatusMessage(CStatusMessage::TypeCore, CStatusMessage::SeverityInfo,
|
||||
QString("Written settings: %1").arg(this->getSettingsFileName()));
|
||||
}
|
||||
else
|
||||
{
|
||||
return CStatusMessage(CStatusMessage::TypeCore, CStatusMessage::SeverityError,
|
||||
QString("Problem writing settings: %1").arg(this->getSettingsFileName()));
|
||||
}
|
||||
}
|
||||
|
||||
QString CContextSettings::getSettingsAsJsonString() const
|
||||
{
|
||||
QJsonDocument doc = this->toJsonDocument();
|
||||
return QString(doc.toJson(QJsonDocument::Indented));
|
||||
}
|
||||
|
||||
/*
|
||||
* JSON document
|
||||
*/
|
||||
QJsonDocument CContextSettings::toJsonDocument() const
|
||||
{
|
||||
QJsonObject obj;
|
||||
obj.insert(IContextSettings::PathNetworkSettings(), this->m_settingsNetwork.toJson());
|
||||
obj.insert(IContextSettings::PathHotkeys(), this->m_hotkeys.toJson());
|
||||
QJsonDocument doc(obj);
|
||||
return doc;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -69,9 +160,8 @@ namespace BlackCore
|
||||
{
|
||||
BlackMisc::Hardware::CKeyboardKeyList hotkeys = value.value<BlackMisc::Hardware::CKeyboardKeyList>();
|
||||
this->m_hotkeys = hotkeys;
|
||||
emit this->changedSettings(SettingsHotKeys);
|
||||
|
||||
msgs.push_back(CStatusMessage::getInfoMessage("set hotkeys"));
|
||||
msgs.push_back(this->write()); // write settings
|
||||
emit this->changedSettings(static_cast<uint>(SettingsHotKeys));
|
||||
return msgs;
|
||||
}
|
||||
}
|
||||
@@ -85,8 +175,8 @@ namespace BlackCore
|
||||
msgs = this->m_settingsNetwork.value(nextLevelPath, command, value, changed);
|
||||
if (changed)
|
||||
{
|
||||
emit this->changedNetworkSettings();
|
||||
emit this->changedSettings(SettingsNetwork);
|
||||
msgs.push_back(this->write());
|
||||
emit this->changedSettings(static_cast<uint>(SettingsNetwork));
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
#include "context_settings.h"
|
||||
#include "dbus_server.h"
|
||||
#include "coreruntime.h"
|
||||
#include "context_runtime.h"
|
||||
|
||||
#include "blackmisc/setnetwork.h"
|
||||
#include "blackmisc/statusmessagelist.h"
|
||||
@@ -61,11 +61,23 @@ namespace BlackCore
|
||||
*/
|
||||
virtual BlackMisc::CStatusMessageList value(const QString &path, const QString &command, QDBusVariant value, int unifiedBlackMetaType);
|
||||
|
||||
//! \brief read settings
|
||||
virtual BlackMisc::CStatusMessage read() override;
|
||||
|
||||
//! \copydoc IContextSettings::write
|
||||
virtual BlackMisc::CStatusMessage write() const override;
|
||||
|
||||
//! Settings file name
|
||||
virtual QString getSettingsFileName() const override { return BlackMisc::Settings::CSettingUtilities::getSettingsFile(); }
|
||||
|
||||
//! JSON represenation
|
||||
virtual QString getSettingsAsJsonString() const override;
|
||||
|
||||
private:
|
||||
friend class CRuntime;
|
||||
BlackMisc::Settings::CSettingsNetwork m_settingsNetwork;
|
||||
BlackMisc::Hardware::CKeyboardKeyList m_hotkeys;
|
||||
QJsonDocument toJsonDocument() const;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ namespace BlackCore
|
||||
void CContextSettingsProxy::relaySignals(const QString &serviceName, QDBusConnection &connection)
|
||||
{
|
||||
connection.connect(serviceName, IContextSettings::ObjectPath(), IContextSettings::InterfaceName(),
|
||||
"changedNetworkSettings", this, SIGNAL(changedNetworkSettings()));
|
||||
"changedSettings", this, SIGNAL(changedSettings(uint)));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -65,8 +65,35 @@ namespace BlackCore
|
||||
}
|
||||
|
||||
/*
|
||||
* Write settings
|
||||
*/
|
||||
BlackMisc::CStatusMessage CContextSettingsProxy::write() const
|
||||
{
|
||||
return this->m_dBusInterface->callDBusRet<BlackMisc::CStatusMessage>(QLatin1Literal("write"));
|
||||
}
|
||||
|
||||
/*
|
||||
* Read settings
|
||||
*/
|
||||
CStatusMessage CContextSettingsProxy::read()
|
||||
{
|
||||
return this->m_dBusInterface->callDBusRet<BlackMisc::CStatusMessage>(QLatin1Literal("read"));
|
||||
}
|
||||
|
||||
/*
|
||||
* File name
|
||||
*/
|
||||
QString CContextSettingsProxy::getSettingsFileName() const
|
||||
{
|
||||
return this->m_dBusInterface->callDBusRet<QString>(QLatin1Literal("getSettingsFileName"));
|
||||
}
|
||||
|
||||
/*
|
||||
* As JSON string
|
||||
*/
|
||||
QString CContextSettingsProxy::getSettingsAsJsonString() const
|
||||
{
|
||||
return this->m_dBusInterface->callDBusRet<QString>(QLatin1Literal("getSettingsAsJsonString"));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -57,6 +57,18 @@ namespace BlackCore
|
||||
//! \copydoc IContextSettings::value
|
||||
virtual BlackMisc::CStatusMessageList value(const QString &path, const QString &command, const QVariant &value) override;
|
||||
|
||||
//! \copydoc IContextSettings::write
|
||||
BlackMisc::CStatusMessage write() const override;
|
||||
|
||||
//! \brief read settings
|
||||
virtual BlackMisc::CStatusMessage read() override;
|
||||
|
||||
//! \brief settings file name
|
||||
virtual QString getSettingsFileName() const override;
|
||||
|
||||
//! \brief as JSON string
|
||||
virtual QString getSettingsAsJsonString() const override;
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user