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 \r
15 namespace Handbrake.Presets\r
16 {\r
17     public class PresetsHandler\r
18     {\r
19         List<Preset> presets = new List<Preset>();\r
20         List<Preset> user_presets = new List<Preset>();\r
21         private static readonly XmlSerializer ser = new XmlSerializer(typeof(List<Preset>));\r
22         String userPresetFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\user_presets.xml";\r
23         string hbPresetFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\presets.xml";\r
24 \r
25         /// <summary>\r
26         /// Add a new preset to the system\r
27         /// </summary>\r
28         /// <param name="presetName">String, The name of the new preset</param>\r
29         /// <param name="query">String, the CLI query for the new preset</param>\r
30         /// <param name="pictureSettings"> Bool, store crop/picture sizes in the presets</param>\r
31         public Boolean addPreset(string presetName, string query, Boolean pictureSettings)\r
32         {\r
33             if (checkIfPresetExists(presetName) == false)\r
34             {\r
35                 Preset newPreset = new Preset { Name = presetName, Query = query, PictureSettings = pictureSettings, Version = Properties.Settings.Default.hb_version};\r
36                 user_presets.Add(newPreset);\r
37                 updateUserPresetsFile();\r
38                 return true;\r
39             }\r
40             MessageBox.Show("Sorry, that preset name already exists. Please choose another!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
41             return false;\r
42         }\r
43 \r
44         /// <summary>\r
45         /// Remove a preset with a given name from either the built in or user preset list.\r
46         /// </summary>\r
47         /// <param name="name">String, the preset name</param>\r
48         public void remove(string name)\r
49         {\r
50             List<Preset> newPresets = new List<Preset>();\r
51             List<Preset> newUserPresets = new List<Preset>();\r
52 \r
53             // Built In Presets\r
54             foreach (Preset item in presets)\r
55             {\r
56                 if (item.Name != name)\r
57                 {\r
58                     newPresets.Add(item);\r
59                 }\r
60             }\r
61             presets = newPresets;\r
62 \r
63             // User Presets\r
64             foreach (Preset item in user_presets)\r
65             {\r
66                 if (item.Name != name)\r
67                 {\r
68                     newUserPresets.Add(item);\r
69                 }\r
70             }\r
71             user_presets = newUserPresets;\r
72 \r
73             // Rebuild the user_presets.xml file\r
74             updateUserPresetsFile();\r
75             updatePresetsFile();\r
76         }\r
77 \r
78         /// <summary>\r
79         /// Save changes to a given preset in the user preset list.\r
80         /// </summary>\r
81         /// <param name="presetName">String, The name of the new preset</param>\r
82         /// <param name="query">String, the CLI query for the new preset</param>\r
83         /// <param name="pictureSettings"> Bool, store crop/picture sizes in the preset</param>\r
84         public void updatePreset(string presetName, string query, Boolean pictureSettings)\r
85         {\r
86             // User Presets\r
87             foreach (Preset item in user_presets)\r
88             {\r
89                 if (item.Name == presetName)\r
90                 {\r
91                     item.Query = query;\r
92                     item.PictureSettings = pictureSettings;\r
93                     MessageBox.Show("Changes to \"" + presetName + "\" Saved", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);\r
94                     updateUserPresetsFile();\r
95                 }\r
96             }\r
97         }\r
98 \r
99         /// <summary>\r
100         /// Return the CLI query for a preset name given in name\r
101         /// </summary>\r
102         /// <param name="name">String, The preset's name</param>\r
103         /// <returns>String, the CLI query for the given preset name</returns>    not\r
104         public Preset getPreset(string name)\r
105         {\r
106             // Built In Presets\r
107             foreach (Preset item in presets)\r
108             {\r
109                 if (item.Name == name)\r
110                     return item;\r
111             }\r
112 \r
113             // User Presets\r
114             foreach (Preset item in user_presets)\r
115             {\r
116                 if (item.Name == name)\r
117                     return item;\r
118             }\r
119 \r
120             return null;\r
121         }\r
122 \r
123         /// <summary>\r
124         /// Reads the CLI's CLI output format and load's them into the preset List<Preset>\r
125         /// </summary>\r
126         public void updateBuiltInPresets()\r
127         {\r
128             // Create a new tempory file and execute the CLI to get the built in presets.\r
129             string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");\r
130             string presetsPath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");\r
131             string strCmdLine = String.Format(@"cmd /c """"{0}"" --preset-list >""{1}"" 2>&1""", handbrakeCLIPath, presetsPath);\r
132 \r
133             ProcessStartInfo hbGetPresets = new ProcessStartInfo("CMD.exe", strCmdLine) { WindowStyle = ProcessWindowStyle.Hidden };\r
134             Process hbproc = Process.Start(hbGetPresets);\r
135             if (hbproc != null)\r
136             {\r
137                 hbproc.WaitForExit();\r
138                 hbproc.Dispose();\r
139                 hbproc.Close();\r
140             }\r
141 \r
142             // Clear the current built in presets and now parse the tempory presets file.\r
143             presets.Clear();\r
144             string filePath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");\r
145 \r
146             if (File.Exists(filePath))\r
147             {\r
148                 StreamReader presetInput = new StreamReader(filePath);\r
149 \r
150                 int level = 1;\r
151                 string category = String.Empty;\r
152                 string level_1_category = String.Empty;\r
153 \r
154                 while (!presetInput.EndOfStream)\r
155                 {\r
156                     string line = presetInput.ReadLine();\r
157                     if (line.Contains("<") && !line.Contains("<<")) // Found the beginning of a preset block \r
158                     {\r
159                         level = 1;\r
160                         category = line.Replace("<", "").Trim();\r
161                         level_1_category = category;\r
162                     }\r
163 \r
164                     if (line.Contains("<<")) // found a sub preset block\r
165                     {\r
166                         level = 2;\r
167                         category = line.Replace("<<", "").Trim();\r
168                     }\r
169 \r
170                     if (line.Trim().Contains(">>")) // End of sub preset block\r
171                     {\r
172                         level = 1;\r
173                         category = level_1_category;\r
174                     }\r
175 \r
176                     if (line.Contains("+")) // A Preset\r
177                     {\r
178                         Regex r = new Regex("(:  )"); // Split on hyphens. \r
179                         string[] presetName = r.Split(line);\r
180 \r
181                         Preset newPreset = new Preset\r
182                                                {\r
183                                                    Level = level,\r
184                                                    Category = category,\r
185                                                    TopCategory = level_1_category,\r
186                                                    Name = presetName[0].Replace("+", "").Trim(),\r
187                                                    Query = presetName[2],\r
188                                                    Version = Properties.Settings.Default.hb_version,\r
189                                                    PictureSettings = true\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 }