/* UserSettingService.cs $ This file is part of the HandBrake source code. Homepage: . It may be used under the terms of the GNU General Public License. */ 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; /// /// The User Setting Serivce /// public class UserSettingService : IUserSettingService { /// /// The Settings File /// private readonly string settingsFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\settings.xml"; /// /// The XML Serializer /// readonly XmlSerializer serializer = new XmlSerializer(typeof(SerializableDictionary)); /// /// The User Settings /// private SerializableDictionary userSettings; /// /// Initializes a new instance of the class. /// public UserSettingService() { this.Load(); if (userSettings == null || userSettings.Count == 0) { this.LoadDefaults(); } } /// /// Set the specified user setting. /// /// /// Name of the property /// /// /// The value to store. /// public void SetUserSetting(string name, object value) { this.userSettings[name] = value; this.Save(); } /// /// Get user setting for a given key. /// /// /// The name. /// /// /// The Type of the setting /// /// /// The user setting /// public T GetUserSetting(string name) { if (this.userSettings.ContainsKey(name)) { return (T)this.userSettings[name]; } return default(T); } /// /// Get an StringCollection type user setting /// /// /// The setting name /// /// /// The settings value /// public StringCollection GetUserSettingStringCollection(string name) { return (StringCollection)this.userSettings[name]; } /// /// Save the User Settings /// private void Save() { using (FileStream strm = new FileStream(this.settingsFile, FileMode.Create, FileAccess.Write)) { serializer.Serialize(strm, this.userSettings); } } /// /// Load the User Settings /// private void Load() { try { if (File.Exists(this.settingsFile)) { using (StreamReader reader = new StreamReader(this.settingsFile)) { SerializableDictionary data = (SerializableDictionary)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); } } /// /// Load Default Settings /// private void LoadDefaults() { // TODO, maybe extract this out to a file. userSettings = new SerializableDictionary(); 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; userSettings[UserSettingConstants.HandBrakeVersion] = string.Empty; } } }