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

View file

@ -50,12 +50,21 @@ namespace Setting {
} }
void Manager::loadSetting(Interface* handle) { 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) { void Manager::saveSetting(Interface* handle) {
auto key = handle->getKey(); const auto& key = handle->getKey();
QVariant handleValue = UNSET_VALUE; QVariant handleValue = UNSET_VALUE;
if (handle->isSet()) { if (handle->isSet()) {
handleValue = handle->getVariant(); handleValue = handle->getVariant();
@ -85,24 +94,22 @@ namespace Setting {
} }
void Manager::saveAll() { void Manager::saveAll() {
QHash<QString, QVariant> newPendingChanges;
withWriteLock([&] { 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 // Restart timer
if (_saveTimer) { if (_saveTimer) {
_saveTimer->start(); _saveTimer->start();