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