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, 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 // Now, Update the presets.xml and user_presets.xml file with the new items.
\r
72 string userPresets = Application.StartupPath.ToString() + "\\user_presets.xml";
\r
73 string presetsFile = Application.StartupPath.ToString() + "\\presets.xml";
\r
75 // Rebuild the user_presets.xml file
\r
76 updateUserPresetsFile();
\r
77 updatePresetsFile();
\r
81 /// Get a List of all the built in preset names.
\r
83 /// <returns>List<String> of preset names</returns>
\r
84 public List<Preset> getBuildInPresets()
\r
90 /// Get a List of all the User preset names.
\r
92 /// <returns>List<String> of preset names</returns>
\r
93 public List<string> getUserPresetNames()
\r
95 List<string> names = new List<string>();
\r
98 foreach (Preset item in user_presets)
\r
100 names.Add(item.Name);
\r
107 /// Return the CLI query for a preset name given in name
\r
109 /// <param name="name">String, The preset's name</param>
\r
110 /// <returns>String, the CLI query for the given preset name</returns>
\r
111 public Preset getPreset(string name)
\r
113 // Built In Presets
\r
114 foreach (Preset item in presets)
\r
116 if (item.Name == name)
\r
121 foreach (Preset item in user_presets)
\r
123 if (item.Name == name)
\r
131 /// Reads the CLI's CLI output format and load's them into the preset List<Preset>
\r
133 public void updateBuiltInPresets()
\r
135 // Create a new tempory file and execute the CLI to get the built in presets.
\r
136 string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");
\r
137 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);
\r
142 hbGetPresets.WindowStyle = ProcessWindowStyle.Hidden;
\r
144 Process hbproc = Process.Start(hbGetPresets);
\r
145 hbproc.WaitForExit();
\r
149 // Clear the current built in presets and now parse the tempory presets file.
\r
151 string filePath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");
\r
152 if (File.Exists(filePath))
\r
154 StreamReader presetInput = new StreamReader(filePath);
\r
156 string category = String.Empty;
\r
157 string level_1_category = String.Empty;
\r
159 while (!presetInput.EndOfStream)
\r
161 string line = presetInput.ReadLine();
\r
162 if (line.Contains("<") && !line.Contains("<<"))
\r
165 category = line.Replace("<", "").Trim();
\r
166 level_1_category = category;
\r
169 if (line.Contains("<<"))
\r
172 category = line.Replace("<<", "").Trim();
\r
175 if (line.Trim().Contains(">>"))
\r
178 category = level_1_category;
\r
181 if (line.Contains("+"))
\r
183 Regex r = new Regex("(: )"); // Split on hyphens.
\r
184 string[] presetName = r.Split(line);
\r
186 Preset newPreset = new Preset();
\r
187 newPreset.Level = level;
\r
188 newPreset.Category = category;
\r
189 newPreset.Name = presetName[0].Replace("+", "").Trim();
\r
190 newPreset.Query = presetName[2];
\r
191 presets.Add(newPreset);
\r
194 presetInput.Close();
\r
195 presetInput.Dispose();
\r
198 // Finally, Create a new or update the current presets.xml file
\r
199 updatePresetsFile();
\r
203 /// Load in the preset data from presets.xml and user_presets.xml
\r
204 /// Load it into the 2 arraylist's presets and user_presets
\r
206 public void loadPresetData()
\r
208 // First clear the presets arraylists
\r
210 user_presets.Clear();
\r
212 string filePath = string.Empty;
\r
214 // Load in the users presets from user_presets.xml
\r
215 filePath = Application.StartupPath.ToString() + "\\presets.xml";
\r
216 if (File.Exists(filePath))
\r
218 using (FileStream strm = new FileStream(filePath, FileMode.Open, FileAccess.Read))
\r
220 if (strm.Length != 0)
\r
222 List<Preset> list = ser.Deserialize(strm) as List<Preset>;
\r
224 foreach (Preset preset in list)
\r
225 presets.Add(preset);
\r
230 // Load in the users presets from user_presets.xml
\r
231 filePath = Application.StartupPath.ToString() + "\\user_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 user_presets.Add(preset);
\r
248 /// Updates the presets.xml file which contains the built in presets
\r
249 /// It takes the List of Presets and converts them into XML which is stored in presets.xml
\r
251 private void updatePresetsFile()
\r
253 string userPresets = Application.StartupPath.ToString() + "\\presets.xml";
\r
256 using (FileStream strm = new FileStream(userPresets, FileMode.Create, FileAccess.Write))
\r
258 ser.Serialize(strm, presets);
\r
263 catch (Exception exc)
\r
265 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
270 /// Updates the user_presets.xml file which contains the built in presets
\r
271 /// It takes the List of Presets and converts them into XML which is stored in user_presets.xml
\r
273 private void updateUserPresetsFile()
\r
275 string userPresets = Application.StartupPath.ToString() + "\\user_presets.xml";
\r
278 using (FileStream strm = new FileStream(userPresets, FileMode.Create, FileAccess.Write))
\r
280 ser.Serialize(strm, user_presets);
\r
285 catch (Exception exc)
\r
287 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
292 /// Check if the preset "name" exists in either presets or user_presets lists.
\r
294 /// <param name="name"></param>
\r
295 /// <returns></returns>
\r
296 private Boolean checkIfPresetExists(string name)
\r
298 if (name == string.Empty)
\r
301 // Built In Presets
\r
302 foreach (Preset item in presets)
\r
304 if (item.Name == name)
\r
309 foreach (Preset item in user_presets)
\r
311 if (item.Name == name)
\r