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