--- a/src/mpdcli.cxx
+++ b/src/mpdcli.cxx
@@ -33,7 +33,8 @@
MPDCli::MPDCli(const string& host, int port, const string& pass)
: m_conn(0), m_ok(false), m_premutevolume(0), m_cachedvolume(50),
- m_host(host), m_port(port), m_password(pass)
+ m_host(host), m_port(port), m_password(pass), m_lastinsertid(-1),
+ m_lastinsertpos(-1), m_lastinsertqvers(-1)
{
regcomp(&m_tpuexpr, "^[[:alpha:]]+://.+", REG_EXTENDED|REG_NOSUB);
if (!openconn()) {
@@ -502,46 +503,50 @@
if (!ok())
return -1;
- if (!updStatus())
- return -1;
-
- int id;
- RETRY_CMD((id=mpd_run_add_id_to(M_CONN, uri.c_str(), (unsigned)pos))!=-1);
+ RETRY_CMD((m_lastinsertid =
+ mpd_run_add_id_to(M_CONN, uri.c_str(), (unsigned)pos)) != -1);
if (m_have_addtagid)
- send_tag_data(id, meta);
-
- return id;
+ send_tag_data(m_lastinsertid, meta);
+
+ m_lastinsertpos = pos;
+ updStatus();
+ m_lastinsertqvers = m_stat.qvers;
+ return m_lastinsertid;
}
int MPDCli::insertAfterId(const string& uri, int id, const UpSong& meta)
{
LOGDEB("MPDCli::insertAfterId: id " << id << " uri " << uri << endl);
if (!ok())
- return -1;
-
- if (!updStatus())
return -1;
// id == 0 means insert at start
if (id == 0) {
return insert(uri, 0, meta);
}
-
- vector<mpd_song*> songs;
- if (!getQueueSongs(songs)) {
- return false;
- }
- int newid = -1;
- for (unsigned int pos = 0; pos < songs.size(); pos++) {
- unsigned int qid = mpd_song_get_id(songs[pos]);
- if (qid == (unsigned int)id || pos == songs.size() -1) {
- newid = insert(uri, pos+1, meta);
- break;
+ updStatus();
+
+ int newpos = 0;
+ if (m_lastinsertid == id && m_lastinsertpos >= 0 &&
+ m_lastinsertqvers == m_stat.qvers) {
+ newpos = m_lastinsertpos + 1;
+ } else {
+ // Translate input id to insert position
+ vector<mpd_song*> songs;
+ if (!getQueueSongs(songs)) {
+ return false;
}
- }
- freeSongs(songs);
- return newid;
+ for (unsigned int pos = 0; pos < songs.size(); pos++) {
+ unsigned int qid = mpd_song_get_id(songs[pos]);
+ if (qid == (unsigned int)id || pos == songs.size() -1) {
+ newpos = pos + 1;
+ break;
+ }
+ }
+ freeSongs(songs);
+ }
+ return insert(uri, newpos, meta);
}
bool MPDCli::clearQueue()
@@ -603,11 +608,12 @@
while ((song = mpd_recv_song(M_CONN)) != NULL) {
songs.push_back(song);
}
-
+
if (!mpd_response_finish(M_CONN)) {
LOGERR("MPDCli::getQueueSongs: mpd_list_queue_meta failed"<< endl);
return false;
}
+ LOGDEB("MPDCli::getQueueSongs: " << songs.size() << " songs " << endl);
return true;
}