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