summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorScott <[email protected]>2015-10-30 20:42:33 +0000
committerScott <[email protected]>2015-10-30 20:42:33 +0000
commitccf4b57959c7a454e01c5491a03e5c144b712114 (patch)
treed0569a276da51240622c921f93455df8f83f44f9
parentd08fc5eea2727c74b12bc0d8fd127c7672804491 (diff)
Tidy up the Preset Service. Preset version checking should now be working for the new format.
-rw-r--r--win/CS/HandBrakeWPF/Properties/Resources.Designer.cs17
-rw-r--r--win/CS/HandBrakeWPF/Properties/Resources.resx11
-rw-r--r--win/CS/HandBrakeWPF/Services/Presets/Factories/JsonPresetFactory.cs2
-rw-r--r--win/CS/HandBrakeWPF/Services/Presets/Model/Preset.cs5
-rw-r--r--win/CS/HandBrakeWPF/Services/Presets/PresetService.cs90
5 files changed, 88 insertions, 37 deletions
diff --git a/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs b/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs
index 6cc8e0f60..30084463c 100644
--- a/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs
+++ b/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs
@@ -1131,7 +1131,9 @@ namespace HandBrakeWPF.Properties {
}
/// <summary>
- /// Looks up a localized string similar to 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&apos;t get loaded on next launch..
+ /// Looks up a localized string similar to HandBrake has detected your presets file is from an older version.
+ ///It will try and load the file anyway.
+ ///If it fails, it will archive off the old file and create a new one..
/// </summary>
public static string PresetService_PresetsOutOfDate {
get {
@@ -1140,7 +1142,7 @@ namespace HandBrakeWPF.Properties {
}
/// <summary>
- /// Looks up a localized string similar to Unable to load user presets..
+ /// Looks up a localized string similar to Unable to load presets..
/// </summary>
public static string PresetService_UnableToLoad {
get {
@@ -1149,6 +1151,17 @@ namespace HandBrakeWPF.Properties {
}
/// <summary>
+ /// Looks up a localized string similar to HandBrake was unable to load your presets file. It may have been from an older unsupported version of HandBrake or corrupted.
+ ///
+ ///Your old presets file was archived to:.
+ /// </summary>
+ public static string PresetService_UnableToLoadPresets {
+ get {
+ return ResourceManager.GetString("PresetService_UnableToLoadPresets", resourceCulture);
+ }
+ }
+
+ /// <summary>
/// Looks up a localized string similar to Preview.
/// </summary>
public static string Preview {
diff --git a/win/CS/HandBrakeWPF/Properties/Resources.resx b/win/CS/HandBrakeWPF/Properties/Resources.resx
index 26770ab9e..7ce2bf391 100644
--- a/win/CS/HandBrakeWPF/Properties/Resources.resx
+++ b/win/CS/HandBrakeWPF/Properties/Resources.resx
@@ -718,9 +718,16 @@ Please make sure VLC is installed and the directory specified in HandBrake's opt
<value>Archived File:</value>
</data>
<data name="PresetService_PresetsOutOfDate" xml:space="preserve">
- <value>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.</value>
+ <value>HandBrake has detected your presets file is from an older version.
+It will try and load the file anyway.
+If it fails, it will archive off the old file and create a new one.</value>
</data>
<data name="PresetService_UnableToLoad" xml:space="preserve">
- <value>Unable to load user presets.</value>
+ <value>Unable to load presets.</value>
+ </data>
+ <data name="PresetService_UnableToLoadPresets" xml:space="preserve">
+ <value>HandBrake was unable to load your presets file. It may have been from an older unsupported version of HandBrake or corrupted.
+
+Your old presets file was archived to:</value>
</data>
</root> \ No newline at end of file
diff --git a/win/CS/HandBrakeWPF/Services/Presets/Factories/JsonPresetFactory.cs b/win/CS/HandBrakeWPF/Services/Presets/Factories/JsonPresetFactory.cs
index 52c0c6e99..453ed39e7 100644
--- a/win/CS/HandBrakeWPF/Services/Presets/Factories/JsonPresetFactory.cs
+++ b/win/CS/HandBrakeWPF/Services/Presets/Factories/JsonPresetFactory.cs
@@ -60,7 +60,7 @@ namespace HandBrakeWPF.Services.Presets.Factories
preset.Task = new EncodeTask();
preset.IsDefault = importedPreset.Default;
preset.IsBuildIn = importedPreset.Type == 0;
-
+
// Step 1, Create the EncodeTask Object that can be loaded into the UI.
/* Output Settings */
diff --git a/win/CS/HandBrakeWPF/Services/Presets/Model/Preset.cs b/win/CS/HandBrakeWPF/Services/Presets/Model/Preset.cs
index 78ed92090..a6190052e 100644
--- a/win/CS/HandBrakeWPF/Services/Presets/Model/Preset.cs
+++ b/win/CS/HandBrakeWPF/Services/Presets/Model/Preset.cs
@@ -85,11 +85,6 @@ namespace HandBrakeWPF.Services.Presets.Model
public EncodeTask Task { get; set; }
/// <summary>
- /// Gets or sets The version number which associates this preset with a HB build
- /// </summary>
- public string Version { get; set; }
-
- /// <summary>
/// Gets or sets the audio track behaviours.
/// </summary>
public AudioBehaviours AudioTrackBehaviours { get; set; }
diff --git a/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs b/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs
index 44e237462..dae5facaf 100644
--- a/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs
+++ b/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs
@@ -73,11 +73,6 @@ namespace HandBrakeWPF.Services.Presets
}
/// <summary>
- /// Gets or sets LastPresetAdded.
- /// </summary>
- public Preset LastPresetAdded { get; set; }
-
- /// <summary>
/// Gets the DefaultPreset.
/// </summary>
public Preset DefaultPreset
@@ -118,10 +113,9 @@ namespace HandBrakeWPF.Services.Presets
/// </returns>
public bool Add(Preset preset)
{
- if (this.CheckIfPresetExists(preset.Name) == false)
+ if (!this.CheckIfPresetExists(preset.Name))
{
this.presets.Add(preset);
- this.LastPresetAdded = preset;
// Update the presets file
this.SavePresetFiles();
@@ -140,12 +134,11 @@ namespace HandBrakeWPF.Services.Presets
/// </param>
public void Import(string filename)
{
- // TODO needs a tidy up but will do for now.
if (!string.IsNullOrEmpty(filename))
{
PresetTransportContainer container = HandBrakePresetService.GetPresetFromFile(filename);
- if (container == null || container.PresetList == null || container.PresetList.Count == 0)
+ if (container?.PresetList == null || container.PresetList.Count == 0)
{
this.errorService.ShowError(Resources.Main_PresetImportFailed, Resources.Main_PresetImportFailedSolution, string.Empty);
return;
@@ -181,11 +174,9 @@ namespace HandBrakeWPF.Services.Presets
return;
}
- // TODO Better version checking.
-
- if (this.CheckIfPresetExists(preset.Name))
+ if (this.CheckIfPresetExists(preset.Name))
{
- if (!CanUpdatePreset(preset.Name))
+ if (!this.CanUpdatePreset(preset.Name))
{
MessageBox.Show(Resources.Main_PresetErrorBuiltInName, Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error);
return;
@@ -194,12 +185,12 @@ namespace HandBrakeWPF.Services.Presets
MessageBoxResult result = MessageBox.Show(Resources.Main_PresetOverwriteWarning, Resources.Overwrite, MessageBoxButton.YesNo, MessageBoxImage.Warning);
if (result == MessageBoxResult.Yes)
{
- Update(preset);
+ this.Update(preset);
}
}
else
{
- Add(preset);
+ this.Add(preset);
}
}
}
@@ -360,8 +351,7 @@ namespace HandBrakeWPF.Services.Presets
foreach (var hbpreset in category.ChildrenArray)
{
Preset preset = JsonPresetFactory.ImportPreset(hbpreset);
- preset.Version = VersionHelper.GetVersion();
- preset.IsBuildIn = true; // Older versions did not have this flag so explicitly make sure it is set.
+ preset.IsBuildIn = true;
preset.Category = category.PresetName;
if (preset.Name == "iPod")
@@ -432,7 +422,7 @@ namespace HandBrakeWPF.Services.Presets
/// <returns>
/// The <see cref="string"/>.
/// </returns>
- private static string RecoverFromCorruptedPresetFile(string file)
+ private string RecoverFromCorruptedPresetFile(string file)
{
try
{
@@ -458,6 +448,32 @@ namespace HandBrakeWPF.Services.Presets
}
/// <summary>
+ /// Archive the presets file without deleting it.
+ /// </summary>
+ /// <param name="file">The filename to archive</param>
+ /// <returns>The archived filename</returns>
+ private string ArchivePresetFile(string file)
+ {
+ try
+ {
+ // Recover from Error.
+ string archiveFile = string.Format("{0}.{1}", file, GeneralUtilities.ProcessId);
+ if (File.Exists(file))
+ {
+ File.Copy(file, archiveFile);
+ }
+
+ return archiveFile;
+ }
+ catch (IOException)
+ {
+ // Give up
+ }
+
+ return "Sorry, the archiving failed.";
+ }
+
+ /// <summary>
/// Load in the Built-in and User presets into the collection
/// </summary>
private void LoadPresets()
@@ -476,18 +492,34 @@ namespace HandBrakeWPF.Services.Presets
}
// Otherwise, we already have a file, so lets try load it.
+ bool versionCheckChange = false;
using (StreamReader reader = new StreamReader(this.presetFile))
{
// New Preset Format.
- PresetTransportContainer container = JsonConvert.DeserializeObject<PresetTransportContainer>(reader.ReadToEnd());
+ PresetTransportContainer container = null;
+ try
+ {
+ container = JsonConvert.DeserializeObject<PresetTransportContainer>(reader.ReadToEnd());
+ }
+ catch (Exception)
+ {
+ // ignored
+ }
// Sanity Check. Did the container deserialise.
- if (container == null)
+ if (container?.PresetList == null)
{
// Close and Dispose of early.
reader.Close();
reader.Dispose();
- RecoverFromCorruptedPresetFile(this.presetFile);
+
+ string filename = this.RecoverFromCorruptedPresetFile(this.presetFile);
+ this.errorService.ShowMessageBox(
+ Resources.PresetService_UnableToLoadPresets + filename,
+ Resources.PresetService_UnableToLoad,
+ MessageBoxButton.OK,
+ MessageBoxImage.Exclamation);
+
this.UpdateBuiltInPresets();
return;
}
@@ -497,18 +529,16 @@ namespace HandBrakeWPF.Services.Presets
// incase something goes wrong.
if (container.VersionMajor != Constants.PresetVersionMajor || container.VersionMinor != Constants.PresetVersionMinor || container.VersionMicro != Constants.PresetVersionMicro)
{
- string fileName = RecoverFromCorruptedPresetFile(this.presetFile);
- this.UpdateBuiltInPresets();
+ string fileName = this.ArchivePresetFile(this.presetFile);
this.errorService.ShowMessageBox(
Resources.PresetService_PresetsOutOfDate
+ Environment.NewLine + Environment.NewLine + Resources.PresetService_ArchiveFile + fileName,
Resources.PresetService_UnableToLoad,
MessageBoxButton.OK,
MessageBoxImage.Exclamation);
- return;
+ versionCheckChange = true;
}
-
// Process the presets.
foreach (var item in container.PresetList)
{
@@ -555,11 +585,17 @@ namespace HandBrakeWPF.Services.Presets
}
}
}
+
+ // Resave the presets if we failed the version check to update the container
+ if (versionCheckChange)
+ {
+ this.SavePresetFiles();
+ }
}
catch (Exception ex)
{
Debug.WriteLine(ex);
- RecoverFromCorruptedPresetFile(this.presetFile);
+ this.RecoverFromCorruptedPresetFile(this.presetFile);
this.UpdateBuiltInPresets();
}
}
@@ -573,7 +609,7 @@ namespace HandBrakeWPF.Services.Presets
{
// Verify Directories.
string directory = Path.GetDirectoryName(this.presetFile);
- if (!Directory.Exists(directory))
+ if (directory != null && !Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}