1 /***************************************************************************
2 * Copyright (C) 2005-2019 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>
29 #include <QCoreApplication>
37 class COMMON_EXPORT SettingsChangeNotifier : public QObject
42 void valueChanged(const QVariant& newValue);
45 friend class Settings;
48 class COMMON_EXPORT Settings
58 //! Calls the given slot on change of the given key
59 template<typename Receiver, typename Slot>
60 void notify(const QString& key, const Receiver* receiver, Slot slot) const
62 static_assert(!std::is_same<Slot, const char*>::value, "Old-style slots not supported");
63 QObject::connect(notifier(normalizedKey(_group, keyForNotify(key))), &SettingsChangeNotifier::valueChanged, receiver, slot);
66 //! Sets up notification and calls the given slot to set the initial value
67 template<typename Receiver, typename Slot>
68 void initAndNotify(const QString& key, const Receiver* receiver, Slot slot, const QVariant& defaultValue = {}) const
70 notify(key, receiver, std::move(slot));
71 auto notifyKey = keyForNotify(key);
72 emit notifier(normalizedKey(_group, notifyKey))->valueChanged(localValue(notifyKey, defaultValue));
76 * Get the major configuration version
78 * This indicates the backwards/forwards incompatible version of configuration.
80 * @return Major configuration version (the X in XX.YY)
82 virtual uint version() const;
85 * Get the minor configuration version
87 * This indicates the backwards/forwards compatible version of configuration.
89 * @see Settings::setVersionMinor()
90 * @return Minor configuration version (the Y in XX.YY)
92 virtual uint versionMinor() const;
95 * Set the minor configuration version
97 * When making backwards/forwards compatible changes, call this with the new version number.
98 * This does not implement any upgrade logic; implement that when checking Settings::version(),
99 * e.g. in Core::Core() and QtUiApplication::init().
101 * @param[in] versionMinor New minor version number
103 virtual void setVersionMinor(const uint versionMinor);
106 * Persist unsaved changes to permanent storage
108 * @return true if succeeded, false otherwise
113 * Check if the configuration storage is writable.
115 * @return true if writable, false otherwise
117 bool isWritable() const;
120 Settings(QString group, QString appName);
121 virtual ~Settings() = default;
123 void setGroup(QString group);
126 * Allows subclasses to transform the key given to notify().
128 * Default implementation just returns the given key.
130 * @param key Key given to notify()
131 * @returns Key that should be used for notfication
133 virtual QString keyForNotify(const QString& key) const;
135 virtual QStringList allLocalKeys() const;
136 virtual QStringList localChildKeys(const QString& rootkey = QString()) const;
137 virtual QStringList localChildGroups(const QString& rootkey = QString()) const;
139 virtual void setLocalValue(const QString& key, const QVariant& data);
140 virtual QVariant localValue(const QString& key, const QVariant& def = QVariant()) const;
143 * Gets if a key exists in settings
145 * @param[in] key ID of local settings key
146 * @returns True if key exists in settings, otherwise false
148 virtual bool localKeyExists(const QString& key) const;
150 virtual void removeLocalKey(const QString& key);
156 QSettings::Format format() const;
158 QString fileName() const;
160 QString normalizedKey(const QString& group, const QString& key) const;
163 * Update the cache of whether or not a given settings key persists on disk
165 * @param normKey Normalized settings key ID
166 * @param exists True if key exists, otherwise false
168 void setCacheKeyPersisted(const QString& normKey, bool exists) const;
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 bool cacheKeyPersisted(const QString& normKey) const;
181 * Check if the persistence of the given settings key ID has been cached
183 * @param normKey Normalized settings key ID
184 * @return True if key persistence has been cached, otherwise false
186 bool isKeyPersistedCached(const QString& normKey) const;
188 void setCacheValue(const QString& normKey, const QVariant& data) const;
190 QVariant cacheValue(const QString& normKey) const;
192 bool isCached(const QString& normKey) const;
194 SettingsChangeNotifier* notifier(const QString& normKey) const;
196 bool hasNotifier(const QString& normKey) const;
199 static QHash<QString, QVariant> _settingsCache; ///< Cached settings values
200 static QHash<QString, bool> _settingsKeyPersistedCache; ///< Cached settings key exists on disk
201 static QHash<QString, std::shared_ptr<SettingsChangeNotifier>> _settingsChangeNotifier;