/* 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
/// 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
+ /// <returns>\r
+ /// The add.\r
+ /// </returns>\r
+ public bool Add(string presetName, string query, bool pictureSettings)\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
+ PictureSettings = pictureSettings, \r
+ Version = Properties.Settings.Default.hb_version\r
+ };\r
+ this.userPresets.Add(newPreset);\r
+ this.UpdatePresetFiles();\r
return true;\r
}\r
return false;\r
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
+ this.UpdatePresetFiles();\r
+ this.UpdatePresetFiles();\r
}\r
\r
/// <summary>\r
/// </summary>\r
public void RemoveBuiltInPresets()\r
{\r
- Presets.Clear();\r
- UpdatePresetFiles();\r
+ this.presets.Clear();\r
+ this.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 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
+ MessageBox.Show(\r
+ "Changes to \"" + presetName + "\" Saved",\r
+ "Success",\r
+ MessageBoxButtons.OK, \r
+ MessageBoxIcon.Information);\r
+ this.UpdatePresetFiles();\r
}\r
}\r
}\r
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
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
}\r
\r
// Clear the current built in Presets and now parse the tempory Presets file.\r
- Presets.Clear();\r
+ this.presets.Clear();\r
\r
if (File.Exists(presetsPath))\r
{\r
{\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
\r
Preset newPreset = new Preset\r
{\r
- Category = category,\r
- Name = presetName[0].Replace("+", "").Trim(),\r
- Query = presetName[2],\r
- Version = Properties.Settings.Default.hb_version,\r
+ Category = category, \r
+ Name = presetName[0].Replace("+", string.Empty).Trim(), \r
+ Query = presetName[2], \r
+ Version = Properties.Settings.Default.hb_version, \r
PictureSettings = pic\r
};\r
- Presets.Add(newPreset);\r
+ this.presets.Add(newPreset);\r
}\r
}\r
presetInput.Close();\r
}\r
\r
// Finally, Create a new or update the current Presets.xml file\r
- UpdatePresetFiles();\r
+ this.UpdatePresetFiles();\r
+ }\r
+\r
+ /// <summary>\r
+ /// Setup the frmMain preset panel\r
+ /// </summary>\r
+ /// <param name="presetPanel">The Preset Panel from the Main Window</param>\r
+ public void GetPresetPanel(ref TreeView presetPanel)\r
+ {\r
+ this.LoadPresetData();\r
+ presetPanel.Nodes.Clear();\r
+ string category = string.Empty;\r
+ TreeNode rootNode = null;\r
+\r
+ if (this.presets.Count != 0) // Built In Presets\r
+ {\r
+ foreach (Preset preset in this.presets)\r
+ {\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
+\r
+ rootNode = null;\r
+ category = null;\r
+ foreach (Preset preset in this.userPresets) // User Presets\r
+ {\r
+ if (preset.Category != category && preset.Category != string.Empty)\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});\r
+ else\r
+ presetPanel.Nodes.Add(new TreeNode(preset.Name) {ForeColor = Color.Black});\r
+ }\r
+ }\r
+\r
+ /// <summary>\r
+ /// Check if the user preset "name" exists in UserPresets list.\r
+ /// </summary>\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
+ // 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
+ return false;\r
}\r
\r
/// <summary>\r
private void LoadPresetData()\r
{\r
// First clear the Presets arraylists\r
- Presets.Clear();\r
- UserPresets.Clear();\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(HbPresetFile))\r
+ if (File.Exists(this.hbPresetFile))\r
{\r
- using (FileStream strm = new FileStream(HbPresetFile, FileMode.Open, FileAccess.Read))\r
+ using (FileStream strm = new FileStream(this.hbPresetFile, FileMode.Open, FileAccess.Read))\r
{\r
if (strm.Length != 0)\r
{\r
\r
if (list != null)\r
foreach (Preset preset in list)\r
- Presets.Add(preset);\r
+ this.presets.Add(preset);\r
}\r
}\r
}\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" + HbPresetFile, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
- UpdateBuiltInPresets();\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
try\r
{\r
// Load in the users Presets from UserPresets.xml\r
- if (File.Exists(UserPresetFile))\r
+ if (File.Exists(this.userPresetFile))\r
{\r
- using (FileStream strm = new FileStream(UserPresetFile, FileMode.Open, FileAccess.Read))\r
+ using (FileStream strm = new FileStream(this.userPresetFile, FileMode.Open, FileAccess.Read))\r
{\r
if (strm.Length != 0)\r
{\r
\r
if (list != null)\r
foreach (Preset preset in list)\r
- UserPresets.Add(preset);\r
+ this.userPresets.Add(preset);\r
}\r
}\r
}\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(UserPresetFile) + "\n" +\r
- "You may be able to recover some presets if you know the XML language.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\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(UserPresetFile))\r
+ if (File.Exists(this.userPresetFile))\r
{\r
- string disabledFile = UserPresetFile + ".old";\r
+ string disabledFile = this.userPresetFile + ".old";\r
if (File.Exists(disabledFile))\r
File.Delete(disabledFile);\r
- File.Move(UserPresetFile, disabledFile);\r
+ File.Move(this.userPresetFile, disabledFile);\r
}\r
-\r
- }\r
- }\r
-\r
- /// <summary>\r
- /// Setup the frmMain preset panel\r
- /// </summary>\r
- /// <param name="presetPanel"></param>\r
- public void GetPresetPanel(ref TreeView presetPanel)\r
- {\r
- this.LoadPresetData();\r
- presetPanel.Nodes.Clear();\r
- string category = string.Empty;\r
- TreeNode rootNode = null;\r
-\r
- if (Presets.Count != 0) // Built In Presets\r
- {\r
-\r
- foreach (Preset preset in Presets)\r
- {\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
-\r
- rootNode = null; category = null;\r
- foreach (Preset preset in UserPresets) // User Presets\r
- {\r
- if (preset.Category != category && preset.Category != null)\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 });\r
- else\r
- presetPanel.Nodes.Add(new TreeNode(preset.Name) { ForeColor = Color.Black });\r
}\r
}\r
\r
{\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
/// </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