diff --git a/Application/FileConverter/Application.xaml.cs b/Application/FileConverter/Application.xaml.cs index 7e26bf5..eecada6 100644 --- a/Application/FileConverter/Application.xaml.cs +++ b/Application/FileConverter/Application.xaml.cs @@ -134,19 +134,11 @@ namespace FileConverter //System.Array.Resize(ref args, 2); //args[1] = "--settings"; - //System.Array.Resize(ref args, 5); - //args[1] = "--conversion-preset"; - //args[2] = "To Flac"; - //args[3] = "--verbose"; - //args[4] = @"D:\Test\02 - Corn on the Cob.mp3"; - - //System.Array.Resize(ref args, 6); - //args[1] = "--conversion-preset"; - //args[2] = "Extract CDA To Ogg"; - //args[3] = "--verbose"; - - //args[4] = @"E:\Track01.cda"; - //args[5] = @"E:\Track02.cda"; + System.Array.Resize(ref args, 5); + args[1] = "--conversion-preset"; + args[2] = "To Aac"; + args[3] = "--verbose"; + args[4] = @"D:\Test\02 - Corn on the Cob.flac"; } #endif diff --git a/Application/FileConverter/Settings.cs b/Application/FileConverter/Settings.cs index 5f56b36..833a59d 100644 --- a/Application/FileConverter/Settings.cs +++ b/Application/FileConverter/Settings.cs @@ -4,7 +4,6 @@ namespace FileConverter { using System; using System.Collections.Generic; - using System.Collections.ObjectModel; using System.ComponentModel; using System.Diagnostics; using System.IO; @@ -71,8 +70,8 @@ namespace FileConverter return; } - Settings settings = new Settings(); - XmlHelpers.LoadFromFile("Settings", temporaryFilePath, ref settings); + Settings settings = null; + XmlHelpers.LoadFromFile("Settings", temporaryFilePath, out settings); RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(@"Software\FileConverter"); if (registryKey == null) @@ -97,13 +96,33 @@ namespace FileConverter public static Settings Load() { - Settings settings = new Settings(); + Settings defaultSettings = null; + + // Load the default settings. + string defaultFilePath = Settings.GetDefaultSettingsFilePath(); + if (File.Exists(defaultFilePath)) + { + try + { + XmlHelpers.LoadFromFile("Settings", defaultFilePath, out 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); + } + + Settings userSettings = null; string userFilePath = Settings.GetUserSettingsFilePath(); if (File.Exists(userFilePath)) { try { - XmlHelpers.LoadFromFile("Settings", userFilePath, ref settings); + XmlHelpers.LoadFromFile("Settings", userFilePath, out userSettings); } catch (Exception exception) { @@ -119,33 +138,13 @@ namespace FileConverter } } - if (settings.SerializationVersion != Version) + if (userSettings != null && userSettings.SerializationVersion != Version) { - Diagnostics.Debug.Log("File converter settings has been imported from version {0} to version {1}.", settings.SerializationVersion, Version); - } - } - else - { - // If user settings doesn't exist, load the default settings. - string defaultFilePath = Settings.GetDefaultSettingsFilePath(); - if (File.Exists(defaultFilePath)) - { - try - { - XmlHelpers.LoadFromFile("Settings", defaultFilePath, ref settings); - } - 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); + Diagnostics.Debug.Log("File converter settings has been imported from version {0} to version {1}.", userSettings.SerializationVersion, Version); } } - return settings; + return userSettings != null ? userSettings.Merge(defaultSettings) : defaultSettings; } public ConversionPreset GetPresetFromName(string presetName) @@ -404,5 +403,26 @@ namespace FileConverter { this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } + + private Settings Merge(Settings settings) + { + if (settings == null) + { + return this; + } + + for (int index = 0; index < settings.conversionPresets.Count; index++) + { + ConversionPreset conversionPreset = settings.conversionPresets[index]; + if (this.conversionPresets.Any(match => match.Name == conversionPreset.Name)) + { + continue; + } + + this.conversionPresets.Add(conversionPreset); + } + + return this; + } } } diff --git a/Application/FileConverter/XmlHelpers.cs b/Application/FileConverter/XmlHelpers.cs index 09e7c49..b33b337 100644 --- a/Application/FileConverter/XmlHelpers.cs +++ b/Application/FileConverter/XmlHelpers.cs @@ -10,18 +10,13 @@ namespace FileConverter public class XmlHelpers { - public static void LoadFromFile(string root, string path, ref T deserializedObject) + public static void LoadFromFile(string root, string path, out T deserializedObject) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException("path"); } - if (deserializedObject == null) - { - throw new ArgumentNullException("deserializedObject"); - } - XmlRootAttribute xmlRoot = new XmlRootAttribute { ElementName = root