diff --git a/FRESHMusicPlayer/FRESHMusicPlayer-WPF-UI-Test/Forms/TagEditor/Integrations/DiscogsIntegration.cs b/FRESHMusicPlayer/FRESHMusicPlayer-WPF-UI-Test/Forms/TagEditor/Integrations/DiscogsIntegration.cs index 00f232f..2484669 100644 --- a/FRESHMusicPlayer/FRESHMusicPlayer-WPF-UI-Test/Forms/TagEditor/Integrations/DiscogsIntegration.cs +++ b/FRESHMusicPlayer/FRESHMusicPlayer-WPF-UI-Test/Forms/TagEditor/Integrations/DiscogsIntegration.cs @@ -15,10 +15,10 @@ namespace FRESHMusicPlayer.Forms.TagEditor.Integrations private readonly string Secret = "TaUMdjJnmmcjGttJbegdmRyOHyqQxljK"; public bool NeedsInternetConnection => true; - public DiscogsIntegration() + public bool Worked { get; set; } = true; + public DiscogsIntegration(HttpClient httpClient) { - httpClient = new HttpClient(); - httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("FRESHMusicPlayer/8.2.0 (https://github.com/Royce551/FRESHMusicPlayer)"); + this.httpClient = httpClient; } public TagEditorRelease Fetch(string id) @@ -49,7 +49,14 @@ namespace FRESHMusicPlayer.Forms.TagEditor.Integrations public List<(string Name, string Id)> Search(string query) { var releases = new List<(string Name, string Id)>(); - var json = JObject.Parse(httpClient.GetStringAsync($"https://api.discogs.com/database/search?q={{{query}}}&{{track}}&per_page=1&key={Key}&secret={Secret}").Result); + string jsonString; + try { jsonString = httpClient.GetStringAsync($"https://api.discogs.com/database/search?q={{{query}}}&{{track}}&per_page=1&key={Key}&secret={Secret}").Result; } + catch (HttpRequestException) + { + Worked = false; + return releases; + } + var json = JObject.Parse(jsonString); var z = json.SelectToken("results"); // Format for searching: https://www.discogs.com/developers#page:database,header:database-search foreach (var x in z) { diff --git a/FRESHMusicPlayer/FRESHMusicPlayer-WPF-UI-Test/Forms/TagEditor/Integrations/IReleaseIntegration.cs b/FRESHMusicPlayer/FRESHMusicPlayer-WPF-UI-Test/Forms/TagEditor/Integrations/IReleaseIntegration.cs index e13b9c6..d359d14 100644 --- a/FRESHMusicPlayer/FRESHMusicPlayer-WPF-UI-Test/Forms/TagEditor/Integrations/IReleaseIntegration.cs +++ b/FRESHMusicPlayer/FRESHMusicPlayer-WPF-UI-Test/Forms/TagEditor/Integrations/IReleaseIntegration.cs @@ -9,6 +9,7 @@ namespace FRESHMusicPlayer.Forms.TagEditor.Integrations interface IReleaseIntegration { bool NeedsInternetConnection { get; } + bool Worked { get; set; } List<(string Name, string Id)> Search(string query); TagEditorRelease Fetch(string id); } diff --git a/FRESHMusicPlayer/FRESHMusicPlayer-WPF-UI-Test/Forms/TagEditor/Integrations/MusicBrainzIntegration.cs b/FRESHMusicPlayer/FRESHMusicPlayer-WPF-UI-Test/Forms/TagEditor/Integrations/MusicBrainzIntegration.cs index 5a3db41..f2e010c 100644 --- a/FRESHMusicPlayer/FRESHMusicPlayer-WPF-UI-Test/Forms/TagEditor/Integrations/MusicBrainzIntegration.cs +++ b/FRESHMusicPlayer/FRESHMusicPlayer-WPF-UI-Test/Forms/TagEditor/Integrations/MusicBrainzIntegration.cs @@ -13,11 +13,11 @@ namespace FRESHMusicPlayer.Forms.TagEditor.Integrations { private readonly HttpClient httpClient; public bool NeedsInternetConnection => true; + public bool Worked { get; set; } = true; - public MusicBrainzIntegration() + public MusicBrainzIntegration(HttpClient httpClient) { - httpClient = new HttpClient(); - httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("FRESHMusicPlayer/8.2.0 (https://github.com/Royce551/FRESHMusicPlayer)"); + this.httpClient = httpClient; } public TagEditorRelease Fetch(string id) @@ -45,7 +45,14 @@ namespace FRESHMusicPlayer.Forms.TagEditor.Integrations public List<(string Name, string Id)> Search(string query) { var releases = new List<(string Name, string Id)>(); - var json = JObject.Parse(httpClient.GetStringAsync($"https://musicbrainz.org/ws/2/release/?query={query}&fmt=json").Result); + string jsonString; + try { jsonString = httpClient.GetStringAsync($"https://musicbrainz.org/ws/2/release/?query={query}&fmt=json").Result; } + catch (HttpRequestException) + { + Worked = false; + return releases; + } + var json = JObject.Parse(jsonString); var z = json.SelectToken("releases"); foreach (var x in z) { diff --git a/FRESHMusicPlayer/FRESHMusicPlayer-WPF-UI-Test/Forms/TagEditor/TagEditor.xaml.cs b/FRESHMusicPlayer/FRESHMusicPlayer-WPF-UI-Test/Forms/TagEditor/TagEditor.xaml.cs index 500b0dc..d976123 100644 --- a/FRESHMusicPlayer/FRESHMusicPlayer-WPF-UI-Test/Forms/TagEditor/TagEditor.xaml.cs +++ b/FRESHMusicPlayer/FRESHMusicPlayer-WPF-UI-Test/Forms/TagEditor/TagEditor.xaml.cs @@ -7,6 +7,7 @@ using System.IO; using System.Linq; using System.Windows; using System.Windows.Controls; +using System.Net.Http; using Winforms = System.Windows.Forms; using System.Windows.Shapes; using FRESHMusicPlayer.Forms.TagEditor.Integrations; @@ -22,9 +23,11 @@ namespace FRESHMusicPlayer.Forms.TagEditor private List filePathsToSaveInBackground = new List(); List Displayfilepaths = new List(); private bool unsavedChanges = false; + private HttpClient httpClient = new HttpClient(); public TagEditor(List filePaths) { InitializeComponent(); + httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("FRESHMusicPlayer/8.2.0 (https://github.com/Royce551/FRESHMusicPlayer)"); FilePaths = filePaths; MainWindow.Player.SongChanged += Player_SongChanged; InitFields(); @@ -177,50 +180,49 @@ namespace FRESHMusicPlayer.Forms.TagEditor InitFields(); } - private void DiscogsSourceMenuItem_Click(object sender, RoutedEventArgs e) => OpenAlbumIntegration(new DiscogsIntegration()); - private void MusicBrainzSourceMenuItem_Click(object sender, RoutedEventArgs e) => OpenAlbumIntegration(new MusicBrainzIntegration()); + private void DiscogsSourceMenuItem_Click(object sender, RoutedEventArgs e) => OpenAlbumIntegration(new DiscogsIntegration(httpClient)); + private void MusicBrainzSourceMenuItem_Click(object sender, RoutedEventArgs e) => OpenAlbumIntegration(new MusicBrainzIntegration(httpClient)); private void OpenAlbumIntegration(IReleaseIntegration integration) { - var dialog = new FMPTextEntryBox("Album", AlbumBox.Text); + + var dialog = new FMPTextEntryBox(Properties.Resources.TRACKINFO_ALBUM, AlbumBox.Text); dialog.ShowDialog(); if (!dialog.OK) return; - try + string query = dialog.Response; + var results = integration.Search(query); + + var index = 0; + if (!integration.Worked && integration.NeedsInternetConnection) { - string query = dialog.Response; - var results = integration.Search(query); - - var index = 0; - if (results.Count == 0) - { - MessageBox.Show("No results were found for this album :(", "FRESHMusicPlayer Tag Editor", MessageBoxButton.OK, MessageBoxImage.Error); - return; - } - if (results.Count > 1) - { - var disambiguation = new IntegrationDisambiguation(results); - disambiguation.ShowDialog(); - if (disambiguation.OK) index = disambiguation.SelectedIndex; - else return; - } - - var filePath = FilePaths[0]; - var release = integration.Fetch(results[index].Id); - var editor = new ReleaseIntegrationPage(release, new Track(filePath), filePath); - editor.ShowDialog(); - if (editor.OK) - { - ArtistBox.Text = editor.TrackToSave.Artist; - TitleBox.Text = editor.TrackToSave.Title; - AlbumBox.Text = editor.TrackToSave.Album; - GenreBox.Text = editor.TrackToSave.Genre; - YearBox.Text = editor.TrackToSave.Year.ToString(); - TrackNumBox.Text = editor.TrackToSave.TrackNumber.ToString(); - } + MessageBox.Show("Looks like you aren't connected to the internet", "FRESHMusicPlayer Tag Editor", MessageBoxButton.OK, MessageBoxImage.Error); + return; } - catch (System.Net.Http.HttpRequestException) + if (results.Count == 0) { + MessageBox.Show("No results were found for this album :(", "FRESHMusicPlayer Tag Editor", MessageBoxButton.OK, MessageBoxImage.Error); + return; + } + if (results.Count > 1) + { + var disambiguation = new IntegrationDisambiguation(results); + disambiguation.ShowDialog(); + if (disambiguation.OK) index = disambiguation.SelectedIndex; + else return; + } + var filePath = FilePaths[0]; + var release = integration.Fetch(results[index].Id); + var editor = new ReleaseIntegrationPage(release, new Track(filePath), filePath); + editor.ShowDialog(); + if (editor.OK) + { + ArtistBox.Text = editor.TrackToSave.Artist; + TitleBox.Text = editor.TrackToSave.Title; + AlbumBox.Text = editor.TrackToSave.Album; + GenreBox.Text = editor.TrackToSave.Genre; + YearBox.Text = editor.TrackToSave.Year.ToString(); + TrackNumBox.Text = editor.TrackToSave.TrackNumber.ToString(); } } diff --git a/FRESHMusicPlayer/FRESHMusicPlayer-WPF-UI-Test/MainWindow.xaml.cs b/FRESHMusicPlayer/FRESHMusicPlayer-WPF-UI-Test/MainWindow.xaml.cs index c724df5..0e9d800 100644 --- a/FRESHMusicPlayer/FRESHMusicPlayer-WPF-UI-Test/MainWindow.xaml.cs +++ b/FRESHMusicPlayer/FRESHMusicPlayer-WPF-UI-Test/MainWindow.xaml.cs @@ -583,24 +583,17 @@ namespace FRESHMusicPlayer { if (Environment.OSVersion.Version.Major >= 10 && App.Config.IntegrateSMTC) { - try - { - var smtcInterop = (WindowsInteropUtils.ISystemMediaTransportControlsInterop)WindowsRuntimeMarshal.GetActivationFactory(typeof(SystemMediaTransportControls)); - Window window = GetWindow(this); - var wih = new WindowInteropHelper(window); - IntPtr hWnd = wih.Handle; - Smtc = smtcInterop.GetForWindow(hWnd, new Guid("99FA3FF4-1742-42A6-902E-087D41F965EC")); - Smtc.IsPlayEnabled = true; - Smtc.IsPauseEnabled = true; - Smtc.IsNextEnabled = true; - Smtc.IsStopEnabled = true; - Smtc.IsPreviousEnabled = true; - Smtc.ButtonPressed += Smtc_ButtonPressed; - } - catch - { - // TODO: HACK - ignored; the way i'm detecting windows 10 currently does not work - } + var smtcInterop = (WindowsInteropUtils.ISystemMediaTransportControlsInterop)WindowsRuntimeMarshal.GetActivationFactory(typeof(SystemMediaTransportControls)); + Window window = GetWindow(this); + var wih = new WindowInteropHelper(window); + IntPtr hWnd = wih.Handle; + Smtc = smtcInterop.GetForWindow(hWnd, new Guid("99FA3FF4-1742-42A6-902E-087D41F965EC")); + Smtc.IsPlayEnabled = true; + Smtc.IsPauseEnabled = true; + Smtc.IsNextEnabled = true; + Smtc.IsStopEnabled = true; + Smtc.IsPreviousEnabled = true; + Smtc.ButtonPressed += Smtc_ButtonPressed; } else Smtc = null; if (App.Config.IntegrateDiscordRPC) Player.InitDiscordRPC("656678380283887626");