From 46ce553d75c6e0a79e79be085dccc55068559e08 Mon Sep 17 00:00:00 2001 From: Craig Watson Date: Fri, 4 Mar 2016 14:31:25 +0100 Subject: [PATCH] Check settings at launch to avoid crashing Some settings were changed between v1.02 and 1.10 (current), and some of these changes cause OpenBoard to crash at launch. This commit adds a function to check for these specific new settings, and wipe the old values if they are found. This avoids problems when the user upgrades from 1.02 without deleting their configuration file. (This is an alternative to having a post-install script, which would be ineffective in a multi-user configuration) --- src/core/UBSetting.h | 5 +++ src/core/UBSettings.cpp | 86 ++++++++++++++++++++++++++++++++++++++--- src/core/UBSettings.h | 3 ++ 3 files changed, 88 insertions(+), 6 deletions(-) diff --git a/src/core/UBSetting.h b/src/core/UBSetting.h index 1294a82c..01ac9f35 100644 --- a/src/core/UBSetting.h +++ b/src/core/UBSetting.h @@ -62,6 +62,11 @@ class UBSetting : public QObject return mPath; } + virtual QVariant defaultValue() const + { + return mDefaultValue; + } + public slots: void setBool(bool pValue); diff --git a/src/core/UBSettings.cpp b/src/core/UBSettings.cpp index f567a0f5..23e2fe27 100644 --- a/src/core/UBSettings.cpp +++ b/src/core/UBSettings.cpp @@ -226,13 +226,13 @@ void UBSettings::init() appPreferredLanguage = new UBSetting(this,"App","PreferredLanguage", ""); rightLibPaletteBoardModeWidth = new UBSetting(this, "Board", "RightLibPaletteBoardModeWidth", 270); - rightLibPaletteBoardModeIsCollapsed = new UBSetting(this,"Board", "RightLibPaletteBoardModeIsCollapsed",false); + rightLibPaletteBoardModeIsCollapsed = new UBSetting(this,"Board", "RightLibPaletteBoardModeIsCollapsed",true); rightLibPaletteDesktopModeWidth = new UBSetting(this, "Board", "RightLibPaletteDesktopModeWidth", 270); - rightLibPaletteDesktopModeIsCollapsed = new UBSetting(this,"Board", "RightLibPaletteDesktopModeIsCollapsed",false); + rightLibPaletteDesktopModeIsCollapsed = new UBSetting(this,"Board", "RightLibPaletteDesktopModeIsCollapsed",true); leftLibPaletteBoardModeWidth = new UBSetting(this, "Board", "LeftLibPaletteBoardModeWidth",270); - leftLibPaletteBoardModeIsCollapsed = new UBSetting(this,"Board","LeftLibPaletteBoardModeIsCollapsed",false); + leftLibPaletteBoardModeIsCollapsed = new UBSetting(this,"Board","LeftLibPaletteBoardModeIsCollapsed",true); leftLibPaletteDesktopModeWidth = new UBSetting(this, "Board", "LeftLibPaletteDesktopModeWidth",270); - leftLibPaletteDesktopModeIsCollapsed = new UBSetting(this,"Board","LeftLibPaletteDesktopModeIsCollapsed",false); + leftLibPaletteDesktopModeIsCollapsed = new UBSetting(this,"Board","LeftLibPaletteDesktopModeIsCollapsed",true); appIsInSoftwareUpdateProcess = new UBSetting(this, "App", "IsInSoftwareUpdateProcess", false); appLastSessionDocumentUUID = new UBSetting(this, "App", "LastSessionDocumentUUID", ""); @@ -268,11 +268,11 @@ void UBSettings::init() pageDpi = new UBSetting(this, "Board", "pageDpi", 0); QStringList penLightBackgroundColors; - penLightBackgroundColors << "#000000" << "#FF0000" <<"#004080" << "#008000" << "#FFDD00" << "#C87400" << "#800040" << "#008080" << "#5F2D0A"; + penLightBackgroundColors << "#000000" << "#FF0000" <<"#004080" << "#008000" << "#FFDD00" << "#C87400" << "#800040" << "#008080" << "#5F2D0A" << "#FFFFFF"; boardPenLightBackgroundColors = new UBColorListSetting(this, "Board", "PenLightBackgroundColors", penLightBackgroundColors, 1.0); QStringList penDarkBackgroundColors; - penDarkBackgroundColors << "#FFFFFF" << "#FF3400" <<"#66C0FF" << "#81FF5C" << "#FFFF00" << "#B68360" << "#FF497E" << "#8D69FF"; + penDarkBackgroundColors << "#FFFFFF" << "#FF3400" <<"#66C0FF" << "#81FF5C" << "#FFFF00" << "#B68360" << "#FF497E" << "#8D69FF" << "#000000"; boardPenDarkBackgroundColors = new UBColorListSetting(this, "Board", "PenDarkBackgroundColors", penDarkBackgroundColors, 1.0); boardMarkerAlpha = new UBSetting(this, "Board", "MarkerAlpha", 0.5); @@ -421,6 +421,7 @@ void UBSettings::init() useSystemOnScreenKeyboard = new UBSetting(this, "App", "UseSystemOnScreenKeyboard", true); cleanNonPersistentSettings(); + checkNewSettings(); } @@ -1339,3 +1340,76 @@ void UBSettings::cleanNonPersistentSettings() youTubeUserEMail->set(QVariant("")); } } + +/** + * @brief Permanently remove a setting, from local memory and config files + * @param setting The setting to remove + */ +void UBSettings::removeSetting(const QString &setting) +{ + if (sAppSettings->contains(setting)) + sAppSettings->remove(setting); + + if (mUserSettings->contains(setting)) + mUserSettings->remove(setting); + + if (mSettingsQueue.contains(setting)) + mSettingsQueue.remove(setting); +} + +void UBSettings::checkNewSettings() +{ + /* + * Some settings were modified in new versions and OpenBoard can crash + * if an old settings file is used. This function checks these settings and + * if necessary, resets them to the values initialized in UBSettings::init(). + * + * Thus this method can be removed when it is no longer deemed useful. + */ + + // OB 1.10 introduced an extra pen color; for simplicity, if the old settings + // are still present (i.e only 4 selected pen colors), we just reset all color settings. + // Having too few colors actually causes OpenBoard to crash, hence these measures. + + QList colorSettings; + colorSettings << boardPenDarkBackgroundSelectedColors + << boardPenLightBackgroundSelectedColors + << boardMarkerDarkBackgroundSelectedColors + << boardMarkerLightBackgroundSelectedColors; + + foreach (UBColorListSetting* setting, colorSettings) { + if (setting->colors().size() < 5) + setting->reset(); // Make sure that there are 5 selected colors + } + + colorSettings.clear(); + + // Next we check whether all the new colors were added; i.e if some default + // colors are not also in the config file, we add them to it. + + // This is not nearly as critical as the above issue, but the users (or admins) seemingly + // can't be trusted to delete or modify their own config file when upgrading, so they might + // wonder why they don't have the new colors in their app. + + colorSettings << boardPenDarkBackgroundColors + << boardPenLightBackgroundColors + << boardMarkerDarkBackgroundColors + << boardMarkerLightBackgroundColors; + + foreach (UBColorListSetting* setting, colorSettings) { + QStringList defaultColors = qvariant_cast(setting->defaultValue()); + QStringList currentColors(qvariant_cast(setting->get())); // copy + + foreach (QString c, defaultColors) { + if (!currentColors.contains(c)) + currentColors.append(QString(c)); + } + + setting->set(currentColors); + } + + + // A typo was corrected in version 1.10 + removeSetting("Board/useSystemOnScreenKeybard"); + +} diff --git a/src/core/UBSettings.h b/src/core/UBSettings.h index 6f3a8c1f..a8e6745d 100644 --- a/src/core/UBSettings.h +++ b/src/core/UBSettings.h @@ -425,6 +425,9 @@ class UBSettings : public QObject static bool checkDirectory(QString& dirPath); static QString replaceWildcard(QString& path); + void removeSetting(const QString& setting); + void checkNewSettings(); + };