enable persisting of posted settings to a JSON file

This commit is contained in:
Stephen Birarda 2014-06-25 11:20:09 -07:00
parent 11f23ca6f6
commit f8dccad6ec
6 changed files with 116 additions and 15 deletions

View file

@ -1,13 +1,11 @@
{
"groups": {
"audio": {
"label": "Audio",
"settings": {
"unattenuated-zone": {
"label": "Unattenuated Zone",
"help": "Boxes for source and listener (corner x, corner y, corner z, size x, size y, size z, corner x, corner y, corner z, size x, size y, size z)",
"placeholder": "0,0,0,20,20,20,50,50,50,10,10,10"
}
"audio": {
"label": "Audio",
"settings": {
"unattenuated-zone": {
"label": "Unattenuated Zone",
"help": "Boxes for source and listener (corner x, corner y, corner z, size x, size y, size z, corner x, corner y, corner z, size x, size y, size z)",
"placeholder": "0,0,0,20,20,20,50,50,50,10,10,10"
}
}
}

View file

@ -5,7 +5,7 @@
<form class="form-horizontal" id="settings-form" role="form">
<script id="template" type="text/x-handlebars-template">
{{#each groups}}
{{#each .}}
{{setKey @key}}
<div class="panel panel-default">
<div class="panel-heading">

View file

@ -41,7 +41,8 @@ DomainServer::DomainServer(int argc, char* argv[]) :
_oauthClientID(),
_hostname(),
_networkReplyUUIDMap(),
_sessionAuthenticationHash()
_sessionAuthenticationHash(),
_settingsManager()
{
setOrganizationName("High Fidelity");
setOrganizationDomain("highfidelity.io");
@ -1162,8 +1163,8 @@ bool DomainServer::handleHTTPRequest(HTTPConnection* connection, const QUrl& url
}
}
// didn't process the request, let our HTTPManager handle
return false;
// didn't process the request, let our DomainServerSettingsManager or HTTPManager handle
return _settingsManager.handleHTTPRequest(connection, url);
}
bool DomainServer::handleHTTPSRequest(HTTPSConnection* connection, const QUrl &url) {

View file

@ -24,6 +24,7 @@
#include <HTTPSConnection.h>
#include <LimitedNodeList.h>
#include "DomainServerSettingsManager.h"
#include "WalletTransaction.h"
#include "PendingAssignedNodeData.h"
@ -110,6 +111,8 @@ private:
QString _hostname;
QMap<QNetworkReply*, QUuid> _networkReplyUUIDMap;
QHash<QUuid, bool> _sessionAuthenticationHash;
DomainServerSettingsManager _settingsManager;
};
#endif // hifi_DomainServer_h

View file

@ -9,4 +9,91 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include "DomainServerSettingsManager.h"
#include <QtCore/QCoreApplication>
#include <QtCore/QFile>
#include <QtCore/QJsonObject>
#include <QtCore/QUrl>
#include <HTTPConnection.h>
#include "DomainServerSettingsManager.h"
const QString SETTINGS_DESCRIPTION_RELATIVE_PATH = "/resources/web/settings/describe.json";
DomainServerSettingsManager::DomainServerSettingsManager() :
_descriptionObject(),
_settingsMap()
{
// load the description object from the settings description
QFile descriptionFile(QCoreApplication::applicationDirPath() + SETTINGS_DESCRIPTION_RELATIVE_PATH);
descriptionFile.open(QIODevice::ReadOnly);
_descriptionObject = QJsonDocument::fromJson(descriptionFile.readAll()).object();
qDebug() << _descriptionObject;
}
bool DomainServerSettingsManager::handleHTTPRequest(HTTPConnection* connection, const QUrl &url) {
if (connection->requestOperation() == QNetworkAccessManager::PostOperation && url.path() == "/settings.json") {
// this is a POST operation to change one or more settings
QJsonDocument postedDocument = QJsonDocument::fromJson(connection->requestContent());
QJsonObject postedObject = postedDocument.object();
// we recurse one level deep below each group for the appropriate setting
recurseJSONObjectAndOverwriteSettings(postedObject, _settingsMap, _descriptionObject);
// now let's look at the settingsMap?
qDebug() << QJsonDocument::fromVariant(_settingsMap).toJson();
persistToFile();
return false;
}
return false;
}
const QString DESCRIPTION_SETTINGS_KEY = "settings";
void DomainServerSettingsManager::recurseJSONObjectAndOverwriteSettings(const QJsonObject& postedObject,
QVariantMap& settingsVariant,
QJsonObject descriptionObject) {
foreach(const QString& key, postedObject.keys()) {
QJsonValue rootValue = postedObject[key];
// we don't continue if this key is not present in our descriptionObject
if (descriptionObject.contains(key)) {
if (rootValue.isString()) {
// if this is a string then just set it in our settingsVariant
settingsVariant[key] = rootValue.toString();
} else if (rootValue.isObject()) {
QJsonObject nextDescriptionObject = descriptionObject[key].toObject();
if (nextDescriptionObject.contains(DESCRIPTION_SETTINGS_KEY)) {
if (!settingsVariant.contains(key)) {
// we don't have a map below this key yet, so set it up now
settingsVariant[key] = QVariantMap();
}
// there's a JSON Object to explore, so recurse into it
recurseJSONObjectAndOverwriteSettings(rootValue.toObject(),
*reinterpret_cast<QVariantMap*>(settingsVariant[key].data()),
nextDescriptionObject[DESCRIPTION_SETTINGS_KEY].toObject());
}
}
}
}
}
const QString SETTINGS_JSON_FILE_RELATIVE_PATH = "/resources/config.json";
void DomainServerSettingsManager::persistToFile() {
QFile settingsFile(QCoreApplication::applicationDirPath() + SETTINGS_JSON_FILE_RELATIVE_PATH);
if (settingsFile.open(QIODevice::WriteOnly)) {
settingsFile.write(QJsonDocument::fromVariant(_settingsMap).toJson());
} else {
qCritical("Could not write to JSON settings file. Unable to persist settings.");
}
}

View file

@ -14,8 +14,20 @@
#include <QtCore/QJsonDocument>
class DomainServerSettingsManager {
#include <HTTPManager.h>
class DomainServerSettingsManager : public QObject, HTTPRequestHandler {
Q_OBJECT
public:
DomainServerSettingsManager();
bool handleHTTPRequest(HTTPConnection* connection, const QUrl& url);
private:
void recurseJSONObjectAndOverwriteSettings(const QJsonObject& postedObject, QVariantMap& settingsVariant,
QJsonObject descriptionObject);
void persistToFile();
QJsonObject _descriptionObject;
QVariantMap _settingsMap;
};
#endif // hifi_DomainServerSettingsManager_h