-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
SettingsManager.cpp
127 lines (107 loc) · 4.05 KB
/
SettingsManager.cpp
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
/*---------------------------------------------------------*\
| 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-only |
\*---------------------------------------------------------*/
#include <fstream>
#include <iostream>
#include "SettingsManager.h"
#include "LogManager.h"
SettingsManager::SettingsManager()
{
config_found = false;
}
SettingsManager::~SettingsManager()
{
}
json SettingsManager::GetSettings(std::string settings_key)
{
/*---------------------------------------------------------*\
| 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 |
\*---------------------------------------------------------*/
json result;
mutex.lock();
if(settings_data.contains(settings_key))
{
result = settings_data[settings_key];
}
mutex.unlock();
return result;
}
void SettingsManager::SetSettings(std::string settings_key, json new_settings)
{
mutex.lock();
settings_data[settings_key] = new_settings;
mutex.unlock();
}
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()
{
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("[SettingsManager] Cannot write to file: %s", e.what());
}
settings_file.close();
}
mutex.unlock();
}