From 3b32d84a7662aa7e93f251c4e6922609d0fb7bd1 Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Thu, 19 Feb 2009 00:00:31 +0100 Subject: [PATCH] make Playlist::NowPlayingSong() return pointer, not reference --- src/lyrics.cpp | 7 ++++--- src/ncmpcpp.cpp | 32 ++++++++++++++++---------------- src/playlist.cpp | 10 +++++++--- src/playlist.h | 2 +- src/status.cpp | 18 +++++++++--------- 5 files changed, 37 insertions(+), 32 deletions(-) diff --git a/src/lyrics.cpp b/src/lyrics.cpp index 3531ac0e..f1fee09f 100644 --- a/src/lyrics.cpp +++ b/src/lyrics.cpp @@ -78,8 +78,8 @@ void Lyrics::Update() if (!Reload) return; - const MPD::Song &s = myPlaylist->NowPlayingSong(); - if (!s.GetArtist().empty() && !s.GetTitle().empty()) + const MPD::Song *s = myPlaylist->NowPlayingSong(); + if (s && !s->GetArtist().empty() && !s->GetTitle().empty()) SwitchTo(); else Reload = 0; @@ -106,7 +106,7 @@ void Lyrics::SwitchTo() } # endif - const MPD::Song *s = Reload ? &myPlaylist->NowPlayingSong() : myScreen->CurrentSong(); + const MPD::Song *s = Reload ? myPlaylist->NowPlayingSong() : myScreen->CurrentSong(); if (!s) return; @@ -117,6 +117,7 @@ void Lyrics::SwitchTo() Resize(); itsScrollBegin = 0; itsSong = *s; + itsSong.Localize(); if (!Reload) { myOldScreen = myScreen; diff --git a/src/ncmpcpp.cpp b/src/ncmpcpp.cpp index a8397e1f..9e0e4c9c 100644 --- a/src/ncmpcpp.cpp +++ b/src/ncmpcpp.cpp @@ -851,12 +851,12 @@ int main(int argc, char *argv[]) } else if (Keypressed(input, Key.SeekForward) || Keypressed(input, Key.SeekBackward)) { - if (!myPlaylist->isPlaying()) + const Song *s = myPlaylist->NowPlayingSong(); + + if (!s) continue; - const Song &s = Mpd->GetCurrentSong(); - - if (!s.GetTotalLength()) + if (!s->GetTotalLength()) { ShowMessage("Unknown item length!"); continue; @@ -877,13 +877,13 @@ int main(int argc, char *argv[]) int howmuch = Config.incremental_seeking ? (timer-t)/2+Config.seek_time : Config.seek_time; - if (songpos < s.GetTotalLength() && Keypressed(input, Key.SeekForward)) + if (songpos < s->GetTotalLength() && Keypressed(input, Key.SeekForward)) { songpos += howmuch; - if (songpos > s.GetTotalLength()) - songpos = s.GetTotalLength(); + if (songpos > s->GetTotalLength()) + songpos = s->GetTotalLength(); } - if (songpos < s.GetTotalLength() && songpos > 0 && Keypressed(input, Key.SeekBackward)) + if (songpos < s->GetTotalLength() && songpos > 0 && Keypressed(input, Key.SeekBackward)) { songpos -= howmuch; if (songpos < 0) @@ -891,9 +891,9 @@ int main(int argc, char *argv[]) } wFooter->Bold(1); - string tracklength = "[" + Song::ShowTime(songpos) + "/" + s.GetLength() + "]"; + string tracklength = "[" + Song::ShowTime(songpos) + "/" + s->GetLength() + "]"; *wFooter << XY(wFooter->GetWidth()-tracklength.length(), 1) << tracklength; - double progressbar_size = (double)songpos/(s.GetTotalLength()); + double progressbar_size = (double)songpos/(s->GetTotalLength()); int howlong = wFooter->GetWidth()*progressbar_size; mvwhline(wFooter->Raw(), 0, 0, 0, wFooter->GetWidth()); @@ -1211,12 +1211,12 @@ int main(int argc, char *argv[]) } else if (Keypressed(input, Key.GoToPosition)) { - if (!myPlaylist->isPlaying()) + const Song *s = myPlaylist->NowPlayingSong(); + + if (!s) continue; - const Song &s = Mpd->GetCurrentSong(); - - if (!s.GetTotalLength()) + if (!s->GetTotalLength()) { ShowMessage("Unknown item length!"); continue; @@ -1226,7 +1226,7 @@ int main(int argc, char *argv[]) string position = wFooter->GetString(3); int newpos = StrToInt(position); if (newpos > 0 && newpos < 100 && !position.empty()) - Mpd->Seek(s.GetTotalLength()*newpos/100.0); + Mpd->Seek(s->GetTotalLength()*newpos/100.0); UnlockStatusbar(); } else if (Keypressed(input, Key.ReverseSelection)) @@ -1382,7 +1382,7 @@ int main(int argc, char *argv[]) // if mpd deletes now playing song deletion will be sluggishly slow // then so we have to assure it will be deleted at the very end. if (myPlaylist->isPlaying() && !myPlaylist->Main()->isSelected(myPlaylist->NowPlaying)) - Mpd->QueueDeleteSongId(myPlaylist->NowPlayingSong().GetID()); + Mpd->QueueDeleteSongId(myPlaylist->NowPlayingSong()->GetID()); ShowMessage("Deleting all items but selected..."); Mpd->CommitQueue(); diff --git a/src/playlist.cpp b/src/playlist.cpp index a24efb7e..159533c7 100644 --- a/src/playlist.cpp +++ b/src/playlist.cpp @@ -327,10 +327,14 @@ std::string Playlist::TotalLength() return result.str(); } -const MPD::Song &Playlist::NowPlayingSong() +const MPD::Song *Playlist::NowPlayingSong() { - static MPD::Song null; - return isPlaying() ? w->at(NowPlaying) : null; + bool was_filtered = w->isFiltered(); + w->ShowAll(); + const MPD::Song *s = isPlaying() ? &w->at(NowPlaying) : 0; + if (was_filtered) + w->ShowFiltered(); + return s; } std::string Playlist::SongToString(const MPD::Song &s, void *data) diff --git a/src/playlist.h b/src/playlist.h index 4ded2375..28a0fb99 100644 --- a/src/playlist.h +++ b/src/playlist.h @@ -51,7 +51,7 @@ class Playlist : public Screen< Menu > virtual List *GetList() { return w; } bool isPlaying() { return NowPlaying >= 0 && !w->Empty(); } - const MPD::Song &NowPlayingSong(); + const MPD::Song *NowPlayingSong(); void Sort(); diff --git a/src/status.cpp b/src/status.cpp index 7b92ffc6..6753fe61 100644 --- a/src/status.cpp +++ b/src/status.cpp @@ -351,26 +351,26 @@ void NcmpcppStatusChanged(Connection *Mpd, StatusChanges changed, void *) } if (changed.ElapsedTime) { - const Song &s = Mpd->GetCurrentSong(); - if (!player_state.empty() && !s.Empty()) + const Song *s = myPlaylist->NowPlayingSong(); + if (s) { int elapsed = Mpd->GetElapsedTime(); // 'repeat one' mode check - be sure that we deal with item with known length - if (s.GetTotalLength() && elapsed == s.GetTotalLength()-1) + if (s->GetTotalLength() && elapsed == s->GetTotalLength()-1) repeat_one_allowed = 1; - WindowTitle(utf_to_locale_cpy(s.toString(Config.song_window_title_format))); + WindowTitle(utf_to_locale_cpy(s->toString(Config.song_window_title_format))); if (!block_statusbar_update && Config.statusbar_visibility) { string tracklength; - if (s.GetTotalLength()) + if (s->GetTotalLength()) { tracklength = " ["; tracklength += Song::ShowTime(elapsed); tracklength += "/"; - tracklength += s.GetLength(); + tracklength += s->GetLength(); tracklength += "]"; } else @@ -381,17 +381,17 @@ void NcmpcppStatusChanged(Connection *Mpd, StatusChanges changed, void *) } *wFooter << XY(0, 1) << wclrtoeol << player_state << fmtBoldEnd - << Scroller(utf_to_locale_cpy(s.toString(Config.song_status_format)), wFooter->GetWidth()-player_state.length()-tracklength.length(), playing_song_scroll_begin) + << Scroller(utf_to_locale_cpy(s->toString(Config.song_status_format)), wFooter->GetWidth()-player_state.length()-tracklength.length(), playing_song_scroll_begin) << fmtBold << XY(wFooter->GetWidth()-tracklength.length(), 1) << tracklength; } if (!block_progressbar_update) { - double progressbar_size = (double)elapsed/(s.GetTotalLength()); + double progressbar_size = (double)elapsed/(s->GetTotalLength()); int howlong = wFooter->GetWidth()*progressbar_size; wFooter->SetColor(Config.progressbar_color); mvwhline(wFooter->Raw(), 0, 0, 0, wFooter->GetWidth()); - if (s.GetTotalLength()) + if (s->GetTotalLength()) { mvwhline(wFooter->Raw(), 0, 0, '=',howlong); mvwaddch(wFooter->Raw(), 0, howlong, '>');