1 /* PresetHandler.cs $
\r
3 This file is part of the HandBrake source code.
\r
4 Homepage: <http://handbrake.fr>.
\r
5 It may be used under the terms of the GNU General Public License. */
\r
7 using System.Collections.Generic;
\r
8 using System.Drawing;
\r
9 using System.Windows.Forms;
\r
11 using System.Text.RegularExpressions;
\r
12 using System.Diagnostics;
\r
13 using System.Xml.Serialization;
\r
15 namespace Handbrake.Presets
\r
17 public class PresetsHandler
\r
19 List<Preset> presets = new List<Preset>();
\r
20 List<Preset> user_presets = new List<Preset>();
\r
21 private static readonly XmlSerializer ser = new XmlSerializer(typeof(List<Preset>));
\r
22 string userPresetFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\user_presets.xml";
\r
23 string hbPresetFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\presets.xml";
\r
26 /// Add a new preset to the system
\r
28 /// <param name="presetName">String, The name of the new preset</param>
\r
29 /// <param name="query">String, the CLI query for the new preset</param>
\r
30 /// <param name="pictureSettings"> Bool, store crop/picture sizes in the presets</param>
\r
31 public Boolean addPreset(string presetName, string query, Boolean pictureSettings)
\r
33 if (checkIfPresetExists(presetName) == false)
\r
35 Preset newPreset = new Preset { Name = presetName, Query = query, PictureSettings = pictureSettings, Version = Properties.Settings.Default.hb_version};
\r
36 user_presets.Add(newPreset);
\r
37 updateUserPresetsFile();
\r
40 MessageBox.Show("Sorry, that preset name already exists. Please choose another!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
45 /// Remove a preset with a given name from either the built in or user preset list.
\r
47 /// <param name="name">String, the preset name</param>
\r
48 public void remove(string name)
\r
50 List<Preset> newPresets = new List<Preset>();
\r
51 List<Preset> newUserPresets = new List<Preset>();
\r
54 foreach (Preset item in presets)
\r
56 if (item.Name != name)
\r
58 newPresets.Add(item);
\r
61 presets = newPresets;
\r
64 foreach (Preset item in user_presets)
\r
66 if (item.Name != name)
\r
68 newUserPresets.Add(item);
\r
71 user_presets = newUserPresets;
\r
73 // Rebuild the user_presets.xml file
\r
74 updateUserPresetsFile();
\r
75 updatePresetsFile();
\r
79 /// Remove all built in presets;
\r
81 public void removeBuiltInPresets()
\r
84 updatePresetsFile();
\r
88 /// Save changes to a given preset in the user preset list.
\r
90 /// <param name="presetName">String, The name of the new preset</param>
\r
91 /// <param name="query">String, the CLI query for the new preset</param>
\r
92 /// <param name="pictureSettings"> Bool, store crop/picture sizes in the preset</param>
\r
93 public void updatePreset(string presetName, string query, Boolean pictureSettings)
\r
96 foreach (Preset item in user_presets)
\r
98 if (item.Name == presetName)
\r
100 item.Query = query;
\r
101 item.PictureSettings = pictureSettings;
\r
102 MessageBox.Show("Changes to \"" + presetName + "\" Saved", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
\r
103 updateUserPresetsFile();
\r
109 /// Return the CLI query for a preset name given in name
\r
111 /// <param name="name">String, The preset's name</param>
\r
112 /// <returns>String, the CLI query for the given preset name</returns> not
\r
113 public Preset getPreset(string name)
\r
115 // Built In Presets
\r
116 foreach (Preset item in presets)
\r
118 if (item.Name == name)
\r
123 foreach (Preset item in user_presets)
\r
125 if (item.Name == name)
\r
133 /// Reads the CLI's CLI output format and load's them into the preset List<Preset>
\r
135 public void updateBuiltInPresets()
\r
137 // Create a new tempory file and execute the CLI to get the built in presets.
\r
138 string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");
\r
139 string presetsPath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");
\r
140 string strCmdLine = String.Format(@"cmd /c """"{0}"" --preset-list >""{1}"" 2>&1""", handbrakeCLIPath, presetsPath);
\r
142 ProcessStartInfo hbGetPresets = new ProcessStartInfo("CMD.exe", strCmdLine) { WindowStyle = ProcessWindowStyle.Hidden };
\r
143 Process hbproc = Process.Start(hbGetPresets);
\r
144 if (hbproc != null)
\r
146 hbproc.WaitForExit();
\r
151 // Clear the current built in presets and now parse the tempory presets file.
\r
153 string filePath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");
\r
155 if (File.Exists(filePath))
\r
157 StreamReader presetInput = new StreamReader(filePath);
\r
160 string category = String.Empty;
\r
161 string level_1_category = String.Empty;
\r
163 while (!presetInput.EndOfStream)
\r
165 string line = presetInput.ReadLine();
\r
166 if (line.Contains("<") && !line.Contains("<<")) // Found the beginning of a preset block
\r
169 category = line.Replace("<", "").Trim();
\r
170 level_1_category = category;
\r
173 if (line.Contains("<<")) // found a sub preset block
\r
176 category = line.Replace("<<", "").Trim();
\r
179 if (line.Trim().Contains(">>")) // End of sub preset block
\r
182 category = level_1_category;
\r
185 if (line.Contains("+")) // A Preset
\r
187 Regex r = new Regex("(: )"); // Split on hyphens.
\r
188 string[] presetName = r.Split(line);
\r
190 Preset newPreset = new Preset
\r
193 Category = category,
\r
194 TopCategory = level_1_category,
\r
195 Name = presetName[0].Replace("+", "").Trim(),
\r
196 Query = presetName[2],
\r
197 Version = Properties.Settings.Default.hb_version,
\r
198 PictureSettings = true
\r
200 presets.Add(newPreset);
\r
203 presetInput.Close();
\r
204 presetInput.Dispose();
\r
207 // Finally, Create a new or update the current presets.xml file
\r
208 updatePresetsFile();
\r
212 /// Load in the preset data from presets.xml and user_presets.xml
\r
213 /// Load it into the 2 arraylist's presets and user_presets
\r
215 private void loadPresetData()
\r
217 // First clear the presets arraylists
\r
219 user_presets.Clear();
\r
221 // Load in the users presets from user_presets.xml
\r
222 if (File.Exists(hbPresetFile))
\r
224 using (FileStream strm = new FileStream(hbPresetFile, FileMode.Open, FileAccess.Read))
\r
226 if (strm.Length != 0)
\r
228 List<Preset> list = ser.Deserialize(strm) as List<Preset>;
\r
231 foreach (Preset preset in list)
\r
232 presets.Add(preset);
\r
237 // Load in the users presets from user_presets.xml
\r
238 if (File.Exists(userPresetFile))
\r
240 using (FileStream strm = new FileStream(userPresetFile, FileMode.Open, FileAccess.Read))
\r
242 if (strm.Length != 0)
\r
244 List<Preset> list = ser.Deserialize(strm) as List<Preset>;
\r
247 foreach (Preset preset in list)
\r
248 user_presets.Add(preset);
\r
255 /// Setup the frmMain preset panel
\r
257 /// <param name="presetPanel"></param>
\r
258 public void getPresetPanel(ref TreeView presetPanel)
\r
260 this.loadPresetData();
\r
261 presetPanel.Nodes.Clear();
\r
263 if (presets.Count != 0)
\r
265 string category = presets[0].Category;
\r
266 TreeNode rootNode = new TreeNode(presets[0].Category);
\r
267 TreeNode childNode = null;
\r
268 Boolean addChildNode = false;
\r
270 foreach (Preset preset in presets)
\r
273 if (preset.Category == category && preset.TopCategory == category)
\r
274 rootNode.Nodes.Add(preset.Name);
\r
275 else if (preset.Category != category && preset.TopCategory == category) // Deal with child nodes for that root
\r
277 if (childNode == null) // For the first child node
\r
278 childNode = new TreeNode(preset.Category);
\r
280 childNode.Nodes.Add(preset.Name);
\r
281 addChildNode = true;
\r
283 else if (preset.Category != category && preset.TopCategory != category && preset.Level == 1)// Deal with changing root nodes
\r
285 // If we find there are child nodes, add them to the current root node set before adding the rootnode to the panel.
\r
288 rootNode.Nodes.Add(childNode);
\r
290 addChildNode = false;
\r
292 // Add the current rootnodes to the panel, and prepare for a new category of presets
\r
293 presetPanel.Nodes.Add(rootNode);
\r
294 rootNode = new TreeNode(preset.Category);
\r
295 rootNode.Nodes.Add(preset.Name);
\r
297 category = preset.Category;
\r
300 presetPanel.Nodes.Add(rootNode); // Add the final set of nodes
\r
304 foreach (Preset preset in user_presets)
\r
306 TreeNode preset_treeview = new TreeNode(preset.Name) { ForeColor = Color.Black };
\r
307 presetPanel.Nodes.Add(preset_treeview);
\r
312 /// Updates the presets.xml file which contains the built in presets
\r
313 /// It takes the List of Presets and converts them into XML which is stored in presets.xml
\r
315 private void updatePresetsFile()
\r
317 string userPresets = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\presets.xml";
\r
320 using (FileStream strm = new FileStream(userPresets, FileMode.Create, FileAccess.Write))
\r
322 ser.Serialize(strm, presets);
\r
327 catch (Exception exc)
\r
329 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
334 /// Updates the user_presets.xml file which contains the built in presets
\r
335 /// It takes the List of Presets and converts them into XML which is stored in user_presets.xml
\r
337 private void updateUserPresetsFile()
\r
341 using (FileStream strm = new FileStream(userPresetFile, FileMode.Create, FileAccess.Write))
\r
343 ser.Serialize(strm, user_presets);
\r
348 catch (Exception exc)
\r
350 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
355 /// Check if the preset "name" exists in either presets or user_presets lists.
\r
357 /// <param name="name"></param>
\r
358 /// <returns></returns>
\r
359 private Boolean checkIfPresetExists(string name)
\r
361 if (name == string.Empty)
\r
364 // Built In Presets
\r
365 foreach (Preset item in presets)
\r
367 if (item.Name == name)
\r
372 foreach (Preset item in user_presets)
\r
374 if (item.Name == name)
\r
382 /// Check if the user preset "name" exists in user_presets list.
\r
384 /// <param name="name"></param>
\r
385 /// <returns></returns>
\r
386 public Boolean checkIfUserPresetExists(string name)
\r
388 if (name == string.Empty)
\r
392 foreach (Preset item in user_presets)
\r
394 if (item.Name == name)
\r
402 /// Check if the built in presets stored are not out of date.
\r
403 /// Update them if they are.
\r
405 /// <returns></returns>
\r
406 public Boolean checkIfPresetsAreOutOfDate()
\r
409 // Update built-in presets if the built-in presets belong to an older version.
\r
410 if (presets.Count != 0)
\r
411 if (presets[0].Version != Properties.Settings.Default.hb_version)
\r
413 updateBuiltInPresets();
\r