OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / Presets / PresetsHandler.cs
1 /*  PresetHandler.cs $\r
2         \r
3            This file is part of the HandBrake source code.\r
4            Homepage: <http://handbrake.fr>.\r
5            It may be used under the terms of the GNU General Public License. */\r
6 using System;\r
7 using System.Collections.Generic;\r
8 using System.Drawing;\r
9 using System.Windows.Forms;\r
10 using System.IO;\r
11 using System.Text.RegularExpressions;\r
12 using System.Diagnostics;\r
13 using System.Xml.Serialization;\r
14 using System.Collections;\r
15 \r
16 namespace Handbrake.Presets\r
17 {\r
18     public class PresetsHandler\r
19     {\r
20         List<Preset> presets = new List<Preset>();\r
21         List<Preset> user_presets = new List<Preset>();\r
22         private static readonly XmlSerializer ser = new XmlSerializer(typeof(List<Preset>));\r
23         String userPresetFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\user_presets.xml";\r
24         string hbPresetFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\presets.xml";\r
25 \r
26         /// <summary>\r
27         /// Add a new preset to the system\r
28         /// </summary>\r
29         /// <param name="presetName">String, The name of the new preset</param>\r
30         /// <param name="query">String, the CLI query for the new preset</param>\r
31         /// <param name="pictureSettings"> Bool, store crop/picture sizes in the presets</param>\r
32         public Boolean addPreset(string presetName, string query, Boolean pictureSettings)\r
33         {\r
34             if (checkIfPresetExists(presetName) == false)\r
35             {\r
36                 Preset newPreset = new Preset { Name = presetName, Query = query, PictureSettings = pictureSettings, Version = Properties.Settings.Default.hb_version};\r
37                 user_presets.Add(newPreset);\r
38                 updateUserPresetsFile();\r
39                 return true;\r
40             }\r
41             MessageBox.Show("Sorry, that preset name already exists. Please choose another!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
42             return false;\r
43         }\r
44 \r
45         /// <summary>\r
46         /// Remove a preset with a given name from either the built in or user preset list.\r
47         /// </summary>\r
48         /// <param name="name">String, the preset name</param>\r
49         public void remove(string name)\r
50         {\r
51             List<Preset> newPresets = new List<Preset>();\r
52             List<Preset> newUserPresets = new List<Preset>();\r
53 \r
54             // Built In Presets\r
55             foreach (Preset item in presets)\r
56             {\r
57                 if (item.Name != name)\r
58                 {\r
59                     newPresets.Add(item);\r
60                 }\r
61             }\r
62             presets = newPresets;\r
63 \r
64             // User Presets\r
65             foreach (Preset item in user_presets)\r
66             {\r
67                 if (item.Name != name)\r
68                 {\r
69                     newUserPresets.Add(item);\r
70                 }\r
71             }\r
72             user_presets = newUserPresets;\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         /// Save changes to a given preset in the user preset list.\r
81         /// </summary>\r
82         /// <param name="presetName">String, The name of the new preset</param>\r
83         /// <param name="query">String, the CLI query for the new preset</param>\r
84         /// <param name="pictureSettings"> Bool, store crop/picture sizes in the preset</param>\r
85         public void updatePreset(string presetName, string query, Boolean pictureSettings)\r
86         {\r
87             // User Presets\r
88             foreach (Preset item in user_presets)\r
89             {\r
90                 if (item.Name == presetName)\r
91                 {\r
92                     item.Query = query;\r
93                     item.PictureSettings = pictureSettings;\r
94                     MessageBox.Show("Changes to \"" + presetName + "\" Saved", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);\r
95                     updateUserPresetsFile();\r
96                 }\r
97             }\r
98         }\r
99 \r
100         /// <summary>\r
101         /// Return the CLI query for a preset name given in name\r
102         /// </summary>\r
103         /// <param name="name">String, The preset's name</param>\r
104         /// <returns>String, the CLI query for the given preset name</returns>    not\r
105         public Preset getPreset(string name)\r
106         {\r
107             // Built In Presets\r
108             foreach (Preset item in presets)\r
109             {\r
110                 if (item.Name == name)\r
111                     return item;\r
112             }\r
113 \r
114             // User Presets\r
115             foreach (Preset item in user_presets)\r
116             {\r
117                 if (item.Name == name)\r
118                     return item;\r
119             }\r
120 \r
121             return null;\r
122         }\r
123 \r
124         /// <summary>\r
125         /// Reads the CLI's CLI output format and load's them into the preset List<Preset>\r
126         /// </summary>\r
127         public void updateBuiltInPresets()\r
128         {\r
129             // Create a new tempory file and execute the CLI to get the built in presets.\r
130             string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");\r
131             string presetsPath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");\r
132             string strCmdLine = String.Format(@"cmd /c """"{0}"" --preset-list >""{1}"" 2>&1""", handbrakeCLIPath, presetsPath);\r
133 \r
134             ProcessStartInfo hbGetPresets = new ProcessStartInfo("CMD.exe", strCmdLine) { WindowStyle = ProcessWindowStyle.Hidden };\r
135             Process hbproc = Process.Start(hbGetPresets);\r
136             if (hbproc != null)\r
137             {\r
138                 hbproc.WaitForExit();\r
139                 hbproc.Dispose();\r
140                 hbproc.Close();\r
141             }\r
142 \r
143             // Clear the current built in presets and now parse the tempory presets file.\r
144             presets.Clear();\r
145             string filePath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");\r
146 \r
147             if (File.Exists(filePath))\r
148             {\r
149                 StreamReader presetInput = new StreamReader(filePath);\r
150 \r
151                 int level = 1;\r
152                 string category = String.Empty;\r
153                 string level_1_category = String.Empty;\r
154 \r
155                 while (!presetInput.EndOfStream)\r
156                 {\r
157                     string line = presetInput.ReadLine();\r
158                     if (line.Contains("<") && !line.Contains("<<")) // Found the beginning of a preset block \r
159                     {\r
160                         level = 1;\r
161                         category = line.Replace("<", "").Trim();\r
162                         level_1_category = category;\r
163                     }\r
164 \r
165                     if (line.Contains("<<")) // found a sub preset block\r
166                     {\r
167                         level = 2;\r
168                         category = line.Replace("<<", "").Trim();\r
169                     }\r
170 \r
171                     if (line.Trim().Contains(">>")) // End of sub preset block\r
172                     {\r
173                         level = 1;\r
174                         category = level_1_category;\r
175                     }\r
176 \r
177                     if (line.Contains("+")) // A Preset\r
178                     {\r
179                         Regex r = new Regex("(:  )"); // Split on hyphens. \r
180                         string[] presetName = r.Split(line);\r
181 \r
182                         Preset newPreset = new Preset\r
183                                                {\r
184                                                    Level = level,\r
185                                                    Category = category,\r
186                                                    TopCategory = level_1_category,\r
187                                                    Name = presetName[0].Replace("+", "").Trim(),\r
188                                                    Query = presetName[2],\r
189                                                    Version = Properties.Settings.Default.hb_version\r
190                                                };\r
191                         presets.Add(newPreset);\r
192                     }\r
193                 }\r
194                 presetInput.Close();\r
195                 presetInput.Dispose();\r
196             }\r
197 \r
198             // Finally, Create a new or update the current presets.xml file\r
199             updatePresetsFile();\r
200         }\r
201 \r
202         /// <summary>\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
205         /// </summary>\r
206         private void loadPresetData()\r
207         {\r
208             // First clear the presets arraylists\r
209             presets.Clear();\r
210             user_presets.Clear();\r
211 \r
212             // Load in the users presets from user_presets.xml\r
213             if (File.Exists(hbPresetFile))\r
214             {\r
215                 using (FileStream strm = new FileStream(hbPresetFile, FileMode.Open, FileAccess.Read))\r
216                 {\r
217                     if (strm.Length != 0)\r
218                     {\r
219                         List<Preset> list = ser.Deserialize(strm) as List<Preset>;\r
220 \r
221                         if (list != null)\r
222                             foreach (Preset preset in list)\r
223                                 presets.Add(preset);\r
224                     }\r
225                 }\r
226             }\r
227 \r
228             // Load in the users presets from user_presets.xml\r
229             if (File.Exists(userPresetFile))\r
230             {\r
231                 using (FileStream strm = new FileStream(userPresetFile, FileMode.Open, FileAccess.Read))\r
232                 {\r
233                     if (strm.Length != 0)\r
234                     {\r
235                         List<Preset> list = ser.Deserialize(strm) as List<Preset>;\r
236 \r
237                         if (list != null)\r
238                             foreach (Preset preset in list)\r
239                                 user_presets.Add(preset);\r
240                     }\r
241                 }\r
242             }\r
243         }\r
244 \r
245         /// <summary>\r
246         /// Setup the frmMain preset panel\r
247         /// </summary>\r
248         /// <param name="presetPanel"></param>\r
249         public void getPresetPanel(ref TreeView presetPanel)\r
250         {\r
251             this.loadPresetData();\r
252             presetPanel.Nodes.Clear();\r
253 \r
254             if (presets.Count != 0)\r
255             {\r
256                 string category = presets[0].Category;\r
257                 TreeNode rootNode = new TreeNode(presets[0].Category);\r
258                 TreeNode childNode = null;\r
259                 Boolean addChildNode = false;\r
260 \r
261                 foreach (Preset preset in presets)\r
262                 {\r
263                     // Deal with Root\r
264                     if (preset.Category == category && preset.TopCategory == category)\r
265                         rootNode.Nodes.Add(preset.Name);\r
266                     else if (preset.Category != category && preset.TopCategory == category) // Deal with child nodes for that root\r
267                     {\r
268                         if (childNode == null) // For the first child node\r
269                             childNode = new TreeNode(preset.Category);\r
270 \r
271                         childNode.Nodes.Add(preset.Name);\r
272                         addChildNode = true;\r
273                     }\r
274                     else if (preset.Category != category && preset.TopCategory != category && preset.Level == 1)// Deal with changing root nodes\r
275                     {\r
276                         // If we find there are child nodes, add them to the current root node set before adding the rootnode to the panel.\r
277                         if (addChildNode)\r
278                         {\r
279                             rootNode.Nodes.Add(childNode);\r
280                             childNode = null;\r
281                             addChildNode = false;\r
282                         }\r
283                         // Add the current rootnodes to the panel, and prepare for a new category of presets\r
284                         presetPanel.Nodes.Add(rootNode);\r
285                         rootNode = new TreeNode(preset.Category);\r
286                         rootNode.Nodes.Add(preset.Name);\r
287 \r
288                         category = preset.Category;\r
289                     }\r
290                 }\r
291                 presetPanel.Nodes.Add(rootNode); // Add the final set of nodes \r
292             }\r
293 \r
294             // User Presets\r
295             foreach (Preset preset in user_presets)\r
296             {\r
297                 TreeNode preset_treeview = new TreeNode(preset.Name) { ForeColor = Color.Black };\r
298                 presetPanel.Nodes.Add(preset_treeview);\r
299             }\r
300         }\r
301 \r
302         /// <summary>\r
303         /// Updates the presets.xml file which contains the built in presets\r
304         /// It takes the List of Presets and converts them into XML which is stored in presets.xml\r
305         /// </summary>\r
306         private void updatePresetsFile()\r
307         {\r
308             string userPresets = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\presets.xml";\r
309             try\r
310             {\r
311                 using (FileStream strm = new FileStream(userPresets, FileMode.Create, FileAccess.Write))\r
312                 {\r
313                     ser.Serialize(strm, presets);\r
314                     strm.Close();\r
315                     strm.Dispose();\r
316                 }\r
317             }\r
318             catch (Exception exc)\r
319             {\r
320                 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
321             }\r
322         }\r
323 \r
324         /// <summary>\r
325         /// Updates the user_presets.xml file which contains the built in presets\r
326         /// It takes the List of Presets and converts them into XML which is stored in user_presets.xml\r
327         /// </summary>\r
328         private void updateUserPresetsFile()\r
329         {\r
330             try\r
331             {\r
332                 using (FileStream strm = new FileStream(userPresetFile, FileMode.Create, FileAccess.Write))\r
333                 {\r
334                     ser.Serialize(strm, user_presets);\r
335                     strm.Close();\r
336                     strm.Dispose();\r
337                 }\r
338             }\r
339             catch (Exception exc)\r
340             {\r
341                 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
342             }\r
343         }\r
344 \r
345         /// <summary>\r
346         /// Check if the preset "name" exists in either presets or user_presets lists.\r
347         /// </summary>\r
348         /// <param name="name"></param>\r
349         /// <returns></returns>\r
350         private Boolean checkIfPresetExists(string name)\r
351         {\r
352             if (name == string.Empty)\r
353                 return true;\r
354 \r
355             // Built In Presets\r
356             foreach (Preset item in presets)\r
357             {\r
358                 if (item.Name == name)\r
359                     return true;\r
360             }\r
361 \r
362             // User Presets\r
363             foreach (Preset item in user_presets)\r
364             {\r
365                 if (item.Name == name)\r
366                     return true;\r
367             }\r
368 \r
369             return false;\r
370         }\r
371 \r
372         /// <summary>\r
373         /// Check if the user preset "name" exists in user_presets list.\r
374         /// </summary>\r
375         /// <param name="name"></param>\r
376         /// <returns></returns>\r
377         public Boolean checkIfUserPresetExists(string name)\r
378         {\r
379             if (name == string.Empty)\r
380                 return false;\r
381 \r
382             // User Presets\r
383             foreach (Preset item in user_presets)\r
384             {\r
385                 if (item.Name == name)\r
386                     return true;\r
387             }\r
388 \r
389             return false;\r
390         }\r
391 \r
392         /// <summary>\r
393         /// Check if the built in presets stored are not out of date.\r
394         /// Update them if they are.\r
395         /// </summary>\r
396         /// <returns></returns>\r
397         public Boolean checkIfPresetsAreOutOfDate()\r
398         {\r
399             loadPresetData();\r
400             // Update built-in presets if the built-in presets belong to an older version.\r
401             if (presets.Count != 0)\r
402                 if (presets[0].Version != Properties.Settings.Default.hb_version)\r
403                 {\r
404                     updateBuiltInPresets();\r
405                     return true;\r
406                 }\r
407 \r
408             return false;\r
409         }\r
410     }\r
411 }