From 20126379229272311e0ff3a786eb453aa553f9eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Sun, 19 May 2019 17:34:06 +0200 Subject: [PATCH] Add Settings and Color Theme Versioning (#1557) --- src/Main.cpp | 124 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 95 insertions(+), 29 deletions(-) diff --git a/src/Main.cpp b/src/Main.cpp index 858d2bcb..40297d61 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -10,18 +10,109 @@ /** * @brief Migrate Settings used before Cutter 1.8 + * + * @return whether any settings have been migrated */ -static void migrateSettings(QSettings &newSettings) +static bool migrateSettingsPre18(QSettings &newSettings) { + if(newSettings.value("settings_migrated", false).toBool()) { + return false; + } QSettings oldSettings(QSettings::NativeFormat, QSettings::Scope::UserScope, "Cutter", "Cutter"); - for (const QString &key : oldSettings.allKeys()) { + QStringList allKeys = oldSettings.allKeys(); + if (allKeys.isEmpty()) { + return false; + } + qInfo() << "Migrating Settings from pre-1.8"; + for (const QString &key : allKeys) { newSettings.setValue(key, oldSettings.value(key)); } oldSettings.clear(); QFile settingsFile(oldSettings.fileName()); settingsFile.remove(); + newSettings.setValue("settings_migrated", true); + return true; } +#define CUTTER_SETTINGS_VERSION_CURRENT 1 +#define CUTTER_SETTINGS_VERSION_KEY "version" + +/* + * How Settings migrations work: + * + * Every time settings are changed in a way that needs migration, + * CUTTER_SETTINGS_VERSION_CURRENT is raised by 1 and a function migrateSettingsToX + * is implemented and added to initializeSettings(). + * This function takes care of migrating from EXACTLY version X-1 to X. + */ + +static void migrateSettingsTo1(QSettings &settings) { + settings.remove("settings_migrated"); // now handled by version + settings.remove("updated_custom_themes"); // now handled by theme_version +} + +static void initializeSettings() +{ + QSettings::setDefaultFormat(QSettings::IniFormat); + QSettings settings; + + int settingsVersion = settings.value(CUTTER_SETTINGS_VERSION_KEY, 0).toInt(); + if(settingsVersion == 0) { + migrateSettingsPre18(settings); + } + + if(settings.allKeys().length() > 0) { + if (settingsVersion > CUTTER_SETTINGS_VERSION_CURRENT) { + qWarning() << "Settings have a higher version than current! Skipping migration."; + } else if(settingsVersion >= 0) { + for (int v = settingsVersion + 1; v <= CUTTER_SETTINGS_VERSION_CURRENT; v++) { + qInfo() << "Migrating Settings to Version" << v; + switch (v) { + case 1: + migrateSettingsTo1(settings); + default: + break; + } + } + } + } + settings.setValue(CUTTER_SETTINGS_VERSION_KEY, CUTTER_SETTINGS_VERSION_CURRENT); +} + + +#define THEME_VERSION_CURRENT 1 +#define THEME_VERSION_KEY "theme_version" + +static void removeObsoleteOptionsFromCustomThemes() { + const QStringList options = Core()->cmdj("ecj").object().keys() + << ColorThemeWorker::cutterSpecificOptions; + for (auto theme : Core()->cmdList("eco*")) { + theme = theme.trimmed(); + if (!ThemeWorker().isCustomTheme(theme)) { + continue; + } + QJsonObject updatedTheme; + auto sch = ThemeWorker().getTheme(theme).object(); + for (const auto& key : sch.keys()) { + if (options.contains(key)) { + updatedTheme.insert(key, sch[key]); + } + } + ThemeWorker().save(QJsonDocument(updatedTheme), theme); + } +} + +static void migrateThemes() +{ + QSettings settings; + int themeVersion = settings.value(THEME_VERSION_KEY, 0).toInt(); + if (themeVersion != THEME_VERSION_CURRENT) { + removeObsoleteOptionsFromCustomThemes(); + settings.setValue(THEME_VERSION_KEY, THEME_VERSION_CURRENT); + } +} + + int main(int argc, char *argv[]) { if (argc >= 3 && QString::fromLocal8Bit(argv[1]) == "--start-crash-handler") { @@ -38,39 +129,14 @@ int main(int argc, char *argv[]) QCoreApplication::setOrganizationName("RadareOrg"); QCoreApplication::setApplicationName("Cutter"); - QSettings::setDefaultFormat(QSettings::IniFormat); - QSettings settings; - if (!settings.value("settings_migrated", false).toBool()) { - qInfo() << "Settings have not been migrated before, trying to migrate from pre-1.8 if possible."; - migrateSettings(settings); - settings.setValue("settings_migrated", true); - } + initializeSettings(); QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts); // needed for QtWebEngine inside Plugins CutterApplication a(argc, argv); - // Removes obsolete color options (highlight and highlightWord) from custom theme files - if (!settings.value("updated_custom_themes", false).toBool()) { - const QStringList options = Core()->cmdj("ecj").object().keys() - << ColorThemeWorker::cutterSpecificOptions; - for (auto theme : Core()->cmdList("eco*")) { - theme = theme.trimmed(); - if (!ThemeWorker().isCustomTheme(theme)) { - continue; - } - QJsonObject updatedTheme; - auto sch = ThemeWorker().getTheme(theme).object(); - for (auto key : sch.keys()) { - if (options.contains(key)) { - updatedTheme.insert(key, sch[key]); - } - } - ThemeWorker().save(QJsonDocument(updatedTheme), theme); - } - settings.setValue("updated_custom_themes", true); - } + migrateThemes(); if (Config()->getAutoUpdateEnabled()) { UpdateWorker *updateWorker = new UpdateWorker;