modernize: Remove old-style slot usage in NetworkModelController
[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 <memory>
26
27 #include <QCoreApplication>
28 #include <QHash>
29 #include <QSettings>
30 #include <QString>
31 #include <QVariant>
32 #include <utility>
33
34 #include "quassel.h"
35
36 class COMMON_EXPORT SettingsChangeNotifier : public QObject
37 {
38     Q_OBJECT
39
40 signals:
41     void valueChanged(const QVariant &newValue);
42
43 private:
44     friend class Settings;
45 };
46
47
48 class COMMON_EXPORT Settings
49 {
50 public:
51     enum Mode { Default, Custom };
52
53 public:
54     //! Call the given slot on change of the given key
55     void notify(const QString &key, QObject *receiver, const char *slot) const;
56
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;
59
60     /**
61      * Get the major configuration version
62      *
63      * This indicates the backwards/forwards incompatible version of configuration.
64      *
65      * @return Major configuration version (the X in XX.YY)
66      */
67     virtual uint version() const;
68
69     /**
70      * Get the minor configuration version
71      *
72      * This indicates the backwards/forwards compatible version of configuration.
73      *
74      * @see Settings::setVersionMinor()
75      * @return Minor configuration version (the Y in XX.YY)
76      */
77     virtual uint versionMinor() const;
78
79     /**
80      * Set the minor configuration version
81      *
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().
85      *
86      * @param[in] versionMinor New minor version number
87      */
88     virtual void setVersionMinor(const uint versionMinor);
89
90     /**
91      * Persist unsaved changes to permanent storage
92      *
93      * @return true if succeeded, false otherwise
94      */
95     bool sync();
96
97     /**
98      * Check if the configuration storage is writable.
99      *
100      * @return true if writable, false otherwise
101      */
102     bool isWritable() const;
103
104 protected:
105     Settings(QString group, QString appName);
106     virtual ~Settings() = default;
107
108     void setGroup(QString group);
109
110     virtual QStringList allLocalKeys() const;
111     virtual QStringList localChildKeys(const QString &rootkey = QString()) const;
112     virtual QStringList localChildGroups(const QString &rootkey = QString()) const;
113
114     virtual void setLocalValue(const QString &key, const QVariant &data);
115     virtual QVariant localValue(const QString &key, const QVariant &def = QVariant()) const;
116
117     /**
118      * Gets if a key exists in settings
119      *
120      * @param[in] key ID of local settings key
121      * @returns True if key exists in settings, otherwise false
122      */
123     virtual bool localKeyExists(const QString &key) const;
124
125     virtual void removeLocalKey(const QString &key);
126
127     QString _group;
128     QString _appName;
129
130 private:
131     QSettings::Format format() const;
132
133     QString fileName() const;
134
135     QString normalizedKey(const QString &group, const QString &key) const;
136
137     /**
138      * Update the cache of whether or not a given settings key persists on disk
139      *
140      * @param normKey Normalized settings key ID
141      * @param exists  True if key exists, otherwise false
142      */
143     void setCacheKeyPersisted(const QString &normKey, bool exists) const;
144
145     /**
146      * Check if the given settings key ID persists on disk (rather than being a default value)
147      *
148      * @see Settings::localKeyExists()
149      *
150      * @param normKey Normalized settings key ID
151      * @return True if key exists and persistence has been cached, otherwise false
152      */
153     bool cacheKeyPersisted(const QString &normKey) const;
154
155     /**
156      * Check if the persistence of the given settings key ID has been cached
157      *
158      * @param normKey Normalized settings key ID
159      * @return True if key persistence has been cached, otherwise false
160      */
161     bool isKeyPersistedCached(const QString &normKey) const;
162
163     void setCacheValue(const QString &normKey, const QVariant &data) const;
164
165     QVariant cacheValue(const QString &normKey) const;
166
167     bool isCached(const QString &normKey) const;
168
169     SettingsChangeNotifier *notifier(const QString &normKey) const;
170
171     bool hasNotifier(const QString &normKey) const;
172
173 private:
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;
177 };