OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / Functions / Presets.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 \r
9 namespace Handbrake.Functions\r
10 {\r
11     public class Presets\r
12     {\r
13         List<string> presets = new List<string>();\r
14         List<string> user_presets = new List<string>();\r
15 \r
16         /// <summary>\r
17         /// Add a new preset to the system\r
18         /// </summary>\r
19         /// <param name="presetName">String, The name of the new preset</param>\r
20         /// <param name="query">String, the CLI query for the new preset</param>\r
21         public Boolean addPreset(string presetName, string query)\r
22         {\r
23             if (checkIfPresetExists(presetName) == false)\r
24             {\r
25                 String preset = "+ " + presetName + ":  " + query;\r
26                 user_presets.Add(preset);\r
27                 addPresetToFile(preset);\r
28                 return true;\r
29             }\r
30             else\r
31             {\r
32                 MessageBox.Show("Sorry, that preset name already exists. Please choose another!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
33                 return false;\r
34             }\r
35         }\r
36 \r
37         /// <summary>\r
38         /// Remove a preset with a given name from either the built in or user preset list.\r
39         /// </summary>\r
40         /// <param name="name">String, the preset name</param>\r
41         public void remove(string name)\r
42         {\r
43             List<string> newPresets = new List<string>();\r
44             List<string> newUserPresets = new List<string>();\r
45 \r
46             // Built In Presets\r
47             foreach (string item in presets)\r
48             {\r
49                 string x = item.Replace("+ ", "");\r
50                 Regex r = new Regex("(:  )"); // Split on hyphens. \r
51                 string[] presetName = r.Split(x);\r
52                 if (presetName[0] != name)\r
53                     newPresets.Add(item);\r
54             }\r
55 \r
56             // User Presets\r
57             foreach (string item in user_presets)\r
58             {\r
59                 string x = item.Replace("+ ", "");\r
60                 Regex r = new Regex("(:  )"); // Split on hyphens. \r
61                 string[] presetName = r.Split(x);\r
62                 if (presetName[0] != name)\r
63                     newUserPresets.Add(item);\r
64             }\r
65 \r
66             // Now, Update the presets.dat and user_presets.dat file with the new items.\r
67             string userPresets = Application.StartupPath.ToString() + "\\user_presets.dat";\r
68             string presetsFile = Application.StartupPath.ToString() + "\\presets.dat";\r
69 \r
70             // Rebuild the presets.dat file\r
71             StreamWriter line = new StreamWriter(presetsFile);\r
72             foreach (string item in newPresets)\r
73             {\r
74                 line.WriteLine("+ " + item);\r
75             }\r
76             line.Close();\r
77             line.Dispose();\r
78 \r
79             // Rebuild the user_presets.dat file\r
80             line = new StreamWriter(userPresets);\r
81             foreach (string item in newUserPresets)\r
82             {\r
83                 line.WriteLine("+ " + item);\r
84             }\r
85             line.Close();\r
86             line.Dispose();\r
87         }\r
88 \r
89         /// <summary>\r
90         /// Get a List of all the built in preset names.\r
91         /// </summary>\r
92         /// <returns>List<String> of preset names</returns>\r
93         public List<string> getBuildInPresetNames()\r
94         {\r
95             List<string> names = new List<string>();\r
96 \r
97             // Built In Presets\r
98             foreach (string item in presets)\r
99             {\r
100                 string x = item.Replace("+ ", "");\r
101                 Regex r = new Regex("(:  )"); // Split on hyphens. \r
102                 string[] presetName = r.Split(x);\r
103                 names.Add(presetName[0]);\r
104 \r
105             }\r
106             return names;\r
107         }\r
108 \r
109         /// <summary>\r
110         /// Get a List of all the User preset names.\r
111         /// </summary>\r
112         /// <returns>List<String> of preset names</returns>\r
113         public List<string> getUserPresetNames()\r
114         {\r
115             List<string> names = new List<string>();\r
116 \r
117             // User Presets\r
118             foreach (string item in user_presets)\r
119             {\r
120                 string x = item.Replace("+ ", "");\r
121                 Regex r = new Regex("(:  )");\r
122                 string[] presetName = r.Split(x);\r
123                 names.Add(presetName[0]);\r
124 \r
125             }\r
126 \r
127             return names;\r
128         }\r
129 \r
130         /// <summary>\r
131         /// Return the CLI query for a preset name given in name\r
132         /// </summary>\r
133         /// <param name="name">String, The preset's name</param>\r
134         /// <returns>String, the CLI query for the given preset name</returns>\r
135         public string getCliForPreset(string name)\r
136         {\r
137             // Built In Presets\r
138             foreach (string item in presets)\r
139             {\r
140                 string x = item.Replace("+ ", "");\r
141                 Regex r = new Regex("(:  )"); // Split on hyphens. \r
142                 string[] presetName = r.Split(x);\r
143                 if (presetName[0] == name)\r
144                     return presetName[2];\r
145             }\r
146 \r
147             // User Presets\r
148             foreach (string item in user_presets)\r
149             {\r
150                 string x = item.Replace("+ ", "");\r
151                 Regex r = new Regex("(:  )"); // Split on hyphens. \r
152                 string[] presetName = r.Split(x);\r
153                 if (presetName[0] == name)\r
154                     return presetName[2];\r
155             }\r
156 \r
157             return null;\r
158         }\r
159 \r
160         /// <summary>\r
161         /// Update the presets.dat file with the latest version of HandBrake's presets from the CLI\r
162         /// </summary>\r
163         public void grabCLIPresets()\r
164         {\r
165             string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");\r
166             string presetsPath = Path.Combine(Application.StartupPath, "presets.dat");\r
167 \r
168             string strCmdLine = String.Format(@"cmd /c """"{0}"" --preset-list >""{1}"" 2>&1""", handbrakeCLIPath, presetsPath);\r
169 \r
170             ProcessStartInfo hbGetPresets = new ProcessStartInfo("CMD.exe", strCmdLine);\r
171             hbGetPresets.WindowStyle = ProcessWindowStyle.Hidden;\r
172 \r
173             Process hbproc = Process.Start(hbGetPresets);\r
174             hbproc.WaitForExit();\r
175             hbproc.Dispose();\r
176             hbproc.Close();\r
177         }\r
178 \r
179         /// <summary>\r
180         /// Load in the preset data from presets.dat and user_presets.dat\r
181         /// Load it into the 2 arraylist's presets and user_presets\r
182         /// </summary>\r
183         public void loadPresetFiles()\r
184         {\r
185             // First clear the presets arraylists\r
186             presets.Clear();\r
187             user_presets.Clear();\r
188 \r
189             // Load in the built in presets from presets.dat\r
190             // We'll store them in the array in the format:   presetName:  ClI Query\r
191             string filePath = Application.StartupPath.ToString() + "\\presets.dat";\r
192             if (File.Exists(filePath))\r
193             {\r
194                 StreamReader presetInput = new StreamReader(filePath);\r
195                 while (!presetInput.EndOfStream)\r
196                 {\r
197                     if ((char)presetInput.Peek() == '+')\r
198                         presets.Add(presetInput.ReadLine().Replace("+ ", ""));\r
199                     else\r
200                         presetInput.ReadLine();\r
201                 }\r
202                 presetInput.Close();\r
203                 presetInput.Dispose();\r
204             }\r
205 \r
206             // Load in the users presets from user_presets.dat\r
207             filePath = Application.StartupPath.ToString() + "\\user_presets.dat";\r
208             if (File.Exists(filePath))\r
209             {\r
210                 StreamReader presetInput = new StreamReader(filePath);\r
211                 while (!presetInput.EndOfStream)\r
212                 {\r
213                     if ((char)presetInput.Peek() == '+')\r
214                         user_presets.Add(presetInput.ReadLine().Replace("+ ", ""));\r
215                     else\r
216                         presetInput.ReadLine();\r
217                 }\r
218                 presetInput.Close();\r
219                 presetInput.Dispose();\r
220             }\r
221         }\r
222 \r
223         // Add a single preset to user_presets.dat\r
224         private void addPresetToFile(string preset)\r
225         {\r
226             string userPresets = Application.StartupPath.ToString() + "\\user_presets.dat";\r
227             try\r
228             {\r
229                 // Create a StreamWriter and open the file\r
230                 StreamWriter line = File.AppendText(userPresets);\r
231 \r
232                 // Generate and write the preset string to the file\r
233                 line.WriteLine(preset);\r
234 \r
235                 // close the stream\r
236                 line.Close();\r
237                 line.Dispose();\r
238             }\r
239             catch (Exception exc)\r
240             {\r
241                 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
242             }\r
243         }\r
244 \r
245         // Check if a preset already exists in either the built in or user presets\r
246         private Boolean checkIfPresetExists(string name)\r
247         {\r
248             if (name == string.Empty)\r
249                 return true;\r
250 \r
251             // Built In Presets\r
252             foreach (string item in presets)\r
253             {\r
254                 string x = item.Replace("+ ", "");\r
255                 Regex r = new Regex("(:  )"); // Split on hyphens. \r
256                 string[] presetName = r.Split(x);\r
257                 if (presetName[0] == name)\r
258                     return true;\r
259             }\r
260 \r
261             // User Presets\r
262             foreach (string item in user_presets)\r
263             {\r
264                 string x = item.Replace("+ ", "");\r
265                 Regex r = new Regex("(:  )"); // Split on hyphens. \r
266                 string[] presetName = r.Split(x);\r
267                 if (presetName[0] == name)\r
268                     return true;\r
269             }\r
270 \r
271             return false;\r
272         }\r
273     }\r
274 \r
275 }