- dont overwrite an invalid settings file in any case
- fix file writing
This commit is contained in:
parent
9dad927239
commit
5f429f4558
2 changed files with 27 additions and 6 deletions
|
@ -34,7 +34,7 @@ class ScStwSettings : public QObject
|
|||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit ScStwSettings(QObject *parent = nullptr);
|
||||
explicit ScStwSettings(QObject *parent = nullptr, bool overwriteFileOnErrors = true);
|
||||
|
||||
typedef QString(*keyToStringConverter)(int);
|
||||
typedef QVariant::Type(*keyToTypeConverter)(int);
|
||||
|
@ -104,6 +104,8 @@ protected:
|
|||
bool registerKeyLevelConverters(int keyLevel, keyToStringConverter, keyToTypeConverter);
|
||||
|
||||
private:
|
||||
bool fileIsReadonly;
|
||||
|
||||
QFile * settingsFile;
|
||||
QVariantMap settingsCache;
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
|
||||
#include "../headers/scstwsettings.h"
|
||||
|
||||
ScStwSettings::ScStwSettings(QObject *parent) : QObject(parent)
|
||||
ScStwSettings::ScStwSettings(QObject *parent, bool overwriteFileOnErrors) : QObject(parent)
|
||||
{
|
||||
#ifdef RASPI
|
||||
QString path = "/root/.config/ScStwBasestation";
|
||||
|
@ -30,14 +30,21 @@ ScStwSettings::ScStwSettings(QObject *parent) : QObject(parent)
|
|||
qFatal("[FATAL] Failed to create writable directory for settings at %s", qPrintable(path));
|
||||
|
||||
this->settingsFile = new QFile(path + "/settings.json");
|
||||
this->fileIsReadonly = false;
|
||||
|
||||
if(!this->settingsFile->open(QFile::ReadWrite))
|
||||
qFatal("[FATAL] Couldn't open settings file %s", qPrintable(path + "/settings.json"));
|
||||
|
||||
if(!this->loadSettingsFromFile() && this->settingsFile->size() != 0) {
|
||||
if(this->settingsFile->size() != 0 && !this->loadSettingsFromFile()) {
|
||||
if(overwriteFileOnErrors) {
|
||||
this->writeSettingsToFile();
|
||||
qWarning("[WARNING] Settings file (%s) was of invalid format and therefore overwritten!", qPrintable(path + "/settings.json"));
|
||||
}
|
||||
else {
|
||||
qWarning("[WARNING] Settings file (%s) was of invalid format!", qPrintable(path + "/settings.json"));
|
||||
this->fileIsReadonly = true;
|
||||
}
|
||||
}
|
||||
|
||||
connect(this, &ScStwSettings::settingChanged, this, &ScStwSettings::writeSettingsToFile);
|
||||
|
||||
|
@ -144,9 +151,15 @@ bool ScStwSettings::registerKeyLevelConverters(int keyLevel, keyToStringConverte
|
|||
*/
|
||||
|
||||
bool ScStwSettings::writeSettingsToFile() {
|
||||
if(this->fileIsReadonly) {
|
||||
qWarning() << "[WARNING][SETTINS] Cannot write changes to file! It has been marked readonly due to parse errors (see above).";
|
||||
return false;
|
||||
}
|
||||
|
||||
QJsonDocument doc = QJsonDocument::fromVariant(this->settingsCache);
|
||||
|
||||
// overwrite file
|
||||
this->settingsFile->resize(0);
|
||||
this->settingsFile->reset();
|
||||
this->settingsFile->write(doc.toJson(QJsonDocument::Indented));
|
||||
this->settingsFile->flush();
|
||||
|
@ -155,7 +168,13 @@ bool ScStwSettings::writeSettingsToFile() {
|
|||
}
|
||||
|
||||
bool ScStwSettings::loadSettingsFromFile() {
|
||||
QJsonDocument doc = QJsonDocument::fromJson(this->settingsFile->readAll());
|
||||
QJsonParseError error;
|
||||
QJsonDocument doc = QJsonDocument::fromJson(this->settingsFile->readAll(), &error);
|
||||
|
||||
if(error.error != QJsonParseError::NoError) {
|
||||
qWarning() << "[ERROR][SETTINGS] Error when parsing settings file: " << error.errorString() << " at offset " << error.offset;
|
||||
return false;
|
||||
}
|
||||
|
||||
if(doc.toVariant().type() != QVariant::Map)
|
||||
return false;
|
||||
|
|
Reference in a new issue