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"
26 #include <type_traits>
28 #include <QCoreApplication>
37 class COMMON_EXPORT SettingsChangeNotifier : public QObject
42 void valueChanged(const QVariant &newValue);
45 friend class Settings;
49 class COMMON_EXPORT Settings
52 enum Mode { Default, Custom };
55 //! Calls the given slot on change of the given key
56 template<typename Receiver, typename Slot>
57 void notify(const QString &key, const Receiver *receiver, Slot slot) const
59 static_assert(!std::is_same<Slot, const char*>::value, "Old-style slots not supported");
60 QObject::connect(notifier(normalizedKey(_group, keyForNotify(key))), &SettingsChangeNotifier::valueChanged, receiver, slot);
63 //! Sets up notification and calls the given slot to set the initial value
64 template<typename Receiver, typename Slot>
65 void initAndNotify(const QString &key, const Receiver *receiver, Slot slot, const QVariant &defaultValue = {}) const
67 notify(key, receiver, std::move(slot));
68 auto notifyKey = keyForNotify(key);
69 emit notifier(normalizedKey(_group, notifyKey))->valueChanged(localValue(notifyKey, defaultValue));
73 * Get the major configuration version
75 * This indicates the backwards/forwards incompatible version of configuration.
77 * @return Major configuration version (the X in XX.YY)
79 virtual uint version() const;
82 * Get the minor configuration version
84 * This indicates the backwards/forwards compatible version of configuration.
86 * @see Settings::setVersionMinor()
87 * @return Minor configuration version (the Y in XX.YY)
89 virtual uint versionMinor() const;
92 * Set the minor configuration version
94 * When making backwards/forwards compatible changes, call this with the new version number.
95 * This does not implement any upgrade logic; implement that when checking Settings::version(),
96 * e.g. in Core::Core() and QtUiApplication::init().
98 * @param[in] versionMinor New minor version number
100 virtual void setVersionMinor(const uint versionMinor);
103 * Persist unsaved changes to permanent storage
105 * @return true if succeeded, false otherwise
110 * Check if the configuration storage is writable.
112 * @return true if writable, false otherwise
114 bool isWritable() const;
117 Settings(QString group, QString appName);
118 virtual ~Settings() = default;
120 void setGroup(QString group);
123 * Allows subclasses to transform the key given to notify().
125 * Default implementation just returns the given key.
127 * @param key Key given to notify()
128 * @returns Key that should be used for notfication
130 virtual QString keyForNotify(const QString &key) const;
132 virtual QStringList allLocalKeys() const;
133 virtual QStringList localChildKeys(const QString &rootkey = QString()) const;
134 virtual QStringList localChildGroups(const QString &rootkey = QString()) const;
136 virtual void setLocalValue(const QString &key, const QVariant &data);
137 virtual QVariant localValue(const QString &key, const QVariant &def = QVariant()) const;
140 * Gets if a key exists in settings
142 * @param[in] key ID of local settings key
143 * @returns True if key exists in settings, otherwise false
145 virtual bool localKeyExists(const QString &key) const;
147 virtual void removeLocalKey(const QString &key);
153 QSettings::Format format() const;
155 QString fileName() const;
157 QString normalizedKey(const QString &group, const QString &key) const;
160 * Update the cache of whether or not a given settings key persists on disk
162 * @param normKey Normalized settings key ID
163 * @param exists True if key exists, otherwise false
165 void setCacheKeyPersisted(const QString &normKey, bool exists) const;
168 * Check if the given settings key ID persists on disk (rather than being a default value)
170 * @see Settings::localKeyExists()
172 * @param normKey Normalized settings key ID
173 * @return True if key exists and persistence has been cached, otherwise false
175 bool cacheKeyPersisted(const QString &normKey) const;
178 * Check if the persistence of the given settings key ID has been cached
180 * @param normKey Normalized settings key ID
181 * @return True if key persistence has been cached, otherwise false
183 bool isKeyPersistedCached(const QString &normKey) const;
185 void setCacheValue(const QString &normKey, const QVariant &data) const;
187 QVariant cacheValue(const QString &normKey) const;
189 bool isCached(const QString &normKey) const;
191 SettingsChangeNotifier *notifier(const QString &normKey) const;
193 bool hasNotifier(const QString &normKey) const;
196 static QHash<QString, QVariant> _settingsCache; ///< Cached settings values
197 static QHash<QString, bool> _settingsKeyPersistedCache; ///< Cached settings key exists on disk
198 static QHash<QString, std::shared_ptr<SettingsChangeNotifier>> _settingsChangeNotifier;