1a0fa6d7f9ee5bfd9198fc799c23326ed5a0b7b7
[quassel.git] / src / common / settings.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #pragma once
22
23 #include "common-export.h"
24
25 #include <QCoreApplication>
26 #include <QHash>
27 #include <QSettings>
28 #include <QString>
29 #include <QVariant>
30 #include <utility>
31
32 #include "quassel.h"
33
34 class COMMON_EXPORT SettingsChangeNotifier : public QObject
35 {
36     Q_OBJECT
37
38 signals:
39     void valueChanged(const QVariant &newValue);
40
41 private:
42     friend class Settings;
43 };
44
45
46 class COMMON_EXPORT Settings
47 {
48 public:
49     enum Mode { Default, Custom };
50
51 public:
52     //! Call the given slot on change of the given key
53     virtual void notify(const QString &key, QObject *receiver, const char *slot);
54
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());
57
58     /**
59      * Get the major configuration version
60      *
61      * This indicates the backwards/forwards incompatible version of configuration.
62      *
63      * @return Major configuration version (the X in XX.YY)
64      */
65     virtual uint version();
66
67     /**
68      * Get the minor configuration version
69      *
70      * This indicates the backwards/forwards compatible version of configuration.
71      *
72      * @see Settings::setVersionMinor()
73      * @return Minor configuration version (the Y in XX.YY)
74      */
75     virtual uint versionMinor();
76
77     /**
78      * Set the minor configuration version
79      *
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().
83      *
84      * @param[in] versionMinor New minor version number
85      */
86     virtual void setVersionMinor(const uint versionMinor);
87
88     /**
89      * Persist unsaved changes to permanent storage
90      *
91      * @return true if succeeded, false otherwise
92      */
93     bool sync();
94
95     /**
96      * Check if the configuration storage is writable.
97      *
98      * @return true if writable, false otherwise
99      */
100     bool isWritable();
101
102 protected:
103     inline Settings(QString group_, QString appName_) : group(std::move(group_)), appName(std::move(appName_)) {}
104     inline virtual ~Settings() = default;
105
106     inline void setGroup(const QString &group_) { group = group_; }
107
108     virtual QStringList allLocalKeys();
109     virtual QStringList localChildKeys(const QString &rootkey = QString());
110     virtual QStringList localChildGroups(const QString &rootkey = QString());
111
112     virtual void setLocalValue(const QString &key, const QVariant &data);
113     virtual QVariant localValue(const QString &key, const QVariant &def = QVariant());
114
115     /**
116      * Gets if a key exists in settings
117      *
118      * @param[in] key ID of local settings key
119      * @returns True if key exists in settings, otherwise false
120      */
121     virtual bool localKeyExists(const QString &key);
122
123     virtual void removeLocalKey(const QString &key);
124
125     QString group;
126     QString appName;
127
128 private:
129     inline QSettings::Format format()
130     {
131 #ifdef Q_OS_WIN
132         return QSettings::IniFormat;
133 #else
134         return QSettings::NativeFormat;
135 #endif
136     }
137
138
139     inline QString fileName()
140     {
141         return Quassel::configDirPath() + appName
142                + ((format() == QSettings::NativeFormat) ? QLatin1String(".conf") : QLatin1String(".ini"));
143     }
144
145
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;
149
150     inline QString normalizedKey(const QString &group, const QString &key)
151     {
152         if (group.isEmpty())
153             return key;
154         return group + '/' + key;
155     }
156
157
158     /**
159      * Update the cache of whether or not a given settings key persists on disk
160      *
161      * @param normKey Normalized settings key ID
162      * @param exists  True if key exists, otherwise false
163      */
164     inline void setCacheKeyPersisted(const QString &normKey, bool exists)
165     {
166         settingsKeyPersistedCache[normKey] = exists;
167     }
168
169
170     /**
171      * Check if the given settings key ID persists on disk (rather than being a default value)
172      *
173      * @see Settings::localKeyExists()
174      *
175      * @param normKey Normalized settings key ID
176      * @return True if key exists and persistence has been cached, otherwise false
177      */
178     inline const bool &cacheKeyPersisted(const QString &normKey)
179     {
180         return settingsKeyPersistedCache[normKey];
181     }
182
183
184     /**
185      * Check if the persistence of the given settings key ID has been cached
186      *
187      * @param normKey Normalized settings key ID
188      * @return True if key persistence has been cached, otherwise false
189      */
190     inline bool isKeyPersistedCached(const QString &normKey)
191     {
192         return settingsKeyPersistedCache.contains(normKey);
193     }
194
195
196     inline void setCacheValue(const QString &normKey, const QVariant &data)
197     {
198         settingsCache[normKey] = data;
199     }
200
201
202     inline const QVariant &cacheValue(const QString &normKey)
203     {
204         return settingsCache[normKey];
205     }
206
207
208     inline bool isCached(const QString &normKey)
209     {
210         return settingsCache.contains(normKey);
211     }
212
213
214     inline SettingsChangeNotifier *notifier(const QString &normKey)
215     {
216         if (!hasNotifier(normKey))
217             settingsChangeNotifier[normKey] = new SettingsChangeNotifier();
218         return settingsChangeNotifier[normKey];
219     }
220
221
222     inline bool hasNotifier(const QString &normKey)
223     {
224         return settingsChangeNotifier.contains(normKey);
225     }
226 };