{\r
public class PresetsHandler\r
{\r
- List<Preset> presets = new List<Preset>();\r
- List<Preset> user_presets = new List<Preset>();\r
- private static readonly XmlSerializer ser = new XmlSerializer(typeof(List<Preset>));\r
- String userPresetFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\user_presets.xml";\r
- string hbPresetFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\presets.xml";\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
- /// <param name="pictureSettings"> Bool, store crop/picture sizes in the presets</param>\r
- public Boolean addPreset(string presetName, string query, Boolean pictureSettings)\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 { Name = presetName, Query = query, PictureSettings = pictureSettings, Version = Properties.Settings.Default.hb_version};\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
- MessageBox.Show("Sorry, that preset name already exists. Please choose another!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
return false;\r
}\r
\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
+ _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
+ /// Remove all built in _presets;\r
+ /// </summary>\r
+ public void RemoveBuiltInPresets()\r
+ {\r
+ _presets.Clear();\r
+ UpdatePresetFiles();\r
}\r
\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 preset</param>\r
- public void updatePreset(string presetName, string query, Boolean pictureSettings)\r
+ public void Update(string presetName, string query, Boolean pictureSettings)\r
{\r
// User Presets\r
- foreach (Preset item in user_presets)\r
+ foreach (Preset item in _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
- updateUserPresetsFile();\r
+ UpdatePresetFiles();\r
}\r
}\r
}\r
/// </summary>\r
/// <param name="name">String, The preset's name</param>\r
/// <returns>String, the CLI query for the given preset name</returns> not\r
- public Preset getPreset(string name)\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;\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;\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
+ 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
hbproc.Close();\r
}\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
+ // Clear the current built in _presets and now parse the tempory _presets file.\r
+ _presets.Clear();\r
\r
- if (File.Exists(filePath))\r
+ if (File.Exists(presetsPath))\r
{\r
- StreamReader presetInput = new StreamReader(filePath);\r
+ StreamReader presetInput = new StreamReader(presetsPath);\r
\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("<<")) // Found the beginning of a preset block \r
- {\r
- level = 1;\r
category = line.Replace("<", "").Trim();\r
- level_1_category = category;\r
- }\r
-\r
- if (line.Contains("<<")) // found a sub preset block\r
- {\r
- level = 2;\r
- category = line.Replace("<<", "").Trim();\r
- }\r
-\r
- if (line.Trim().Contains(">>")) // End of sub preset block\r
- {\r
- level = 1;\r
- category = level_1_category;\r
- }\r
\r
if (line.Contains("+")) // A Preset\r
{\r
string[] presetName = r.Split(line);\r
\r
Preset newPreset = new Preset\r
- {\r
- Level = level,\r
- Category = category,\r
- TopCategory = level_1_category,\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
+ _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
- private void loadPresetData()\r
+ private void LoadPresetData()\r
{\r
- // First clear the presets arraylists\r
- presets.Clear();\r
- user_presets.Clear();\r
+ // First clear the _presets arraylists\r
+ _presets.Clear();\r
+ _userPresets.Clear();\r
\r
- // Load in the users presets from user_presets.xml\r
- if (File.Exists(hbPresetFile))\r
+ // Load in the users _presets from _userPresets.xml\r
+ if (File.Exists(_hbPresetFile))\r
{\r
- using (FileStream strm = new FileStream(hbPresetFile, 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
if (list != null)\r
foreach (Preset preset in list)\r
- presets.Add(preset);\r
+ _presets.Add(preset);\r
}\r
}\r
}\r
\r
- // Load in the users presets from user_presets.xml\r
- if (File.Exists(userPresetFile))\r
+ // Load in the users _presets from _userPresets.xml\r
+ if (File.Exists(_userPresetFile))\r
{\r
- using (FileStream strm = new FileStream(userPresetFile, 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
if (list != null)\r
foreach (Preset preset in list)\r
- user_presets.Add(preset);\r
+ _userPresets.Add(preset);\r
}\r
}\r
}\r
/// Setup the frmMain preset panel\r
/// </summary>\r
/// <param name="presetPanel"></param>\r
- public void getPresetPanel(ref TreeView presetPanel)\r
+ public void GetPresetPanel(ref TreeView presetPanel)\r
{\r
- this.loadPresetData();\r
+ this.LoadPresetData();\r
presetPanel.Nodes.Clear();\r
\r
- if (presets.Count != 0)\r
+ if (_presets.Count != 0) // Built In Presets\r
{\r
- string category = presets[0].Category;\r
- TreeNode rootNode = new TreeNode(presets[0].Category);\r
- TreeNode childNode = null;\r
- Boolean addChildNode = false;\r
+ string category = string.Empty;\r
+ TreeNode rootNode = null;\r
\r
- foreach (Preset preset in presets)\r
+ foreach (Preset preset in _presets)\r
{\r
- // Deal with Root\r
- if (preset.Category == category && preset.TopCategory == category)\r
- rootNode.Nodes.Add(preset.Name);\r
- else if (preset.Category != category && preset.TopCategory == category) // Deal with child nodes for that root\r
+ if (preset.Category != category)\r
{\r
- if (childNode == null) // For the first child node\r
- childNode = new TreeNode(preset.Category);\r
-\r
- childNode.Nodes.Add(preset.Name);\r
- addChildNode = true;\r
- }\r
- else if (preset.Category != category && preset.TopCategory != category && preset.Level == 1)// Deal with changing root nodes\r
- {\r
- // If we find there are child nodes, add them to the current root node set before adding the rootnode to the panel.\r
- if (addChildNode)\r
- {\r
- rootNode.Nodes.Add(childNode);\r
- childNode = null;\r
- addChildNode = false;\r
- }\r
- // Add the current rootnodes to the panel, and prepare for a new category of presets\r
- presetPanel.Nodes.Add(rootNode);\r
rootNode = new TreeNode(preset.Category);\r
- rootNode.Nodes.Add(preset.Name);\r
-\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
- presetPanel.Nodes.Add(rootNode); // Add the final set of nodes \r
}\r
\r
- // User Presets\r
- foreach (Preset preset in user_presets)\r
+ foreach (Preset preset in _userPresets) // User Presets\r
{\r
- TreeNode preset_treeview = new TreeNode(preset.Name) { ForeColor = Color.Black };\r
- presetPanel.Nodes.Add(preset_treeview);\r
+ TreeNode presetTreeview = new TreeNode(preset.Name) { ForeColor = Color.Black };\r
+ presetPanel.Nodes.Add(presetTreeview);\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
+ /// Update the preset files\r
/// </summary>\r
- private void updatePresetsFile()\r
+ private void UpdatePresetFiles()\r
{\r
- string userPresets = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\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, presets);\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, "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
- try\r
- {\r
- using (FileStream strm = new FileStream(userPresetFile, FileMode.Create, FileAccess.Write))\r
+ using (FileStream strm = new FileStream(_userPresetFile, FileMode.Create, FileAccess.Write))\r
{\r
- ser.Serialize(strm, user_presets);\r
+ Ser.Serialize(strm, _userPresets);\r
strm.Close();\r
strm.Dispose();\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
/// <summary>\r
- /// Check if the user preset "name" exists in user_presets list.\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
+ public Boolean CheckIfUserPresetExists(string name)\r
{\r
if (name == string.Empty)\r
return false;\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
/// <summary>\r
- /// Check if the built in presets stored are not out of date.\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
+ 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
+ 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
+ UpdateBuiltInPresets();\r
return true;\r
}\r
\r