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>();
\r
16 List<Preset> user_presets = new List<Preset>();
\r
17 private static readonly XmlSerializer ser = new XmlSerializer(typeof(List<Preset>));
\r
18 String userPresetFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\user_presets.xml";
\r
19 string hbPresetFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\presets.xml";
\r
22 /// Add a new preset to the system
\r
24 /// <param name="presetName">String, The name of the new preset</param>
\r
25 /// <param name="query">String, the CLI query for the new preset</param>
\r
26 /// <param name="pictureSettings"> Bool, store crop/picture sizes in the presets</param>
\r
27 public Boolean addPreset(string presetName, string query, Boolean pictureSettings)
\r
29 if (checkIfPresetExists(presetName) == false)
\r
31 Preset newPreset = new Preset { Name = presetName, Query = query, PictureSettings = pictureSettings, Version = Properties.Settings.Default.hb_version};
\r
32 user_presets.Add(newPreset);
\r
33 updateUserPresetsFile();
\r
36 MessageBox.Show("Sorry, that preset name already exists. Please choose another!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
41 /// Remove a preset with a given name from either the built in or user preset list.
\r
43 /// <param name="name">String, the preset name</param>
\r
44 public void remove(string name)
\r
46 List<Preset> newPresets = new List<Preset>();
\r
47 List<Preset> newUserPresets = new List<Preset>();
\r
50 foreach (Preset item in presets)
\r
52 if (item.Name != name)
\r
54 newPresets.Add(item);
\r
57 presets = newPresets;
\r
60 foreach (Preset item in user_presets)
\r
62 if (item.Name != name)
\r
64 newUserPresets.Add(item);
\r
67 user_presets = newUserPresets;
\r
69 // Rebuild the user_presets.xml file
\r
70 updateUserPresetsFile();
\r
71 updatePresetsFile();
\r
75 /// Save changes to a given preset in the user preset list.
\r
77 /// <param name="presetName">String, The name of the new preset</param>
\r
78 /// <param name="query">String, the CLI query for the new preset</param>
\r
79 /// <param name="pictureSettings"> Bool, store crop/picture sizes in the preset</param>
\r
80 public void updatePreset(string presetName, string query, Boolean pictureSettings)
\r
83 foreach (Preset item in user_presets)
\r
85 if (item.Name == presetName)
\r
88 item.PictureSettings = pictureSettings;
\r
89 MessageBox.Show("Changes to \"" + presetName + "\" Saved", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
\r
90 updateUserPresetsFile();
\r
96 /// Return the CLI query for a preset name given in name
\r
98 /// <param name="name">String, The preset's name</param>
\r
99 /// <returns>String, the CLI query for the given preset name</returns> not
\r
100 public Preset getPreset(string name)
\r
102 // Built In Presets
\r
103 foreach (Preset item in presets)
\r
105 if (item.Name == name)
\r
110 foreach (Preset item in user_presets)
\r
112 if (item.Name == name)
\r
120 /// Reads the CLI's CLI output format and load's them into the preset List<Preset>
\r
122 public void updateBuiltInPresets()
\r
124 // Create a new tempory file and execute the CLI to get the built in presets.
\r
125 string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");
\r
126 string presetsPath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");
\r
127 string strCmdLine = String.Format(@"cmd /c """"{0}"" --preset-list >""{1}"" 2>&1""", handbrakeCLIPath, presetsPath);
\r
129 ProcessStartInfo hbGetPresets = new ProcessStartInfo("CMD.exe", strCmdLine) { WindowStyle = ProcessWindowStyle.Hidden };
\r
130 Process hbproc = Process.Start(hbGetPresets);
\r
131 if (hbproc != null)
\r
133 hbproc.WaitForExit();
\r
138 // Clear the current built in presets and now parse the tempory presets file.
\r
140 string filePath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");
\r
142 if (File.Exists(filePath))
\r
144 StreamReader presetInput = new StreamReader(filePath);
\r
147 string category = String.Empty;
\r
148 string level_1_category = String.Empty;
\r
150 while (!presetInput.EndOfStream)
\r
152 string line = presetInput.ReadLine();
\r
153 if (line.Contains("<") && !line.Contains("<<")) // Found the beginning of a preset block
\r
156 category = line.Replace("<", "").Trim();
\r
157 level_1_category = category;
\r
160 if (line.Contains("<<")) // found a sub preset block
\r
163 category = line.Replace("<<", "").Trim();
\r
166 if (line.Trim().Contains(">>")) // End of sub preset block
\r
169 category = level_1_category;
\r
172 if (line.Contains("+")) // A Preset
\r
174 Regex r = new Regex("(: )"); // Split on hyphens.
\r
175 string[] presetName = r.Split(line);
\r
177 Preset newPreset = new Preset
\r
180 Category = category,
\r
181 TopCategory = level_1_category,
\r
182 Name = presetName[0].Replace("+", "").Trim(),
\r
183 Query = presetName[2],
\r
184 Version = Properties.Settings.Default.hb_version
\r
186 presets.Add(newPreset);
\r
189 presetInput.Close();
\r
190 presetInput.Dispose();
\r
193 // Finally, Create a new or update the current presets.xml file
\r
194 updatePresetsFile();
\r
198 /// Load in the preset data from presets.xml and user_presets.xml
\r
199 /// Load it into the 2 arraylist's presets and user_presets
\r
201 private void loadPresetData()
\r
203 // First clear the presets arraylists
\r
205 user_presets.Clear();
\r
207 // Load in the users presets from user_presets.xml
\r
208 if (File.Exists(hbPresetFile))
\r
210 using (FileStream strm = new FileStream(hbPresetFile, FileMode.Open, FileAccess.Read))
\r
212 if (strm.Length != 0)
\r
214 List<Preset> list = ser.Deserialize(strm) as List<Preset>;
\r
217 foreach (Preset preset in list)
\r
218 presets.Add(preset);
\r
223 // Load in the users presets from user_presets.xml
\r
224 if (File.Exists(userPresetFile))
\r
226 using (FileStream strm = new FileStream(userPresetFile, FileMode.Open, FileAccess.Read))
\r
228 if (strm.Length != 0)
\r
230 List<Preset> list = ser.Deserialize(strm) as List<Preset>;
\r
233 foreach (Preset preset in list)
\r
234 user_presets.Add(preset);
\r
241 /// Setup the frmMain preset panel
\r
243 /// <param name="presetPanel"></param>
\r
244 public void getPresetPanel(ref TreeView presetPanel)
\r
246 this.loadPresetData();
\r
247 presetPanel.Nodes.Clear();
\r
249 if (presets.Count != 0)
\r
251 string category = presets[0].Category;
\r
252 TreeNode rootNode = new TreeNode(presets[0].Category);
\r
253 TreeNode childNode = null;
\r
254 Boolean addChildNode = false;
\r
256 foreach (Preset preset in presets)
\r
259 if (preset.Category == category && preset.TopCategory == category)
\r
260 rootNode.Nodes.Add(preset.Name);
\r
261 else if (preset.Category != category && preset.TopCategory == category) // 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)// Deal with changing root nodes
\r
271 // If we find there are child nodes, add them to the current root node set before adding the rootnode to the panel.
\r
274 rootNode.Nodes.Add(childNode);
\r
276 addChildNode = false;
\r
278 // Add the current rootnodes to the panel, and prepare for a new category of presets
\r
279 presetPanel.Nodes.Add(rootNode);
\r
280 rootNode = new TreeNode(preset.Category);
\r
281 rootNode.Nodes.Add(preset.Name);
\r
283 category = preset.Category;
\r
286 presetPanel.Nodes.Add(rootNode); // Add the final set of nodes
\r
290 foreach (Preset preset in user_presets)
\r
292 TreeNode preset_treeview = new TreeNode(preset.Name) { ForeColor = Color.Black };
\r
293 presetPanel.Nodes.Add(preset_treeview);
\r
298 /// Updates the presets.xml file which contains the built in presets
\r
299 /// It takes the List of Presets and converts them into XML which is stored in presets.xml
\r
301 private void updatePresetsFile()
\r
303 string userPresets = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\presets.xml";
\r
306 using (FileStream strm = new FileStream(userPresets, FileMode.Create, FileAccess.Write))
\r
308 ser.Serialize(strm, presets);
\r
313 catch (Exception exc)
\r
315 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
320 /// Updates the user_presets.xml file which contains the built in presets
\r
321 /// It takes the List of Presets and converts them into XML which is stored in user_presets.xml
\r
323 private void updateUserPresetsFile()
\r
327 using (FileStream strm = new FileStream(userPresetFile, FileMode.Create, FileAccess.Write))
\r
329 ser.Serialize(strm, user_presets);
\r
334 catch (Exception exc)
\r
336 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
341 /// Check if the preset "name" exists in either presets or user_presets lists.
\r
343 /// <param name="name"></param>
\r
344 /// <returns></returns>
\r
345 private Boolean checkIfPresetExists(string name)
\r
347 if (name == string.Empty)
\r
350 // Built In Presets
\r
351 foreach (Preset item in presets)
\r
353 if (item.Name == name)
\r
358 foreach (Preset item in user_presets)
\r
360 if (item.Name == name)
\r
368 /// Check if the user preset "name" exists in user_presets list.
\r
370 /// <param name="name"></param>
\r
371 /// <returns></returns>
\r
372 public Boolean checkIfUserPresetExists(string name)
\r
374 if (name == string.Empty)
\r
378 foreach (Preset item in user_presets)
\r
380 if (item.Name == name)
\r
388 /// Check if the built in presets stored are not out of date.
\r
389 /// Update them if they are.
\r
391 /// <returns></returns>
\r
392 public Boolean checkIfPresetsAreOutOfDate()
\r
395 // Update built-in presets if the built-in presets belong to an older version.
\r
396 if (presets.Count != 0)
\r
397 if (presets[0].Version != Properties.Settings.Default.hb_version)
\r
399 updateBuiltInPresets();
\r