mirror of
https://github.com/Tichau/FileConverter.git
synced 2026-11-28 01:10:56 +01:00
Serialize and deserialize settings.
This commit is contained in:
1 parent
43f5cb91cd
commit
03e975b371
7 files changed
+130
-9
No files matched your search
@@ -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<string> InputTypes
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
set;
|
||||
}
|
||||
|
||||
[System.Xml.Serialization.XmlElement]
|
||||
[System.Xml.Serialization.XmlIgnore]
|
||||
public Setting[] Settings
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
set;
|
||||
}
|
||||
|
||||
public class Setting
|
||||
|
||||
@@ -79,6 +79,7 @@
|
||||
<DependentUpon>SettingsWindow.xaml</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Version.cs" />
|
||||
<Compile Include="XmlHelpers.cs" />
|
||||
<Page Include="MainWindow.xaml">
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
<SubType>Designer</SubType>
|
||||
|
||||
@@ -5,4 +5,5 @@
|
||||
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=StyleCop_002ESA1602/@EntryIndexedValue">DO_NOT_SHOW</s:String>
|
||||
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=SuggestUseVarKeywordEvident/@EntryIndexedValue">DO_NOT_SHOW</s:String>
|
||||
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_ABSTRACT_ACCESSORHOLDER_ON_SINGLE_LINE/@EntryValue">False</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_SIMPLE_ACCESSOR_ATTRIBUTE_ON_SAME_LINE/@EntryValue">False</s:Boolean></wpf:ResourceDictionary>
|
||||
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_SIMPLE_ACCESSOR_ATTRIBUTE_ON_SAME_LINE/@EntryValue">False</s:Boolean>
|
||||
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=MethodPropertyEvent/@EntryIndexedValue"><Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /></s:String></wpf:ResourceDictionary>
|
||||
@@ -4,17 +4,42 @@ namespace FileConverter
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
public class Settings
|
||||
{
|
||||
public List<ConversionPreset> conversionPresets = new List<ConversionPreset>();
|
||||
public List<ConversionPreset> ConversionPresets = new List<ConversionPreset>();
|
||||
|
||||
public void Load()
|
||||
{
|
||||
// TODO: Load from file.
|
||||
List<ConversionPreset> 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<ConversionPreset>("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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="48"/>
|
||||
<RowDefinition Height="*"/>
|
||||
<RowDefinition Height="28"/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<Grid>
|
||||
@@ -82,5 +83,9 @@
|
||||
</Grid>
|
||||
</GroupBox>
|
||||
</Grid>
|
||||
|
||||
<Button Name="CloseButton" Grid.Row="2" Content="Close" HorizontalAlignment="Right" Width="100" Margin="0,4,105,0" Click="CloseButton_Click" />
|
||||
<Button Name="SaveButton" Grid.Row="2" Content="Save" HorizontalAlignment="Right" Width="100" Margin="0,4,0,0" Click="SaveButton_Click"/>
|
||||
|
||||
</Grid>
|
||||
</Window>
|
||||
@@ -22,7 +22,7 @@ namespace FileConverter
|
||||
this.InitializeComponent();
|
||||
|
||||
Application application = Application.Current as Application;
|
||||
this.PresetList.ItemsSource = application.Settings.conversionPresets;
|
||||
this.PresetList.ItemsSource = application.Settings.ConversionPresets;
|
||||
|
||||
OutputType[] outputTypes = new[]
|
||||
{
|
||||
@@ -79,5 +79,23 @@ namespace FileConverter
|
||||
|
||||
this.selectedPreset.InputTypes.Remove(inputFormat);
|
||||
}
|
||||
|
||||
private void CloseButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
// Load previous preset in order to cancel changes.
|
||||
Application application = Application.Current as Application;
|
||||
application.Settings.Load();
|
||||
|
||||
this.Close();
|
||||
}
|
||||
|
||||
private void SaveButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
// Save changes.
|
||||
Application application = Application.Current as Application;
|
||||
application.Settings.Save();
|
||||
|
||||
this.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
using FileConverter;
|
||||
|
||||
public class XmlHelpers
|
||||
{
|
||||
public static void LoadFromFile<T>(string root, string path, ref List<T> list)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
throw new ArgumentNullException("path");
|
||||
}
|
||||
|
||||
XmlRootAttribute xmlRoot = new XmlRootAttribute
|
||||
{
|
||||
ElementName = root
|
||||
};
|
||||
|
||||
XmlSerializer serializer = new XmlSerializer(typeof(List<T>), xmlRoot);
|
||||
|
||||
try
|
||||
{
|
||||
using (StreamReader reader = new StreamReader(path))
|
||||
using (XmlReader xmlReader = XmlReader.Create(reader, new XmlReaderSettings { IgnoreWhitespace = true, IgnoreComments = true }))
|
||||
{
|
||||
list.AddRange((List<T>)serializer.Deserialize(xmlReader));
|
||||
}
|
||||
}
|
||||
catch (System.Exception exception)
|
||||
{
|
||||
Diagnostics.Log("The database of type '" + typeof(T) + "' failed to load the asset. The following exception was raised:\n " + exception.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SaveToFile<T>(string root, string path, List<T> objectsToSerialize)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
XmlRootAttribute xmlRoot = new XmlRootAttribute
|
||||
{
|
||||
ElementName = root
|
||||
};
|
||||
|
||||
XmlSerializer serializer = new XmlSerializer(typeof(List<T>), xmlRoot);
|
||||
|
||||
try
|
||||
{
|
||||
using (StreamWriter writer = new StreamWriter(path))
|
||||
using (XmlWriter xmlWriter = XmlWriter.Create(writer, new XmlWriterSettings { Indent = true, IndentChars = " " }))
|
||||
{
|
||||
serializer.Serialize(xmlWriter, objectsToSerialize);
|
||||
}
|
||||
}
|
||||
catch (System.Exception exception)
|
||||
{
|
||||
Diagnostics.Log("The database of type '" + typeof(T) + "' failed to load the asset. The following exception was raised:\n " + exception.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user