diff options
Diffstat (limited to 'win/C#/Presets/PresetsHandler.cs')
-rw-r--r-- | win/C#/Presets/PresetsHandler.cs | 331 |
1 files changed, 173 insertions, 158 deletions
diff --git a/win/C#/Presets/PresetsHandler.cs b/win/C#/Presets/PresetsHandler.cs index a5978fccf..1a47a4dd8 100644 --- a/win/C#/Presets/PresetsHandler.cs +++ b/win/C#/Presets/PresetsHandler.cs @@ -1,8 +1,8 @@ /* PresetHandler.cs $
-
- This file is part of the HandBrake source code.
- Homepage: <http://handbrake.fr>.
- It may be used under the terms of the GNU General Public License. */
+ This file is part of the HandBrake source code.
+ Homepage: <http://handbrake.fr>.
+ It may be used under the terms of the GNU General Public License. */
+
namespace Handbrake.Presets
{
using System;
@@ -10,31 +10,59 @@ namespace Handbrake.Presets using System.Diagnostics;
using System.Drawing;
using System.IO;
+ using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Xml.Serialization;
+ /// <summary>
+ /// The Preset Handler Class
+ /// </summary>
public class PresetsHandler
{
- private List<Preset> Presets = new List<Preset>();
- private List<Preset> UserPresets = new List<Preset>();
- private static readonly XmlSerializer Ser = new XmlSerializer(typeof (List<Preset>));
+ /// <summary>
+ /// The User Preset file
+ /// </summary>
+ private readonly string userPresetFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\user_presets.xml";
+
+ /// <summary>
+ /// The Built In Presets File
+ /// </summary>
+ private readonly string hbPresetFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\presets.xml";
- private readonly string UserPresetFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
- "\\HandBrake\\user_presets.xml";
+ /// <summary>
+ /// XML Serializer
+ /// </summary>
+ private static readonly XmlSerializer Ser = new XmlSerializer(typeof(List<Preset>));
+
+ /// <summary>
+ /// A List of built-in presets
+ /// </summary>
+ private List<Preset> presets = new List<Preset>();
- private readonly string HbPresetFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
- "\\HandBrake\\presets.xml";
+ /// <summary>
+ /// A List of user presets
+ /// </summary>
+ private List<Preset> userPresets = new List<Preset>();
/// <summary>
/// Add a new preset to the system
/// </summary>
- /// <param name="presetName">String, The name of the new preset</param>
- /// <param name="query">String, the CLI query for the new preset</param>
- /// <param name="pictureSettings"> Bool, store crop/picture sizes in the Presets</param>
+ /// <param name="presetName">
+ /// String, The name of the new preset
+ /// </param>
+ /// <param name="query">
+ /// String, the CLI query for the new preset
+ /// </param>
+ /// <param name="pictureSettings">
+ /// Bool, store crop/picture sizes in the Presets
+ /// </param>
+ /// <returns>
+ /// The add.
+ /// </returns>
public bool Add(string presetName, string query, bool pictureSettings)
{
- if (CheckIfPresetExists(presetName) == false)
+ if (this.CheckIfPresetExists(presetName) == false)
{
Preset newPreset = new Preset
{
@@ -43,8 +71,8 @@ namespace Handbrake.Presets PictureSettings = pictureSettings,
Version = Properties.Settings.Default.hb_version
};
- UserPresets.Add(newPreset);
- UpdatePresetFiles();
+ this.userPresets.Add(newPreset);
+ this.UpdatePresetFiles();
return true;
}
return false;
@@ -60,28 +88,28 @@ namespace Handbrake.Presets List<Preset> newUserPresets = new List<Preset>();
// Built In Presets
- foreach (Preset item in Presets)
+ foreach (Preset item in this.presets)
{
if (item.Name != name)
{
newPresets.Add(item);
}
}
- Presets = newPresets;
+ this.presets = newPresets;
// User Presets
- foreach (Preset item in UserPresets)
+ foreach (Preset item in this.userPresets)
{
if (item.Name != name)
{
newUserPresets.Add(item);
}
}
- UserPresets = newUserPresets;
+ this.userPresets = newUserPresets;
// Rebuild the UserPresets.xml file
- UpdatePresetFiles();
- UpdatePresetFiles();
+ this.UpdatePresetFiles();
+ this.UpdatePresetFiles();
}
/// <summary>
@@ -89,8 +117,8 @@ namespace Handbrake.Presets /// </summary>
public void RemoveBuiltInPresets()
{
- Presets.Clear();
- UpdatePresetFiles();
+ this.presets.Clear();
+ this.UpdatePresetFiles();
}
/// <summary>
@@ -102,15 +130,18 @@ namespace Handbrake.Presets public void Update(string presetName, string query, bool pictureSettings)
{
// User Presets
- foreach (Preset item in UserPresets)
+ foreach (Preset item in this.userPresets)
{
if (item.Name == presetName)
{
item.Query = query;
item.PictureSettings = pictureSettings;
- MessageBox.Show("Changes to \"" + presetName + "\" Saved", "Success", MessageBoxButtons.OK,
+ MessageBox.Show(
+ "Changes to \"" + presetName + "\" Saved",
+ "Success",
+ MessageBoxButtons.OK,
MessageBoxIcon.Information);
- UpdatePresetFiles();
+ this.UpdatePresetFiles();
}
}
}
@@ -123,32 +154,25 @@ namespace Handbrake.Presets public Preset GetPreset(string name)
{
// Built In Presets
- foreach (Preset item in Presets)
+ foreach (Preset item in this.presets)
{
if (item.Name == name)
return item;
}
// User Presets
- foreach (Preset item in UserPresets)
- {
- if (item.Name == name)
- return item;
- }
-
- return null;
+ return this.userPresets.FirstOrDefault(item => item.Name == name);
}
/// <summary>
- /// Reads the CLI's CLI output format and load's them into the preset List<Preset>
+ /// Reads the CLI's CLI output format and load's them into the preset List Preset
/// </summary>
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);
+ string strCmdLine = String.Format(@"cmd /c """"{0}"" --preset-list >""{1}"" 2>&1""", handbrakeCLIPath, presetsPath);
ProcessStartInfo hbGetPresets = new ProcessStartInfo("CMD.exe", strCmdLine)
{
@@ -163,7 +187,7 @@ namespace Handbrake.Presets }
// Clear the current built in Presets and now parse the tempory Presets file.
- Presets.Clear();
+ this.presets.Clear();
if (File.Exists(presetsPath))
{
@@ -194,7 +218,7 @@ namespace Handbrake.Presets Version = Properties.Settings.Default.hb_version,
PictureSettings = pic
};
- Presets.Add(newPreset);
+ this.presets.Add(newPreset);
}
}
presetInput.Close();
@@ -202,7 +226,81 @@ namespace Handbrake.Presets }
// Finally, Create a new or update the current Presets.xml file
- UpdatePresetFiles();
+ this.UpdatePresetFiles();
+ }
+
+ /// <summary>
+ /// Setup the frmMain preset panel
+ /// </summary>
+ /// <param name="presetPanel">The Preset Panel from the Main Window</param>
+ public void GetPresetPanel(ref TreeView presetPanel)
+ {
+ this.LoadPresetData();
+ presetPanel.Nodes.Clear();
+ string category = string.Empty;
+ TreeNode rootNode = null;
+
+ if (this.presets.Count != 0) // Built In Presets
+ {
+ foreach (Preset preset in this.presets)
+ {
+ if (preset.Category != category)
+ {
+ rootNode = new TreeNode(preset.Category);
+ presetPanel.Nodes.Add(rootNode);
+ category = preset.Category;
+ }
+
+ if (preset.Category == category && rootNode != null)
+ rootNode.Nodes.Add(preset.Name);
+ }
+ }
+
+ rootNode = null;
+ category = null;
+ foreach (Preset preset in this.userPresets) // User Presets
+ {
+ if (preset.Category != category && preset.Category != string.Empty)
+ {
+ rootNode = new TreeNode(preset.Category) {ForeColor = Color.Black};
+ presetPanel.Nodes.Add(rootNode);
+ category = preset.Category;
+ }
+
+ if (preset.Category == category && rootNode != null)
+ rootNode.Nodes.Add(new TreeNode(preset.Name) {ForeColor = Color.Black});
+ else
+ presetPanel.Nodes.Add(new TreeNode(preset.Name) {ForeColor = Color.Black});
+ }
+ }
+
+ /// <summary>
+ /// Check if the user preset "name" exists in UserPresets list.
+ /// </summary>
+ /// <param name="name">Name of the preset</param>
+ /// <returns>true if found</returns>
+ public bool CheckIfUserPresetExists(string name)
+ {
+ return name != string.Empty && this.userPresets.Any(item => item.Name == name);
+ }
+
+ /// <summary>
+ /// Check if the built in Presets stored are not out of date.
+ /// Update them if they are.
+ /// </summary>
+ /// <returns>true if out of date</returns>
+ public bool CheckIfPresetsAreOutOfDate()
+ {
+ this.LoadPresetData();
+ // Update built-in Presets if the built-in Presets belong to an older version.
+ if (this.presets.Count != 0)
+ if (this.presets[0].Version != Properties.Settings.Default.hb_version)
+ {
+ this.UpdateBuiltInPresets();
+ return true;
+ }
+
+ return false;
}
/// <summary>
@@ -212,15 +310,15 @@ namespace Handbrake.Presets private void LoadPresetData()
{
// First clear the Presets arraylists
- Presets.Clear();
- UserPresets.Clear();
+ this.presets.Clear();
+ this.userPresets.Clear();
try
{
// Load in the users Presets from UserPresets.xml
- if (File.Exists(HbPresetFile))
+ if (File.Exists(this.hbPresetFile))
{
- using (FileStream strm = new FileStream(HbPresetFile, FileMode.Open, FileAccess.Read))
+ using (FileStream strm = new FileStream(this.hbPresetFile, FileMode.Open, FileAccess.Read))
{
if (strm.Length != 0)
{
@@ -228,7 +326,7 @@ namespace Handbrake.Presets if (list != null)
foreach (Preset preset in list)
- Presets.Add(preset);
+ this.presets.Add(preset);
}
}
}
@@ -237,17 +335,19 @@ namespace Handbrake.Presets {
MessageBox.Show(
"HandBrakes preset file appears to have been corrupted. This file will now be re-generated!\n" +
- "If the problem presists, please delete the file: \n\n" + HbPresetFile, "Error",
- MessageBoxButtons.OK, MessageBoxIcon.Error);
- UpdateBuiltInPresets();
+ "If the problem presists, please delete the file: \n\n" + this.hbPresetFile,
+ "Error",
+ MessageBoxButtons.OK,
+ MessageBoxIcon.Error);
+ this.UpdateBuiltInPresets();
}
try
{
// Load in the users Presets from UserPresets.xml
- if (File.Exists(UserPresetFile))
+ if (File.Exists(this.userPresetFile))
{
- using (FileStream strm = new FileStream(UserPresetFile, FileMode.Open, FileAccess.Read))
+ using (FileStream strm = new FileStream(this.userPresetFile, FileMode.Open, FileAccess.Read))
{
if (strm.Length != 0)
{
@@ -255,7 +355,7 @@ namespace Handbrake.Presets if (list != null)
foreach (Preset preset in list)
- UserPresets.Add(preset);
+ this.userPresets.Add(preset);
}
}
}
@@ -266,83 +366,40 @@ namespace Handbrake.Presets "Your User presets file appears to have been corrupted.\n" +
"Your presets will not be loaded. You may need to re-create your presets.\n\n" +
"Your user presets file has been renamed to 'user_presets.xml.old' and is located in:\n " +
- Path.GetDirectoryName(UserPresetFile) + "\n" +
- "You may be able to recover some presets if you know the XML language.", "Error",
- MessageBoxButtons.OK, MessageBoxIcon.Error);
+ Path.GetDirectoryName(this.userPresetFile) + "\n" +
+ "You may be able to recover some presets if you know the XML language.",
+ "Error",
+ MessageBoxButtons.OK,
+ MessageBoxIcon.Error);
// Recover from Error.
- if (File.Exists(UserPresetFile))
+ if (File.Exists(this.userPresetFile))
{
- string disabledFile = UserPresetFile + ".old";
+ string disabledFile = this.userPresetFile + ".old";
if (File.Exists(disabledFile))
File.Delete(disabledFile);
- File.Move(UserPresetFile, disabledFile);
+ File.Move(this.userPresetFile, disabledFile);
}
}
}
/// <summary>
- /// Setup the frmMain preset panel
- /// </summary>
- /// <param name="presetPanel"></param>
- public void GetPresetPanel(ref TreeView presetPanel)
- {
- this.LoadPresetData();
- presetPanel.Nodes.Clear();
- string category = string.Empty;
- TreeNode rootNode = null;
-
- if (Presets.Count != 0) // Built In Presets
- {
- foreach (Preset preset in Presets)
- {
- if (preset.Category != category)
- {
- rootNode = new TreeNode(preset.Category);
- presetPanel.Nodes.Add(rootNode);
- category = preset.Category;
- }
-
- if (preset.Category == category && rootNode != null)
- rootNode.Nodes.Add(preset.Name);
- }
- }
-
- rootNode = null;
- category = null;
- foreach (Preset preset in UserPresets) // User Presets
- {
- if (preset.Category != category && preset.Category != null)
- {
- rootNode = new TreeNode(preset.Category) {ForeColor = Color.Black};
- presetPanel.Nodes.Add(rootNode);
- category = preset.Category;
- }
-
- if (preset.Category == category && rootNode != null)
- rootNode.Nodes.Add(new TreeNode(preset.Name) {ForeColor = Color.Black});
- else
- presetPanel.Nodes.Add(new TreeNode(preset.Name) {ForeColor = Color.Black});
- }
- }
-
- /// <summary>
/// Update the preset files
/// </summary>
private void UpdatePresetFiles()
{
try
{
- using (FileStream strm = new FileStream(HbPresetFile, FileMode.Create, FileAccess.Write))
+ using (FileStream strm = new FileStream(this.hbPresetFile, FileMode.Create, FileAccess.Write))
{
- Ser.Serialize(strm, Presets);
+ Ser.Serialize(strm, this.presets);
strm.Close();
strm.Dispose();
}
- using (FileStream strm = new FileStream(UserPresetFile, FileMode.Create, FileAccess.Write))
+ using (FileStream strm = new FileStream(this.userPresetFile, FileMode.Create, FileAccess.Write))
{
- Ser.Serialize(strm, UserPresets);
+ Ser.Serialize(strm, this.userPresets);
strm.Close();
strm.Dispose();
}
@@ -351,74 +408,32 @@ namespace Handbrake.Presets {
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);
+ exc,
+ "Error",
+ MessageBoxButtons.OK,
+ MessageBoxIcon.Hand);
}
}
/// <summary>
/// Check if the preset "name" exists in either Presets or UserPresets lists.
/// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
+ /// <param name="name">Name of the preset</param>
+ /// <returns>True if found</returns>
private bool 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 UserPresets)
+ foreach (Preset item in this.presets)
{
if (item.Name == name)
return true;
}
- return false;
- }
-
- /// <summary>
- /// Check if the user preset "name" exists in UserPresets list.
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- public bool CheckIfUserPresetExists(string name)
- {
- if (name == string.Empty)
- return false;
-
// User Presets
- foreach (Preset item in UserPresets)
- {
- if (item.Name == name)
- return true;
- }
-
- return false;
- }
-
- /// <summary>
- /// Check if the built in Presets stored are not out of date.
- /// Update them if they are.
- /// </summary>
- /// <returns></returns>
- public bool CheckIfPresetsAreOutOfDate()
- {
- LoadPresetData();
- // Update built-in Presets if the built-in Presets belong to an older version.
- if (Presets.Count != 0)
- if (Presets[0].Version != Properties.Settings.Default.hb_version)
- {
- UpdateBuiltInPresets();
- return true;
- }
-
- return false;
+ return this.userPresets.Any(item => item.Name == name);
}
}
}
\ No newline at end of file |