-
-
Notifications
You must be signed in to change notification settings - Fork 379
Expand file tree
/
Copy pathSettingsManager.cpp
More file actions
380 lines (321 loc) · 13.1 KB
/
Copy pathSettingsManager.cpp
File metadata and controls
380 lines (321 loc) · 13.1 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*---------------------------------------------------------*\
| SettingsManager.cpp |
| |
| OpenRGB Settings Manager maintains a list of application|
| settings in JSON format. Other components may register |
| settings with this class and store/load values. |
| |
| Adam Honse (CalcProgrammer1) 04 Nov 2020 |
| |
| This file is part of the OpenRGB project |
| SPDX-License-Identifier: GPL-2.0-or-later |
\*---------------------------------------------------------*/
#include <fstream>
#include <iostream>
#include "JsonUtils.h"
#include "LogManager.h"
#include "NetworkClient.h"
#include "ResourceManager.h"
#include "SettingsManager.h"
#include "StringUtils.h"
/*---------------------------------------------------------*\
| SettingsManager name for log entries |
\*---------------------------------------------------------*/
const char* SETTINGSMANAGER = "SettingsManager";
static const std::string ui_settings_keys[4] =
{
"UserInterface",
"AutoStart",
"Plugins",
"Client",
};
SettingsManager::SettingsManager()
{
config_found = false;
}
SettingsManager::~SettingsManager()
{
}
json SettingsManager::GetSettings(std::string settings_key)
{
json result;
bool ui_settings_key = false;
for(std::size_t settings_key_idx = 0; settings_key_idx < 4; settings_key_idx++)
{
if(settings_key == ui_settings_keys[settings_key_idx])
{
ui_settings_key = true;
break;
}
}
if(!ui_settings_key && ResourceManager::get()->IsLocalClient() && (ResourceManager::get()->GetLocalClient()->GetSupportsSettingsManagerAPI()))
{
/*-------------------------------------------------*\
| If this is a local client, request the settings |
| from the server |
\*-------------------------------------------------*/
JsonUtils::JsonParse(ResourceManager::get()->GetLocalClient()->SettingsManager_GetSettings(settings_key), result);
}
else
{
/*-------------------------------------------------*\
| Check to see if the key exists in the settings |
| store and return the settings associated with the |
| key if it exists. We lock the mutex to protect |
| the value from changing while data is being read |
| and copy before unlocking. |
\*-------------------------------------------------*/
mutex.lock();
if(settings_data.contains(settings_key))
{
result = settings_data[settings_key];
}
mutex.unlock();
}
return result;
}
json SettingsManager::GetSettingsSchema(std::string settings_key)
{
if(settings_key == "")
{
return(settings_schema);
}
else if(settings_schema.contains(settings_key) && settings_schema[settings_key].contains("properties"))
{
return(settings_schema[settings_key]["properties"]);
}
else
{
nlohmann::json empty;
return(empty);
}
}
void SettingsManager::RegisterSettingsSchema(std::string settings_key, std::string settings_title, json& new_schema)
{
RegisterSettingsSchema(settings_key, settings_title, new_schema, -1);
}
void SettingsManager::RegisterSettingsSchema(std::string settings_key, std::string settings_title, json& new_schema, int order)
{
settings_schema[settings_key]["title"] = settings_title;
settings_schema[settings_key]["type"] = "object";
settings_schema[settings_key]["properties"].update(new_schema, true);
if(order >= 0)
{
settings_schema[settings_key]["order"] = order;
}
SignalSettingsManagerUpdate(SETTINGSMANAGER_UPDATE_REASON_SETTINGS_SCHEMA_UPDATED);
}
void SettingsManager::ModifySettings(std::string settings_key, json new_settings)
{
bool ui_settings_key = false;
for(std::size_t settings_key_idx = 0; settings_key_idx < 4; settings_key_idx++)
{
if(settings_key == ui_settings_keys[settings_key_idx])
{
ui_settings_key = true;
break;
}
}
if(!ui_settings_key && ResourceManager::get()->IsLocalClient() && (ResourceManager::get()->GetLocalClient()->GetSupportsSettingsManagerAPI()))
{
/*-------------------------------------------------*\
| If this is a local client, request the settings |
| from the server |
\*-------------------------------------------------*/
nlohmann::json settings_json;
settings_json[settings_key] = new_settings;
ResourceManager::get()->GetLocalClient()->SettingsManager_ModifySettings(settings_json.dump());
}
else
{
mutex.lock();
settings_data[settings_key].update(new_settings, true);
mutex.unlock();
}
SignalSettingsManagerUpdate(SETTINGSMANAGER_UPDATE_REASON_SETTINGS_UPDATED);
}
void SettingsManager::ModifySettingsFromJsonString(std::string settings_json_str)
{
/*-----------------------------------------------------*\
| Parse the JSON string |
\*-----------------------------------------------------*/
nlohmann::json settings_json;
JsonUtils::JsonParse(settings_json_str, settings_json);
/*-----------------------------------------------------*\
| Get key/value pairs from JSON, call SetSettings for |
| each key. This use of `auto` is acceptable due to |
| how the JSON library implements iterators, the type |
| would change based on the library version. |
\*-----------------------------------------------------*/
for(auto& element : settings_json.items())
{
ModifySettings(element.key(), element.value());
}
}
void SettingsManager::SetSettings(std::string settings_key, json new_settings)
{
bool ui_settings_key = false;
for(std::size_t settings_key_idx = 0; settings_key_idx < 4; settings_key_idx++)
{
if(settings_key == ui_settings_keys[settings_key_idx])
{
ui_settings_key = true;
break;
}
}
if(!ui_settings_key && ResourceManager::get()->IsLocalClient() && (ResourceManager::get()->GetLocalClient()->GetSupportsSettingsManagerAPI()))
{
/*-------------------------------------------------*\
| If this is a local client, request the settings |
| from the server |
\*-------------------------------------------------*/
nlohmann::json settings_json;
settings_json[settings_key] = new_settings;
ResourceManager::get()->GetLocalClient()->SettingsManager_SetSettings(settings_json.dump());
}
else
{
mutex.lock();
settings_data[settings_key] = new_settings;
mutex.unlock();
}
SignalSettingsManagerUpdate(SETTINGSMANAGER_UPDATE_REASON_SETTINGS_UPDATED);
}
void SettingsManager::SetSettingsFromJsonString(std::string settings_json_str)
{
/*-----------------------------------------------------*\
| Parse the JSON string |
\*-----------------------------------------------------*/
nlohmann::json settings_json;
JsonUtils::JsonParse(settings_json_str, settings_json);
/*-----------------------------------------------------*\
| Get key/value pairs from JSON, call SetSettings for |
| each key. This use of `auto` is acceptable due to |
| how the JSON library implements iterators, the type |
| would change based on the library version. |
\*-----------------------------------------------------*/
for(auto& element : settings_json.items())
{
SetSettings(element.key(), element.value());
}
}
void SettingsManager::LoadSettings(const filesystem::path& filename)
{
/*-----------------------------------------------------*\
| Clear any stored settings before loading |
\*-----------------------------------------------------*/
mutex.lock();
settings_data.clear();
/*-----------------------------------------------------*\
| Store settings filename, so we can save to it later |
\*-----------------------------------------------------*/
settings_filename = filename;
/*-----------------------------------------------------*\
| Open input file in binary mode |
\*-----------------------------------------------------*/
config_found = filesystem::exists(filename);
if(config_found)
{
std::ifstream settings_file(settings_filename, std::ios::in | std::ios::binary);
/*-------------------------------------------------*\
| Read settings into JSON store |
\*-------------------------------------------------*/
if(settings_file)
{
try
{
settings_file >> settings_data;
}
catch(const std::exception& e)
{
/*-----------------------------------------*\
| If an exception was caught, that means |
| the JSON parsing failed. Clear out any |
| data in the store as it is corrupt. We |
| could attempt a reload for backup |
| location |
\*-----------------------------------------*/
LOG_ERROR("[SettingsManager] JSON parsing failed: %s", e.what());
settings_data.clear();
}
}
settings_file.close();
}
mutex.unlock();
}
void SettingsManager::SaveSettings()
{
if(ResourceManager::get()->IsLocalClient() && (ResourceManager::get()->GetLocalClient()->GetSupportsSettingsManagerAPI()))
{
/*-------------------------------------------------*\
| If this is a local client, save the settings on |
| the server |
\*-------------------------------------------------*/
ResourceManager::get()->GetLocalClient()->SettingsManager_SaveSettings();
}
mutex.lock();
std::ofstream settings_file(settings_filename, std::ios::out | std::ios::binary);
if(settings_file)
{
try
{
settings_file << settings_data.dump(4);
}
catch(const std::exception& e)
{
LOG_ERROR("[%s] Cannot write to file: %s", SETTINGSMANAGER, e.what());
}
settings_file.close();
}
mutex.unlock();
}
/*---------------------------------------------------------*\
| Callback Registration Functions |
\*---------------------------------------------------------*/
void SettingsManager::RegisterSettingsManagerCallback(SettingsManagerCallback new_callback, void * new_callback_arg)
{
SettingsManagerCallbackMutex.lock();
for(size_t idx = 0; idx < SettingsManagerCallbacks.size(); idx++)
{
if(SettingsManagerCallbackArgs[idx] == new_callback && SettingsManagerCallbackArgs[idx] == new_callback_arg)
{
SettingsManagerCallbackMutex.unlock();
LOG_TRACE("[%s] Tried to register an already registered SettingsManager callback, skipping. Total callbacks registered: %d", SETTINGSMANAGER, SettingsManagerCallbacks.size());
return;
}
}
SettingsManagerCallbacks.push_back(new_callback);
SettingsManagerCallbackArgs.push_back(new_callback_arg);
SettingsManagerCallbackMutex.unlock();
LOG_TRACE("[%s] Registered SettingsManager callback. Total callbacks registered: %d", SETTINGSMANAGER, SettingsManagerCallbacks.size());
}
void SettingsManager::UnregisterSettingsManagerCallback(SettingsManagerCallback callback, void * callback_arg)
{
SettingsManagerCallbackMutex.lock();
for(size_t idx = 0; idx < SettingsManagerCallbacks.size(); idx++)
{
if(SettingsManagerCallbacks[idx] == callback && SettingsManagerCallbackArgs[idx] == callback_arg)
{
SettingsManagerCallbacks.erase(SettingsManagerCallbacks.begin() + idx);
SettingsManagerCallbackArgs.erase(SettingsManagerCallbackArgs.begin() + idx);
}
}
SettingsManagerCallbackMutex.unlock();
LOG_TRACE("[%s] Unregistered SettingsManager callback. Total callbacks registered: %d", SETTINGSMANAGER, SettingsManagerCallbackArgs.size());
}
void SettingsManager::SignalSettingsManagerUpdate(unsigned int update_reason)
{
// NetworkServer* server = ResourceManager::get()->GetServer();
//
// if(server)
// {
// server->SignalProfileManagerUpdate(update_reason);
// }
SettingsManagerCallbackMutex.lock();
for(std::size_t callback_idx = 0; callback_idx < SettingsManagerCallbacks.size(); callback_idx++)
{
SettingsManagerCallbacks[callback_idx](SettingsManagerCallbackArgs[callback_idx], update_reason);
}
SettingsManagerCallbackMutex.unlock();
LOG_TRACE("[%s] SettingsManager update signalled: %d", SETTINGSMANAGER, update_reason);
}