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 ***************************************************************************/
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 * Persist unsaved changes to permanent storage
89 * @return true if succeeded, false otherwise
94 * Check if the configuration storage is writable.
96 * @return true if writable, false otherwise
101 inline Settings(QString group_, QString appName_) : group(group_), appName(appName_) {}
102 inline virtual ~Settings() {}
104 inline void setGroup(const QString &group_) { group = group_; }
106 virtual QStringList allLocalKeys();
107 virtual QStringList localChildKeys(const QString &rootkey = QString());
108 virtual QStringList localChildGroups(const QString &rootkey = QString());
110 virtual void setLocalValue(const QString &key, const QVariant &data);
111 virtual const QVariant &localValue(const QString &key, const QVariant &def = QVariant());
114 * Gets if a key exists in settings
116 * @param[in] key ID of local settings key
117 * @returns True if key exists in settings, otherwise false
119 virtual bool localKeyExists(const QString &key);
121 virtual void removeLocalKey(const QString &key);
127 inline QSettings::Format format()
130 return QSettings::IniFormat;
132 return QSettings::NativeFormat;
137 inline QString fileName()
139 return Quassel::configDirPath() + appName
140 + ((format() == QSettings::NativeFormat) ? QLatin1String(".conf") : QLatin1String(".ini"));
144 static QHash<QString, QVariant> settingsCache; ///< Cached settings values
145 static QHash<QString, bool> settingsKeyPersistedCache; ///< Cached settings key exists on disk
146 static QHash<QString, SettingsChangeNotifier *> settingsChangeNotifier;
148 inline QString normalizedKey(const QString &group, const QString &key)
152 return group + '/' + key;
157 * Update the cache of whether or not a given settings key persists on disk
159 * @param normKey Normalized settings key ID
160 * @param exists True if key exists, otherwise false
162 inline void setCacheKeyPersisted(const QString &normKey, bool exists)
164 settingsKeyPersistedCache[normKey] = exists;
169 * Check if the given settings key ID persists on disk (rather than being a default value)
171 * @see Settings::localKeyExists()
173 * @param normKey Normalized settings key ID
174 * @return True if key exists and persistence has been cached, otherwise false
176 inline const bool &cacheKeyPersisted(const QString &normKey)
178 return settingsKeyPersistedCache[normKey];
183 * Check if the persistence of the given settings key ID has been cached
185 * @param normKey Normalized settings key ID
186 * @return True if key persistence has been cached, otherwise false
188 inline bool isKeyPersistedCached(const QString &normKey)
190 return settingsKeyPersistedCache.contains(normKey);
194 inline void setCacheValue(const QString &normKey, const QVariant &data)
196 settingsCache[normKey] = data;
200 inline const QVariant &cacheValue(const QString &normKey)
202 return settingsCache[normKey];
206 inline bool isCached(const QString &normKey)
208 return settingsCache.contains(normKey);
212 inline SettingsChangeNotifier *notifier(const QString &normKey)
214 if (!hasNotifier(normKey))
215 settingsChangeNotifier[normKey] = new SettingsChangeNotifier();
216 return settingsChangeNotifier[normKey];
220 inline bool hasNotifier(const QString &normKey)
222 return settingsChangeNotifier.contains(normKey);