diff options
author | Scott <[email protected]> | 2015-10-16 22:29:31 +0100 |
---|---|---|
committer | Scott <[email protected]> | 2015-10-16 22:30:21 +0100 |
commit | 6ae82c27e75f1eb4bd3c16c07e5b2a2cd120e82c (patch) | |
tree | 04b2b3a81d2c838b9fd5d56568eed275dc3c27b5 | |
parent | 5eb711545f4b3fbda67f199f7dd68db920381be8 (diff) |
Completely re-factored the preset file storage to use the same format as the Mac and Linux GUI's. You can now copy the file between platforms and it should load correctly.
WARNING: Any current presets will be lost when you install this or any later build as the format is not backwards compatible and there is no upgrade path.
7 files changed, 224 insertions, 248 deletions
diff --git a/win/CS/HandBrake.ApplicationServices/Interop/Json/Presets/PresetTransportContainer.cs b/win/CS/HandBrake.ApplicationServices/Interop/Json/Presets/PresetTransportContainer.cs index 8f1dd9e78..8eb2b4cfc 100644 --- a/win/CS/HandBrake.ApplicationServices/Interop/Json/Presets/PresetTransportContainer.cs +++ b/win/CS/HandBrake.ApplicationServices/Interop/Json/Presets/PresetTransportContainer.cs @@ -18,10 +18,21 @@ namespace HandBrake.ApplicationServices.Interop.Json.Presets /// </summary>
public class PresetTransportContainer
{
+ public PresetTransportContainer()
+ {
+ }
+
+ public PresetTransportContainer(string versionMajor, string versionMinor, string versionMicro)
+ {
+ this.VersionMajor = versionMajor;
+ this.VersionMicro = versionMicro;
+ this.VersionMinor = versionMinor;
+ }
+
/// <summary>
/// Gets or sets the children array.
/// </summary>
- public List<HBPreset> PresetList { get; set; }
+ public List<object> PresetList { get; set; }
/// <summary>
/// Gets or sets the version major.
diff --git a/win/CS/HandBrakeWPF/Constants.cs b/win/CS/HandBrakeWPF/Constants.cs index 735691dc6..1c985ecdb 100644 --- a/win/CS/HandBrakeWPF/Constants.cs +++ b/win/CS/HandBrakeWPF/Constants.cs @@ -69,9 +69,19 @@ namespace HandBrakeWPF /// </summary>
public const string Bitrate = "{bitrate}";
+ /// <summary>
+ /// Preset Major Version
+ /// </summary>
+ public const string PresetVersionMajor = "11";
- public const string PresetVersionMajor = "0";
- public const string PresetVersionMinor = "10";
- public const string PresetVersionMicro = "2";
+ /// <summary>
+ /// Preset Minor Version
+ /// </summary>
+ public const string PresetVersionMinor = "0";
+
+ /// <summary>
+ /// Preset Micro Version
+ /// </summary>
+ public const string PresetVersionMicro = "0";
}
}
diff --git a/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs b/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs index 3c4a3275b..6cc8e0f60 100644 --- a/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs +++ b/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs @@ -1122,6 +1122,33 @@ namespace HandBrakeWPF.Properties { }
/// <summary>
+ /// Looks up a localized string similar to Archived File:.
+ /// </summary>
+ public static string PresetService_ArchiveFile {
+ get {
+ return ResourceManager.GetString("PresetService_ArchiveFile", resourceCulture);
+ }
+ }
+
+ /// <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't get loaded on next launch..
+ /// </summary>
+ public static string PresetService_PresetsOutOfDate {
+ get {
+ return ResourceManager.GetString("PresetService_PresetsOutOfDate", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Unable to load user presets..
+ /// </summary>
+ public static string PresetService_UnableToLoad {
+ get {
+ return ResourceManager.GetString("PresetService_UnableToLoad", 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 faf06438a..26770ab9e 100644 --- a/win/CS/HandBrakeWPF/Properties/Resources.resx +++ b/win/CS/HandBrakeWPF/Properties/Resources.resx @@ -714,4 +714,13 @@ Please make sure VLC is installed and the directory specified in HandBrake's opt <data name="SubtitlesViewModel_ConfigureDefaults" xml:space="preserve">
<value>Configure Defaults</value>
</data>
+ <data name="PresetService_ArchiveFile" xml:space="preserve">
+ <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>
+ </data>
+ <data name="PresetService_UnableToLoad" xml:space="preserve">
+ <value>Unable to load user presets.</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 4227c189b..2a5a8d4aa 100644 --- a/win/CS/HandBrakeWPF/Services/Presets/Factories/JsonPresetFactory.cs +++ b/win/CS/HandBrakeWPF/Services/Presets/Factories/JsonPresetFactory.cs @@ -58,6 +58,8 @@ namespace HandBrakeWPF.Services.Presets.Factories preset.Name = importedPreset.PresetName;
preset.Description = importedPreset.PresetDescription;
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.
@@ -391,7 +393,6 @@ namespace HandBrakeWPF.Services.Presets.Factories // public int PictureForceWidth { get; set; }
// public bool AudioSecondaryEncoderMode { get; set; }
// public List<object> ChildrenArray { get; set; }
- // public bool Default { get; set; }
// public bool Folder { get; set; }
// public bool FolderOpen { get; set; }
// public int PictureDARWidth { get; set; }
@@ -419,7 +420,7 @@ namespace HandBrakeWPF.Services.Presets.Factories container.VersionMinor = Constants.PresetVersionMinor;
container.VersionMicro = Constants.PresetVersionMicro;
- container.PresetList = new List<HBPreset> { CreateHbPreset(export, config) };
+ container.PresetList = new List<object> { CreateHbPreset(export, config) };
return container;
}
@@ -439,7 +440,8 @@ namespace HandBrakeWPF.Services.Presets.Factories List<HBPreset> presets = exportList.Select(item => CreateHbPreset(item, config)).ToList();
- container.PresetList = presets;
+ container.PresetList = new List<object>();
+ container.PresetList.AddRange(presets);
return container;
}
@@ -461,9 +463,9 @@ namespace HandBrakeWPF.Services.Presets.Factories // Preset
preset.PresetDescription = export.Description;
preset.PresetName = export.Name;
- preset.Type = 1; // User Preset
+ preset.Type = export.IsBuildIn ? 0 : 1;
preset.UsesPictureSettings = (int)export.PictureSettingsMode;
- preset.Default = false; // TODO Can other GUI's handle this?
+ preset.Default = export.IsDefault;
// Audio
preset.AudioCopyMask = export.Task.AllowedPassthruOptions.AllowedPassthruOptions.Select(EnumHelper<AudioEncoder>.GetShortName).ToList();
diff --git a/win/CS/HandBrakeWPF/Services/Presets/Interfaces/IPresetService.cs b/win/CS/HandBrakeWPF/Services/Presets/Interfaces/IPresetService.cs index bfc21e347..88fd341b0 100644 --- a/win/CS/HandBrakeWPF/Services/Presets/Interfaces/IPresetService.cs +++ b/win/CS/HandBrakeWPF/Services/Presets/Interfaces/IPresetService.cs @@ -139,13 +139,6 @@ namespace HandBrakeWPF.Services.Presets.Interfaces void UpdateBuiltInPresets();
/// <summary>
- /// Check if the built in Presets stored are not out of date.
- /// Update them if they are.
- /// </summary>
- /// <returns>true if out of date</returns>
- bool CheckIfPresetsAreOutOfDate();
-
- /// <summary>
/// Check if the preset "name" exists in either Presets or UserPresets lists.
/// </summary>
/// <param name="name">Name of the preset</param>
diff --git a/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs b/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs index 763ea8fd1..44e237462 100644 --- a/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs +++ b/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs @@ -16,7 +16,6 @@ namespace HandBrakeWPF.Services.Presets using System.IO;
using System.Linq;
using System.Windows;
- using System.Xml.Serialization;
using HandBrake.ApplicationServices.Interop;
using HandBrake.ApplicationServices.Interop.Json.Presets;
@@ -44,36 +43,9 @@ namespace HandBrakeWPF.Services.Presets {
#region Private Variables
- private static readonly int CurrentPresetVersion = 5;
-
- /// <summary>
- /// User Preset Default Catgory Name
- /// </summary>
public static string UserPresetCatgoryName = "User Presets";
-
- /// <summary>
- /// The User Preset file
- /// </summary>
- 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
- /// </summary>
- private readonly string builtInPresetFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\presets.json";
-
- /// <summary>
- /// A Collection of presets
- /// </summary>
+ private readonly string presetFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\presets.json";
private readonly ObservableCollection<Preset> presets = new ObservableCollection<Preset>();
-
- /// <summary>
- /// The Error Service.
- /// </summary>
private readonly IErrorService errorService;
#endregion
@@ -124,20 +96,13 @@ namespace HandBrakeWPF.Services.Presets public void Load()
{
// If the preset file doesn't exist. Create it.
- if (!File.Exists(this.builtInPresetFile))
+ if (!File.Exists(this.presetFile))
{
this.UpdateBuiltInPresets();
}
// Load the presets from file
this.LoadPresets();
-
- // Check they are up-to-date.
- if (this.CheckIfPresetsAreOutOfDate())
- {
- this.UpdateBuiltInPresets();
- this.LoadPresets(); // Reload again.
- }
}
/// <summary>
@@ -159,7 +124,7 @@ namespace HandBrakeWPF.Services.Presets this.LastPresetAdded = preset;
// Update the presets file
- this.UpdatePresetFiles();
+ this.SavePresetFiles();
return true;
}
@@ -186,52 +151,60 @@ namespace HandBrakeWPF.Services.Presets return;
}
- HBPreset hbPreset = container.PresetList.FirstOrDefault();
-
- Preset preset = null;
- try
+ // HBPreset Handling
+ IList<HBPreset> hbPresets = container.PresetList as IList<HBPreset>;
+ if (hbPresets != null)
{
- preset = JsonPresetFactory.ImportPreset(hbPreset);
- preset.Category = UserPresetCatgoryName;
-
- // IF we are using Source Max, Set the Max Width / Height values.
- if (preset.PictureSettingsMode == PresetPictureSettingsMode.SourceMaximum)
+ foreach (var hbPreset in hbPresets)
{
- preset.Task.MaxWidth = preset.Task.Height;
- preset.Task.MaxHeight = preset.Task.Width;
- }
- }
- catch (Exception exc)
- {
- this.errorService.ShowError(Resources.Main_PresetImportFailed, Resources.Main_PresetImportFailedSolution, exc);
- }
+ Preset preset = null;
+ try
+ {
+ preset = JsonPresetFactory.ImportPreset(hbPreset);
+ preset.Category = UserPresetCatgoryName;
- if (preset == null)
- {
- this.errorService.ShowError(Resources.Main_PresetImportFailed, Resources.Main_PresetImportFailedSolution, string.Empty);
- return;
- }
+ // IF we are using Source Max, Set the Max Width / Height values.
+ if (preset.PictureSettingsMode == PresetPictureSettingsMode.SourceMaximum)
+ {
+ preset.Task.MaxWidth = preset.Task.Height;
+ preset.Task.MaxHeight = preset.Task.Width;
+ }
+ }
+ catch (Exception exc)
+ {
+ this.errorService.ShowError(Resources.Main_PresetImportFailed, Resources.Main_PresetImportFailedSolution, exc);
+ }
- // TODO Better version checking.
-
- if (this.CheckIfPresetExists(preset.Name))
- {
- if (!CanUpdatePreset(preset.Name))
- {
- MessageBox.Show(Resources.Main_PresetErrorBuiltInName, Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error);
- return;
- }
+ if (preset == null)
+ {
+ this.errorService.ShowError(Resources.Main_PresetImportFailed, Resources.Main_PresetImportFailedSolution, string.Empty);
+ return;
+ }
- MessageBoxResult result = MessageBox.Show(Resources.Main_PresetOverwriteWarning, Resources.Overwrite, MessageBoxButton.YesNo, MessageBoxImage.Warning);
- if (result == MessageBoxResult.Yes)
- {
- Update(preset);
+ // TODO Better version checking.
+
+ if (this.CheckIfPresetExists(preset.Name))
+ {
+ if (!CanUpdatePreset(preset.Name))
+ {
+ MessageBox.Show(Resources.Main_PresetErrorBuiltInName, Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error);
+ return;
+ }
+
+ MessageBoxResult result = MessageBox.Show(Resources.Main_PresetOverwriteWarning, Resources.Overwrite, MessageBoxButton.YesNo, MessageBoxImage.Warning);
+ if (result == MessageBoxResult.Yes)
+ {
+ Update(preset);
+ }
+ }
+ else
+ {
+ Add(preset);
+ }
}
}
- else
- {
- Add(preset);
- }
+
+ // Category Handling.
}
}
@@ -249,6 +222,7 @@ namespace HandBrakeWPF.Services.Presets /// </param>
public void Export(string filename, Preset preset, HBConfiguration configuration)
{
+ // TODO Add support for multiple export
PresetTransportContainer container = JsonPresetFactory.ExportPreset(preset, configuration);
HandBrakePresetService.ExportPreset(filename, container);
}
@@ -274,7 +248,7 @@ namespace HandBrakeWPF.Services.Presets preset.SubtitleTrackBehaviours = update.SubtitleTrackBehaviours;
// Update the presets file
- this.UpdatePresetFiles();
+ this.SavePresetFiles();
break;
}
}
@@ -294,7 +268,7 @@ namespace HandBrakeWPF.Services.Presets }
this.presets.Remove(preset);
- this.UpdatePresetFiles();
+ this.SavePresetFiles();
}
/// <summary>
@@ -317,7 +291,7 @@ namespace HandBrakeWPF.Services.Presets this.presets.Remove(preset);
}
- this.UpdatePresetFiles();
+ this.SavePresetFiles();
}
/// <summary>
@@ -334,7 +308,7 @@ namespace HandBrakeWPF.Services.Presets }
name.IsDefault = true;
- this.UpdatePresetFiles();
+ this.SavePresetFiles();
}
/// <summary>
@@ -381,14 +355,14 @@ namespace HandBrakeWPF.Services.Presets IList<PresetCategory> presetCategories = HandBrakePresetService.GetBuiltInPresets();
- foreach (var item in presetCategories)
+ foreach (var category in presetCategories)
{
- foreach (var hbpreset in item.ChildrenArray)
+ 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.Category = item.PresetName;
+ preset.Category = category.PresetName;
if (preset.Name == "iPod")
{
@@ -413,31 +387,7 @@ namespace HandBrakeWPF.Services.Presets }
// Store the changes to disk
- this.UpdatePresetFiles();
- }
-
- /// <summary>
- /// Check if the built in Presets stored are not out of date.
- /// Update them if they are.
- /// </summary>
- /// <returns>true if out of date</returns>
- public bool CheckIfPresetsAreOutOfDate()
- {
- // Update built-in Presets if the built-in Presets belong to an older version.
- if (this.presets.Count != 0)
- {
- List<Preset> preset = this.presets.Where(p => p.IsBuildIn).ToList();
- if (preset.Count > 0)
- {
- if (preset[0].Version != VersionHelper.GetVersion())
- {
- this.UpdateBuiltInPresets();
- return true;
- }
- }
- }
-
- return false;
+ this.SavePresetFiles();
}
/// <summary>
@@ -515,23 +465,64 @@ namespace HandBrakeWPF.Services.Presets // First clear the Presets arraylists
this.presets.Clear();
- // Load in the Presets from Presets.xml
+ // Load the presets file.
try
{
- if (File.Exists(this.builtInPresetFile))
+ // If we don't have a presets file. Create one for first load.
+ if (!File.Exists(this.presetFile))
+ {
+ this.UpdateBuiltInPresets();
+ return;
+ }
+
+ // Otherwise, we already have a file, so lets try load it.
+ using (StreamReader reader = new StreamReader(this.presetFile))
{
- using (StreamReader reader = new StreamReader(this.builtInPresetFile))
+ // New Preset Format.
+ PresetTransportContainer container = JsonConvert.DeserializeObject<PresetTransportContainer>(reader.ReadToEnd());
+
+ // Sanity Check. Did the container deserialise.
+ if (container == null)
+ {
+ // Close and Dispose of early.
+ reader.Close();
+ reader.Dispose();
+ RecoverFromCorruptedPresetFile(this.presetFile);
+ this.UpdateBuiltInPresets();
+ return;
+ }
+
+ // Version Check
+ // 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 (container.VersionMajor != Constants.PresetVersionMajor || container.VersionMinor != Constants.PresetVersionMinor || container.VersionMicro != Constants.PresetVersionMicro)
{
- // New Preset Format.
- IList<PresetCategory> presetCategories = JsonConvert.DeserializeObject<IList<PresetCategory>>(reader.ReadToEnd());
+ string fileName = RecoverFromCorruptedPresetFile(this.presetFile);
+ this.UpdateBuiltInPresets();
+ this.errorService.ShowMessageBox(
+ Resources.PresetService_PresetsOutOfDate
+ + Environment.NewLine + Environment.NewLine + Resources.PresetService_ArchiveFile + fileName,
+ Resources.PresetService_UnableToLoad,
+ MessageBoxButton.OK,
+ MessageBoxImage.Exclamation);
+ return;
+ }
- foreach (var item in presetCategories)
+
+ // Process the presets.
+ foreach (var item in container.PresetList)
+ {
+ object deserialisedItem = JsonConvert.DeserializeObject<PresetCategory>(item.ToString()); ;
+
+ // Handle Categorised Presets.
+ PresetCategory category = deserialisedItem as PresetCategory;
+ if (category != null && category.Folder)
{
- foreach (var hbpreset in item.ChildrenArray)
+ foreach (HBPreset hbpreset in category.ChildrenArray)
{
Preset preset = JsonPresetFactory.ImportPreset(hbpreset);
- preset.Category = item.PresetName;
- preset.IsBuildIn = true;
+ preset.Category = category.PresetName;
+ preset.IsBuildIn = hbpreset.Type == 0;
// IF we are using Source Max, Set the Max Width / Height values.
if (preset.PictureSettingsMode == PresetPictureSettingsMode.SourceMaximum)
@@ -543,88 +534,17 @@ namespace HandBrakeWPF.Services.Presets this.presets.Add(preset);
}
}
- }
- }
- }
- catch (Exception)
- {
- RecoverFromCorruptedPresetFile(this.builtInPresetFile);
- this.UpdateBuiltInPresets();
- }
- // 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
- {
- XmlSerializer Ser = new XmlSerializer(typeof(List<Preset>));
- var oldPresets = (List<Preset>)Ser.Deserialize(reader);
- foreach (Preset oldPreset in oldPresets)
- {
- this.presets.Add(oldPreset);
- }
- updatePresets = true;
- }
- catch (Exception exc)
+ // Uncategorised Presets
+ deserialisedItem = JsonConvert.DeserializeObject<HBPreset>(item.ToString());
+ HBPreset hbPreset = deserialisedItem as HBPreset;
+ if (hbPreset != null && !hbPreset.Folder)
{
- // Do Nothing
- Debug.WriteLine(exc);
- }
- }
+ Preset preset = JsonPresetFactory.ImportPreset(hbPreset);
+ preset.Category = UserPresetCatgoryName;
+ preset.IsBuildIn = hbPreset.Type == 1;
- // 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.
- bool createBackup = false;
- PresetTransportContainer presetContainer = null;
- using (StreamReader reader = new StreamReader(this.userPresetFile))
- {
- try
- {
- presetContainer = JsonConvert.DeserializeObject<PresetTransportContainer>(reader.ReadToEnd());
- }
- catch (Exception exc)
- {
- createBackup = true;
- Debug.WriteLine(exc);
- }
- }
-
- // 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.VersionMajor != Constants.PresetVersionMajor || presetContainer.VersionMinor != Constants.PresetVersionMinor || presetContainer.VersionMicro != Constants.PresetVersionMicro))
- {
- 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.PresetList != null)
- {
- foreach (var item in presetContainer.PresetList)
- {
- Preset preset = JsonPresetFactory.ImportPreset(item);
- preset.Category = item.PresetName;
- preset.IsBuildIn = true;
-
- // If we are using Source Max, Set the Max Width / Height values.
+ // IF we are using Source Max, Set the Max Width / Height values.
if (preset.PictureSettingsMode == PresetPictureSettingsMode.SourceMaximum)
{
preset.Task.MaxWidth = preset.Task.Height;
@@ -635,68 +555,71 @@ namespace HandBrakeWPF.Services.Presets }
}
}
-
- // We did a preset convertion, so save the updates.
- if (updatePresets)
- {
- this.UpdatePresetFiles();
- }
}
- catch (Exception exc)
+ catch (Exception ex)
{
- RecoverFromCorruptedPresetFile(this.userPresetFile);
- throw new GeneralApplicationException("HandBrake has detected a problem with your presets.", "Your old presets file has been renamed so that it doesn't get loaded on next launch.", exc);
+ Debug.WriteLine(ex);
+ RecoverFromCorruptedPresetFile(this.presetFile);
+ this.UpdateBuiltInPresets();
}
}
/// <summary>
/// Update the preset files
/// </summary>
- private void UpdatePresetFiles()
+ private void SavePresetFiles()
{
try
{
- // Setup
- string directory = Path.GetDirectoryName(this.userPresetFile);
+ // Verify Directories.
+ string directory = Path.GetDirectoryName(this.presetFile);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
- JsonSerializerSettings settings = new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Ignore };
-
- // Built-in Presets
+ // Orgamise the Presets list into Json Equivilent objects.
Dictionary<string, PresetCategory> presetCategories = new Dictionary<string, PresetCategory>();
- foreach (var item in this.presets.Where(p => p.IsBuildIn).ToList())
+ List<HBPreset> uncategorisedPresets = new List<HBPreset>();
+ foreach (Preset item in this.presets)
{
- HBPreset preset = JsonPresetFactory.CreateHbPreset(item, HBConfigurationFactory.Create());
- if (presetCategories.ContainsKey(item.Category))
+ if (string.IsNullOrEmpty(item.Category))
{
- presetCategories[item.Category].ChildrenArray.Add(preset);
+ uncategorisedPresets.Add(JsonPresetFactory.CreateHbPreset(item, HBConfigurationFactory.Create()));
}
else
{
- presetCategories[item.Category] = new PresetCategory { ChildrenArray = new List<HBPreset>(), Folder = true, PresetName = item.Category, Type = 0 };
- }
- }
-
- using (FileStream strm = new FileStream(this.builtInPresetFile, FileMode.Create, FileAccess.Write))
- {
- string presetsJson = JsonConvert.SerializeObject(presetCategories, Formatting.Indented, settings);
- using (StreamWriter writer = new StreamWriter(strm))
- {
- writer.WriteLine(presetsJson);
+ HBPreset preset = JsonPresetFactory.CreateHbPreset(item, HBConfigurationFactory.Create());
+ if (presetCategories.ContainsKey(item.Category))
+ {
+ presetCategories[item.Category].ChildrenArray.Add(preset);
+ }
+ else
+ {
+ presetCategories[item.Category] = new PresetCategory
+ {
+ ChildrenArray = new List<HBPreset> { preset },
+ Folder = true,
+ PresetName = item.Category,
+ Type = item.IsBuildIn ? 0 : 1
+ };
+ }
}
}
- // User Presets
- using (FileStream strm = new FileStream(this.userPresetFile, FileMode.Create, FileAccess.Write))
+ // Wrap the categories in a container.
+ JsonSerializerSettings settings = new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Ignore };
+ PresetTransportContainer container = new PresetTransportContainer(
+ Constants.PresetVersionMajor,
+ Constants.PresetVersionMinor,
+ Constants.PresetVersionMicro) { PresetList = new List<object>() };
+ container.PresetList.AddRange(presetCategories.Values);
+ container.PresetList.AddRange(uncategorisedPresets);
+
+ // Write the preset container out to file.
+ using (FileStream strm = new FileStream(this.presetFile, FileMode.Create, FileAccess.Write))
{
- List<Preset> userPresets = this.presets.Where(p => p.IsBuildIn == false).ToList();
- PresetTransportContainer container = JsonPresetFactory.ExportPresets(userPresets, HBConfigurationFactory.Create());
-
string presetsJson = JsonConvert.SerializeObject(container, Formatting.Indented, settings);
-
using (StreamWriter writer = new StreamWriter(strm))
{
writer.WriteLine(presetsJson);
@@ -705,6 +628,7 @@ namespace HandBrakeWPF.Services.Presets }
catch (Exception exc)
{
+ Debug.WriteLine(exc);
throw new GeneralApplicationException("Unable to write to the presets file.", "The details section below may indicate why this error has occured.", exc);
}
}
|