Skip to content

Commit 2af0ba0

Browse files
committed
FIX(client): settings_version on global level
1 parent 922be06 commit 2af0ba0

File tree

3 files changed

+35
-22
lines changed

3 files changed

+35
-22
lines changed

src/mumble/JSONSerialization.cpp

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "JSONSerialization.h"
77
#include "Cert.h"
88
#include "SettingsMacros.h"
9+
#include "Global.h"
910

1011

1112
template< typename T, bool isEnum > struct SaveValueConverter {
@@ -91,11 +92,7 @@ void load(const nlohmann::json &json, const char *category, const SettingsKey &k
9192
load(categoryJSON, key, variable, defaultValue, useDefault);
9293
}
9394

94-
95-
9695
void to_json(nlohmann::json &j, const Settings &settings) {
97-
j[SettingsKeys::SETTINGS_VERSION_KEY] = 1;
98-
9996
const Settings defaultValues;
10097

10198
#define PROCESS(category, key, variable) \
@@ -172,12 +169,10 @@ void migrateSettings(nlohmann::json &json, int settingsVersion) {
172169
}
173170

174171
void from_json(const nlohmann::json &j, Settings &settings) {
175-
int settingsVersion = j.at(SettingsKeys::SETTINGS_VERSION_KEY).get< int >();
176-
177172
// Copy since we might have to make modifications
178173
nlohmann::json json = j;
179174

180-
migrateSettings(json, settingsVersion);
175+
migrateSettings(json, Global::get().profiles.settings_version);
181176

182177
#define PROCESS(category, key, variable) \
183178
load(json, #category, SettingsKeys::key, settings.variable, settings.variable, true);
@@ -242,16 +237,40 @@ void from_json(const nlohmann::json &j, OverlaySettings &settings) {
242237
}
243238

244239

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);
247244

248245
PROCESS_ALL_PROFILE_SETTINGS
249246

250247
#undef PROCESS
251248
}
252249

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);
255274

256275
PROCESS_ALL_PROFILE_SETTINGS
257276

src/mumble/Settings.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -265,17 +265,8 @@ void Settings::load(const QString &path) {
265265
nlohmann::json settingsJSON;
266266
try {
267267
stream >> settingsJSON;
268-
269-
if (settingsJSON.contains(SettingsKeys::ACTIVE_PROFILE)) {
270-
settingsJSON.get_to(Global::get().profiles);
271-
loadProfile();
272-
} else {
273-
// The file does not contain the key "SettingsKeys::ACTIVE_PROFILE"
274-
// We assume the JSON file does not contain any profiles, because it is
275-
// old. We load the file raw instead and convert it to the s_default_profile_name profile.
276-
qWarning("Migrating settings file to 'default' profile");
277-
settingsJSON.get_to(*this);
278-
}
268+
settingsJSON.get_to(Global::get().profiles);
269+
loadProfile();
279270

280271
if (!mumbleQuitNormally) {
281272
// These settings were saved without Mumble quitting normally afterwards. In order to prevent loading
@@ -447,6 +438,7 @@ std::size_t qHash(const ChannelTarget &target) {
447438
}
448439

449440
const QString Profiles::s_default_profile_name = QLatin1String("default");
441+
const int Profiles::s_current_settings_version = 2;
450442

451443
const QString Settings::cqsDefaultPushClickOn = QLatin1String(":/on.ogg");
452444
const QString Settings::cqsDefaultPushClickOff = QLatin1String(":/off.ogg");

src/mumble/Settings.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,9 @@ struct OverlaySettings {
190190

191191
struct Profiles {
192192
static const QString s_default_profile_name;
193+
static const int s_current_settings_version;
193194

195+
int settings_version = s_current_settings_version;
194196
QString activeProfileName = s_default_profile_name;
195197
QMap< QString, Settings > allProfiles = {};
196198
};

0 commit comments

Comments
 (0)