From 6166bd60d54e18b9ef299a4367e082e195846baf Mon Sep 17 00:00:00 2001 From: Tichau Date: Mon, 14 Dec 2015 22:31:57 +0100 Subject: [PATCH] Handle incorrect user settings case. Ask to the user if he want to delete its invalid user settings. --- Application/FileConverter/Application.xaml.cs | 15 +++++++-- .../FileConverter/Settings.Settings.cs | 4 ++- Application/FileConverter/Settings.cs | 32 +++++++++++++++++-- Application/FileConverter/XmlHelpers.cs | 2 +- 4 files changed, 47 insertions(+), 6 deletions(-) diff --git a/Application/FileConverter/Application.xaml.cs b/Application/FileConverter/Application.xaml.cs index cc276d7..7e26bf5 100644 --- a/Application/FileConverter/Application.xaml.cs +++ b/Application/FileConverter/Application.xaml.cs @@ -43,6 +43,11 @@ namespace FileConverter public Application() { this.ConvertionJobs = this.conversionJobs.AsReadOnly(); + } + + protected override void OnStartup(StartupEventArgs e) + { + base.OnStartup(e); this.Initialize(); @@ -98,6 +103,12 @@ namespace FileConverter // Load settigns. Debug.Log("Load settings..."); this.Settings = Settings.Load(); + if (this.Settings == null) + { + Diagnostics.Debug.LogError("The application will now shutdown. If you want to fix the problem yourself please edit or delete the file: C:\\Users\\UserName\\AppData\\Local\\FileConverter\\Settings.user.xml."); + Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown())); + return; + } // Retrieve arguments. Debug.Log("Retrieve arguments..."); @@ -120,8 +131,8 @@ namespace FileConverter args[6] = @"D:\Test\Track03.mp3"; args[7] = @"D:\Test\Track04.mp3"; - System.Array.Resize(ref args, 2); - args[1] = "--settings"; + //System.Array.Resize(ref args, 2); + //args[1] = "--settings"; //System.Array.Resize(ref args, 5); //args[1] = "--conversion-preset"; diff --git a/Application/FileConverter/Settings.Settings.cs b/Application/FileConverter/Settings.Settings.cs index 0b54fcc..376841d 100644 --- a/Application/FileConverter/Settings.Settings.cs +++ b/Application/FileConverter/Settings.Settings.cs @@ -8,6 +8,8 @@ namespace FileConverter public partial class Settings : IXmlSerializable { + public const int Version = 1; + private double settingsWindowHeight = 640; private double settingsWindowWidth = 800; private bool exitApplicationWhenConversionsFinished = true; @@ -19,7 +21,7 @@ namespace FileConverter { get; set; - } = 0; + } = Version; [XmlIgnore] public ObservableCollection ConversionPresets diff --git a/Application/FileConverter/Settings.cs b/Application/FileConverter/Settings.cs index 1b914b4..5f56b36 100644 --- a/Application/FileConverter/Settings.cs +++ b/Application/FileConverter/Settings.cs @@ -101,7 +101,28 @@ namespace FileConverter string userFilePath = Settings.GetUserSettingsFilePath(); if (File.Exists(userFilePath)) { - XmlHelpers.LoadFromFile("Settings", userFilePath, ref settings); + try + { + XmlHelpers.LoadFromFile("Settings", userFilePath, ref settings); + } + catch (Exception exception) + { + MessageBoxResult messageBoxResult = MessageBox.Show("Can't load file converter user settings. Do you want to fall back to default settings ?", "Error", MessageBoxButton.YesNo, MessageBoxImage.Exclamation); + if (messageBoxResult == MessageBoxResult.Yes) + { + System.IO.File.Delete(userFilePath); + return Settings.Load(); + } + else if (messageBoxResult == MessageBoxResult.No) + { + return null; + } + } + + if (settings.SerializationVersion != Version) + { + Diagnostics.Debug.Log("File converter settings has been imported from version {0} to version {1}.", settings.SerializationVersion, Version); + } } else { @@ -109,7 +130,14 @@ namespace FileConverter string defaultFilePath = Settings.GetDefaultSettingsFilePath(); if (File.Exists(defaultFilePath)) { - XmlHelpers.LoadFromFile("Settings", defaultFilePath, ref settings); + try + { + XmlHelpers.LoadFromFile("Settings", defaultFilePath, ref settings); + } + catch (Exception exception) + { + Diagnostics.Debug.LogError("Fail to load file converter default settings. {0}", exception.Message); + } } else { diff --git a/Application/FileConverter/XmlHelpers.cs b/Application/FileConverter/XmlHelpers.cs index 921b3b4..09e7c49 100644 --- a/Application/FileConverter/XmlHelpers.cs +++ b/Application/FileConverter/XmlHelpers.cs @@ -47,7 +47,7 @@ namespace FileConverter } catch (System.Exception exception) { - Diagnostics.Debug.LogError("Fail to load asset of type '" + typeof(T) + "'. The following exception was raised:\n " + exception.Message); + throw; } IXmlSerializable xmlSerializableObject = deserializedObject as IXmlSerializable;