]> granicus.if.org Git - handbrake/commitdiff
Refactoring the Preset service to store the Built-In Presets and User Presets in...
authorScott <sr55.hb@outlook.com>
Sun, 11 Oct 2015 20:56:02 +0000 (21:56 +0100)
committerScott <sr55.hb@outlook.com>
Fri, 16 Oct 2015 21:30:20 +0000 (22:30 +0100)
win/CS/HandBrakeWPF/Constants.cs
win/CS/HandBrakeWPF/HandBrakeWPF.csproj
win/CS/HandBrakeWPF/Services/Presets/Factories/JsonPresetFactory.cs
win/CS/HandBrakeWPF/Services/Presets/Model/PresetContainer.cs [deleted file]
win/CS/HandBrakeWPF/Services/Presets/PresetService.cs

index ee171b207080cdac0d8abc52b35ce5bd3c9dde44..735691dc6b1319981824c1336f9b456b7331edf0 100644 (file)
@@ -68,5 +68,10 @@ namespace HandBrakeWPF
         /// The bitrate.\r
         /// </summary>\r
         public const string Bitrate = "{bitrate}";\r
+\r
+\r
+        public const string PresetVersionMajor = "0";\r
+        public const string PresetVersionMinor = "10";\r
+        public const string PresetVersionMicro = "2";\r
     }\r
 }\r
index fa2b4bf5ac2f8562665422bd91c3790c0705f87a..91d292c2064586debc119194c2d5c94ebcd7e64b 100644 (file)
     <Compile Include="Factories\HBConfigurationFactory.cs" />\r
     <Compile Include="Services\Presets\Interfaces\IPresetService.cs" />\r
     <Compile Include="Services\Interfaces\IUserSettingService.cs" />\r
-    <Compile Include="Services\Presets\Model\PresetContainer.cs" />\r
     <Compile Include="Services\Presets\PresetService.cs" />\r
     <Compile Include="Services\Queue\Model\QueueTaskContainer.cs" />\r
     <Compile Include="Services\Queue\QueueProcessor.cs" />\r
index bf6e6560e241d3394f1f2c974073b5c4107f96c1..4227c189b157ca434f6acc049c12990b17409a7b 100644 (file)
@@ -415,14 +415,35 @@ namespace HandBrakeWPF.Services.Presets.Factories
         public static PresetTransportContainer ExportPreset(Preset export, HBConfiguration config)\r
         {\r
             PresetTransportContainer container = new PresetTransportContainer();\r
-            container.VersionMajor = "0";\r
-            container.VersionMinor = "10";\r
-            container.VersionMicro = "2";\r
+            container.VersionMajor = Constants.PresetVersionMajor;\r
+            container.VersionMinor = Constants.PresetVersionMinor;\r
+            container.VersionMicro = Constants.PresetVersionMicro;\r
+\r
             container.PresetList = new List<HBPreset> { CreateHbPreset(export, config) };\r
 \r
             return container;\r
         }\r
 \r
+        /// <summary>\r
+        /// Export a list of Presets.\r
+        /// </summary>\r
+        /// <param name="exportList">A list of presets to export</param>\r
+        /// <param name="config">HB's configuration</param>\r
+        /// <returns>A list of JSON object presets.</returns>\r
+        public static PresetTransportContainer ExportPresets(IEnumerable<Preset> exportList, HBConfiguration config)\r
+        {\r
+            PresetTransportContainer container = new PresetTransportContainer();\r
+            container.VersionMajor = Constants.PresetVersionMajor;\r
+            container.VersionMinor = Constants.PresetVersionMinor;\r
+            container.VersionMicro = Constants.PresetVersionMicro;\r
+\r
+            List<HBPreset> presets = exportList.Select(item => CreateHbPreset(item, config)).ToList();\r
+\r
+            container.PresetList = presets;\r
+\r
+            return container;\r
+        }\r
+\r
         /// <summary>\r
         /// The create hb preset.\r
         /// </summary>\r
@@ -433,7 +454,7 @@ namespace HandBrakeWPF.Services.Presets.Factories
         /// <returns>\r
         /// The <see cref="HBPreset"/>.\r
         /// </returns>\r
-        private static HBPreset CreateHbPreset(Preset export, HBConfiguration config)\r
+        public static HBPreset CreateHbPreset(Preset export, HBConfiguration config)\r
         {\r
             HBPreset preset = new HBPreset();\r
 \r
diff --git a/win/CS/HandBrakeWPF/Services/Presets/Model/PresetContainer.cs b/win/CS/HandBrakeWPF/Services/Presets/Model/PresetContainer.cs
deleted file mode 100644 (file)
index f84f88b..0000000
+++ /dev/null
@@ -1,42 +0,0 @@
-// --------------------------------------------------------------------------------------------------------------------\r
-// <copyright file="PresetContainer.cs" company="HandBrake Project (http://handbrake.fr)">\r
-//   This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.\r
-// </copyright>\r
-// <summary>\r
-//   A container object for presets. This object should not change often as it's designed for preset version tracking.\r
-// </summary>\r
-// --------------------------------------------------------------------------------------------------------------------\r
-\r
-namespace HandBrakeWPF.Services.Presets.Model\r
-{\r
-    /// <summary>\r
-    /// The preset container.\r
-    /// </summary>\r
-    public class PresetContainer\r
-    {\r
-        /// <summary>\r
-        /// Initializes a new instance of the <see cref="PresetContainer"/> class.\r
-        /// </summary>\r
-        /// <param name="version">\r
-        /// The version.\r
-        /// </param>\r
-        /// <param name="presets">\r
-        /// The presets.\r
-        /// </param>\r
-        public PresetContainer(int version, string presets)\r
-        {\r
-            Version = version;\r
-            Presets = presets;\r
-        }\r
-\r
-        /// <summary>\r
-        /// Gets or sets the version of the presets stored in this container.\r
-        /// </summary>\r
-        public int Version { get; set; }\r
-\r
-        /// <summary>\r
-        /// Gets or sets the presets. This is a serialised string.\r
-        /// </summary>\r
-        public string Presets { get; set;  }\r
-    }\r
-}\r
index 1174e2f8224abac4ff574e91fc6b86a1c3248c10..5707bac27d99da7bbcf9a2200068960dcf7bafe2 100644 (file)
@@ -23,6 +23,7 @@ namespace HandBrakeWPF.Services.Presets
     using HandBrake.ApplicationServices.Model;\r
     using HandBrake.ApplicationServices.Utilities;\r
 \r
+    using HandBrakeWPF.Factories;\r
     using HandBrakeWPF.Model.Picture;\r
     using HandBrakeWPF.Properties;\r
     using HandBrakeWPF.Services.Encode.Model.Models;\r
@@ -522,21 +523,26 @@ namespace HandBrakeWPF.Services.Presets
                     using (StreamReader reader = new StreamReader(this.builtInPresetFile))\r
                     {\r
                         // New Preset Format.\r
-                        try\r
-                        {\r
-                            var presetList = JsonConvert.DeserializeObject<List<Preset>>(reader.ReadToEnd());\r
+                        IList<PresetCategory> presetCategories = JsonConvert.DeserializeObject<IList<PresetCategory>>(reader.ReadToEnd());\r
 \r
-                            foreach (Preset preset in presetList)\r
+                        foreach (var item in presetCategories)\r
+                        {\r
+                            foreach (var hbpreset in item.ChildrenArray)\r
                             {\r
-                                preset.IsBuildIn = true;  // Older versions did not have this flag so explicitly make sure it is set.\r
+                                Preset preset = JsonPresetFactory.ImportPreset(hbpreset);\r
+                                preset.Category = item.PresetName;\r
+                                preset.IsBuildIn = true;\r
+\r
+                                // IF we are using Source Max, Set the Max Width / Height values.\r
+                                if (preset.PictureSettingsMode == PresetPictureSettingsMode.SourceMaximum)\r
+                                {\r
+                                    preset.Task.MaxWidth = preset.Task.Height;\r
+                                    preset.Task.MaxHeight = preset.Task.Width;\r
+                                }\r
+\r
                                 this.presets.Add(preset);\r
                             }\r
                         }\r
-                        catch (Exception exc)\r
-                        {\r
-                            // Do Nothing.\r
-                            Debug.WriteLine(exc);\r
-                        }\r
                     }\r
                 }\r
             }\r
@@ -581,12 +587,12 @@ namespace HandBrakeWPF.Services.Presets
                 {\r
                     // New Preset Format.\r
                     bool createBackup = false;\r
-                    PresetContainer presetContainer = null;\r
+                    PresetTransportContainer presetContainer = null;\r
                     using (StreamReader reader = new StreamReader(this.userPresetFile))\r
                     {\r
                         try\r
                         {\r
-                            presetContainer = JsonConvert.DeserializeObject<PresetContainer>(reader.ReadToEnd());\r
+                            presetContainer = JsonConvert.DeserializeObject<PresetTransportContainer>(reader.ReadToEnd());\r
                         }\r
                         catch (Exception exc)\r
                         {\r
@@ -597,7 +603,7 @@ namespace HandBrakeWPF.Services.Presets
 \r
                     // If we have old presets, or the container wasn't parseable, or we have a version mismatch, backup the user preset file \r
                     // incase something goes wrong.\r
-                    if (createBackup || (presetContainer != null && presetContainer.Version < CurrentPresetVersion))\r
+                    if (createBackup || (presetContainer.VersionMajor != Constants.PresetVersionMajor || presetContainer.VersionMinor != Constants.PresetVersionMinor  || presetContainer.VersionMicro != Constants.PresetVersionMicro))\r
                     {\r
                         string fileName = RecoverFromCorruptedPresetFile(this.userPresetFile);\r
                         this.errorService.ShowMessageBox(\r
@@ -610,12 +616,21 @@ namespace HandBrakeWPF.Services.Presets
                     }\r
 \r
                     // Load the current presets.\r
-                    if (presetContainer != null && !string.IsNullOrEmpty(presetContainer.Presets))\r
+                    if (presetContainer.PresetList != null)\r
                     {\r
-                        JsonSerializerSettings settings = new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Ignore };\r
-                        List<Preset> list = JsonConvert.DeserializeObject<List<Preset>>(presetContainer.Presets, settings);\r
-                        foreach (Preset preset in list)\r
+                        foreach (var item in presetContainer.PresetList)\r
                         {\r
+                            Preset preset = JsonPresetFactory.ImportPreset(item);\r
+                            preset.Category = item.PresetName;\r
+                            preset.IsBuildIn = true;\r
+\r
+                            // If we are using Source Max, Set the Max Width / Height values.\r
+                            if (preset.PictureSettingsMode == PresetPictureSettingsMode.SourceMaximum)\r
+                            {\r
+                                preset.Task.MaxWidth = preset.Task.Height;\r
+                                preset.Task.MaxHeight = preset.Task.Width;\r
+                            }\r
+\r
                             this.presets.Add(preset);\r
                         }\r
                     }\r
@@ -651,10 +666,23 @@ namespace HandBrakeWPF.Services.Presets
                 JsonSerializerSettings settings = new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Ignore };\r
 \r
                 // Built-in Presets\r
+                Dictionary<string, PresetCategory> presetCategories = new Dictionary<string, PresetCategory>();\r
+                foreach (var item in this.presets.Where(p => p.IsBuildIn).ToList())\r
+                {\r
+                    HBPreset preset = JsonPresetFactory.CreateHbPreset(item, HBConfigurationFactory.Create());\r
+                    if (presetCategories.ContainsKey(item.Category))\r
+                    {\r
+                        presetCategories[item.Category].ChildrenArray.Add(preset);\r
+                    }\r
+                    else\r
+                    {\r
+                        presetCategories[item.Category] = new PresetCategory { ChildrenArray = new List<HBPreset>(), Folder = true, PresetName = item.Category, Type = 0 };\r
+                    }                \r
+                }\r
 \r
                 using (FileStream strm = new FileStream(this.builtInPresetFile, FileMode.Create, FileAccess.Write))\r
                 {\r
-                    string presetsJson = JsonConvert.SerializeObject(this.presets.Where(p => p.IsBuildIn).ToList(), Formatting.Indented, settings);\r
+                    string presetsJson = JsonConvert.SerializeObject(presetCategories, Formatting.Indented, settings);\r
                     using (StreamWriter writer = new StreamWriter(strm))\r
                     {\r
                         writer.WriteLine(presetsJson);\r
@@ -667,12 +695,9 @@ namespace HandBrakeWPF.Services.Presets
                     List<Preset> userPresets = this.presets.Where(p => p.IsBuildIn == false).ToList();\r
                     string presetsJson = JsonConvert.SerializeObject(userPresets, Formatting.Indented, settings);\r
 \r
-                    PresetContainer container = new PresetContainer(CurrentPresetVersion, presetsJson);\r
-                    string containerJson = JsonConvert.SerializeObject(container, Formatting.Indented, settings);\r
-\r
                     using (StreamWriter writer = new StreamWriter(strm))\r
                     {\r
-                        writer.WriteLine(containerJson);\r
+                        writer.WriteLine(presetsJson);\r
                     }\r
                 }\r
             }\r