2 using System.Collections.Generic;
\r
4 using System.Windows.Forms;
\r
6 using System.Text.RegularExpressions;
\r
7 using System.Diagnostics;
\r
8 using System.Xml.Serialization;
\r
10 namespace Handbrake.Presets
\r
12 public class PresetsHandler
\r
14 List<Preset> presets = new List<Preset>(); // Category+Level+Preset Name: Query
\r
15 List<Preset> user_presets = new List<Preset>(); // Preset Name: Query
\r
16 private static XmlSerializer ser = new XmlSerializer(typeof(List<Preset>));
\r
19 /// Add a new preset to the system
\r
21 /// <param name="presetName">String, The name of the new preset</param>
\r
22 /// <param name="query">String, the CLI query for the new preset</param>
\r
23 public Boolean addPreset(string presetName, string query)
\r
25 if (checkIfPresetExists(presetName) == false)
\r
27 Preset newPreset = new Preset();
\r
28 newPreset.Name = presetName;
\r
29 newPreset.Query = query;
\r
30 user_presets.Add(newPreset);
\r
31 updateUserPresetsFile();
\r
36 MessageBox.Show("Sorry, that preset name already exists. Please choose another!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
42 /// Remove a preset with a given name from either the built in or user preset list.
\r
44 /// <param name="name">String, the preset name</param>
\r
45 public void remove(string name)
\r
47 List<Preset> newPresets = new List<Preset>();
\r
48 List<Preset> newUserPresets = new List<Preset>();
\r
51 foreach (Preset item in presets)
\r
53 if (item.Name != name)
\r
55 newPresets.Add(item);
\r
58 presets = newPresets;
\r
61 foreach (Preset item in user_presets)
\r
63 if (item.Name != name)
\r
65 newUserPresets.Add(item);
\r
68 user_presets = newUserPresets;
\r
70 // Now, Update the presets.xml and user_presets.xml file with the new items.
\r
71 string userPresets = Application.StartupPath.ToString() + "\\user_presets.xml";
\r
72 string presetsFile = Application.StartupPath.ToString() + "\\presets.xml";
\r
74 // Rebuild the user_presets.xml file
\r
75 updateUserPresetsFile();
\r
76 updatePresetsFile();
\r
80 /// Get a List of all the built in preset names.
\r
82 /// <returns>List<String> of preset names</returns>
\r
83 public List<Preset> getBuildInPresets()
\r
89 /// Get a List of all the User preset names.
\r
91 /// <returns>List<String> of preset names</returns>
\r
92 public List<string> getUserPresetNames()
\r
94 List<string> names = new List<string>();
\r
97 foreach (Preset item in user_presets)
\r
99 names.Add(item.Name);
\r
106 /// Return the CLI query for a preset name given in name
\r
108 /// <param name="name">String, The preset's name</param>
\r
109 /// <returns>String, the CLI query for the given preset name</returns>
\r
110 public string getCliForPreset(string name)
\r
112 // Built In Presets
\r
113 foreach (Preset item in presets)
\r
115 if (item.Name == name)
\r
120 foreach (Preset item in user_presets)
\r
122 if (item.Name == name)
\r
130 /// Reads the CLI's CLI output format and load's them into the preset List<Preset>
\r
132 public void updateBuiltInPresets()
\r
134 // Create a new tempory file and execute the CLI to get the built in presets.
\r
135 string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");
\r
136 string presetsPath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");
\r
138 string strCmdLine = String.Format(@"cmd /c """"{0}"" --preset-list >""{1}"" 2>&1""", handbrakeCLIPath, presetsPath);
\r
140 ProcessStartInfo hbGetPresets = new ProcessStartInfo("CMD.exe", strCmdLine);
\r
141 hbGetPresets.WindowStyle = ProcessWindowStyle.Hidden;
\r
143 Process hbproc = Process.Start(hbGetPresets);
\r
144 hbproc.WaitForExit();
\r
148 // Clear the current built in presets and now parse the tempory presets file.
\r
150 string filePath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");
\r
151 if (File.Exists(filePath))
\r
153 StreamReader presetInput = new StreamReader(filePath);
\r
155 string category = String.Empty;
\r
156 string level_1_category = String.Empty;
\r
158 while (!presetInput.EndOfStream)
\r
160 string line = presetInput.ReadLine();
\r
161 if (line.Contains("<") && !line.Contains("<<"))
\r
164 category = line.Replace("<", "").Trim();
\r
165 level_1_category = category;
\r
168 if (line.Contains("<<"))
\r
171 category = line.Replace("<<", "").Trim();
\r
174 if (line.Trim().Contains(">>"))
\r
177 category = level_1_category;
\r
180 if (line.Contains("+"))
\r
182 Regex r = new Regex("(: )"); // Split on hyphens.
\r
183 string[] presetName = r.Split(line);
\r
185 Preset newPreset = new Preset();
\r
186 newPreset.Level = level;
\r
187 newPreset.Category = category;
\r
188 newPreset.Name = presetName[0].Replace("+", "").Trim();
\r
189 newPreset.Query = presetName[2];
\r
190 presets.Add(newPreset);
\r
193 presetInput.Close();
\r
194 presetInput.Dispose();
\r
197 // Finally, Create a new or update the current presets.xml file
\r
198 updatePresetsFile();
\r
202 /// Load in the preset data from presets.xml and user_presets.xml
\r
203 /// Load it into the 2 arraylist's presets and user_presets
\r
205 public void loadPresetData()
\r
207 // First clear the presets arraylists
\r
209 user_presets.Clear();
\r
211 string filePath = string.Empty;
\r
213 // Load in the users presets from user_presets.xml
\r
214 filePath = Application.StartupPath.ToString() + "\\presets.xml";
\r
215 if (File.Exists(filePath))
\r
217 using (FileStream strm = new FileStream(filePath, FileMode.Open, FileAccess.Read))
\r
219 if (strm.Length != 0)
\r
221 List<Preset> list = ser.Deserialize(strm) as List<Preset>;
\r
223 foreach (Preset preset in list)
\r
224 presets.Add(preset);
\r
229 // Load in the users presets from user_presets.xml
\r
230 filePath = Application.StartupPath.ToString() + "\\user_presets.xml";
\r
231 if (File.Exists(filePath))
\r
233 using (FileStream strm = new FileStream(filePath, FileMode.Open, FileAccess.Read))
\r
235 if (strm.Length != 0)
\r
237 List<Preset> list = ser.Deserialize(strm) as List<Preset>;
\r
239 foreach (Preset preset in list)
\r
240 user_presets.Add(preset);
\r
247 /// Updates the presets.xml file which contains the built in presets
\r
248 /// It takes the List of Presets and converts them into XML which is stored in presets.xml
\r
250 private void updatePresetsFile()
\r
252 string userPresets = Application.StartupPath.ToString() + "\\presets.xml";
\r
255 using (FileStream strm = new FileStream(userPresets, FileMode.Create, FileAccess.Write))
\r
257 ser.Serialize(strm, presets);
\r
262 catch (Exception exc)
\r
264 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
269 /// Updates the user_presets.xml file which contains the built in presets
\r
270 /// It takes the List of Presets and converts them into XML which is stored in user_presets.xml
\r
272 private void updateUserPresetsFile()
\r
274 string userPresets = Application.StartupPath.ToString() + "\\user_presets.xml";
\r
277 using (FileStream strm = new FileStream(userPresets, FileMode.Create, FileAccess.Write))
\r
279 ser.Serialize(strm, user_presets);
\r
284 catch (Exception exc)
\r
286 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
291 /// Check if the preset "name" exists in either presets or user_presets lists.
\r
293 /// <param name="name"></param>
\r
294 /// <returns></returns>
\r
295 private Boolean checkIfPresetExists(string name)
\r
297 if (name == string.Empty)
\r
300 // Built In Presets
\r
301 foreach (Preset item in presets)
\r
303 if (item.Name == name)
\r
308 foreach (Preset item in user_presets)
\r
310 if (item.Name == name)
\r