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