summaryrefslogtreecommitdiffstats
path: root/win/CS/HandBrakeWPF/Services/UserSettingService.cs
diff options
context:
space:
mode:
Diffstat (limited to 'win/CS/HandBrakeWPF/Services/UserSettingService.cs')
-rw-r--r--win/CS/HandBrakeWPF/Services/UserSettingService.cs147
1 files changed, 112 insertions, 35 deletions
diff --git a/win/CS/HandBrakeWPF/Services/UserSettingService.cs b/win/CS/HandBrakeWPF/Services/UserSettingService.cs
index b6c3f5724..8bd70f509 100644
--- a/win/CS/HandBrakeWPF/Services/UserSettingService.cs
+++ b/win/CS/HandBrakeWPF/Services/UserSettingService.cs
@@ -10,6 +10,9 @@
namespace HandBrakeWPF.Services
{
using System;
+ using System.Collections.Generic;
+ using System.Collections.Specialized;
+ using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
@@ -17,32 +20,28 @@ namespace HandBrakeWPF.Services
using HandBrake.Interop.Utilities;
+ using HandBrakeWPF.Collections;
+ using HandBrakeWPF.Extensions;
using HandBrakeWPF.Properties;
using HandBrakeWPF.Services.Interfaces;
using HandBrakeWPF.Utilities;
- using GeneralApplicationException = HandBrakeWPF.Exceptions.GeneralApplicationException;
- using SettingChangedEventArgs = HandBrakeWPF.EventArgs.SettingChangedEventArgs;
+ using Newtonsoft.Json;
+ using Newtonsoft.Json.Linq;
+
+ using GeneralApplicationException = Exceptions.GeneralApplicationException;
+ using SettingChangedEventArgs = EventArgs.SettingChangedEventArgs;
/// <summary>
/// The User Setting Service
/// </summary>
public class UserSettingService : IUserSettingService
{
- /// <summary>
- /// The Settings File
- /// </summary>
- private readonly string settingsFile = Path.Combine(DirectoryUtilities.GetUserStoragePath(VersionHelper.IsNightly()), "settings.xml");
-
- /// <summary>
- /// The XML Serializer
- /// </summary>
- private readonly XmlSerializer serializer = new XmlSerializer(typeof(Collections.SerializableDictionary<string, object>));
-
- /// <summary>
- /// The User Settings
- /// </summary>
- private Collections.SerializableDictionary<string, object> userSettings;
+ private readonly string settingsFile = Path.Combine(DirectoryUtilities.GetUserStoragePath(VersionHelper.IsNightly()), "settings.json");
+ private readonly string releaseSettingsFile = Path.Combine(DirectoryUtilities.GetUserStoragePath(false), "settings.json");
+ private readonly string nightlySettingsFile = Path.Combine(DirectoryUtilities.GetUserStoragePath(true), "settings.json");
+ private readonly JsonSerializerSettings settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
+ private Dictionary<string, object> userSettings;
/// <summary>
/// Initializes a new instance of the <see cref="UserSettingService"/> class.
@@ -80,17 +79,41 @@ namespace HandBrakeWPF.Services
/// <param name="name">
/// The name.
/// </param>
+ /// <param name="conversionType">
+ /// The conversion Type.
+ /// </param>
/// <typeparam name="T">
/// The Type of the setting
/// </typeparam>
/// <returns>
/// The user setting
/// </returns>
- public T GetUserSetting<T>(string name)
+ public T GetUserSetting<T>(string name, Type conversionType = null)
{
if (this.userSettings.ContainsKey(name))
{
- return (T)this.userSettings[name];
+ if (conversionType != null && typeof(int) == conversionType)
+ {
+ object storedValue = this.userSettings[name];
+ object converted = storedValue?.ToString().ToInt();
+ return (T)converted;
+ }
+
+ // Treat String Arrays as StringCollections. TODO refactor upstream code to more traditional string arrays.
+ object settingValue = this.userSettings[name];
+ if (settingValue.GetType() == typeof(JArray))
+ {
+ string[] stringArr = ((JArray)settingValue).ToObject<string[]>();
+ StringCollection stringCollection = new StringCollection();
+ foreach (var item in stringArr)
+ {
+ stringCollection.Add(item);
+ }
+
+ settingValue = stringCollection;
+ }
+
+ return (T)settingValue;
}
return default(T);
@@ -124,9 +147,11 @@ namespace HandBrakeWPF.Services
Directory.CreateDirectory(directory);
}
- using (FileStream strm = new FileStream(this.settingsFile, FileMode.Create, FileAccess.Write))
+
+ using (StreamWriter file = new StreamWriter(new FileStream(this.settingsFile, FileMode.Create, FileAccess.Write)))
{
- this.serializer.Serialize(strm, this.userSettings);
+ string appSettings = JsonConvert.SerializeObject(this.userSettings, Formatting.Indented, this.settings);
+ file.Write(appSettings);
}
}
catch (Exception exc)
@@ -150,35 +175,37 @@ namespace HandBrakeWPF.Services
{
using (StreamReader reader = new StreamReader(this.settingsFile))
{
- Collections.SerializableDictionary<string, object> data = (Collections.SerializableDictionary<string, object>)this.serializer.Deserialize(reader);
- this.userSettings = data;
+ string appSettings = reader.ReadToEnd();
+ Dictionary<string, object> deserialisedSettings = JsonConvert.DeserializeObject< Dictionary<string, object>>(appSettings);
+
+ this.userSettings = deserialisedSettings;
}
}
- else if (VersionHelper.IsNightly() && File.Exists(Path.Combine(DirectoryUtilities.GetUserStoragePath(false), "settings.xml")))
+ else if (VersionHelper.IsNightly() && File.Exists(this.releaseSettingsFile))
{
// Port the release versions config to the nightly.
- string releasePresetFile = Path.Combine(DirectoryUtilities.GetUserStoragePath(false), "settings.xml");
-
if (!Directory.Exists(DirectoryUtilities.GetUserStoragePath(true)))
{
Directory.CreateDirectory(DirectoryUtilities.GetUserStoragePath(true));
}
- File.Copy(releasePresetFile, Path.Combine(DirectoryUtilities.GetUserStoragePath(true), "settings.xml"));
+ File.Copy(this.releaseSettingsFile, this.nightlySettingsFile);
using (StreamReader reader = new StreamReader(this.settingsFile))
{
- Collections.SerializableDictionary<string, object> data = (Collections.SerializableDictionary<string, object>)this.serializer.Deserialize(reader);
- this.userSettings = data;
+ string appSettings = reader.ReadToEnd();
+ Dictionary<string, object> deserialisedSettings = JsonConvert.DeserializeObject<Dictionary<string, object>>(appSettings);
+ this.userSettings = deserialisedSettings;
}
}
else
{
- this.userSettings = new Collections.SerializableDictionary<string, object>();
+ Dictionary<string, object> deserialisedSettings = this.GetLegacySettings(); // Check for Legacy files
+ this.userSettings = deserialisedSettings ?? new SerializableDictionary<string, object>();
}
// Add any missing / new settings
- Collections.SerializableDictionary<string, object> defaults = this.GetDefaults();
+ SerializableDictionary<string, object> defaults = this.GetDefaults();
foreach (var item in defaults.Where(item => !this.userSettings.Keys.Contains(item.Key)))
{
this.userSettings.Add(item.Key, item.Value);
@@ -194,6 +221,7 @@ namespace HandBrakeWPF.Services
{
File.Delete(this.settingsFile);
}
+
this.Save();
throw new GeneralApplicationException(Resources.UserSettings_YourSettingsHaveBeenReset, Resources.UserSettings_YourSettingsAreCorrupt, exc);
@@ -211,23 +239,72 @@ namespace HandBrakeWPF.Services
/// <returns>
/// The get defaults.
/// </returns>
- private Collections.SerializableDictionary<string, object> GetDefaults()
+ private SerializableDictionary<string, object> GetDefaults()
{
+ // TODO Convert this to JSON.
try
{
Assembly assembly = Assembly.GetEntryAssembly();
Stream stream = assembly.GetManifestResourceStream("HandBrakeWPF.defaultsettings.xml");
if (stream != null)
{
- return (Collections.SerializableDictionary<string, object>)this.serializer.Deserialize(stream);
+ XmlSerializer serializer = new XmlSerializer(typeof(Collections.SerializableDictionary<string, object>));
+ return (SerializableDictionary<string, object>)serializer.Deserialize(stream);
}
}
catch (Exception)
{
- return new Collections.SerializableDictionary<string, object>();
+ return new SerializableDictionary<string, object>();
+ }
+
+ return new SerializableDictionary<string, object>();
+ }
+
+ private SerializableDictionary<string, object> GetLegacySettings()
+ {
+ // TODO can be removed after 1.2 releases. Useful for 1 version to upgrade users settings to the new format.
+ SerializableDictionary<string, object> oldAppSettings = null;
+
+ try
+ {
+ string legacyReleaseFile = Path.Combine(DirectoryUtilities.GetUserStoragePath(false), "settings.xml");
+ string legacyNightlyFile = Path.Combine(DirectoryUtilities.GetUserStoragePath(true), "settings.xml");
+
+ if (VersionHelper.IsNightly())
+ {
+ if (File.Exists(legacyNightlyFile))
+ {
+ using (StreamReader reader = new StreamReader(legacyNightlyFile))
+ {
+ XmlSerializer serializer =
+ new XmlSerializer(typeof(SerializableDictionary<string, object>));
+ oldAppSettings = (SerializableDictionary<string, object>)serializer.Deserialize(reader);
+ }
+
+ File.Delete(legacyNightlyFile);
+ }
+ }
+ else
+ {
+ if (File.Exists(legacyReleaseFile))
+ {
+ using (StreamReader reader = new StreamReader(legacyReleaseFile))
+ {
+ XmlSerializer serializer =
+ new XmlSerializer(typeof(SerializableDictionary<string, object>));
+ oldAppSettings = (SerializableDictionary<string, object>)serializer.Deserialize(reader);
+ }
+
+ File.Delete(legacyReleaseFile);
+ }
+ }
+ }
+ catch (Exception exc)
+ {
+ Debug.WriteLine(exc);
}
- return new Collections.SerializableDictionary<string, object>();
+ return oldAppSettings;
}
}
-}
+} \ No newline at end of file