OSDN Git Service

merge 0.9.4 to jp
[handbrake-jp/handbrake-jp.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 /*\r
41             else\r
42             {\r
43                 MessageBox.Show("そのプリセット名はすでに存在しています。 別の名前を選択してください", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
44                 return false;\r
45             }\r
46 */\r
47             return false;\r
48         }\r
49 \r
50         /// <summary>\r
51         /// Remove a preset with a given name from either the built in or user preset list.\r
52         /// </summary>\r
53         /// <param name="name">String, the preset name</param>\r
54         public void Remove(string name)\r
55         {\r
56             List<Preset> newPresets = new List<Preset>();\r
57             List<Preset> newUserPresets = new List<Preset>();\r
58 \r
59             // Built In Presets\r
60             foreach (Preset item in _presets)\r
61             {\r
62                 if (item.Name != name)\r
63                 {\r
64                     newPresets.Add(item);\r
65                 }\r
66             }\r
67             _presets = newPresets;\r
68 \r
69             // User Presets\r
70             foreach (Preset item in _userPresets)\r
71             {\r
72                 if (item.Name != name)\r
73                 {\r
74                     newUserPresets.Add(item);\r
75                 }\r
76             }\r
77             _userPresets = newUserPresets;\r
78 \r
79             // Rebuild the _userPresets.xml file\r
80             UpdatePresetFiles();\r
81             UpdatePresetFiles();\r
82         }\r
83 \r
84         /// <summary>\r
85         /// Remove all built in _presets;\r
86         /// </summary>\r
87         public void RemoveBuiltInPresets()\r
88         {\r
89             _presets.Clear();\r
90             UpdatePresetFiles();\r
91         }\r
92 \r
93         /// <summary>\r
94         /// Save changes to a given preset in the user preset list.\r
95         /// </summary>\r
96         /// <param name="presetName">String, The name of the new preset</param>\r
97         /// <param name="query">String, the CLI query for the new preset</param>\r
98         /// <param name="pictureSettings"> Bool, store crop/picture sizes in the preset</param>\r
99         public void Update(string presetName, string query, Boolean pictureSettings)\r
100         {\r
101             // User Presets\r
102             foreach (Preset item in _userPresets)\r
103             {\r
104                 if (item.Name == presetName)\r
105                 {\r
106                     item.Query = query;\r
107                     item.PictureSettings = pictureSettings;\r
108                     MessageBox.Show("Changes to \"" + presetName + "\" Saved", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);\r
109                     UpdatePresetFiles();\r
110                 }\r
111             }\r
112         }\r
113 \r
114         /// <summary>\r
115         /// Return the CLI query for a preset name given in name\r
116         /// </summary>\r
117         /// <param name="name">String, The preset's name</param>\r
118         /// <returns>String, the CLI query for the given preset name</returns>    not\r
119         public Preset GetPreset(string name)\r
120         {\r
121             // Built In Presets\r
122             foreach (Preset item in _presets)\r
123             {\r
124                 if (item.Name == name)\r
125                     return item;\r
126             }\r
127 \r
128             // User Presets\r
129             foreach (Preset item in _userPresets)\r
130             {\r
131                 if (item.Name == name)\r
132                     return item;\r
133             }\r
134 \r
135             return null;\r
136         }\r
137 \r
138         /// <summary>\r
139         /// Reads the CLI's CLI output format and load's them into the preset List<Preset>\r
140         /// </summary>\r
141         public void UpdateBuiltInPresets()\r
142         {\r
143             // Create a new tempory file and execute the CLI to get the built in _presets.\r
144             string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");\r
145             string presetsPath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");\r
146             string strCmdLine = String.Format(@"cmd /c """"{0}"" --preset-list >""{1}"" 2>&1""", handbrakeCLIPath, presetsPath);\r
147 \r
148             ProcessStartInfo hbGetPresets = new ProcessStartInfo("CMD.exe", strCmdLine) { WindowStyle = ProcessWindowStyle.Hidden };\r
149             Process hbproc = Process.Start(hbGetPresets);\r
150             if (hbproc != null)\r
151             {\r
152                 hbproc.WaitForExit();\r
153                 hbproc.Dispose();\r
154                 hbproc.Close();\r
155             }\r
156 \r
157             // Clear the current built in _presets and now parse the tempory _presets file.\r
158             _presets.Clear();\r
159 \r
160             if (File.Exists(presetsPath))\r
161             {\r
162                 StreamReader presetInput = new StreamReader(presetsPath);\r
163 \r
164                 string category = String.Empty;\r
165 \r
166                 while (!presetInput.EndOfStream)\r
167                 {\r
168                     string line = presetInput.ReadLine();\r
169                     if (line.Contains("<") && !line.Contains("<<")) // Found the beginning of a preset block \r
170                         category = line.Replace("<", "").Trim();\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                                                {   Category = category,\r
179                                                    Name = presetName[0].Replace("+", "").Trim(),\r
180                                                    Query = presetName[2],\r
181                                                    Version = Properties.Settings.Default.hb_version,\r
182                                                    PictureSettings = true\r
183                                                };\r
184                         _presets.Add(newPreset);\r
185                     }\r
186                 }\r
187                 presetInput.Close();\r
188                 presetInput.Dispose();\r
189             }\r
190 \r
191             // Finally, Create a new or update the current _presets.xml file\r
192             UpdatePresetFiles();\r
193         }\r
194 \r
195         /// <summary>\r
196         /// Load in the preset data from _presets.xml and _userPresets.xml\r
197         /// Load it into the 2 arraylist's _presets and _userPresets\r
198         /// </summary>\r
199         private void LoadPresetData()\r
200         {\r
201             // First clear the _presets arraylists\r
202             _presets.Clear();\r
203             _userPresets.Clear();\r
204 \r
205             // Load in the users _presets from _userPresets.xml\r
206             if (File.Exists(_hbPresetFile))\r
207             {\r
208                 using (FileStream strm = new FileStream(_hbPresetFile, 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 _userPresets.xml\r
222             if (File.Exists(_userPresetFile))\r
223             {\r
224                 using (FileStream strm = new FileStream(_userPresetFile, 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                                 _userPresets.Add(preset);\r
233                     }\r
234                 }\r
235             }\r
236         }\r
237 \r
238         /// <summary>\r
239         /// Setup the frmMain preset panel\r
240         /// </summary>\r
241         /// <param name="presetPanel"></param>\r
242         public void GetPresetPanel(ref TreeView presetPanel)\r
243         {\r
244             this.LoadPresetData();\r
245             presetPanel.Nodes.Clear();\r
246 \r
247             if (_presets.Count != 0) // Built In Presets\r
248             {\r
249                 string category = string.Empty;\r
250                 TreeNode rootNode = null;\r
251 \r
252                 foreach (Preset preset in _presets)\r
253                 {\r
254                     if (preset.Category != category)\r
255                     {\r
256                         rootNode = new TreeNode(preset.Category);\r
257                         presetPanel.Nodes.Add(rootNode);\r
258                         category = preset.Category;\r
259                     }\r
260 \r
261                     if (preset.Category == category && rootNode != null)\r
262                         rootNode.Nodes.Add(preset.Name);\r
263                 }\r
264             }\r
265 \r
266             foreach (Preset preset in _userPresets) // User Presets\r
267             {\r
268 /*\r
269                 MessageBox.Show("ファイルの書き込みに失敗しました。\n Error Information: \n\n" + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\r
270 */\r
271                 TreeNode presetTreeview = new TreeNode(preset.Name) { ForeColor = Color.Black };\r
272                 presetPanel.Nodes.Add(presetTreeview);\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("ファイルの書き込みに失敗しました。\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 }