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
171 if (presetName[2].Contains("crop"))
\r
174 Preset newPreset = new Preset
\r
176 Category = category,
\r
177 Name = presetName[0].Replace("+", "").Trim(),
\r
178 Query = presetName[2],
\r
179 Version = Properties.Settings.Default.hb_version,
\r
180 PictureSettings = pic
\r
182 _presets.Add(newPreset);
\r
185 presetInput.Close();
\r
186 presetInput.Dispose();
\r
189 // Finally, Create a new or update the current _presets.xml file
\r
190 UpdatePresetFiles();
\r
194 /// Load in the preset data from _presets.xml and _userPresets.xml
\r
195 /// Load it into the 2 arraylist's _presets and _userPresets
\r
197 private void LoadPresetData()
\r
199 // First clear the _presets arraylists
\r
201 _userPresets.Clear();
\r
205 // Load in the users _presets from _userPresets.xml
\r
206 if (File.Exists(_hbPresetFile))
\r
208 using (FileStream strm = new FileStream(_hbPresetFile, FileMode.Open, FileAccess.Read))
\r
210 if (strm.Length != 0)
\r
212 List<Preset> list = Ser.Deserialize(strm) as List<Preset>;
\r
215 foreach (Preset preset in list)
\r
216 _presets.Add(preset);
\r
224 "HandBrakes preset file appears to have been corrupted. This file will now be re-generated!\n" +
\r
225 "If the problem presists, please delete the file: \n\n" + _hbPresetFile, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
226 UpdateBuiltInPresets();
\r
231 // Load in the users _presets from _userPresets.xml
\r
232 if (File.Exists(_userPresetFile))
\r
234 using (FileStream strm = new FileStream(_userPresetFile, FileMode.Open, FileAccess.Read))
\r
236 if (strm.Length != 0)
\r
238 List<Preset> list = Ser.Deserialize(strm) as List<Preset>;
\r
241 foreach (Preset preset in list)
\r
242 _userPresets.Add(preset);
\r
250 "Your User presets file appears to have been corrupted.\n" +
\r
251 "Your presets will not be loaded. You may need to re-create your presets.\n\n" +
\r
252 "Your user presets file has been renamed to 'user_presets.xml.old' and is located in:\n " +
\r
253 Path.GetDirectoryName(_userPresetFile) + "\n" +
\r
254 "You may be able to recover some presets if you know the XML language.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
256 // Recover from Error.
\r
257 if (File.Exists(_userPresetFile))
\r
259 string disabledFile = _userPresetFile + ".old";
\r
260 if (File.Exists(disabledFile))
\r
261 File.Delete(disabledFile);
\r
262 File.Move(_userPresetFile, disabledFile);
\r
269 /// Setup the frmMain preset panel
\r
271 /// <param name="presetPanel"></param>
\r
272 public void GetPresetPanel(ref TreeView presetPanel)
\r
274 this.LoadPresetData();
\r
275 presetPanel.Nodes.Clear();
\r
276 string category = string.Empty;
\r
277 TreeNode rootNode = null;
\r
279 if (_presets.Count != 0) // Built In Presets
\r
282 foreach (Preset preset in _presets)
\r
284 if (preset.Category != category)
\r
286 rootNode = new TreeNode(preset.Category);
\r
287 presetPanel.Nodes.Add(rootNode);
\r
288 category = preset.Category;
\r
291 if (preset.Category == category && rootNode != null)
\r
292 rootNode.Nodes.Add(preset.Name);
\r
296 rootNode = null; category = null;
\r
297 foreach (Preset preset in _userPresets) // User Presets
\r
299 if (preset.Category != category && preset.Category != null)
\r
301 rootNode = new TreeNode(preset.Category) { ForeColor = Color.Black };
\r
302 presetPanel.Nodes.Add(rootNode);
\r
303 category = preset.Category;
\r
306 if (preset.Category == category && rootNode != null)
\r
307 rootNode.Nodes.Add(new TreeNode(preset.Name) { ForeColor = Color.Black });
\r
309 presetPanel.Nodes.Add(new TreeNode(preset.Name) { ForeColor = Color.Black });
\r
314 /// Update the preset files
\r
316 private void UpdatePresetFiles()
\r
320 using (FileStream strm = new FileStream(_hbPresetFile, FileMode.Create, FileAccess.Write))
\r
322 Ser.Serialize(strm, _presets);
\r
327 using (FileStream strm = new FileStream(_userPresetFile, FileMode.Create, FileAccess.Write))
\r
329 Ser.Serialize(strm, _userPresets);
\r
334 catch (Exception exc)
\r
336 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
341 /// Check if the preset "name" exists in either _presets or _userPresets lists.
\r
343 /// <param name="name"></param>
\r
344 /// <returns></returns>
\r
345 private Boolean CheckIfPresetExists(string name)
\r
347 if (name == string.Empty)
\r
350 // Built In Presets
\r
351 foreach (Preset item in _presets)
\r
353 if (item.Name == name)
\r
358 foreach (Preset item in _userPresets)
\r
360 if (item.Name == name)
\r
368 /// Check if the user preset "name" exists in _userPresets list.
\r
370 /// <param name="name"></param>
\r
371 /// <returns></returns>
\r
372 public Boolean CheckIfUserPresetExists(string name)
\r
374 if (name == string.Empty)
\r
378 foreach (Preset item in _userPresets)
\r
380 if (item.Name == name)
\r
388 /// Check if the built in _presets stored are not out of date.
\r
389 /// Update them if they are.
\r
391 /// <returns></returns>
\r
392 public Boolean CheckIfPresetsAreOutOfDate()
\r
395 // Update built-in _presets if the built-in _presets belong to an older version.
\r
396 if (_presets.Count != 0)
\r
397 if (_presets[0].Version != Properties.Settings.Default.hb_version)
\r
399 UpdateBuiltInPresets();
\r