Code

screen_browser: optimize "add+play song" with addid/playid
authorMax Kellermann <max@duempel.org>
Tue, 29 Sep 2009 20:22:54 +0000 (22:22 +0200)
committerMax Kellermann <max@duempel.org>
Tue, 29 Sep 2009 20:22:54 +0000 (22:22 +0200)
Use the "addid"/"playid" commands in enqueue_and_play().  This
requires MPD 0.12.

NEWS
src/playlist.c
src/playlist.h
src/screen_browser.c

diff --git a/NEWS b/NEWS
index 3c4d0fc5c07b29ea2fe7b0b6a9101e40b98b34d1..571f7bf9436f8f1b1397f0c3c199e24aa4d40e2a 100644 (file)
--- 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
index 5fced75f0c7c1041eb7d7280fb5a1e8f34676c62..a6e4005672dc63bb12944bba4be977551eb87689 100644 (file)
@@ -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;
+}
index cee91002f840f288d92602888958896fa7b549de..67319d24b346f46422a1dfb38d09f13f4492c6bf 100644 (file)
@@ -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
index 3b80bdbccdb9343224bad7a6ab2e027cbe15bb4b..a2204f9e92aa5da4afbe73eecd652614c68370c0 100644 (file)
@@ -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;
 }