From e8a01c91f23438a9d0cde5f35c40a33bd368ff1a Mon Sep 17 00:00:00 2001 From: Tichau Date: Wed, 13 Jan 2016 20:17:55 +0100 Subject: [PATCH] Post installation settings initialization Fixed: Issue where it is impossible to delete a conversion preset define in default settings. Fixed: Issue where the windows context menu does not contains user preset after an application upgrade. --- Application/FileConverter/Application.xaml.cs | 53 +++++---- Application/FileConverter/Settings.cs | 64 ++++++++++- Installer/Product.wxs | 103 +++--------------- 3 files changed, 107 insertions(+), 113 deletions(-) diff --git a/Application/FileConverter/Application.xaml.cs b/Application/FileConverter/Application.xaml.cs index ad57477..372d8d1 100644 --- a/Application/FileConverter/Application.xaml.cs +++ b/Application/FileConverter/Application.xaml.cs @@ -156,16 +156,6 @@ namespace FileConverter { Diagnostics.Debug.Log("The number of processors on this computer is {0}. Set the default number of conversion threads to {0}", Environment.ProcessorCount); this.numberOfConversionThread = Environment.ProcessorCount; - - // 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..."); @@ -198,6 +188,7 @@ namespace FileConverter } #endif + // Log arguments. for (int index = 0; index < args.Length; index++) { string argument = args[index]; @@ -215,10 +206,9 @@ namespace FileConverter return; } - ConversionPreset conversionPreset = null; - List filePaths = new List(); - // Parse arguments. + List filePaths = new List(); + string conversionPresetName = null; for (int index = 1; index < args.Length; index++) { string argument = args[index]; @@ -229,6 +219,11 @@ namespace FileConverter switch (parameterTitle) { + case "post-install-init": + Settings.PostInstallationInitialization(); + Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown())); + return; + case "version": Console.Write(ApplicationVersion.ToString()); Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown())); @@ -252,14 +247,7 @@ namespace FileConverter return; } - conversionPreset = this.Settings.GetPresetFromName(args[index + 1]); - if (conversionPreset == null) - { - Debug.LogError("Invalid conversion preset '{0}'. (code 0x02)", args[index + 1]); - Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown())); - return; - } - + conversionPresetName = args[index + 1]; index++; continue; @@ -281,6 +269,17 @@ 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; + } + + // Check upgrade. if (this.Settings.CheckUpgradeAtStartup) { long fileTime = Registry.GetValue(Registry.Keys.LastUpdateCheckDate); @@ -293,6 +292,18 @@ namespace FileConverter } } + ConversionPreset conversionPreset = null; + if (!string.IsNullOrEmpty(conversionPresetName)) + { + conversionPreset = this.Settings.GetPresetFromName(conversionPresetName); + if (conversionPreset == null) + { + Debug.LogError("Invalid conversion preset '{0}'. (code 0x02)", conversionPresetName); + Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown())); + return; + } + } + if (conversionPreset != null) { // Create convertion jobs. diff --git a/Application/FileConverter/Settings.cs b/Application/FileConverter/Settings.cs index cd4ac11..a91f0c6 100644 --- a/Application/FileConverter/Settings.cs +++ b/Application/FileConverter/Settings.cs @@ -93,7 +93,7 @@ namespace FileConverter } } - public static Settings Load() + public static void PostInstallationInitialization() { Settings defaultSettings = null; @@ -115,6 +115,7 @@ namespace FileConverter Diagnostics.Debug.LogError("Default settings not found at path {0}. You should try to reinstall the application.", defaultFilePath); } + // Load user settings if exists. Settings userSettings = null; string userFilePath = Settings.GetUserSettingsFilePath(); if (File.Exists(userFilePath)) @@ -125,7 +126,37 @@ namespace FileConverter } catch (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); + System.IO.File.Delete(userFilePath); + } + + if (userSettings != null && userSettings.SerializationVersion != Version) + { + Diagnostics.Debug.Log("File converter settings has been imported from version {0} to version {1}.", userSettings.SerializationVersion, Version); + } + } + + Settings settings = userSettings != null ? userSettings.Merge(defaultSettings) : defaultSettings; + settings.Save(); + } + + public static Settings Load() + { + Settings settings = null; + string userFilePath = Settings.GetUserSettingsFilePath(); + if (File.Exists(userFilePath)) + { + Settings userSettings = null; + try + { + XmlHelpers.LoadFromFile("Settings", userFilePath, out userSettings); + settings = userSettings; + } + catch (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); @@ -139,11 +170,34 @@ namespace FileConverter if (userSettings != null && userSettings.SerializationVersion != Version) { - Diagnostics.Debug.Log("File converter settings has been imported from version {0} to version {1}.", userSettings.SerializationVersion, Version); + Diagnostics.Debug.Log("File converter settings has been imported from version {0} to version {1}.", + userSettings.SerializationVersion, Version); + } + } + else + { + // Load the default settings. + string defaultFilePath = Settings.GetDefaultSettingsFilePath(); + if (File.Exists(defaultFilePath)) + { + Settings defaultSettings = null; + try + { + XmlHelpers.LoadFromFile("Settings", defaultFilePath, out defaultSettings); + settings = defaultSettings; + } + catch (Exception exception) + { + Diagnostics.Debug.LogError("Fail to load file converter default settings. {0}", exception.Message); + } + } + else + { + Diagnostics.Debug.LogError("Default settings not found at path {0}. You should try to reinstall the application.", defaultFilePath); } } - return userSettings != null ? userSettings.Merge(defaultSettings) : defaultSettings; + return settings; } public ConversionPreset GetPresetFromName(string presetName) @@ -170,7 +224,7 @@ namespace FileConverter // Write the settings in registry. if (Settings.IsInAdmininstratorPrivileges) { - // We are in admin mode, write reigstry ... + // We are in admin mode, write registry ... bool succeed = Settings.ApplyRegistryModifications(registryEntries); if (succeed) { diff --git a/Installer/Product.wxs b/Installer/Product.wxs index 3816728..a8c7e81 100644 --- a/Installer/Product.wxs +++ b/Installer/Product.wxs @@ -29,6 +29,11 @@ + + + + NOT Installed + @@ -53,17 +58,23 @@ + + + + + + - - + + - - + + @@ -77,95 +88,13 @@ - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -