diff options
author | Scott <[email protected]> | 2015-10-11 21:56:02 +0100 |
---|---|---|
committer | Scott <[email protected]> | 2015-10-16 22:30:20 +0100 |
commit | 24ac78a88cec224379acd8533fcfaaa42a5cbd94 (patch) | |
tree | 6c332725f714bf4b4404d062172b92da6f353f71 | |
parent | fc1188fc2447c39f9abb282e101758e516ee9a07 (diff) |
Refactoring the Preset service to store the Built-In Presets and User Presets in the new version of the JSON format. This allows HandBrakeCLI to use the presets stored by the GUI.
5 files changed, 77 insertions, 69 deletions
diff --git a/win/CS/HandBrakeWPF/Constants.cs b/win/CS/HandBrakeWPF/Constants.cs index ee171b207..735691dc6 100644 --- a/win/CS/HandBrakeWPF/Constants.cs +++ b/win/CS/HandBrakeWPF/Constants.cs @@ -68,5 +68,10 @@ namespace HandBrakeWPF /// The bitrate.
/// </summary>
public const string Bitrate = "{bitrate}";
+
+
+ public const string PresetVersionMajor = "0";
+ public const string PresetVersionMinor = "10";
+ public const string PresetVersionMicro = "2";
}
}
diff --git a/win/CS/HandBrakeWPF/HandBrakeWPF.csproj b/win/CS/HandBrakeWPF/HandBrakeWPF.csproj index fa2b4bf5a..91d292c20 100644 --- a/win/CS/HandBrakeWPF/HandBrakeWPF.csproj +++ b/win/CS/HandBrakeWPF/HandBrakeWPF.csproj @@ -209,7 +209,6 @@ <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\Queue\Model\QueueTaskContainer.cs" />
<Compile Include="Services\Queue\QueueProcessor.cs" />
diff --git a/win/CS/HandBrakeWPF/Services/Presets/Factories/JsonPresetFactory.cs b/win/CS/HandBrakeWPF/Services/Presets/Factories/JsonPresetFactory.cs index bf6e6560e..4227c189b 100644 --- a/win/CS/HandBrakeWPF/Services/Presets/Factories/JsonPresetFactory.cs +++ b/win/CS/HandBrakeWPF/Services/Presets/Factories/JsonPresetFactory.cs @@ -415,15 +415,36 @@ namespace HandBrakeWPF.Services.Presets.Factories public static PresetTransportContainer ExportPreset(Preset export, HBConfiguration config)
{
PresetTransportContainer container = new PresetTransportContainer();
- container.VersionMajor = "0";
- container.VersionMinor = "10";
- container.VersionMicro = "2";
+ container.VersionMajor = Constants.PresetVersionMajor;
+ container.VersionMinor = Constants.PresetVersionMinor;
+ container.VersionMicro = Constants.PresetVersionMicro;
+
container.PresetList = new List<HBPreset> { CreateHbPreset(export, config) };
return container;
}
/// <summary>
+ /// Export a list of Presets.
+ /// </summary>
+ /// <param name="exportList">A list of presets to export</param>
+ /// <param name="config">HB's configuration</param>
+ /// <returns>A list of JSON object presets.</returns>
+ public static PresetTransportContainer ExportPresets(IEnumerable<Preset> exportList, HBConfiguration config)
+ {
+ PresetTransportContainer container = new PresetTransportContainer();
+ container.VersionMajor = Constants.PresetVersionMajor;
+ container.VersionMinor = Constants.PresetVersionMinor;
+ container.VersionMicro = Constants.PresetVersionMicro;
+
+ List<HBPreset> presets = exportList.Select(item => CreateHbPreset(item, config)).ToList();
+
+ container.PresetList = presets;
+
+ return container;
+ }
+
+ /// <summary>
/// The create hb preset.
/// </summary>
/// <param name="export">
@@ -433,7 +454,7 @@ namespace HandBrakeWPF.Services.Presets.Factories /// <returns>
/// The <see cref="HBPreset"/>.
/// </returns>
- private static HBPreset CreateHbPreset(Preset export, HBConfiguration config)
+ public static HBPreset CreateHbPreset(Preset export, HBConfiguration config)
{
HBPreset preset = new HBPreset();
diff --git a/win/CS/HandBrakeWPF/Services/Presets/Model/PresetContainer.cs b/win/CS/HandBrakeWPF/Services/Presets/Model/PresetContainer.cs deleted file mode 100644 index f84f88b67..000000000 --- a/win/CS/HandBrakeWPF/Services/Presets/Model/PresetContainer.cs +++ /dev/null @@ -1,42 +0,0 @@ -// --------------------------------------------------------------------------------------------------------------------
-// <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
-{
- /// <summary>
- /// The preset container.
- /// </summary>
- public class PresetContainer
- {
- /// <summary>
- /// Initializes a new instance of the <see cref="PresetContainer"/> class.
- /// </summary>
- /// <param name="version">
- /// The version.
- /// </param>
- /// <param name="presets">
- /// The presets.
- /// </param>
- 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 1174e2f82..5707bac27 100644 --- a/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs +++ b/win/CS/HandBrakeWPF/Services/Presets/PresetService.cs @@ -23,6 +23,7 @@ namespace HandBrakeWPF.Services.Presets using HandBrake.ApplicationServices.Model;
using HandBrake.ApplicationServices.Utilities;
+ using HandBrakeWPF.Factories;
using HandBrakeWPF.Model.Picture;
using HandBrakeWPF.Properties;
using HandBrakeWPF.Services.Encode.Model.Models;
@@ -522,21 +523,26 @@ namespace HandBrakeWPF.Services.Presets using (StreamReader reader = new StreamReader(this.builtInPresetFile))
{
// New Preset Format.
- try
- {
- var presetList = JsonConvert.DeserializeObject<List<Preset>>(reader.ReadToEnd());
+ IList<PresetCategory> presetCategories = JsonConvert.DeserializeObject<IList<PresetCategory>>(reader.ReadToEnd());
- foreach (Preset preset in presetList)
+ foreach (var item in presetCategories)
+ {
+ foreach (var hbpreset in item.ChildrenArray)
{
- preset.IsBuildIn = true; // Older versions did not have this flag so explicitly make sure it is set.
+ Preset preset = JsonPresetFactory.ImportPreset(hbpreset);
+ preset.Category = item.PresetName;
+ preset.IsBuildIn = true;
+
+ // 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;
+ }
+
this.presets.Add(preset);
}
}
- catch (Exception exc)
- {
- // Do Nothing.
- Debug.WriteLine(exc);
- }
}
}
}
@@ -581,12 +587,12 @@ namespace HandBrakeWPF.Services.Presets {
// New Preset Format.
bool createBackup = false;
- PresetContainer presetContainer = null;
+ PresetTransportContainer presetContainer = null;
using (StreamReader reader = new StreamReader(this.userPresetFile))
{
try
{
- presetContainer = JsonConvert.DeserializeObject<PresetContainer>(reader.ReadToEnd());
+ presetContainer = JsonConvert.DeserializeObject<PresetTransportContainer>(reader.ReadToEnd());
}
catch (Exception exc)
{
@@ -597,7 +603,7 @@ namespace HandBrakeWPF.Services.Presets // 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))
+ if (createBackup || (presetContainer.VersionMajor != Constants.PresetVersionMajor || presetContainer.VersionMinor != Constants.PresetVersionMinor || presetContainer.VersionMicro != Constants.PresetVersionMicro))
{
string fileName = RecoverFromCorruptedPresetFile(this.userPresetFile);
this.errorService.ShowMessageBox(
@@ -610,12 +616,21 @@ namespace HandBrakeWPF.Services.Presets }
// Load the current presets.
- if (presetContainer != null && !string.IsNullOrEmpty(presetContainer.Presets))
+ if (presetContainer.PresetList != null)
{
- JsonSerializerSettings settings = new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Ignore };
- List<Preset> list = JsonConvert.DeserializeObject<List<Preset>>(presetContainer.Presets, settings);
- foreach (Preset preset in list)
+ 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 (preset.PictureSettingsMode == PresetPictureSettingsMode.SourceMaximum)
+ {
+ preset.Task.MaxWidth = preset.Task.Height;
+ preset.Task.MaxHeight = preset.Task.Width;
+ }
+
this.presets.Add(preset);
}
}
@@ -651,10 +666,23 @@ namespace HandBrakeWPF.Services.Presets JsonSerializerSettings settings = new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Ignore };
// Built-in Presets
+ Dictionary<string, PresetCategory> presetCategories = new Dictionary<string, PresetCategory>();
+ foreach (var item in this.presets.Where(p => p.IsBuildIn).ToList())
+ {
+ 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>(), Folder = true, PresetName = item.Category, Type = 0 };
+ }
+ }
using (FileStream strm = new FileStream(this.builtInPresetFile, FileMode.Create, FileAccess.Write))
{
- string presetsJson = JsonConvert.SerializeObject(this.presets.Where(p => p.IsBuildIn).ToList(), Formatting.Indented, settings);
+ string presetsJson = JsonConvert.SerializeObject(presetCategories, Formatting.Indented, settings);
using (StreamWriter writer = new StreamWriter(strm))
{
writer.WriteLine(presetsJson);
@@ -667,12 +695,9 @@ namespace HandBrakeWPF.Services.Presets List<Preset> userPresets = this.presets.Where(p => p.IsBuildIn == false).ToList();
string presetsJson = JsonConvert.SerializeObject(userPresets, Formatting.Indented, settings);
- PresetContainer container = new PresetContainer(CurrentPresetVersion, presetsJson);
- string containerJson = JsonConvert.SerializeObject(container, Formatting.Indented, settings);
-
using (StreamWriter writer = new StreamWriter(strm))
{
- writer.WriteLine(containerJson);
+ writer.WriteLine(presetsJson);
}
}
}
|