OSDN Git Service

import original 0.9.5 release
[handbrake-jp/handbrake-jp.git] / win / C# / Presets / PresetsHandler.cs
index 1609c1f..456cdfd 100644 (file)
@@ -1,40 +1,87 @@
 /*  PresetHandler.cs $\r
-       \r
-          This file is part of the HandBrake source code.\r
-          Homepage: <http://handbrake.fr>.\r
-          It may be used under the terms of the GNU General Public License. */\r
-using System;\r
-using System.Collections.Generic;\r
-using System.Drawing;\r
-using System.Windows.Forms;\r
-using System.IO;\r
-using System.Text.RegularExpressions;\r
-using System.Diagnostics;\r
-using System.Xml.Serialization;\r
+    This file is part of the HandBrake source code.\r
+    Homepage: <http://handbrake.fr>.\r
+    It may be used under the terms of the GNU General Public License. */\r
 \r
 namespace Handbrake.Presets\r
 {\r
+    using System;\r
+    using System.Collections.Generic;\r
+    using System.Diagnostics;\r
+    using System.Drawing;\r
+    using System.IO;\r
+    using System.Linq;\r
+    using System.Text.RegularExpressions;\r
+    using System.Windows.Forms;\r
+    using System.Xml.Serialization;\r
+\r
+    /// <summary>\r
+    /// The Preset Handler Class\r
+    /// </summary>\r
     public class PresetsHandler\r
     {\r
-        List<Preset> _presets = new List<Preset>();\r
-        List<Preset> _userPresets = new List<Preset>();\r
+        /// <summary>\r
+        /// The User Preset file\r
+        /// </summary>\r
+        private readonly string userPresetFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\user_presets.xml";\r
+\r
+        /// <summary>\r
+        /// The Built In Presets File\r
+        /// </summary>\r
+        private readonly string hbPresetFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\presets.xml";\r
+\r
+        /// <summary>\r
+        /// XML Serializer\r
+        /// </summary>\r
         private static readonly XmlSerializer Ser = new XmlSerializer(typeof(List<Preset>));\r
-        readonly string _userPresetFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\user_presets.xml";\r
-        readonly string _hbPresetFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\presets.xml";\r
+\r
+        /// <summary>\r
+        /// A List of built-in presets\r
+        /// </summary>\r
+        private List<Preset> presets = new List<Preset>();\r
+\r
+        /// <summary>\r
+        /// A List of user presets\r
+        /// </summary>\r
+        private List<Preset> userPresets = new List<Preset>();\r
+\r
+        /// <summary>\r
+        ///  Gets or sets the Last preset added\r
+        /// </summary>\r
+        public Preset LastPresetAdded { get; set; }\r
 \r
         /// <summary>\r
         /// Add a new preset to the system\r
         /// </summary>\r
-        /// <param name="presetName">String, The name of the new preset</param>\r
-        /// <param name="query">String, the CLI query for the new preset</param>\r
-        /// <param name="pictureSettings"> Bool, store crop/picture sizes in the _presets</param>\r
-        public Boolean Add(string presetName, string query, Boolean pictureSettings)\r
+        /// <param name="presetName">\r
+        /// String, The name of the new preset\r
+        /// </param>\r
+        /// <param name="query">\r
+        /// String, the CLI query for the new preset\r
+        /// </param>\r
+        /// <param name="pictureSettings">\r
+        /// Bool, store crop/picture sizes in the Presets\r
+        /// </param>\r
+        /// <param name="description">\r
+        /// The description.\r
+        /// </param>\r
+        /// <returns>\r
+        /// The add.\r
+        /// </returns>\r
+        public bool Add(string presetName, string query, bool pictureSettings, string description)\r
         {\r
-            if (CheckIfPresetExists(presetName) == false)\r
+            if (this.CheckIfPresetExists(presetName) == false)\r
             {\r
-                Preset newPreset = new Preset { Name = presetName, Query = query, PictureSettings = pictureSettings, Version = Properties.Settings.Default.hb_version };\r
-                _userPresets.Add(newPreset);\r
-                UpdatePresetFiles();\r
+                Preset newPreset = new Preset\r
+                                       {\r
+                                           Name = presetName, \r
+                                           Query = query, \r
+                                           CropSettings = pictureSettings, \r
+                                           Version = Properties.Settings.Default.hb_version\r
+                                       };\r
+                this.userPresets.Add(newPreset);\r
+                this.UpdatePresetFiles();\r
+                this.LastPresetAdded = newPreset;\r
                 return true;\r
             }\r
             return false;\r
@@ -50,37 +97,36 @@ namespace Handbrake.Presets
             List<Preset> newUserPresets = new List<Preset>();\r
 \r
             // Built In Presets\r
-            foreach (Preset item in _presets)\r
+            foreach (Preset item in this.presets)\r
             {\r
                 if (item.Name != name)\r
                 {\r
                     newPresets.Add(item);\r
                 }\r
             }\r
-            _presets = newPresets;\r
+            this.presets = newPresets;\r
 \r
             // User Presets\r
-            foreach (Preset item in _userPresets)\r
+            foreach (Preset item in this.userPresets)\r
             {\r
                 if (item.Name != name)\r
                 {\r
                     newUserPresets.Add(item);\r
                 }\r
             }\r
-            _userPresets = newUserPresets;\r
+            this.userPresets = newUserPresets;\r
 \r
-            // Rebuild the _userPresets.xml file\r
-            UpdatePresetFiles();\r
-            UpdatePresetFiles();\r
+            // Rebuild the Preset XML files\r
+            this.UpdatePresetFiles();\r
         }\r
 \r
         /// <summary>\r
-        /// Remove all built in _presets;\r
+        /// Remove all built in Presets;\r
         /// </summary>\r
         public void RemoveBuiltInPresets()\r
         {\r
-            _presets.Clear();\r
-            UpdatePresetFiles();\r
+            this.presets.Clear();\r
+            this.UpdatePresetFiles();\r
         }\r
 \r
         /// <summary>\r
@@ -89,17 +135,21 @@ namespace Handbrake.Presets
         /// <param name="presetName">String, The name of the new preset</param>\r
         /// <param name="query">String, the CLI query for the new preset</param>\r
         /// <param name="pictureSettings"> Bool, store crop/picture sizes in the preset</param>\r
-        public void Update(string presetName, string query, Boolean pictureSettings)\r
+        public void Update(string presetName, string query, bool pictureSettings)\r
         {\r
             // User Presets\r
-            foreach (Preset item in _userPresets)\r
+            foreach (Preset item in this.userPresets)\r
             {\r
                 if (item.Name == presetName)\r
                 {\r
                     item.Query = query;\r
-                    item.PictureSettings = pictureSettings;\r
-                    MessageBox.Show("Changes to \"" + presetName + "\" Saved", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);\r
-                    UpdatePresetFiles();\r
+                    item.CropSettings = pictureSettings;\r
+                    MessageBox.Show(\r
+                        "Changes to \"" + presetName + "\" Saved",\r
+                                    "Success",\r
+                                    MessageBoxButtons.OK, \r
+                                    MessageBoxIcon.Information);\r
+                    this.UpdatePresetFiles();\r
                 }\r
             }\r
         }\r
@@ -112,33 +162,30 @@ namespace Handbrake.Presets
         public Preset GetPreset(string name)\r
         {\r
             // Built In Presets\r
-            foreach (Preset item in _presets)\r
+            foreach (Preset item in this.presets)\r
             {\r
                 if (item.Name == name)\r
                     return item;\r
             }\r
 \r
             // User Presets\r
-            foreach (Preset item in _userPresets)\r
-            {\r
-                if (item.Name == name)\r
-                    return item;\r
-            }\r
-\r
-            return null;\r
+            return this.userPresets.FirstOrDefault(item => item.Name == name);\r
         }\r
 \r
         /// <summary>\r
-        /// Reads the CLI's CLI output format and load's them into the preset List<Preset>\r
+        /// Reads the CLI's CLI output format and load's them into the preset List Preset\r
         /// </summary>\r
         public void UpdateBuiltInPresets()\r
         {\r
-            // Create a new tempory file and execute the CLI to get the built in _presets.\r
+            // Create a new tempory file and execute the CLI to get the built in Presets.\r
             string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");\r
             string presetsPath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");\r
             string strCmdLine = String.Format(@"cmd /c """"{0}"" --preset-list >""{1}"" 2>&1""", handbrakeCLIPath, presetsPath);\r
 \r
-            ProcessStartInfo hbGetPresets = new ProcessStartInfo("CMD.exe", strCmdLine) { WindowStyle = ProcessWindowStyle.Hidden };\r
+            ProcessStartInfo hbGetPresets = new ProcessStartInfo("CMD.exe", strCmdLine)\r
+                                                {\r
+                                                   WindowStyle = ProcessWindowStyle.Hidden\r
+                                                };\r
             Process hbproc = Process.Start(hbGetPresets);\r
             if (hbproc != null)\r
             {\r
@@ -147,8 +194,8 @@ namespace Handbrake.Presets
                 hbproc.Close();\r
             }\r
 \r
-            // Clear the current built in _presets and now parse the tempory _presets file.\r
-            _presets.Clear();\r
+            // Clear the current built in Presets and now parse the tempory Presets file.\r
+            this.presets.Clear();\r
 \r
             if (File.Exists(presetsPath))\r
             {\r
@@ -160,106 +207,190 @@ namespace Handbrake.Presets
                 {\r
                     string line = presetInput.ReadLine();\r
                     if (line.Contains("<") && !line.Contains("<<")) // Found the beginning of a preset block \r
-                        category = line.Replace("<", "").Trim();\r
+                        category = line.Replace("<", string.Empty).Trim();\r
 \r
                     if (line.Contains("+")) // A Preset\r
                     {\r
                         Regex r = new Regex("(:  )"); // Split on hyphens. \r
                         string[] presetName = r.Split(line);\r
 \r
+                        bool pic = false;\r
+                        if (presetName[2].Contains("crop"))\r
+                            pic = true;\r
+\r
                         Preset newPreset = new Preset\r
-                                               {   Category = category,\r
-                                                   Name = presetName[0].Replace("+", "").Trim(),\r
-                                                   Query = presetName[2],\r
-                                                   Version = Properties.Settings.Default.hb_version,\r
-                                                   PictureSettings = true\r
+                                               {\r
+                                                   Category = category, \r
+                                                   Name = presetName[0].Replace("+", string.Empty).Trim(), \r
+                                                   Query = presetName[2], \r
+                                                   Version = Properties.Settings.Default.hb_version, \r
+                                                   CropSettings = pic,\r
+                                                   Description = string.Empty // Maybe one day we will populate this.\r
                                                };\r
-                        _presets.Add(newPreset);\r
+                        this.presets.Add(newPreset);\r
                     }\r
                 }\r
                 presetInput.Close();\r
                 presetInput.Dispose();\r
             }\r
 \r
-            // Finally, Create a new or update the current _presets.xml file\r
-            UpdatePresetFiles();\r
+            // Finally, Create a new or update the current Presets.xml file\r
+            this.UpdatePresetFiles();\r
         }\r
 \r
         /// <summary>\r
-        /// Load in the preset data from _presets.xml and _userPresets.xml\r
-        /// Load it into the 2 arraylist's _presets and _userPresets\r
+        /// Setup the frmMain preset panel\r
         /// </summary>\r
-        private void LoadPresetData()\r
+        /// <param name="presetPanel">The Preset Panel from the Main Window</param>\r
+        public void GetPresetPanel(ref TreeView presetPanel)\r
         {\r
-            // First clear the _presets arraylists\r
-            _presets.Clear();\r
-            _userPresets.Clear();\r
+            this.LoadPresetData();\r
+            presetPanel.Nodes.Clear();\r
+            string category = string.Empty; // The category we are currnetly processing\r
+            TreeNode rootNode = null;\r
 \r
-            // Load in the users _presets from _userPresets.xml\r
-            if (File.Exists(_hbPresetFile))\r
+            if (this.presets.Count != 0) // Built In Presets\r
             {\r
-                using (FileStream strm = new FileStream(_hbPresetFile, FileMode.Open, FileAccess.Read))\r
+                foreach (Preset preset in this.presets)\r
                 {\r
-                    if (strm.Length != 0)\r
+                    // If the category of this preset doesn't match the current category we are processing\r
+                    // Then we need to create a new root node.\r
+                    if (preset.Category != category)\r
                     {\r
-                        List<Preset> list = Ser.Deserialize(strm) as List<Preset>;\r
-\r
-                        if (list != null)\r
-                            foreach (Preset preset in list)\r
-                                _presets.Add(preset);\r
+                        rootNode = new TreeNode(preset.Category);\r
+                        presetPanel.Nodes.Add(rootNode);\r
+                        category = preset.Category;\r
                     }\r
+\r
+                    if (preset.Category == category && rootNode != null)\r
+                        rootNode.Nodes.Add(new TreeNode(preset.Name) { ToolTipText = preset.Description });\r
                 }\r
             }\r
 \r
-            // Load in the users _presets from _userPresets.xml\r
-            if (File.Exists(_userPresetFile))\r
+            rootNode = null;\r
+            category = null;\r
+            foreach (Preset preset in this.userPresets) // User Presets\r
             {\r
-                using (FileStream strm = new FileStream(_userPresetFile, FileMode.Open, FileAccess.Read))\r
+                if (preset.Category != category && preset.Category != string.Empty)\r
                 {\r
-                    if (strm.Length != 0)\r
-                    {\r
-                        List<Preset> list = Ser.Deserialize(strm) as List<Preset>;\r
-\r
-                        if (list != null)\r
-                            foreach (Preset preset in list)\r
-                                _userPresets.Add(preset);\r
-                    }\r
+                    rootNode = new TreeNode(preset.Category) {ForeColor = Color.Black};\r
+                    presetPanel.Nodes.Add(rootNode);\r
+                    category = preset.Category;\r
                 }\r
+\r
+                if (preset.Category == category && rootNode != null)\r
+                    rootNode.Nodes.Add(new TreeNode(preset.Name) {ForeColor = Color.Black, ToolTipText = preset.Description});\r
+                else\r
+                    presetPanel.Nodes.Add(new TreeNode(preset.Name) { ForeColor = Color.Black, ToolTipText = preset.Description });\r
             }\r
         }\r
 \r
         /// <summary>\r
-        /// Setup the frmMain preset panel\r
+        /// Check if the user preset "name" exists in UserPresets list.\r
         /// </summary>\r
-        /// <param name="presetPanel"></param>\r
-        public void GetPresetPanel(ref TreeView presetPanel)\r
+        /// <param name="name">Name of the preset</param>\r
+        /// <returns>true if found</returns>\r
+        public bool CheckIfUserPresetExists(string name)\r
+        {\r
+            return name != string.Empty && this.userPresets.Any(item => item.Name == name);\r
+        }\r
+\r
+        /// <summary>\r
+        /// Check if the built in Presets stored are not out of date.\r
+        /// Update them if they are.\r
+        /// </summary>\r
+        /// <returns>true if out of date</returns>\r
+        public bool CheckIfPresetsAreOutOfDate()\r
         {\r
             this.LoadPresetData();\r
-            presetPanel.Nodes.Clear();\r
+            // Update built-in Presets if the built-in Presets belong to an older version.\r
+            if (this.presets.Count != 0)\r
+                if (this.presets[0].Version != Properties.Settings.Default.hb_version)\r
+                {\r
+                    this.UpdateBuiltInPresets();\r
+                    return true;\r
+                }\r
 \r
-            if (_presets.Count != 0) // Built In Presets\r
-            {\r
-                string category = string.Empty;\r
-                TreeNode rootNode = null;\r
+            return false;\r
+        }\r
 \r
-                foreach (Preset preset in _presets)\r
+        /// <summary>\r
+        /// Load in the preset data from Presets.xml and UserPresets.xml\r
+        /// Load it into the 2 arraylist's Presets and UserPresets\r
+        /// </summary>\r
+        private void LoadPresetData()\r
+        {\r
+            // First clear the Presets arraylists\r
+            this.presets.Clear();\r
+            this.userPresets.Clear();\r
+\r
+            try\r
+            {\r
+                // Load in the users Presets from UserPresets.xml\r
+                if (File.Exists(this.hbPresetFile))\r
                 {\r
-                    if (preset.Category != category)\r
+                    using (FileStream strm = new FileStream(this.hbPresetFile, FileMode.Open, FileAccess.Read))\r
                     {\r
-                        rootNode = new TreeNode(preset.Category);\r
-                        presetPanel.Nodes.Add(rootNode);\r
-                        category = preset.Category;\r
+                        if (strm.Length != 0)\r
+                        {\r
+                            List<Preset> list = Ser.Deserialize(strm) as List<Preset>;\r
+\r
+                            if (list != null)\r
+                                foreach (Preset preset in list)\r
+                                    this.presets.Add(preset);\r
+                        }\r
                     }\r
-\r
-                    if (preset.Category == category && rootNode != null)\r
-                        rootNode.Nodes.Add(preset.Name);\r
                 }\r
             }\r
+            catch (Exception)\r
+            {\r
+                MessageBox.Show(\r
+                    "HandBrakes preset file appears to have been corrupted. This file will now be re-generated!\n" +\r
+                    "If the problem presists, please delete the file: \n\n" + this.hbPresetFile, \r
+                    "Error",\r
+                    MessageBoxButtons.OK, \r
+                    MessageBoxIcon.Error);\r
+                this.UpdateBuiltInPresets();\r
+            }\r
 \r
-            foreach (Preset preset in _userPresets) // User Presets\r
+            try\r
+            {\r
+                // Load in the users Presets from UserPresets.xml\r
+                if (File.Exists(this.userPresetFile))\r
+                {\r
+                    using (FileStream strm = new FileStream(this.userPresetFile, FileMode.Open, FileAccess.Read))\r
+                    {\r
+                        if (strm.Length != 0)\r
+                        {\r
+                            List<Preset> list = Ser.Deserialize(strm) as List<Preset>;\r
+\r
+                            if (list != null)\r
+                                foreach (Preset preset in list)\r
+                                    this.userPresets.Add(preset);\r
+                        }\r
+                    }\r
+                }\r
+            }\r
+            catch (Exception)\r
             {\r
-                TreeNode presetTreeview = new TreeNode(preset.Name) { ForeColor = Color.Black };\r
-                presetPanel.Nodes.Add(presetTreeview);\r
+                MessageBox.Show(\r
+                    "Your User presets file appears to have been corrupted.\n" +\r
+                    "Your presets will not be loaded. You may need to re-create your presets.\n\n" +\r
+                    "Your user presets file has been renamed to 'user_presets.xml.old' and is located in:\n " +\r
+                    Path.GetDirectoryName(this.userPresetFile) + "\n" + \r
+                    "You may be able to recover some presets if you know the XML language.", \r
+                    "Error",\r
+                    MessageBoxButtons.OK, \r
+                    MessageBoxIcon.Error);\r
+\r
+                // Recover from Error.\r
+                if (File.Exists(this.userPresetFile))\r
+                {\r
+                    string disabledFile = this.userPresetFile + ".old";\r
+                    if (File.Exists(disabledFile))\r
+                        File.Delete(disabledFile);\r
+                    File.Move(this.userPresetFile, disabledFile);\r
+                }\r
             }\r
         }\r
 \r
@@ -270,90 +401,50 @@ namespace Handbrake.Presets
         {\r
             try\r
             {\r
-                using (FileStream strm = new FileStream(_hbPresetFile, FileMode.Create, FileAccess.Write))\r
+                using (FileStream strm = new FileStream(this.hbPresetFile, FileMode.Create, FileAccess.Write))\r
                 {\r
-                    Ser.Serialize(strm, _presets);\r
+                    Ser.Serialize(strm, this.presets);\r
                     strm.Close();\r
                     strm.Dispose();\r
                 }\r
 \r
-                using (FileStream strm = new FileStream(_userPresetFile, FileMode.Create, FileAccess.Write))\r
+                using (FileStream strm = new FileStream(this.userPresetFile, FileMode.Create, FileAccess.Write))\r
                 {\r
-                    Ser.Serialize(strm, _userPresets);\r
+                    Ser.Serialize(strm, this.userPresets);\r
                     strm.Close();\r
                     strm.Dispose();\r
                 }\r
             }\r
             catch (Exception exc)\r
             {\r
-                MessageBox.Show("Unable to write to the file. Please make sure the location has the correct permissions for file writing.\n Error Information: \n\n" + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\r
+                MessageBox.Show(\r
+                    "Unable to write to the file. Please make sure the location has the correct permissions for file writing.\n Error Information: \n\n" +\r
+                    exc, \r
+                    "Error", \r
+                    MessageBoxButtons.OK, \r
+                    MessageBoxIcon.Hand);\r
             }\r
         }\r
 \r
         /// <summary>\r
-        /// Check if the preset "name" exists in either _presets or _userPresets lists.\r
+        /// Check if the preset "name" exists in either Presets or UserPresets lists.\r
         /// </summary>\r
-        /// <param name="name"></param>\r
-        /// <returns></returns>\r
-        private Boolean CheckIfPresetExists(string name)\r
+        /// <param name="name">Name of the preset</param>\r
+        /// <returns>True if found</returns>\r
+        private bool CheckIfPresetExists(string name)\r
         {\r
             if (name == string.Empty)\r
                 return true;\r
 \r
             // Built In Presets\r
-            foreach (Preset item in _presets)\r
+            foreach (Preset item in this.presets)\r
             {\r
                 if (item.Name == name)\r
                     return true;\r
             }\r
 \r
             // User Presets\r
-            foreach (Preset item in _userPresets)\r
-            {\r
-                if (item.Name == name)\r
-                    return true;\r
-            }\r
-\r
-            return false;\r
-        }\r
-\r
-        /// <summary>\r
-        /// Check if the user preset "name" exists in _userPresets list.\r
-        /// </summary>\r
-        /// <param name="name"></param>\r
-        /// <returns></returns>\r
-        public Boolean CheckIfUserPresetExists(string name)\r
-        {\r
-            if (name == string.Empty)\r
-                return false;\r
-\r
-            // User Presets\r
-            foreach (Preset item in _userPresets)\r
-            {\r
-                if (item.Name == name)\r
-                    return true;\r
-            }\r
-\r
-            return false;\r
-        }\r
-\r
-        /// <summary>\r
-        /// Check if the built in _presets stored are not out of date.\r
-        /// Update them if they are.\r
-        /// </summary>\r
-        /// <returns></returns>\r
-        public Boolean CheckIfPresetsAreOutOfDate()\r
-        {\r
-            LoadPresetData();\r
-            // Update built-in _presets if the built-in _presets belong to an older version.\r
-            if (_presets.Count != 0)\r
-                if (_presets[0].Version != Properties.Settings.Default.hb_version)\r
-                {\r
-                    UpdateBuiltInPresets();\r
-                    return true;\r
-                }\r
-\r
-            return false;\r
+            return this.userPresets.Any(item => item.Name == name);\r
         }\r
     }\r
 }
\ No newline at end of file