don't cache settings... use the settings file directly

pull/135/head
Michael Zanetti 2017-09-14 13:57:11 +02:00
parent b3ea0d1a1b
commit c40a9eaa45
2 changed files with 35 additions and 52 deletions

View File

@ -30,18 +30,21 @@ namespace guhserver {
GuhConfiguration::GuhConfiguration(QObject *parent) :
QObject(parent)
{
// Load guhd settings
GuhSettings settings(GuhSettings::SettingsRoleGlobal);
settings.beginGroup("guhd");
m_serverName = settings.value("name", "guhIO").toString();
m_timeZone = settings.value("timeZone", QTimeZone::systemTimeZoneId()).toByteArray();
m_locale = QLocale(settings.value("language", "en_US").toString());
m_serverUuid = settings.value("uuid", QUuid()).toUuid();
if (m_serverUuid.isNull()) {
m_serverUuid = QUuid::createUuid();
settings.setValue("uuid", m_serverUuid);
// Init server uuid if we don't have one.
QUuid id = serverUuid();
if (id.isNull()) {
id = QUuid::createUuid();
setServerUuid(id);
}
settings.endGroup();
// Make sure default values are in configuration file so that it's easier for users to modify
setServerName(serverName());
setTimeZone(timeZone());
setLocale(locale());
setBluetoothServerEnabled(bluetoothServerEnabled());
setSslCertificate(sslCertificate(), sslCertificateKey());
GuhSettings settings(GuhSettings::SettingsRoleGlobal);
#ifndef TESTING_ENABLED
// TcpServer
@ -168,26 +171,20 @@ GuhConfiguration::GuhConfiguration(QObject *parent) :
wssConfig.authenticationEnabled = true;
m_webSocketServerConfigs[wssConfig.id] = wssConfig;
#endif
// Bluetooth server
settings.beginGroup("BluetoothServer");
setBluetoothServerEnabled(settings.value("enabled", false).toBool());
settings.endGroup();
// SSL configuration
settings.beginGroup("SSL");
setSslCertificate(settings.value("certificate", "/etc/ssl/certs/guhd-certificate.crt").toString(), settings.value("certificate-key", "/etc/ssl/certs/guhd-certificate.key").toString());
settings.endGroup();
}
QUuid GuhConfiguration::serverUuid() const
{
return m_serverUuid;
GuhSettings settings(GuhSettings::SettingsRoleGlobal);
settings.beginGroup("guhd");
return settings.value("uuid", QUuid()).toUuid();
}
QString GuhConfiguration::serverName() const
{
return m_serverName;
GuhSettings settings(GuhSettings::SettingsRoleGlobal);
settings.beginGroup("guhd");
return settings.value("name", "guhIO").toString();
}
void GuhConfiguration::setServerName(const QString &serverName)
@ -198,14 +195,14 @@ void GuhConfiguration::setServerName(const QString &serverName)
settings.beginGroup("guhd");
settings.setValue("name", serverName);
settings.endGroup();
m_serverName = serverName;
emit serverNameChanged();
}
QByteArray GuhConfiguration::timeZone() const
{
return m_timeZone;
GuhSettings settings(GuhSettings::SettingsRoleGlobal);
settings.beginGroup("guhd");
return settings.value("timeZone", QTimeZone::systemTimeZoneId()).toByteArray();
}
void GuhConfiguration::setTimeZone(const QByteArray &timeZone)
@ -216,14 +213,14 @@ void GuhConfiguration::setTimeZone(const QByteArray &timeZone)
settings.beginGroup("guhd");
settings.setValue("timeZone", timeZone);
settings.endGroup();
m_timeZone = timeZone;
emit timeZoneChanged();
}
QLocale GuhConfiguration::locale() const
{
return m_locale;
GuhSettings settings(GuhSettings::SettingsRoleGlobal);
settings.beginGroup("guhd");
return settings.value("language", "en_US").toString();
}
void GuhConfiguration::setLocale(const QLocale &locale)
@ -234,8 +231,6 @@ void GuhConfiguration::setLocale(const QLocale &locale)
settings.beginGroup("guhd");
settings.setValue("language", locale.name());
settings.endGroup();
m_locale = locale;
emit localeChanged();
}
@ -308,7 +303,9 @@ void GuhConfiguration::removeWebSocketServerConfiguration(const QString &id)
bool GuhConfiguration::bluetoothServerEnabled() const
{
return m_bluetoothServerEnabled;
GuhSettings settings(GuhSettings::SettingsRoleGlobal);
settings.beginGroup("BluetoothServer");
return settings.value("enabled", false).toBool();
}
void GuhConfiguration::setBluetoothServerEnabled(const bool &enabled)
@ -319,19 +316,21 @@ void GuhConfiguration::setBluetoothServerEnabled(const bool &enabled)
settings.beginGroup("BluetoothServer");
settings.setValue("enabled", enabled);
settings.endGroup();
m_bluetoothServerEnabled = enabled;
emit bluetoothServerEnabled();
}
QString GuhConfiguration::sslCertificate() const
{
return m_sslCertificate;
GuhSettings settings(GuhSettings::SettingsRoleGlobal);
settings.beginGroup("SSL");
return settings.value("certificate", "/etc/ssl/certs/guhd-certificate.crt").toString();
}
QString GuhConfiguration::sslCertificateKey() const
{
return m_sslCertificateKey;
GuhSettings settings(GuhSettings::SettingsRoleGlobal);
settings.beginGroup("SSL");
return settings.value("certificate-key", "/etc/ssl/certs/guhd-certificate.key").toString();
}
void GuhConfiguration::setSslCertificate(const QString &sslCertificate, const QString &sslCertificateKey)
@ -343,10 +342,6 @@ void GuhConfiguration::setSslCertificate(const QString &sslCertificate, const QS
settings.setValue("certificate", sslCertificate);
settings.setValue("certificate-key", sslCertificateKey);
settings.endGroup();
m_sslCertificate = sslCertificate;
m_sslCertificateKey = sslCertificateKey;
emit sslCertificateChanged();
}
@ -358,8 +353,6 @@ void GuhConfiguration::setServerUuid(const QUuid &uuid)
settings.beginGroup("guhd");
settings.setValue("uuid", uuid);
settings.endGroup();
m_serverUuid = uuid;
}
void GuhConfiguration::storeServerConfig(const QString &group, const ServerConfiguration &config)

View File

@ -106,20 +106,10 @@ public:
void setSslCertificate(const QString &sslCertificate, const QString &sslCertificateKey);
private:
QUuid m_serverUuid;
QString m_serverName;
QByteArray m_timeZone;
QLocale m_locale;
QHash<QString, ServerConfiguration> m_tcpServerConfigs;
QHash<QString, WebServerConfiguration> m_webServerConfigs;
QHash<QString, ServerConfiguration> m_webSocketServerConfigs;
bool m_bluetoothServerEnabled;
QString m_sslCertificate;
QString m_sslCertificateKey;
void setServerUuid(const QUuid &uuid);
void setWebServerPublicFolder(const QString & path);