-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlbumDetails.cs
186 lines (174 loc) · 5.77 KB
/
AlbumDetails.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Hqub.MusicBrainz.API.Entities;
using ParkSquare.Gracenote;
namespace AlbumRecorder {
/// <summary>
/// Search Internet databases for album details
/// </summary>
public partial class AlbumDetails : Form {
TaskRunner m_Task;
public AlbumDetails() {
InitializeComponent();
m_Task = new TaskRunner(TaskException);
}
/// <summary>
/// Do an action in the UI thread
/// </summary>
public void Despatch(Action a) {
if (InvokeRequired) {
Invoke(a);
} else
a();
}
/// <summary>
/// Set the status label (can call from any thread)
/// </summary>
public void Status(string text) {
Despatch(delegate() { lblStatus.Text = text; });
}
/// <summary>
/// The currently selected album
/// </summary>
public AlbumInfo Album {
get {
return Results.SelectedItems.Count > 0 ? (AlbumInfo)Results.SelectedItems[0].Tag : null;
}
}
/// <summary>
/// Catch exceptions in the task
/// </summary>
private void TaskException(object sender, Exception ex) {
System.Diagnostics.Trace.WriteLine(ex);
Status(ex.Message);
}
/// <summary>
/// Change Cancel button to Select if an album is selected
/// </summary>
private void Results_SelectedIndexChanged(object sender, EventArgs e) {
btnCancel.Text = Results.SelectedItems.Count > 0 ? "Select" : "Cancel";
}
/// <summary>
/// Cancel/Select button clicked
/// </summary>
private void btnCancel_Click(object sender, EventArgs e) {
AlbumInfo a = Album;
if (a != null && (Program.Album == null || Program.Album.ReleaseId != a.ReleaseId || Program.Album.GracenoteId != a.GracenoteId)) {
// A different album is selected
DialogResult = System.Windows.Forms.DialogResult.OK;
} else {
// No change, or nothing selected
DialogResult = System.Windows.Forms.DialogResult.Cancel;
}
Close();
}
/// <summary>
/// Search button clicked
/// </summary>
private void btnOK_Click(object sender, EventArgs e) {
m_Task.Stop(); // Stop any existing search
Results.Items.Clear();
string artist = txtArtist.Text;
string title = txtTitle.Text;
m_Task.Run(async delegate(TaskRunner.Task t) {
int count = 0; // No of items added
int insertPoint = 0; // Where to insert close matches
try {
// Build query
List<string> b = new List<string>();
if (!string.IsNullOrWhiteSpace(artist))
b.Add("artist:\"" + artist.Replace("\"", "") + "\"");
if (!string.IsNullOrWhiteSpace(title))
b.Add("release:\"" + title.Replace("\"", "") + "\"");
if (b.Count == 0) {
Status("No artist or title provided");
return;
}
Status("Searching MusicBrainz database");
// Get list of releases
foreach (Release r in (await Release.SearchAsync(string.Join(" AND ", b.ToArray()))).Items) {
if (t.Stop)
return;
try {
// Get recordings for release (i.e. list of Mediums/Media)
Release rel = await Release.GetAsync(r.Id, "recordings");
foreach (Medium m in rel.MediumList.Items) {
if (t.Stop)
return;
// Artists
string art = string.Join(", ", r.Credits.Select(c => c.Artist.Name).ToArray());
ListViewItem item = new ListViewItem(new string[] { art, r.Title, r.Date, m.Tracks.Items.Count + ":" + string.Join(",", m.Tracks.Items.Select(tr => tr.Recording.Title)) });
// Make an AlbumInfo
item.Tag = new AlbumInfo(art, rel, m);
if (SoundEx.Equals(artist, art) && SoundEx.Equals(title, r.Title)) {
// Close match - put near top of list
Despatch(delegate() { Results.Items.Insert(insertPoint, item); });
insertPoint++;
} else {
// Not so close - put at end
Despatch(delegate() { Results.Items.Add(item); });
}
count++;
}
} catch (Hqub.MusicBrainz.API.HttpClientException) {
// Ignore these exceptions - they seem to happen all the time
}
}
} catch (Hqub.MusicBrainz.API.HttpClientException) {
// Ignore these exceptions - they seem to happen all the time
}
if (count == 0 && !t.Stop) {
// Nothing found - try Gracenote (NB - no track lengths here)
Status("Searching Gracenote database");
GracenoteClient Client = new GracenoteClient(Program.GracenoteKey);
SearchResult result = Client.Search(new SearchCriteria() {
AlbumTitle = title,
Artist = artist
});
foreach (Album a in result.Albums) {
if (t.Stop)
return;
ListViewItem item = new ListViewItem(new string[] { a.Artist, a.Title, a.Year.ToString(), a.Tracks.Count() + ":" + string.Join(",", a.Tracks.Select(tr => tr.Title)) });
item.Tag = new AlbumInfo(a);
if (SoundEx.Equals(artist, a.Artist) && SoundEx.Equals(title, a.Title))
Despatch(delegate() { Results.Items.Add(item); });
else {
Despatch(delegate() { Results.Items.Insert(insertPoint, item); });
insertPoint++;
}
count++;
}
}
if (!t.Stop) {
if (count > 0) {
// Resize columns to fit data
Invoke((Action)delegate() { Results.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent); });
Status("Search complete");
} else
Status("Nothing found");
}
});
}
/// <summary>
/// Stop any search when form closes
/// </summary>
private void AlbumDetails_FormClosing(object sender, FormClosingEventArgs e) {
m_Task.Stop();
}
/// <summary>
/// Reset Cancel button text on entry
/// </summary>
private void AlbumDetails_Load(object sender, EventArgs e) {
btnCancel.Text = "Cancel";
}
}
}