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
172 Category = category,
\r
173 Name = presetName[0].Replace("+", "").Trim(),
\r
174 Query = presetName[2],
\r
175 Version = Properties.Settings.Default.hb_version,
\r
176 PictureSettings = true
\r
178 _presets.Add(newPreset);
\r
181 presetInput.Close();
\r
182 presetInput.Dispose();
\r
185 // Finally, Create a new or update the current _presets.xml file
\r
186 UpdatePresetFiles();
\r
190 /// Load in the preset data from _presets.xml and _userPresets.xml
\r
191 /// Load it into the 2 arraylist's _presets and _userPresets
\r
193 private void LoadPresetData()
\r
195 // First clear the _presets arraylists
\r
197 _userPresets.Clear();
\r
201 // Load in the users _presets from _userPresets.xml
\r
202 if (File.Exists(_hbPresetFile))
\r
204 using (FileStream strm = new FileStream(_hbPresetFile, FileMode.Open, FileAccess.Read))
\r
206 if (strm.Length != 0)
\r
208 List<Preset> list = Ser.Deserialize(strm) as List<Preset>;
\r
211 foreach (Preset preset in list)
\r
212 _presets.Add(preset);
\r
220 "HandBrakes preset file appears to have been corrupted. This file will now be re-generated!\n" +
\r
221 "If the problem presists, please delete the file: \n\n" + _hbPresetFile, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
222 UpdateBuiltInPresets();
\r
227 // Load in the users _presets from _userPresets.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 _userPresets.Add(preset);
\r
246 "Your User presets file appears to have been corrupted.\n" +
\r
247 "Your presets will not be loaded. You may need to re-create your presets.\n\n" +
\r
248 "Your user presets file has been renamed to 'user_presets.xml.old' and is located in:\n " +
\r
249 Path.GetDirectoryName(_userPresetFile) + "\n" +
\r
250 "You may be able to recover some presets if you know the XML language.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
252 // Recover from Error.
\r
253 if (File.Exists(_userPresetFile))
\r
255 string disabledFile = _userPresetFile + ".old";
\r
256 if (File.Exists(disabledFile))
\r
257 File.Delete(disabledFile);
\r
258 File.Move(_userPresetFile, disabledFile);
\r
265 /// Setup the frmMain preset panel
\r
267 /// <param name="presetPanel"></param>
\r
268 public void GetPresetPanel(ref TreeView presetPanel)
\r
270 this.LoadPresetData();
\r
271 presetPanel.Nodes.Clear();
\r
272 string category = string.Empty;
\r
273 TreeNode rootNode = null;
\r
275 if (_presets.Count != 0) // Built In Presets
\r
278 foreach (Preset preset in _presets)
\r
280 if (preset.Category != category)
\r
282 rootNode = new TreeNode(preset.Category);
\r
283 presetPanel.Nodes.Add(rootNode);
\r
284 category = preset.Category;
\r
287 if (preset.Category == category && rootNode != null)
\r
288 rootNode.Nodes.Add(preset.Name);
\r
292 rootNode = null; category = null;
\r
293 foreach (Preset preset in _userPresets) // User Presets
\r
295 if (preset.Category != category && preset.Category != null)
\r
297 rootNode = new TreeNode(preset.Category) { ForeColor = Color.Black };
\r
298 presetPanel.Nodes.Add(rootNode);
\r
299 category = preset.Category;
\r
302 if (preset.Category == category && rootNode != null)
\r
303 rootNode.Nodes.Add(new TreeNode(preset.Name) { ForeColor = Color.Black });
\r
305 presetPanel.Nodes.Add(new TreeNode(preset.Name) { ForeColor = Color.Black });
\r
310 /// Update the preset files
\r
312 private void UpdatePresetFiles()
\r
316 using (FileStream strm = new FileStream(_hbPresetFile, FileMode.Create, FileAccess.Write))
\r
318 Ser.Serialize(strm, _presets);
\r
323 using (FileStream strm = new FileStream(_userPresetFile, FileMode.Create, FileAccess.Write))
\r
325 Ser.Serialize(strm, _userPresets);
\r
330 catch (Exception exc)
\r
332 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
337 /// Check if the preset "name" exists in either _presets or _userPresets lists.
\r
339 /// <param name="name"></param>
\r
340 /// <returns></returns>
\r
341 private Boolean CheckIfPresetExists(string name)
\r
343 if (name == string.Empty)
\r
346 // Built In Presets
\r
347 foreach (Preset item in _presets)
\r
349 if (item.Name == name)
\r
354 foreach (Preset item in _userPresets)
\r
356 if (item.Name == name)
\r
364 /// Check if the user preset "name" exists in _userPresets list.
\r
366 /// <param name="name"></param>
\r
367 /// <returns></returns>
\r
368 public Boolean CheckIfUserPresetExists(string name)
\r
370 if (name == string.Empty)
\r
374 foreach (Preset item in _userPresets)
\r
376 if (item.Name == name)
\r
384 /// Check if the built in _presets stored are not out of date.
\r
385 /// Update them if they are.
\r
387 /// <returns></returns>
\r
388 public Boolean CheckIfPresetsAreOutOfDate()
\r
391 // Update built-in _presets if the built-in _presets belong to an older version.
\r
392 if (_presets.Count != 0)
\r
393 if (_presets[0].Version != Properties.Settings.Default.hb_version)
\r
395 UpdateBuiltInPresets();
\r