From 03e975b371e52eca11c95f38420f5d5e9cb11d2a Mon Sep 17 00:00:00 2001 From: Tichau Date: Thu, 30 Apr 2015 00:27:46 +0200 Subject: [PATCH] Serialize and deserialize settings. --- Application/FileConverter/ConversionPreset.cs | 10 ++- .../FileConverter/FileConverter.csproj | 1 + .../FileConverter.sln.DotSettings | 3 +- Application/FileConverter/Settings.cs | 33 +++++++-- Application/FileConverter/SettingsWindow.xaml | 5 ++ .../FileConverter/SettingsWindow.xaml.cs | 20 +++++- Application/FileConverter/XmlHelpers.cs | 67 +++++++++++++++++++ 7 files changed, 130 insertions(+), 9 deletions(-) create mode 100644 Application/FileConverter/XmlHelpers.cs diff --git a/Application/FileConverter/ConversionPreset.cs b/Application/FileConverter/ConversionPreset.cs index 1322df0..ed623c9 100644 --- a/Application/FileConverter/ConversionPreset.cs +++ b/Application/FileConverter/ConversionPreset.cs @@ -6,6 +6,10 @@ using System.Threading.Tasks; namespace FileConverter { + using System.Xml.Serialization; + + [XmlRoot] + [XmlType] public class ConversionPreset { public ConversionPreset() @@ -39,14 +43,14 @@ namespace FileConverter public List InputTypes { get; - private set; + set; } - [System.Xml.Serialization.XmlElement] + [System.Xml.Serialization.XmlIgnore] public Setting[] Settings { get; - private set; + set; } public class Setting diff --git a/Application/FileConverter/FileConverter.csproj b/Application/FileConverter/FileConverter.csproj index 4e1cf3b..a57eb94 100644 --- a/Application/FileConverter/FileConverter.csproj +++ b/Application/FileConverter/FileConverter.csproj @@ -79,6 +79,7 @@ SettingsWindow.xaml + MSBuild:Compile Designer diff --git a/Application/FileConverter/FileConverter.sln.DotSettings b/Application/FileConverter/FileConverter.sln.DotSettings index 0599cda..8b6b0a3 100644 --- a/Application/FileConverter/FileConverter.sln.DotSettings +++ b/Application/FileConverter/FileConverter.sln.DotSettings @@ -5,4 +5,5 @@ DO_NOT_SHOW DO_NOT_SHOW False - False \ No newline at end of file + False + <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /> \ No newline at end of file diff --git a/Application/FileConverter/Settings.cs b/Application/FileConverter/Settings.cs index f313f68..3480017 100644 --- a/Application/FileConverter/Settings.cs +++ b/Application/FileConverter/Settings.cs @@ -4,17 +4,42 @@ namespace FileConverter { using System; using System.Collections.Generic; + using System.IO; public class Settings { - public List conversionPresets = new List(); + public List ConversionPresets = new List(); public void Load() { - // TODO: Load from file. + List conversionPresets = this.ConversionPresets; + conversionPresets.Clear(); - this.conversionPresets.Add(new ConversionPreset("To Flac", OutputType.Flac, new string[] { "Mp3", "Wav", "Ogg", "Wma" })); - this.conversionPresets.Add(new ConversionPreset("To Ogg", OutputType.Ogg, new string[] { "Mp3", "Wav", "Flac", "Wma" })); + string userFilePath = this.GetSettingsUserFilePath(); + + XmlHelpers.LoadFromFile("Settings", userFilePath, ref conversionPresets); + + // TODO: If user settings doesn't exist, load the default settings. + } + + public void Save() + { + string userFilePath = this.GetSettingsUserFilePath(); + XmlHelpers.SaveToFile("Settings", userFilePath, this.ConversionPresets); + } + + private string GetSettingsUserFilePath() + { + string path = Environment.GetEnvironmentVariable("LocalAppData"); + path = Path.Combine(path, "FileConverter"); + + if (!Directory.Exists(path)) + { + Directory.CreateDirectory(path); + } + + path = Path.Combine(path, "Settings.xml"); + return path; } } } diff --git a/Application/FileConverter/SettingsWindow.xaml b/Application/FileConverter/SettingsWindow.xaml index 07f94d0..1320108 100644 --- a/Application/FileConverter/SettingsWindow.xaml +++ b/Application/FileConverter/SettingsWindow.xaml @@ -15,6 +15,7 @@ + @@ -82,5 +83,9 @@ + +