diff --git a/Application/FileConverter/Application.xaml.cs b/Application/FileConverter/Application.xaml.cs index 1ca8359..2a277af 100644 --- a/Application/FileConverter/Application.xaml.cs +++ b/Application/FileConverter/Application.xaml.cs @@ -72,7 +72,7 @@ namespace FileConverter public Settings Settings { get; - private set; + set; } public bool ShowSettings @@ -99,8 +99,7 @@ namespace FileConverter // Load settigns. Debug.Log("Load settings..."); - this.Settings = new Settings(); - this.Settings.Load(); + this.Settings = Settings.Load(); // Retrieve arguments. Debug.Log("Retrieve arguments..."); @@ -123,21 +122,22 @@ 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, 4); + //System.Array.Resize(ref args, 5); //args[1] = "--conversion-preset"; - //args[2] = "To Ogg"; + //args[2] = "To Png"; + //args[3] = "--verbose"; + //args[4] = @"D:\Test\ApplicationIcon.ico"; + + //System.Array.Resize(ref args, 6); + //args[1] = "--conversion-preset"; + //args[2] = "Extract CDA To Ogg"; //args[3] = "--verbose"; - 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"; + //args[4] = @"E:\Track01.cda"; + //args[5] = @"E:\Track02.cda"; } #endif @@ -304,25 +304,26 @@ namespace FileConverter Thread.Sleep(50); } -#if !DEBUG - bool allConversionsSucceed = true; - for (int index = 0; index < this.conversionJobs.Count; index++) + if (this.Settings.QuitApplicationWhenConversionsFinished) { - allConversionsSucceed &= this.conversionJobs[index].State == ConversionJob.ConversionState.Done; - } - - if (allConversionsSucceed) - { - System.Threading.Thread.Sleep(3000); - - if (this.cancelAutoExit) + bool allConversionsSucceed = true; + for (int index = 0; index < this.conversionJobs.Count; index++) { - return; + allConversionsSucceed &= this.conversionJobs[index].State == ConversionJob.ConversionState.Done; } - Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown())); + if (allConversionsSucceed) + { + System.Threading.Thread.Sleep((int)this.Settings.DurationBetweenEndOfConversionsAndApplicationQuit * 1000); + + if (this.cancelAutoExit) + { + return; + } + + Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown())); + } } -#endif } private void ExecuteConversionJob(object parameter) diff --git a/Application/FileConverter/ConversionPreset/ConversionPreset.cs b/Application/FileConverter/ConversionPreset/ConversionPreset.cs index b9e1e60..f364d9d 100644 --- a/Application/FileConverter/ConversionPreset/ConversionPreset.cs +++ b/Application/FileConverter/ConversionPreset/ConversionPreset.cs @@ -257,6 +257,24 @@ namespace FileConverter } } + public void Clean() + { + // Remove unrelevant settings. + List settingsToRemove = new List(); + foreach (string settingsKey in this.settings.Keys) + { + if (!this.IsRelevantSetting(settingsKey)) + { + settingsToRemove.Add(settingsKey); + } + } + + for (int index = 0; index < settingsToRemove.Count; index++) + { + this.settings.Remove(settingsToRemove[index]); + } + } + public void OnDeserializationComplete() { for (int index = 0; index < this.InputTypes.Count; index++) @@ -431,12 +449,12 @@ namespace FileConverter { if (outputType == OutputType.Wav) { - this.InitializeSettingsValue(ConversionPreset.ConversionSettingKeys.AudioEncodingMode, EncodingMode.Wav16.ToString()); + this.InitializeSettingsValue(ConversionPreset.ConversionSettingKeys.AudioEncodingMode, EncodingMode.Wav16.ToString(), true); } if (outputType == OutputType.Mp3) { - this.InitializeSettingsValue(ConversionPreset.ConversionSettingKeys.AudioEncodingMode, EncodingMode.Mp3VBR.ToString()); + this.InitializeSettingsValue(ConversionPreset.ConversionSettingKeys.AudioEncodingMode, EncodingMode.Mp3VBR.ToString(), true); this.InitializeSettingsValue(ConversionPreset.ConversionSettingKeys.AudioBitrate, "190"); } @@ -468,7 +486,7 @@ namespace FileConverter this.OnPropertyChanged("Settings"); } - private void InitializeSettingsValue(string settingsKey, string value) + private void InitializeSettingsValue(string settingsKey, string value, bool force = false) { if (string.IsNullOrEmpty(settingsKey)) { @@ -484,6 +502,10 @@ namespace FileConverter { this.settings.Add(settingsKey, value); } + else if (force) + { + this.settings[settingsKey] = value; + } } private string Validate(string propertyName) diff --git a/Application/FileConverter/FileConverter.csproj b/Application/FileConverter/FileConverter.csproj index 8138e0e..7176ab3 100644 --- a/Application/FileConverter/FileConverter.csproj +++ b/Application/FileConverter/FileConverter.csproj @@ -101,6 +101,7 @@ True True + diff --git a/Application/FileConverter/Settings.Settings.cs b/Application/FileConverter/Settings.Settings.cs new file mode 100644 index 0000000..9ab75e2 --- /dev/null +++ b/Application/FileConverter/Settings.Settings.cs @@ -0,0 +1,86 @@ +// License: http://www.gnu.org/licenses/gpl.html GPL version 3. + +using System.Linq; + +namespace FileConverter +{ + using System.Collections.ObjectModel; + using System.Xml.Serialization; + + public partial class Settings : IXmlSerializable + { + private bool quitApplicationWhenConversionsFinished = true; + private float durationBetweenEndOfConversionsAndApplicationQuit = 3f; + private ObservableCollection conversionPresets = new ObservableCollection(); + + [XmlIgnore] + public ObservableCollection ConversionPresets + { + get + { + return this.conversionPresets; + } + + set + { + this.conversionPresets = value; + this.OnPropertyChanged(); + } + } + + [XmlElement] + public bool QuitApplicationWhenConversionsFinished + { + get + { + return this.quitApplicationWhenConversionsFinished; + } + + set + { + this.quitApplicationWhenConversionsFinished = value; + this.OnPropertyChanged(); + } + } + + [XmlElement] + public float DurationBetweenEndOfConversionsAndApplicationQuit + { + get + { + return this.durationBetweenEndOfConversionsAndApplicationQuit; + } + + set + { + this.durationBetweenEndOfConversionsAndApplicationQuit = value; + this.OnPropertyChanged(); + } + } + + [XmlElement("ConversionPreset")] + public ConversionPreset[] SerializableConversionPresets + { + get + { + return this.ConversionPresets.ToArray(); + } + + set + { + for (int index = 0; index < value.Length; index++) + { + this.ConversionPresets.Add(value[index]); + } + } + } + + public void OnDeserializationComplete() + { + for (int index = 0; index < this.ConversionPresets.Count; index++) + { + this.ConversionPresets[index].OnDeserializationComplete(); + } + } + } +} diff --git a/Application/FileConverter/Settings.cs b/Application/FileConverter/Settings.cs index 7c7d17d..3e207d3 100644 --- a/Application/FileConverter/Settings.cs +++ b/Application/FileConverter/Settings.cs @@ -12,27 +12,22 @@ namespace FileConverter using System.Reflection; using System.Runtime.CompilerServices; using System.Security.Principal; + using System.Xml.Serialization; using System.Windows; using Microsoft.Win32; using FileConverter.Annotations; - using FileConverter.Diagnostics; - public class Settings : INotifyPropertyChanged, IDataErrorInfo + [XmlRoot] + [XmlType] + public partial class Settings : INotifyPropertyChanged, IDataErrorInfo { private const char PresetSeparator = ';'; - private ObservableCollection conversionPresets = new ObservableCollection(); - public event PropertyChangedEventHandler PropertyChanged; - public ObservableCollection ConversionPresets - { - get { return this.conversionPresets; } - set { this.conversionPresets = value; } - } - + [XmlIgnore] public string Error { get @@ -58,6 +53,7 @@ namespace FileConverter } } + [XmlIgnore] public string this[string columnName] { get @@ -75,18 +71,18 @@ namespace FileConverter return; } - ICollection conversionPresets = new ObservableCollection(); - XmlHelpers.LoadFromFile("Settings", temporaryFilePath, ref conversionPresets); + Settings settings = new Settings(); + XmlHelpers.LoadFromFile("Settings", temporaryFilePath, ref settings); RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(@"Software\FileConverter"); if (registryKey == null) { - MessageBox.Show("Can't apply settings in registry. (code 0x03)", "Error", MessageBoxButton.OK, MessageBoxImage.Error); + Diagnostics.Debug.LogError("Can't apply settings in registry. (code 0x03)"); return; } // Compute the registry entries data from settings. - Dictionary> registryEntries = ComputeRegistryEntriesFromConvertionPresets(conversionPresets); + Dictionary> registryEntries = ComputeRegistryEntriesFromConvertionPresets(settings.ConversionPresets); bool succeed = Settings.ApplyRegistryModifications(registryEntries); @@ -99,20 +95,13 @@ namespace FileConverter } } - public ConversionPreset GetPresetFromName(string presetName) + public static Settings Load() { - return this.conversionPresets.FirstOrDefault(match => match.Name == presetName); - } - - public void Load() - { - ICollection presets = this.ConversionPresets; - presets.Clear(); - + Settings settings = new Settings(); string userFilePath = Settings.GetUserSettingsFilePath(); if (File.Exists(userFilePath)) { - XmlHelpers.LoadFromFile("Settings", userFilePath, ref presets); + XmlHelpers.LoadFromFile("Settings", userFilePath, ref settings); } else { @@ -120,20 +109,29 @@ namespace FileConverter string defaultFilePath = Settings.GetDefaultSettingsFilePath(); if (File.Exists(defaultFilePath)) { - XmlHelpers.LoadFromFile("Settings", defaultFilePath, ref presets); + XmlHelpers.LoadFromFile("FileConverter", defaultFilePath, ref settings); } else { - Diagnostics.Debug.Log("Default settings not found. You should try to reinstall the application."); + Diagnostics.Debug.LogError("Default settings not found. You should try to reinstall the application."); } } + + return settings; } + public ConversionPreset GetPresetFromName(string presetName) + { + return this.conversionPresets.FirstOrDefault(match => match.Name == presetName); + } + public void Save() { + this.Clean(); + // Save the settings in a temporary files (we'll write the settings file when we'll succeed to write the registry keys). string temporaryFilePath = Settings.GetUserSettingsTemporaryFilePath(); - XmlHelpers.SaveToFile("Settings", temporaryFilePath, this.ConversionPresets); + XmlHelpers.SaveToFile("Settings", temporaryFilePath, this); // Compute the registry entries data from settings. Dictionary> registryEntries = Settings.ComputeRegistryEntriesFromConvertionPresets(this.ConversionPresets); @@ -171,6 +169,14 @@ namespace FileConverter } } + public void Clean() + { + for (int index = 0; index < this.ConversionPresets.Count; index++) + { + this.ConversionPresets[index].Clean(); + } + } + private static bool ApplyRegistryModifications(Dictionary> registryEntries) { if (!Settings.IsInAdmininstratorPrivileges) diff --git a/Application/FileConverter/Windows/SettingsWindow.xaml b/Application/FileConverter/Windows/SettingsWindow.xaml index 2c65465..a110c47 100644 --- a/Application/FileConverter/Windows/SettingsWindow.xaml +++ b/Application/FileConverter/Windows/SettingsWindow.xaml @@ -83,7 +83,7 @@ @@ -119,7 +119,7 @@