2 using System.Collections.Generic;
\r
3 using System.Drawing;
\r
4 using System.Windows.Forms;
\r
6 using System.Text.RegularExpressions;
\r
7 using System.Diagnostics;
\r
8 using System.Xml.Serialization;
\r
9 using System.Collections;
\r
11 namespace Handbrake.Presets
\r
13 public class PresetsHandler
\r
15 List<Preset> presets = new List<Preset>(); // Category+Level+Preset Name: Query
\r
16 List<Preset> user_presets = new List<Preset>(); // Preset Name: Query
\r
17 private static readonly XmlSerializer ser = new XmlSerializer(typeof(List<Preset>));
\r
20 /// Add a new preset to the system
\r
22 /// <param name="presetName">String, The name of the new preset</param>
\r
23 /// <param name="query">String, the CLI query for the new preset</param>
\r
24 /// <param name="pictureSettings"> Bool, store crop/picture sizes in the presets</param>
\r
25 public Boolean addPreset(string presetName, string query, Boolean pictureSettings)
\r
27 if (checkIfPresetExists(presetName) == false)
\r
29 Preset newPreset = new Preset { Name = presetName, Query = query, PictureSettings = pictureSettings };
\r
30 user_presets.Add(newPreset);
\r
31 updateUserPresetsFile();
\r
34 MessageBox.Show("Sorry, that preset name already exists. Please choose another!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
39 /// Remove a preset with a given name from either the built in or user preset list.
\r
41 /// <param name="name">String, the preset name</param>
\r
42 public void remove(string name)
\r
44 List<Preset> newPresets = new List<Preset>();
\r
45 List<Preset> newUserPresets = new List<Preset>();
\r
48 foreach (Preset item in presets)
\r
50 if (item.Name != name)
\r
52 newPresets.Add(item);
\r
55 presets = newPresets;
\r
58 foreach (Preset item in user_presets)
\r
60 if (item.Name != name)
\r
62 newUserPresets.Add(item);
\r
65 user_presets = newUserPresets;
\r
67 // Rebuild the user_presets.xml file
\r
68 updateUserPresetsFile();
\r
69 updatePresetsFile();
\r
73 /// Save changes to a given preset in the user preset list.
\r
75 /// <param name="presetName">String, The name of the new preset</param>
\r
76 /// <param name="query">String, the CLI query for the new preset</param>
\r
77 /// <param name="pictureSettings"> Bool, store crop/picture sizes in the preset</param>
\r
78 public void updatePreset(string presetName, string query, Boolean pictureSettings)
\r
81 foreach (Preset item in user_presets)
\r
83 if (item.Name == presetName)
\r
86 item.PictureSettings = pictureSettings;
\r
87 MessageBox.Show("Changes to \"" + presetName + "\" Saved", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
\r
88 updateUserPresetsFile();
\r
94 /// Return the CLI query for a preset name given in name
\r
96 /// <param name="name">String, The preset's name</param>
\r
97 /// <returns>String, the CLI query for the given preset name</returns> not
\r
98 public Preset getPreset(string name)
\r
100 // Built In Presets
\r
101 foreach (Preset item in presets)
\r
103 if (item.Name == name)
\r
108 foreach (Preset item in user_presets)
\r
110 if (item.Name == name)
\r
118 /// Reads the CLI's CLI output format and load's them into the preset List<Preset>
\r
120 public void updateBuiltInPresets()
\r
122 // Create a new tempory file and execute the CLI to get the built in presets.
\r
123 string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");
\r
124 string presetsPath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");
\r
125 string strCmdLine = String.Format(@"cmd /c """"{0}"" --preset-list >""{1}"" 2>&1""", handbrakeCLIPath, presetsPath);
\r
127 ProcessStartInfo hbGetPresets = new ProcessStartInfo("CMD.exe", strCmdLine) { WindowStyle = ProcessWindowStyle.Hidden };
\r
128 Process hbproc = Process.Start(hbGetPresets);
\r
129 if (hbproc != null)
\r
131 hbproc.WaitForExit();
\r
136 // Clear the current built in presets and now parse the tempory presets file.
\r
138 string filePath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");
\r
140 if (File.Exists(filePath))
\r
142 StreamReader presetInput = new StreamReader(filePath);
\r
145 string category = String.Empty;
\r
146 string level_1_category = String.Empty;
\r
148 while (!presetInput.EndOfStream)
\r
150 string line = presetInput.ReadLine();
\r
151 if (line.Contains("<") && !line.Contains("<<")) // Found the beginning of a preset block
\r
154 category = line.Replace("<", "").Trim();
\r
155 level_1_category = category;
\r
158 if (line.Contains("<<")) // found a sub preset block
\r
161 category = line.Replace("<<", "").Trim();
\r
164 if (line.Trim().Contains(">>")) // End of sub preset block
\r
167 category = level_1_category;
\r
170 if (line.Contains("+")) // A Preset
\r
172 Regex r = new Regex("(: )"); // Split on hyphens.
\r
173 string[] presetName = r.Split(line);
\r
175 Preset newPreset = new Preset
\r
178 Category = category,
\r
179 TopCategory = level_1_category,
\r
180 Name = presetName[0].Replace("+", "").Trim(),
\r
181 Query = presetName[2]
\r
183 presets.Add(newPreset);
\r
186 presetInput.Close();
\r
187 presetInput.Dispose();
\r
190 // Finally, Create a new or update the current presets.xml file
\r
191 updatePresetsFile();
\r
195 /// Load in the preset data from presets.xml and user_presets.xml
\r
196 /// Load it into the 2 arraylist's presets and user_presets
\r
198 private void loadPresetData()
\r
200 // First clear the presets arraylists
\r
202 user_presets.Clear();
\r
204 // Load in the users presets from user_presets.xml
\r
205 string filePath = Application.StartupPath + "\\presets.xml";
\r
206 if (File.Exists(filePath))
\r
208 using (FileStream strm = new FileStream(filePath, FileMode.Open, FileAccess.Read))
\r
210 if (strm.Length != 0)
\r
212 List<Preset> list = ser.Deserialize(strm) as List<Preset>;
\r
215 foreach (Preset preset in list)
\r
216 presets.Add(preset);
\r
221 // Load in the users presets from user_presets.xml
\r
222 filePath = Application.StartupPath + "\\user_presets.xml";
\r
223 if (File.Exists(filePath))
\r
225 using (FileStream strm = new FileStream(filePath, FileMode.Open, FileAccess.Read))
\r
227 if (strm.Length != 0)
\r
229 List<Preset> list = ser.Deserialize(strm) as List<Preset>;
\r
232 foreach (Preset preset in list)
\r
233 user_presets.Add(preset);
\r
240 /// Setup the frmMain preset panel
\r
242 /// <param name="presetPanel"></param>
\r
243 public void getPresetPanel(ref TreeView presetPanel)
\r
245 this.loadPresetData();
\r
246 presetPanel.Nodes.Clear();
\r
248 if (presets.Count != 0)
\r
250 string category = presets[0].Category;
\r
251 TreeNode rootNode = new TreeNode(presets[0].Category);
\r
252 TreeNode childNode = null;
\r
253 Boolean addChildNode = false;
\r
255 foreach (Preset preset in presets)
\r
258 if (preset.Category == category && preset.TopCategory == category)
\r
259 rootNode.Nodes.Add(preset.Name);
\r
260 else if (preset.Category != category && preset.TopCategory == category)
\r
261 // Deal with child nodes for that root
\r
263 if (childNode == null) // For the first child node
\r
264 childNode = new TreeNode(preset.Category);
\r
266 childNode.Nodes.Add(preset.Name);
\r
267 addChildNode = true;
\r
269 else if (preset.Category != category && preset.TopCategory != category && preset.Level == 1)
\r
270 // Deal with changing root nodes
\r
272 // If we find there are child nodes, add them to the current root node set before adding the rootnode to the panel.
\r
275 rootNode.Nodes.Add(childNode);
\r
277 addChildNode = false;
\r
279 // Add the current rootnodes to the panel, and prepare for a new category of presets
\r
280 presetPanel.Nodes.Add(rootNode);
\r
281 rootNode = new TreeNode(preset.Category);
\r
282 rootNode.Nodes.Add(preset.Name);
\r
284 category = preset.Category;
\r
287 presetPanel.Nodes.Add(rootNode); // Add the final set of nodes
\r
291 foreach (Preset preset in user_presets)
\r
293 TreeNode preset_treeview = new TreeNode(preset.Name) { ForeColor = Color.Black };
\r
294 presetPanel.Nodes.Add(preset_treeview);
\r
299 /// Updates the presets.xml file which contains the built in presets
\r
300 /// It takes the List of Presets and converts them into XML which is stored in presets.xml
\r
302 private void updatePresetsFile()
\r
304 string userPresets = Application.StartupPath + "\\presets.xml";
\r
307 using (FileStream strm = new FileStream(userPresets, FileMode.Create, FileAccess.Write))
\r
309 ser.Serialize(strm, presets);
\r
314 catch (Exception exc)
\r
316 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
321 /// Updates the user_presets.xml file which contains the built in presets
\r
322 /// It takes the List of Presets and converts them into XML which is stored in user_presets.xml
\r
324 private void updateUserPresetsFile()
\r
326 string userPresets = Application.StartupPath + "\\user_presets.xml";
\r
329 using (FileStream strm = new FileStream(userPresets, FileMode.Create, FileAccess.Write))
\r
331 ser.Serialize(strm, user_presets);
\r
336 catch (Exception exc)
\r
338 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
343 /// Check if the preset "name" exists in either presets or user_presets lists.
\r
345 /// <param name="name"></param>
\r
346 /// <returns></returns>
\r
347 private Boolean checkIfPresetExists(string name)
\r
349 if (name == string.Empty)
\r
352 // Built In Presets
\r
353 foreach (Preset item in presets)
\r
355 if (item.Name == name)
\r
360 foreach (Preset item in user_presets)
\r
362 if (item.Name == name)
\r
370 /// Check if the user preset "name" exists in user_presets list.
\r
372 /// <param name="name"></param>
\r
373 /// <returns></returns>
\r
374 public Boolean checkIfUserPresetExists(string name)
\r
376 if (name == string.Empty)
\r
380 foreach (Preset item in user_presets)
\r
382 if (item.Name == name)
\r