OSDN Git Service

merge 0.9.4 to jp
[handbrake-jp/handbrake-jp.git] / win / C# / Presets / PresetsHandler.cs
index f6e66e3..0e9aa9c 100644 (file)
@@ -1,6 +1,11 @@
-using System;\r
+/*  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.Text;\r
+using System.Drawing;\r
 using System.Windows.Forms;\r
 using System.IO;\r
 using System.Text.RegularExpressions;\r
@@ -11,116 +16,120 @@ namespace Handbrake.Presets
 {\r
     public class PresetsHandler\r
     {\r
-        List<Preset> presets = new List<Preset>();  // Category+Level+Preset Name: Query\r
-        List<Preset> user_presets = new List<Preset>(); // Preset Name: Query\r
-        private static XmlSerializer ser = new XmlSerializer(typeof(List<Preset>));\r
+        List<Preset> _presets = new List<Preset>();\r
+        List<Preset> _userPresets = new List<Preset>();\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
         /// 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
-        public Boolean addPreset(string presetName, string query)\r
+        /// <param name="pictureSettings"> Bool, store crop/picture sizes in the _presets</param>\r
+        public Boolean Add(string presetName, string query, Boolean pictureSettings)\r
         {\r
-            if (checkIfPresetExists(presetName) == false)\r
+            if (CheckIfPresetExists(presetName) == false)\r
             {\r
-                Preset newPreset = new Preset();\r
-                newPreset.Name = presetName;\r
-                newPreset.Query = query;\r
-                user_presets.Add(newPreset);\r
-                updateUserPresetsFile();\r
+                Preset newPreset = new Preset { Name = presetName, Query = query, PictureSettings = pictureSettings, Version = Properties.Settings.Default.hb_version };\r
+                _userPresets.Add(newPreset);\r
+                UpdatePresetFiles();\r
                 return true;\r
             }\r
+/*\r
             else\r
             {\r
                 MessageBox.Show("そのプリセット名はすでに存在しています。 別の名前を選択してください", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
                 return false;\r
             }\r
+*/\r
+            return false;\r
         }\r
 \r
         /// <summary>\r
         /// Remove a preset with a given name from either the built in or user preset list.\r
         /// </summary>\r
         /// <param name="name">String, the preset name</param>\r
-        public void remove(string name)\r
+        public void Remove(string name)\r
         {\r
             List<Preset> newPresets = new List<Preset>();\r
             List<Preset> newUserPresets = new List<Preset>();\r
 \r
             // Built In Presets\r
-            foreach (Preset item in presets)\r
+            foreach (Preset item in _presets)\r
             {\r
                 if (item.Name != name)\r
                 {\r
                     newPresets.Add(item);\r
                 }\r
             }\r
-            presets = newPresets;\r
+            _presets = newPresets;\r
 \r
             // User Presets\r
-            foreach (Preset item in user_presets)\r
+            foreach (Preset item in _userPresets)\r
             {\r
                 if (item.Name != name)\r
                 {\r
                     newUserPresets.Add(item);\r
                 }\r
             }\r
-            user_presets = newUserPresets;\r
-\r
-            // Now, Update the presets.xml and user_presets.xml file with the new items.\r
-            string userPresets = Application.StartupPath.ToString() + "\\user_presets.xml";\r
-            string presetsFile = Application.StartupPath.ToString() + "\\presets.xml";\r
+            _userPresets = newUserPresets;\r
 \r
-            // Rebuild the user_presets.xml file\r
-            updateUserPresetsFile();\r
-            updatePresetsFile();\r
+            // Rebuild the _userPresets.xml file\r
+            UpdatePresetFiles();\r
+            UpdatePresetFiles();\r
         }\r
 \r
         /// <summary>\r
-        /// Get a List of all the built in preset names.\r
+        /// Remove all built in _presets;\r
         /// </summary>\r
-        /// <returns>List<String> of preset names</returns>\r
-        public List<Preset> getBuildInPresets()\r
+        public void RemoveBuiltInPresets()\r
         {\r
-            return presets;\r
+            _presets.Clear();\r
+            UpdatePresetFiles();\r
         }\r
 \r
         /// <summary>\r
-        /// Get a List of all the User preset names.\r
+        /// Save changes to a given preset in the user preset list.\r
         /// </summary>\r
-        /// <returns>List<String> of preset names</returns>\r
-        public List<string> getUserPresetNames()\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 preset</param>\r
+        public void Update(string presetName, string query, Boolean pictureSettings)\r
         {\r
-            List<string> names = new List<string>();\r
-\r
             // User Presets\r
-            foreach (Preset item in user_presets)\r
+            foreach (Preset item in _userPresets)\r
             {\r
-                names.Add(item.Name);\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
+                }\r
             }\r
-\r
-            return names;\r
         }\r
 \r
         /// <summary>\r
         /// Return the CLI query for a preset name given in name\r
         /// </summary>\r
         /// <param name="name">String, The preset's name</param>\r
-        /// <returns>String, the CLI query for the given preset name</returns>\r
-        public string getCliForPreset(string name)\r
+        /// <returns>String, the CLI query for the given preset name</returns>    not\r
+        public Preset GetPreset(string name)\r
         {\r
             // Built In Presets\r
-            foreach (Preset item in presets)\r
+            foreach (Preset item in _presets)\r
             {\r
                 if (item.Name == name)\r
-                    return item.Query;\r
+                    return item;\r
             }\r
 \r
             // User Presets\r
-            foreach (Preset item in user_presets)\r
+            foreach (Preset item in _userPresets)\r
             {\r
                 if (item.Name == name)\r
-                    return item.Query;\r
+                    return item;\r
             }\r
 \r
             return null;\r
@@ -129,183 +138,207 @@ namespace Handbrake.Presets
         /// <summary>\r
         /// Reads the CLI's CLI output format and load's them into the preset List<Preset>\r
         /// </summary>\r
-        public void updateBuiltInPresets()\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
-\r
             string strCmdLine = String.Format(@"cmd /c """"{0}"" --preset-list >""{1}"" 2>&1""", handbrakeCLIPath, presetsPath);\r
 \r
-            ProcessStartInfo hbGetPresets = new ProcessStartInfo("CMD.exe", strCmdLine);\r
-            hbGetPresets.WindowStyle = ProcessWindowStyle.Hidden;\r
-\r
+            ProcessStartInfo hbGetPresets = new ProcessStartInfo("CMD.exe", strCmdLine) { WindowStyle = ProcessWindowStyle.Hidden };\r
             Process hbproc = Process.Start(hbGetPresets);\r
-            hbproc.WaitForExit();\r
-            hbproc.Dispose();\r
-            hbproc.Close();\r
-\r
-            // Clear the current built in presets and now parse the tempory presets file.\r
-            presets.Clear();\r
-            string filePath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");\r
-            if (File.Exists(filePath))\r
+            if (hbproc != null)\r
+            {\r
+                hbproc.WaitForExit();\r
+                hbproc.Dispose();\r
+                hbproc.Close();\r
+            }\r
+\r
+            // Clear the current built in _presets and now parse the tempory _presets file.\r
+            _presets.Clear();\r
+\r
+            if (File.Exists(presetsPath))\r
             {\r
-                StreamReader presetInput = new StreamReader(filePath);\r
-                int level = 1;\r
+                StreamReader presetInput = new StreamReader(presetsPath);\r
+\r
                 string category = String.Empty;\r
-                string level_1_category = String.Empty;\r
 \r
                 while (!presetInput.EndOfStream)\r
                 {\r
                     string line = presetInput.ReadLine();\r
-                    if (line.Contains("<") && !line.Contains("<<"))\r
-                    {\r
-                        level = 1;\r
+                    if (line.Contains("<") && !line.Contains("<<")) // Found the beginning of a preset block \r
                         category = line.Replace("<", "").Trim();\r
-                        level_1_category = category;\r
-                    }\r
 \r
-                    if (line.Contains("<<"))\r
-                    {\r
-                        level = 2;\r
-                        category = line.Replace("<<", "").Trim();\r
-                    }\r
-\r
-                    if (line.Trim().Contains(">>"))\r
-                    {\r
-                        level = 1;\r
-                        category = level_1_category;\r
-                    }\r
-\r
-                    if (line.Contains("+"))\r
+                    if (line.Contains("+")) // A Preset\r
                     {\r
                         Regex r = new Regex("(:  )"); // Split on hyphens. \r
                         string[] presetName = r.Split(line);\r
 \r
-                        Preset newPreset = new Preset();\r
-                        newPreset.Level = level;\r
-                        newPreset.Category = category;\r
-                        newPreset.Name = presetName[0].Replace("+", "").Trim();\r
-                        newPreset.Query = presetName[2];\r
-                        presets.Add(newPreset);\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
+                        _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
-            updatePresetsFile();\r
+            // Finally, Create a new or update the current _presets.xml file\r
+            UpdatePresetFiles();\r
         }\r
 \r
         /// <summary>\r
-        /// Load in the preset data from presets.xml and user_presets.xml\r
-        /// Load it into the 2 arraylist's presets and user_presets\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
-        public void loadPresetData()\r
+        private void LoadPresetData()\r
         {\r
-            // First clear the presets arraylists\r
-            presets.Clear();\r
-            user_presets.Clear();\r
-\r
-            string filePath = string.Empty;\r
+            // First clear the _presets arraylists\r
+            _presets.Clear();\r
+            _userPresets.Clear();\r
 \r
-            // Load in the users presets from user_presets.xml\r
-            filePath = Application.StartupPath.ToString() + "\\presets.xml";\r
-            if (File.Exists(filePath))\r
+            // Load in the users _presets from _userPresets.xml\r
+            if (File.Exists(_hbPresetFile))\r
             {\r
-                using (FileStream strm = new FileStream(filePath, FileMode.Open, FileAccess.Read))\r
+                using (FileStream strm = new FileStream(_hbPresetFile, FileMode.Open, FileAccess.Read))\r
                 {\r
                     if (strm.Length != 0)\r
                     {\r
-                        List<Preset> list = ser.Deserialize(strm) as List<Preset>;\r
+                        List<Preset> list = Ser.Deserialize(strm) as List<Preset>;\r
 \r
-                        foreach (Preset preset in list)\r
-                            presets.Add(preset);\r
+                        if (list != null)\r
+                            foreach (Preset preset in list)\r
+                                _presets.Add(preset);\r
                     }\r
                 }\r
             }\r
 \r
-            // Load in the users presets from user_presets.xml\r
-            filePath = Application.StartupPath.ToString() + "\\user_presets.xml";\r
-            if (File.Exists(filePath))\r
+            // Load in the users _presets from _userPresets.xml\r
+            if (File.Exists(_userPresetFile))\r
             {\r
-                using (FileStream strm = new FileStream(filePath, FileMode.Open, FileAccess.Read))\r
+                using (FileStream strm = new FileStream(_userPresetFile, FileMode.Open, FileAccess.Read))\r
                 {\r
                     if (strm.Length != 0)\r
                     {\r
-                        List<Preset> list = ser.Deserialize(strm) as List<Preset>;\r
+                        List<Preset> list = Ser.Deserialize(strm) as List<Preset>;\r
 \r
-                        foreach (Preset preset in list)\r
-                            user_presets.Add(preset);\r
+                        if (list != null)\r
+                            foreach (Preset preset in list)\r
+                                _userPresets.Add(preset);\r
                     }\r
                 }\r
             }\r
         }\r
 \r
         /// <summary>\r
-        /// Updates the presets.xml file which contains the built in presets\r
-        /// It takes the List of Presets and converts them into XML which is stored in presets.xml\r
+        /// Setup the frmMain preset panel\r
         /// </summary>\r
-        private void updatePresetsFile()\r
+        /// <param name="presetPanel"></param>\r
+        public void GetPresetPanel(ref TreeView presetPanel)\r
         {\r
-            string userPresets = Application.StartupPath.ToString() + "\\presets.xml";\r
-            try\r
+            this.LoadPresetData();\r
+            presetPanel.Nodes.Clear();\r
+\r
+            if (_presets.Count != 0) // Built In Presets\r
             {\r
-                using (FileStream strm = new FileStream(userPresets, FileMode.Create, FileAccess.Write))\r
+                string category = string.Empty;\r
+                TreeNode rootNode = null;\r
+\r
+                foreach (Preset preset in _presets)\r
                 {\r
-                    ser.Serialize(strm, presets);\r
-                    strm.Close();\r
-                    strm.Dispose();\r
+                    if (preset.Category != category)\r
+                    {\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(preset.Name);\r
                 }\r
             }\r
-            catch (Exception exc)\r
+\r
+            foreach (Preset preset in _userPresets) // User Presets\r
             {\r
+/*\r
                 MessageBox.Show("ファイルの書き込みに失敗しました。\n Error Information: \n\n" + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\r
+*/\r
+                TreeNode presetTreeview = new TreeNode(preset.Name) { ForeColor = Color.Black };\r
+                presetPanel.Nodes.Add(presetTreeview);\r
             }\r
         }\r
 \r
         /// <summary>\r
-        /// Updates the user_presets.xml file which contains the built in presets\r
-        /// It takes the List of Presets and converts them into XML which is stored in user_presets.xml\r
+        /// Update the preset files\r
         /// </summary>\r
-        private void updateUserPresetsFile()\r
+        private void UpdatePresetFiles()\r
         {\r
-            string userPresets = Application.StartupPath.ToString() + "\\user_presets.xml";\r
             try\r
             {\r
-                using (FileStream strm = new FileStream(userPresets, FileMode.Create, FileAccess.Write))\r
+                using (FileStream strm = new FileStream(_hbPresetFile, FileMode.Create, FileAccess.Write))\r
                 {\r
-                    ser.Serialize(strm, user_presets);\r
+                    Ser.Serialize(strm, _presets);\r
+                    strm.Close();\r
+                    strm.Dispose();\r
+                }\r
+\r
+                using (FileStream strm = new FileStream(_userPresetFile, FileMode.Create, FileAccess.Write))\r
+                {\r
+                    Ser.Serialize(strm, _userPresets);\r
                     strm.Close();\r
                     strm.Dispose();\r
                 }\r
             }\r
             catch (Exception exc)\r
             {\r
-                MessageBox.Show("ファイルの書き込みに失敗しました。\n Error Information: \n\n" + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\r
+                MessageBox.Show("ファイルの書き込みに失敗しました。\n Error Information: \n\n" + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\r
             }\r
         }\r
 \r
         /// <summary>\r
-        /// Check if the preset "name" exists in either presets or user_presets 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
+        private Boolean 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 _presets)\r
             {\r
                 if (item.Name == name)\r
                     return true;\r
             }\r
 \r
             // User Presets\r
-            foreach (Preset item in 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
@@ -313,5 +346,24 @@ namespace Handbrake.Presets
 \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
+        }\r
     }\r
 }
\ No newline at end of file