summaryrefslogtreecommitdiffstats
path: root/win/C#/Functions
diff options
context:
space:
mode:
authorsr55 <[email protected]>2008-07-02 19:01:36 +0000
committersr55 <[email protected]>2008-07-02 19:01:36 +0000
commit7dcf52379eb35e0a1553ecb6961b8376833e7603 (patch)
tree1b4a30881079b576f87a4228bde74bd86029182e /win/C#/Functions
parentd4d2fc14259eee2216106049c56f633504de58fc (diff)
WinGui:
- Better implementation of the presets bar. Code moved into it's own class. - Can now Remove Single or delete all built in presets. - User presets are no longer taged with "--" - CLI window no longer appears when grabbing the information for presets.dat from HandBrakeCLI.exe git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@1552 b64f7644-9d1e-0410-96f1-a4d463321fa5
Diffstat (limited to 'win/C#/Functions')
-rw-r--r--win/C#/Functions/CLI.cs13
-rw-r--r--win/C#/Functions/Presets.cs258
2 files changed, 261 insertions, 10 deletions
diff --git a/win/C#/Functions/CLI.cs b/win/C#/Functions/CLI.cs
index b0ad57a70..259397af3 100644
--- a/win/C#/Functions/CLI.cs
+++ b/win/C#/Functions/CLI.cs
@@ -116,10 +116,15 @@ namespace Handbrake.Functions
/// </summary>
public void grabCLIPresets()
{
- // Gets the presets from the CLI and stores them in presets.dat
- string appPath = Application.StartupPath.ToString() + "\\";
- string strCmdLine = "cmd /c " + '"' + '"' + appPath + "HandBrakeCLI.exe" + '"' + " --preset-list >" + '"' + appPath + "presets.dat" + '"' + " 2>&1" + '"';
- Process hbproc = Process.Start("CMD.exe", strCmdLine);
+ string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");
+ string presetsPath = Path.Combine(Application.StartupPath, "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();
diff --git a/win/C#/Functions/Presets.cs b/win/C#/Functions/Presets.cs
index 213a6ea8b..dda7f12b4 100644
--- a/win/C#/Functions/Presets.cs
+++ b/win/C#/Functions/Presets.cs
@@ -1,15 +1,261 @@
using System;
-using System.Collections.Generic;
+using System.Collections;
using System.Text;
+using System.Windows.Forms;
+using System.IO;
+using System.Text.RegularExpressions;
+
namespace Handbrake.Functions
{
- // This file is going to be used for all the preset stuff, when i get around to switching out to something that works a bit better.
-
- class Presets
+ public class Presets
{
- public void loadPresetPanel(frmMain mainWindow)
+ ArrayList presets = new ArrayList();
+ ArrayList user_presets = new ArrayList();
+
+ /// <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>
+ public Boolean addPreset(string presetName, string query)
+ {
+ if (checkIfPresetExists(presetName) == false)
+ {
+ String preset = "+ " + presetName + ": " + query;
+ user_presets.Add(preset);
+ addPresetToFile(preset);
+ return true;
+ }
+ else
+ {
+ MessageBox.Show("Sorry, that preset name already exists. Please choose another!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
+ return false;
+ }
+ }
+
+ /// <summary>
+ /// Remove a preset with a given name from either the built in or user preset list.
+ /// </summary>
+ /// <param name="name">String, the preset name</param>
+ public void remove(string name)
+ {
+ ArrayList newPresets = new ArrayList();
+ ArrayList newUserPresets = new ArrayList();
+
+ // Built In Presets
+ foreach (string item in presets)
+ {
+ string x = item.Replace("+ ", "");
+ Regex r = new Regex("(: )"); // Split on hyphens.
+ string[] presetName = r.Split(x);
+ if (presetName[0] != name)
+ newPresets.Add(item);
+ }
+
+ // User Presets
+ foreach (string item in user_presets)
+ {
+ string x = item.Replace("+ ", "");
+ Regex r = new Regex("(: )"); // Split on hyphens.
+ string[] presetName = r.Split(x);
+ if (presetName[0] != name)
+ newUserPresets.Add(item);
+ }
+
+ // Now, Update the presets.dat and user_presets.dat file with the new items.
+ string userPresets = Application.StartupPath.ToString() + "\\user_presets.dat";
+ string presetsFile = Application.StartupPath.ToString() + "\\presets.dat";
+
+ // Rebuild the presets.dat file
+ StreamWriter line = new StreamWriter(presetsFile);
+ foreach (string item in newPresets)
+ {
+ line.WriteLine("+ " + item);
+ }
+ line.Close();
+ line.Dispose();
+
+ // Rebuild the user_presets.dat file
+ line = new StreamWriter(userPresets);
+ foreach (string item in newUserPresets)
+ {
+ line.WriteLine("+ " + item);
+ }
+ line.Close();
+ line.Dispose();
+ }
+
+ /// <summary>
+ /// Count the number of presets there are.
+ /// </summary>
+ /// <returns>Integer</returns>
+ public int count()
+ {
+ int c = presets.Count;
+ int d = user_presets.Count;
+
+ return c + d;
+ }
+
+ /// <summary>
+ /// Get an Arraylist of all the preset names.
+ /// Includes both built in and user presets.
+ /// </summary>
+ /// <returns>Arraylist of preset names</returns>
+ public ArrayList getPresetNames()
+ {
+ ArrayList names = new ArrayList();
+
+ // Built In Presets
+ foreach (string item in presets)
+ {
+ string x = item.Replace("+ ", "");
+ Regex r = new Regex("(: )"); // Split on hyphens.
+ string[] presetName = r.Split(x);
+ names.Add(presetName[0]);
+
+ }
+
+ // User Presets
+ foreach (string item in user_presets)
+ {
+ string x = item.Replace("+ ", "");
+ Regex r = new Regex("(: )");
+ string[] presetName = r.Split(x);
+ names.Add(presetName[0]);
+
+ }
+
+ return names;
+ }
+
+ /// <summary>
+ /// Return the CLI query for a preset name given in name
+ /// </summary>
+ /// <param name="name">String, The preset's name</param>
+ /// <returns>String, the CLI query for the given preset name</returns>
+ public string getCliForPreset(string name)
+ {
+ // Built In Presets
+ foreach (string item in presets)
+ {
+ string x = item.Replace("+ ", "");
+ Regex r = new Regex("(: )"); // Split on hyphens.
+ string[] presetName = r.Split(x);
+ if (presetName[0] == name)
+ return presetName[2];
+ }
+
+ // User Presets
+ foreach (string item in user_presets)
+ {
+ string x = item.Replace("+ ", "");
+ Regex r = new Regex("(: )"); // Split on hyphens.
+ string[] presetName = r.Split(x);
+ if (presetName[0] == name)
+ return presetName[2];
+ }
+
+ return null;
+ }
+
+ /// <summary>
+ /// Load in the preset data from presets.dat and user_presets.dat
+ /// Load it into the 2 arraylist's presets and user_presets
+ /// </summary>
+ public void loadPresetFiles()
{
+ // First clear the presets arraylists
+ presets.Clear();
+ user_presets.Clear();
+
+ // Load in the built in presets from presets.dat
+ // We'll store them in the array in the format: presetName: ClI Query
+ string filePath = Application.StartupPath.ToString() + "\\presets.dat";
+ if (File.Exists(filePath))
+ {
+ StreamReader presetInput = new StreamReader(filePath);
+ while (!presetInput.EndOfStream)
+ {
+ if ((char)presetInput.Peek() == '+')
+ presets.Add(presetInput.ReadLine().Replace("+ ", ""));
+ else
+ presetInput.ReadLine();
+ }
+ presetInput.Close();
+ presetInput.Dispose();
+ }
+
+ // Load in the users presets from user_presets.dat
+ filePath = Application.StartupPath.ToString() + "\\user_presets.dat";
+ if (File.Exists(filePath))
+ {
+ StreamReader presetInput = new StreamReader(filePath);
+ while (!presetInput.EndOfStream)
+ {
+ if ((char)presetInput.Peek() == '+')
+ user_presets.Add(presetInput.ReadLine().Replace("+ ", ""));
+ else
+ presetInput.ReadLine();
+ }
+ presetInput.Close();
+ presetInput.Dispose();
+ }
+ }
+
+ // Add a single preset to user_presets.dat
+ private void addPresetToFile(string preset)
+ {
+ string userPresets = Application.StartupPath.ToString() + "\\user_presets.dat";
+ try
+ {
+ // Create a StreamWriter and open the file
+ StreamWriter line = File.AppendText(userPresets);
+
+ // Generate and write the preset string to the file
+ line.WriteLine(preset);
+
+ // close the stream
+ line.Close();
+ line.Dispose();
+ MessageBox.Show("Your profile has been sucessfully added.", "Status", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
+ }
+ 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 a preset already exists in either the built in or user presets
+ private Boolean checkIfPresetExists(string name)
+ {
+ if (name == "")
+ return true;
+
+ // Built In Presets
+ foreach (string item in presets)
+ {
+ string x = item.Replace("+ ", "");
+ Regex r = new Regex("(: )"); // Split on hyphens.
+ string[] presetName = r.Split(x);
+ if (presetName[0] == name)
+ return true;
+ }
+
+ // User Presets
+ foreach (string item in user_presets)
+ {
+ string x = item.Replace("+ ", "");
+ Regex r = new Regex("(: )"); // Split on hyphens.
+ string[] presetName = r.Split(x);
+ if (presetName[0] == name)
+ return true;
+ }
+
+ return false;
}
}
-}
+
+} \ No newline at end of file