OSDN Git Service

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