X-Git-Url: https://git.quassel-irc.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fcommon%2Fsettings.cpp;h=10447eaed78de7470aeca5e6fcb92453b98ac519;hb=694f9bfbf7f1af19108461c7e00d133e55082bce;hp=6b0ce0399f8afb667cc8ad2c96043d843261eda4;hpb=17a2e7912f49f70273f90ea3f744960a7d08a6b4;p=quassel.git diff --git a/src/common/settings.cpp b/src/common/settings.cpp index 6b0ce039..10447eae 100644 --- a/src/common/settings.cpp +++ b/src/common/settings.cpp @@ -22,8 +22,16 @@ #include "settings.h" -QHash > Settings::settingsCache; -QHash > Settings::settingsChangeNotifier; +const int VERSION = 1; + +QHash Settings::settingsCache; +QHash Settings::settingsChangeNotifier; + +#ifdef Q_WS_MAC +# define create_qsettings QSettings s(QCoreApplication::organizationDomain(), appName) +#else +# define create_qsettings QSettings s(fileName(), format()) +#endif // Settings::Settings(QString group_, QString appName_) // : group(group_), @@ -50,73 +58,106 @@ QHash > Settings::settingsChan // */ // } -void Settings::notify(const QString &key, QObject *receiver, const char *slot) { - QObject::connect(notifier(group, key), SIGNAL(valueChanged(const QVariant &)), - receiver, slot); +void Settings::notify(const QString &key, QObject *receiver, const char *slot) +{ + QObject::connect(notifier(normalizedKey(group, key)), SIGNAL(valueChanged(const QVariant &)), + receiver, slot); } -QStringList Settings::allLocalKeys() { - QSettings s(fileName(), format()); - s.beginGroup(group); - QStringList res = s.allKeys(); - s.endGroup(); - return res; + +void Settings::initAndNotify(const QString &key, QObject *receiver, const char *slot, const QVariant &defaultValue) +{ + notify(key, receiver, slot); + emit notifier(normalizedKey(group, key))->valueChanged(localValue(key, defaultValue)); } -QStringList Settings::localChildKeys(const QString &rootkey) { - QString g; - if(rootkey.isEmpty()) - g = group; - else - g = QString("%1/%2").arg(group, rootkey); - - QSettings s(fileName(), format()); - s.beginGroup(g); - QStringList res = s.childKeys(); - s.endGroup(); - return res; + +uint Settings::version() +{ + // we don't cache this value, and we ignore the group + create_qsettings; + uint ver = s.value("Config/Version", 0).toUInt(); + if (!ver) { + // No version, so create one + s.setValue("Config/Version", VERSION); + return VERSION; + } + return ver; } -QStringList Settings::localChildGroups(const QString &rootkey) { - QString g; - if(rootkey.isEmpty()) - g = group; - else - g = QString("%1/%2").arg(group, rootkey); - - QSettings s(fileName(), format()); - s.beginGroup(g); - QStringList res = s.childGroups(); - s.endGroup(); - return res; + +QStringList Settings::allLocalKeys() +{ + create_qsettings; + s.beginGroup(group); + QStringList res = s.allKeys(); + s.endGroup(); + return res; } -void Settings::setLocalValue(const QString &key, const QVariant &data) { - QSettings s(fileName(), format()); - s.beginGroup(group); - s.setValue(key, data); - s.endGroup(); - setCacheValue(group, key, data); - if(hasNotifier(group, key)) { - emit notifier(group, key)->valueChanged(data); - } + +QStringList Settings::localChildKeys(const QString &rootkey) +{ + QString g; + if (rootkey.isEmpty()) + g = group; + else + g = QString("%1/%2").arg(group, rootkey); + + create_qsettings; + s.beginGroup(g); + QStringList res = s.childKeys(); + s.endGroup(); + return res; } -const QVariant &Settings::localValue(const QString &key, const QVariant &def) { - if(!isCached(group, key)) { - QSettings s(fileName(), format()); - s.beginGroup(group); - setCacheValue(group, key, s.value(key, def)); + +QStringList Settings::localChildGroups(const QString &rootkey) +{ + QString g; + if (rootkey.isEmpty()) + g = group; + else + g = QString("%1/%2").arg(group, rootkey); + + create_qsettings; + s.beginGroup(g); + QStringList res = s.childGroups(); s.endGroup(); - } - return cacheValue(group, key); + return res; +} + + +void Settings::setLocalValue(const QString &key, const QVariant &data) +{ + QString normKey = normalizedKey(group, key); + create_qsettings; + s.setValue(normKey, data); + setCacheValue(normKey, data); + if (hasNotifier(normKey)) { + emit notifier(normKey)->valueChanged(data); + } } -void Settings::removeLocalKey(const QString &key) { - QSettings s(fileName(), format()); - s.beginGroup(group); - s.remove(key); - s.endGroup(); - if(isCached(group, key)) - settingsCache[group].remove(key); + +const QVariant &Settings::localValue(const QString &key, const QVariant &def) +{ + QString normKey = normalizedKey(group, key); + if (!isCached(normKey)) { + create_qsettings; + setCacheValue(normKey, s.value(normKey, def)); + } + return cacheValue(normKey); +} + + +void Settings::removeLocalKey(const QString &key) +{ + create_qsettings; + s.beginGroup(group); + s.remove(key); + s.endGroup(); + QString normKey = normalizedKey(group, key); + if (isCached(normKey)) + settingsCache.remove(normKey); }