various clean-ups and improvements

This commit is contained in:
unknown
2008-08-18 17:16:00 +02:00
parent 3bfd8125ec
commit 34dfffaab0
8 changed files with 137 additions and 159 deletions

View File

@@ -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++)

View File

@@ -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);

View File

@@ -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<int>::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)
{

View File

@@ -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;
}
void MPDConnection::CommitQueue()
{
if (isConnected)
{
int playlist_length = GetPlaylistLength();
QueueCommand *q = new QueueCommand;
q->type = qctDeleteID;
q->id = id;
itsQueue.push_back(q);
}
}
bool MPDConnection::CommitQueue()
{
bool retval = false;
if (isConnected)
{
mpd_sendCommandListBegin(itsConnection);
for (std::map<int, QueueCommand>::const_iterator it = itsQueue.begin(); it != itsQueue.end(); it++)
for (std::vector<QueueCommand *>::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<QueueCommand *>::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++)

View File

@@ -21,8 +21,6 @@
#ifndef HAVE_MPDPP_H
#define HAVE_MPDPP_H
#include <map>
#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<int, QueueCommand> itsQueue;
int itsQueueIndex;
std::vector<QueueCommand *> itsQueue;
unsigned int itsMaxPlaylistLength;
};

View File

@@ -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();

View File

@@ -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;

View File

@@ -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();
}