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> _userPresets = new List<Preset>();
\r
21 private static readonly XmlSerializer Ser = new XmlSerializer(typeof(List<Preset>));
\r
22 readonly string _userPresetFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\user_presets.xml";
\r
23 readonly 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 Add(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 _userPresets.Add(newPreset);
\r
37 UpdatePresetFiles();
\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 _userPresets)
\r
65 if (item.Name != name)
\r
67 newUserPresets.Add(item);
\r
70 _userPresets = newUserPresets;
\r
72 // Rebuild the _userPresets.xml file
\r
73 UpdatePresetFiles();
\r
74 UpdatePresetFiles();
\r
78 /// Remove all built in _presets;
\r
80 public void RemoveBuiltInPresets()
\r
83 UpdatePresetFiles();
\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 Update(string presetName, string query, Boolean pictureSettings)
\r
95 foreach (Preset item in _userPresets)
\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 UpdatePresetFiles();
\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 _userPresets)
\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
153 if (File.Exists(presetsPath))
\r
155 StreamReader presetInput = new StreamReader(presetsPath);
\r
157 string category = String.Empty;
\r
159 while (!presetInput.EndOfStream)
\r
161 string line = presetInput.ReadLine();
\r
162 if (line.Contains("<") && !line.Contains("<<")) // Found the beginning of a preset block
\r
163 category = line.Replace("<", "").Trim();
\r
165 if (line.Contains("+")) // A Preset
\r
167 Regex r = new Regex("(: )"); // Split on hyphens.
\r
168 string[] presetName = r.Split(line);
\r
170 Preset newPreset = new Preset
\r
171 { Category = category,
\r
172 Name = presetName[0].Replace("+", "").Trim(),
\r
173 Query = presetName[2],
\r
174 Version = Properties.Settings.Default.hb_version,
\r
175 PictureSettings = true
\r
177 _presets.Add(newPreset);
\r
180 presetInput.Close();
\r
181 presetInput.Dispose();
\r
184 // Finally, Create a new or update the current _presets.xml file
\r
185 UpdatePresetFiles();
\r
189 /// Load in the preset data from _presets.xml and _userPresets.xml
\r
190 /// Load it into the 2 arraylist's _presets and _userPresets
\r
192 private void LoadPresetData()
\r
194 // First clear the _presets arraylists
\r
196 _userPresets.Clear();
\r
198 // Load in the users _presets from _userPresets.xml
\r
199 if (File.Exists(_hbPresetFile))
\r
201 using (FileStream strm = new FileStream(_hbPresetFile, FileMode.Open, FileAccess.Read))
\r
203 if (strm.Length != 0)
\r
205 List<Preset> list = Ser.Deserialize(strm) as List<Preset>;
\r
208 foreach (Preset preset in list)
\r
209 _presets.Add(preset);
\r
214 // Load in the users _presets from _userPresets.xml
\r
215 if (File.Exists(_userPresetFile))
\r
217 using (FileStream strm = new FileStream(_userPresetFile, FileMode.Open, FileAccess.Read))
\r
219 if (strm.Length != 0)
\r
221 List<Preset> list = Ser.Deserialize(strm) as List<Preset>;
\r
224 foreach (Preset preset in list)
\r
225 _userPresets.Add(preset);
\r
232 /// Setup the frmMain preset panel
\r
234 /// <param name="presetPanel"></param>
\r
235 public void GetPresetPanel(ref TreeView presetPanel)
\r
237 this.LoadPresetData();
\r
238 presetPanel.Nodes.Clear();
\r
240 if (_presets.Count != 0) // Built In Presets
\r
242 string category = string.Empty;
\r
243 TreeNode rootNode = null;
\r
245 foreach (Preset preset in _presets)
\r
247 if (preset.Category != category)
\r
249 rootNode = new TreeNode(preset.Category);
\r
250 presetPanel.Nodes.Add(rootNode);
\r
251 category = preset.Category;
\r
254 if (preset.Category == category && rootNode != null)
\r
255 rootNode.Nodes.Add(preset.Name);
\r
259 foreach (Preset preset in _userPresets) // User Presets
\r
261 TreeNode presetTreeview = new TreeNode(preset.Name) { ForeColor = Color.Black };
\r
262 presetPanel.Nodes.Add(presetTreeview);
\r
267 /// Update the preset files
\r
269 private void UpdatePresetFiles()
\r
273 using (FileStream strm = new FileStream(_hbPresetFile, FileMode.Create, FileAccess.Write))
\r
275 Ser.Serialize(strm, _presets);
\r
280 using (FileStream strm = new FileStream(_userPresetFile, FileMode.Create, FileAccess.Write))
\r
282 Ser.Serialize(strm, _userPresets);
\r
287 catch (Exception exc)
\r
289 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
294 /// Check if the preset "name" exists in either _presets or _userPresets lists.
\r
296 /// <param name="name"></param>
\r
297 /// <returns></returns>
\r
298 private Boolean CheckIfPresetExists(string name)
\r
300 if (name == string.Empty)
\r
303 // Built In Presets
\r
304 foreach (Preset item in _presets)
\r
306 if (item.Name == name)
\r
311 foreach (Preset item in _userPresets)
\r
313 if (item.Name == name)
\r
321 /// Check if the user preset "name" exists in _userPresets list.
\r
323 /// <param name="name"></param>
\r
324 /// <returns></returns>
\r
325 public Boolean CheckIfUserPresetExists(string name)
\r
327 if (name == string.Empty)
\r
331 foreach (Preset item in _userPresets)
\r
333 if (item.Name == name)
\r
341 /// Check if the built in _presets stored are not out of date.
\r
342 /// Update them if they are.
\r
344 /// <returns></returns>
\r
345 public Boolean CheckIfPresetsAreOutOfDate()
\r
348 // Update built-in _presets if the built-in _presets belong to an older version.
\r
349 if (_presets.Count != 0)
\r
350 if (_presets[0].Version != Properties.Settings.Default.hb_version)
\r
352 UpdateBuiltInPresets();
\r