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                     updateUserPresetsFile();\r
93                 }\r
94             }\r
95         }\r
96 \r
97         /// <summary>\r
98         /// Get a List of all the built in preset names.\r
99         /// </summary>\r
100         /// <returns>List<String> of preset names</returns>\r
101         public List<Preset> getBuildInPresets()\r
102         {\r
103             return presets;\r
104         }\r
105 \r
106         /// <summary>\r
107         /// Get a List of all the User preset names.\r
108         /// </summary>\r
109         /// <returns>List<String> of preset names</returns>\r
110         public List<string> getUserPresetNames()\r
111         {\r
112             List<string> names = new List<string>();\r
113 \r
114             // User Presets\r
115             foreach (Preset item in user_presets)\r
116             {\r
117                 names.Add(item.Name);\r
118             }\r
119 \r
120             return names;\r
121         }\r
122 \r
123         /// <summary>\r
124         /// Return the CLI query for a preset name given in name\r
125         /// </summary>\r
126         /// <param name="name">String, The preset's name</param>\r
127         /// <returns>String, the CLI query for the given preset name</returns>\r
128         public Preset getPreset(string name)\r
129         {\r
130             // Built In Presets\r
131             foreach (Preset item in presets)\r
132             {\r
133                 if (item.Name == name)\r
134                     return item;\r
135             }\r
136 \r
137             // User Presets\r
138             foreach (Preset item in user_presets)\r
139             {\r
140                 if (item.Name == name)\r
141                     return item;\r
142             }\r
143 \r
144             return null;\r
145         }\r
146 \r
147         /// <summary>\r
148         /// Reads the CLI's CLI output format and load's them into the preset List<Preset>\r
149         /// </summary>\r
150         public void updateBuiltInPresets()\r
151         {\r
152             // Create a new tempory file and execute the CLI to get the built in presets.\r
153             string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");\r
154             string presetsPath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");\r
155 \r
156             string strCmdLine = String.Format(@"cmd /c """"{0}"" --preset-list >""{1}"" 2>&1""", handbrakeCLIPath, presetsPath);\r
157 \r
158             ProcessStartInfo hbGetPresets = new ProcessStartInfo("CMD.exe", strCmdLine);\r
159             hbGetPresets.WindowStyle = ProcessWindowStyle.Hidden;\r
160 \r
161             Process hbproc = Process.Start(hbGetPresets);\r
162             hbproc.WaitForExit();\r
163             hbproc.Dispose();\r
164             hbproc.Close();\r
165 \r
166             // Clear the current built in presets and now parse the tempory presets file.\r
167             presets.Clear();\r
168             string filePath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");\r
169             if (File.Exists(filePath))\r
170             {\r
171                 StreamReader presetInput = new StreamReader(filePath);\r
172                 int level = 1;\r
173                 string category = String.Empty;\r
174                 string level_1_category = String.Empty;\r
175 \r
176                 while (!presetInput.EndOfStream)\r
177                 {\r
178                     string line = presetInput.ReadLine();\r
179                     if (line.Contains("<") && !line.Contains("<<"))\r
180                     {\r
181                         level = 1;\r
182                         category = line.Replace("<", "").Trim();\r
183                         level_1_category = category;\r
184                     }\r
185 \r
186                     if (line.Contains("<<"))\r
187                     {\r
188                         level = 2;\r
189                         category = line.Replace("<<", "").Trim();\r
190                     }\r
191 \r
192                     if (line.Trim().Contains(">>"))\r
193                     {\r
194                         level = 1;\r
195                         category = level_1_category;\r
196                     }\r
197 \r
198                     if (line.Contains("+"))\r
199                     {\r
200                         Regex r = new Regex("(:  )"); // Split on hyphens. \r
201                         string[] presetName = r.Split(line);\r
202 \r
203                         Preset newPreset = new Preset();\r
204                         newPreset.Level = level;\r
205                         newPreset.Category = category;\r
206                         newPreset.Name = presetName[0].Replace("+", "").Trim();\r
207                         newPreset.Query = presetName[2];\r
208                         presets.Add(newPreset);\r
209                     }\r
210                 }\r
211                 presetInput.Close();\r
212                 presetInput.Dispose();\r
213             }\r
214 \r
215             // Finally, Create a new or update the current presets.xml file\r
216             updatePresetsFile();\r
217         }\r
218 \r
219         /// <summary>\r
220         /// Load in the preset data from presets.xml and user_presets.xml\r
221         /// Load it into the 2 arraylist's presets and user_presets\r
222         /// </summary>\r
223         public void loadPresetData()\r
224         {\r
225             // First clear the presets arraylists\r
226             presets.Clear();\r
227             user_presets.Clear();\r
228 \r
229             // Load in the users presets from user_presets.xml\r
230             string filePath = Application.StartupPath + "\\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                             presets.Add(preset);\r
241                     }\r
242                 }\r
243             }\r
244 \r
245             // Load in the users presets from user_presets.xml\r
246             filePath = Application.StartupPath + "\\user_presets.xml";\r
247             if (File.Exists(filePath))\r
248             {\r
249                 using (FileStream strm = new FileStream(filePath, FileMode.Open, FileAccess.Read))\r
250                 {\r
251                     if (strm.Length != 0)\r
252                     {\r
253                         List<Preset> list = ser.Deserialize(strm) as List<Preset>;\r
254 \r
255                         foreach (Preset preset in list)\r
256                             user_presets.Add(preset);\r
257                     }\r
258                 }\r
259             }\r
260         }\r
261 \r
262         /// <summary>\r
263         /// Updates the presets.xml file which contains the built in presets\r
264         /// It takes the List of Presets and converts them into XML which is stored in presets.xml\r
265         /// </summary>\r
266         private void updatePresetsFile()\r
267         {\r
268             string userPresets = Application.StartupPath + "\\presets.xml";\r
269             try\r
270             {\r
271                 using (FileStream strm = new FileStream(userPresets, FileMode.Create, FileAccess.Write))\r
272                 {\r
273                     ser.Serialize(strm, presets);\r
274                     strm.Close();\r
275                     strm.Dispose();\r
276                 }\r
277             }\r
278             catch (Exception exc)\r
279             {\r
280                 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, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\r
281             }\r
282         }\r
283 \r
284         /// <summary>\r
285         /// Updates the user_presets.xml file which contains the built in presets\r
286         /// It takes the List of Presets and converts them into XML which is stored in user_presets.xml\r
287         /// </summary>\r
288         private void updateUserPresetsFile()\r
289         {\r
290             string userPresets = Application.StartupPath + "\\user_presets.xml";\r
291             try\r
292             {\r
293                 using (FileStream strm = new FileStream(userPresets, FileMode.Create, FileAccess.Write))\r
294                 {\r
295                     ser.Serialize(strm, user_presets);\r
296                     strm.Close();\r
297                     strm.Dispose();\r
298                 }\r
299             }\r
300             catch (Exception exc)\r
301             {\r
302                 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, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\r
303             }\r
304         }\r
305 \r
306         /// <summary>\r
307         /// Check if the preset "name" exists in either presets or user_presets lists.\r
308         /// </summary>\r
309         /// <param name="name"></param>\r
310         /// <returns></returns>\r
311         private Boolean checkIfPresetExists(string name)\r
312         {\r
313             if (name == string.Empty)\r
314                 return true;\r
315 \r
316             // Built In Presets\r
317             foreach (Preset item in presets)\r
318             {\r
319                 if (item.Name == name)\r
320                     return true;\r
321             }\r
322 \r
323             // User Presets\r
324             foreach (Preset item in user_presets)\r
325             {\r
326                 if (item.Name == name)\r
327                     return true;\r
328             }\r
329 \r
330             return false;\r
331         }\r
332 \r
333         /// <summary>\r
334         /// Check if the user preset "name" exists in user_presets list.\r
335         /// </summary>\r
336         /// <param name="name"></param>\r
337         /// <returns></returns>\r
338         public Boolean checkIfUserPresetExists(string name)\r
339         {\r
340             if (name == string.Empty)\r
341                 return false;\r
342 \r
343             // User Presets\r
344             foreach (Preset item in user_presets)\r
345             {\r
346                 if (item.Name == name)\r
347                     return true;\r
348             }\r
349 \r
350             return false;\r
351         }\r
352     }\r
353 }