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