/
SettingsSystem.cpp
213 lines (182 loc) · 7.32 KB
/
SettingsSystem.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
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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2004-2020 Darby Johnston
// All rights reserved.
#include <djvUI/SettingsSystem.h>
#include <djvUI/ISettings.h>
#include <djvAV/AVSystem.h>
#include <djvCore/Context.h>
#include <djvCore/Error.h>
#include <djvCore/FileIO.h>
#include <djvCore/FileInfo.h>
#include <djvCore/ResourceSystem.h>
using namespace djv::Core;
namespace djv
{
namespace UI
{
namespace Settings
{
namespace
{
const size_t settingsVersion = 25;
} // namespace
void System::_init(bool reset, const std::shared_ptr<Core::Context>& context)
{
ISystem::_init("djv::UI::Settings::System", context);
//! \todo Is there a better way to disable settings for tests?
if ("djvTest" == context->getName())
{
_settingsIO = false;
}
addDependency(context->getSystemT<AV::AVSystem>());
if (auto resourceSystem = context->getSystemT<ResourceSystem>())
{
_settingsPath = resourceSystem->getPath(FileSystem::ResourcePath::SettingsFile);
}
if (!reset)
{
_readSettingsFile(_settingsPath, _json);
}
}
System::System()
{}
System::~System()
{
_saveSettings();
}
std::shared_ptr<System> System::create(bool reset, const std::shared_ptr<Core::Context>& context)
{
auto out = std::shared_ptr<System>(new System);
out->_init(reset, context);
return out;
}
void System::_addSettings(const std::shared_ptr<ISettings> & value)
{
_settings.push_back(value);
}
void System::_removeSettings(const std::shared_ptr<ISettings> & value)
{
const auto i = std::find(_settings.begin(), _settings.end(), value);
if (i != _settings.end())
{
_settings.erase(i);
}
}
void System::_loadSettings(const std::shared_ptr<ISettings> & settings)
{
if (!_settingsIO)
return;
std::stringstream ss;
ss << "Loading settings: " << settings->getName();
_log(ss.str());
picojson::value json;
const auto & name = settings->getName();
auto i = _json.find(name);
if (i != _json.end())
{
try
{
settings->load(i->second);
}
catch (const std::exception & e)
{
std::stringstream ss;
ss << "Cannot read settings" << " '" << name << "'. " << e.what();
_log(ss.str(), LogLevel::Error);
}
}
}
void System::_saveSettings()
{
if (!_settingsIO)
return;
picojson::value object(picojson::object_type, true);
object.get<picojson::object>()["SettingsVersion"] = toJSON(settingsVersion);
// Serialize the settings.
for (const auto & settings : _settings)
{
object.get<picojson::object>()[settings->getName()] = settings->save();
}
// Write the JSON to disk.
_writeSettingsFile(_settingsPath, object);
}
void System::_readSettingsFile(const FileSystem::Path & path, std::map<std::string, picojson::value> & out)
{
try
{
if (FileSystem::FileInfo(path).doesExist())
{
std::stringstream ss;
ss << "Reading settings: " << path;
_log(ss.str());
auto fileIO = FileSystem::FileIO::create();
fileIO->open(std::string(path), FileSystem::FileIO::Mode::Read);
#if defined(DJV_MMAP)
const char * bufP = reinterpret_cast<const char *>(fileIO->mmapP());
const char * bufEnd = reinterpret_cast<const char *>(fileIO->mmapEnd());
#else // DJV_MMAP
std::vector<char> buf;
const size_t fileSize = fileIO->getSize();
buf.resize(fileSize);
fileIO->read(buf.data(), fileSize);
const char* bufP = buf.data();
const char* bufEnd = bufP + fileSize;
#endif // DJV_MMAP
picojson::value v;
std::string error;
picojson::parse(v, bufP, bufEnd, &error);
if (!error.empty())
{
throw std::invalid_argument(error);
}
if (v.is<picojson::object>())
{
const auto & object = v.get<picojson::object>();
size_t readSettingsVersion = 0;
for (const auto& value : object)
{
if ("SettingsVersion" == value.first)
{
fromJSON(value.second, readSettingsVersion);
break;
}
}
if (settingsVersion == readSettingsVersion)
{
for (const auto& value : object)
{
out[value.first] = value.second;
}
}
}
}
}
catch (const std::exception & e)
{
std::stringstream ss;
ss << "Cannot read settings" << " '" << path << "'. " << e.what();
_log(ss.str(), LogLevel::Error);
}
}
void System::_writeSettingsFile(const FileSystem::Path & path, const picojson::value & value)
{
try
{
std::stringstream ss;
ss << "Writing settings: " << path;
_log(ss.str());
auto fileIO = FileSystem::FileIO::create();
fileIO->open(std::string(path), FileSystem::FileIO::Mode::Write);
PicoJSON::write(value, fileIO);
fileIO->write("\n");
}
catch (const std::exception & e)
{
std::stringstream ss;
ss << "Cannot write settings" << " '" << path << "'. " << e.what();
_log(ss.str(), LogLevel::Error);
}
}
} // namespace Settings
} // namespace UI
} // namespace djv