2 using System.Collections.Generic;
\r
4 using System.Windows.Forms;
\r
6 using System.Text.RegularExpressions;
\r
9 namespace Handbrake.Functions
\r
11 public class Presets
\r
13 List<string> presets = new List<string>();
\r
14 List<string> user_presets = new List<string>();
\r
17 /// Add a new preset to the system
\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
23 if (checkIfPresetExists(presetName) == false)
\r
25 String preset = "+ " + presetName + ": " + query;
\r
26 user_presets.Add(preset);
\r
27 addPresetToFile(preset);
\r
32 MessageBox.Show("Sorry, that preset name already exists. Please choose another!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
38 /// Remove a preset with a given name from either the built in or user preset list.
\r
40 /// <param name="name">String, the preset name</param>
\r
41 public void remove(string name)
\r
43 List<string> newPresets = new List<string>();
\r
44 List<string> newUserPresets = new List<string>();
\r
47 foreach (string item in presets)
\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
57 foreach (string item in user_presets)
\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
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
70 // Rebuild the presets.dat file
\r
71 StreamWriter line = new StreamWriter(presetsFile);
\r
72 foreach (string item in newPresets)
\r
74 line.WriteLine("+ " + item);
\r
79 // Rebuild the user_presets.dat file
\r
80 line = new StreamWriter(userPresets);
\r
81 foreach (string item in newUserPresets)
\r
83 line.WriteLine("+ " + item);
\r
90 /// Get an Arraylist of all the preset names.
\r
91 /// Includes both built in and user presets.
\r
93 /// <returns>Arraylist of preset names</returns>
\r
94 public List<string> getPresetNames()
\r
96 List<string> names = new List<string>();
\r
99 foreach (string item in presets)
\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
109 foreach (string item in user_presets)
\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
122 /// Return the CLI query for a preset name given in name
\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
128 // Built In Presets
\r
129 foreach (string item in presets)
\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
139 foreach (string item in user_presets)
\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
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
155 public void loadPresetFiles()
\r
157 // First clear the presets arraylists
\r
159 user_presets.Clear();
\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
166 StreamReader presetInput = new StreamReader(filePath);
\r
167 while (!presetInput.EndOfStream)
\r
169 if ((char)presetInput.Peek() == '+')
\r
170 presets.Add(presetInput.ReadLine().Replace("+ ", ""));
\r
172 presetInput.ReadLine();
\r
174 presetInput.Close();
\r
175 presetInput.Dispose();
\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
182 StreamReader presetInput = new StreamReader(filePath);
\r
183 while (!presetInput.EndOfStream)
\r
185 if ((char)presetInput.Peek() == '+')
\r
186 user_presets.Add(presetInput.ReadLine().Replace("+ ", ""));
\r
188 presetInput.ReadLine();
\r
190 presetInput.Close();
\r
191 presetInput.Dispose();
\r
195 // Add a single preset to user_presets.dat
\r
196 private void addPresetToFile(string preset)
\r
198 string userPresets = Application.StartupPath.ToString() + "\\user_presets.dat";
\r
201 // Create a StreamWriter and open the file
\r
202 StreamWriter line = File.AppendText(userPresets);
\r
204 // Generate and write the preset string to the file
\r
205 line.WriteLine(preset);
\r
207 // close the stream
\r
210 MessageBox.Show("Your profile has been sucessfully added.", "Status", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
\r
212 catch (Exception exc)
\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
219 // Check if a preset already exists in either the built in or user presets
\r
220 private Boolean checkIfPresetExists(string name)
\r
222 if (name == string.Empty)
\r
225 // Built In Presets
\r
226 foreach (string item in presets)
\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
236 foreach (string item in user_presets)
\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