diff --git a/ScStwLibraries/headers/scstwsettings.h b/ScStwLibraries/headers/scstwsettings.h index 282a603..2231460 100644 --- a/ScStwLibraries/headers/scstwsettings.h +++ b/ScStwLibraries/headers/scstwsettings.h @@ -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; diff --git a/ScStwLibraries/sources/scstwsettings.cpp b/ScStwLibraries/sources/scstwsettings.cpp index d0b21b9..2178cba 100644 --- a/ScStwLibraries/sources/scstwsettings.cpp +++ b/ScStwLibraries/sources/scstwsettings.cpp @@ -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,13 +30,20 @@ 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) { - this->writeSettingsToFile(); - qWarning("[WARNING] Settings file (%s) was of invalid format and therefore overwritten!", qPrintable(path + "/settings.json")); + 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;