diff options
Diffstat (limited to 'win/CS/HandBrake.ApplicationServices/Services/UserSettingService.cs')
-rw-r--r-- | win/CS/HandBrake.ApplicationServices/Services/UserSettingService.cs | 146 |
1 files changed, 96 insertions, 50 deletions
diff --git a/win/CS/HandBrake.ApplicationServices/Services/UserSettingService.cs b/win/CS/HandBrake.ApplicationServices/Services/UserSettingService.cs index f6d6c55e2..6ef071d4b 100644 --- a/win/CS/HandBrake.ApplicationServices/Services/UserSettingService.cs +++ b/win/CS/HandBrake.ApplicationServices/Services/UserSettingService.cs @@ -5,6 +5,14 @@ namespace HandBrake.ApplicationServices.Services
{
+ using System;
+ using System.Collections.Generic;
+ using System.Collections.Specialized;
+ using System.IO;
+ using System.Xml.Serialization;
+
+ using HandBrake.ApplicationServices.Collections;
+ using HandBrake.ApplicationServices.Exceptions;
using HandBrake.ApplicationServices.Services.Interfaces;
/// <summary>
@@ -13,6 +21,33 @@ namespace HandBrake.ApplicationServices.Services public class UserSettingService : IUserSettingService
{
/// <summary>
+ /// The Settings File
+ /// </summary>
+ private readonly string settingsFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\settings.xml";
+
+ /// <summary>
+ /// The XML Serializer
+ /// </summary>
+ readonly XmlSerializer serializer = new XmlSerializer(typeof(SerializableDictionary<string, object>));
+
+ /// <summary>
+ /// The User Settings
+ /// </summary>
+ private SerializableDictionary<string, object> userSettings;
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="UserSettingService"/> class.
+ /// </summary>
+ public UserSettingService()
+ {
+ this.Load();
+ if (userSettings == null || userSettings.Count == 0)
+ {
+ this.LoadDefaults();
+ }
+ }
+
+ /// <summary>
/// Set the specified user setting.
/// </summary>
/// <param name="name">
@@ -23,29 +58,34 @@ namespace HandBrake.ApplicationServices.Services /// </param>
public void SetUserSetting(string name, object value)
{
- Properties.Settings.Default[name] = value;
- Properties.Settings.Default.Save();
+ this.userSettings[name] = value;
+ this.Save();
}
/// <summary>
- /// Get an Integer type user setting
+ /// Get user setting for a given key.
/// </summary>
/// <param name="name">
- /// The setting name
+ /// The name.
/// </param>
+ /// <typeparam name="T">
+ /// The Type of the setting
+ /// </typeparam>
/// <returns>
- /// The settings value
+ /// The user setting
/// </returns>
- public int GetUserSettingInt(string name)
+ public T GetUserSetting<T>(string name)
{
- int value;
- int.TryParse(Properties.Settings.Default[name].ToString(), out value);
+ if (this.userSettings.ContainsKey(name))
+ {
+ return (T)this.userSettings[name];
+ }
- return value;
+ return default(T);
}
/// <summary>
- /// Get an String type user setting
+ /// Get an StringCollection type user setting
/// </summary>
/// <param name="name">
/// The setting name
@@ -53,62 +93,68 @@ namespace HandBrake.ApplicationServices.Services /// <returns>
/// The settings value
/// </returns>
- public string GetUserSettingString(string name)
+ public StringCollection GetUserSettingStringCollection(string name)
{
- return Properties.Settings.Default[name].ToString();
+ return (StringCollection)this.userSettings[name];
}
/// <summary>
- /// Get an Boolean type user setting
+ /// Save the User Settings
/// </summary>
- /// <param name="name">
- /// The setting name
- /// </param>
- /// <returns>
- /// The settings value
- /// </returns>
- public bool GetUserSettingBoolean(string name)
+ private void Save()
{
- bool value;
- bool.TryParse(Properties.Settings.Default[name].ToString(), out value);
-
- return value;
+ using (FileStream strm = new FileStream(this.settingsFile, FileMode.Create, FileAccess.Write))
+ {
+ serializer.Serialize(strm, this.userSettings);
+ }
}
/// <summary>
- /// Get an Double type user setting
+ /// Load the User Settings
/// </summary>
- /// <param name="name">
- /// The setting name
- /// </param>
- /// <returns>
- /// The settings value
- /// </returns>
- public double GetUserSettingDouble(string name)
+ private void Load()
{
- double value;
- double.TryParse(Properties.Settings.Default[name].ToString(), out value);
-
- return value;
+ try
+ {
+ if (File.Exists(this.settingsFile))
+ {
+ using (StreamReader reader = new StreamReader(this.settingsFile))
+ {
+ SerializableDictionary<string, object> data = (SerializableDictionary<string, object>)serializer.Deserialize(reader);
+ this.userSettings = data;
+ }
+ }
+ }
+ catch (Exception exc)
+ {
+ throw new GeneralApplicationException(
+ "HandBrake has detected corruption in the settings file. User settings will now be reset to defaults.",
+ "Please restart HandBrake before continuing.",
+ exc);
+ }
}
/// <summary>
- /// Get an StringCollection type user setting
+ /// Load Default Settings
/// </summary>
- /// <param name="name">
- /// The setting name
- /// </param>
- /// <returns>
- /// The settings value
- /// </returns>
- public System.Collections.Specialized.StringCollection GetUserSettingStringCollection(string name)
+ private void LoadDefaults()
{
- System.Collections.Specialized.StringCollection value;
-
- value = (System.Collections.Specialized.StringCollection) Properties.Settings.Default[name];
-
- return value;
+ // TODO, maybe extract this out to a file.
+ userSettings = new SerializableDictionary<string, object>();
+ userSettings[UserSettingConstants.X264Step] = 0.25;
+ userSettings[UserSettingConstants.Verbosity] = 1;
+ userSettings[UserSettingConstants.WhenCompleteAction] = "Do Nothing";
+ userSettings[UserSettingConstants.GrowlEncode] = false;
+ userSettings[UserSettingConstants.GrowlQueue] = false;
+ userSettings[UserSettingConstants.ProcessPriority] = "Below Normal";
+ userSettings[UserSettingConstants.PreventSleep] = true;
+ userSettings[UserSettingConstants.ShowCLI] = false;
+ userSettings[UserSettingConstants.SaveLogToCopyDirectory] = false;
+ userSettings[UserSettingConstants.SaveLogWithVideo] = false;
+ userSettings[UserSettingConstants.DisableLibDvdNav] = false;
+ userSettings[UserSettingConstants.SendFile] = false;
+ userSettings[UserSettingConstants.MinScanDuration] = 10;
+ userSettings[UserSettingConstants.HandBrakeBuild] = 0;
}
-
}
}
|