summaryrefslogtreecommitdiffstats
path: root/win/CS/HandBrakeWPF/Services
diff options
context:
space:
mode:
authorsr55 <[email protected]>2014-12-12 20:59:12 +0000
committersr55 <[email protected]>2014-12-12 20:59:12 +0000
commitfa74388f8e5a42a400daba5ecff61e856d04e6c2 (patch)
tree2b904e6cfef563cf7ebd13e81c3a806596118821 /win/CS/HandBrakeWPF/Services
parent6fc41d211e4cbc3c83bdff3c3327deeb072bc1a3 (diff)
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
Diffstat (limited to 'win/CS/HandBrakeWPF/Services')
-rw-r--r--win/CS/HandBrakeWPF/Services/Presets/Model/PresetContainer.cs30
-rw-r--r--win/CS/HandBrakeWPF/Services/Presets/PresetService.cs95
2 files changed, 119 insertions, 6 deletions
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 @@
+// --------------------------------------------------------------------------------------------------------------------
+// <copyright file="PresetContainer.cs" company="HandBrake Project (http://handbrake.fr)">
+// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
+// </copyright>
+// <summary>
+// A container object for presets. This object should not change often as it's designed for preset version tracking.
+// </summary>
+// --------------------------------------------------------------------------------------------------------------------
+
+namespace HandBrakeWPF.Services.Presets.Model
+{
+ public class PresetContainer
+ {
+ public PresetContainer(int version, string presets)
+ {
+ Version = version;
+ Presets = presets;
+ }
+
+ /// <summary>
+ /// Gets or sets the version of the presets stored in this container.
+ /// </summary>
+ public int Version { get; set; }
+
+ /// <summary>
+ /// Gets or sets the presets. This is a serialised string.
+ /// </summary>
+ 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;
+
/// <summary>
/// The preset service manages HandBrake's presets
/// </summary>
public class PresetService : IPresetService
{
+
+ // TODO refactor filename
+
#region Private Variables
+ private static readonly int CurrentPresetVersion = 1;
+
/// <summary>
/// XML Serializer
/// </summary>
@@ -46,7 +54,13 @@ namespace HandBrakeWPF.Services.Presets
/// <summary>
/// The User Preset file
/// </summary>
- 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";
+
+ /// <summary>
+ /// The Legacy Preset file
+ /// </summary>
+ private readonly string legacyUserPresetFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\user_presets.xml";
+
/// <summary>
/// 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<Preset>)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<Preset> list = (List<Preset>)Ser.Deserialize(reader);
- foreach (Preset preset in list)
+ PresetContainer presetContainer = null;
+
+ bool createBackup = false;
+ try
{
- this.presets.Add(preset);
+ presetContainer = JsonConvert.DeserializeObject<PresetContainer>(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<Preset> list = JsonConvert.DeserializeObject<List<Preset>>(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<Preset> 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)