/* 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.Windows.Forms; 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() { string directory = Path.GetDirectoryName(this.settingsFile); if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) { Directory.CreateDirectory(directory); } 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() { string defaults = Path.Combine(Application.StartupPath, "defaultsettings.xml"); if (File.Exists(defaults)) { using (StreamReader reader = new StreamReader(defaults)) { SerializableDictionary data = (SerializableDictionary)serializer.Deserialize(reader); this.userSettings = data; } } } /// /// This is just a utility method for creating a defaults xml file. Don't use this!!! /// private void SetAllDefaults() { userSettings = new SerializableDictionary(); userSettings[ASUserSettingConstants.X264Step] = 0.25; userSettings[ASUserSettingConstants.Verbosity] = 1; userSettings[ASUserSettingConstants.WhenCompleteAction] = "Do Nothing"; userSettings[ASUserSettingConstants.GrowlEncode] = false; userSettings[ASUserSettingConstants.GrowlQueue] = false; userSettings[ASUserSettingConstants.ProcessPriority] = "Below Normal"; userSettings[ASUserSettingConstants.PreventSleep] = true; userSettings[ASUserSettingConstants.ShowCLI] = false; userSettings[ASUserSettingConstants.SaveLogToCopyDirectory] = false; userSettings[ASUserSettingConstants.SaveLogWithVideo] = false; userSettings[ASUserSettingConstants.DisableLibDvdNav] = false; userSettings[ASUserSettingConstants.SendFile] = false; userSettings[ASUserSettingConstants.MinScanDuration] = 10; userSettings[ASUserSettingConstants.HandBrakeBuild] = 0; userSettings[ASUserSettingConstants.HandBrakeVersion] = string.Empty; userSettings["updateStatus"] = true; userSettings["tooltipEnable"] = true; userSettings["defaultPreset"] = string.Empty; userSettings["skipversion"] = 0; userSettings["autoNaming"] = true; userSettings["autoNamePath"] = string.Empty; userSettings["appcast"] = "http://handbrake.fr/appcast.xml"; userSettings["appcast_unstable"] = "http://handbrake.fr/appcast_unstable.xml"; userSettings["autoNameFormat"] = "{source}-{title}"; userSettings["VLC_Path"] = "C:\\Program Files\\VideoLAN\\vlc\\vlc.exe"; userSettings["MainWindowMinimize"] = true; userSettings["QueryEditorTab"] = false; userSettings["presetNotification"] = false; userSettings["trayIconAlerts"] = true; userSettings["lastUpdateCheckDate"] = DateTime.Today; userSettings["daysBetweenUpdateCheck"] = 7; userSettings["useM4v"] = 0; userSettings["PromptOnUnmatchingQueries"] = true; userSettings["NativeLanguage"] = "Any"; userSettings["DubMode"] = 255; userSettings["CliExeHash"] = string.Empty; userSettings["previewScanCount"] = 10; userSettings["clearOldLogs"] = true; userSettings["AutoNameTitleCase"] = true; userSettings["AutoNameRemoveUnderscore"] = true; userSettings["ActivityWindowLastMode"] = 0; userSettings["useClosedCaption"] = false; userSettings["batchMinDuration"] = "00:18:00"; userSettings["batchMaxDuration"] = "02:30:00"; userSettings["defaultPlayer"] = false; userSettings["SelectedLanguages"] = new StringCollection(); userSettings["DubModeAudio"] = 0; userSettings["DubModeSubtitle"] = 0; userSettings["addOnlyOneAudioPerLanguage"] = true; userSettings["MinTitleLength"] = 10; } } }