Code

screen_artist: use idle events instead of playlist/browse callback
[ncmpc.git] / src / screen_artist.c
index 1dc300a9d24394612972dd74c3abba32052ba497..c6173d0b3cd5c241ed5c1fa9ee54499000cdea54 100644 (file)
@@ -27,6 +27,7 @@
 #include "screen.h"
 #include "screen_utils.h"
 #include "screen_browser.h"
+#include "filelist.h"
 
 #include <ctype.h>
 #include <stdlib.h>
@@ -103,24 +104,6 @@ artist_repaint(void)
        wrefresh(browser.lw->w);
 }
 
-static void
-artist_repaint_if_active(void)
-{
-       if (screen_is_visible(&screen_artist))
-               artist_repaint();
-}
-
-#ifndef NCMPC_MINI
-/* the playlist has been updated -> fix highlights */
-static void
-playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
-{
-       browser_playlist_changed(&browser, c, event, data);
-
-       artist_repaint_if_active();
-}
-#endif
-
 static GPtrArray *
 g_list_to_ptr_array(GList *in)
 {
@@ -143,7 +126,7 @@ string_array_free(GPtrArray *array)
 
        for (i = 0; i < array->len; ++i) {
                char *value = g_ptr_array_index(array, i);
-               free(value);
+               g_free(value);
        }
 
        g_ptr_array_free(array, TRUE);
@@ -163,10 +146,6 @@ free_lists(G_GNUC_UNUSED struct mpdclient *c)
        }
 
        if (browser.filelist) {
-#ifndef NCMPC_MINI
-               if (c != NULL)
-                       mpdclient_remove_playlist_callback(c, playlist_changed_callback);
-#endif
                filelist_free(browser.filelist);
                browser.filelist = NULL;
        }
@@ -220,12 +199,12 @@ load_song_list(struct mpdclient *c)
        if (album[0] == 0)
                browser.filelist =
                        mpdclient_filelist_search(c, TRUE,
-                                                 MPD_TABLE_ARTIST,
+                                                 MPD_TAG_ARTIST,
                                                  artist);
        else
                browser.filelist =
                        mpdclient_filelist_search(c, TRUE,
-                                                 MPD_TABLE_ALBUM,
+                                                 MPD_TAG_ALBUM,
                                                  album);
        if (browser.filelist == NULL)
                browser.filelist = filelist_new();
@@ -234,9 +213,8 @@ load_song_list(struct mpdclient *c)
        filelist_prepend(browser.filelist, NULL);
 
 #ifndef NCMPC_MINI
-       /* install playlist callback and fix highlights */
+       /* fix highlights */
        sync_highlights(c, browser.filelist);
-       mpdclient_install_playlist_callback(c, playlist_changed_callback);
 #endif
 }
 
@@ -306,21 +284,6 @@ reload_lists(struct mpdclient *c)
        }
 }
 
-/* db updated */
-static void
-browse_callback(mpdclient_t *c, int event, G_GNUC_UNUSED gpointer data)
-{
-       switch(event) {
-       case BROWSE_DB_UPDATED:
-               reload_lists(c);
-               break;
-       default:
-               break;
-       }
-
-       artist_repaint_if_active();
-}
-
 static void
 init(WINDOW *w, int cols, int rows)
 {
@@ -337,17 +300,11 @@ quit(void)
 }
 
 static void
-open(mpdclient_t *c)
+open(struct mpdclient *c)
 {
-       static gboolean callback_installed = FALSE;
-
        if (artist_list == NULL && album_list == NULL &&
            browser.filelist == NULL)
                reload_lists(c);
-       if (!callback_installed) {
-               mpdclient_install_browse_callback(c, browse_callback);
-               callback_installed = TRUE;
-       }
 }
 
 static void
@@ -407,15 +364,38 @@ get_title(char *str, size_t size)
 }
 
 static void
-add_query(mpdclient_t *c, int table, char *_filter)
+screen_artist_update(struct mpdclient *c)
+{
+       if (browser.filelist == NULL)
+               return;
+
+       if (c->events & MPD_IDLE_DATABASE)
+               /* the db has changed -> update the list */
+               reload_lists(c);
+
+#ifndef NCMPC_MINI
+       if (c->events & (MPD_IDLE_DATABASE | MPD_IDLE_PLAYLIST))
+               sync_highlights(c, browser.filelist);
+#endif
+
+       if (c->events & (MPD_IDLE_DATABASE
+#ifndef NCMPC_MINI
+                        | MPD_IDLE_PLAYLIST
+#endif
+                        ))
+               artist_repaint();
+}
+
+static void
+add_query(struct mpdclient *c, enum mpd_tag_type table, char *_filter)
 {
        char *str;
-       mpdclient_filelist_t *addlist;
+       struct filelist *addlist;
 
        assert(filter != NULL);
 
        str = utf8_to_locale(_filter);
-       if (table== MPD_TABLE_ALBUM)
+       if (table == MPD_TAG_ALBUM)
                screen_status_printf("Adding album %s...", str);
        else
                screen_status_printf("Adding %s...", str);
@@ -469,7 +449,7 @@ string_array_find(GPtrArray *array, const char *value)
 }
 
 static bool
-artist_cmd(mpdclient_t *c, command_t cmd)
+artist_cmd(struct mpdclient *c, command_t cmd)
 {
        char *selected;
        char *old;
@@ -617,7 +597,7 @@ artist_cmd(mpdclient_t *c, command_t cmd)
                        for(i = browser.lw->selected_start; i <= browser.lw->selected_end; ++i)
                        {
                                selected = g_ptr_array_index(artist_list, i);
-                               add_query(c, MPD_TABLE_ARTIST, selected);
+                               add_query(c, MPD_TAG_ARTIST, selected);
                                cmd = CMD_LIST_NEXT; /* continue and select next item... */
                        }
                        break;
@@ -628,12 +608,12 @@ artist_cmd(mpdclient_t *c, command_t cmd)
                        for(i = browser.lw->selected_start; i <= browser.lw->selected_end; ++i)
                        {
                                if(i == album_list->len + 1)
-                                       add_query(c, MPD_TABLE_ARTIST, artist);
+                                       add_query(c, MPD_TAG_ARTIST, artist);
                                else if (i > 0)
                                {
                                        selected = g_ptr_array_index(album_list,
                                                                     browser.lw->selected - 1);
-                                       add_query(c, MPD_TABLE_ALBUM, selected);
+                                       add_query(c, MPD_TAG_ALBUM, selected);
                                        cmd = CMD_LIST_NEXT; /* continue and select next item... */
                                }
                        }
@@ -710,6 +690,7 @@ const struct screen_functions screen_artist = {
        .open = open,
        .resize = resize,
        .paint = paint,
+       .update = screen_artist_update,
        .cmd = artist_cmd,
        .get_title = get_title,
 };