-
Notifications
You must be signed in to change notification settings - Fork 4
/
TagEditor.cs
253 lines (242 loc) · 9.82 KB
/
TagEditor.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TagLib;
using System.IO;
using ModifiedControls;
namespace Echoes
{
public partial class TagEditor : Form
{
public class TagItem
{
public string tag { get; set; }
public string value { get; set; }
public TagItem(string tag)
{
this.tag = tag;
}
}
DataGridView dgv = new DataGridView();
List<TagItem> tagItems = null;
List<Track> tracks;
Label modWarning;
ModifiedButton btnSave, btnCancel;
int bottomOfGrid;
public TagEditor(List<Track> tracks, Echoes parent)
{
InitializeComponent();
this.Owner = parent;
dgv.KeyDown += TagEditor_KeyDown;
dgv.Dock = DockStyle.Fill;
dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dgv.RowHeadersVisible = false;
dgv.AutoGenerateColumns = true;
dgv.Font = new Font(new FontFamily("Courier New"), 8f);
dgv.AllowUserToResizeRows = false;
dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dgv.AllowUserToOrderColumns = false;
dgv.AllowUserToDeleteRows = false;
dgv.AllowUserToAddRows = false;
dgv.AllowUserToResizeRows = false;
dgv.RowTemplate.Height = 18;
dgv.EnableHeadersVisualStyles = false;
dgv.MouseUp+=dgv_MouseUp;
dgv.Columns.Add(new DataGridViewTextBoxColumn()
{
Width = (int)(this.dgv.Width * 0.3),
DataPropertyName = "tag",
HeaderText = "Tag",
ReadOnly = true
});
dgv.Columns.Add(new DataGridViewTextBoxColumn()
{
Width = (int)(this.dgv.Width * 0.695),
DataPropertyName = "value",
HeaderText = "Value"
});
bottomOfGrid = 7 * dgv.RowTemplate.Height + dgv.ColumnHeadersHeight + (int)(Owner as Echoes).font1.Size + 5;
this.Text = "Tag Editor: " + tracks.Count + " files loaded";
this.tracks = tracks;
ControlBox = false;
btnSave = new ModifiedButton();
btnSave.Font = (Owner as Echoes).font1;
btnSave.Size = new Size(60, 30);
btnSave.Location = new Point(5, bottomOfGrid);
btnSave.Dock = DockStyle.None;
btnSave.Text = "Save";
btnSave.Click += (sender, eventArgs) =>
{
SaveTags();
this.Close();
this.Dispose();
};
btnCancel = new ModifiedButton();
btnCancel.Font = (Owner as Echoes).font1;
btnCancel.Size = new Size(60, 30);
btnCancel.Location = new Point(350, bottomOfGrid);
btnCancel.Dock = DockStyle.None;
btnCancel.Text = "Cancel";
btnCancel.Click += (sender, eventArgs) =>
{
this.Close();
this.Dispose();
};
if (tracks.Where(x => Program.mainWindow.supportedModuleTypes.Contains(Path.GetExtension(x.filename))).ToList().Count > 0)
{
modWarning = new Label();
modWarning.Location = new Point(btnSave.Location.X+btnSave.Width+5, bottomOfGrid+7);
modWarning.BackColor = Program.mainWindow.backgroundColor;
modWarning.ForeColor = Program.mainWindow.controlForeColor;
modWarning.Font = (Owner as Echoes).font1;
modWarning.Dock = DockStyle.None;
modWarning.Text = "Module file tag writing not supported.";
Controls.Add(modWarning);
}
Controls.Add(btnSave);
Controls.Add(btnCancel);
Controls.Add(dgv);
this.SetColors();
PopulateDgv();
this.ClientSize = new Size(btnCancel.Location.X+btnCancel.Width+5,btnSave.Location.Y+btnSave.Height+5);
if(modWarning!=null) modWarning.Width = ClientSize.Width - btnSave.Width * 2 - 20;
LoadTags();
}
void TagEditor_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
SaveTags();
this.Close();
this.Dispose();
}
}
void dgv_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
var hitTestInfo = dgv.HitTest(e.X, e.Y);
if (hitTestInfo.Type == DataGridViewHitTestType.Cell)
dgv.BeginEdit(true);
else
dgv.EndEdit();
}
}
void LoadTags()
{
if (tracks.Count == 1 && !Program.mainWindow.supportedModuleTypes.Contains(Path.GetExtension(tracks[0].filename)))
{
using (TagLib.File tagFile = TagLib.File.Create(tracks[0].filename))
{
dgv.Rows[0].Cells[1].Value = tagFile.Tag.Title;
if (tagFile.Tag.Performers.Length > 0) dgv.Rows[1].Cells[1].Value = tagFile.Tag.Performers[0];
else dgv.Rows[1].Cells[1].Value = "";
dgv.Rows[2].Cells[1].Value = tagFile.Tag.Album;
dgv.Rows[3].Cells[1].Value = tagFile.Tag.Year;
dgv.Rows[4].Cells[1].Value = tagFile.Tag.Comment;
if(tagFile.Tag.Genres.Length>0) dgv.Rows[5].Cells[1].Value = tagFile.Tag.Genres[0];
else dgv.Rows[5].Cells[1].Value = "";
tagFile.Dispose();
}
}
else
{
dgv.Rows[0].Cells[1].Value =
dgv.Rows[1].Cells[1].Value =
dgv.Rows[2].Cells[1].Value =
dgv.Rows[3].Cells[1].Value =
dgv.Rows[4].Cells[1].Value =
dgv.Rows[5].Cells[1].Value = "&unchanged";
}
}
void SaveTags()
{
dgv.EndEdit();
foreach (Track t in tracks)
{
if (Program.mainWindow.nowPlaying!=null && Program.mainWindow.nowPlaying.filename == t.filename) Program.mainWindow.UnloadAudioFile();
if (!System.IO.File.Exists(t.filename)) continue;
using (TagLib.File tagFile = TagLib.File.Create(t.filename))
{
string title = (string)dgv.Rows[0].Cells[1].Value;
if (title == "&unchanged") title = tagFile.Tag.Title;
string artist = (string)dgv.Rows[1].Cells[1].Value;
if (artist == "&unchanged")
{
if (tagFile.Tag.Performers.Length>0)
{
artist = tagFile.Tag.Performers[0];
}
else
{
artist = "";
}
}
string album = (string)dgv.Rows[2].Cells[1].Value;
if (album == "&unchanged") album = tagFile.Tag.Album;
string comment = (string)dgv.Rows[4].Cells[1].Value;
if (comment == "&unchanged") comment = tagFile.Tag.Comment;
string genre = (string)dgv.Rows[5].Cells[1].Value;
if (genre == "&unchanged")
{
if (tagFile.Tag.Genres.Length>0)
{
genre = tagFile.Tag.Genres[0];
}
else
{
genre = "";
}
}
int year = (int)tagFile.Tag.Year;
if ((string)dgv.Rows[3].Cells[1].Value != "&unchanged")
try
{
year = Int32.Parse((string)dgv.Rows[3].Cells[1].Value);
}
catch (Exception)
{
MessageBox.Show("Year field is invalid. Must be a 4-digit number.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
tagFile.Tag.Title = title;
tagFile.Tag.Performers = null;
tagFile.Tag.Performers = new[] { artist };
tagFile.Tag.Album = album;
tagFile.Tag.Year = (uint)year;
tagFile.Tag.Comment = comment;
tagFile.Tag.Genres = null;
tagFile.Tag.Genres = new[] { genre };
tagFile.Save();
if(!String.IsNullOrEmpty(title)) t.title = title;
t.artist = artist;
t.album = album;
//t.year=(string)year;
//t.comment=comment;
tagFile.Dispose();
}
}
Program.mainWindow.UpdateCache(tracks);
(Owner as Echoes).RefreshGrid();
}
void PopulateDgv() {
tagItems = new List<TagItem>()
{
new TagItem("Title"),
new TagItem("Artist"),
new TagItem("Album"),
new TagItem("Year"),
new TagItem("Comment"),
new TagItem("Genre")
};
BindingList<TagItem> source = new BindingList<TagItem>(tagItems);
dgv.DataSource = tagItems;
}
}
}