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> _userPresets = new List<Preset>();\r
21         private static readonly XmlSerializer Ser = new XmlSerializer(typeof(List<Preset>));\r
22         readonly string _userPresetFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\user_presets.xml";\r
23         readonly 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 Add(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                 _userPresets.Add(newPreset);\r
37                 UpdatePresetFiles();\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 _userPresets)\r
64             {\r
65                 if (item.Name != name)\r
66                 {\r
67                     newUserPresets.Add(item);\r
68                 }\r
69             }\r
70             _userPresets = newUserPresets;\r
71 \r
72             // Rebuild the _userPresets.xml file\r
73             UpdatePresetFiles();\r
74             UpdatePresetFiles();\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             UpdatePresetFiles();\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 Update(string presetName, string query, Boolean pictureSettings)\r
93         {\r
94             // User Presets\r
95             foreach (Preset item in _userPresets)\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                     UpdatePresetFiles();\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 _userPresets)\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 \r
153             if (File.Exists(presetsPath))\r
154             {\r
155                 StreamReader presetInput = new StreamReader(presetsPath);\r
156 \r
157                 string category = String.Empty;\r
158 \r
159                 while (!presetInput.EndOfStream)\r
160                 {\r
161                     string line = presetInput.ReadLine();\r
162                     if (line.Contains("<") && !line.Contains("<<")) // Found the beginning of a preset block \r
163                         category = line.Replace("<", "").Trim();\r
164 \r
165                     if (line.Contains("+")) // A Preset\r
166                     {\r
167                         Regex r = new Regex("(:  )"); // Split on hyphens. \r
168                         string[] presetName = r.Split(line);\r
169 \r
170                         Preset newPreset = new Preset\r
171                                                {   Category = category,\r
172                                                    Name = presetName[0].Replace("+", "").Trim(),\r
173                                                    Query = presetName[2],\r
174                                                    Version = Properties.Settings.Default.hb_version,\r
175                                                    PictureSettings = true\r
176                                                };\r
177                         _presets.Add(newPreset);\r
178                     }\r
179                 }\r
180                 presetInput.Close();\r
181                 presetInput.Dispose();\r
182             }\r
183 \r
184             // Finally, Create a new or update the current _presets.xml file\r
185             UpdatePresetFiles();\r
186         }\r
187 \r
188         /// <summary>\r
189         /// Load in the preset data from _presets.xml and _userPresets.xml\r
190         /// Load it into the 2 arraylist's _presets and _userPresets\r
191         /// </summary>\r
192         private void LoadPresetData()\r
193         {\r
194             // First clear the _presets arraylists\r
195             _presets.Clear();\r
196             _userPresets.Clear();\r
197 \r
198             // Load in the users _presets from _userPresets.xml\r
199             if (File.Exists(_hbPresetFile))\r
200             {\r
201                 using (FileStream strm = new FileStream(_hbPresetFile, FileMode.Open, FileAccess.Read))\r
202                 {\r
203                     if (strm.Length != 0)\r
204                     {\r
205                         List<Preset> list = Ser.Deserialize(strm) as List<Preset>;\r
206 \r
207                         if (list != null)\r
208                             foreach (Preset preset in list)\r
209                                 _presets.Add(preset);\r
210                     }\r
211                 }\r
212             }\r
213 \r
214             // Load in the users _presets from _userPresets.xml\r
215             if (File.Exists(_userPresetFile))\r
216             {\r
217                 using (FileStream strm = new FileStream(_userPresetFile, FileMode.Open, FileAccess.Read))\r
218                 {\r
219                     if (strm.Length != 0)\r
220                     {\r
221                         List<Preset> list = Ser.Deserialize(strm) as List<Preset>;\r
222 \r
223                         if (list != null)\r
224                             foreach (Preset preset in list)\r
225                                 _userPresets.Add(preset);\r
226                     }\r
227                 }\r
228             }\r
229         }\r
230 \r
231         /// <summary>\r
232         /// Setup the frmMain preset panel\r
233         /// </summary>\r
234         /// <param name="presetPanel"></param>\r
235         public void GetPresetPanel(ref TreeView presetPanel)\r
236         {\r
237             this.LoadPresetData();\r
238             presetPanel.Nodes.Clear();\r
239             string category = string.Empty;\r
240             TreeNode rootNode = null;\r
241 \r
242             if (_presets.Count != 0) // Built In Presets\r
243             {\r
244                 \r
245                 foreach (Preset preset in _presets)\r
246                 {\r
247                     if (preset.Category != category)\r
248                     {\r
249                         rootNode = new TreeNode(preset.Category);\r
250                         presetPanel.Nodes.Add(rootNode);\r
251                         category = preset.Category;\r
252                     }\r
253 \r
254                     if (preset.Category == category && rootNode != null)\r
255                         rootNode.Nodes.Add(preset.Name);\r
256                 }\r
257             }\r
258 \r
259             rootNode = null; category = null;\r
260             foreach (Preset preset in _userPresets) // User Presets\r
261             {\r
262                 if (preset.Category != category && preset.Category != null)\r
263                 {\r
264                     rootNode = new TreeNode(preset.Category) { ForeColor = Color.Black };\r
265                     presetPanel.Nodes.Add(rootNode);\r
266                     category = preset.Category;\r
267                 }\r
268 \r
269                 if (preset.Category == category && rootNode != null)\r
270                     rootNode.Nodes.Add(new TreeNode(preset.Name) { ForeColor = Color.Black });\r
271                 else\r
272                     presetPanel.Nodes.Add(new TreeNode(preset.Name) { ForeColor = Color.Black });\r
273             }\r
274         }\r
275 \r
276         /// <summary>\r
277         /// Update the preset files\r
278         /// </summary>\r
279         private void UpdatePresetFiles()\r
280         {\r
281             try\r
282             {\r
283                 using (FileStream strm = new FileStream(_hbPresetFile, FileMode.Create, FileAccess.Write))\r
284                 {\r
285                     Ser.Serialize(strm, _presets);\r
286                     strm.Close();\r
287                     strm.Dispose();\r
288                 }\r
289 \r
290                 using (FileStream strm = new FileStream(_userPresetFile, FileMode.Create, FileAccess.Write))\r
291                 {\r
292                     Ser.Serialize(strm, _userPresets);\r
293                     strm.Close();\r
294                     strm.Dispose();\r
295                 }\r
296             }\r
297             catch (Exception exc)\r
298             {\r
299                 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
300             }\r
301         }\r
302 \r
303         /// <summary>\r
304         /// Check if the preset "name" exists in either _presets or _userPresets lists.\r
305         /// </summary>\r
306         /// <param name="name"></param>\r
307         /// <returns></returns>\r
308         private Boolean CheckIfPresetExists(string name)\r
309         {\r
310             if (name == string.Empty)\r
311                 return true;\r
312 \r
313             // Built In Presets\r
314             foreach (Preset item in _presets)\r
315             {\r
316                 if (item.Name == name)\r
317                     return true;\r
318             }\r
319 \r
320             // User Presets\r
321             foreach (Preset item in _userPresets)\r
322             {\r
323                 if (item.Name == name)\r
324                     return true;\r
325             }\r
326 \r
327             return false;\r
328         }\r
329 \r
330         /// <summary>\r
331         /// Check if the user preset "name" exists in _userPresets list.\r
332         /// </summary>\r
333         /// <param name="name"></param>\r
334         /// <returns></returns>\r
335         public Boolean CheckIfUserPresetExists(string name)\r
336         {\r
337             if (name == string.Empty)\r
338                 return false;\r
339 \r
340             // User Presets\r
341             foreach (Preset item in _userPresets)\r
342             {\r
343                 if (item.Name == name)\r
344                     return true;\r
345             }\r
346 \r
347             return false;\r
348         }\r
349 \r
350         /// <summary>\r
351         /// Check if the built in _presets stored are not out of date.\r
352         /// Update them if they are.\r
353         /// </summary>\r
354         /// <returns></returns>\r
355         public Boolean CheckIfPresetsAreOutOfDate()\r
356         {\r
357             LoadPresetData();\r
358             // Update built-in _presets if the built-in _presets belong to an older version.\r
359             if (_presets.Count != 0)\r
360                 if (_presets[0].Version != Properties.Settings.Default.hb_version)\r
361                 {\r
362                     UpdateBuiltInPresets();\r
363                     return true;\r
364                 }\r
365 \r
366             return false;\r
367         }\r
368     }\r
369 }