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"
27 #include <QCoreApplication>
36 class COMMON_EXPORT SettingsChangeNotifier : public QObject
41 void valueChanged(const QVariant &newValue);
44 friend class Settings;
48 class COMMON_EXPORT Settings
51 enum Mode { Default, Custom };
54 //! Call the given slot on change of the given key
55 void notify(const QString &key, QObject *receiver, const char *slot) const;
57 //! Sets up notification and calls the given slot to set the initial value
58 void initAndNotify(const QString &key, QObject *receiver, const char *slot, const QVariant &defaultValue = QVariant()) const;
61 * Get the major configuration version
63 * This indicates the backwards/forwards incompatible version of configuration.
65 * @return Major configuration version (the X in XX.YY)
67 virtual uint version() const;
70 * Get the minor configuration version
72 * This indicates the backwards/forwards compatible version of configuration.
74 * @see Settings::setVersionMinor()
75 * @return Minor configuration version (the Y in XX.YY)
77 virtual uint versionMinor() const;
80 * Set the minor configuration version
82 * When making backwards/forwards compatible changes, call this with the new version number.
83 * This does not implement any upgrade logic; implement that when checking Settings::version(),
84 * e.g. in Core::Core() and QtUiApplication::init().
86 * @param[in] versionMinor New minor version number
88 virtual void setVersionMinor(const uint versionMinor);
91 * Persist unsaved changes to permanent storage
93 * @return true if succeeded, false otherwise
98 * Check if the configuration storage is writable.
100 * @return true if writable, false otherwise
102 bool isWritable() const;
105 Settings(QString group, QString appName);
106 virtual ~Settings() = default;
108 void setGroup(QString group);
110 virtual QStringList allLocalKeys() const;
111 virtual QStringList localChildKeys(const QString &rootkey = QString()) const;
112 virtual QStringList localChildGroups(const QString &rootkey = QString()) const;
114 virtual void setLocalValue(const QString &key, const QVariant &data);
115 virtual QVariant localValue(const QString &key, const QVariant &def = QVariant()) const;
118 * Gets if a key exists in settings
120 * @param[in] key ID of local settings key
121 * @returns True if key exists in settings, otherwise false
123 virtual bool localKeyExists(const QString &key) const;
125 virtual void removeLocalKey(const QString &key);
131 QSettings::Format format() const;
133 QString fileName() const;
135 QString normalizedKey(const QString &group, const QString &key) const;
138 * Update the cache of whether or not a given settings key persists on disk
140 * @param normKey Normalized settings key ID
141 * @param exists True if key exists, otherwise false
143 void setCacheKeyPersisted(const QString &normKey, bool exists) const;
146 * Check if the given settings key ID persists on disk (rather than being a default value)
148 * @see Settings::localKeyExists()
150 * @param normKey Normalized settings key ID
151 * @return True if key exists and persistence has been cached, otherwise false
153 bool cacheKeyPersisted(const QString &normKey) const;
156 * Check if the persistence of the given settings key ID has been cached
158 * @param normKey Normalized settings key ID
159 * @return True if key persistence has been cached, otherwise false
161 bool isKeyPersistedCached(const QString &normKey) const;
163 void setCacheValue(const QString &normKey, const QVariant &data) const;
165 QVariant cacheValue(const QString &normKey) const;
167 bool isCached(const QString &normKey) const;
169 SettingsChangeNotifier *notifier(const QString &normKey) const;
171 bool hasNotifier(const QString &normKey) const;
174 static QHash<QString, QVariant> _settingsCache; ///< Cached settings values
175 static QHash<QString, bool> _settingsKeyPersistedCache; ///< Cached settings key exists on disk
176 static QHash<QString, std::shared_ptr<SettingsChangeNotifier>> _settingsChangeNotifier;