playist: do not duplicate now playing information

This commit is contained in:
Andrzej Rybczak
2012-09-11 20:21:46 +02:00
parent c8d83b9223
commit a98df99ba6
8 changed files with 50 additions and 57 deletions

View File

@@ -473,7 +473,7 @@ void MouseEvent::Run()
&& itsMouseEvent.y == LINES-(Config.statusbar_visibility ? 2 : 1) && itsMouseEvent.y == LINES-(Config.statusbar_visibility ? 2 : 1)
) // progressbar ) // progressbar
{ {
if (!myPlaylist->isPlaying()) if (!Mpd.isPlaying())
return; return;
Mpd.Seek(Mpd.GetTotalTime()*itsMouseEvent.x/double(COLS)); Mpd.Seek(Mpd.GetTotalTime()*itsMouseEvent.x/double(COLS));
} }
@@ -1087,7 +1087,7 @@ void Add::Run()
bool SeekForward::canBeRun() const bool SeekForward::canBeRun() const
{ {
return myPlaylist->NowPlayingSong() && Mpd.GetTotalTime() > 0; return Mpd.isPlaying() && Mpd.GetTotalTime() > 0;
} }
void SeekForward::Run() void SeekForward::Run()
@@ -1097,7 +1097,7 @@ void SeekForward::Run()
bool SeekBackward::canBeRun() const bool SeekBackward::canBeRun() const
{ {
return myPlaylist->NowPlayingSong() && Mpd.GetTotalTime() > 0; return Mpd.isPlaying() && Mpd.GetTotalTime() > 0;
} }
void SeekBackward::Run() void SeekBackward::Run()
@@ -1202,8 +1202,8 @@ void TogglePlayingSongCentering::Run()
{ {
Config.autocenter_mode = !Config.autocenter_mode; Config.autocenter_mode = !Config.autocenter_mode;
ShowMessage("Centering playing song: %s", Config.autocenter_mode ? "On" : "Off"); ShowMessage("Centering playing song: %s", Config.autocenter_mode ? "On" : "Off");
if (Config.autocenter_mode && myPlaylist->isPlaying() && !myPlaylist->Items->isFiltered()) if (Config.autocenter_mode && Mpd.isPlaying() && !myPlaylist->Items->isFiltered())
myPlaylist->Items->highlight(myPlaylist->NowPlaying); myPlaylist->Items->highlight(Mpd.GetCurrentlyPlayingSongPos());
} }
void UpdateDatabase::Run() void UpdateDatabase::Run()
@@ -1223,23 +1223,21 @@ bool JumpToPlayingSong::canBeRun() const
return ((myScreen == myPlaylist && !myPlaylist->isFiltered()) return ((myScreen == myPlaylist && !myPlaylist->isFiltered())
|| myScreen == myBrowser || myScreen == myBrowser
|| myScreen == myLibrary) || myScreen == myLibrary)
&& myPlaylist->isPlaying(); && Mpd.isPlaying();
} }
void JumpToPlayingSong::Run() void JumpToPlayingSong::Run()
{ {
if (myScreen == myPlaylist) if (myScreen == myPlaylist)
myPlaylist->Items->highlight(myPlaylist->NowPlaying); myPlaylist->Items->highlight(Mpd.GetCurrentlyPlayingSongPos());
else if (myScreen == myBrowser) else if (myScreen == myBrowser)
{ {
const MPD::Song *s = myPlaylist->NowPlayingSong(); myBrowser->LocateSong(myPlaylist->nowPlayingSong());
myBrowser->LocateSong(*s);
DrawHeader(); DrawHeader();
} }
else if (myScreen == myLibrary) else if (myScreen == myLibrary)
{ {
const MPD::Song *s = myPlaylist->NowPlayingSong(); myLibrary->LocateSong(myPlaylist->nowPlayingSong());
myLibrary->LocateSong(*s);
} }
} }
@@ -1668,14 +1666,14 @@ void JumpToTagEditor::Run()
bool JumpToPositionInSong::canBeRun() const bool JumpToPositionInSong::canBeRun() const
{ {
return myPlaylist->NowPlayingSong() && Mpd.GetTotalTime() > 0; return Mpd.isPlaying() && Mpd.GetTotalTime() > 0;
} }
void JumpToPositionInSong::Run() void JumpToPositionInSong::Run()
{ {
using Global::wFooter; using Global::wFooter;
const MPD::Song *s = myPlaylist->NowPlayingSong(); const MPD::Song s = myPlaylist->nowPlayingSong();
LockStatusbar(); LockStatusbar();
Statusbar() << "Position to go (in %/mm:ss/seconds(s)): "; Statusbar() << "Position to go (in %/mm:ss/seconds(s)): ";
@@ -1692,7 +1690,7 @@ void JumpToPositionInSong::Run()
if (newpos >= 0 && newpos <= Mpd.GetTotalTime()) if (newpos >= 0 && newpos <= Mpd.GetTotalTime())
Mpd.Seek(newpos); Mpd.Seek(newpos);
else 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 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()) if (newpos >= 0 && newpos <= Mpd.GetTotalTime())
Mpd.Seek(newpos); Mpd.Seek(newpos);
else 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 else
{ {

View File

@@ -97,7 +97,7 @@ void setProperties(NC::Menu<T> &menu, const MPD::Song &s, HasSongs &screen, bool
int song_pos = menu.isFiltered() ? s.getPosition() : drawn_pos; int song_pos = menu.isFiltered() ? s.getPosition() : drawn_pos;
is_now_playing = static_cast<void *>(&menu) == myPlaylist->Items is_now_playing = static_cast<void *>(&menu) == myPlaylist->Items
&& song_pos == myPlaylist->NowPlaying; && song_pos == Mpd.GetCurrentlyPlayingSongPos();
if (is_now_playing) if (is_now_playing)
menu << Config.now_playing_prefix; menu << Config.now_playing_prefix;
} }

View File

@@ -77,12 +77,12 @@ void Lyrics::Update()
# endif // HAVE_CURL_CURL_H # endif // HAVE_CURL_CURL_H
if (ReloadNP) if (ReloadNP)
{ {
const MPD::Song *s = myPlaylist->NowPlayingSong(); const MPD::Song s = myPlaylist->nowPlayingSong();
if (s && !s->getArtist().empty() && !s->getTitle().empty()) if (!s.empty() && !s.getArtist().empty() && !s.getTitle().empty())
{ {
DrawHeader(); DrawHeader();
itsScrollBegin = 0; itsScrollBegin = 0;
itsSong = *s; itsSong = s;
Load(); Load();
} }
ReloadNP = 0; ReloadNP = 0;
@@ -161,21 +161,21 @@ void Lyrics::SpacePressed()
} }
#ifdef HAVE_CURL_CURL_H #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; return;
std::string filename = GenerateFilename(*s); std::string filename = GenerateFilename(s);
std::ifstream f(filename.c_str()); std::ifstream f(filename.c_str());
if (f.is_open()) if (f.is_open())
{ {
f.close(); f.close();
return; 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); pthread_mutex_lock(&itsDIBLock);
if (itsWorkersNumber == itsMaxWorkersNumber) if (itsWorkersNumber == itsMaxWorkersNumber)
itsToDownload.push(s_copy); itsToDownload.push(s_copy);
@@ -194,7 +194,7 @@ void Lyrics::DownloadInBackground(const MPD::Song *s)
void *Lyrics::DownloadInBackgroundImpl(void *void_ptr) void *Lyrics::DownloadInBackgroundImpl(void *void_ptr)
{ {
MPD::Song *s = static_cast<MPD::Song *>(void_ptr); MPD::Song *s = static_cast<MPD::Song *>(void_ptr);
DownloadInBackgroundImplHelper(s); DownloadInBackgroundImplHelper(*s);
delete s; delete s;
while (true) while (true)
@@ -211,7 +211,7 @@ void *Lyrics::DownloadInBackgroundImpl(void *void_ptr)
itsToDownload.pop(); itsToDownload.pop();
pthread_mutex_unlock(&itsDIBLock); pthread_mutex_unlock(&itsDIBLock);
} }
DownloadInBackgroundImplHelper(s); DownloadInBackgroundImplHelper(*s);
delete s; delete s;
} }
@@ -222,10 +222,10 @@ void *Lyrics::DownloadInBackgroundImpl(void *void_ptr)
pthread_exit(0); 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 artist = Curl::escape(s.getArtist());
std::string title = Curl::escape(locale_to_utf_cpy(s->getTitle())); std::string title = Curl::escape(s.getTitle());
LyricsFetcher::Result result; LyricsFetcher::Result result;
bool fetcher_defined = itsFetcher && *itsFetcher; bool fetcher_defined = itsFetcher && *itsFetcher;
@@ -238,7 +238,7 @@ void Lyrics::DownloadInBackgroundImplHelper(MPD::Song *s)
break; break;
} }
if (result.first == true) if (result.first == true)
Save(GenerateFilename(*s), result.second); Save(GenerateFilename(s), result.second);
} }
void *Lyrics::Download() void *Lyrics::Download()

View File

@@ -58,7 +58,7 @@ class Lyrics : public Screen<NC::Scrollpad>
void Refetch(); void Refetch();
static void ToggleFetcher(); static void ToggleFetcher();
static void DownloadInBackground(const MPD::Song *s); static void DownloadInBackground(const MPD::Song &s);
# endif // HAVE_CURL_CURL_H # endif // HAVE_CURL_CURL_H
bool ReloadNP; bool ReloadNP;
@@ -71,8 +71,8 @@ class Lyrics : public Screen<NC::Scrollpad>
void Load(); void Load();
# ifdef HAVE_CURL_CURL_H # ifdef HAVE_CURL_CURL_H
static void *DownloadInBackgroundImpl(void *); static void *DownloadInBackgroundImpl(void *song_ptr);
static void DownloadInBackgroundImplHelper(MPD::Song *); static void DownloadInBackgroundImplHelper(const MPD::Song &s);
// lock for allowing exclusive access to itsToDownload and itsWorkersNumber // lock for allowing exclusive access to itsToDownload and itsWorkersNumber
static pthread_mutex_t itsDIBLock; static pthread_mutex_t itsDIBLock;
// storage for songs for which lyrics are scheduled to be downloaded // storage for songs for which lyrics are scheduled to be downloaded

View File

@@ -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() bool Playlist::isFiltered()
{ {
if (Items->isFiltered()) if (Items->isFiltered())
@@ -444,7 +454,7 @@ std::string Playlist::TotalLength()
if (Config.playlist_show_remaining_time && ReloadRemaining && !Items->isFiltered()) if (Config.playlist_show_remaining_time && ReloadRemaining && !Items->isFiltered())
{ {
itsRemainingTime = 0; 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(); itsRemainingTime += (*Items)[i].value().getDuration();
ReloadRemaining = false; ReloadRemaining = false;
} }
@@ -474,16 +484,6 @@ std::string Playlist::TotalLength()
return result.str(); 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) bool Playlist::Add(const MPD::Song &s, bool play, int position)
{ {
if (Config.ncmpc_like_songs_adding && checkForSong(s)) if (Config.ncmpc_like_songs_adding && checkForSong(s))

View File

@@ -30,7 +30,7 @@
class Playlist : public Screen<NC::Window>, public Filterable, public HasSongs, public Searchable class Playlist : public Screen<NC::Window>, public Filterable, public HasSongs, public Searchable
{ {
public: public:
Playlist() : NowPlaying(-1), itsTotalLength(0), itsRemainingTime(0), itsScrollBegin(0) { } Playlist() : itsTotalLength(0), itsRemainingTime(0), itsScrollBegin(0) { }
~Playlist() { } ~Playlist() { }
// Screen<NC::Window> implementation // Screen<NC::Window> implementation
@@ -67,9 +67,9 @@ class Playlist : public Screen<NC::Window>, public Filterable, public HasSongs,
virtual MPD::SongList getSelectedSongs() OVERRIDE; virtual MPD::SongList getSelectedSongs() OVERRIDE;
// private members // private members
MPD::Song nowPlayingSong();
bool isFiltered(); bool isFiltered();
bool isPlaying() { return NowPlaying >= 0 && !Items->empty(); }
const MPD::Song *NowPlayingSong();
void Sort(); void Sort();
void Reverse(); void Reverse();
@@ -95,8 +95,6 @@ class Playlist : public Screen<NC::Window>, public Filterable, public HasSongs,
NC::Menu< MPD::Song > *Items; NC::Menu< MPD::Song > *Items;
int NowPlaying;
static bool ReloadTotalLength; static bool ReloadTotalLength;
static bool ReloadRemaining; static bool ReloadRemaining;

View File

@@ -228,7 +228,7 @@ void SelectedItemsAdder::EnterPressed()
} }
else if (pos == 3) // after currently playing album else if (pos == 3) // after currently playing album
{ {
std::string album = myPlaylist->NowPlayingSong()->getAlbum(); std::string album = myPlaylist->nowPlayingSong().getAlbum();
int i; int i;
for (i = Mpd.GetCurrentlyPlayingSongPos()+1; i < int(myPlaylist->Items->size()); ++i) for (i = Mpd.GetCurrentlyPlayingSongPos()+1; i < int(myPlaylist->Items->size()); ++i)
if ((*myPlaylist->Items)[i].value().getAlbum() != album) if ((*myPlaylist->Items)[i].value().getAlbum() != album)

View File

@@ -226,8 +226,6 @@ void NcmpcppStatusChanged(MPD::Connection *, MPD::StatusChanges changed, void *)
int sx = wFooter->getX(); int sx = wFooter->getX();
int sy = wFooter->getY(); int sy = wFooter->getY();
myPlaylist->NowPlaying = Mpd.GetCurrentlyPlayingSongPos();
if (changed.Playlist) if (changed.Playlist)
{ {
np = Mpd.GetCurrentlyPlayingSong(); np = Mpd.GetCurrentlyPlayingSong();
@@ -344,7 +342,6 @@ void NcmpcppStatusChanged(MPD::Connection *, MPD::StatusChanges changed, void *)
if (!block_progressbar_update) if (!block_progressbar_update)
DrawProgressbar(0, 0); DrawProgressbar(0, 0);
Playlist::ReloadRemaining = true; Playlist::ReloadRemaining = true;
myPlaylist->NowPlaying = -1;
if (Config.new_design) if (Config.new_design)
{ {
*wHeader << NC::XY(0, 0) << wclrtoeol << NC::XY(0, 1) << wclrtoeol; *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 (changed.SongID)
{ {
if (myPlaylist->isPlaying()) if (Mpd.isPlaying())
{ {
GNUC_UNUSED int res; GNUC_UNUSED int res;
if (!Config.execute_on_song_change.empty()) if (!Config.execute_on_song_change.empty())
@@ -389,7 +386,7 @@ void NcmpcppStatusChanged(MPD::Connection *, MPD::StatusChanges changed, void *)
# ifdef HAVE_CURL_CURL_H # ifdef HAVE_CURL_CURL_H
if (Config.fetch_lyrics_in_background) if (Config.fetch_lyrics_in_background)
Lyrics::DownloadInBackground(myPlaylist->NowPlayingSong()); Lyrics::DownloadInBackground(myPlaylist->nowPlayingSong());
# endif // HAVE_CURL_CURL_H # endif // HAVE_CURL_CURL_H
if (Mpd.isPlaying()) if (Mpd.isPlaying())
@@ -400,7 +397,7 @@ void NcmpcppStatusChanged(MPD::Connection *, MPD::StatusChanges changed, void *)
} }
if (Config.autocenter_mode && !myPlaylist->Items->isFiltered()) 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) if (Config.now_playing_lyrics && isVisible(myLyrics) && Global::myOldScreen == myPlaylist)
myLyrics->ReloadNP = 1; myLyrics->ReloadNP = 1;