2 using System.Collections.Generic;
\r
4 using System.Windows.Forms;
\r
6 using System.Text.RegularExpressions;
\r
7 using System.Diagnostics;
\r
9 namespace Handbrake.Functions
\r
11 public class Presets
\r
13 List<string> presets = new List<string>();
\r
14 List<string> user_presets = new List<string>();
\r
17 /// Add a new preset to the system
\r
19 /// <param name="presetName">String, The name of the new preset</param>
\r
20 /// <param name="query">String, the CLI query for the new preset</param>
\r
21 public Boolean addPreset(string presetName, string query)
\r
23 if (checkIfPresetExists(presetName) == false)
\r
25 String preset = "+ " + presetName + ": " + query;
\r
26 user_presets.Add(preset);
\r
27 addPresetToFile(preset);
\r
32 MessageBox.Show("Sorry, that preset name already exists. Please choose another!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
38 /// Remove a preset with a given name from either the built in or user preset list.
\r
40 /// <param name="name">String, the preset name</param>
\r
41 public void remove(string name)
\r
43 List<string> newPresets = new List<string>();
\r
44 List<string> newUserPresets = new List<string>();
\r
47 foreach (string item in presets)
\r
49 string x = item.Replace("+ ", "");
\r
50 Regex r = new Regex("(: )"); // Split on hyphens.
\r
51 string[] presetName = r.Split(x);
\r
52 if (presetName[0] != name)
\r
53 newPresets.Add(item);
\r
57 foreach (string item in user_presets)
\r
59 string x = item.Replace("+ ", "");
\r
60 Regex r = new Regex("(: )"); // Split on hyphens.
\r
61 string[] presetName = r.Split(x);
\r
62 if (presetName[0] != name)
\r
63 newUserPresets.Add(item);
\r
66 // Now, Update the presets.dat and user_presets.dat file with the new items.
\r
67 string userPresets = Application.StartupPath.ToString() + "\\user_presets.dat";
\r
68 string presetsFile = Application.StartupPath.ToString() + "\\presets.dat";
\r
70 // Rebuild the presets.dat file
\r
71 StreamWriter line = new StreamWriter(presetsFile);
\r
72 foreach (string item in newPresets)
\r
74 line.WriteLine("+ " + item);
\r
79 // Rebuild the user_presets.dat file
\r
80 line = new StreamWriter(userPresets);
\r
81 foreach (string item in newUserPresets)
\r
83 line.WriteLine("+ " + item);
\r
90 /// Get a List of all the built in preset names.
\r
92 /// <returns>List<String> of preset names</returns>
\r
93 public List<string> getBuildInPresetNames()
\r
95 List<string> names = new List<string>();
\r
98 foreach (string item in presets)
\r
100 string x = item.Replace("+ ", "");
\r
101 Regex r = new Regex("(: )"); // Split on hyphens.
\r
102 string[] presetName = r.Split(x);
\r
103 names.Add(presetName[0]);
\r
110 /// Get a List of all the User preset names.
\r
112 /// <returns>List<String> of preset names</returns>
\r
113 public List<string> getUserPresetNames()
\r
115 List<string> names = new List<string>();
\r
118 foreach (string item in user_presets)
\r
120 string x = item.Replace("+ ", "");
\r
121 Regex r = new Regex("(: )");
\r
122 string[] presetName = r.Split(x);
\r
123 names.Add(presetName[0]);
\r
131 /// Return the CLI query for a preset name given in name
\r
133 /// <param name="name">String, The preset's name</param>
\r
134 /// <returns>String, the CLI query for the given preset name</returns>
\r
135 public string getCliForPreset(string name)
\r
137 // Built In Presets
\r
138 foreach (string item in presets)
\r
140 string x = item.Replace("+ ", "");
\r
141 Regex r = new Regex("(: )"); // Split on hyphens.
\r
142 string[] presetName = r.Split(x);
\r
143 if (presetName[0] == name)
\r
144 return presetName[2];
\r
148 foreach (string item in user_presets)
\r
150 string x = item.Replace("+ ", "");
\r
151 Regex r = new Regex("(: )"); // Split on hyphens.
\r
152 string[] presetName = r.Split(x);
\r
153 if (presetName[0] == name)
\r
154 return presetName[2];
\r
161 /// Update the presets.dat file with the latest version of HandBrake's presets from the CLI
\r
163 public void grabCLIPresets()
\r
165 string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");
\r
166 string presetsPath = Path.Combine(Application.StartupPath, "presets.dat");
\r
168 string strCmdLine = String.Format(@"cmd /c """"{0}"" --preset-list >""{1}"" 2>&1""", handbrakeCLIPath, presetsPath);
\r
170 ProcessStartInfo hbGetPresets = new ProcessStartInfo("CMD.exe", strCmdLine);
\r
171 hbGetPresets.WindowStyle = ProcessWindowStyle.Hidden;
\r
173 Process hbproc = Process.Start(hbGetPresets);
\r
174 hbproc.WaitForExit();
\r
180 /// Load in the preset data from presets.dat and user_presets.dat
\r
181 /// Load it into the 2 arraylist's presets and user_presets
\r
183 public void loadPresetFiles()
\r
185 // First clear the presets arraylists
\r
187 user_presets.Clear();
\r
189 // Load in the built in presets from presets.dat
\r
190 // We'll store them in the array in the format: presetName: ClI Query
\r
191 string filePath = Application.StartupPath.ToString() + "\\presets.dat";
\r
192 if (File.Exists(filePath))
\r
194 StreamReader presetInput = new StreamReader(filePath);
\r
195 while (!presetInput.EndOfStream)
\r
197 if ((char)presetInput.Peek() == '+')
\r
198 presets.Add(presetInput.ReadLine().Replace("+ ", ""));
\r
200 presetInput.ReadLine();
\r
202 presetInput.Close();
\r
203 presetInput.Dispose();
\r
206 // Load in the users presets from user_presets.dat
\r
207 filePath = Application.StartupPath.ToString() + "\\user_presets.dat";
\r
208 if (File.Exists(filePath))
\r
210 StreamReader presetInput = new StreamReader(filePath);
\r
211 while (!presetInput.EndOfStream)
\r
213 if ((char)presetInput.Peek() == '+')
\r
214 user_presets.Add(presetInput.ReadLine().Replace("+ ", ""));
\r
216 presetInput.ReadLine();
\r
218 presetInput.Close();
\r
219 presetInput.Dispose();
\r
223 // Add a single preset to user_presets.dat
\r
224 private void addPresetToFile(string preset)
\r
226 string userPresets = Application.StartupPath.ToString() + "\\user_presets.dat";
\r
229 // Create a StreamWriter and open the file
\r
230 StreamWriter line = File.AppendText(userPresets);
\r
232 // Generate and write the preset string to the file
\r
233 line.WriteLine(preset);
\r
235 // close the stream
\r
239 catch (Exception exc)
\r
241 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);
\r
245 // Check if a preset already exists in either the built in or user presets
\r
246 private Boolean checkIfPresetExists(string name)
\r
248 if (name == string.Empty)
\r
251 // Built In Presets
\r
252 foreach (string item in presets)
\r
254 string x = item.Replace("+ ", "");
\r
255 Regex r = new Regex("(: )"); // Split on hyphens.
\r
256 string[] presetName = r.Split(x);
\r
257 if (presetName[0] == name)
\r
262 foreach (string item in user_presets)
\r
264 string x = item.Replace("+ ", "");
\r
265 Regex r = new Regex("(: )"); // Split on hyphens.
\r
266 string[] presetName = r.Split(x);
\r
267 if (presetName[0] == name)
\r