From a98df99ba657c87664aee6f56f15233faabe7f6a Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Tue, 11 Sep 2012 20:21:46 +0200 Subject: [PATCH] playist: do not duplicate now playing information --- src/actions.cpp | 28 +++++++++++++--------------- src/display.cpp | 2 +- src/lyrics.cpp | 28 ++++++++++++++-------------- src/lyrics.h | 6 +++--- src/playlist.cpp | 22 +++++++++++----------- src/playlist.h | 8 +++----- src/sel_items_adder.cpp | 2 +- src/status.cpp | 11 ++++------- 8 files changed, 50 insertions(+), 57 deletions(-) diff --git a/src/actions.cpp b/src/actions.cpp index 4c017ad3..7eee21a5 100644 --- a/src/actions.cpp +++ b/src/actions.cpp @@ -473,7 +473,7 @@ void MouseEvent::Run() && itsMouseEvent.y == LINES-(Config.statusbar_visibility ? 2 : 1) ) // progressbar { - if (!myPlaylist->isPlaying()) + if (!Mpd.isPlaying()) return; Mpd.Seek(Mpd.GetTotalTime()*itsMouseEvent.x/double(COLS)); } @@ -1087,7 +1087,7 @@ void Add::Run() bool SeekForward::canBeRun() const { - return myPlaylist->NowPlayingSong() && Mpd.GetTotalTime() > 0; + return Mpd.isPlaying() && Mpd.GetTotalTime() > 0; } void SeekForward::Run() @@ -1097,7 +1097,7 @@ void SeekForward::Run() bool SeekBackward::canBeRun() const { - return myPlaylist->NowPlayingSong() && Mpd.GetTotalTime() > 0; + return Mpd.isPlaying() && Mpd.GetTotalTime() > 0; } void SeekBackward::Run() @@ -1202,8 +1202,8 @@ void TogglePlayingSongCentering::Run() { Config.autocenter_mode = !Config.autocenter_mode; ShowMessage("Centering playing song: %s", Config.autocenter_mode ? "On" : "Off"); - if (Config.autocenter_mode && myPlaylist->isPlaying() && !myPlaylist->Items->isFiltered()) - myPlaylist->Items->highlight(myPlaylist->NowPlaying); + if (Config.autocenter_mode && Mpd.isPlaying() && !myPlaylist->Items->isFiltered()) + myPlaylist->Items->highlight(Mpd.GetCurrentlyPlayingSongPos()); } void UpdateDatabase::Run() @@ -1223,23 +1223,21 @@ bool JumpToPlayingSong::canBeRun() const return ((myScreen == myPlaylist && !myPlaylist->isFiltered()) || myScreen == myBrowser || myScreen == myLibrary) - && myPlaylist->isPlaying(); + && Mpd.isPlaying(); } void JumpToPlayingSong::Run() { if (myScreen == myPlaylist) - myPlaylist->Items->highlight(myPlaylist->NowPlaying); + myPlaylist->Items->highlight(Mpd.GetCurrentlyPlayingSongPos()); else if (myScreen == myBrowser) { - const MPD::Song *s = myPlaylist->NowPlayingSong(); - myBrowser->LocateSong(*s); + myBrowser->LocateSong(myPlaylist->nowPlayingSong()); DrawHeader(); } else if (myScreen == myLibrary) { - const MPD::Song *s = myPlaylist->NowPlayingSong(); - myLibrary->LocateSong(*s); + myLibrary->LocateSong(myPlaylist->nowPlayingSong()); } } @@ -1668,14 +1666,14 @@ void JumpToTagEditor::Run() bool JumpToPositionInSong::canBeRun() const { - return myPlaylist->NowPlayingSong() && Mpd.GetTotalTime() > 0; + return Mpd.isPlaying() && Mpd.GetTotalTime() > 0; } void JumpToPositionInSong::Run() { using Global::wFooter; - const MPD::Song *s = myPlaylist->NowPlayingSong(); + const MPD::Song s = myPlaylist->nowPlayingSong(); LockStatusbar(); Statusbar() << "Position to go (in %/mm:ss/seconds(s)): "; @@ -1692,7 +1690,7 @@ void JumpToPositionInSong::Run() if (newpos >= 0 && newpos <= Mpd.GetTotalTime()) Mpd.Seek(newpos); else - ShowMessage("Out of bounds, 0:00-%s possible for mm:ss, %s given", s->getLength().c_str(), MPD::Song::ShowTime(newpos).c_str()); + ShowMessage("Out of bounds, 0:00-%s possible for mm:ss, %s given", s.getLength().c_str(), MPD::Song::ShowTime(newpos).c_str()); } else if (position.find('s') != std::string::npos) // probably position in seconds { @@ -1700,7 +1698,7 @@ void JumpToPositionInSong::Run() if (newpos >= 0 && newpos <= Mpd.GetTotalTime()) Mpd.Seek(newpos); else - ShowMessage("Out of bounds, 0-%d possible for seconds, %d given", s->getDuration(), newpos); + ShowMessage("Out of bounds, 0-%d possible for seconds, %d given", s.getDuration(), newpos); } else { diff --git a/src/display.cpp b/src/display.cpp index d10961de..c754a323 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -97,7 +97,7 @@ void setProperties(NC::Menu &menu, const MPD::Song &s, HasSongs &screen, bool int song_pos = menu.isFiltered() ? s.getPosition() : drawn_pos; is_now_playing = static_cast(&menu) == myPlaylist->Items - && song_pos == myPlaylist->NowPlaying; + && song_pos == Mpd.GetCurrentlyPlayingSongPos(); if (is_now_playing) menu << Config.now_playing_prefix; } diff --git a/src/lyrics.cpp b/src/lyrics.cpp index df38bf51..f43b6415 100644 --- a/src/lyrics.cpp +++ b/src/lyrics.cpp @@ -77,12 +77,12 @@ void Lyrics::Update() # endif // HAVE_CURL_CURL_H if (ReloadNP) { - const MPD::Song *s = myPlaylist->NowPlayingSong(); - if (s && !s->getArtist().empty() && !s->getTitle().empty()) + const MPD::Song s = myPlaylist->nowPlayingSong(); + if (!s.empty() && !s.getArtist().empty() && !s.getTitle().empty()) { DrawHeader(); itsScrollBegin = 0; - itsSong = *s; + itsSong = s; Load(); } ReloadNP = 0; @@ -161,21 +161,21 @@ void Lyrics::SpacePressed() } #ifdef HAVE_CURL_CURL_H -void Lyrics::DownloadInBackground(const MPD::Song *s) +void Lyrics::DownloadInBackground(const MPD::Song &s) { - if (!s || s->getArtist().empty() || s->getTitle().empty()) + if (s.empty() || s.getArtist().empty() || s.getTitle().empty()) return; - std::string filename = GenerateFilename(*s); + std::string filename = GenerateFilename(s); std::ifstream f(filename.c_str()); if (f.is_open()) { f.close(); return; } - ShowMessage("Fetching lyrics for \"%s\"...", s->toString(Config.song_status_format_no_colors).c_str()); + ShowMessage("Fetching lyrics for \"%s\"...", s.toString(Config.song_status_format_no_colors).c_str()); - MPD::Song *s_copy = new MPD::Song(*s); + MPD::Song *s_copy = new MPD::Song(s); pthread_mutex_lock(&itsDIBLock); if (itsWorkersNumber == itsMaxWorkersNumber) itsToDownload.push(s_copy); @@ -194,7 +194,7 @@ void Lyrics::DownloadInBackground(const MPD::Song *s) void *Lyrics::DownloadInBackgroundImpl(void *void_ptr) { MPD::Song *s = static_cast(void_ptr); - DownloadInBackgroundImplHelper(s); + DownloadInBackgroundImplHelper(*s); delete s; while (true) @@ -211,7 +211,7 @@ void *Lyrics::DownloadInBackgroundImpl(void *void_ptr) itsToDownload.pop(); pthread_mutex_unlock(&itsDIBLock); } - DownloadInBackgroundImplHelper(s); + DownloadInBackgroundImplHelper(*s); delete s; } @@ -222,10 +222,10 @@ void *Lyrics::DownloadInBackgroundImpl(void *void_ptr) pthread_exit(0); } -void Lyrics::DownloadInBackgroundImplHelper(MPD::Song *s) +void Lyrics::DownloadInBackgroundImplHelper(const MPD::Song &s) { - std::string artist = Curl::escape(locale_to_utf_cpy(s->getArtist())); - std::string title = Curl::escape(locale_to_utf_cpy(s->getTitle())); + std::string artist = Curl::escape(s.getArtist()); + std::string title = Curl::escape(s.getTitle()); LyricsFetcher::Result result; bool fetcher_defined = itsFetcher && *itsFetcher; @@ -238,7 +238,7 @@ void Lyrics::DownloadInBackgroundImplHelper(MPD::Song *s) break; } if (result.first == true) - Save(GenerateFilename(*s), result.second); + Save(GenerateFilename(s), result.second); } void *Lyrics::Download() diff --git a/src/lyrics.h b/src/lyrics.h index f0ee6985..828aa664 100644 --- a/src/lyrics.h +++ b/src/lyrics.h @@ -58,7 +58,7 @@ class Lyrics : public Screen void Refetch(); static void ToggleFetcher(); - static void DownloadInBackground(const MPD::Song *s); + static void DownloadInBackground(const MPD::Song &s); # endif // HAVE_CURL_CURL_H bool ReloadNP; @@ -71,8 +71,8 @@ class Lyrics : public Screen void Load(); # ifdef HAVE_CURL_CURL_H - static void *DownloadInBackgroundImpl(void *); - static void DownloadInBackgroundImplHelper(MPD::Song *); + static void *DownloadInBackgroundImpl(void *song_ptr); + static void DownloadInBackgroundImplHelper(const MPD::Song &s); // lock for allowing exclusive access to itsToDownload and itsWorkersNumber static pthread_mutex_t itsDIBLock; // storage for songs for which lyrics are scheduled to be downloaded diff --git a/src/playlist.cpp b/src/playlist.cpp index 9b749cd1..4ab041d0 100644 --- a/src/playlist.cpp +++ b/src/playlist.cpp @@ -364,6 +364,16 @@ MPD::SongList Playlist::getSelectedSongs() /***********************************************************************/ +MPD::Song Playlist::nowPlayingSong() +{ + MPD::Song s; + if (Mpd.isPlaying()) + withUnfilteredMenu(*Items, [this, &s]() { + s = Items->at(Mpd.GetCurrentSongPos()).value(); + }); + return s; +} + bool Playlist::isFiltered() { if (Items->isFiltered()) @@ -444,7 +454,7 @@ std::string Playlist::TotalLength() if (Config.playlist_show_remaining_time && ReloadRemaining && !Items->isFiltered()) { itsRemainingTime = 0; - for (size_t i = NowPlaying; i < Items->size(); ++i) + for (size_t i = Mpd.GetCurrentlyPlayingSongPos(); i < Items->size(); ++i) itsRemainingTime += (*Items)[i].value().getDuration(); ReloadRemaining = false; } @@ -474,16 +484,6 @@ std::string Playlist::TotalLength() return result.str(); } -const MPD::Song *Playlist::NowPlayingSong() -{ - bool was_filtered = Items->isFiltered(); - Items->showAll(); - const MPD::Song *s = isPlaying() ? &Items->at(NowPlaying).value() : 0; - if (was_filtered) - Items->showFiltered(); - return s; -} - bool Playlist::Add(const MPD::Song &s, bool play, int position) { if (Config.ncmpc_like_songs_adding && checkForSong(s)) diff --git a/src/playlist.h b/src/playlist.h index b3729666..a63fc2dd 100644 --- a/src/playlist.h +++ b/src/playlist.h @@ -30,7 +30,7 @@ class Playlist : public Screen, public Filterable, public HasSongs, public Searchable { public: - Playlist() : NowPlaying(-1), itsTotalLength(0), itsRemainingTime(0), itsScrollBegin(0) { } + Playlist() : itsTotalLength(0), itsRemainingTime(0), itsScrollBegin(0) { } ~Playlist() { } // Screen implementation @@ -67,9 +67,9 @@ class Playlist : public Screen, public Filterable, public HasSongs, virtual MPD::SongList getSelectedSongs() OVERRIDE; // private members + MPD::Song nowPlayingSong(); + bool isFiltered(); - bool isPlaying() { return NowPlaying >= 0 && !Items->empty(); } - const MPD::Song *NowPlayingSong(); void Sort(); void Reverse(); @@ -95,8 +95,6 @@ class Playlist : public Screen, public Filterable, public HasSongs, NC::Menu< MPD::Song > *Items; - int NowPlaying; - static bool ReloadTotalLength; static bool ReloadRemaining; diff --git a/src/sel_items_adder.cpp b/src/sel_items_adder.cpp index bd96a874..490332a2 100644 --- a/src/sel_items_adder.cpp +++ b/src/sel_items_adder.cpp @@ -228,7 +228,7 @@ void SelectedItemsAdder::EnterPressed() } else if (pos == 3) // after currently playing album { - std::string album = myPlaylist->NowPlayingSong()->getAlbum(); + std::string album = myPlaylist->nowPlayingSong().getAlbum(); int i; for (i = Mpd.GetCurrentlyPlayingSongPos()+1; i < int(myPlaylist->Items->size()); ++i) if ((*myPlaylist->Items)[i].value().getAlbum() != album) diff --git a/src/status.cpp b/src/status.cpp index 51ca6fc6..b69bbdff 100644 --- a/src/status.cpp +++ b/src/status.cpp @@ -226,8 +226,6 @@ void NcmpcppStatusChanged(MPD::Connection *, MPD::StatusChanges changed, void *) int sx = wFooter->getX(); int sy = wFooter->getY(); - myPlaylist->NowPlaying = Mpd.GetCurrentlyPlayingSongPos(); - if (changed.Playlist) { np = Mpd.GetCurrentlyPlayingSong(); @@ -344,7 +342,6 @@ void NcmpcppStatusChanged(MPD::Connection *, MPD::StatusChanges changed, void *) if (!block_progressbar_update) DrawProgressbar(0, 0); Playlist::ReloadRemaining = true; - myPlaylist->NowPlaying = -1; if (Config.new_design) { *wHeader << NC::XY(0, 0) << wclrtoeol << NC::XY(0, 1) << wclrtoeol; @@ -381,7 +378,7 @@ void NcmpcppStatusChanged(MPD::Connection *, MPD::StatusChanges changed, void *) } if (changed.SongID) { - if (myPlaylist->isPlaying()) + if (Mpd.isPlaying()) { GNUC_UNUSED int res; if (!Config.execute_on_song_change.empty()) @@ -389,7 +386,7 @@ void NcmpcppStatusChanged(MPD::Connection *, MPD::StatusChanges changed, void *) # ifdef HAVE_CURL_CURL_H if (Config.fetch_lyrics_in_background) - Lyrics::DownloadInBackground(myPlaylist->NowPlayingSong()); + Lyrics::DownloadInBackground(myPlaylist->nowPlayingSong()); # endif // HAVE_CURL_CURL_H if (Mpd.isPlaying()) @@ -398,9 +395,9 @@ void NcmpcppStatusChanged(MPD::Connection *, MPD::StatusChanges changed, void *) if (!np.empty()) WindowTitle(np.toString(Config.song_window_title_format)); } - + if (Config.autocenter_mode && !myPlaylist->Items->isFiltered()) - myPlaylist->Items->highlight(myPlaylist->NowPlaying); + myPlaylist->Items->highlight(Mpd.GetCurrentlyPlayingSongPos()); if (Config.now_playing_lyrics && isVisible(myLyrics) && Global::myOldScreen == myPlaylist) myLyrics->ReloadNP = 1;