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 \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 an Arraylist of all the preset names.\r
91         /// Includes both built in and user presets.\r
92         /// </summary>\r
93         /// <returns>Arraylist of preset names</returns>\r
94         public List<string> getPresetNames()\r
95         {\r
96             List<string> names = new List<string>();\r
97 \r
98             // Built In Presets\r
99             foreach (string item in presets)\r
100             {\r
101                 string x = item.Replace("+ ", "");\r
102                 Regex r = new Regex("(:  )"); // Split on hyphens. \r
103                 string[] presetName = r.Split(x);\r
104                 names.Add(presetName[0]);\r
105 \r
106             }\r
107 \r
108             // User Presets\r
109             foreach (string item in user_presets)\r
110             {\r
111                 string x = item.Replace("+ ", "");\r
112                 Regex r = new Regex("(:  )");\r
113                 string[] presetName = r.Split(x);\r
114                 names.Add(presetName[0]);\r
115 \r
116             }\r
117 \r
118             return names;\r
119         }\r
120 \r
121         /// <summary>\r
122         /// Return the CLI query for a preset name given in name\r
123         /// </summary>\r
124         /// <param name="name">String, The preset's name</param>\r
125         /// <returns>String, the CLI query for the given preset name</returns>\r
126         public string getCliForPreset(string name)\r
127         {\r
128             // Built In Presets\r
129             foreach (string item in presets)\r
130             {\r
131                 string x = item.Replace("+ ", "");\r
132                 Regex r = new Regex("(:  )"); // Split on hyphens. \r
133                 string[] presetName = r.Split(x);\r
134                 if (presetName[0] == name)\r
135                     return presetName[2];\r
136             }\r
137 \r
138             // User Presets\r
139             foreach (string item in user_presets)\r
140             {\r
141                 string x = item.Replace("+ ", "");\r
142                 Regex r = new Regex("(:  )"); // Split on hyphens. \r
143                 string[] presetName = r.Split(x);\r
144                 if (presetName[0] == name)\r
145                     return presetName[2];\r
146             }\r
147 \r
148             return null;\r
149         }\r
150 \r
151         /// <summary>\r
152         /// Load in the preset data from presets.dat and user_presets.dat\r
153         /// Load it into the 2 arraylist's presets and user_presets\r
154         /// </summary>\r
155         public void loadPresetFiles()\r
156         {\r
157             // First clear the presets arraylists\r
158             presets.Clear();\r
159             user_presets.Clear();\r
160 \r
161             // Load in the built in presets from presets.dat\r
162             // We'll store them in the array in the format:   presetName:  ClI Query\r
163             string filePath = Application.StartupPath.ToString() + "\\presets.dat";\r
164             if (File.Exists(filePath))\r
165             {\r
166                 StreamReader presetInput = new StreamReader(filePath);\r
167                 while (!presetInput.EndOfStream)\r
168                 {\r
169                     if ((char)presetInput.Peek() == '+')\r
170                         presets.Add(presetInput.ReadLine().Replace("+ ", ""));\r
171                     else\r
172                         presetInput.ReadLine();\r
173                 }\r
174                 presetInput.Close();\r
175                 presetInput.Dispose();\r
176             }\r
177 \r
178             // Load in the users presets from user_presets.dat\r
179             filePath = Application.StartupPath.ToString() + "\\user_presets.dat";\r
180             if (File.Exists(filePath))\r
181             {\r
182                 StreamReader presetInput = new StreamReader(filePath);\r
183                 while (!presetInput.EndOfStream)\r
184                 {\r
185                     if ((char)presetInput.Peek() == '+')\r
186                         user_presets.Add(presetInput.ReadLine().Replace("+ ", ""));\r
187                     else\r
188                         presetInput.ReadLine();\r
189                 }\r
190                 presetInput.Close();\r
191                 presetInput.Dispose();\r
192             }\r
193         }\r
194 \r
195         // Add a single preset to user_presets.dat\r
196         private void addPresetToFile(string preset)\r
197         {\r
198             string userPresets = Application.StartupPath.ToString() + "\\user_presets.dat";\r
199             try\r
200             {\r
201                 // Create a StreamWriter and open the file\r
202                 StreamWriter line = File.AppendText(userPresets);\r
203 \r
204                 // Generate and write the preset string to the file\r
205                 line.WriteLine(preset);\r
206 \r
207                 // close the stream\r
208                 line.Close();\r
209                 line.Dispose();\r
210                 MessageBox.Show("Your profile has been sucessfully added.", "Status", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);\r
211             }\r
212             catch (Exception exc)\r
213             {\r
214                 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
215             }\r
216 \r
217         }\r
218 \r
219         // Check if a preset already exists in either the built in or user presets\r
220         private Boolean checkIfPresetExists(string name)\r
221         {\r
222             if (name == string.Empty)\r
223                 return true;\r
224 \r
225             // Built In Presets\r
226             foreach (string item in presets)\r
227             {\r
228                 string x = item.Replace("+ ", "");\r
229                 Regex r = new Regex("(:  )"); // Split on hyphens. \r
230                 string[] presetName = r.Split(x);\r
231                 if (presetName[0] == name)\r
232                     return true;\r
233             }\r
234 \r
235             // User Presets\r
236             foreach (string item in user_presets)\r
237             {\r
238                 string x = item.Replace("+ ", "");\r
239                 Regex r = new Regex("(:  )"); // Split on hyphens. \r
240                 string[] presetName = r.Split(x);\r
241                 if (presetName[0] == name)\r
242                     return true;\r
243             }\r
244 \r
245             return false;\r
246         }\r
247     }\r
248 \r
249 }