1 /***************************************************************************
2 * Copyright (C) 2005-2018 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include <QStringList>
25 const int VERSION = 1; /// Settings version for backwords/forwards incompatible changes
27 // This is used if no VersionMinor key exists, e.g. upgrading from a Quassel version before this
28 // change. This shouldn't be increased from 1; instead, change the logic in Core::Core() and
29 // QtUiApplication::init() to handle upgrading and downgrading.
30 const int VERSION_MINOR_INITIAL = 1; /// Initial settings version for compatible changes
32 QHash<QString, QVariant> Settings::settingsCache;
33 QHash<QString, bool> Settings::settingsKeyPersistedCache;
34 QHash<QString, SettingsChangeNotifier *> Settings::settingsChangeNotifier;
37 # define create_qsettings QSettings s(QCoreApplication::organizationDomain(), appName)
39 # define create_qsettings QSettings s(fileName(), format())
42 // Settings::Settings(QString group_, QString appName_)
47 // /* we need to call the constructor immediately in order to set the path...
49 // QSettings(QCoreApplication::organizationName(), applicationName);
51 // // FIXME sandboxDir() is not currently working correctly...
52 // //if(Qtopia::sandboxDir().isEmpty()) QSettings();
53 // //else QSettings(Qtopia::sandboxDir() + "/etc/QuasselIRC.conf", QSettings::NativeFormat);
54 // // ...so we have to use a workaround:
55 // QString appPath = QCoreApplication::applicationFilePath();
56 // if(appPath.startsWith(Qtopia::packagePath())) {
57 // QString sandboxPath = appPath.left(Qtopia::packagePath().length() + 32);
58 // QSettings(sandboxPath + "/etc/QuasselIRC.conf", QSettings::IniFormat);
59 // qDebug() << sandboxPath + "/etc/QuasselIRC.conf";
61 // QSettings(QCoreApplication::organizationName(), applicationName);
67 void Settings::notify(const QString &key, QObject *receiver, const char *slot)
69 QObject::connect(notifier(normalizedKey(group, key)), SIGNAL(valueChanged(const QVariant &)),
74 void Settings::initAndNotify(const QString &key, QObject *receiver, const char *slot, const QVariant &defaultValue)
76 notify(key, receiver, slot);
77 emit notifier(normalizedKey(group, key))->valueChanged(localValue(key, defaultValue));
81 uint Settings::version()
83 // we don't cache this value, and we ignore the group
85 uint ver = s.value("Config/Version", 0).toUInt();
87 // No version, so create one
88 s.setValue("Config/Version", VERSION);
95 uint Settings::versionMinor()
97 // Don't cache this value; ignore the group
99 // '0' means new configuration, anything else indicates an existing configuration. Application
100 // initialization should check this value and manage upgrades/downgrades, e.g. in Core::Core()
101 // and QtUiApplication::init().
102 uint verMinor = s.value("Config/VersionMinor", 0).toUInt();
104 // As previous Quassel versions didn't implement this, we need to check if any settings other
105 // than Config/Version exist. If so, assume it's version 1.
106 if (verMinor == 0 && s.allKeys().count() > 1) {
107 // More than 1 key exists, but version's never been set. Assume and set version 1.
108 setVersionMinor(VERSION_MINOR_INITIAL);
109 return VERSION_MINOR_INITIAL;
116 void Settings::setVersionMinor(const uint versionMinor)
118 // Don't cache this value; ignore the group
120 // Set the value directly.
121 s.setValue("Config/VersionMinor", versionMinor);
125 bool Settings::sync() {
128 switch (s.status()) {
129 case QSettings::NoError:
137 bool Settings::isWritable() {
139 return s.isWritable();
143 QStringList Settings::allLocalKeys()
147 QStringList res = s.allKeys();
153 QStringList Settings::localChildKeys(const QString &rootkey)
156 if (rootkey.isEmpty())
159 g = QString("%1/%2").arg(group, rootkey);
163 QStringList res = s.childKeys();
169 QStringList Settings::localChildGroups(const QString &rootkey)
172 if (rootkey.isEmpty())
175 g = QString("%1/%2").arg(group, rootkey);
179 QStringList res = s.childGroups();
185 void Settings::setLocalValue(const QString &key, const QVariant &data)
187 QString normKey = normalizedKey(group, key);
189 s.setValue(normKey, data);
190 setCacheKeyPersisted(normKey, true);
191 setCacheValue(normKey, data);
192 if (hasNotifier(normKey)) {
193 emit notifier(normKey)->valueChanged(data);
198 QVariant Settings::localValue(const QString &key, const QVariant &def)
200 QString normKey = normalizedKey(group, key);
201 if (!isCached(normKey)) {
203 // Since we're loading from settings anyways, cache whether or not the key exists on disk
204 setCacheKeyPersisted(normKey, s.contains(normKey));
206 setCacheValue(normKey, s.value(normKey, def));
208 if (cacheKeyPersisted(normKey)) {
209 return cacheValue(normKey);
211 // Don't return possibly wrong cached values
212 // A key gets cached with the first default value requested and never changes afterwards
218 bool Settings::localKeyExists(const QString &key)
220 QString normKey = normalizedKey(group, key);
221 if (!isKeyPersistedCached(normKey)) {
223 // Cache whether or not key exists on disk
224 // We can't cache key value as we don't know the default
225 setCacheKeyPersisted(normKey, s.contains(normKey));
228 return cacheKeyPersisted(normKey);
232 void Settings::removeLocalKey(const QString &key)
238 QString normKey = normalizedKey(group, key);
239 if (isCached(normKey)) {
240 settingsCache.remove(normKey);
242 if (isKeyPersistedCached(normKey)) {
243 settingsKeyPersistedCache.remove(normKey);
245 if (hasNotifier(normKey)) {
246 emit notifier(normKey)->valueChanged({});