Code

screen_artist: don't convert _() to locale
[ncmpc.git] / src / screen_artist.c
index 5c0ff30994d3a025ca330b70658a0bb7e3ebcf89..31ff1c731ee23aa0062ee427381a531ecd681ab5 100644 (file)
@@ -46,11 +46,12 @@ static struct screen_browser browser;
 static gint
 compare_utf8(gconstpointer s1, gconstpointer s2)
 {
+       const char *const*t1 = s1, *const*t2 = s2;
        char *key1, *key2;
        int n;
 
-       key1 = g_utf8_collate_key(s1,-1);
-       key2 = g_utf8_collate_key(s2,-1);
+       key1 = g_utf8_collate_key(*t1,-1);
+       key2 = g_utf8_collate_key(*t2,-1);
        n = strcmp(key1,key2);
        g_free(key1);
        g_free(key2);
@@ -59,8 +60,7 @@ compare_utf8(gconstpointer s1, gconstpointer s2)
 
 /* list_window callback */
 static const char *
-screen_artist_lw_callback(unsigned idx, G_GNUC_UNUSED bool *highlight,
-                         G_GNUC_UNUSED char** sc, G_GNUC_UNUSED void *data)
+screen_artist_lw_callback(unsigned idx, void *data)
 {
        GPtrArray *list = data;
        static char buf[BUFSIZE];
@@ -70,17 +70,14 @@ screen_artist_lw_callback(unsigned idx, G_GNUC_UNUSED bool *highlight,
                if (idx == 0)
                        return "[..]";
                else if (idx == list->len + 1) {
-                       str = utf8_to_locale(_("All tracks"));
-                       g_snprintf(buf, BUFSIZE, "[%s]", str);
-                       g_free(str);
+                       g_snprintf(buf, BUFSIZE, "[%s]", _("All tracks"));
                        return buf;
                }
 
                --idx;
        }
 
-       if (idx >= list->len)
-               return NULL;
+       assert(idx < list->len);
 
        str_utf8 = g_ptr_array_index(list, idx);
        assert(str_utf8 != NULL);
@@ -102,21 +99,6 @@ artist_repaint(void)
        wrefresh(browser.lw->w);
 }
 
-static GPtrArray *
-g_list_to_ptr_array(GList *in)
-{
-       GPtrArray *out = g_ptr_array_sized_new(g_list_length(in));
-       GList *head = in;
-
-       while (in != NULL) {
-               g_ptr_array_add(out, in->data);
-               in = g_list_next(in);
-       }
-
-       g_list_free(head);
-       return out;
-}
-
 static void
 string_array_free(GPtrArray *array)
 {
@@ -131,7 +113,7 @@ string_array_free(GPtrArray *array)
 }
 
 static void
-free_lists(G_GNUC_UNUSED struct mpdclient *c)
+free_lists(void)
 {
        if (artist_list != NULL) {
                string_array_free(artist_list);
@@ -149,10 +131,22 @@ free_lists(G_GNUC_UNUSED struct mpdclient *c)
        }
 }
 
+static void
+recv_tag_values(struct mpd_connection *connection, enum mpd_tag_type tag,
+               GPtrArray *list)
+{
+       struct mpd_pair *pair;
+
+       while ((pair = mpd_recv_pair_tag(connection, tag)) != NULL) {
+               g_ptr_array_add(list, g_strdup(pair->value));
+               mpd_return_pair(connection, pair);
+       }
+}
+
 static void
 load_artist_list(struct mpdclient *c)
 {
-       GList *list;
+       struct mpd_connection *connection = mpdclient_get_connection(c);
 
        assert(mode == LIST_ARTISTS);
        assert(artist == NULL);
@@ -161,17 +155,26 @@ load_artist_list(struct mpdclient *c)
        assert(album_list == NULL);
        assert(browser.filelist == NULL);
 
-       list = mpdclient_get_artists(c);
-       /* sort list */
-       list = g_list_sort(list, compare_utf8);
+       artist_list = g_ptr_array_new();
+
+       if (connection != NULL) {
+               mpd_search_db_tags(connection, MPD_TAG_ARTIST);
+               mpd_search_commit(connection);
+               recv_tag_values(connection, MPD_TAG_ARTIST, artist_list);
+
+               if (!mpd_response_finish(connection))
+                       mpdclient_handle_error(c);
+       }
 
-       artist_list = g_list_to_ptr_array(list);
+       /* sort list */
+       g_ptr_array_sort(artist_list, compare_utf8);
+       list_window_set_length(browser.lw, artist_list->len);
 }
 
 static void
 load_album_list(struct mpdclient *c)
 {
-       GList *list;
+       struct mpd_connection *connection = mpdclient_get_connection(c);
 
        assert(mode == LIST_ALBUMS);
        assert(artist != NULL);
@@ -179,60 +182,76 @@ load_album_list(struct mpdclient *c)
        assert(album_list == NULL);
        assert(browser.filelist == NULL);
 
-       list = mpdclient_get_albums(c, artist);
+       album_list = g_ptr_array_new();
+
+       if (connection != NULL) {
+               mpd_search_db_tags(connection, MPD_TAG_ALBUM);
+               mpd_search_add_tag_constraint(connection,
+                                             MPD_OPERATOR_DEFAULT,
+                                             MPD_TAG_ARTIST, artist);
+               mpd_search_commit(connection);
+
+               recv_tag_values(connection, MPD_TAG_ALBUM, album_list);
+
+               if (!mpd_response_finish(connection))
+                       mpdclient_handle_error(c);
+       }
+
        /* sort list */
-       list = g_list_sort(list, compare_utf8);
+       g_ptr_array_sort(album_list, compare_utf8);
 
-       album_list = g_list_to_ptr_array(list);
+       list_window_set_length(browser.lw, album_list->len + 2);
 }
 
 static void
 load_song_list(struct mpdclient *c)
 {
-       struct mpd_connection *connection = c->connection;
+       struct mpd_connection *connection = mpdclient_get_connection(c);
 
        assert(mode == LIST_SONGS);
        assert(artist != NULL);
        assert(album != NULL);
        assert(browser.filelist == NULL);
 
-       mpd_search_db_songs(connection, true);
-       mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
-                                     MPD_TAG_ARTIST, artist);
-       if (album[0] != 0)
-               mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
-                                             MPD_TAG_ALBUM, album);
-       mpd_search_commit(connection);
+       browser.filelist = filelist_new();
+       /* add a dummy entry for ".." */
+       filelist_append(browser.filelist, NULL);
 
-       browser.filelist = filelist_new_recv(connection);
+       if (connection != NULL) {
+               mpd_search_db_songs(connection, true);
+               mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
+                                             MPD_TAG_ARTIST, artist);
+               if (album[0] != 0)
+                       mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
+                                                     MPD_TAG_ALBUM, album);
+               mpd_search_commit(connection);
 
-       if (!mpd_response_finish(connection))
-               mpdclient_handle_error(c);
+               filelist_recv(browser.filelist, connection);
 
-       /* add a dummy entry for ".." */
-       filelist_prepend(browser.filelist, NULL);
+               if (!mpd_response_finish(connection))
+                       mpdclient_handle_error(c);
+       }
 
-#ifndef NCMPC_MINI
        /* fix highlights */
        screen_browser_sync_highlights(browser.filelist, &c->playlist);
-#endif
+       list_window_set_length(browser.lw, filelist_length(browser.filelist));
 }
 
 static void
-free_state(struct mpdclient *c)
+free_state(void)
 {
        g_free(artist);
        g_free(album);
        artist = NULL;
        album = NULL;
 
-       free_lists(c);
+       free_lists();
 }
 
 static void
 open_artist_list(struct mpdclient *c)
 {
-       free_state(c);
+       free_state();
 
        mode = LIST_ARTISTS;
        load_artist_list(c);
@@ -243,7 +262,7 @@ open_album_list(struct mpdclient *c, char *_artist)
 {
        assert(_artist != NULL);
 
-       free_state(c);
+       free_state();
 
        mode = LIST_ALBUMS;
        artist = _artist;
@@ -256,7 +275,7 @@ open_song_list(struct mpdclient *c, char *_artist, char *_album)
        assert(_artist != NULL);
        assert(_album != NULL);
 
-       free_state(c);
+       free_state();
 
        mode = LIST_SONGS;
        artist = _artist;
@@ -267,7 +286,7 @@ open_song_list(struct mpdclient *c, char *_artist, char *_album)
 static void
 reload_lists(struct mpdclient *c)
 {
-       free_lists(c);
+       free_lists();
 
        switch (mode) {
        case LIST_ARTISTS:
@@ -295,7 +314,7 @@ screen_artist_init(WINDOW *w, int cols, int rows)
 static void
 screen_artist_quit(void)
 {
-       free_state(NULL);
+       free_state();
        list_window_free(browser.lw);
 }
 
@@ -310,16 +329,14 @@ screen_artist_open(struct mpdclient *c)
 static void
 screen_artist_resize(int cols, int rows)
 {
-       browser.lw->cols = cols;
-       browser.lw->rows = rows;
+       list_window_resize(browser.lw, cols, rows);
 }
 
 static void
 screen_artist_paint(void)
 {
        if (browser.filelist) {
-               list_window_paint(browser.lw, browser_lw_callback,
-                                 browser.filelist);
+               screen_browser_paint(&browser);
        } else if (album_list != NULL)
                list_window_paint(browser.lw, screen_artist_lw_callback,
                                  album_list);
@@ -375,14 +392,12 @@ screen_artist_update(struct mpdclient *c)
                /* the db has changed -> update the list */
                reload_lists(c);
 
-#ifndef NCMPC_MINI
-       if (c->events & (MPD_IDLE_DATABASE | MPD_IDLE_PLAYLIST))
+       if (c->events & (MPD_IDLE_DATABASE | MPD_IDLE_QUEUE))
                screen_browser_sync_highlights(browser.filelist, &c->playlist);
-#endif
 
        if (c->events & (MPD_IDLE_DATABASE
 #ifndef NCMPC_MINI
-                        | MPD_IDLE_PLAYLIST
+                        | MPD_IDLE_QUEUE
 #endif
                         ))
                artist_repaint();
@@ -391,12 +406,15 @@ screen_artist_update(struct mpdclient *c)
 static void
 add_query(struct mpdclient *c, enum mpd_tag_type table, char *_filter)
 {
-       struct mpd_connection *connection = c->connection;
+       struct mpd_connection *connection = mpdclient_get_connection(c);
        char *str;
        struct filelist *addlist;
 
        assert(filter != NULL);
 
+       if (connection == NULL)
+               return;
+
        str = utf8_to_locale(_filter);
        if (table == MPD_TAG_ALBUM)
                screen_status_printf("Adding album %s...", str);
@@ -419,24 +437,13 @@ add_query(struct mpdclient *c, enum mpd_tag_type table, char *_filter)
        filelist_free(addlist);
 }
 
-static unsigned
-metalist_length(void)
-{
-       assert(mode != LIST_ARTISTS || artist_list != NULL);
-       assert(mode != LIST_ALBUMS || album_list != NULL);
-
-       return mode == LIST_ALBUMS
-               ? album_list->len + 2
-               : artist_list->len;
-}
-
 static int
 screen_artist_lw_cmd(struct mpdclient *c, command_t cmd)
 {
        switch (mode) {
        case LIST_ARTISTS:
        case LIST_ALBUMS:
-               return list_window_cmd(browser.lw, metalist_length(), cmd);
+               return list_window_cmd(browser.lw, cmd);
 
        case LIST_SONGS:
                return browser_cmd(&browser, c, cmd);
@@ -462,6 +469,7 @@ string_array_find(GPtrArray *array, const char *value)
 static bool
 screen_artist_cmd(struct mpdclient *c, command_t cmd)
 {
+       struct list_window_range range;
        char *selected;
        char *old;
        int idx;
@@ -470,6 +478,9 @@ screen_artist_cmd(struct mpdclient *c, command_t cmd)
        case CMD_PLAY:
                switch (mode) {
                case LIST_ARTISTS:
+                       if (browser.lw->selected >= artist_list->len)
+                               return true;
+
                        selected = g_ptr_array_index(artist_list,
                                                     browser.lw->selected);
                        open_album_list(c, g_strdup(selected));
@@ -490,9 +501,8 @@ screen_artist_cmd(struct mpdclient *c, command_t cmd)
                                g_free(old);
 
                                if (idx >= 0) {
-                                       list_window_set_selected(browser.lw, idx);
-                                       list_window_center(browser.lw,
-                                                          artist_list->len, idx);
+                                       list_window_set_cursor(browser.lw, idx);
+                                       list_window_center(browser.lw, idx);
                                }
                        } else if (browser.lw->selected == album_list->len + 1) {
                                /* handle "show all" */
@@ -524,9 +534,8 @@ screen_artist_cmd(struct mpdclient *c, command_t cmd)
 
                                if (idx >= 0) {
                                        ++idx;
-                                       list_window_set_selected(browser.lw, idx);
-                                       list_window_center(browser.lw,
-                                                          album_list->len, idx);
+                                       list_window_set_cursor(browser.lw, idx);
+                                       list_window_center(browser.lw, idx);
                                }
 
                                artist_repaint();
@@ -553,9 +562,8 @@ screen_artist_cmd(struct mpdclient *c, command_t cmd)
                        g_free(old);
 
                        if (idx >= 0) {
-                               list_window_set_selected(browser.lw, idx);
-                               list_window_center(browser.lw,
-                                                  artist_list->len, idx);
+                               list_window_set_cursor(browser.lw, idx);
+                               list_window_center(browser.lw, idx);
                        }
                        break;
 
@@ -572,9 +580,8 @@ screen_artist_cmd(struct mpdclient *c, command_t cmd)
 
                        if (idx >= 0) {
                                ++idx;
-                               list_window_set_selected(browser.lw, idx);
-                               list_window_center(browser.lw,
-                                                  album_list->len, idx);
+                               list_window_set_cursor(browser.lw, idx);
+                               list_window_center(browser.lw, idx);
                        }
                        break;
                }
@@ -603,8 +610,11 @@ screen_artist_cmd(struct mpdclient *c, command_t cmd)
        case CMD_ADD:
                switch(mode) {
                case LIST_ARTISTS:
-                       for (unsigned i = browser.lw->selected_start;
-                            i <= browser.lw->selected_end; ++i) {
+                       if (browser.lw->selected >= artist_list->len)
+                               return true;
+
+                       list_window_get_range(browser.lw, &range);
+                       for (unsigned i = range.start; i < range.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... */
@@ -612,8 +622,8 @@ screen_artist_cmd(struct mpdclient *c, command_t cmd)
                        break;
 
                case LIST_ALBUMS:
-                       for (unsigned i = browser.lw->selected_start;
-                            i <= browser.lw->selected_end; ++i) {
+                       list_window_get_range(browser.lw, &range);
+                       for (unsigned i = range.start; i < range.end; ++i) {
                                if(i == album_list->len + 1)
                                        add_query(c, MPD_TAG_ARTIST, artist);
                                else if (i > 0)
@@ -643,16 +653,14 @@ screen_artist_cmd(struct mpdclient *c, command_t cmd)
        case CMD_LIST_RFIND_NEXT:
                switch (mode) {
                case LIST_ARTISTS:
-                       screen_find(browser.lw, artist_list->len,
-                                   cmd, screen_artist_lw_callback,
-                                   artist_list);
+                       screen_find(browser.lw, cmd,
+                                   screen_artist_lw_callback, artist_list);
                        artist_repaint();
                        return true;
 
                case LIST_ALBUMS:
-                       screen_find(browser.lw, album_list->len + 2,
-                                   cmd, screen_artist_lw_callback,
-                                   album_list);
+                       screen_find(browser.lw, cmd,
+                                   screen_artist_lw_callback, album_list);
                        artist_repaint();
                        return true;
 
@@ -664,13 +672,13 @@ screen_artist_cmd(struct mpdclient *c, command_t cmd)
                switch (mode) {
                case LIST_ARTISTS:
                        screen_jump(browser.lw, screen_artist_lw_callback,
-                                   artist_list);
+                                   NULL, artist_list);
                        artist_repaint();
                        return true;
 
                case LIST_ALBUMS:
                        screen_jump(browser.lw, screen_artist_lw_callback,
-                                   album_list);
+                                   NULL, album_list);
                        artist_repaint();
                        return true;