From fa74388f8e5a42a400daba5ecff61e856d04e6c2 Mon Sep 17 00:00:00 2001 From: sr55 Date: Fri, 12 Dec 2014 20:59:12 +0000 Subject: WinGui: Switching the preset format over to JSON and adding a container object for the presets which should make the archiving of presets when the app is unable to load them a bit more reliable. The app will try and bring forward the settings it can, but will not migrate ones it can't. Backup your presets before updating to this build incase something goes wrong! git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@6599 b64f7644-9d1e-0410-96f1-a4d463321fa5 --- .../Services/Presets/Model/PresetContainer.cs | 30 +++++++ .../HandBrakeWPF/Services/Presets/PresetService.cs | 95 ++++++++++++++++++++-- 2 files changed, 119 insertions(+), 6 deletions(-) create mode 100644 win/CS/HandBrakeWPF/Services/Presets/Model/PresetContainer.cs (limited to 'win/CS/HandBrakeWPF/Services/Presets') diff --git a/win/CS/HandBrakeWPF/Services/Presets/Model/PresetContainer.cs b/win/CS/HandBrakeWPF/Services/Presets/Model/PresetContainer.cs new file mode 100644 index 000000000..be7e4afdc --- /dev/null +++ b/win/CS/HandBrakeWPF/Services/Presets/Model/PresetContainer.cs @@ -0,0 +1,30 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. +// +// +// A container object for presets. This object should not change often as it's designed for preset version tracking. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace HandBrakeWPF.Services.Presets.Model +{ + public class PresetContainer + { + public PresetContainer(int version, string presets) + { + Version = version; + Presets = presets; + } + + /// + /// Gets or sets the version of the presets stored in this container. + /// + public int Version { get; set; } + + /// + /// Gets or sets the presets. This is a serialised string. + /// + public string Presets { get; set; } + } +} diff --git a/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs b/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs index 48c3409cb..82d3b22be 100644 --- a/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs +++ b/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs @@ -17,6 +17,7 @@ namespace HandBrakeWPF.Services.Presets using System.Reflection; using System.Text; using System.Text.RegularExpressions; + using System.Windows.Forms; using System.Xml.Serialization; using HandBrake.ApplicationServices.Exceptions; @@ -26,13 +27,20 @@ namespace HandBrakeWPF.Services.Presets using HandBrakeWPF.Services.Presets.Interfaces; using HandBrakeWPF.Services.Presets.Model; + using Newtonsoft.Json; + /// /// The preset service manages HandBrake's presets /// public class PresetService : IPresetService { + + // TODO refactor filename + #region Private Variables + private static readonly int CurrentPresetVersion = 1; + /// /// XML Serializer /// @@ -46,7 +54,13 @@ namespace HandBrakeWPF.Services.Presets /// /// The User Preset file /// - private readonly string userPresetFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\user_presets.xml"; + private readonly string userPresetFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\user_presets.json"; + + /// + /// The Legacy Preset file + /// + private readonly string legacyUserPresetFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\user_presets.xml"; + /// /// The Built In Presets File @@ -432,18 +446,78 @@ namespace HandBrakeWPF.Services.Presets // Load in the users Presets from UserPresets.xml try - { + { + // Handle Legacy Preset Format. + bool updatePresets = false; + if (File.Exists(this.legacyUserPresetFile)) + { + using (StreamReader reader = new StreamReader(this.legacyUserPresetFile)) + { + try + { + var oldPresets = (List)Ser.Deserialize(reader); + foreach (Preset oldPreset in oldPresets) + { + this.presets.Add(oldPreset); + } + updatePresets = true; + + } + catch (Exception exc) + { + // Do Nothing + } + } + + // Archive the old file incase the user needs it. + File.Move(this.legacyUserPresetFile, this.legacyUserPresetFile + ".archive." + GeneralUtilities.ProcessId); + } + + // New JSON Format. if (File.Exists(this.userPresetFile)) { + // New Preset Format. using (StreamReader reader = new StreamReader(this.userPresetFile)) { - List list = (List)Ser.Deserialize(reader); - foreach (Preset preset in list) + PresetContainer presetContainer = null; + + bool createBackup = false; + try { - this.presets.Add(preset); + presetContainer = JsonConvert.DeserializeObject(reader.ReadToEnd()); + } + catch (Exception exc) + { + createBackup = true; + } + + // If we have old presets, or the container wasn't parseable, or we have a version mismatch, backup the user preset file + // incase something goes wrong. + if (createBackup || (presetContainer != null && presetContainer.Version < CurrentPresetVersion)) + { + string backupFile = this.userPresetFile + "." + GeneralUtilities.ProcessId; + File.Copy(this.userPresetFile, backupFile); + } + + // Load the current presets. + if (presetContainer != null && !string.IsNullOrEmpty(presetContainer.Presets)) + { + JsonSerializerSettings settings = new JsonSerializerSettings(); + settings.MissingMemberHandling = MissingMemberHandling.Ignore; + List list = JsonConvert.DeserializeObject>(presetContainer.Presets); + foreach (Preset preset in list) + { + this.presets.Add(preset); + } } } } + + // We did a preset convertion, so save the updates. + if (updatePresets) + { + UpdatePresetFiles(); + } } catch (Exception exc) { @@ -472,7 +546,16 @@ namespace HandBrakeWPF.Services.Presets using (FileStream strm = new FileStream(this.userPresetFile, FileMode.Create, FileAccess.Write)) { - Ser.Serialize(strm, this.presets.Where(p => p.IsBuildIn == false).ToList()); + List userPresets = this.presets.Where(p => p.IsBuildIn == false).ToList(); + string presetsJson = JsonConvert.SerializeObject(userPresets, Formatting.Indented); + + PresetContainer container = new PresetContainer(CurrentPresetVersion, presetsJson); + string containerJson = JsonConvert.SerializeObject(container, Formatting.Indented); + + using (StreamWriter writer = new StreamWriter(strm)) + { + writer.WriteLine(containerJson); + } } } catch (Exception exc) -- cgit v1.2.3