From: Manuel Nickschas Date: Tue, 28 Jul 2009 14:58:07 +0000 (+0200) Subject: Make the Settings caches flat structures X-Git-Tag: 0.5-rc1~119 X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=commitdiff_plain;h=adda0fa0aeef6087045256b250ff30c88ee7dc00;ds=sidebyside Make the Settings caches flat structures As settingspages tend to access values by absolute path (rather than by group and key), we should use a flat hash rather than a nested tree structure, such that caching and in particular the notifiers work with both the group/key combination and the absolute key. This should also be slightly more efficient. --- diff --git a/src/common/settings.cpp b/src/common/settings.cpp index f832f776..2731da26 100644 --- a/src/common/settings.cpp +++ b/src/common/settings.cpp @@ -24,8 +24,8 @@ const int VERSION = 1; -QHash > Settings::settingsCache; -QHash > Settings::settingsChangeNotifier; +QHash Settings::settingsCache; +QHash Settings::settingsChangeNotifier; #ifdef Q_WS_MAC # define create_qsettings QSettings s(QCoreApplication::organizationDomain(), appName) @@ -59,7 +59,7 @@ QHash > Settings::settingsChan // } void Settings::notify(const QString &key, QObject *receiver, const char *slot) { - QObject::connect(notifier(group, key), SIGNAL(valueChanged(const QVariant &)), + QObject::connect(notifier(normalizedKey(group, key)), SIGNAL(valueChanged(const QVariant &)), receiver, slot); } @@ -112,24 +112,22 @@ QStringList Settings::localChildGroups(const QString &rootkey) { } void Settings::setLocalValue(const QString &key, const QVariant &data) { + QString normKey = normalizedKey(group, key); create_qsettings; - s.beginGroup(group); - s.setValue(key, data); - s.endGroup(); - setCacheValue(group, key, data); - if(hasNotifier(group, key)) { - emit notifier(group, key)->valueChanged(data); + s.setValue(normKey, data); + setCacheValue(normKey, data); + if(hasNotifier(normKey)) { + emit notifier(normKey)->valueChanged(data); } } const QVariant &Settings::localValue(const QString &key, const QVariant &def) { - if(!isCached(group, key)) { + QString normKey = normalizedKey(group, key); + if(!isCached(normKey)) { create_qsettings; - s.beginGroup(group); - setCacheValue(group, key, s.value(key, def)); - s.endGroup(); + setCacheValue(normKey, s.value(normKey, def)); } - return cacheValue(group, key); + return cacheValue(normKey); } void Settings::removeLocalKey(const QString &key) { @@ -137,6 +135,7 @@ void Settings::removeLocalKey(const QString &key) { s.beginGroup(group); s.remove(key); s.endGroup(); - if(isCached(group, key)) - settingsCache[group].remove(key); + QString normKey = normalizedKey(group, key); + if(isCached(normKey)) + settingsCache.remove(normKey); } diff --git a/src/common/settings.h b/src/common/settings.h index 26c8ead0..64743538 100644 --- a/src/common/settings.h +++ b/src/common/settings.h @@ -78,27 +78,33 @@ private: + ((format() == QSettings::NativeFormat) ? QLatin1String(".conf") : QLatin1String(".ini")); } - static QHash > settingsCache; - static QHash > settingsChangeNotifier; + static QHash settingsCache; + static QHash settingsChangeNotifier; - inline void setCacheValue(const QString &group, const QString &key, const QVariant &data) { - settingsCache[group][key] = data; + inline QString normalizedKey(const QString &group, const QString &key) { + if(group.isEmpty()) + return key; + return group + '/' + key; } - inline const QVariant &cacheValue(const QString &group, const QString &key) { - return settingsCache[group][key]; + + inline void setCacheValue(const QString &normKey, const QVariant &data) { + settingsCache[normKey] = data; + } + inline const QVariant &cacheValue(const QString &normKey) { + return settingsCache[normKey]; } - inline bool isCached(const QString &group, const QString &key) { - return settingsCache.contains(group) && settingsCache[group].contains(key); + inline bool isCached(const QString &normKey) { + return settingsCache.contains(normKey); } - inline SettingsChangeNotifier *notifier(const QString &group, const QString &key) { - if(!hasNotifier(group, key)) - settingsChangeNotifier[group][key] = new SettingsChangeNotifier(); - return settingsChangeNotifier[group][key]; + inline SettingsChangeNotifier *notifier(const QString &normKey) { + if(!hasNotifier(normKey)) + settingsChangeNotifier[normKey] = new SettingsChangeNotifier(); + return settingsChangeNotifier[normKey]; } - inline bool hasNotifier(const QString &group, const QString &key) { - return settingsChangeNotifier.contains(group) && settingsChangeNotifier[group].contains(key); + inline bool hasNotifier(const QString &normKey) { + return settingsChangeNotifier.contains(normKey); } };