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
44 /// Remove a preset with a given name from either the built in or user preset list.
\r
46 /// <param name="name">String, the preset name</param>
\r
47 public void remove(string name)
\r
49 List<Preset> newPresets = new List<Preset>();
\r
50 List<Preset> newUserPresets = new List<Preset>();
\r
53 foreach (Preset item in presets)
\r
55 if (item.Name != name)
\r
57 newPresets.Add(item);
\r
60 presets = newPresets;
\r
63 foreach (Preset item in user_presets)
\r
65 if (item.Name != name)
\r
67 newUserPresets.Add(item);
\r
70 user_presets = newUserPresets;
\r
72 // Rebuild the user_presets.xml file
\r
73 updateUserPresetsFile();
\r
74 updatePresetsFile();
\r
78 /// Remove all built in presets;
\r
80 public void removeBuiltInPresets()
\r
83 updatePresetsFile();
\r
87 /// Save changes to a given preset in the user preset list.
\r
89 /// <param name="presetName">String, The name of the new preset</param>
\r
90 /// <param name="query">String, the CLI query for the new preset</param>
\r
91 /// <param name="pictureSettings"> Bool, store crop/picture sizes in the preset</param>
\r
92 public void updatePreset(string presetName, string query, Boolean pictureSettings)
\r
95 foreach (Preset item in user_presets)
\r
97 if (item.Name == presetName)
\r
100 item.PictureSettings = pictureSettings;
\r
101 MessageBox.Show("Changes to \"" + presetName + "\" Saved", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
\r
102 updateUserPresetsFile();
\r
108 /// Return the CLI query for a preset name given in name
\r
110 /// <param name="name">String, The preset's name</param>
\r
111 /// <returns>String, the CLI query for the given preset name</returns> not
\r
112 public Preset getPreset(string name)
\r
114 // Built In Presets
\r
115 foreach (Preset item in presets)
\r
117 if (item.Name == name)
\r
122 foreach (Preset item in user_presets)
\r
124 if (item.Name == name)
\r
132 /// Reads the CLI's CLI output format and load's them into the preset List<Preset>
\r
134 public void updateBuiltInPresets()
\r
136 // Create a new tempory file and execute the CLI to get the built in presets.
\r
137 string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");
\r
138 string presetsPath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");
\r
139 string strCmdLine = String.Format(@"cmd /c """"{0}"" --preset-list >""{1}"" 2>&1""", handbrakeCLIPath, presetsPath);
\r
141 ProcessStartInfo hbGetPresets = new ProcessStartInfo("CMD.exe", strCmdLine) { WindowStyle = ProcessWindowStyle.Hidden };
\r
142 Process hbproc = Process.Start(hbGetPresets);
\r
143 if (hbproc != null)
\r
145 hbproc.WaitForExit();
\r
150 // Clear the current built in presets and now parse the tempory presets file.
\r
152 string filePath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");
\r
154 if (File.Exists(filePath))
\r
156 StreamReader presetInput = new StreamReader(filePath);
\r
159 string category = String.Empty;
\r
160 string level_1_category = String.Empty;
\r
162 while (!presetInput.EndOfStream)
\r
164 string line = presetInput.ReadLine();
\r
165 if (line.Contains("<") && !line.Contains("<<")) // Found the beginning of a preset block
\r
168 category = line.Replace("<", "").Trim();
\r
169 level_1_category = category;
\r
172 if (line.Contains("<<")) // found a sub preset block
\r
175 category = line.Replace("<<", "").Trim();
\r
178 if (line.Trim().Contains(">>")) // End of sub preset block
\r
181 category = level_1_category;
\r
184 if (line.Contains("+")) // A Preset
\r
186 Regex r = new Regex("(: )"); // Split on hyphens.
\r
187 string[] presetName = r.Split(line);
\r
189 Preset newPreset = new Preset
\r
192 Category = category,
\r
193 TopCategory = level_1_category,
\r
194 Name = presetName[0].Replace("+", "").Trim(),
\r
195 Query = presetName[2],
\r
196 Version = Properties.Settings.Default.hb_version,
\r
197 PictureSettings = true
\r
199 presets.Add(newPreset);
\r
202 presetInput.Close();
\r
203 presetInput.Dispose();
\r
206 // Finally, Create a new or update the current presets.xml file
\r
207 updatePresetsFile();
\r
211 /// Load in the preset data from presets.xml and user_presets.xml
\r
212 /// Load it into the 2 arraylist's presets and user_presets
\r
214 private void loadPresetData()
\r
216 // First clear the presets arraylists
\r
218 user_presets.Clear();
\r
220 // Load in the users presets from user_presets.xml
\r
221 if (File.Exists(hbPresetFile))
\r
223 using (FileStream strm = new FileStream(hbPresetFile, FileMode.Open, FileAccess.Read))
\r
225 if (strm.Length != 0)
\r
227 List<Preset> list = ser.Deserialize(strm) as List<Preset>;
\r
230 foreach (Preset preset in list)
\r
231 presets.Add(preset);
\r
236 // Load in the users presets from user_presets.xml
\r
237 if (File.Exists(userPresetFile))
\r
239 using (FileStream strm = new FileStream(userPresetFile, FileMode.Open, FileAccess.Read))
\r
241 if (strm.Length != 0)
\r
243 List<Preset> list = ser.Deserialize(strm) as List<Preset>;
\r
246 foreach (Preset preset in list)
\r
247 user_presets.Add(preset);
\r
254 /// Setup the frmMain preset panel
\r
256 /// <param name="presetPanel"></param>
\r
257 public void getPresetPanel(ref TreeView presetPanel)
\r
259 this.loadPresetData();
\r
260 presetPanel.Nodes.Clear();
\r
262 if (presets.Count != 0)
\r
264 string category = presets[0].Category;
\r
265 TreeNode rootNode = new TreeNode(presets[0].Category);
\r
266 TreeNode childNode = null;
\r
267 Boolean addChildNode = false;
\r
269 foreach (Preset preset in presets)
\r
272 if (preset.Category == category && preset.TopCategory == category)
\r
273 rootNode.Nodes.Add(preset.Name);
\r
274 else if (preset.Category != category && preset.TopCategory == category) // Deal with child nodes for that root
\r
276 if (childNode == null) // For the first child node
\r
277 childNode = new TreeNode(preset.Category);
\r
279 childNode.Nodes.Add(preset.Name);
\r
280 addChildNode = true;
\r
282 else if (preset.Category != category && preset.TopCategory != category && preset.Level == 1)// Deal with changing root nodes
\r
284 // If we find there are child nodes, add them to the current root node set before adding the rootnode to the panel.
\r
287 rootNode.Nodes.Add(childNode);
\r
289 addChildNode = false;
\r
291 // Add the current rootnodes to the panel, and prepare for a new category of presets
\r
292 presetPanel.Nodes.Add(rootNode);
\r
293 rootNode = new TreeNode(preset.Category);
\r
294 rootNode.Nodes.Add(preset.Name);
\r
296 category = preset.Category;
\r
299 presetPanel.Nodes.Add(rootNode); // Add the final set of nodes
\r
303 foreach (Preset preset in user_presets)
\r
305 TreeNode preset_treeview = new TreeNode(preset.Name) { ForeColor = Color.Black };
\r
306 presetPanel.Nodes.Add(preset_treeview);
\r
311 /// Updates the presets.xml file which contains the built in presets
\r
312 /// It takes the List of Presets and converts them into XML which is stored in presets.xml
\r
314 private void updatePresetsFile()
\r
316 string userPresets = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\presets.xml";
\r
319 using (FileStream strm = new FileStream(userPresets, FileMode.Create, FileAccess.Write))
\r
321 ser.Serialize(strm, presets);
\r
326 catch (Exception exc)
\r
328 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
333 /// Updates the user_presets.xml file which contains the built in presets
\r
334 /// It takes the List of Presets and converts them into XML which is stored in user_presets.xml
\r
336 private void updateUserPresetsFile()
\r
340 using (FileStream strm = new FileStream(userPresetFile, FileMode.Create, FileAccess.Write))
\r
342 ser.Serialize(strm, user_presets);
\r
347 catch (Exception exc)
\r
349 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
354 /// Check if the preset "name" exists in either presets or user_presets lists.
\r
356 /// <param name="name"></param>
\r
357 /// <returns></returns>
\r
358 private Boolean checkIfPresetExists(string name)
\r
360 if (name == string.Empty)
\r
363 // Built In Presets
\r
364 foreach (Preset item in presets)
\r
366 if (item.Name == name)
\r
371 foreach (Preset item in user_presets)
\r
373 if (item.Name == name)
\r
381 /// Check if the user preset "name" exists in user_presets list.
\r
383 /// <param name="name"></param>
\r
384 /// <returns></returns>
\r
385 public Boolean checkIfUserPresetExists(string name)
\r
387 if (name == string.Empty)
\r
391 foreach (Preset item in user_presets)
\r
393 if (item.Name == name)
\r
401 /// Check if the built in presets stored are not out of date.
\r
402 /// Update them if they are.
\r
404 /// <returns></returns>
\r
405 public Boolean checkIfPresetsAreOutOfDate()
\r
408 // Update built-in presets if the built-in presets belong to an older version.
\r
409 if (presets.Count != 0)
\r
410 if (presets[0].Version != Properties.Settings.Default.hb_version)
\r
412 updateBuiltInPresets();
\r