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
43 MessageBox.Show("そのプリセット名はすでに存在しています。 別の名前を選択してください", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
51 /// Remove a preset with a given name from either the built in or user preset list.
\r
53 /// <param name="name">String, the preset name</param>
\r
54 public void Remove(string name)
\r
56 List<Preset> newPresets = new List<Preset>();
\r
57 List<Preset> newUserPresets = new List<Preset>();
\r
60 foreach (Preset item in _presets)
\r
62 if (item.Name != name)
\r
64 newPresets.Add(item);
\r
67 _presets = newPresets;
\r
70 foreach (Preset item in _userPresets)
\r
72 if (item.Name != name)
\r
74 newUserPresets.Add(item);
\r
77 _userPresets = newUserPresets;
\r
79 // Rebuild the _userPresets.xml file
\r
80 UpdatePresetFiles();
\r
81 UpdatePresetFiles();
\r
85 /// Remove all built in _presets;
\r
87 public void RemoveBuiltInPresets()
\r
90 UpdatePresetFiles();
\r
94 /// Save changes to a given preset in the user preset list.
\r
96 /// <param name="presetName">String, The name of the new preset</param>
\r
97 /// <param name="query">String, the CLI query for the new preset</param>
\r
98 /// <param name="pictureSettings"> Bool, store crop/picture sizes in the preset</param>
\r
99 public void Update(string presetName, string query, Boolean pictureSettings)
\r
102 foreach (Preset item in _userPresets)
\r
104 if (item.Name == presetName)
\r
106 item.Query = query;
\r
107 item.PictureSettings = pictureSettings;
\r
108 MessageBox.Show("Changes to \"" + presetName + "\" Saved", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
\r
109 UpdatePresetFiles();
\r
115 /// Return the CLI query for a preset name given in name
\r
117 /// <param name="name">String, The preset's name</param>
\r
118 /// <returns>String, the CLI query for the given preset name</returns> not
\r
119 public Preset GetPreset(string name)
\r
121 // Built In Presets
\r
122 foreach (Preset item in _presets)
\r
124 if (item.Name == name)
\r
129 foreach (Preset item in _userPresets)
\r
131 if (item.Name == name)
\r
139 /// Reads the CLI's CLI output format and load's them into the preset List<Preset>
\r
141 public void UpdateBuiltInPresets()
\r
143 // Create a new tempory file and execute the CLI to get the built in _presets.
\r
144 string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");
\r
145 string presetsPath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");
\r
146 string strCmdLine = String.Format(@"cmd /c """"{0}"" --preset-list >""{1}"" 2>&1""", handbrakeCLIPath, presetsPath);
\r
148 ProcessStartInfo hbGetPresets = new ProcessStartInfo("CMD.exe", strCmdLine) { WindowStyle = ProcessWindowStyle.Hidden };
\r
149 Process hbproc = Process.Start(hbGetPresets);
\r
150 if (hbproc != null)
\r
152 hbproc.WaitForExit();
\r
157 // Clear the current built in _presets and now parse the tempory _presets file.
\r
160 if (File.Exists(presetsPath))
\r
162 StreamReader presetInput = new StreamReader(presetsPath);
\r
164 string category = String.Empty;
\r
166 while (!presetInput.EndOfStream)
\r
168 string line = presetInput.ReadLine();
\r
169 if (line.Contains("<") && !line.Contains("<<")) // Found the beginning of a preset block
\r
170 category = line.Replace("<", "").Trim();
\r
172 if (line.Contains("+")) // A Preset
\r
174 Regex r = new Regex("(: )"); // Split on hyphens.
\r
175 string[] presetName = r.Split(line);
\r
177 Preset newPreset = new Preset
\r
178 { Category = category,
\r
179 Name = presetName[0].Replace("+", "").Trim(),
\r
180 Query = presetName[2],
\r
181 Version = Properties.Settings.Default.hb_version,
\r
182 PictureSettings = true
\r
184 _presets.Add(newPreset);
\r
187 presetInput.Close();
\r
188 presetInput.Dispose();
\r
191 // Finally, Create a new or update the current _presets.xml file
\r
192 UpdatePresetFiles();
\r
196 /// Load in the preset data from _presets.xml and _userPresets.xml
\r
197 /// Load it into the 2 arraylist's _presets and _userPresets
\r
199 private void LoadPresetData()
\r
201 // First clear the _presets arraylists
\r
203 _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
221 // Load in the users _presets from _userPresets.xml
\r
222 if (File.Exists(_userPresetFile))
\r
224 using (FileStream strm = new FileStream(_userPresetFile, FileMode.Open, FileAccess.Read))
\r
226 if (strm.Length != 0)
\r
228 List<Preset> list = Ser.Deserialize(strm) as List<Preset>;
\r
231 foreach (Preset preset in list)
\r
232 _userPresets.Add(preset);
\r
239 /// Setup the frmMain preset panel
\r
241 /// <param name="presetPanel"></param>
\r
242 public void GetPresetPanel(ref TreeView presetPanel)
\r
244 this.LoadPresetData();
\r
245 presetPanel.Nodes.Clear();
\r
247 if (_presets.Count != 0) // Built In Presets
\r
249 string category = string.Empty;
\r
250 TreeNode rootNode = null;
\r
252 foreach (Preset preset in _presets)
\r
254 if (preset.Category != category)
\r
256 rootNode = new TreeNode(preset.Category);
\r
257 presetPanel.Nodes.Add(rootNode);
\r
258 category = preset.Category;
\r
261 if (preset.Category == category && rootNode != null)
\r
262 rootNode.Nodes.Add(preset.Name);
\r
266 foreach (Preset preset in _userPresets) // User Presets
\r
269 MessageBox.Show("ファイルの書き込みに失敗しました。\n Error Information: \n\n" + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
\r
271 TreeNode presetTreeview = new TreeNode(preset.Name) { ForeColor = Color.Black };
\r
272 presetPanel.Nodes.Add(presetTreeview);
\r
277 /// Update the preset files
\r
279 private void UpdatePresetFiles()
\r
283 using (FileStream strm = new FileStream(_hbPresetFile, FileMode.Create, FileAccess.Write))
\r
285 Ser.Serialize(strm, _presets);
\r
290 using (FileStream strm = new FileStream(_userPresetFile, FileMode.Create, FileAccess.Write))
\r
292 Ser.Serialize(strm, _userPresets);
\r
297 catch (Exception exc)
\r
299 MessageBox.Show("ファイルの書き込みに失敗しました。\n Error Information: \n\n" + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
\r
304 /// Check if the preset "name" exists in either _presets or _userPresets lists.
\r
306 /// <param name="name"></param>
\r
307 /// <returns></returns>
\r
308 private Boolean CheckIfPresetExists(string name)
\r
310 if (name == string.Empty)
\r
313 // Built In Presets
\r
314 foreach (Preset item in _presets)
\r
316 if (item.Name == name)
\r
321 foreach (Preset item in _userPresets)
\r
323 if (item.Name == name)
\r
331 /// Check if the user preset "name" exists in _userPresets list.
\r
333 /// <param name="name"></param>
\r
334 /// <returns></returns>
\r
335 public Boolean CheckIfUserPresetExists(string name)
\r
337 if (name == string.Empty)
\r
341 foreach (Preset item in _userPresets)
\r
343 if (item.Name == name)
\r
351 /// Check if the built in _presets stored are not out of date.
\r
352 /// Update them if they are.
\r
354 /// <returns></returns>
\r
355 public Boolean CheckIfPresetsAreOutOfDate()
\r
358 // Update built-in _presets if the built-in _presets belong to an older version.
\r
359 if (_presets.Count != 0)
\r
360 if (_presets[0].Version != Properties.Settings.Default.hb_version)
\r
362 UpdateBuiltInPresets();
\r