1 /***************************************************************************
2 * Copyright (C) 2005-2016 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 ***************************************************************************/
24 #include <QCoreApplication>
32 class SettingsChangeNotifier : public QObject
37 void valueChanged(const QVariant &newValue);
40 friend class Settings;
47 enum Mode { Default, Custom };
50 //! Call the given slot on change of the given key
51 virtual void notify(const QString &key, QObject *receiver, const char *slot);
53 //! Sets up notification and calls the given slot to set the initial value
54 void initAndNotify(const QString &key, QObject *receiver, const char *slot, const QVariant &defaultValue = QVariant());
57 * Get the major configuration version
59 * This indicates the backwards/forwards incompatible version of configuration.
61 * @return Major configuration version (the X in XX.YY)
63 virtual uint version();
66 * Get the minor configuration version
68 * This indicates the backwards/forwards compatible version of configuration.
70 * @see Settings::setVersionMinor()
71 * @return Minor configuration version (the Y in XX.YY)
73 virtual uint versionMinor();
76 * Set the minor configuration version
78 * When making backwards/forwards compatible changes, call this with the new version number.
79 * This does not implement any upgrade logic; implement that when checking Settings::version(),
80 * e.g. in Core::Core() and QtUiApplication::init().
82 * @param[in] versionMinor New minor version number
84 virtual void setVersionMinor(const uint versionMinor);
87 * Check if the configuration storage is writable.
89 * @return true if writable, false otherwise
94 inline Settings(QString group_, QString appName_) : group(group_), appName(appName_) {}
95 inline virtual ~Settings() {}
97 inline void setGroup(const QString &group_) { group = group_; }
99 virtual QStringList allLocalKeys();
100 virtual QStringList localChildKeys(const QString &rootkey = QString());
101 virtual QStringList localChildGroups(const QString &rootkey = QString());
103 virtual void setLocalValue(const QString &key, const QVariant &data);
104 virtual const QVariant &localValue(const QString &key, const QVariant &def = QVariant());
107 * Gets if a key exists in settings
109 * @param[in] key ID of local settings key
110 * @returns True if key exists in settings, otherwise false
112 virtual bool localKeyExists(const QString &key);
114 virtual void removeLocalKey(const QString &key);
120 inline QSettings::Format format()
123 return QSettings::IniFormat;
125 return QSettings::NativeFormat;
130 inline QString fileName()
132 return Quassel::configDirPath() + appName
133 + ((format() == QSettings::NativeFormat) ? QLatin1String(".conf") : QLatin1String(".ini"));
137 static QHash<QString, QVariant> settingsCache;
138 static QHash<QString, SettingsChangeNotifier *> settingsChangeNotifier;
140 inline QString normalizedKey(const QString &group, const QString &key)
144 return group + '/' + key;
148 inline void setCacheValue(const QString &normKey, const QVariant &data)
150 settingsCache[normKey] = data;
154 inline const QVariant &cacheValue(const QString &normKey)
156 return settingsCache[normKey];
160 inline bool isCached(const QString &normKey)
162 return settingsCache.contains(normKey);
166 inline SettingsChangeNotifier *notifier(const QString &normKey)
168 if (!hasNotifier(normKey))
169 settingsChangeNotifier[normKey] = new SettingsChangeNotifier();
170 return settingsChangeNotifier[normKey];
174 inline bool hasNotifier(const QString &normKey)
176 return settingsChangeNotifier.contains(normKey);