From: Max Kellermann Date: Tue, 29 Sep 2009 20:22:54 +0000 (+0200) Subject: screen_browser: optimize "add+play song" with addid/playid X-Git-Tag: release-0.16~296 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=dad8644db50c1754f7d497af8c28bbf1e9fc813b;p=ncmpc.git screen_browser: optimize "add+play song" with addid/playid Use the "addid"/"playid" commands in enqueue_and_play(). This requires MPD 0.12. --- diff --git a/NEWS b/NEWS index 3c4d0fc..571f7bf 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,7 @@ ncmpc 0.16 - not yet released * require MPD 0.12 * allow multiple queued database updates * reactivate incremental playlist changes +* optimize "add+play song" with addid/playid ncmpc 0.15 - 2009-09-24 diff --git a/src/playlist.c b/src/playlist.c index 5fced75..a6e4005 100644 --- a/src/playlist.c +++ b/src/playlist.c @@ -119,3 +119,17 @@ playlist_get_index_from_file(const struct mpdclient_playlist *playlist, return -1; } + +gint +playlist_get_id_from_uri(const struct mpdclient_playlist *playlist, + const gchar *uri) +{ + for (guint i = 0; i < playlist_length(playlist); ++i) { + const struct mpd_song *song = playlist_get(playlist, i); + + if (strcmp(mpd_song_get_uri(song), uri) == 0) + return mpd_song_get_id(song); + } + + return -1; +} diff --git a/src/playlist.h b/src/playlist.h index cee9100..67319d2 100644 --- a/src/playlist.h +++ b/src/playlist.h @@ -144,4 +144,15 @@ playlist_get_index_from_same_song(const struct mpdclient_playlist *playlist, return playlist_get_index_from_file(playlist, mpd_song_get_uri(song)); } +gint +playlist_get_id_from_uri(const struct mpdclient_playlist *playlist, + const gchar *uri); + +static inline gint +playlist_get_id_from_same_song(const struct mpdclient_playlist *playlist, + const struct mpd_song *song) +{ + return playlist_get_id_from_uri(playlist, mpd_song_get_uri(song)); +} + #endif diff --git a/src/screen_browser.c b/src/screen_browser.c index 3b80bdb..a2204f9 100644 --- a/src/screen_browser.c +++ b/src/screen_browser.c @@ -187,29 +187,39 @@ load_playlist(struct mpdclient *c, const struct mpd_playlist *playlist) static bool enqueue_and_play(struct mpdclient *c, struct filelist_entry *entry) { - int idx; const struct mpd_song *song = mpd_entity_get_song(entry->entity); + int id; #ifndef NCMPC_MINI - if (!(entry->flags & HIGHLIGHT)) { + if (!(entry->flags & HIGHLIGHT)) + id = -1; + else #endif - if (mpdclient_cmd_add(c, song) == 0) { - char buf[BUFSIZE]; + id = playlist_get_id_from_same_song(&c->playlist, song); + + if (id < 0) { + char buf[BUFSIZE]; + + id = mpd_run_add_id(c->connection, mpd_song_get_uri(song)); + if (id < 0) { + mpdclient_handle_error(c); + return false; + } #ifndef NCMPC_MINI - entry->flags |= HIGHLIGHT; + entry->flags |= HIGHLIGHT; #endif - strfsong(buf, BUFSIZE, options.list_format, song); - screen_status_printf(_("Adding \'%s\' to playlist"), buf); - mpdclient_update(c); /* get song id */ - } else - return false; + strfsong(buf, BUFSIZE, options.list_format, song); + screen_status_printf(_("Adding \'%s\' to playlist"), buf); #ifndef NCMPC_MINI } #endif - idx = playlist_get_index_from_same_song(&c->playlist, song); - mpdclient_cmd_play(c, idx); + if (!mpd_run_play_id(c->connection, id)) { + mpdclient_handle_error(c); + return false; + } + return true; }