summaryrefslogtreecommitdiffstats
path: root/win/CS/HandBrakeWPF/Services
diff options
context:
space:
mode:
authorsr55 <[email protected]>2018-07-18 22:28:13 +0100
committersr55 <[email protected]>2018-07-18 22:28:27 +0100
commit3e41c7995374e71e9d61b0dce4ac59066a261599 (patch)
treec7e643e1e5df2ab9d3bb78740c4f6ad740efba31 /win/CS/HandBrakeWPF/Services
parent45f24decdf2bb41c8eac24ead218d05fbd92955a (diff)
WinGui: Move UserSettings over to JSON format. Settings from the older XML format will automatically transfer and the legacy files will be removed.
Diffstat (limited to 'win/CS/HandBrakeWPF/Services')
-rw-r--r--win/CS/HandBrakeWPF/Services/Interfaces/IUserSettingService.cs7
-rw-r--r--win/CS/HandBrakeWPF/Services/Presets/PresetService.cs2
-rw-r--r--win/CS/HandBrakeWPF/Services/UpdateService.cs2
-rw-r--r--win/CS/HandBrakeWPF/Services/UserSettingService.cs147
4 files changed, 120 insertions, 38 deletions
diff --git a/win/CS/HandBrakeWPF/Services/Interfaces/IUserSettingService.cs b/win/CS/HandBrakeWPF/Services/Interfaces/IUserSettingService.cs
index 5c85387df..2dee3cde9 100644
--- a/win/CS/HandBrakeWPF/Services/Interfaces/IUserSettingService.cs
+++ b/win/CS/HandBrakeWPF/Services/Interfaces/IUserSettingService.cs
@@ -9,6 +9,8 @@
namespace HandBrakeWPF.Services.Interfaces
{
+ using System;
+
using SettingChangedEventArgs = HandBrakeWPF.EventArgs.SettingChangedEventArgs;
/// <summary>
@@ -49,12 +51,15 @@ namespace HandBrakeWPF.Services.Interfaces
/// <param name="name">
/// The name.
/// </param>
+ /// <param name="convertType">
+ /// The convert Type.
+ /// </param>
/// <typeparam name="T">
/// The Type of the setting
/// </typeparam>
/// <returns>
/// The user setting
/// </returns>
- T GetUserSetting<T>(string name);
+ T GetUserSetting<T>(string name, Type convertType = null);
}
} \ No newline at end of file
diff --git a/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs b/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs
index c53eb4f8a..b851d8a7c 100644
--- a/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs
+++ b/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs
@@ -740,7 +740,7 @@ namespace HandBrakeWPF.Services.Presets
}
// Force Upgrade of presets
- if (this.userSettingService.GetUserSetting<int>(UserSettingConstants.ForcePresetReset) < ForcePresetReset)
+ if (this.userSettingService.GetUserSetting<int>(UserSettingConstants.ForcePresetReset, typeof(int)) < ForcePresetReset)
{
this.userSettingService.SetUserSetting(UserSettingConstants.ForcePresetReset, ForcePresetReset);
diff --git a/win/CS/HandBrakeWPF/Services/UpdateService.cs b/win/CS/HandBrakeWPF/Services/UpdateService.cs
index d268a06e4..6a2a289a9 100644
--- a/win/CS/HandBrakeWPF/Services/UpdateService.cs
+++ b/win/CS/HandBrakeWPF/Services/UpdateService.cs
@@ -77,7 +77,7 @@ namespace HandBrakeWPF.Services
if (this.userSettingService.GetUserSetting<bool>(UserSettingConstants.UpdateStatus))
{
DateTime lastUpdateCheck = this.userSettingService.GetUserSetting<DateTime>(UserSettingConstants.LastUpdateCheckDate);
- int checkFrequency = this.userSettingService.GetUserSetting<int>(UserSettingConstants.DaysBetweenUpdateCheck) == 0 ? 7 : 30;
+ int checkFrequency = this.userSettingService.GetUserSetting<int>(UserSettingConstants.DaysBetweenUpdateCheck, typeof(int)) == 0 ? 7 : 30;
if (DateTime.Now.Subtract(lastUpdateCheck).TotalDays > checkFrequency)
{
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