Make the Settings caches flat structures
authorManuel Nickschas <sputnick@quassel-irc.org>
Tue, 28 Jul 2009 14:58:07 +0000 (16:58 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Thu, 6 Aug 2009 18:25:05 +0000 (20:25 +0200)
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.

src/common/settings.cpp
src/common/settings.h

index f832f77..2731da2 100644 (file)
@@ -24,8 +24,8 @@
 
 const int VERSION = 1;
 
-QHash<QString, QHash<QString, QVariant> > Settings::settingsCache;
-QHash<QString, QHash<QString, SettingsChangeNotifier *> > Settings::settingsChangeNotifier;
+QHash<QString, QVariant> Settings::settingsCache;
+QHash<QString, SettingsChangeNotifier *> Settings::settingsChangeNotifier;
 
 #ifdef Q_WS_MAC
 #  define create_qsettings QSettings s(QCoreApplication::organizationDomain(), appName)
@@ -59,7 +59,7 @@ QHash<QString, QHash<QString, SettingsChangeNotifier *> > 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);
 }
index 26c8ead..6474353 100644 (file)
@@ -78,27 +78,33 @@ private:
            + ((format() == QSettings::NativeFormat) ? QLatin1String(".conf") : QLatin1String(".ini"));
   }
 
-  static QHash<QString, QHash<QString, QVariant> > settingsCache;
-  static QHash<QString, QHash<QString, SettingsChangeNotifier *> > settingsChangeNotifier;
+  static QHash<QString, QVariant> settingsCache;
+  static QHash<QString, SettingsChangeNotifier *> 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);
   }
 };