2 using System.Collections.Generic;
\r
3 using System.Windows.Forms;
\r
5 using System.Text.RegularExpressions;
\r
6 using System.Diagnostics;
\r
7 using System.Xml.Serialization;
\r
9 namespace Handbrake.Presets
\r
11 public class PresetsHandler
\r
13 List<Preset> presets = new List<Preset>(); // Category+Level+Preset Name: Query
\r
14 List<Preset> user_presets = new List<Preset>(); // Preset Name: Query
\r
15 private static XmlSerializer ser = new XmlSerializer(typeof(List<Preset>));
\r
18 /// Add a new preset to the system
\r
20 /// <param name="presetName">String, The name of the new preset</param>
\r
21 /// <param name="query">String, the CLI query for the new preset</param>
\r
22 /// <param name="pictureSettings"> Bool, store crop/picture sizes in the presets</param>
\r
23 public Boolean addPreset(string presetName, string query, Boolean pictureSettings)
\r
25 if (checkIfPresetExists(presetName) == false)
\r
27 Preset newPreset = new Preset();
\r
28 newPreset.Name = presetName;
\r
29 newPreset.Query = query;
\r
30 newPreset.PictureSettings = pictureSettings;
\r
31 user_presets.Add(newPreset);
\r
32 updateUserPresetsFile();
\r
37 MessageBox.Show("Sorry, that preset name already exists. Please choose another!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
43 /// Remove a preset with a given name from either the built in or user preset list.
\r
45 /// <param name="name">String, the preset name</param>
\r
46 public void remove(string name)
\r
48 List<Preset> newPresets = new List<Preset>();
\r
49 List<Preset> newUserPresets = new List<Preset>();
\r
52 foreach (Preset item in presets)
\r
54 if (item.Name != name)
\r
56 newPresets.Add(item);
\r
59 presets = newPresets;
\r
62 foreach (Preset item in user_presets)
\r
64 if (item.Name != name)
\r
66 newUserPresets.Add(item);
\r
69 user_presets = newUserPresets;
\r
71 // Rebuild the user_presets.xml file
\r
72 updateUserPresetsFile();
\r
73 updatePresetsFile();
\r
77 /// Save changes to a given preset in the user preset list.
\r
79 /// <param name="presetName">String, The name of the new preset</param>
\r
80 /// <param name="query">String, the CLI query for the new preset</param>
\r
81 /// <param name="pictureSettings"> Bool, store crop/picture sizes in the preset</param>
\r
82 public void updatePreset(string presetName, string query, Boolean pictureSettings)
\r
85 foreach (Preset item in user_presets)
\r
87 if (item.Name == presetName)
\r
90 item.PictureSettings = pictureSettings;
\r
91 MessageBox.Show("Changes to \"" + presetName + "\" Saved", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
\r
92 updateUserPresetsFile();
\r
98 /// Get a List of all the built in preset names.
\r
100 /// <returns>List<String> of preset names</returns>
\r
101 public List<Preset> getBuildInPresets()
\r
107 /// Get a List of all the User preset names.
\r
109 /// <returns>List<String> of preset names</returns>
\r
110 public List<string> getUserPresetNames()
\r
112 List<string> names = new List<string>();
\r
115 foreach (Preset item in user_presets)
\r
117 names.Add(item.Name);
\r
124 /// Return the CLI query for a preset name given in name
\r
126 /// <param name="name">String, The preset's name</param>
\r
127 /// <returns>String, the CLI query for the given preset name</returns>
\r
128 public Preset getPreset(string name)
\r
130 // Built In Presets
\r
131 foreach (Preset item in presets)
\r
133 if (item.Name == name)
\r
138 foreach (Preset item in user_presets)
\r
140 if (item.Name == name)
\r
148 /// Reads the CLI's CLI output format and load's them into the preset List<Preset>
\r
150 public void updateBuiltInPresets()
\r
152 // Create a new tempory file and execute the CLI to get the built in presets.
\r
153 string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");
\r
154 string presetsPath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");
\r
156 string strCmdLine = String.Format(@"cmd /c """"{0}"" --preset-list >""{1}"" 2>&1""", handbrakeCLIPath, presetsPath);
\r
158 ProcessStartInfo hbGetPresets = new ProcessStartInfo("CMD.exe", strCmdLine);
\r
159 hbGetPresets.WindowStyle = ProcessWindowStyle.Hidden;
\r
161 Process hbproc = Process.Start(hbGetPresets);
\r
162 hbproc.WaitForExit();
\r
166 // Clear the current built in presets and now parse the tempory presets file.
\r
168 string filePath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");
\r
169 if (File.Exists(filePath))
\r
171 StreamReader presetInput = new StreamReader(filePath);
\r
173 string category = String.Empty;
\r
174 string level_1_category = String.Empty;
\r
176 while (!presetInput.EndOfStream)
\r
178 string line = presetInput.ReadLine();
\r
179 if (line.Contains("<") && !line.Contains("<<"))
\r
182 category = line.Replace("<", "").Trim();
\r
183 level_1_category = category;
\r
186 if (line.Contains("<<"))
\r
189 category = line.Replace("<<", "").Trim();
\r
192 if (line.Trim().Contains(">>"))
\r
195 category = level_1_category;
\r
198 if (line.Contains("+"))
\r
200 Regex r = new Regex("(: )"); // Split on hyphens.
\r
201 string[] presetName = r.Split(line);
\r
203 Preset newPreset = new Preset();
\r
204 newPreset.Level = level;
\r
205 newPreset.Category = category;
\r
206 newPreset.Name = presetName[0].Replace("+", "").Trim();
\r
207 newPreset.Query = presetName[2];
\r
208 presets.Add(newPreset);
\r
211 presetInput.Close();
\r
212 presetInput.Dispose();
\r
215 // Finally, Create a new or update the current presets.xml file
\r
216 updatePresetsFile();
\r
220 /// Load in the preset data from presets.xml and user_presets.xml
\r
221 /// Load it into the 2 arraylist's presets and user_presets
\r
223 public void loadPresetData()
\r
225 // First clear the presets arraylists
\r
227 user_presets.Clear();
\r
229 // Load in the users presets from user_presets.xml
\r
230 string filePath = Application.StartupPath + "\\presets.xml";
\r
231 if (File.Exists(filePath))
\r
233 using (FileStream strm = new FileStream(filePath, FileMode.Open, FileAccess.Read))
\r
235 if (strm.Length != 0)
\r
237 List<Preset> list = ser.Deserialize(strm) as List<Preset>;
\r
239 foreach (Preset preset in list)
\r
240 presets.Add(preset);
\r
245 // Load in the users presets from user_presets.xml
\r
246 filePath = Application.StartupPath + "\\user_presets.xml";
\r
247 if (File.Exists(filePath))
\r
249 using (FileStream strm = new FileStream(filePath, FileMode.Open, FileAccess.Read))
\r
251 if (strm.Length != 0)
\r
253 List<Preset> list = ser.Deserialize(strm) as List<Preset>;
\r
255 foreach (Preset preset in list)
\r
256 user_presets.Add(preset);
\r
263 /// Updates the presets.xml file which contains the built in presets
\r
264 /// It takes the List of Presets and converts them into XML which is stored in presets.xml
\r
266 private void updatePresetsFile()
\r
268 string userPresets = Application.StartupPath + "\\presets.xml";
\r
271 using (FileStream strm = new FileStream(userPresets, FileMode.Create, FileAccess.Write))
\r
273 ser.Serialize(strm, presets);
\r
278 catch (Exception exc)
\r
280 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
285 /// Updates the user_presets.xml file which contains the built in presets
\r
286 /// It takes the List of Presets and converts them into XML which is stored in user_presets.xml
\r
288 private void updateUserPresetsFile()
\r
290 string userPresets = Application.StartupPath + "\\user_presets.xml";
\r
293 using (FileStream strm = new FileStream(userPresets, FileMode.Create, FileAccess.Write))
\r
295 ser.Serialize(strm, user_presets);
\r
300 catch (Exception exc)
\r
302 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
307 /// Check if the preset "name" exists in either presets or user_presets lists.
\r
309 /// <param name="name"></param>
\r
310 /// <returns></returns>
\r
311 private Boolean checkIfPresetExists(string name)
\r
313 if (name == string.Empty)
\r
316 // Built In Presets
\r
317 foreach (Preset item in presets)
\r
319 if (item.Name == name)
\r
324 foreach (Preset item in user_presets)
\r
326 if (item.Name == name)
\r
334 /// Check if the user preset "name" exists in user_presets list.
\r
336 /// <param name="name"></param>
\r
337 /// <returns></returns>
\r
338 public Boolean checkIfUserPresetExists(string name)
\r
340 if (name == string.Empty)
\r
344 foreach (Preset item in user_presets)
\r
346 if (item.Name == name)
\r