Debian Codegraph @

Dekko/backend/settings/SettingsObjectBase.h

raw symbols SCIP
1 /* Copyright (C) 2014-2016 Dan Chapman <[email protected]>
2
3 This file is part of Dekko email client for Ubuntu Devices/
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of
8 the License or (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, see <http://www.gnu.org/licenses/>.
17 */
18 #ifndef SETTINGSOBJECTBASE_H
19 #define SETTINGSOBJECTBASE_H
20
21 #include <QObject>
22 #include <QStringList>
23 #include <QJsonObject>
24 #include <QJsonValue>
25 #include <QJsonArray>
26 #include <QDateTime>
27 #include <QVariantMap>
28 #include <QSharedPointer>
29 #include <QScopedPointer>
30 #include <QLockFile>
31 #include "SettingsFileBase.h"
32
33 class SettingsFileBase;
34 /**
35 * @brief The SettingsObjectBase class
36 *
37 * Provides a settings object that supports atomic writes and change notifications across
38 * multiple instances for a single process.
39 *
40 * The first created instance holds the lockfile for this process.
41 */
42 class SettingsObjectBase : public QObject
43 {
44 Q_OBJECT
45 /**
46 * @brief Raw data object stored in the settings file
47 *
48 * Individual object properties can be access in QML using dot notation
49 *
50 * @code
51 * import QtQuick 2.4
52 * import Dekko.Settings 1.0
53 *
54 * Item {
55 * property string labelVal: settings.data.label.value
56 *
57 * SettingsObject {
58 * id: settings
59 * Component.onCompleted: {
60 * write("label.value", "Hello world!")
61 * }
62 * }
63 * }
64 * @endcode
65 *
66 * This means you can bind to properties and get change notifications in QML.
67 *
68 * @accessors %data(), setData()
69 */
70 Q_PROPERTY(QJsonObject data READ data WRITE setData NOTIFY dataChanged)
71 /**
72 * @brief The base path used to resolve QJsonObject returned by data()
73 *
74 * You can position a SettingsObjectBase instance at a specific point in the json hierachy
75 * if you are only interested in accessing or getting change notifications for certain properties.
76 *
77 * Say we have a JSON object that looks like
78 *
79 * @code
80 * {
81 * "accounts": {
82 * "account1" : {
83 * "id": 0,
84 * "name": "Foo Bar"
85 * },
86 * "account2" : {
87 * "id": 1,
88 * "name": "Bar Foo"
89 * }
90 * }
91 * }
92 * @endcode
93 *
94 * By setting the path to "accounts.account2" only the account2 object will be returned by data().
95 * So in QML you would write something like settings.data.name and not settings.data.account2.name.
96 * The result is you will only get change notifications for changes to account2 :-)
97 *
98 * @accessors %path(), setPath()
99 */
100 Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged)
101 /**
102 * @brief Is the QJsonObject at path() valid
103 * @accessors %isValid()
104 */
105 Q_PROPERTY(bool isValid READ isValid NOTIFY dataChanged)
106
107 public:
108 explicit SettingsObjectBase(QObject *parent = 0);
109
110 QJsonObject data() const;
111 void setData(const QJsonObject &data);
112 QString path() const;
113 void setPath(const QString &path);
114 bool isValid() const;
115 bool hasKey() const { return !m_settingsKey.isEmpty(); }
116
117 Q_INVOKABLE QJsonValue read(const QString &key, const QJsonValue &defaultValue = QJsonValue::Undefined) const;
118 template<typename T> T read(const QString &key) const;
119 Q_INVOKABLE void write(const QString &key, const QJsonValue &value);
120 template<typename T> void write(const QString &key, const T &value);
121
122 QJsonValue read(const char *key, const QJsonValue &defaultValue = QJsonValue::Undefined) const
123 {
124 return read(QString::fromLatin1(key), defaultValue);
125 }
126 template<typename T> T read(const char *key) const
127 {
128 return read<T>(QString::fromLatin1(key));
129 }
130 void write(const char *key, const QJsonValue &value)
131 {
132 write(QString::fromLatin1(key), value);
133 }
134 template<typename T> void write(const char *key, const T &value)
135 {
136 write<T>(QString::fromLatin1(key), value);
137 }
138
139 Q_INVOKABLE void undefine();
140
141 signals:
142 void pathChanged();
143 void dataChanged();
144 void modified(const QString &path, const QJsonValue &value);
145 public slots:
146 void modified(const QStringList &path, const QJsonValue &value);
147
148 protected:
149 virtual void createDefaultsIfNotExist();
150 void setSettingsKey(const QString &key);
151
152 private:
153 void setFile(QSharedPointer<SettingsFileBase> file);
154 QSharedPointer<SettingsFileBase> m_file;
155 QScopedPointer<QLockFile> *m_lock;
156 QStringList m_path;
157 QJsonObject m_object;
158 bool m_invalid;
159 QString m_settingsKey;
160
161 };
162 template<typename T> inline void SettingsObjectBase::write(const QString &key, const T &value)
163 {
164 write(key, QJsonValue(value));
165 }
166
167 template<> inline QString SettingsObjectBase::read<QString>(const QString &key) const
168 {
169 return read(key).toString();
170 }
171
172 template<> inline QJsonArray SettingsObjectBase::read<QJsonArray>(const QString &key) const
173 {
174 return read(key).toArray();
175 }
176
177 template<> inline QJsonObject SettingsObjectBase::read<QJsonObject>(const QString &key) const
178 {
179 return read(key).toObject();
180 }
181
182 template<> inline double SettingsObjectBase::read<double>(const QString &key) const
183 {
184 return read(key).toDouble();
185 }
186
187 template<> inline int SettingsObjectBase::read<int>(const QString &key) const
188 {
189 return read(key).toInt();
190 }
191
192 template<> inline bool SettingsObjectBase::read<bool>(const QString &key) const
193 {
194 return read(key).toBool();
195 }
196
197 template<> inline QDateTime SettingsObjectBase::read<QDateTime>(const QString &key) const
198 {
199 QString value = read(key).toString();
200 if (value.isEmpty())
201 return QDateTime();
202 return QDateTime::fromString(value, Qt::ISODate).toLocalTime();
203 }
204
205 template<> inline void SettingsObjectBase::write<QDateTime>(const QString &key, const QDateTime &value)
206 {
207 write(key, QJsonValue(value.toUTC().toString(Qt::ISODate)));
208 }
209
210 #endif // SETTINGSOBJECTBASE_H