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>
34 class COMMON_EXPORT SettingsChangeNotifier : public QObject
39 void valueChanged(const QVariant &newValue);
42 friend class Settings;
46 class COMMON_EXPORT Settings
49 enum Mode { Default, Custom };
52 //! Call the given slot on change of the given key
53 virtual void notify(const QString &key, QObject *receiver, const char *slot);
55 //! Sets up notification and calls the given slot to set the initial value
56 void initAndNotify(const QString &key, QObject *receiver, const char *slot, const QVariant &defaultValue = QVariant());
59 * Get the major configuration version
61 * This indicates the backwards/forwards incompatible version of configuration.
63 * @return Major configuration version (the X in XX.YY)
65 virtual uint version();
68 * Get the minor configuration version
70 * This indicates the backwards/forwards compatible version of configuration.
72 * @see Settings::setVersionMinor()
73 * @return Minor configuration version (the Y in XX.YY)
75 virtual uint versionMinor();
78 * Set the minor configuration version
80 * When making backwards/forwards compatible changes, call this with the new version number.
81 * This does not implement any upgrade logic; implement that when checking Settings::version(),
82 * e.g. in Core::Core() and QtUiApplication::init().
84 * @param[in] versionMinor New minor version number
86 virtual void setVersionMinor(const uint versionMinor);
89 * Persist unsaved changes to permanent storage
91 * @return true if succeeded, false otherwise
96 * Check if the configuration storage is writable.
98 * @return true if writable, false otherwise
103 inline Settings(QString group_, QString appName_) : group(std::move(group_)), appName(std::move(appName_)) {}
104 inline virtual ~Settings() = default;
106 inline void setGroup(const QString &group_) { group = group_; }
108 virtual QStringList allLocalKeys();
109 virtual QStringList localChildKeys(const QString &rootkey = QString());
110 virtual QStringList localChildGroups(const QString &rootkey = QString());
112 virtual void setLocalValue(const QString &key, const QVariant &data);
113 virtual QVariant localValue(const QString &key, const QVariant &def = QVariant());
116 * Gets if a key exists in settings
118 * @param[in] key ID of local settings key
119 * @returns True if key exists in settings, otherwise false
121 virtual bool localKeyExists(const QString &key);
123 virtual void removeLocalKey(const QString &key);
129 inline QSettings::Format format()
132 return QSettings::IniFormat;
134 return QSettings::NativeFormat;
139 inline QString fileName()
141 return Quassel::configDirPath() + appName
142 + ((format() == QSettings::NativeFormat) ? QLatin1String(".conf") : QLatin1String(".ini"));
146 static QHash<QString, QVariant> settingsCache; ///< Cached settings values
147 static QHash<QString, bool> settingsKeyPersistedCache; ///< Cached settings key exists on disk
148 static QHash<QString, SettingsChangeNotifier *> settingsChangeNotifier;
150 inline QString normalizedKey(const QString &group, const QString &key)
154 return group + '/' + key;
159 * Update the cache of whether or not a given settings key persists on disk
161 * @param normKey Normalized settings key ID
162 * @param exists True if key exists, otherwise false
164 inline void setCacheKeyPersisted(const QString &normKey, bool exists)
166 settingsKeyPersistedCache[normKey] = exists;
171 * Check if the given settings key ID persists on disk (rather than being a default value)
173 * @see Settings::localKeyExists()
175 * @param normKey Normalized settings key ID
176 * @return True if key exists and persistence has been cached, otherwise false
178 inline const bool &cacheKeyPersisted(const QString &normKey)
180 return settingsKeyPersistedCache[normKey];
185 * Check if the persistence of the given settings key ID has been cached
187 * @param normKey Normalized settings key ID
188 * @return True if key persistence has been cached, otherwise false
190 inline bool isKeyPersistedCached(const QString &normKey)
192 return settingsKeyPersistedCache.contains(normKey);
196 inline void setCacheValue(const QString &normKey, const QVariant &data)
198 settingsCache[normKey] = data;
202 inline const QVariant &cacheValue(const QString &normKey)
204 return settingsCache[normKey];
208 inline bool isCached(const QString &normKey)
210 return settingsCache.contains(normKey);
214 inline SettingsChangeNotifier *notifier(const QString &normKey)
216 if (!hasNotifier(normKey))
217 settingsChangeNotifier[normKey] = new SettingsChangeNotifier();
218 return settingsChangeNotifier[normKey];
222 inline bool hasNotifier(const QString &normKey)
224 return settingsChangeNotifier.contains(normKey);