2 using System.Collections.Generic;
\r
3 using System.Windows.Forms;
\r
5 using System.Text.RegularExpressions;
\r
6 using System.Diagnostics;
\r
7 using System.Xml.Serialization;
\r
9 namespace Handbrake.Presets
\r
11 public class PresetsHandler
\r
13 List<Preset> presets = new List<Preset>(); // Category+Level+Preset Name: Query
\r
14 List<Preset> user_presets = new List<Preset>(); // Preset Name: Query
\r
15 private static XmlSerializer ser = new XmlSerializer(typeof(List<Preset>));
\r
18 /// Add a new preset to the system
\r
20 /// <param name="presetName">String, The name of the new preset</param>
\r
21 /// <param name="query">String, the CLI query for the new preset</param>
\r
22 /// <param name="pictureSettings"> Bool, store crop/picture sizes in the presets</param>
\r
23 public Boolean addPreset(string presetName, string query, Boolean pictureSettings)
\r
25 if (checkIfPresetExists(presetName) == false)
\r
27 Preset newPreset = new Preset();
\r
28 newPreset.Name = presetName;
\r
29 newPreset.Query = query;
\r
30 newPreset.PictureSettings = pictureSettings;
\r
31 user_presets.Add(newPreset);
\r
32 updateUserPresetsFile();
\r
37 MessageBox.Show("Sorry, that preset name already exists. Please choose another!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
43 /// Remove a preset with a given name from either the built in or user preset list.
\r
45 /// <param name="name">String, the preset name</param>
\r
46 public void remove(string name)
\r
48 List<Preset> newPresets = new List<Preset>();
\r
49 List<Preset> newUserPresets = new List<Preset>();
\r
52 foreach (Preset item in presets)
\r
54 if (item.Name != name)
\r
56 newPresets.Add(item);
\r
59 presets = newPresets;
\r
62 foreach (Preset item in user_presets)
\r
64 if (item.Name != name)
\r
66 newUserPresets.Add(item);
\r
69 user_presets = newUserPresets;
\r
71 // Rebuild the user_presets.xml file
\r
72 updateUserPresetsFile();
\r
73 updatePresetsFile();
\r
77 /// Save changes to a given preset in the user preset list.
\r
79 /// <param name="presetName">String, The name of the new preset</param>
\r
80 /// <param name="query">String, the CLI query for the new preset</param>
\r
81 /// <param name="pictureSettings"> Bool, store crop/picture sizes in the preset</param>
\r
82 public void updatePreset(string presetName, string query, Boolean pictureSettings)
\r
85 foreach (Preset item in user_presets)
\r
87 if (item.Name == presetName)
\r
90 item.PictureSettings = pictureSettings;
\r
91 MessageBox.Show("Changes to \"" + presetName + "\" Saved", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
\r
97 /// Get a List of all the built in preset names.
\r
99 /// <returns>List<String> of preset names</returns>
\r
100 public List<Preset> getBuildInPresets()
\r
106 /// Get a List of all the User preset names.
\r
108 /// <returns>List<String> of preset names</returns>
\r
109 public List<string> getUserPresetNames()
\r
111 List<string> names = new List<string>();
\r
114 foreach (Preset item in user_presets)
\r
116 names.Add(item.Name);
\r
123 /// Return the CLI query for a preset name given in name
\r
125 /// <param name="name">String, The preset's name</param>
\r
126 /// <returns>String, the CLI query for the given preset name</returns>
\r
127 public Preset getPreset(string name)
\r
129 // Built In Presets
\r
130 foreach (Preset item in presets)
\r
132 if (item.Name == name)
\r
137 foreach (Preset item in user_presets)
\r
139 if (item.Name == name)
\r
147 /// Reads the CLI's CLI output format and load's them into the preset List<Preset>
\r
149 public void updateBuiltInPresets()
\r
151 // Create a new tempory file and execute the CLI to get the built in presets.
\r
152 string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");
\r
153 string presetsPath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");
\r
155 string strCmdLine = String.Format(@"cmd /c """"{0}"" --preset-list >""{1}"" 2>&1""", handbrakeCLIPath, presetsPath);
\r
157 ProcessStartInfo hbGetPresets = new ProcessStartInfo("CMD.exe", strCmdLine);
\r
158 hbGetPresets.WindowStyle = ProcessWindowStyle.Hidden;
\r
160 Process hbproc = Process.Start(hbGetPresets);
\r
161 hbproc.WaitForExit();
\r
165 // Clear the current built in presets and now parse the tempory presets file.
\r
167 string filePath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");
\r
168 if (File.Exists(filePath))
\r
170 StreamReader presetInput = new StreamReader(filePath);
\r
172 string category = String.Empty;
\r
173 string level_1_category = String.Empty;
\r
175 while (!presetInput.EndOfStream)
\r
177 string line = presetInput.ReadLine();
\r
178 if (line.Contains("<") && !line.Contains("<<"))
\r
181 category = line.Replace("<", "").Trim();
\r
182 level_1_category = category;
\r
185 if (line.Contains("<<"))
\r
188 category = line.Replace("<<", "").Trim();
\r
191 if (line.Trim().Contains(">>"))
\r
194 category = level_1_category;
\r
197 if (line.Contains("+"))
\r
199 Regex r = new Regex("(: )"); // Split on hyphens.
\r
200 string[] presetName = r.Split(line);
\r
202 Preset newPreset = new Preset();
\r
203 newPreset.Level = level;
\r
204 newPreset.Category = category;
\r
205 newPreset.Name = presetName[0].Replace("+", "").Trim();
\r
206 newPreset.Query = presetName[2];
\r
207 presets.Add(newPreset);
\r
210 presetInput.Close();
\r
211 presetInput.Dispose();
\r
214 // Finally, Create a new or update the current presets.xml file
\r
215 updatePresetsFile();
\r
219 /// Load in the preset data from presets.xml and user_presets.xml
\r
220 /// Load it into the 2 arraylist's presets and user_presets
\r
222 public void loadPresetData()
\r
224 // First clear the presets arraylists
\r
226 user_presets.Clear();
\r
228 string filePath = string.Empty;
\r
230 // Load in the users presets from user_presets.xml
\r
231 filePath = Application.StartupPath.ToString() + "\\presets.xml";
\r
232 if (File.Exists(filePath))
\r
234 using (FileStream strm = new FileStream(filePath, FileMode.Open, FileAccess.Read))
\r
236 if (strm.Length != 0)
\r
238 List<Preset> list = ser.Deserialize(strm) as List<Preset>;
\r
240 foreach (Preset preset in list)
\r
241 presets.Add(preset);
\r
246 // Load in the users presets from user_presets.xml
\r
247 filePath = Application.StartupPath.ToString() + "\\user_presets.xml";
\r
248 if (File.Exists(filePath))
\r
250 using (FileStream strm = new FileStream(filePath, FileMode.Open, FileAccess.Read))
\r
252 if (strm.Length != 0)
\r
254 List<Preset> list = ser.Deserialize(strm) as List<Preset>;
\r
256 foreach (Preset preset in list)
\r
257 user_presets.Add(preset);
\r
264 /// Updates the presets.xml file which contains the built in presets
\r
265 /// It takes the List of Presets and converts them into XML which is stored in presets.xml
\r
267 private void updatePresetsFile()
\r
269 string userPresets = Application.StartupPath.ToString() + "\\presets.xml";
\r
272 using (FileStream strm = new FileStream(userPresets, FileMode.Create, FileAccess.Write))
\r
274 ser.Serialize(strm, presets);
\r
279 catch (Exception exc)
\r
281 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.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
\r
286 /// Updates the user_presets.xml file which contains the built in presets
\r
287 /// It takes the List of Presets and converts them into XML which is stored in user_presets.xml
\r
289 private void updateUserPresetsFile()
\r
291 string userPresets = Application.StartupPath.ToString() + "\\user_presets.xml";
\r
294 using (FileStream strm = new FileStream(userPresets, FileMode.Create, FileAccess.Write))
\r
296 ser.Serialize(strm, user_presets);
\r
301 catch (Exception exc)
\r
303 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.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
\r
308 /// Check if the preset "name" exists in either presets or user_presets lists.
\r
310 /// <param name="name"></param>
\r
311 /// <returns></returns>
\r
312 private Boolean checkIfPresetExists(string name)
\r
314 if (name == string.Empty)
\r
317 // Built In Presets
\r
318 foreach (Preset item in presets)
\r
320 if (item.Name == name)
\r
325 foreach (Preset item in user_presets)
\r
327 if (item.Name == name)
\r
335 /// Check if the user preset "name" exists in user_presets list.
\r
337 /// <param name="name"></param>
\r
338 /// <returns></returns>
\r
339 public Boolean checkIfUserPresetExists(string name)
\r
341 if (name == string.Empty)
\r
345 foreach (Preset item in user_presets)
\r
347 if (item.Name == name)
\r