-
Notifications
You must be signed in to change notification settings - Fork 5
/
Pub.cs
349 lines (303 loc) · 8.89 KB
/
Pub.cs
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
using System;
using System.Configuration;
using System.Windows.Forms;
using System.IO;
using System.Text;
using System.Collections;
namespace Subindex
{
public enum SaveDoType {Dialog,View,Nothing};
/// <summary>
///
/// </summary>
public class Pub
{
public static readonly string Limit="\r\n";
public static readonly string SrtSymbol="-->";
public static readonly string SsaSymbol="Dialogue: ";
public static readonly string SsaLimit=",";
public static readonly string StyleBeginChar="{";
public static readonly string StyleEndChar="}";
public static readonly string[] Txt=new string[]
{
"Current is empty!",
"Open",
" already exist.\r\n Replace it?",
"Save as",
"\r\nfile no exist ! Please recheck it",
"Open file",
"\r\nfile saved! View it with notepad?",
"Save succeed",
"File format invalid !",
" File format invalid !",
"Parse failed"
};
public static readonly string Format="HH:mm:ss,fff";
public static readonly string FileType="SRT files (*.srt)|*.srt|SSA files (*.ssa)|*.ssa|ASS files (*.ass)|*.ass|Any Support files (*.srt,*.ssa,*.ass)|*.srt;*.ssa;*.ass|All files (*.*)|*.*";
public static int BeginIndex=1;
public static bool AutoWrap=true;
public static bool KeepFormat=false;
public static bool KeepLog=true;
public static string WrapSplit=" ";
public static string MillisecondChar=",";
public static SaveDoType SaveDoType ;
private static OpenFileDialog openbox;
private static string stmp;
public static string copyRight="";
//public static Section copyRight
static Pub()
{
}
private Pub()
{
}
public static string GetSetting(string Key)
{
string Value=null;
try
{
if (AppSettings.ConfigFileExist()) Value=ConfigurationSettings.AppSettings[Key];
}
catch
{
}
return Value;
}
// public static string GetSetting(string Key,string DefaultValue)
// {
// string Value=DefaultValue;
// try
// {
// if (AppSettings.ConfigFileExist())
// Value=ConfigurationSettings.AppSettings[Key];
// }
// catch
// {
// }
// return Value;
// }
public static void BuildUi(Control obj)
{
if (obj.Controls.Count>0)
for (int i=0;i<obj.Controls.Count;i++) BuildUi(obj.Controls[i]);
stmp=GetSetting(obj.Name);
if (stmp!=null) obj.Text=stmp;
switch (obj.GetType().Name)
{
case "ComboBox":
BuildComboBox(obj as ComboBox);
break;
case "DropDownButton":
BuildDropDownButton(obj as DropDownButton);
break;
}
//obj.MouseEnter+=new EventHandler(obj_MouseEnter);
}
// private static void obj_MouseEnter(object sender, EventArgs e)
// {
// (sender as Control).Focus();
//
// }
public static void BuildComboBox(ComboBox obj)
{
string listValue;
for (int i=0;i<obj.Items.Count;i++)
{
listValue=Pub.GetSetting(obj.Name+i);
if (listValue!=null) obj.Items[i]=listValue;
}
if (obj.Items.Count>0) obj.SelectedIndex=0;
}
public static void BuildDropDownButton(DropDownButton obj)
{
string listValue=null;
for (int i=0;i<obj.Items.Count;i++)
{
listValue=Pub.GetSetting(obj.Name+i);
if (listValue!=null) obj.Items[i].Text=listValue;
}
}
public static string[] ReadFile(string FileName)
{
if (!FileExist(FileName)) return null;
ArrayList list=new ArrayList();
string line;
try
{
using (StreamReader sr = new StreamReader(FileName,Encoding.Default))
while ((line=sr.ReadLine())!=null) list.Add(line);
}
catch
{
throw;
}
return (string[]) list.ToArray(typeof(string));
}
public static string ReadFileString(string FileName)
{
if (!FileExist(FileName)) return null;
string line;
try
{
using (StreamReader sr = new StreamReader(FileName,Encoding.Default))
line=sr.ReadToEnd();
}
catch
{
throw;
}
return line;
}
public static bool FileExist(string FileName)
{
if (FileName==null||FileName.Trim()=="") return false;
bool exist=File.Exists(FileName);
if (!exist)
MessageBox.Show(FileName+GetSetting("OpenFileNoExistText").Replace(@"\r\n","\r\n"),Pub.GetSetting("OpenFileNoExistTitle"),MessageBoxButtons.OK,MessageBoxIcon.Warning);
return exist;
}
public static string OpenFile()
{
if (openbox==null)
{
openbox=new OpenFileDialog();
// openbox.InitialDirectory = "c:\\" ;
openbox.FilterIndex = 0 ;
openbox.RestoreDirectory = true ;
};
openbox.FileName ="";
openbox.Filter = Pub.FileType;
openbox.ShowDialog();
return openbox.FileName;
}
public static Caption LoadFile(string FileName,TextBox tb)
{
Caption ct=null;
string[] buffer=Pub.ReadFile(FileName);
if (buffer!=null)
{
if (tb!=null) tb.Lines=buffer;
try
{
ct=CaptionParser.Parse(buffer);
}
catch (ExtendException ee)
{
MessageBox.Show(ee.Message,Pub.GetSetting("CaptionParseExceptionTitle"),MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
}
return ct;
}
public static string SaveFile(string FileBuffer)
{
SaveFileDialog sd=new SaveFileDialog();
sd.Filter="SRT files (*.srt)|*.srt";
sd.RestoreDirectory = true ;
if (sd.ShowDialog()==DialogResult.OK)
SaveFile(FileBuffer,sd.FileName,true);
return sd.FileName;
}
public static string SaveFile(string FileBuffer,string FileName,bool OverWrite)
{
if (!OverWrite&&File.Exists(FileName))
if (MessageBox.Show(FileName+GetSetting("SaveFileOverWriteText").Replace(@"\r\n","\r\n"),GetSetting("SaveFileOverWriteTitle"),MessageBoxButtons.YesNo,MessageBoxIcon.Warning)==DialogResult.No)
return "";
StreamWriter sw = new StreamWriter(FileName,false,Encoding.Default);
sw.Write(Loging(FileBuffer));
sw.Close();
switch (Pub.SaveDoType)
{
case SaveDoType.Dialog:
if (MessageBox.Show(FileName+GetSetting("SaveFileSucceedText").Replace(@"\r\n","\r\n") ,Pub.GetSetting("SaveFileSucceedTitle"),MessageBoxButtons.YesNo,MessageBoxIcon.Information)==DialogResult.Yes)
goto case SaveDoType.View;
break;
case SaveDoType.View:
System.Diagnostics.Process.Start("notepad.exe",FileName);
break;
}
return FileName;
}
private static string Loging(string FileBuffer)
{
return FileBuffer;
}
public static bool GetAppReg()
{
bool state=false;
Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.ClassesRoot;
Microsoft.Win32.RegistryKey ext = key.OpenSubKey(@"Subindex.exe\shell\open\command");
if (ext!=null)
{
string Value="\"" + Application.ExecutablePath + "\" \"%1\"";
state=Value==ext.GetValue("","").ToString();
}
return state;
}
public static bool GetRegState(string fileType)
{
bool state=false;
string appName = "Subindex.exe";
Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.ClassesRoot;
Microsoft.Win32.RegistryKey ext = key.OpenSubKey (fileType);
if (ext!=null)
state=appName==ext.GetValue("","").ToString();
return state&&GetAppReg();
}
public static void SetAppReg()
{
Microsoft.Win32.RegistryKey ext= Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(@"Subindex.exe\shell\open\command");
ext.SetValue("","\"" + Application.ExecutablePath + "\" \"%1\"");
ext.Flush();
ext.Close();
}
public static void RegFileType(string fileType)
{
string appName = "Subindex.exe";
Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.ClassesRoot;
Microsoft.Win32.RegistryKey ext = key.CreateSubKey(fileType);
string Value=ext.GetValue("","").ToString();
if (Value!=""&&Value!=appName) ext.SetValue(appName+".Undo",Value);
ext.SetValue("",appName);
ext.Flush();
ext.Close();
key=Microsoft.Win32.Registry.CurrentUser;
ext =key.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\"+fileType);
Value=ext.GetValue("Application","").ToString();
if (Value!=""&&Value!=appName) ext.SetValue(appName+".Undo",Value);
ext.SetValue("Application",appName);
Value=ext.GetValue("Progid","").ToString();
if (Value!="")
{
ext.SetValue("Progid.Undo",Value);
ext.DeleteValue("Progid");
}
ext.Flush();
ext.Close();
key.Close();
}
public static void UnRegFileType(string fileType)
{
string appName = "Subindex.exe";
Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.ClassesRoot;
Microsoft.Win32.RegistryKey ext = key.CreateSubKey(fileType);
string Value=ext.GetValue(appName+".Undo","").ToString();
ext.SetValue("",Value);
if (Value!="") ext.DeleteValue(appName+".Undo");
ext.Flush();
ext.Close();
key=Microsoft.Win32.Registry.CurrentUser;
ext =key.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\"+fileType);
Value=ext.GetValue(appName+".Undo","").ToString();
ext.SetValue("Application",Value);
if (Value!="") ext.DeleteValue(appName+".Undo");
Value=ext.GetValue("Progid.Undo","").ToString();
if (Value!="")
{
ext.SetValue("Progid",Value);
ext.DeleteValue("Progid.Undo");
}
ext.Flush();
ext.Close();
}
}
}