OSDN Git Service

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