OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / Presets / PresetsHandler.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using System.Windows.Forms;\r
4 using System.IO;\r
5 using System.Text.RegularExpressions;\r
6 using System.Diagnostics;\r
7 using System.Xml.Serialization;\r
8 \r
9 namespace Handbrake.Presets\r
10 {\r
11     public class PresetsHandler\r
12     {\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
16 \r
17         /// <summary>\r
18         /// Add a new preset to the system\r
19         /// </summary>\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
24         {\r
25             if (checkIfPresetExists(presetName) == false)\r
26             {\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
33                 return true;\r
34             }\r
35             else\r
36             {\r
37                 MessageBox.Show("Sorry, that preset name already exists. Please choose another!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
38                 return false;\r
39             }\r
40         }\r
41 \r
42         /// <summary>\r
43         /// Remove a preset with a given name from either the built in or user preset list.\r
44         /// </summary>\r
45         /// <param name="name">String, the preset name</param>\r
46         public void remove(string name)\r
47         {\r
48             List<Preset> newPresets = new List<Preset>();\r
49             List<Preset> newUserPresets = new List<Preset>();\r
50 \r
51             // Built In Presets\r
52             foreach (Preset item in presets)\r
53             {\r
54                 if (item.Name != name)\r
55                 {\r
56                     newPresets.Add(item);\r
57                 }\r
58             }\r
59             presets = newPresets;\r
60 \r
61             // User Presets\r
62             foreach (Preset item in user_presets)\r
63             {\r
64                 if (item.Name != name)\r
65                 {\r
66                     newUserPresets.Add(item);\r
67                 }\r
68             }\r
69             user_presets = newUserPresets;\r
70 \r
71             // Rebuild the user_presets.xml file\r
72             updateUserPresetsFile();\r
73             updatePresetsFile();\r
74         }\r
75 \r
76         /// <summary>\r
77         /// Save changes to a given preset in the user preset list.\r
78         /// </summary>\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
83         {\r
84             // User Presets\r
85             foreach (Preset item in user_presets)\r
86             {\r
87                 if (item.Name == presetName)\r
88                 {\r
89                     item.Query = query;\r
90                     item.PictureSettings = pictureSettings;\r
91                     MessageBox.Show("Changes to \"" + presetName + "\" Saved", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);\r
92                 }\r
93             }\r
94         }\r
95 \r
96         /// <summary>\r
97         /// Get a List of all the built in preset names.\r
98         /// </summary>\r
99         /// <returns>List<String> of preset names</returns>\r
100         public List<Preset> getBuildInPresets()\r
101         {\r
102             return presets;\r
103         }\r
104 \r
105         /// <summary>\r
106         /// Get a List of all the User preset names.\r
107         /// </summary>\r
108         /// <returns>List<String> of preset names</returns>\r
109         public List<string> getUserPresetNames()\r
110         {\r
111             List<string> names = new List<string>();\r
112 \r
113             // User Presets\r
114             foreach (Preset item in user_presets)\r
115             {\r
116                 names.Add(item.Name);\r
117             }\r
118 \r
119             return names;\r
120         }\r
121 \r
122         /// <summary>\r
123         /// Return the CLI query for a preset name given in name\r
124         /// </summary>\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
128         {\r
129             // Built In Presets\r
130             foreach (Preset item in presets)\r
131             {\r
132                 if (item.Name == name)\r
133                     return item;\r
134             }\r
135 \r
136             // User Presets\r
137             foreach (Preset item in user_presets)\r
138             {\r
139                 if (item.Name == name)\r
140                     return item;\r
141             }\r
142 \r
143             return null;\r
144         }\r
145 \r
146         /// <summary>\r
147         /// Reads the CLI's CLI output format and load's them into the preset List<Preset>\r
148         /// </summary>\r
149         public void updateBuiltInPresets()\r
150         {\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
154 \r
155             string strCmdLine = String.Format(@"cmd /c """"{0}"" --preset-list >""{1}"" 2>&1""", handbrakeCLIPath, presetsPath);\r
156 \r
157             ProcessStartInfo hbGetPresets = new ProcessStartInfo("CMD.exe", strCmdLine);\r
158             hbGetPresets.WindowStyle = ProcessWindowStyle.Hidden;\r
159 \r
160             Process hbproc = Process.Start(hbGetPresets);\r
161             hbproc.WaitForExit();\r
162             hbproc.Dispose();\r
163             hbproc.Close();\r
164 \r
165             // Clear the current built in presets and now parse the tempory presets file.\r
166             presets.Clear();\r
167             string filePath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");\r
168             if (File.Exists(filePath))\r
169             {\r
170                 StreamReader presetInput = new StreamReader(filePath);\r
171                 int level = 1;\r
172                 string category = String.Empty;\r
173                 string level_1_category = String.Empty;\r
174 \r
175                 while (!presetInput.EndOfStream)\r
176                 {\r
177                     string line = presetInput.ReadLine();\r
178                     if (line.Contains("<") && !line.Contains("<<"))\r
179                     {\r
180                         level = 1;\r
181                         category = line.Replace("<", "").Trim();\r
182                         level_1_category = category;\r
183                     }\r
184 \r
185                     if (line.Contains("<<"))\r
186                     {\r
187                         level = 2;\r
188                         category = line.Replace("<<", "").Trim();\r
189                     }\r
190 \r
191                     if (line.Trim().Contains(">>"))\r
192                     {\r
193                         level = 1;\r
194                         category = level_1_category;\r
195                     }\r
196 \r
197                     if (line.Contains("+"))\r
198                     {\r
199                         Regex r = new Regex("(:  )"); // Split on hyphens. \r
200                         string[] presetName = r.Split(line);\r
201 \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
208                     }\r
209                 }\r
210                 presetInput.Close();\r
211                 presetInput.Dispose();\r
212             }\r
213 \r
214             // Finally, Create a new or update the current presets.xml file\r
215             updatePresetsFile();\r
216         }\r
217 \r
218         /// <summary>\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
221         /// </summary>\r
222         public void loadPresetData()\r
223         {\r
224             // First clear the presets arraylists\r
225             presets.Clear();\r
226             user_presets.Clear();\r
227 \r
228             string filePath = string.Empty;\r
229 \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
233             {\r
234                 using (FileStream strm = new FileStream(filePath, FileMode.Open, FileAccess.Read))\r
235                 {\r
236                     if (strm.Length != 0)\r
237                     {\r
238                         List<Preset> list = ser.Deserialize(strm) as List<Preset>;\r
239 \r
240                         foreach (Preset preset in list)\r
241                             presets.Add(preset);\r
242                     }\r
243                 }\r
244             }\r
245 \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
249             {\r
250                 using (FileStream strm = new FileStream(filePath, FileMode.Open, FileAccess.Read))\r
251                 {\r
252                     if (strm.Length != 0)\r
253                     {\r
254                         List<Preset> list = ser.Deserialize(strm) as List<Preset>;\r
255 \r
256                         foreach (Preset preset in list)\r
257                             user_presets.Add(preset);\r
258                     }\r
259                 }\r
260             }\r
261         }\r
262 \r
263         /// <summary>\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
266         /// </summary>\r
267         private void updatePresetsFile()\r
268         {\r
269             string userPresets = Application.StartupPath.ToString() + "\\presets.xml";\r
270             try\r
271             {\r
272                 using (FileStream strm = new FileStream(userPresets, FileMode.Create, FileAccess.Write))\r
273                 {\r
274                     ser.Serialize(strm, presets);\r
275                     strm.Close();\r
276                     strm.Dispose();\r
277                 }\r
278             }\r
279             catch (Exception exc)\r
280             {\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
282             }\r
283         }\r
284 \r
285         /// <summary>\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
288         /// </summary>\r
289         private void updateUserPresetsFile()\r
290         {\r
291             string userPresets = Application.StartupPath.ToString() + "\\user_presets.xml";\r
292             try\r
293             {\r
294                 using (FileStream strm = new FileStream(userPresets, FileMode.Create, FileAccess.Write))\r
295                 {\r
296                     ser.Serialize(strm, user_presets);\r
297                     strm.Close();\r
298                     strm.Dispose();\r
299                 }\r
300             }\r
301             catch (Exception exc)\r
302             {\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
304             }\r
305         }\r
306 \r
307         /// <summary>\r
308         /// Check if the preset "name" exists in either presets or user_presets lists.\r
309         /// </summary>\r
310         /// <param name="name"></param>\r
311         /// <returns></returns>\r
312         private Boolean checkIfPresetExists(string name)\r
313         {\r
314             if (name == string.Empty)\r
315                 return true;\r
316 \r
317             // Built In Presets\r
318             foreach (Preset item in presets)\r
319             {\r
320                 if (item.Name == name)\r
321                     return true;\r
322             }\r
323 \r
324             // User Presets\r
325             foreach (Preset item in user_presets)\r
326             {\r
327                 if (item.Name == name)\r
328                     return true;\r
329             }\r
330 \r
331             return false;\r
332         }\r
333 \r
334         /// <summary>\r
335         /// Check if the user preset "name" exists in user_presets list.\r
336         /// </summary>\r
337         /// <param name="name"></param>\r
338         /// <returns></returns>\r
339         public Boolean checkIfUserPresetExists(string name)\r
340         {\r
341             if (name == string.Empty)\r
342                 return false;\r
343 \r
344             // User Presets\r
345             foreach (Preset item in user_presets)\r
346             {\r
347                 if (item.Name == name)\r
348                     return true;\r
349             }\r
350 \r
351             return false;\r
352         }\r
353     }\r
354 }