|
6 | 6 | #include "JSONSerialization.h" |
7 | 7 | #include "Cert.h" |
8 | 8 | #include "SettingsMacros.h" |
| 9 | +#include "Global.h" |
9 | 10 |
|
10 | 11 |
|
11 | 12 | template< typename T, bool isEnum > struct SaveValueConverter { |
@@ -91,11 +92,7 @@ void load(const nlohmann::json &json, const char *category, const SettingsKey &k |
91 | 92 | load(categoryJSON, key, variable, defaultValue, useDefault); |
92 | 93 | } |
93 | 94 |
|
94 | | - |
95 | | - |
96 | 95 | void to_json(nlohmann::json &j, const Settings &settings) { |
97 | | - j[SettingsKeys::SETTINGS_VERSION_KEY] = 1; |
98 | | - |
99 | 96 | const Settings defaultValues; |
100 | 97 |
|
101 | 98 | #define PROCESS(category, key, variable) \ |
@@ -172,12 +169,10 @@ void migrateSettings(nlohmann::json &json, int settingsVersion) { |
172 | 169 | } |
173 | 170 |
|
174 | 171 | void from_json(const nlohmann::json &j, Settings &settings) { |
175 | | - int settingsVersion = j.at(SettingsKeys::SETTINGS_VERSION_KEY).get< int >(); |
176 | | - |
177 | 172 | // Copy since we might have to make modifications |
178 | 173 | nlohmann::json json = j; |
179 | 174 |
|
180 | | - migrateSettings(json, settingsVersion); |
| 175 | + migrateSettings(json, Global::get().profiles.settings_version); |
181 | 176 |
|
182 | 177 | #define PROCESS(category, key, variable) \ |
183 | 178 | load(json, #category, SettingsKeys::key, settings.variable, settings.variable, true); |
@@ -242,16 +237,40 @@ void from_json(const nlohmann::json &j, OverlaySettings &settings) { |
242 | 237 | } |
243 | 238 |
|
244 | 239 |
|
245 | | -void to_json(nlohmann::json &j, const Profiles &settings) { |
246 | | -#define PROCESS(category, key, variable) save(j, SettingsKeys::key, settings.variable); |
| 240 | +void to_json(nlohmann::json &j, const Profiles &profiles) { |
| 241 | + j[SettingsKeys::SETTINGS_VERSION_KEY] = Profiles::s_current_settings_version; |
| 242 | + |
| 243 | +#define PROCESS(category, key, variable) save(j, SettingsKeys::key, profiles.variable); |
247 | 244 |
|
248 | 245 | PROCESS_ALL_PROFILE_SETTINGS |
249 | 246 |
|
250 | 247 | #undef PROCESS |
251 | 248 | } |
252 | 249 |
|
253 | | -void from_json(const nlohmann::json &j, Profiles &settings) { |
254 | | -#define PROCESS(category, key, variable) load(j, SettingsKeys::key, settings.variable, settings.variable, true); |
| 250 | +void migrateProfiles(nlohmann::json &json, int settingsVersion) { |
| 251 | + if (settingsVersion < 2) { |
| 252 | + // The file does not contain profiles yet, because it is old. |
| 253 | + // We convert the existing json to the s_default_profile_name profile. |
| 254 | + qWarning("Migrating settings file to default profile"); |
| 255 | + |
| 256 | + nlohmann::json defaultProfile = json; |
| 257 | + nlohmann::json profiles = nlohmann::json::object({ { Profiles::s_default_profile_name, defaultProfile } }); |
| 258 | + json[SettingsKeys::PROFILES] = profiles; |
| 259 | + } |
| 260 | +} |
| 261 | + |
| 262 | +void from_json(const nlohmann::json &j, Profiles &profiles) { |
| 263 | + profiles.settings_version = 0; |
| 264 | + if (j.contains(SettingsKeys::SETTINGS_VERSION_KEY)) { |
| 265 | + profiles.settings_version = j.at(SettingsKeys::SETTINGS_VERSION_KEY).get< int >(); |
| 266 | + } |
| 267 | + |
| 268 | + // Copy since we might have to make modifications |
| 269 | + nlohmann::json json = j; |
| 270 | + |
| 271 | + migrateProfiles(json, Global::get().profiles.settings_version); |
| 272 | + |
| 273 | +#define PROCESS(category, key, variable) load(json, SettingsKeys::key, profiles.variable, profiles.variable, true); |
255 | 274 |
|
256 | 275 | PROCESS_ALL_PROFILE_SETTINGS |
257 | 276 |
|
|
0 commit comments