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, SettingsChangeNotifier *> Settings::settingsChangeNotifier;
36 # define create_qsettings QSettings s(QCoreApplication::organizationDomain(), appName)
38 # define create_qsettings QSettings s(fileName(), format())
41 // Settings::Settings(QString group_, QString appName_)
46 // /* we need to call the constructor immediately in order to set the path...
48 // QSettings(QCoreApplication::organizationName(), applicationName);
50 // // FIXME sandboxDir() is not currently working correctly...
51 // //if(Qtopia::sandboxDir().isEmpty()) QSettings();
52 // //else QSettings(Qtopia::sandboxDir() + "/etc/QuasselIRC.conf", QSettings::NativeFormat);
53 // // ...so we have to use a workaround:
54 // QString appPath = QCoreApplication::applicationFilePath();
55 // if(appPath.startsWith(Qtopia::packagePath())) {
56 // QString sandboxPath = appPath.left(Qtopia::packagePath().length() + 32);
57 // QSettings(sandboxPath + "/etc/QuasselIRC.conf", QSettings::IniFormat);
58 // qDebug() << sandboxPath + "/etc/QuasselIRC.conf";
60 // QSettings(QCoreApplication::organizationName(), applicationName);
66 void Settings::notify(const QString &key, QObject *receiver, const char *slot)
68 QObject::connect(notifier(normalizedKey(group, key)), SIGNAL(valueChanged(const QVariant &)),
73 void Settings::initAndNotify(const QString &key, QObject *receiver, const char *slot, const QVariant &defaultValue)
75 notify(key, receiver, slot);
76 emit notifier(normalizedKey(group, key))->valueChanged(localValue(key, defaultValue));
80 uint Settings::version()
82 // we don't cache this value, and we ignore the group
84 uint ver = s.value("Config/Version", 0).toUInt();
86 // No version, so create one
87 s.setValue("Config/Version", VERSION);
94 uint Settings::versionMinor()
96 // Don't cache this value; ignore the group
98 // '0' means new configuration, anything else indicates an existing configuration. Application
99 // initialization should check this value and manage upgrades/downgrades, e.g. in Core::Core()
100 // and QtUiApplication::init().
101 uint verMinor = s.value("Config/VersionMinor", 0).toUInt();
103 // As previous Quassel versions didn't implement this, we need to check if any settings other
104 // than Config/Version exist. If so, assume it's version 1.
105 if (verMinor == 0 && s.allKeys().count() > 1) {
106 // More than 1 key exists, but version's never been set. Assume and set version 1.
107 setVersionMinor(VERSION_MINOR_INITIAL);
108 return VERSION_MINOR_INITIAL;
115 void Settings::setVersionMinor(const uint versionMinor)
117 // Don't cache this value; ignore the group
119 // Set the value directly.
120 s.setValue("Config/VersionMinor", versionMinor);
124 bool Settings::sync() {
127 switch (s.status()) {
128 case QSettings::NoError:
136 bool Settings::isWritable() {
138 return s.isWritable();
142 QStringList Settings::allLocalKeys()
146 QStringList res = s.allKeys();
152 QStringList Settings::localChildKeys(const QString &rootkey)
155 if (rootkey.isEmpty())
158 g = QString("%1/%2").arg(group, rootkey);
162 QStringList res = s.childKeys();
168 QStringList Settings::localChildGroups(const QString &rootkey)
171 if (rootkey.isEmpty())
174 g = QString("%1/%2").arg(group, rootkey);
178 QStringList res = s.childGroups();
184 void Settings::setLocalValue(const QString &key, const QVariant &data)
186 QString normKey = normalizedKey(group, key);
188 s.setValue(normKey, data);
189 setCacheValue(normKey, data);
190 if (hasNotifier(normKey)) {
191 emit notifier(normKey)->valueChanged(data);
196 const QVariant &Settings::localValue(const QString &key, const QVariant &def)
198 QString normKey = normalizedKey(group, key);
199 if (!isCached(normKey)) {
201 setCacheValue(normKey, s.value(normKey, def));
203 return cacheValue(normKey);
207 bool Settings::localKeyExists(const QString &key)
209 QString normKey = normalizedKey(group, key);
210 if (isCached(normKey))
214 return s.contains(normKey);
218 void Settings::removeLocalKey(const QString &key)
224 QString normKey = normalizedKey(group, key);
225 if (isCached(normKey)) {
226 settingsCache.remove(normKey);
228 if (hasNotifier(normKey)) {
229 emit notifier(normKey)->valueChanged({});