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 /// Save changes to a given preset in the user preset list.
\r
81 /// <param name="presetName">String, The name of the new preset</param>
\r
82 /// <param name="query">String, the CLI query for the new preset</param>
\r
83 /// <param name="pictureSettings"> Bool, store crop/picture sizes in the preset</param>
\r
84 public void updatePreset(string presetName, string query, Boolean pictureSettings)
\r
87 foreach (Preset item in user_presets)
\r
89 if (item.Name == presetName)
\r
92 item.PictureSettings = pictureSettings;
\r
93 MessageBox.Show("Changes to \"" + presetName + "\" Saved", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
\r
94 updateUserPresetsFile();
\r
100 /// Return the CLI query for a preset name given in name
\r
102 /// <param name="name">String, The preset's name</param>
\r
103 /// <returns>String, the CLI query for the given preset name</returns> not
\r
104 public Preset getPreset(string name)
\r
106 // Built In Presets
\r
107 foreach (Preset item in presets)
\r
109 if (item.Name == name)
\r
114 foreach (Preset item in user_presets)
\r
116 if (item.Name == name)
\r
124 /// Reads the CLI's CLI output format and load's them into the preset List<Preset>
\r
126 public void updateBuiltInPresets()
\r
128 // Create a new tempory file and execute the CLI to get the built in presets.
\r
129 string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");
\r
130 string presetsPath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");
\r
131 string strCmdLine = String.Format(@"cmd /c """"{0}"" --preset-list >""{1}"" 2>&1""", handbrakeCLIPath, presetsPath);
\r
133 ProcessStartInfo hbGetPresets = new ProcessStartInfo("CMD.exe", strCmdLine) { WindowStyle = ProcessWindowStyle.Hidden };
\r
134 Process hbproc = Process.Start(hbGetPresets);
\r
135 if (hbproc != null)
\r
137 hbproc.WaitForExit();
\r
142 // Clear the current built in presets and now parse the tempory presets file.
\r
144 string filePath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");
\r
146 if (File.Exists(filePath))
\r
148 StreamReader presetInput = new StreamReader(filePath);
\r
151 string category = String.Empty;
\r
152 string level_1_category = String.Empty;
\r
154 while (!presetInput.EndOfStream)
\r
156 string line = presetInput.ReadLine();
\r
157 if (line.Contains("<") && !line.Contains("<<")) // Found the beginning of a preset block
\r
160 category = line.Replace("<", "").Trim();
\r
161 level_1_category = category;
\r
164 if (line.Contains("<<")) // found a sub preset block
\r
167 category = line.Replace("<<", "").Trim();
\r
170 if (line.Trim().Contains(">>")) // End of sub preset block
\r
173 category = level_1_category;
\r
176 if (line.Contains("+")) // A Preset
\r
178 Regex r = new Regex("(: )"); // Split on hyphens.
\r
179 string[] presetName = r.Split(line);
\r
181 Preset newPreset = new Preset
\r
184 Category = category,
\r
185 TopCategory = level_1_category,
\r
186 Name = presetName[0].Replace("+", "").Trim(),
\r
187 Query = presetName[2],
\r
188 Version = Properties.Settings.Default.hb_version,
\r
189 PictureSettings = true
\r
191 presets.Add(newPreset);
\r
194 presetInput.Close();
\r
195 presetInput.Dispose();
\r
198 // Finally, Create a new or update the current presets.xml file
\r
199 updatePresetsFile();
\r
203 /// Load in the preset data from presets.xml and user_presets.xml
\r
204 /// Load it into the 2 arraylist's presets and user_presets
\r
206 private void loadPresetData()
\r
208 // First clear the presets arraylists
\r
210 user_presets.Clear();
\r
212 // Load in the users presets from user_presets.xml
\r
213 if (File.Exists(hbPresetFile))
\r
215 using (FileStream strm = new FileStream(hbPresetFile, FileMode.Open, FileAccess.Read))
\r
217 if (strm.Length != 0)
\r
219 List<Preset> list = ser.Deserialize(strm) as List<Preset>;
\r
222 foreach (Preset preset in list)
\r
223 presets.Add(preset);
\r
228 // Load in the users presets from user_presets.xml
\r
229 if (File.Exists(userPresetFile))
\r
231 using (FileStream strm = new FileStream(userPresetFile, FileMode.Open, FileAccess.Read))
\r
233 if (strm.Length != 0)
\r
235 List<Preset> list = ser.Deserialize(strm) as List<Preset>;
\r
238 foreach (Preset preset in list)
\r
239 user_presets.Add(preset);
\r
246 /// Setup the frmMain preset panel
\r
248 /// <param name="presetPanel"></param>
\r
249 public void getPresetPanel(ref TreeView presetPanel)
\r
251 this.loadPresetData();
\r
252 presetPanel.Nodes.Clear();
\r
254 if (presets.Count != 0)
\r
256 string category = presets[0].Category;
\r
257 TreeNode rootNode = new TreeNode(presets[0].Category);
\r
258 TreeNode childNode = null;
\r
259 Boolean addChildNode = false;
\r
261 foreach (Preset preset in presets)
\r
264 if (preset.Category == category && preset.TopCategory == category)
\r
265 rootNode.Nodes.Add(preset.Name);
\r
266 else if (preset.Category != category && preset.TopCategory == category) // Deal with child nodes for that root
\r
268 if (childNode == null) // For the first child node
\r
269 childNode = new TreeNode(preset.Category);
\r
271 childNode.Nodes.Add(preset.Name);
\r
272 addChildNode = true;
\r
274 else if (preset.Category != category && preset.TopCategory != category && preset.Level == 1)// Deal with changing root nodes
\r
276 // If we find there are child nodes, add them to the current root node set before adding the rootnode to the panel.
\r
279 rootNode.Nodes.Add(childNode);
\r
281 addChildNode = false;
\r
283 // Add the current rootnodes to the panel, and prepare for a new category of presets
\r
284 presetPanel.Nodes.Add(rootNode);
\r
285 rootNode = new TreeNode(preset.Category);
\r
286 rootNode.Nodes.Add(preset.Name);
\r
288 category = preset.Category;
\r
291 presetPanel.Nodes.Add(rootNode); // Add the final set of nodes
\r
295 foreach (Preset preset in user_presets)
\r
297 TreeNode preset_treeview = new TreeNode(preset.Name) { ForeColor = Color.Black };
\r
298 presetPanel.Nodes.Add(preset_treeview);
\r
303 /// Updates the presets.xml file which contains the built in presets
\r
304 /// It takes the List of Presets and converts them into XML which is stored in presets.xml
\r
306 private void updatePresetsFile()
\r
308 string userPresets = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\presets.xml";
\r
311 using (FileStream strm = new FileStream(userPresets, FileMode.Create, FileAccess.Write))
\r
313 ser.Serialize(strm, presets);
\r
318 catch (Exception exc)
\r
320 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
325 /// Updates the user_presets.xml file which contains the built in presets
\r
326 /// It takes the List of Presets and converts them into XML which is stored in user_presets.xml
\r
328 private void updateUserPresetsFile()
\r
332 using (FileStream strm = new FileStream(userPresetFile, FileMode.Create, FileAccess.Write))
\r
334 ser.Serialize(strm, user_presets);
\r
339 catch (Exception exc)
\r
341 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
346 /// Check if the preset "name" exists in either presets or user_presets lists.
\r
348 /// <param name="name"></param>
\r
349 /// <returns></returns>
\r
350 private Boolean checkIfPresetExists(string name)
\r
352 if (name == string.Empty)
\r
355 // Built In Presets
\r
356 foreach (Preset item in presets)
\r
358 if (item.Name == name)
\r
363 foreach (Preset item in user_presets)
\r
365 if (item.Name == name)
\r
373 /// Check if the user preset "name" exists in user_presets list.
\r
375 /// <param name="name"></param>
\r
376 /// <returns></returns>
\r
377 public Boolean checkIfUserPresetExists(string name)
\r
379 if (name == string.Empty)
\r
383 foreach (Preset item in user_presets)
\r
385 if (item.Name == name)
\r
393 /// Check if the built in presets stored are not out of date.
\r
394 /// Update them if they are.
\r
396 /// <returns></returns>
\r
397 public Boolean checkIfPresetsAreOutOfDate()
\r
400 // Update built-in presets if the built-in presets belong to an older version.
\r
401 if (presets.Count != 0)
\r
402 if (presets[0].Version != Properties.Settings.Default.hb_version)
\r
404 updateBuiltInPresets();
\r