Merge user settings with default settings to prevent errors after installing a new version.

This commit is contained in:
Tichau committed 2015-12-21 22:59:53 +01:00
1 parent b6980780a8
commit 07f2f9a2dd
3 files changed
+54 -47

No files matched your search

+5 -13
View File
@@ -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
+48 -28
View File
@@ -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>("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>("Settings", userFilePath, ref settings);
XmlHelpers.LoadFromFile<Settings>("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>("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;
}
}
}
+1 -6
View File
@@ -10,18 +10,13 @@ namespace FileConverter
public class XmlHelpers
{
public static void LoadFromFile<T>(string root, string path, ref T deserializedObject)
public static void LoadFromFile<T>(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