Repository navigation
Expand file tree
/
Copy pathAudioFileInspectorForm.cs
More file actions
221 lines (192 loc) · 6.7 KB
/
Copy pathAudioFileInspectorForm.cs
File metadata and controls
221 lines (192 loc) · 6.7 KB
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.ComponentModel.Composition;
namespace AudioFileInspector;
[Export(typeof(AudioFileInspectorForm))]
public partial class AudioFileInspectorForm : Form
{
public ICollection<IAudioFileInspector> Inspectors { get; private set; }
private string filterString;
private int filterIndex;
private string currentFile;
private FindForm findForm;
public string[] CommandLineArguments { get; set; }
[ImportingConstructor]
public AudioFileInspectorForm([ImportMany(typeof(IAudioFileInspector))] IEnumerable<IAudioFileInspector> inspectors)
{
InitializeComponent();
this.Inspectors = new List<IAudioFileInspector>(inspectors);
}
private void DescribeFile(string fileName)
{
currentFile = fileName;
textLog.Text = String.Format("Opening {0}\r\n", fileName);
try
{
string extension = System.IO.Path.GetExtension(fileName).ToLower();
bool described = false;
foreach (IAudioFileInspector inspector in Inspectors)
{
if (extension == inspector.FileExtension)
{
textLog.AppendText(inspector.Describe(fileName));
described = true;
break;
}
}
if (!described)
{
textLog.AppendText("Unrecognised file type");
}
}
catch (Exception ex)
{
textLog.AppendText(ex.ToString());
}
}
private void CreateFilterString()
{
StringBuilder stringBuilder = new StringBuilder();
if (Inspectors.Count > 0)
{
stringBuilder.Append("All Supported Files|");
foreach (IAudioFileInspector inspector in Inspectors)
{
stringBuilder.AppendFormat("*{0};", inspector.FileExtension);
}
stringBuilder.Length--;
stringBuilder.Append("|");
foreach (IAudioFileInspector inspector in Inspectors)
{
stringBuilder.AppendFormat("{0}|*{1}|", inspector.FileTypeDescription, inspector.FileExtension);
}
}
stringBuilder.Append("All files (*.*)|*.*");
filterString = stringBuilder.ToString();
filterIndex = 1;
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = filterString;
ofd.FilterIndex = filterIndex;
if (ofd.ShowDialog() == DialogResult.OK)
{
filterIndex = ofd.FilterIndex;
DescribeFile(ofd.FileName);
textLog.SelectionStart = 0;
textLog.SelectionLength = 0;
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
NAudio.Utils.AboutForm aboutForm = new NAudio.Utils.AboutForm();
aboutForm.ShowDialog();
}
private void AudioFileInspectorForm_Load(object sender, EventArgs e)
{
CreateFilterString();
if (CommandLineArguments != null && CommandLineArguments.Length > 0)
{
DescribeFile(CommandLineArguments[0]);
}
}
private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
{
OptionsForm optionsForm = new OptionsForm(Inspectors);
optionsForm.ShowDialog();
}
private void AudioFileInspectorForm_DragOver(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
}
private void AudioFileInspectorForm_DragDrop(object sender, DragEventArgs e)
{
if (!e.Data.GetDataPresent(DataFormats.FileDrop))
{
return;
}
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (files.Length > 0)
{
DescribeFile(files[0]);
}
}
private void saveLogToolStripMenuItem_Click(object sender, EventArgs args)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.DefaultExt = ".txt";
if (currentFile != null)
{
saveFileDialog.InitialDirectory = Path.GetDirectoryName(currentFile);
saveFileDialog.FileName = Path.GetFileNameWithoutExtension(currentFile) + ".txt";
}
saveFileDialog.Filter = "Text Files (*.txt)|*.txt";
saveFileDialog.FilterIndex = 1;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
using StreamWriter writer = new StreamWriter(saveFileDialog.FileName);
string text = textLog.Text;
if (!text.Contains("\r"))
{
text = text.Replace("\n", "\r\n");
}
writer.Write(text);
}
catch (Exception e)
{
MessageBox.Show(
String.Format("Error saving conversion log\r\n{0}", e.Message),
Application.ProductName,
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
private void contentsToolStripMenuItem_Click(object sender, EventArgs e)
{
string helpFilePath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "audio_file_inspector.html");
try
{
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(helpFilePath) { UseShellExecute = true });
}
catch (Win32Exception)
{
MessageBox.Show("Could not display the help file", Application.ProductName);
}
}
private void clearLogToolStripMenuItem_Click(object sender, EventArgs e)
{
textLog.Clear();
}
private void findToolStripMenuItem_Click(object sender, EventArgs e)
{
if (findForm == null)
{
findForm = new FindForm(textLog);
findForm.Disposed += new EventHandler(findForm_Disposed);
}
findForm.Show(this);
}
private void findForm_Disposed(object sender, EventArgs e)
{
findForm = null;
}
private void AudioFileInspectorForm_FormClosed(object sender, FormClosedEventArgs e)
{
findForm?.Close();
}
}