OSDN Git Service

fbd36fbdb12836062095add3be34ef0a03341e10
[handbrake-jp/handbrake-jp-git.git] / win / C# / Presets / PresetsHandler.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using System.Text;\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 \r
10 namespace Handbrake.Presets\r
11 {\r
12     public class PresetsHandler\r
13     {\r
14         List<Preset> presets = new List<Preset>();  // Category+Level+Preset Name: Query\r
15         List<Preset> user_presets = new List<Preset>(); // Preset Name: Query\r
16         private static XmlSerializer ser = new XmlSerializer(typeof(List<Preset>));\r
17 \r
18         /// <summary>\r
19         /// Add a new preset to the system\r
20         /// </summary>\r
21         /// <param name="presetName">String, The name of the new preset</param>\r
22         /// <param name="query">String, the CLI query for the new preset</param>\r
23         public Boolean addPreset(string presetName, string query, Boolean pictureSettings)\r
24         {\r
25             if (checkIfPresetExists(presetName) == false)\r
26             {\r
27                 Preset newPreset = new Preset();\r
28                 newPreset.Name = presetName;\r
29                 newPreset.Query = query;\r
30                 newPreset.PictureSettings = pictureSettings;\r
31                 user_presets.Add(newPreset);\r
32                 updateUserPresetsFile();\r
33                 return true;\r
34             }\r
35             else\r
36             {\r
37                 MessageBox.Show("Sorry, that preset name already exists. Please choose another!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
38                 return false;\r
39             }\r
40         }\r
41 \r
42         /// <summary>\r
43         /// Remove a preset with a given name from either the built in or user preset list.\r
44         /// </summary>\r
45         /// <param name="name">String, the preset name</param>\r
46         public void remove(string name)\r
47         {\r
48             List<Preset> newPresets = new List<Preset>();\r
49             List<Preset> newUserPresets = new List<Preset>();\r
50 \r
51             // Built In Presets\r
52             foreach (Preset item in presets)\r
53             {\r
54                 if (item.Name != name)\r
55                 {\r
56                     newPresets.Add(item);\r
57                 }\r
58             }\r
59             presets = newPresets;\r
60 \r
61             // User Presets\r
62             foreach (Preset item in user_presets)\r
63             {\r
64                 if (item.Name != name)\r
65                 {\r
66                     newUserPresets.Add(item);\r
67                 }\r
68             }\r
69             user_presets = newUserPresets;\r
70 \r
71             // Now, Update the presets.xml and user_presets.xml file with the new items.\r
72             string userPresets = Application.StartupPath.ToString() + "\\user_presets.xml";\r
73             string presetsFile = Application.StartupPath.ToString() + "\\presets.xml";\r
74 \r
75             // Rebuild the user_presets.xml file\r
76             updateUserPresetsFile();\r
77             updatePresetsFile();\r
78         }\r
79 \r
80         /// <summary>\r
81         /// Get a List of all the built in preset names.\r
82         /// </summary>\r
83         /// <returns>List<String> of preset names</returns>\r
84         public List<Preset> getBuildInPresets()\r
85         {\r
86             return presets;\r
87         }\r
88 \r
89         /// <summary>\r
90         /// Get a List of all the User preset names.\r
91         /// </summary>\r
92         /// <returns>List<String> of preset names</returns>\r
93         public List<string> getUserPresetNames()\r
94         {\r
95             List<string> names = new List<string>();\r
96 \r
97             // User Presets\r
98             foreach (Preset item in user_presets)\r
99             {\r
100                 names.Add(item.Name);\r
101             }\r
102 \r
103             return names;\r
104         }\r
105 \r
106         /// <summary>\r
107         /// Return the CLI query for a preset name given in name\r
108         /// </summary>\r
109         /// <param name="name">String, The preset's name</param>\r
110         /// <returns>String, the CLI query for the given preset name</returns>\r
111         public Preset getPreset(string name)\r
112         {\r
113             // Built In Presets\r
114             foreach (Preset item in presets)\r
115             {\r
116                 if (item.Name == name)\r
117                     return item;\r
118             }\r
119 \r
120             // User Presets\r
121             foreach (Preset item in user_presets)\r
122             {\r
123                 if (item.Name == name)\r
124                     return item;\r
125             }\r
126 \r
127             return null;\r
128         }\r
129 \r
130         /// <summary>\r
131         /// Reads the CLI's CLI output format and load's them into the preset List<Preset>\r
132         /// </summary>\r
133         public void updateBuiltInPresets()\r
134         {\r
135             // Create a new tempory file and execute the CLI to get the built in presets.\r
136             string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");\r
137             string presetsPath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");\r
138 \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);\r
142             hbGetPresets.WindowStyle = ProcessWindowStyle.Hidden;\r
143 \r
144             Process hbproc = Process.Start(hbGetPresets);\r
145             hbproc.WaitForExit();\r
146             hbproc.Dispose();\r
147             hbproc.Close();\r
148 \r
149             // Clear the current built in presets and now parse the tempory presets file.\r
150             presets.Clear();\r
151             string filePath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");\r
152             if (File.Exists(filePath))\r
153             {\r
154                 StreamReader presetInput = new StreamReader(filePath);\r
155                 int level = 1;\r
156                 string category = String.Empty;\r
157                 string level_1_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("<<"))\r
163                     {\r
164                         level = 1;\r
165                         category = line.Replace("<", "").Trim();\r
166                         level_1_category = category;\r
167                     }\r
168 \r
169                     if (line.Contains("<<"))\r
170                     {\r
171                         level = 2;\r
172                         category = line.Replace("<<", "").Trim();\r
173                     }\r
174 \r
175                     if (line.Trim().Contains(">>"))\r
176                     {\r
177                         level = 1;\r
178                         category = level_1_category;\r
179                     }\r
180 \r
181                     if (line.Contains("+"))\r
182                     {\r
183                         Regex r = new Regex("(:  )"); // Split on hyphens. \r
184                         string[] presetName = r.Split(line);\r
185 \r
186                         Preset newPreset = new Preset();\r
187                         newPreset.Level = level;\r
188                         newPreset.Category = category;\r
189                         newPreset.Name = presetName[0].Replace("+", "").Trim();\r
190                         newPreset.Query = presetName[2];\r
191                         presets.Add(newPreset);\r
192                     }\r
193                 }\r
194                 presetInput.Close();\r
195                 presetInput.Dispose();\r
196             }\r
197 \r
198             // Finally, Create a new or update the current presets.xml file\r
199             updatePresetsFile();\r
200         }\r
201 \r
202         /// <summary>\r
203         /// Load in the preset data from presets.xml and user_presets.xml\r
204         /// Load it into the 2 arraylist's presets and user_presets\r
205         /// </summary>\r
206         public void loadPresetData()\r
207         {\r
208             // First clear the presets arraylists\r
209             presets.Clear();\r
210             user_presets.Clear();\r
211 \r
212             string filePath = string.Empty;\r
213 \r
214             // Load in the users presets from user_presets.xml\r
215             filePath = Application.StartupPath.ToString() + "\\presets.xml";\r
216             if (File.Exists(filePath))\r
217             {\r
218                 using (FileStream strm = new FileStream(filePath, FileMode.Open, FileAccess.Read))\r
219                 {\r
220                     if (strm.Length != 0)\r
221                     {\r
222                         List<Preset> list = ser.Deserialize(strm) as List<Preset>;\r
223 \r
224                         foreach (Preset preset in list)\r
225                             presets.Add(preset);\r
226                     }\r
227                 }\r
228             }\r
229 \r
230             // Load in the users presets from user_presets.xml\r
231             filePath = Application.StartupPath.ToString() + "\\user_presets.xml";\r
232             if (File.Exists(filePath))\r
233             {\r
234                 using (FileStream strm = new FileStream(filePath, FileMode.Open, FileAccess.Read))\r
235                 {\r
236                     if (strm.Length != 0)\r
237                     {\r
238                         List<Preset> list = ser.Deserialize(strm) as List<Preset>;\r
239 \r
240                         foreach (Preset preset in list)\r
241                             user_presets.Add(preset);\r
242                     }\r
243                 }\r
244             }\r
245         }\r
246 \r
247         /// <summary>\r
248         /// Updates the presets.xml file which contains the built in presets\r
249         /// It takes the List of Presets and converts them into XML which is stored in presets.xml\r
250         /// </summary>\r
251         private void updatePresetsFile()\r
252         {\r
253             string userPresets = Application.StartupPath.ToString() + "\\presets.xml";\r
254             try\r
255             {\r
256                 using (FileStream strm = new FileStream(userPresets, FileMode.Create, FileAccess.Write))\r
257                 {\r
258                     ser.Serialize(strm, presets);\r
259                     strm.Close();\r
260                     strm.Dispose();\r
261                 }\r
262             }\r
263             catch (Exception exc)\r
264             {\r
265                 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.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\r
266             }\r
267         }\r
268 \r
269         /// <summary>\r
270         /// Updates the user_presets.xml file which contains the built in presets\r
271         /// It takes the List of Presets and converts them into XML which is stored in user_presets.xml\r
272         /// </summary>\r
273         private void updateUserPresetsFile()\r
274         {\r
275             string userPresets = Application.StartupPath.ToString() + "\\user_presets.xml";\r
276             try\r
277             {\r
278                 using (FileStream strm = new FileStream(userPresets, FileMode.Create, FileAccess.Write))\r
279                 {\r
280                     ser.Serialize(strm, user_presets);\r
281                     strm.Close();\r
282                     strm.Dispose();\r
283                 }\r
284             }\r
285             catch (Exception exc)\r
286             {\r
287                 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.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\r
288             }\r
289         }\r
290 \r
291         /// <summary>\r
292         /// Check if the preset "name" exists in either presets or user_presets lists.\r
293         /// </summary>\r
294         /// <param name="name"></param>\r
295         /// <returns></returns>\r
296         private Boolean checkIfPresetExists(string name)\r
297         {\r
298             if (name == string.Empty)\r
299                 return true;\r
300 \r
301             // Built In Presets\r
302             foreach (Preset item in presets)\r
303             {\r
304                 if (item.Name == name)\r
305                     return true;\r
306             }\r
307 \r
308             // User Presets\r
309             foreach (Preset item in user_presets)\r
310             {\r
311                 if (item.Name == name)\r
312                     return true;\r
313             }\r
314 \r
315             return false;\r
316         }\r
317     }\r
318 }