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
190 presets.Add(newPreset);
\r
193 presetInput.Close();
\r
194 presetInput.Dispose();
\r
197 // Finally, Create a new or update the current presets.xml file
\r
198 updatePresetsFile();
\r
202 /// Load in the preset data from presets.xml and user_presets.xml
\r
203 /// Load it into the 2 arraylist's presets and user_presets
\r
205 private void loadPresetData()
\r
207 // First clear the presets arraylists
\r
209 user_presets.Clear();
\r
211 // Load in the users presets from user_presets.xml
\r
212 if (File.Exists(hbPresetFile))
\r
214 using (FileStream strm = new FileStream(hbPresetFile, FileMode.Open, FileAccess.Read))
\r
216 if (strm.Length != 0)
\r
218 List<Preset> list = ser.Deserialize(strm) as List<Preset>;
\r
221 foreach (Preset preset in list)
\r
222 presets.Add(preset);
\r
227 // Load in the users presets from user_presets.xml
\r
228 if (File.Exists(userPresetFile))
\r
230 using (FileStream strm = new FileStream(userPresetFile, FileMode.Open, FileAccess.Read))
\r
232 if (strm.Length != 0)
\r
234 List<Preset> list = ser.Deserialize(strm) as List<Preset>;
\r
237 foreach (Preset preset in list)
\r
238 user_presets.Add(preset);
\r
245 /// Setup the frmMain preset panel
\r
247 /// <param name="presetPanel"></param>
\r
248 public void getPresetPanel(ref TreeView presetPanel)
\r
250 this.loadPresetData();
\r
251 presetPanel.Nodes.Clear();
\r
253 if (presets.Count != 0)
\r
255 string category = presets[0].Category;
\r
256 TreeNode rootNode = new TreeNode(presets[0].Category);
\r
257 TreeNode childNode = null;
\r
258 Boolean addChildNode = false;
\r
260 foreach (Preset preset in presets)
\r
263 if (preset.Category == category && preset.TopCategory == category)
\r
264 rootNode.Nodes.Add(preset.Name);
\r
265 else if (preset.Category != category && preset.TopCategory == category) // Deal with child nodes for that root
\r
267 if (childNode == null) // For the first child node
\r
268 childNode = new TreeNode(preset.Category);
\r
270 childNode.Nodes.Add(preset.Name);
\r
271 addChildNode = true;
\r
273 else if (preset.Category != category && preset.TopCategory != category && preset.Level == 1)// Deal with changing root nodes
\r
275 // If we find there are child nodes, add them to the current root node set before adding the rootnode to the panel.
\r
278 rootNode.Nodes.Add(childNode);
\r
280 addChildNode = false;
\r
282 // Add the current rootnodes to the panel, and prepare for a new category of presets
\r
283 presetPanel.Nodes.Add(rootNode);
\r
284 rootNode = new TreeNode(preset.Category);
\r
285 rootNode.Nodes.Add(preset.Name);
\r
287 category = preset.Category;
\r
290 presetPanel.Nodes.Add(rootNode); // Add the final set of nodes
\r
294 foreach (Preset preset in user_presets)
\r
296 TreeNode preset_treeview = new TreeNode(preset.Name) { ForeColor = Color.Black };
\r
297 presetPanel.Nodes.Add(preset_treeview);
\r
302 /// Updates the presets.xml file which contains the built in presets
\r
303 /// It takes the List of Presets and converts them into XML which is stored in presets.xml
\r
305 private void updatePresetsFile()
\r
307 string userPresets = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\presets.xml";
\r
310 using (FileStream strm = new FileStream(userPresets, FileMode.Create, FileAccess.Write))
\r
312 ser.Serialize(strm, presets);
\r
317 catch (Exception exc)
\r
319 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
324 /// Updates the user_presets.xml file which contains the built in presets
\r
325 /// It takes the List of Presets and converts them into XML which is stored in user_presets.xml
\r
327 private void updateUserPresetsFile()
\r
331 using (FileStream strm = new FileStream(userPresetFile, FileMode.Create, FileAccess.Write))
\r
333 ser.Serialize(strm, user_presets);
\r
338 catch (Exception exc)
\r
340 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
345 /// Check if the preset "name" exists in either presets or user_presets lists.
\r
347 /// <param name="name"></param>
\r
348 /// <returns></returns>
\r
349 private Boolean checkIfPresetExists(string name)
\r
351 if (name == string.Empty)
\r
354 // Built In Presets
\r
355 foreach (Preset item in presets)
\r
357 if (item.Name == name)
\r
362 foreach (Preset item in user_presets)
\r
364 if (item.Name == name)
\r
372 /// Check if the user preset "name" exists in user_presets list.
\r
374 /// <param name="name"></param>
\r
375 /// <returns></returns>
\r
376 public Boolean checkIfUserPresetExists(string name)
\r
378 if (name == string.Empty)
\r
382 foreach (Preset item in user_presets)
\r
384 if (item.Name == name)
\r
392 /// Check if the built in presets stored are not out of date.
\r
393 /// Update them if they are.
\r
395 /// <returns></returns>
\r
396 public Boolean checkIfPresetsAreOutOfDate()
\r
399 // Update built-in presets if the built-in presets belong to an older version.
\r
400 if (presets.Count != 0)
\r
401 if (presets[0].Version != Properties.Settings.Default.hb_version)
\r
403 updateBuiltInPresets();
\r