public void OnUpdateAlbumInfoCompleted(AlbumInfoRequest request, List<Song> AlbumTracks) { if (request.Equals(_lastAlbumRequest)) { GUIGraphicsContext.form.Invoke(new AlbumInfoCompletedDelegate(DoUpdateAlbumInfo), new object[] {request, AlbumTracks}); } else { Log.Warn("GUIMusicPlayingNow: OnUpdateAlbumInfoCompleted: unexpected responsetype for request: {0}", request.Type); } }
private void DoUpdateAlbumInfo(AlbumInfoRequest request, List<Song> AlbumTracks) { GUIListItem item = null; facadeAlbumInfo.Clear(); ToggleTopTrackRatings(false, PopularityRating.unknown); if (AlbumTracks.Count > 0) { // get total ratings float AlbumSongRating = 0; float ratingBase = 0; foreach (Song Song in AlbumTracks) { AlbumSongRating += Convert.ToSingle(Song.TimesPlayed); } // set % rating if (AlbumTracks[0].TimesPlayed > 0) { ratingBase = AlbumSongRating / Convert.ToSingle(AlbumTracks[0].TimesPlayed); } //else // ratingBase = 0.01f; // avoid division by zero AlbumSongRating = AlbumSongRating > 0 ? AlbumSongRating : 1; for (int i = 0; i < AlbumTracks.Count; i++) { float rating = 0; if (i == 0) { AlbumTracks[i].Rating = 11; } else { rating = (int)(ratingBase * Convert.ToSingle(AlbumTracks[i].TimesPlayed)); AlbumTracks[i].Rating = (int)(rating * 10 / AlbumSongRating); } item = new GUIListItem(AlbumTracks[i].ToShortString()); item.Label = AlbumTracks[i].Title; //item.Label2 = " (" + GUILocalizeStrings.Get(931) + ": " + Convert.ToString(AlbumTracks[i].TimesPlayed) + ")"; //item.Label2 = " (" + GUILocalizeStrings.Get(931) + ": " + Convert.ToString(AlbumTracks[i].Rating) + ")"; item.MusicTag = AlbumTracks[i].ToMusicTag(); item.IsPlayed = AlbumTracks[i].URL == "local" ? false : true; facadeAlbumInfo.Add(item); string currentTrackRatingProperty = "#Lastfm.Rating.AlbumTrack" + Convert.ToString(i + 1); try { GUIPropertyManager.SetProperty(currentTrackRatingProperty, Convert.ToString(AlbumTracks[i].Rating)); } catch (Exception ex) { Log.Warn("GUIMusicPlayingNow: Could not set last.fm rating - {0}", ex.Message); break; } // display 3 items only if (facadeAlbumInfo.Count == DISPLAY_LISTITEM_COUNT) { break; } } } if (facadeAlbumInfo.Count > 0) { int popularity = AlbumTracks[0].TimesPlayed; // only display stars if list is filled if (facadeAlbumInfo.Count == DISPLAY_LISTITEM_COUNT) { if (popularity > 40000) { ToggleTopTrackRatings(true, PopularityRating.famous); } else if (popularity > 10000) { ToggleTopTrackRatings(true, PopularityRating.known); } else if (popularity > 2500) { ToggleTopTrackRatings(true, PopularityRating.existent); } else { ToggleTopTrackRatings(true, PopularityRating.unknown); } } CurrentThumbFileName = GUIMusicBaseWindow.GetCoverArt(false, CurrentTrackFileName, CurrentTrackTag); if (CurrentThumbFileName.Length > 0) { // let us test if there is a larger cover art image string strLarge = Util.Utils.ConvertToLargeCoverArt(CurrentThumbFileName); if (Util.Utils.FileExistsInCache(strLarge)) { CurrentThumbFileName = strLarge; } AddImageToImagePathContainer(CurrentThumbFileName); } if (LblBestAlbumTracks != null) { LblBestAlbumTracks.Visible = true; } UpdateImagePathContainer(); // previously focus was set all the time // to maintain previous functionality set focus if // defauly skin control is present if (lblForceFocus != null) { GUIControl.FocusControl(GetID, ((int)ControlIDs.LIST_ALBUM_INFO)); } } }
/// <summary> /// Updates the album info for the current track playing. /// The album info is fetched asynchronously by adding a request onto the request queue of the AudioScrobblerUtils /// class. The response will be received via callback by a delegate (OnUpdateAlbumInfoCompleted). /// </summary> private void UpdateAlbumInfo() { string CurrentArtist = CleanTagString(CurrentTrackTag.Artist); string CurrentAlbum = CleanTagString(CurrentTrackTag.Album); if (_doAlbumLookups) { if (CurrentTrackTag == null) { return; } if (CurrentTrackTag.Artist == string.Empty || CurrentTrackTag.Album == string.Empty) { Log.Warn("GUIMusicPlayingNow: current tag invalid for album info lookup. File: {0}", g_Player.CurrentFile); return; } AlbumInfoRequest request = new AlbumInfoRequest( CurrentArtist, CurrentAlbum, true, true, new AlbumInfoRequest.AlbumInfoRequestHandler(OnUpdateAlbumInfoCompleted) ); _lastAlbumRequest = request; InfoScrobbler.AddRequest(request); } }