mirror of
https://github.com/overte-org/overte.git
synced 2025-08-06 19:59:28 +02:00
Merge pull request #8057 from jherico/settings
Fixing issues with unclosed groups in settings persistence
This commit is contained in:
commit
55330f2205
7 changed files with 68 additions and 18 deletions
|
@ -173,6 +173,7 @@ void AccountManager::setAuthURL(const QUrl& authURL) {
|
||||||
<< "from previous settings file";
|
<< "from previous settings file";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
settings.endGroup();
|
||||||
|
|
||||||
if (_accountInfo.getAccessToken().token.isEmpty()) {
|
if (_accountInfo.getAccessToken().token.isEmpty()) {
|
||||||
qCWarning(networking) << "Unable to load account file. No existing account settings will be loaded.";
|
qCWarning(networking) << "Unable to load account file. No existing account settings will be loaded.";
|
||||||
|
|
|
@ -334,6 +334,7 @@ void ScriptEngines::clearScripts() {
|
||||||
Settings settings;
|
Settings settings;
|
||||||
settings.beginWriteArray(SETTINGS_KEY);
|
settings.beginWriteArray(SETTINGS_KEY);
|
||||||
settings.remove("");
|
settings.remove("");
|
||||||
|
settings.endArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ScriptEngines::saveScripts() {
|
void ScriptEngines::saveScripts() {
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
|
|
||||||
const QString Settings::firstRun { "firstRun" };
|
const QString Settings::firstRun { "firstRun" };
|
||||||
|
|
||||||
|
|
||||||
Settings::Settings() :
|
Settings::Settings() :
|
||||||
_manager(DependencyManager::get<Setting::Manager>()),
|
_manager(DependencyManager::get<Setting::Manager>()),
|
||||||
_locker(&(_manager->getLock()))
|
_locker(&(_manager->getLock()))
|
||||||
|
@ -25,6 +26,9 @@ Settings::Settings() :
|
||||||
}
|
}
|
||||||
|
|
||||||
Settings::~Settings() {
|
Settings::~Settings() {
|
||||||
|
if (_prefixes.size() != 0) {
|
||||||
|
qFatal("Unstable Settings Prefixes: You must call endGroup for every beginGroup and endArray for every begin*Array call");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Settings::remove(const QString& key) {
|
void Settings::remove(const QString& key) {
|
||||||
|
@ -50,14 +54,17 @@ bool Settings::contains(const QString& key) const {
|
||||||
}
|
}
|
||||||
|
|
||||||
int Settings::beginReadArray(const QString & prefix) {
|
int Settings::beginReadArray(const QString & prefix) {
|
||||||
|
_prefixes.push(prefix);
|
||||||
return _manager->beginReadArray(prefix);
|
return _manager->beginReadArray(prefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Settings::beginWriteArray(const QString& prefix, int size) {
|
void Settings::beginWriteArray(const QString& prefix, int size) {
|
||||||
|
_prefixes.push(prefix);
|
||||||
_manager->beginWriteArray(prefix, size);
|
_manager->beginWriteArray(prefix, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Settings::endArray() {
|
void Settings::endArray() {
|
||||||
|
_prefixes.pop();
|
||||||
_manager->endArray();
|
_manager->endArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,10 +73,12 @@ void Settings::setArrayIndex(int i) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Settings::beginGroup(const QString& prefix) {
|
void Settings::beginGroup(const QString& prefix) {
|
||||||
|
_prefixes.push(prefix);
|
||||||
_manager->beginGroup(prefix);
|
_manager->beginGroup(prefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Settings::endGroup() {
|
void Settings::endGroup() {
|
||||||
|
_prefixes.pop();
|
||||||
_manager->endGroup();
|
_manager->endGroup();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -58,8 +58,10 @@ public:
|
||||||
void setQuatValue(const QString& name, const glm::quat& quatValue);
|
void setQuatValue(const QString& name, const glm::quat& quatValue);
|
||||||
void getQuatValueIfValid(const QString& name, glm::quat& quatValue);
|
void getQuatValueIfValid(const QString& name, glm::quat& quatValue);
|
||||||
|
|
||||||
|
private:
|
||||||
QSharedPointer<Setting::Manager> _manager;
|
QSharedPointer<Setting::Manager> _manager;
|
||||||
QWriteLocker _locker;
|
QWriteLocker _locker;
|
||||||
|
QStack<QString> _prefixes;
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace Setting {
|
namespace Setting {
|
||||||
|
@ -75,15 +77,40 @@ namespace Setting {
|
||||||
virtual ~Handle() { deinit(); }
|
virtual ~Handle() { deinit(); }
|
||||||
|
|
||||||
// Returns setting value, returns its default value if not found
|
// Returns setting value, returns its default value if not found
|
||||||
T get() { return get(_defaultValue); }
|
T get() const {
|
||||||
|
return get(_defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
// Returns setting value, returns other if not found
|
// Returns setting value, returns other if not found
|
||||||
T get(const T& other) { maybeInit(); return (_isSet) ? _value : other; }
|
T get(const T& other) const {
|
||||||
T getDefault() const { return _defaultValue; }
|
maybeInit();
|
||||||
|
return (_isSet) ? _value : other;
|
||||||
|
}
|
||||||
|
|
||||||
|
const T& getDefault() const {
|
||||||
|
return _defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
void set(const T& value) { maybeInit(); _value = value; _isSet = true; }
|
void reset() {
|
||||||
void reset() { set(_defaultValue); }
|
set(_defaultValue);
|
||||||
|
}
|
||||||
void remove() { maybeInit(); _isSet = false; }
|
|
||||||
|
void set(const T& value) {
|
||||||
|
maybeInit();
|
||||||
|
if ((!_isSet && (value != _defaultValue)) || _value != value) {
|
||||||
|
_value = value;
|
||||||
|
_isSet = true;
|
||||||
|
save();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void remove() {
|
||||||
|
maybeInit();
|
||||||
|
if (_isSet) {
|
||||||
|
_isSet = false;
|
||||||
|
save();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void setVariant(const QVariant& variant);
|
virtual void setVariant(const QVariant& variant);
|
||||||
|
|
|
@ -98,6 +98,8 @@ namespace Setting {
|
||||||
// Register Handle
|
// Register Handle
|
||||||
manager->registerHandle(this);
|
manager->registerHandle(this);
|
||||||
_isInitialized = true;
|
_isInitialized = true;
|
||||||
|
} else {
|
||||||
|
qWarning() << "Settings interface used after manager destroyed";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load value from disk
|
// Load value from disk
|
||||||
|
@ -117,9 +119,9 @@ namespace Setting {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Interface::maybeInit() {
|
void Interface::maybeInit() const {
|
||||||
if (!_isInitialized) {
|
if (!_isInitialized) {
|
||||||
init();
|
const_cast<Interface*>(this)->init();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -39,19 +39,20 @@ namespace Setting {
|
||||||
virtual ~Interface() = default;
|
virtual ~Interface() = default;
|
||||||
|
|
||||||
void init();
|
void init();
|
||||||
void maybeInit();
|
void maybeInit() const;
|
||||||
void deinit();
|
void deinit();
|
||||||
|
|
||||||
void save();
|
void save();
|
||||||
void load();
|
void load();
|
||||||
|
|
||||||
bool _isInitialized = false;
|
|
||||||
bool _isSet = false;
|
bool _isSet = false;
|
||||||
const QString _key;
|
const QString _key;
|
||||||
|
|
||||||
|
private:
|
||||||
|
mutable bool _isInitialized = false;
|
||||||
|
|
||||||
friend class Manager;
|
friend class Manager;
|
||||||
|
mutable QWeakPointer<Manager> _manager;
|
||||||
QWeakPointer<Manager> _manager;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,8 +33,8 @@ namespace Setting {
|
||||||
void Manager::customDeleter() { }
|
void Manager::customDeleter() { }
|
||||||
|
|
||||||
|
|
||||||
void Manager::registerHandle(Setting::Interface* handle) {
|
void Manager::registerHandle(Interface* handle) {
|
||||||
QString key = handle->getKey();
|
const QString& key = handle->getKey();
|
||||||
withWriteLock([&] {
|
withWriteLock([&] {
|
||||||
if (_handles.contains(key)) {
|
if (_handles.contains(key)) {
|
||||||
qWarning() << "Setting::Manager::registerHandle(): Key registered more than once, overriding: " << key;
|
qWarning() << "Setting::Manager::registerHandle(): Key registered more than once, overriding: " << key;
|
||||||
|
@ -58,7 +58,9 @@ namespace Setting {
|
||||||
} else {
|
} else {
|
||||||
loadedValue = value(key);
|
loadedValue = value(key);
|
||||||
}
|
}
|
||||||
handle->setVariant(loadedValue);
|
if (loadedValue.isValid()) {
|
||||||
|
handle->setVariant(loadedValue);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,6 +96,7 @@ namespace Setting {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Manager::saveAll() {
|
void Manager::saveAll() {
|
||||||
|
bool forceSync = false;
|
||||||
withWriteLock([&] {
|
withWriteLock([&] {
|
||||||
for (auto key : _pendingChanges.keys()) {
|
for (auto key : _pendingChanges.keys()) {
|
||||||
auto newValue = _pendingChanges[key];
|
auto newValue = _pendingChanges[key];
|
||||||
|
@ -101,15 +104,21 @@ namespace Setting {
|
||||||
if (newValue == savedValue) {
|
if (newValue == savedValue) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (newValue == UNSET_VALUE) {
|
if (newValue == UNSET_VALUE || !newValue.isValid()) {
|
||||||
|
forceSync = true;
|
||||||
remove(key);
|
remove(key);
|
||||||
} else {
|
} else {
|
||||||
|
forceSync = true;
|
||||||
setValue(key, newValue);
|
setValue(key, newValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_pendingChanges.clear();
|
_pendingChanges.clear();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (forceSync) {
|
||||||
|
sync();
|
||||||
|
}
|
||||||
|
|
||||||
// Restart timer
|
// Restart timer
|
||||||
if (_saveTimer) {
|
if (_saveTimer) {
|
||||||
_saveTimer->start();
|
_saveTimer->start();
|
||||||
|
|
Loading…
Reference in a new issue