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.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         /// <param name="pictureSettings"> Bool, store crop/picture sizes in the presets</param>\r
24         public Boolean addPreset(string presetName, string query, Boolean pictureSettings)\r
25         {\r
26             if (checkIfPresetExists(presetName) == false)\r
27             {\r
28                 Preset newPreset = new Preset();\r
29                 newPreset.Name = presetName;\r
30                 newPreset.Query = query;\r
31                 newPreset.PictureSettings = pictureSettings;\r
32                 user_presets.Add(newPreset);\r
33                 updateUserPresetsFile();\r
34                 return true;\r
35             }\r
36             else\r
37             {\r
38                 MessageBox.Show("Sorry, that preset name already exists. Please choose another!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
39                 return false;\r
40             }\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 user_presets)\r
64             {\r
65                 if (item.Name != name)\r
66                 {\r
67                     newUserPresets.Add(item);\r
68                 }\r
69             }\r
70             user_presets = newUserPresets;\r
71 \r
72             // Rebuild the user_presets.xml file\r
73             updateUserPresetsFile();\r
74             updatePresetsFile();\r
75         }\r
76 \r
77         /// <summary>\r
78         /// Save changes to a given preset in the user preset list.\r
79         /// </summary>\r
80         /// <param name="presetName">String, The name of the new preset</param>\r
81         /// <param name="query">String, the CLI query for the new preset</param>\r
82         /// <param name="pictureSettings"> Bool, store crop/picture sizes in the preset</param>\r
83         public void updatePreset(string presetName, string query, Boolean pictureSettings)\r
84         {\r
85             // User Presets\r
86             foreach (Preset item in user_presets)\r
87             {\r
88                 if (item.Name == presetName)\r
89                 {\r
90                     item.Query = query;\r
91                     item.PictureSettings = pictureSettings;\r
92                     MessageBox.Show("Changes to \"" + presetName + "\" Saved", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);\r
93                 }\r
94             }\r
95         }\r
96 \r
97         /// <summary>\r
98         /// Get a List of all the built in preset names.\r
99         /// </summary>\r
100         /// <returns>List<String> of preset names</returns>\r
101         public List<Preset> getBuildInPresets()\r
102         {\r
103             return presets;\r
104         }\r
105 \r
106         /// <summary>\r
107         /// Get a List of all the User preset names.\r
108         /// </summary>\r
109         /// <returns>List<String> of preset names</returns>\r
110         public List<string> getUserPresetNames()\r
111         {\r
112             List<string> names = new List<string>();\r
113 \r
114             // User Presets\r
115             foreach (Preset item in user_presets)\r
116             {\r
117                 names.Add(item.Name);\r
118             }\r
119 \r
120             return names;\r
121         }\r
122 \r
123         /// <summary>\r
124         /// Return the CLI query for a preset name given in name\r
125         /// </summary>\r
126         /// <param name="name">String, The preset's name</param>\r
127         /// <returns>String, the CLI query for the given preset name</returns>\r
128         public Preset getPreset(string name)\r
129         {\r
130             // Built In Presets\r
131             foreach (Preset item in presets)\r
132             {\r
133                 if (item.Name == name)\r
134                     return item;\r
135             }\r
136 \r
137             // User Presets\r
138             foreach (Preset item in user_presets)\r
139             {\r
140                 if (item.Name == name)\r
141                     return item;\r
142             }\r
143 \r
144             return null;\r
145         }\r
146 \r
147         /// <summary>\r
148         /// Reads the CLI's CLI output format and load's them into the preset List<Preset>\r
149         /// </summary>\r
150         public void updateBuiltInPresets()\r
151         {\r
152             // Create a new tempory file and execute the CLI to get the built in presets.\r
153             string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");\r
154             string presetsPath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");\r
155 \r
156             string strCmdLine = String.Format(@"cmd /c """"{0}"" --preset-list >""{1}"" 2>&1""", handbrakeCLIPath, presetsPath);\r
157 \r
158             ProcessStartInfo hbGetPresets = new ProcessStartInfo("CMD.exe", strCmdLine);\r
159             hbGetPresets.WindowStyle = ProcessWindowStyle.Hidden;\r
160 \r
161             Process hbproc = Process.Start(hbGetPresets);\r
162             hbproc.WaitForExit();\r
163             hbproc.Dispose();\r
164             hbproc.Close();\r
165 \r
166             // Clear the current built in presets and now parse the tempory presets file.\r
167             presets.Clear();\r
168             string filePath = Path.Combine(Path.GetTempPath(), "temp_presets.dat");\r
169             if (File.Exists(filePath))\r
170             {\r
171                 StreamReader presetInput = new StreamReader(filePath);\r
172                 int level = 1;\r
173                 string category = String.Empty;\r
174                 string level_1_category = String.Empty;\r
175 \r
176                 while (!presetInput.EndOfStream)\r
177                 {\r
178                     string line = presetInput.ReadLine();\r
179                     if (line.Contains("<") && !line.Contains("<<"))\r
180                     {\r
181                         level = 1;\r
182                         category = line.Replace("<", "").Trim();\r
183                         level_1_category = category;\r
184                     }\r
185 \r
186                     if (line.Contains("<<"))\r
187                     {\r
188                         level = 2;\r
189                         category = line.Replace("<<", "").Trim();\r
190                     }\r
191 \r
192                     if (line.Trim().Contains(">>"))\r
193                     {\r
194                         level = 1;\r
195                         category = level_1_category;\r
196                     }\r
197 \r
198                     if (line.Contains("+"))\r
199                     {\r
200                         Regex r = new Regex("(:  )"); // Split on hyphens. \r
201                         string[] presetName = r.Split(line);\r
202 \r
203                         Preset newPreset = new Preset();\r
204                         newPreset.Level = level;\r
205                         newPreset.Category = category;\r
206                         newPreset.Name = presetName[0].Replace("+", "").Trim();\r
207                         newPreset.Query = presetName[2];\r
208                         presets.Add(newPreset);\r
209                     }\r
210                 }\r
211                 presetInput.Close();\r
212                 presetInput.Dispose();\r
213             }\r
214 \r
215             // Finally, Create a new or update the current presets.xml file\r
216             updatePresetsFile();\r
217         }\r
218 \r
219         /// <summary>\r
220         /// Load in the preset data from presets.xml and user_presets.xml\r
221         /// Load it into the 2 arraylist's presets and user_presets\r
222         /// </summary>\r
223         public void loadPresetData()\r
224         {\r
225             // First clear the presets arraylists\r
226             presets.Clear();\r
227             user_presets.Clear();\r
228 \r
229             string filePath = string.Empty;\r
230 \r
231             // Load in the users presets from user_presets.xml\r
232             filePath = Application.StartupPath.ToString() + "\\presets.xml";\r
233             if (File.Exists(filePath))\r
234             {\r
235                 using (FileStream strm = new FileStream(filePath, FileMode.Open, FileAccess.Read))\r
236                 {\r
237                     if (strm.Length != 0)\r
238                     {\r
239                         List<Preset> list = ser.Deserialize(strm) as List<Preset>;\r
240 \r
241                         foreach (Preset preset in list)\r
242                             presets.Add(preset);\r
243                     }\r
244                 }\r
245             }\r
246 \r
247             // Load in the users presets from user_presets.xml\r
248             filePath = Application.StartupPath.ToString() + "\\user_presets.xml";\r
249             if (File.Exists(filePath))\r
250             {\r
251                 using (FileStream strm = new FileStream(filePath, FileMode.Open, FileAccess.Read))\r
252                 {\r
253                     if (strm.Length != 0)\r
254                     {\r
255                         List<Preset> list = ser.Deserialize(strm) as List<Preset>;\r
256 \r
257                         foreach (Preset preset in list)\r
258                             user_presets.Add(preset);\r
259                     }\r
260                 }\r
261             }\r
262         }\r
263 \r
264         /// <summary>\r
265         /// Updates the presets.xml file which contains the built in presets\r
266         /// It takes the List of Presets and converts them into XML which is stored in presets.xml\r
267         /// </summary>\r
268         private void updatePresetsFile()\r
269         {\r
270             string userPresets = Application.StartupPath.ToString() + "\\presets.xml";\r
271             try\r
272             {\r
273                 using (FileStream strm = new FileStream(userPresets, FileMode.Create, FileAccess.Write))\r
274                 {\r
275                     ser.Serialize(strm, presets);\r
276                     strm.Close();\r
277                     strm.Dispose();\r
278                 }\r
279             }\r
280             catch (Exception exc)\r
281             {\r
282                 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
283             }\r
284         }\r
285 \r
286         /// <summary>\r
287         /// Updates the user_presets.xml file which contains the built in presets\r
288         /// It takes the List of Presets and converts them into XML which is stored in user_presets.xml\r
289         /// </summary>\r
290         private void updateUserPresetsFile()\r
291         {\r
292             string userPresets = Application.StartupPath.ToString() + "\\user_presets.xml";\r
293             try\r
294             {\r
295                 using (FileStream strm = new FileStream(userPresets, FileMode.Create, FileAccess.Write))\r
296                 {\r
297                     ser.Serialize(strm, user_presets);\r
298                     strm.Close();\r
299                     strm.Dispose();\r
300                 }\r
301             }\r
302             catch (Exception exc)\r
303             {\r
304                 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
305             }\r
306         }\r
307 \r
308         /// <summary>\r
309         /// Check if the preset "name" exists in either presets or user_presets lists.\r
310         /// </summary>\r
311         /// <param name="name"></param>\r
312         /// <returns></returns>\r
313         private Boolean checkIfPresetExists(string name)\r
314         {\r
315             if (name == string.Empty)\r
316                 return true;\r
317 \r
318             // Built In Presets\r
319             foreach (Preset item in presets)\r
320             {\r
321                 if (item.Name == name)\r
322                     return true;\r
323             }\r
324 \r
325             // User Presets\r
326             foreach (Preset item in user_presets)\r
327             {\r
328                 if (item.Name == name)\r
329                     return true;\r
330             }\r
331 \r
332             return false;\r
333         }\r
334 \r
335         /// <summary>\r
336         /// Check if the user preset "name" exists in user_presets list.\r
337         /// </summary>\r
338         /// <param name="name"></param>\r
339         /// <returns></returns>\r
340         public Boolean checkIfUserPresetExists(string name)\r
341         {\r
342             if (name == string.Empty)\r
343                 return false;\r
344 \r
345             // User Presets\r
346             foreach (Preset item in user_presets)\r
347             {\r
348                 if (item.Name == name)\r
349                     return true;\r
350             }\r
351 \r
352             return false;\r
353         }\r
354     }\r
355 }