From 34dfffaab0e7d47a7590a9af77a8ededb20e413c Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 18 Aug 2008 17:16:00 +0200 Subject: [PATCH] various clean-ups and improvements --- src/helpers.cpp | 1 - src/lyrics.cpp | 2 +- src/menu.cpp | 33 +---------- src/mpdpp.cpp | 104 ++++++++++++++++++-------------- src/mpdpp.h | 11 ++-- src/ncmpcpp.cpp | 132 ++++++++++++++++++++--------------------- src/settings.cpp | 2 +- src/status_checker.cpp | 5 +- 8 files changed, 134 insertions(+), 156 deletions(-) diff --git a/src/helpers.cpp b/src/helpers.cpp index 04eeab39..ebf1db96 100644 --- a/src/helpers.cpp +++ b/src/helpers.cpp @@ -596,7 +596,6 @@ void GetDirectory(string dir) } case itSong: { - it->name = it->song->GetFile(); Song *s = it->song; bool bold = 0; for (SongList::const_iterator it = vPlaylist.begin(); it != vPlaylist.end(); it++) diff --git a/src/lyrics.cpp b/src/lyrics.cpp index 5b503fe3..18a0a76f 100644 --- a/src/lyrics.cpp +++ b/src/lyrics.cpp @@ -68,7 +68,7 @@ string GetLyrics(string artist, string song) curl_easy_setopt(lyrics, CURLOPT_URL, url.c_str()); curl_easy_setopt(lyrics, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(lyrics, CURLOPT_WRITEDATA, &result); - curl_easy_setopt(lyrics, CURLOPT_CONNECTTIMEOUT, 30); + curl_easy_setopt(lyrics, CURLOPT_CONNECTTIMEOUT, 10); code = curl_easy_perform(lyrics); curl_easy_cleanup(lyrics); diff --git a/src/menu.cpp b/src/menu.cpp index dc1fe6a3..9bb5000d 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -254,10 +254,7 @@ void Menu::Display(bool redraw_whole_window) void Menu::Refresh(bool redraw_whole_window) { if (!itsOptions.empty() && is_static()) - if (itsHighlight == 0) - Go(DOWN); - else - Go(UP); + itsHighlight == 0 ? Go(DOWN) : Go(UP); int MaxBeginning = itsOptions.size() < itsHeight ? 0 : itsOptions.size()-itsHeight; if (itsBeginning > MaxBeginning) @@ -269,32 +266,6 @@ void Menu::Refresh(bool redraw_whole_window) if (redraw_whole_window) redraw_screen(); - int line = 0; - /*int last; - - if (itsOptions.size() < itsHeight) - last = itsOptions.size(); - else - last = itsBeginning+itsHeight; - - int check = 0; - bool next = 1; - for (int i = last-1; i > itsBeginning && next; i--) - { - next = 0; - try - { - itsOptions.at(i); - } - catch (std::out_of_range) - { - check++; - next = 1; - } - } - itsBeginning -= check; - last -= check;*/ - for (vector::const_iterator it = NeedsRedraw.begin(); it != NeedsRedraw.end(); it++) { try @@ -306,7 +277,7 @@ void Menu::Refresh(bool redraw_whole_window) break; } - line = *it-itsBeginning; + int line = *it-itsBeginning; if (*it == itsHighlight && itsHighlightEnabled) { diff --git a/src/mpdpp.cpp b/src/mpdpp.cpp index 25fb85b2..7a8403ac 100644 --- a/src/mpdpp.cpp +++ b/src/mpdpp.cpp @@ -22,7 +22,7 @@ const string playlist_max_message = "playlist is at the max size"; -MPDConnection::MPDConnection() : isConnected(0), MPD_HOST("localhost"), MPD_PORT(6600), MPD_TIMEOUT(30), itsUpdater(0), itsErrorHandler(0), itsQueueIndex(0), itsMaxPlaylistLength(-1) +MPDConnection::MPDConnection() : isConnected(0), MPD_HOST("localhost"), MPD_PORT(6600), MPD_TIMEOUT(30), itsUpdater(0), itsErrorHandler(0), itsMaxPlaylistLength(-1) { itsConnection = 0; itsCurrentStats = 0; @@ -43,16 +43,23 @@ MPDConnection::~MPDConnection() mpd_freeStatus(itsOldStatus); if (itsCurrentStatus) mpd_freeStatus(itsCurrentStatus); + ClearQueue(); } -void MPDConnection::Connect() +bool MPDConnection::Connect() { if (!isConnected && !itsConnection) { itsConnection = mpd_newConnection(MPD_HOST.c_str(), MPD_PORT, MPD_TIMEOUT); isConnected = 1; + if (!CheckForErrors()) + return isConnected; + SendPassword(); CheckForErrors(); + return isConnected; } + else + return true; } bool MPDConnection::Connected() @@ -79,8 +86,7 @@ void MPDConnection::Disconnect() itsOldStatus = 0; isConnected = 0; itsMaxPlaylistLength = -1; - itsQueueIndex = 0; - itsQueue.clear(); + ClearQueue(); } void MPDConnection::SendPassword() @@ -245,6 +251,15 @@ void MPDConnection::Seek(int where) const } } +void MPDConnection::Shuffle() const +{ + if (isConnected) + { + mpd_sendShuffleCommand(itsConnection); + mpd_finishCommand(itsConnection); + } +} + void MPDConnection::ClearPlaylist() const { if (isConnected) @@ -390,83 +405,78 @@ int MPDConnection::AddSong(const string &path) int MPDConnection::AddSong(const Song &s) { - int id = -1; - if (isConnected) - { - if (GetPlaylistLength() < itsMaxPlaylistLength) - { - id = mpd_sendAddIdCommand(itsConnection, s.GetFile().c_str()); - mpd_finishCommand(itsConnection); - UpdateStatus(); - } - else - if (itsErrorHandler) - itsErrorHandler(this, MPD_ACK_ERROR_PLAYLIST_MAX, playlist_max_message, NULL); - } - return id; + return !s.Empty() ? AddSong(s.GetFile()) : -1; } void MPDConnection::QueueAddSong(const string &path) { if (isConnected && GetPlaylistLength() < itsMaxPlaylistLength) { - itsQueue[itsQueueIndex].type = qctAdd; - itsQueue[itsQueueIndex++].path = path; + QueueCommand *q = new QueueCommand; + q->type = qctAdd; + q->path = path; + itsQueue.push_back(q); } } void MPDConnection::QueueAddSong(const Song &s) { - itsQueue[itsQueueIndex].type = qctAdd; - itsQueue[itsQueueIndex++].path = s.GetFile(); + if (!s.Empty()) + QueueAddSong(s.GetFile()); } void MPDConnection::QueueDeleteSong(int id) { - itsQueue[itsQueueIndex].type = qctDelete; - itsQueue[itsQueueIndex++].id = id; + if (isConnected) + { + QueueCommand *q = new QueueCommand; + q->type = qctDelete; + q->id = id; + itsQueue.push_back(q); + } } void MPDConnection::QueueDeleteSongId(int id) { - itsQueue[itsQueueIndex].type = qctDeleteID; - itsQueue[itsQueueIndex++].id = id; + if (isConnected) + { + QueueCommand *q = new QueueCommand; + q->type = qctDeleteID; + q->id = id; + itsQueue.push_back(q); + } } -void MPDConnection::CommitQueue() +bool MPDConnection::CommitQueue() { + bool retval = false; if (isConnected) { - int playlist_length = GetPlaylistLength(); mpd_sendCommandListBegin(itsConnection); - for (std::map::const_iterator it = itsQueue.begin(); it != itsQueue.end(); it++) + for (std::vector::const_iterator it = itsQueue.begin(); it != itsQueue.end(); it++) { - switch (it->second.type) + switch ((*it)->type) { case qctAdd: - if (playlist_length < itsMaxPlaylistLength) - { - mpd_sendAddCommand(itsConnection, it->second.path.c_str()); - playlist_length++; - } - else - if (itsErrorHandler) - itsErrorHandler(this, MPD_ACK_ERROR_PLAYLIST_MAX, playlist_max_message, NULL); + mpd_sendAddCommand(itsConnection, (*it)->path.c_str()); break; case qctDelete: - mpd_sendDeleteCommand(itsConnection, it->second.id); + mpd_sendDeleteCommand(itsConnection, (*it)->id); break; case qctDeleteID: - mpd_sendDeleteIdCommand(itsConnection, it->second.id); + mpd_sendDeleteIdCommand(itsConnection, (*it)->id); break; } } mpd_sendCommandListEnd(itsConnection); mpd_finishCommand(itsConnection); UpdateStatus(); + if (GetPlaylistLength() == itsMaxPlaylistLength && itsErrorHandler) + itsErrorHandler(this, MPD_ACK_ERROR_PLAYLIST_MAX, playlist_max_message, NULL); + retval = !itsQueue.empty(); } - itsQueueIndex = 0; - itsQueue.clear(); + ClearQueue(); + return retval; } void MPDConnection::DeletePlaylist(const string &name) @@ -487,7 +497,7 @@ bool MPDConnection::SavePlaylist(const string &name) return !(itsConnection->error == MPD_ERROR_ACK && itsConnection->errorCode == MPD_ACK_ERROR_EXIST); } else - return 0; + return false; } void MPDConnection::GetArtists(TagList &v) const @@ -609,6 +619,7 @@ int MPDConnection::CheckForErrors() if (itsConnection->error == MPD_ERROR_ACK) { // this is to avoid setting too small max size as we check it before fetching current status + // setting real max playlist length is in UpdateStatus() if (itsConnection->errorCode == MPD_ACK_ERROR_PLAYLIST_MAX && itsMaxPlaylistLength == -1) itsMaxPlaylistLength = 0; @@ -629,6 +640,13 @@ int MPDConnection::CheckForErrors() return errid; } +void MPDConnection::ClearQueue() +{ + for (std::vector::iterator it = itsQueue.begin(); it != itsQueue.end(); it++) + delete *it; + itsQueue.clear(); +} + void FreeSongList(SongList &l) { for (SongList::iterator i = l.begin(); i != l.end(); i++) diff --git a/src/mpdpp.h b/src/mpdpp.h index 9d3823f9..a88cd0b9 100644 --- a/src/mpdpp.h +++ b/src/mpdpp.h @@ -21,8 +21,6 @@ #ifndef HAVE_MPDPP_H #define HAVE_MPDPP_H -#include - #include "ncmpcpp.h" #include "song.h" @@ -77,7 +75,7 @@ class MPDConnection MPDConnection(); ~MPDConnection(); - void Connect(); + bool Connect(); bool Connected(); void Disconnect(); @@ -101,6 +99,7 @@ class MPDConnection void Prev() const; void Move(int, int) const; void Seek(int) const; + void Shuffle() const; void ClearPlaylist() const; PlayerState GetState() const { return isConnected && itsCurrentStatus ? (PlayerState)itsCurrentStatus->state : psUnknown; } @@ -135,7 +134,7 @@ class MPDConnection void QueueAddSong(const Song &); void QueueDeleteSong(int); void QueueDeleteSongId(int); - void CommitQueue(); + bool CommitQueue(); void DeletePlaylist(const string &); bool SavePlaylist(const string &); @@ -151,6 +150,7 @@ class MPDConnection private: int CheckForErrors(); + void ClearQueue(); string itsLastErrorMessage; mpd_Connection *itsConnection; @@ -171,8 +171,7 @@ class MPDConnection ErrorHandler itsErrorHandler; void *itsErrorHandlerUserdata; - std::map itsQueue; - int itsQueueIndex; + std::vector itsQueue; unsigned int itsMaxPlaylistLength; }; diff --git a/src/ncmpcpp.cpp b/src/ncmpcpp.cpp index 2209d5d3..0fa021a3 100644 --- a/src/ncmpcpp.cpp +++ b/src/ncmpcpp.cpp @@ -140,6 +140,8 @@ extern string UNKNOWN_ALBUM; extern string playlist_stats; +const string message_part_of_songs_added = "Only part of requested songs' list added to playlist!"; + int main(int argc, char *argv[]) { DefaultConfiguration(Config); @@ -155,16 +157,14 @@ int main(int argc, char *argv[]) if (getenv("MPD_PASSWORD")) Mpd->SetPassword(getenv("MPD_PASSWORD")); - Mpd->Connect(); + Mpd->SetTimeout(Config.mpd_connection_timeout); - if (!Mpd->Connected()) + if (!Mpd->Connect()) { printf("Cannot connect to mpd (%s)\n", Mpd->GetLastErrorMessage().c_str()); return -1; } - Mpd->SendPassword(); - setlocale(LC_ALL,""); initscr(); noecho(); @@ -314,8 +314,7 @@ int main(int argc, char *argv[]) { ShowMessage("Attempting to reconnect..."); Mpd->Disconnect(); - Mpd->Connect(); - if (Mpd->Connected()) + if (Mpd->Connect()) ShowMessage("Connected!"); messages_allowed = 0; } @@ -636,7 +635,7 @@ int main(int argc, char *argv[]) } case itSong: { - Song s = Mpd->GetSong(vBrowser[ci].name); + Song &s = *vBrowser[ci].song; int id = Mpd->AddSong(s); if (id >= 0) { @@ -649,21 +648,17 @@ int main(int argc, char *argv[]) case itPlaylist: { SongList list; - ShowMessage("Loading and playing playlist " + vBrowser[ci].name + "..."); Mpd->GetPlaylistContent(vBrowser[ci].name, list); - for(SongList::const_iterator it = list.begin(); it != list.end(); it++) + for (SongList::const_iterator it = list.begin(); it != list.end(); it++) Mpd->QueueAddSong(**it); - Mpd->CommitQueue(); - - try + if (Mpd->CommitQueue()) { - Song *s = vPlaylist.at(vPlaylist.size()-list.size()); - if (s->GetHash() == list.at(0)->GetHash()) + ShowMessage("Loading and playing playlist " + vBrowser[ci].name + "..."); + Song *s = vPlaylist[vPlaylist.size()-list.size()]; + if (s->GetHash() == list[0]->GetHash()) Mpd->PlayID(s->GetID()); - } - catch (std::out_of_range) - { - ShowMessage("Couldn't play playlist!"); + else + ShowMessage(message_part_of_songs_added); } FreeSongList(list); break; @@ -956,8 +951,7 @@ int main(int argc, char *argv[]) } default: { - int ci = mSearcher->GetRealChoice()-1; - Song s = Mpd->GetSong(vSearched[ci-1]->GetFile()); + Song &s = *vSearched[mSearcher->GetRealChoice()-2]; int id = Mpd->AddSong(s); if (id >= 0) { @@ -979,47 +973,40 @@ int main(int argc, char *argv[]) if (wCurrent == mLibArtists) { const string &artist = mLibArtists->GetCurrentOption(); - ShowMessage("Adding all songs artist's: " + artist); Mpd->StartSearch(1); Mpd->AddSearch(MPD_TAG_ITEM_ARTIST, artist); Mpd->CommitSearch(list); for (SongList::const_iterator it = list.begin(); it != list.end(); it++) Mpd->QueueAddSong(**it); - Mpd->CommitQueue(); - - if (input == ENTER) + if (Mpd->CommitQueue()) { - try + ShowMessage("Adding all songs artist's: " + artist); + Song *s = vPlaylist[vPlaylist.size()-list.size()]; + if (s->GetHash() == list[0]->GetHash()) { - Song *s = vPlaylist.at(vPlaylist.size()-list.size()); - if (s->GetHash() == list.at(0)->GetHash()) + if (input == ENTER) Mpd->PlayID(s->GetID()); } - catch (std::out_of_range) - { - ShowMessage("Couldn't play!"); - } + else + ShowMessage(message_part_of_songs_added); } } if (wCurrent == mLibAlbums) { - ShowMessage("Adding songs from album: " + mLibAlbums->GetCurrentOption()); for (SongList::const_iterator it = vSongs.begin(); it != vSongs.end(); it++) Mpd->QueueAddSong(**it); - Mpd->CommitQueue(); - if (input == ENTER) + if (Mpd->CommitQueue()) { - try + ShowMessage("Adding songs from album: " + mLibAlbums->GetCurrentOption()); + Song *s = vPlaylist[vPlaylist.size()-vSongs.size()]; + if (s->GetHash() == vSongs[0]->GetHash()) { - Song *s = vPlaylist.at(vPlaylist.size()-vSongs.size()); - if (s->GetHash() == vSongs.at(0)->GetHash()) + if (input == ENTER) Mpd->PlayID(s->GetID()); } - catch (std::out_of_range) - { - ShowMessage("Couldn't play!"); - } + else + ShowMessage(message_part_of_songs_added); } } @@ -1066,29 +1053,37 @@ int main(int argc, char *argv[]) for (SongList::const_iterator it = list.begin(); it != list.end(); it++) Mpd->QueueAddSong(**it); - + if (Mpd->CommitQueue()) + { + ShowMessage("Added folder: " + getdir); + Song *s = vPlaylist[vPlaylist.size()-list.size()]; + if (s->GetHash() != list[0]->GetHash()) + ShowMessage(message_part_of_songs_added); + } FreeSongList(list); - ShowMessage("Added folder: " + getdir); - Mpd->CommitQueue(); break; } case itSong: { - Song s = Mpd->GetSong(vBrowser[ci].name); - int id = Mpd->AddSong(s); - if (id >= 0) + Song &s = *vBrowser[ci].song; + if (Mpd->AddSong(s) != -1) ShowMessage("Added to playlist: " + OmitBBCodes(DisplaySong(s))); break; } case itPlaylist: { - ShowMessage("Loading playlist " + vBrowser[ci].name + "..."); SongList list; Mpd->GetPlaylistContent(vBrowser[ci].name, list); for (SongList::const_iterator it = list.begin(); it != list.end(); it++) Mpd->QueueAddSong(**it); + if (Mpd->CommitQueue()) + { + ShowMessage("Loading playlist " + vBrowser[ci].name + "..."); + Song *s = vPlaylist[vPlaylist.size()-list.size()]; + if (s->GetHash() != list[0]->GetHash()) + ShowMessage(message_part_of_songs_added); + } FreeSongList(list); - Mpd->CommitQueue(); break; } } @@ -1101,8 +1096,8 @@ int main(int argc, char *argv[]) break; Song &s = *vSearched[id]; - Mpd->AddSong(s); - ShowMessage("Added to playlist: " + OmitBBCodes(DisplaySong(s))); + if (Mpd->AddSong(s) != -1) + ShowMessage("Added to playlist: " + OmitBBCodes(DisplaySong(s))); mSearcher->Go(DOWN); } if (current_screen == csLibrary) @@ -1162,7 +1157,7 @@ int main(int argc, char *argv[]) break; } } - case '-': //volume down + case '-': // volume down { Mpd->SetVolume(Mpd->GetVolume()-1); break; @@ -1230,12 +1225,12 @@ int main(int argc, char *argv[]) } break; } - case '<': + case '<': // previous { Mpd->Prev(); break; } - case '>': + case '>': // next { Mpd->Next(); break; @@ -1301,7 +1296,7 @@ int main(int argc, char *argv[]) } break; } - case 'n': //move song down + case 'n': // move song down { block_playlist_update = 1; int pos = mPlaylist->GetChoice()-1; @@ -1398,12 +1393,12 @@ int main(int argc, char *argv[]) Mpd->SetRepeat(!Mpd->GetRepeat()); break; } - case 'Z': + case 'Z': // shuffle playlist { - //mpd_playlist_shuffle(conn); + Mpd->Shuffle(); break; } - case 'z': //switch random state + case 'z': // switch random state { Mpd->SetRandom(!Mpd->GetRandom()); break; @@ -1609,26 +1604,26 @@ int main(int argc, char *argv[]) || (wCurrent == mSearcher && !vSearched.empty() && mSearcher->GetChoice() > search_engine_static_option) || (wCurrent == mLibSongs)) { - Song s; + Song *s; switch (current_screen) { case csPlaylist: - s = *vPlaylist[mPlaylist->GetChoice()-1]; + s = vPlaylist[mPlaylist->GetChoice()-1]; break; case csBrowser: - s = Mpd->GetSong(vBrowser[mBrowser->GetChoice()-1].name.c_str()); + s = vBrowser[mBrowser->GetChoice()-1].song; break; case csSearcher: - s = *vSearched[mSearcher->GetChoice()-search_engine_static_option-1]; + s = vSearched[mSearcher->GetChoice()-search_engine_static_option-1]; break; case csLibrary: - s = *vSongs[mLibSongs->GetChoice()-1]; + s = vSongs[mLibSongs->GetChoice()-1]; break; default: break; } - if (s.GetArtist() != UNKNOWN_ARTIST && s.GetTitle() != UNKNOWN_TITLE) + if (s->GetArtist() != UNKNOWN_ARTIST && s->GetTitle() != UNKNOWN_TITLE) { wPrev = wCurrent; prev_screen = current_screen; @@ -1639,8 +1634,8 @@ int main(int argc, char *argv[]) sLyrics->WriteXY(0, 0, "Fetching lyrics..."); sLyrics->Refresh(); - sLyrics->Add("[b]" + s.GetArtist() + " - " + s.GetTitle() + "[/b]\n\n"); - sLyrics->Add(GetLyrics(s.GetArtist(), s.GetTitle())); + sLyrics->Add("[b]" + s->GetArtist() + " - " + s->GetTitle() + "[/b]\n\n"); + sLyrics->Add(GetLyrics(s->GetArtist(), s->GetTitle())); sLyrics->Timeout(ncmpcpp_window_timeout); } } @@ -1656,7 +1651,7 @@ int main(int argc, char *argv[]) } break; } - case KEY_TAB: //switch between playlist and browser + case KEY_TAB: // switch between playlist and browser { if (wCurrent == mPlaylist) goto KEY_TAB_BROWSER_REDIRECT; @@ -1779,12 +1774,11 @@ int main(int argc, char *argv[]) } break; } - case 'q': case 'Q': + case 'q': case 'Q': // quit main_exit = 1; default: continue; } } - Mpd->Disconnect(); curs_set(1); endwin(); diff --git a/src/settings.cpp b/src/settings.cpp index 524b4654..9c836f48 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -43,7 +43,7 @@ void DefaultConfiguration(ncmpcpp_config &conf) conf.header_visibility = true; conf.statusbar_visibility = true; conf.set_window_title = true; - conf.mpd_connection_timeout = 60; + conf.mpd_connection_timeout = 15; conf.crossfade_time = 5; conf.playlist_disable_highlight_delay = 5; conf.message_delay_time = 4; diff --git a/src/status_checker.cpp b/src/status_checker.cpp index f57f0aa5..c4313772 100644 --- a/src/status_checker.cpp +++ b/src/status_checker.cpp @@ -22,8 +22,6 @@ #include "helpers.h" #include "settings.h" -#define FOR_EACH_MPD_DATA(x) for (; (x); (x) = mpd_data_get_next(x)) - extern MPDConnection *Mpd; extern ncmpcpp_config Config; @@ -447,12 +445,11 @@ void NcmpcppStatusChanged(MPDConnection *Mpd, MPDStatusChanges changed, void *da wHeader->WriteXY(wHeader->GetWidth()-volume_state.length(), 0, volume_state); wHeader->SetColor(Config.header_color); } + wCurrent->Refresh(); wFooter->Bold(0); wFooter->GotoXY(sx, sy); wFooter->Refresh(); wFooter->AutoRefresh(1); wFooter->EnableBB(); - if (changed.SongID || changed.PlayerState) - wCurrent->Refresh(); }