Code

screen_artist: use idle events instead of playlist/browse callback
[ncmpc.git] / src / screen_artist.c
index 01fbc9c1fbc35184f6d0ff5200446ff19bbfb711..c6173d0b3cd5c241ed5c1fa9ee54499000cdea54 100644 (file)
@@ -1,20 +1,21 @@
-/*
- * (c) 2005 by Kalle Wallin <kaw@linux.se>
- *
+/* ncmpc (Ncurses MPD Client)
+ * (c) 2004-2009 The Music Player Daemon Project
+ * Project homepage: http://musicpd.org
+
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- *
+
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- *
- */
+
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
 
 #include "i18n.h"
 #include "options.h"
@@ -26,7 +27,7 @@
 #include "screen.h"
 #include "screen_utils.h"
 #include "screen_browser.h"
-#include "gcc.h"
+#include "filelist.h"
 
 #include <ctype.h>
 #include <stdlib.h>
@@ -60,7 +61,8 @@ compare_utf8(gconstpointer s1, gconstpointer s2)
 
 /* list_window callback */
 static const char *
-artist_lw_callback(unsigned idx, mpd_unused int *highlight, mpd_unused void *data)
+artist_lw_callback(unsigned idx, G_GNUC_UNUSED bool *highlight,
+                  G_GNUC_UNUSED char** sc, G_GNUC_UNUSED void *data)
 {
        GPtrArray *list = data;
        static char buf[BUFSIZE];
@@ -102,22 +104,6 @@ artist_repaint(void)
        wrefresh(browser.lw->w);
 }
 
-static void
-artist_repaint_if_active(void)
-{
-       if (screen_is_visible(&screen_artist))
-               artist_repaint();
-}
-
-/* the playlist have 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();
-}
-
 static GPtrArray *
 g_list_to_ptr_array(GList *in)
 {
@@ -140,14 +126,14 @@ 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);
 }
 
 static void
-free_lists(struct mpdclient *c)
+free_lists(G_GNUC_UNUSED struct mpdclient *c)
 {
        if (artist_list != NULL) {
                string_array_free(artist_list);
@@ -160,8 +146,6 @@ free_lists(struct mpdclient *c)
        }
 
        if (browser.filelist) {
-               if (c != NULL)
-                       mpdclient_remove_playlist_callback(c, playlist_changed_callback);
                filelist_free(browser.filelist);
                browser.filelist = NULL;
        }
@@ -215,22 +199,23 @@ 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(NULL);
+               browser.filelist = filelist_new();
 
        /* add a dummy entry for ".." */
        filelist_prepend(browser.filelist, NULL);
 
-       /* install playlist callback and fix highlights */
+#ifndef NCMPC_MINI
+       /* fix highlights */
        sync_highlights(c, browser.filelist);
-       mpdclient_install_playlist_callback(c, playlist_changed_callback);
+#endif
 }
 
 static void
@@ -299,21 +284,6 @@ reload_lists(struct mpdclient *c)
        }
 }
 
-/* db updated */
-static void
-browse_callback(mpdclient_t *c, int event, mpd_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)
 {
@@ -330,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
@@ -400,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);
@@ -462,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;
@@ -605,22 +592,33 @@ artist_cmd(mpdclient_t *c, command_t cmd)
        case CMD_ADD:
                switch(mode) {
                case LIST_ARTISTS:
-                       selected = g_ptr_array_index(artist_list,
-                                                    browser.lw->selected);
-                       add_query(c, MPD_TABLE_ARTIST, selected);
-                       cmd = CMD_LIST_NEXT; /* continue and select next item... */
+               {
+                       unsigned i;
+                       for(i = browser.lw->selected_start; i <= browser.lw->selected_end; ++i)
+                       {
+                               selected = g_ptr_array_index(artist_list, i);
+                               add_query(c, MPD_TAG_ARTIST, selected);
+                               cmd = CMD_LIST_NEXT; /* continue and select next item... */
+                       }
                        break;
-
+               }
                case LIST_ALBUMS:
-                       if (browser.lw->selected == album_list->len + 1)
-                               add_query(c, MPD_TABLE_ARTIST, artist);
-                       else if (browser.lw->selected > 0) {
-                               selected = g_ptr_array_index(album_list,
-                                                            browser.lw->selected - 1);
-                               add_query(c, MPD_TABLE_ALBUM, selected);
-                               cmd = CMD_LIST_NEXT; /* continue and select next item... */
+               {
+                       unsigned i;
+                       for(i = browser.lw->selected_start; i <= browser.lw->selected_end; ++i)
+                       {
+                               if(i == album_list->len + 1)
+                                       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_TAG_ALBUM, selected);
+                                       cmd = CMD_LIST_NEXT; /* continue and select next item... */
+                               }
                        }
                        break;
+               }
 
                case LIST_SONGS:
                        /* handled by browser_cmd() */
@@ -631,7 +629,6 @@ artist_cmd(mpdclient_t *c, command_t cmd)
                /* continue and update... */
        case CMD_SCREEN_UPDATE:
                reload_lists(c);
-               screen_status_printf(_("Screen updated!"));
                return false;
 
        case CMD_LIST_FIND:
@@ -655,6 +652,22 @@ artist_cmd(mpdclient_t *c, command_t cmd)
                        /* handled by browser_cmd() */
                        break;
                }
+       case CMD_LIST_JUMP:
+               switch (mode) {
+               case LIST_ARTISTS:
+                       screen_jump(browser.lw, artist_lw_callback, artist_list);
+                       artist_repaint();
+                       return true;
+
+               case LIST_ALBUMS:
+                       screen_jump(browser.lw, artist_lw_callback, album_list);
+                       artist_repaint();
+                       return true;
+
+               case LIST_SONGS:
+                       /* handled by browser_cmd() */
+                       break;
+               }
 
                break;
 
@@ -677,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,
 };