OSDN Git Service

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