Fix unix builds, make settings ACID

This commit is contained in:
Brad Davis 2016-06-04 18:39:46 -07:00
parent 39dcd1f9bd
commit 65d8f65ed7
2 changed files with 27 additions and 18 deletions

View file

@ -18,6 +18,8 @@
#include <QtCore/QVariant>
namespace Setting {
class Manager;
void preInit();
void init();
void cleanupSettings();
@ -26,7 +28,7 @@ namespace Setting {
public:
static const QString FIRST_RUN;
QString getKey() const { return _key; }
const QString& getKey() const { return _key; }
bool isSet() const { return _isSet; }
virtual void setVariant(const QVariant& variant) = 0;

View file

@ -50,12 +50,21 @@ namespace Setting {
}
void Manager::loadSetting(Interface* handle) {
handle->setVariant(value(handle->getKey()));
const auto& key = handle->getKey();
withWriteLock([&] {
QVariant loadedValue;
if (_pendingChanges.contains(key)) {
loadedValue = _pendingChanges[key];
} else {
loadedValue = value(key);
}
handle->setVariant(loadedValue);
});
}
void Manager::saveSetting(Interface* handle) {
auto key = handle->getKey();
const auto& key = handle->getKey();
QVariant handleValue = UNSET_VALUE;
if (handle->isSet()) {
handleValue = handle->getVariant();
@ -85,24 +94,22 @@ namespace Setting {
}
void Manager::saveAll() {
QHash<QString, QVariant> newPendingChanges;
withWriteLock([&] {
newPendingChanges.swap(_pendingChanges);
for (auto key : _pendingChanges.keys()) {
auto newValue = _pendingChanges[key];
auto savedValue = value(key, UNSET_VALUE);
if (newValue == savedValue) {
continue;
}
if (newValue == UNSET_VALUE) {
remove(key);
} else {
setValue(key, newValue);
}
}
_pendingChanges.clear();
});
for (auto key : newPendingChanges.keys()) {
auto newValue = newPendingChanges[key];
auto savedValue = value(key, UNSET_VALUE);
if (newValue == savedValue) {
continue;
}
if (newValue == UNSET_VALUE) {
remove(key);
} else {
setValue(key, newValue);
}
}
// Restart timer
if (_saveTimer) {
_saveTimer->start();