summaryrefslogtreecommitdiffstats
path: root/win
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
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')
-rw-r--r--win/CS/HandBrake.ApplicationServices/Model/Encoding/AudioTrack.cs3
-rw-r--r--win/CS/HandBrakeWPF/HandBrakeWPF.csproj1
-rw-r--r--win/CS/HandBrakeWPF/Properties/Resources.Designer.cs5
-rw-r--r--win/CS/HandBrakeWPF/Properties/Resources.resx3
-rw-r--r--win/CS/HandBrakeWPF/Services/Presets/Model/PresetContainer.cs30
-rw-r--r--win/CS/HandBrakeWPF/Services/Presets/PresetService.cs95
6 files changed, 128 insertions, 9 deletions
diff --git a/win/CS/HandBrake.ApplicationServices/Model/Encoding/AudioTrack.cs b/win/CS/HandBrake.ApplicationServices/Model/Encoding/AudioTrack.cs
index 75e2bb1b3..992d15a8a 100644
--- a/win/CS/HandBrake.ApplicationServices/Model/Encoding/AudioTrack.cs
+++ b/win/CS/HandBrake.ApplicationServices/Model/Encoding/AudioTrack.cs
@@ -19,6 +19,8 @@ namespace HandBrake.ApplicationServices.Model.Encoding
using HandBrake.ApplicationServices.Utilities;
using HandBrake.Interop.Model.Encoding;
+ using Newtonsoft.Json;
+
/// <summary>
/// An Audio Track for the Audio Panel
/// </summary>
@@ -377,6 +379,7 @@ namespace HandBrake.ApplicationServices.Model.Encoding
/// <summary>
/// Gets TrackReference.
/// </summary>
+ [JsonIgnore]
public AudioTrack TrackReference
{
get { return this; }
diff --git a/win/CS/HandBrakeWPF/HandBrakeWPF.csproj b/win/CS/HandBrakeWPF/HandBrakeWPF.csproj
index 7dd16a6dd..3041a20b3 100644
--- a/win/CS/HandBrakeWPF/HandBrakeWPF.csproj
+++ b/win/CS/HandBrakeWPF/HandBrakeWPF.csproj
@@ -167,6 +167,7 @@
<Compile Include="Factories\HBConfigurationFactory.cs" />
<Compile Include="Services\Presets\Interfaces\IPresetService.cs" />
<Compile Include="Services\Interfaces\IUserSettingService.cs" />
+ <Compile Include="Services\Presets\Model\PresetContainer.cs" />
<Compile Include="Services\Presets\PresetService.cs" />
<Compile Include="Services\UserSettingService.cs" />
<Compile Include="Utilities\AppcastReader.cs" />
diff --git a/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs b/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs
index 32236a77b..f027e6ba7 100644
--- a/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs
+++ b/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs
@@ -1,7 +1,7 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
-// Runtime Version:4.0.30319.18444
+// Runtime Version:4.0.30319.34209
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
@@ -556,7 +556,8 @@ namespace HandBrakeWPF.Properties {
/// <summary>
/// Looks up a localized string similar to HandBrake has determined your built-in presets are out of date... These presets will now be updated.
- ///Your custom presets have not been updated so you may have to re-create these by deleting and re-adding them..
+ ///Your custom presets have not been updated so you may have to re-create these by deleting and re-adding them.
+ ///The previous user_presets.xml file was backed up..
/// </summary>
public static string Main_PresetUpdateNotification {
get {
diff --git a/win/CS/HandBrakeWPF/Properties/Resources.resx b/win/CS/HandBrakeWPF/Properties/Resources.resx
index 918a6b44a..e1697925f 100644
--- a/win/CS/HandBrakeWPF/Properties/Resources.resx
+++ b/win/CS/HandBrakeWPF/Properties/Resources.resx
@@ -433,7 +433,8 @@ In order to use the QuickSync encoder, you must:
</data>
<data name="Main_PresetUpdateNotification" xml:space="preserve">
<value>HandBrake has determined your built-in presets are out of date... These presets will now be updated.
-Your custom presets have not been updated so you may have to re-create these by deleting and re-adding them.</value>
+Your custom presets have not been updated so you may have to re-create these by deleting and re-adding them.
+The previous user_presets.xml file was backed up.</value>
</data>
<data name="Main_QueueFinished" xml:space="preserve">
<value>Queue Finished</value>
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)