summaryrefslogtreecommitdiffstats
path: root/win/CS/HandBrakeWPF/Services
diff options
context:
space:
mode:
Diffstat (limited to 'win/CS/HandBrakeWPF/Services')
-rw-r--r--win/CS/HandBrakeWPF/Services/Presets/PresetService.cs85
1 files changed, 56 insertions, 29 deletions
diff --git a/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs b/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs
index 2528679b6..be8357f68 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;
using System.Windows.Forms;
using System.Xml.Serialization;
@@ -24,6 +25,7 @@ namespace HandBrakeWPF.Services.Presets
using HandBrake.ApplicationServices.Services.Encode.Model.Models;
using HandBrake.ApplicationServices.Utilities;
+ using HandBrakeWPF.Services.Interfaces;
using HandBrakeWPF.Services.Presets.Interfaces;
using HandBrakeWPF.Services.Presets.Model;
@@ -34,9 +36,6 @@ namespace HandBrakeWPF.Services.Presets
/// </summary>
public class PresetService : IPresetService
{
-
- // TODO refactor filename
-
#region Private Variables
private static readonly int CurrentPresetVersion = 1;
@@ -61,7 +60,6 @@ namespace HandBrakeWPF.Services.Presets
/// </summary>
private readonly string legacyUserPresetFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\user_presets.xml";
-
/// <summary>
/// The Built In Presets File
/// </summary>
@@ -72,9 +70,25 @@ namespace HandBrakeWPF.Services.Presets
/// </summary>
private readonly ObservableCollection<Preset> presets = new ObservableCollection<Preset>();
+ /// <summary>
+ /// The Error Service.
+ /// </summary>
+ private readonly IErrorService errorService;
+
#endregion
/// <summary>
+ /// Initializes a new instance of the <see cref="PresetService"/> class.
+ /// </summary>
+ /// <param name="errorService">
+ /// The error service.
+ /// </param>
+ public PresetService(IErrorService errorService)
+ {
+ this.errorService = errorService;
+ }
+
+ /// <summary>
/// Gets a Collection of presets.
/// </summary>
public ObservableCollection<Preset> Presets
@@ -392,25 +406,32 @@ namespace HandBrakeWPF.Services.Presets
/// <param name="file">
/// The broken presets file.
/// </param>
- private static void RecoverFromCorruptedPresetFile(string file)
+ /// <returns>
+ /// The <see cref="string"/>.
+ /// </returns>
+ private static string RecoverFromCorruptedPresetFile(string file)
{
try
{
// Recover from Error.
+ string disabledFile = string.Format("{0}.{1}", file, GeneralUtilities.ProcessId);
if (File.Exists(file))
{
- string disabledFile = string.Format("{0}.{1}", file, GeneralUtilities.ProcessId);
- File.Move(file, disabledFile);
+ File.Move(file, disabledFile);
if (File.Exists(file))
{
File.Delete(file);
}
}
+
+ return disabledFile;
}
catch (IOException)
{
// Give up
}
+
+ return "Sorry, the archiving failed.";
}
/// <summary>
@@ -477,40 +498,46 @@ namespace HandBrakeWPF.Services.Presets
if (File.Exists(this.userPresetFile))
{
// New Preset Format.
+ bool createBackup = false;
+ PresetContainer presetContainer = null;
using (StreamReader reader = new StreamReader(this.userPresetFile))
{
- PresetContainer presetContainer = null;
-
- bool createBackup = false;
try
{
- presetContainer = JsonConvert.DeserializeObject<PresetContainer>(reader.ReadToEnd());
- }
+ 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);
- }
+ // 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 fileName = RecoverFromCorruptedPresetFile(this.userPresetFile);
+ this.errorService.ShowMessageBox(
+ "HandBrake is unable to load your user presets because they are from an older version of HandBrake. Your old presets file has been renamed so that it doesn't get loaded on next launch."
+ + Environment.NewLine + Environment.NewLine + "Archived File: " + fileName,
+ "Unable to load user presets.",
+ MessageBoxButton.OK,
+ MessageBoxImage.Exclamation);
+ return;
+ }
- // Load the current presets.
- if (presetContainer != null && !string.IsNullOrEmpty(presetContainer.Presets))
+ // 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)
{
- 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);
- }
+ this.presets.Add(preset);
}
- }
+ }
}
// We did a preset convertion, so save the updates.