From 54f291c822357595718264fe167cf63c25e5fbb8 Mon Sep 17 00:00:00 2001 From: sr55 Date: Sat, 25 Oct 2008 20:36:04 +0000 Subject: WinGui: - Nested Presets bar (Built-in presets only) - Both user and built-in presets are now stored in XML files - Preset bar now has right-click menu with: Expand All, Collapse All and Delete Known Issues: - When a preset is removed, all items in the preset bar with child presets are collapsed. - Right Click menu only works if the preset was selected before right clicking. Code is probably a bit buggy so all feedback on this new bar is welcome. git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@1866 b64f7644-9d1e-0410-96f1-a4d463321fa5 --- win/C#/Presets/PresetsHandler.cs | 317 +++++++++++++++++++++++++++++++++++++++ win/C#/Presets/preset.cs | 60 ++++++++ 2 files changed, 377 insertions(+) create mode 100644 win/C#/Presets/PresetsHandler.cs create mode 100644 win/C#/Presets/preset.cs (limited to 'win/C#/Presets') diff --git a/win/C#/Presets/PresetsHandler.cs b/win/C#/Presets/PresetsHandler.cs new file mode 100644 index 000000000..172125a9f --- /dev/null +++ b/win/C#/Presets/PresetsHandler.cs @@ -0,0 +1,317 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Windows.Forms; +using System.IO; +using System.Text.RegularExpressions; +using System.Diagnostics; +using System.Xml.Serialization; + +namespace Handbrake.Presets +{ + public class PresetsHandler + { + List presets = new List(); // Category+Level+Preset Name: Query + List user_presets = new List(); // Preset Name: Query + private static XmlSerializer ser = new XmlSerializer(typeof(List)); + + /// + /// Add a new preset to the system + /// + /// String, The name of the new preset + /// String, the CLI query for the new preset + public Boolean addPreset(string presetName, string query) + { + if (checkIfPresetExists(presetName) == false) + { + Preset newPreset = new Preset(); + newPreset.Name = presetName; + newPreset.Query = query; + user_presets.Add(newPreset); + updateUserPresetsFile(); + return true; + } + else + { + MessageBox.Show("Sorry, that preset name already exists. Please choose another!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); + return false; + } + } + + /// + /// Remove a preset with a given name from either the built in or user preset list. + /// + /// String, the preset name + public void remove(string name) + { + List newPresets = new List(); + List newUserPresets = new List(); + + // Built In Presets + foreach (Preset item in presets) + { + if (item.Name != name) + { + newPresets.Add(item); + } + } + presets = newPresets; + + // User Presets + foreach (Preset item in user_presets) + { + if (item.Name != name) + { + newUserPresets.Add(item); + } + } + user_presets = newUserPresets; + + // Now, Update the presets.xml and user_presets.xml file with the new items. + string userPresets = Application.StartupPath.ToString() + "\\user_presets.xml"; + string presetsFile = Application.StartupPath.ToString() + "\\presets.xml"; + + // Rebuild the user_presets.xml file + updateUserPresetsFile(); + updatePresetsFile(); + } + + /// + /// Get a List of all the built in preset names. + /// + /// List of preset names + public List getBuildInPresets() + { + return presets; + } + + /// + /// Get a List of all the User preset names. + /// + /// List of preset names + public List getUserPresetNames() + { + List names = new List(); + + // User Presets + foreach (Preset item in user_presets) + { + names.Add(item.Name); + } + + return names; + } + + /// + /// Return the CLI query for a preset name given in name + /// + /// String, The preset's name + /// String, the CLI query for the given preset name + public string getCliForPreset(string name) + { + // Built In Presets + foreach (Preset item in presets) + { + if (item.Name == name) + return item.Query; + } + + // User Presets + foreach (Preset item in user_presets) + { + if (item.Name == name) + return item.Query; + } + + return null; + } + + /// + /// Reads the CLI's CLI output format and load's them into the preset List + /// + public void updateBuiltInPresets() + { + // Create a new tempory file and execute the CLI to get the built in presets. + string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe"); + string presetsPath = Path.Combine(Path.GetTempPath(), "temp_presets.dat"); + + string strCmdLine = String.Format(@"cmd /c """"{0}"" --preset-list >""{1}"" 2>&1""", handbrakeCLIPath, presetsPath); + + ProcessStartInfo hbGetPresets = new ProcessStartInfo("CMD.exe", strCmdLine); + hbGetPresets.WindowStyle = ProcessWindowStyle.Hidden; + + Process hbproc = Process.Start(hbGetPresets); + hbproc.WaitForExit(); + hbproc.Dispose(); + hbproc.Close(); + + // Clear the current built in presets and now parse the tempory presets file. + presets.Clear(); + string filePath = Path.Combine(Path.GetTempPath(), "temp_presets.dat"); + if (File.Exists(filePath)) + { + StreamReader presetInput = new StreamReader(filePath); + int level = 1; + string category = String.Empty; + string level_1_category = String.Empty; + + while (!presetInput.EndOfStream) + { + string line = presetInput.ReadLine(); + if (line.Contains("<") && !line.Contains("<<")) + { + level = 1; + category = line.Replace("<", "").Trim(); + level_1_category = category; + } + + if (line.Contains("<<")) + { + level = 2; + category = line.Replace("<<", "").Trim(); + } + + if (line.Trim().Contains(">>")) + { + level = 1; + category = level_1_category; + } + + if (line.Contains("+")) + { + Regex r = new Regex("(: )"); // Split on hyphens. + string[] presetName = r.Split(line); + + Preset newPreset = new Preset(); + newPreset.Level = level; + newPreset.Category = category; + newPreset.Name = presetName[0].Replace("+", "").Trim(); + newPreset.Query = presetName[2]; + presets.Add(newPreset); + } + } + presetInput.Close(); + presetInput.Dispose(); + } + + // Finally, Create a new or update the current presets.xml file + updatePresetsFile(); + } + + /// + /// Load in the preset data from presets.xml and user_presets.xml + /// Load it into the 2 arraylist's presets and user_presets + /// + public void loadPresetData() + { + // First clear the presets arraylists + presets.Clear(); + user_presets.Clear(); + + string filePath = string.Empty; + + // Load in the users presets from user_presets.xml + filePath = Application.StartupPath.ToString() + "\\presets.xml"; + if (File.Exists(filePath)) + { + using (FileStream strm = new FileStream(filePath, FileMode.Open, FileAccess.Read)) + { + if (strm.Length != 0) + { + List list = ser.Deserialize(strm) as List; + + foreach (Preset preset in list) + presets.Add(preset); + } + } + } + + // Load in the users presets from user_presets.xml + filePath = Application.StartupPath.ToString() + "\\user_presets.xml"; + if (File.Exists(filePath)) + { + using (FileStream strm = new FileStream(filePath, FileMode.Open, FileAccess.Read)) + { + if (strm.Length != 0) + { + List list = ser.Deserialize(strm) as List; + + foreach (Preset preset in list) + user_presets.Add(preset); + } + } + } + } + + /// + /// Updates the presets.xml file which contains the built in presets + /// It takes the List of Presets and converts them into XML which is stored in presets.xml + /// + private void updatePresetsFile() + { + string userPresets = Application.StartupPath.ToString() + "\\presets.xml"; + try + { + using (FileStream strm = new FileStream(userPresets, FileMode.Create, FileAccess.Write)) + { + ser.Serialize(strm, presets); + strm.Close(); + strm.Dispose(); + } + } + catch (Exception exc) + { + 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); + } + } + + /// + /// Updates the user_presets.xml file which contains the built in presets + /// It takes the List of Presets and converts them into XML which is stored in user_presets.xml + /// + private void updateUserPresetsFile() + { + string userPresets = Application.StartupPath.ToString() + "\\user_presets.xml"; + try + { + using (FileStream strm = new FileStream(userPresets, FileMode.Create, FileAccess.Write)) + { + ser.Serialize(strm, user_presets); + strm.Close(); + strm.Dispose(); + } + } + catch (Exception exc) + { + 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); + } + } + + /// + /// Check if the preset "name" exists in either presets or user_presets lists. + /// + /// + /// + private Boolean checkIfPresetExists(string name) + { + if (name == string.Empty) + return true; + + // Built In Presets + foreach (Preset item in presets) + { + if (item.Name == name) + return true; + } + + // User Presets + foreach (Preset item in user_presets) + { + if (item.Name == name) + return true; + } + + return false; + } + } +} \ No newline at end of file diff --git a/win/C#/Presets/preset.cs b/win/C#/Presets/preset.cs new file mode 100644 index 000000000..9f7eed8bd --- /dev/null +++ b/win/C#/Presets/preset.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Windows.Forms; +using System.IO; +using System.Text.RegularExpressions; +using System.Diagnostics; + +namespace Handbrake.Presets +{ + public class Preset + { + + private int level = 0; + private string category = null; + private string name; + private string query; + + public Preset() + { + } + + /// + /// Get or Set the preset's level. This indicated if it is a root or child node + /// + public int Level + { + get { return level; } + set { this.level = value; } + } + + /// + /// Get or Set the category which the preset resides under + /// + public string Category + { + get { return category; } + set { this.category = value; } + } + + /// + /// Get or Set the preset name + /// + public string Name + { + get { return name; } + set { this.name = value; } + } + + /// + /// Get or set the preset query + /// + public string Query + { + get { return query; } + set { this.query = value; } + } + + } +} \ No newline at end of file -- cgit v1.2.3