forked from YIO-Remote/remote-software
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.h
225 lines (187 loc) Β· 8.5 KB
/
config.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/******************************************************************************
*
* Copyright (C) 2020 Markus Zehnder <business@markuszehnder.ch>
* Copyright (C) 2018-2019 Marton Borzak <hello@martonborzak.com>
*
* This file is part of the YIO-Remote software project.
*
* YIO-Remote software is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* YIO-Remote software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with YIO-Remote software. If not, see <https://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*****************************************************************************/
#pragma once
#include <QJsonArray>
#include <QObject>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "environment.h"
#include "jsonfile.h"
#include "yio-interface/configinterface.h"
#include "yio-interface/unitsystem.h"
class Config : public QObject, public ConfigInterface {
Q_OBJECT
Q_INTERFACES(ConfigInterface)
Q_PROPERTY(bool valid READ isValid CONSTANT)
Q_PROPERTY(QString error READ getError CONSTANT)
Q_PROPERTY(QVariantMap config READ getConfig WRITE setConfig NOTIFY configChanged)
Q_PROPERTY(QString profileId READ getProfileId WRITE setProfileId NOTIFY profileIdChanged)
Q_PROPERTY(QStringList profileFavorites READ profileFavorites NOTIFY profileFavoritesChanged)
Q_PROPERTY(QVariantMap settings READ getSettings WRITE setSettings NOTIFY settingsChanged)
Q_PROPERTY(QVariantMap profiles READ getProfiles NOTIFY profilesChanged)
Q_PROPERTY(QVariantMap ui_config READ getUIConfig WRITE setUIConfig NOTIFY uiConfigChanged)
Q_PROPERTY(QVariantMap pages READ getPages NOTIFY pagesChanged)
Q_PROPERTY(QVariantMap groups READ getGroups NOTIFY groupsChanged)
Q_PROPERTY(UnitSystem::Enum unitSystem READ getUnitSystem WRITE setUnitSystem NOTIFY unitSystemChanged)
public:
static const QString CFG_FILE_NAME;
static const QString CFG_FILE_NAME_DEF;
// Common configuration keys. Initialized in config.cpp
// HELP: anyone knows how to properly define "static const QString" constants across Qt plugin boundaries?
static const QString KEY_ID;
static const QString KEY_FRIENDLYNAME;
static const QString KEY_ENTITY_ID;
static const QString KEY_AREA;
static const QString KEY_INTEGRATION;
static const QString KEY_SUPPORTED_FEATURES;
static const QString KEY_CUSTOM_FEATURES;
static const QString KEY_TYPE;
static const QString KEY_MDNS;
static const QString KEY_WORKERTHREAD;
static const QString OBJ_DATA;
// valid
bool isValid() const { return m_error.isEmpty(); }
// error
QString getError() const { return m_error; }
// config
QVariantMap getConfig() override { return m_config; }
void setConfig(const QVariantMap& config) override;
// profile Id
QString getProfileId() { return m_cacheProfileId; }
void setProfileId(QString id);
// profile favorites
QStringList profileFavorites() { return m_cacheUIProfile.value("favorites").toStringList(); }
// settings
QVariantMap getSettings() override { return m_cacheSettings; }
void setSettings(const QVariantMap& config);
// profiles
QVariantMap getProfiles() { return m_cacheUIProfiles; }
void setProfiles(const QVariantMap& config);
// ui_config
QVariantMap getUIConfig() { return m_cacheUIConfig; }
void setUIConfig(const QVariantMap& config);
// pages
QVariantMap getPages() { return m_cacheUIPages; }
void setPages(const QVariantMap& config);
// groups
QVariantMap getGroups() { return m_cacheUIGroups; }
void setGroups(const QVariantMap& config);
// unit system
UnitSystem::Enum getUnitSystem() override { return m_cacheUnitSystem; }
void setUnitSystem(UnitSystem::Enum value);
// languages
QVariantList getLanguages() { return m_languages; }
// Shortcuts to get the config items, and to decouple a bit from Json structure
// Please avoid old access via read property
Q_INVOKABLE QVariantMap getProfile(const QString& profile) { return m_cacheUIProfiles.value(profile).toMap(); }
Q_INVOKABLE QVariantMap getPage(const QString& pageId) { return m_cacheUIPages.value(pageId).toMap(); }
Q_INVOKABLE QVariantMap getGroup(const QString& groupId) {
return m_cacheUIGroups.value(groupId).toMap();
} // not used anywhere
// The selected, cached profile
Q_INVOKABLE QVariantMap getProfile() { return m_cacheUIProfile; }
Q_INVOKABLE QStringList getProfilePages() { return m_cacheUIProfile.value("pages").toStringList(); }
// set favorite entity
void setFavorite(const QString& entityId, bool value);
// read configuration to file
bool readConfig();
/**
* @brief Persists the configuration. In case of an error, configWriteError is emitted.
* @return true if configuration could be successfully written
*/
bool writeConfig();
// get a QML object, you need to have objectName property of the QML object set to be able to use this
QObject* getQMLObject(QList<QObject*> nodes, const QString& name);
QObject* getQMLObject(const QString& name) override;
// get all integrations and entities
QVariantMap getAllIntegrations() override { return m_config["integrations"].toMap(); }
QVariantMap getIntegration(const QString& type) override { return getAllIntegrations().value(type).toMap(); }
QVariantMap getAllEntities() override { return m_config["entities"].toMap(); }
QVariantList getEntities(const QString& type) override { return getAllEntities().value(type).toList(); }
QQmlApplicationEngine* getAppEngine() { return m_engine; }
/**
* @brief QML facade for Environment::isFirstRun
*/
Q_INVOKABLE bool isFirstRun() { return m_environment->isFirstRun(); }
/**
* @brief QML facade for Environment::finishFirstRun().
*/
Q_INVOKABLE bool finishFirstRun() { return m_environment->finishFirstRun(); }
/**
* @brief Resets the configuration and prepares the remote for the initial setup run.
* The remote must be rebooted after calling this method.
* @return true if successful, false otherwise
*/
bool resetConfigurationForFirstRun();
public:
explicit Config(QQmlApplicationEngine* engine, QString configFilePath, QString schemaFilePath,
Environment* environment);
~Config() override;
static Config* getInstance() { return s_instance; }
signals:
void configChanged();
void profileIdChanged();
void profileFavoritesChanged();
void settingsChanged();
void profilesChanged();
void uiConfigChanged();
void pagesChanged();
void groupsChanged();
void unitSystemChanged();
void configWriteError(const QString& error);
private:
void syncConfigToCache();
void syncCacheToConfig();
template <class EnumClass>
QString enumToString(const EnumClass& enumKey) const {
const auto metaEnum = QMetaEnum::fromType<EnumClass>();
return metaEnum.valueToKey(static_cast<int>(enumKey));
}
template <class EnumClass>
EnumClass stringToEnum(const QVariant& enumString, const EnumClass& defaultValue) const {
const auto metaEnum = QMetaEnum::fromType<EnumClass>();
bool ok;
const auto value = metaEnum.keyToValue(enumString.toString().toUtf8(), &ok);
return ok ? static_cast<EnumClass>(value) : defaultValue;
}
private:
static Config* s_instance;
QQmlApplicationEngine* m_engine;
// loaded configuration values
QVariantMap m_config;
QVariantList m_languages;
// json configuration file
JsonFile m_jsf;
// last read or write error message
QString m_error;
// Caches to improve performance
QString m_cacheProfileId;
QVariantMap m_cacheSettings;
QVariantMap m_cacheUIConfig;
QVariantMap m_cacheUIProfile; // of selected Profile
QVariantMap m_cacheUIProfiles;
QVariantMap m_cacheUIPages;
QVariantMap m_cacheUIGroups;
UnitSystem::Enum m_cacheUnitSystem = UnitSystem::METRIC;
Environment* m_environment;
};