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 ***************************************************************************/
23 #include "common-export.h"
25 #include <QCoreApplication>
33 class COMMON_EXPORT SettingsChangeNotifier : public QObject
38 void valueChanged(const QVariant &newValue);
41 friend class Settings;
45 class COMMON_EXPORT Settings
48 enum Mode { Default, Custom };
51 //! Call the given slot on change of the given key
52 virtual void notify(const QString &key, QObject *receiver, const char *slot);
54 //! Sets up notification and calls the given slot to set the initial value
55 void initAndNotify(const QString &key, QObject *receiver, const char *slot, const QVariant &defaultValue = QVariant());
58 * Get the major configuration version
60 * This indicates the backwards/forwards incompatible version of configuration.
62 * @return Major configuration version (the X in XX.YY)
64 virtual uint version();
67 * Get the minor configuration version
69 * This indicates the backwards/forwards compatible version of configuration.
71 * @see Settings::setVersionMinor()
72 * @return Minor configuration version (the Y in XX.YY)
74 virtual uint versionMinor();
77 * Set the minor configuration version
79 * When making backwards/forwards compatible changes, call this with the new version number.
80 * This does not implement any upgrade logic; implement that when checking Settings::version(),
81 * e.g. in Core::Core() and QtUiApplication::init().
83 * @param[in] versionMinor New minor version number
85 virtual void setVersionMinor(const uint versionMinor);
88 * Persist unsaved changes to permanent storage
90 * @return true if succeeded, false otherwise
95 * Check if the configuration storage is writable.
97 * @return true if writable, false otherwise
102 inline Settings(QString group_, QString appName_) : group(group_), appName(appName_) {}
103 inline virtual ~Settings() {}
105 inline void setGroup(const QString &group_) { group = group_; }
107 virtual QStringList allLocalKeys();
108 virtual QStringList localChildKeys(const QString &rootkey = QString());
109 virtual QStringList localChildGroups(const QString &rootkey = QString());
111 virtual void setLocalValue(const QString &key, const QVariant &data);
112 virtual QVariant localValue(const QString &key, const QVariant &def = QVariant());
115 * Gets if a key exists in settings
117 * @param[in] key ID of local settings key
118 * @returns True if key exists in settings, otherwise false
120 virtual bool localKeyExists(const QString &key);
122 virtual void removeLocalKey(const QString &key);
128 inline QSettings::Format format()
131 return QSettings::IniFormat;
133 return QSettings::NativeFormat;
138 inline QString fileName()
140 return Quassel::configDirPath() + appName
141 + ((format() == QSettings::NativeFormat) ? QLatin1String(".conf") : QLatin1String(".ini"));
145 static QHash<QString, QVariant> settingsCache; ///< Cached settings values
146 static QHash<QString, bool> settingsKeyPersistedCache; ///< Cached settings key exists on disk
147 static QHash<QString, SettingsChangeNotifier *> settingsChangeNotifier;
149 inline QString normalizedKey(const QString &group, const QString &key)
153 return group + '/' + key;
158 * Update the cache of whether or not a given settings key persists on disk
160 * @param normKey Normalized settings key ID
161 * @param exists True if key exists, otherwise false
163 inline void setCacheKeyPersisted(const QString &normKey, bool exists)
165 settingsKeyPersistedCache[normKey] = exists;
170 * Check if the given settings key ID persists on disk (rather than being a default value)
172 * @see Settings::localKeyExists()
174 * @param normKey Normalized settings key ID
175 * @return True if key exists and persistence has been cached, otherwise false
177 inline const bool &cacheKeyPersisted(const QString &normKey)
179 return settingsKeyPersistedCache[normKey];
184 * Check if the persistence of the given settings key ID has been cached
186 * @param normKey Normalized settings key ID
187 * @return True if key persistence has been cached, otherwise false
189 inline bool isKeyPersistedCached(const QString &normKey)
191 return settingsKeyPersistedCache.contains(normKey);
195 inline void setCacheValue(const QString &normKey, const QVariant &data)
197 settingsCache[normKey] = data;
201 inline const QVariant &cacheValue(const QString &normKey)
203 return settingsCache[normKey];
207 inline bool isCached(const QString &normKey)
209 return settingsCache.contains(normKey);
213 inline SettingsChangeNotifier *notifier(const QString &normKey)
215 if (!hasNotifier(normKey))
216 settingsChangeNotifier[normKey] = new SettingsChangeNotifier();
217 return settingsChangeNotifier[normKey];
221 inline bool hasNotifier(const QString &normKey)
223 return settingsChangeNotifier.contains(normKey);