+using System;\r
+using System.Collections.Generic;\r
+using System.Text;\r
+using System.Windows.Forms;\r
+using System.IO;\r
+using System.Text.RegularExpressions;\r
+using System.Diagnostics;\r
+using System.Xml.Serialization;\r
+\r
+namespace Handbrake.Presets\r
+{\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
+\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
+ {\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
+ return true;\r
+ }\r
+ else\r
+ {\r
+ MessageBox.Show("Sorry, that preset name already exists. Please choose another!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
+ return false;\r
+ }\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
+ {\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
+ {\r
+ if (item.Name != name)\r
+ {\r
+ newPresets.Add(item);\r
+ }\r
+ }\r
+ presets = newPresets;\r
+\r
+ // User Presets\r
+ foreach (Preset item in user_presets)\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
+\r
+ // Rebuild the user_presets.xml file\r
+ updateUserPresetsFile();\r
+ updatePresetsFile();\r
+ }\r
+\r
+ /// <summary>\r
+ /// Get a List of all the built in preset names.\r
+ /// </summary>\r
+ /// <returns>List<String> of preset names</returns>\r
+ public List<Preset> getBuildInPresets()\r
+ {\r
+ return presets;\r
+ }\r
+\r
+ /// <summary>\r
+ /// Get a List of all the User preset names.\r
+ /// </summary>\r
+ /// <returns>List<String> of preset names</returns>\r
+ public List<string> getUserPresetNames()\r
+ {\r
+ List<string> names = new List<string>();\r
+\r
+ // User Presets\r
+ foreach (Preset item in user_presets)\r
+ {\r
+ names.Add(item.Name);\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
+ {\r
+ // Built In Presets\r
+ foreach (Preset item in presets)\r
+ {\r
+ if (item.Name == name)\r
+ return item.Query;\r
+ }\r
+\r
+ // User Presets\r
+ foreach (Preset item in user_presets)\r
+ {\r
+ if (item.Name == name)\r
+ return item.Query;\r
+ }\r
+\r
+ return null;\r
+ }\r
+\r
+ /// <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
+ {\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
+ 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
+ {\r
+ StreamReader presetInput = new StreamReader(filePath);\r
+ int level = 1;\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
+ 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
+ {\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
+ }\r
+ }\r
+ presetInput.Close();\r
+ presetInput.Dispose();\r
+ }\r
+\r
+ // Finally, Create a new or update the current presets.xml file\r
+ updatePresetsFile();\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
+ /// </summary>\r
+ public void loadPresetData()\r
+ {\r
+ // First clear the presets arraylists\r
+ presets.Clear();\r
+ user_presets.Clear();\r
+\r
+ string filePath = string.Empty;\r
+\r
+ // Load in the users presets from user_presets.xml\r
+ filePath = Application.StartupPath.ToString() + "\\presets.xml";\r
+ if (File.Exists(filePath))\r
+ {\r
+ using (FileStream strm = new FileStream(filePath, FileMode.Open, FileAccess.Read))\r
+ {\r
+ if (strm.Length != 0)\r
+ {\r
+ List<Preset> list = ser.Deserialize(strm) as List<Preset>;\r
+\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
+ {\r
+ using (FileStream strm = new FileStream(filePath, FileMode.Open, FileAccess.Read))\r
+ {\r
+ if (strm.Length != 0)\r
+ {\r
+ List<Preset> list = ser.Deserialize(strm) as List<Preset>;\r
+\r
+ foreach (Preset preset in list)\r
+ user_presets.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
+ /// </summary>\r
+ private void updatePresetsFile()\r
+ {\r
+ string userPresets = Application.StartupPath.ToString() + "\\presets.xml";\r
+ try\r
+ {\r
+ using (FileStream strm = new FileStream(userPresets, FileMode.Create, FileAccess.Write))\r
+ {\r
+ ser.Serialize(strm, presets);\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.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\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
+ /// </summary>\r
+ private void updateUserPresetsFile()\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
+ {\r
+ ser.Serialize(strm, user_presets);\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.ToString(), "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
+ /// </summary>\r
+ /// <param name="name"></param>\r
+ /// <returns></returns>\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
+ {\r
+ if (item.Name == name)\r
+ return true;\r
+ }\r
+\r
+ // User Presets\r
+ foreach (Preset item in user_presets)\r
+ {\r
+ if (item.Name == name)\r
+ return true;\r
+ }\r
+\r
+ return false;\r
+ }\r
+ }\r
+}
\ No newline at end of file