From: Max Kellermann Date: Sat, 10 Oct 2009 16:30:21 +0000 (+0200) Subject: screen_artist.c: use libmpdclient to obtain artist/album list X-Git-Tag: release-0.16~130 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=64b0ff4e6edf98beee80262a6570040f926f4f4a;p=ncmpc.git screen_artist.c: use libmpdclient to obtain artist/album list Moved the mpdclient_get_artists/albums() code from mpdclient.c to screen_artist.c. --- diff --git a/src/mpdclient.c b/src/mpdclient.c index 9b896e2..30e0fe6 100644 --- a/src/mpdclient.c +++ b/src/mpdclient.c @@ -758,61 +758,3 @@ mpdclient_filelist_add_all(struct mpdclient *c, struct filelist *fl) mpd_command_list_end(connection); return mpdclient_finish_command(c); } - -static GList * -recv_tag_values(struct mpd_connection *connection, enum mpd_tag_type tag) -{ - GList *list = NULL; - struct mpd_pair *pair; - - while ((pair = mpd_recv_pair_tag(connection, tag)) != NULL) { - list = g_list_append(list, g_strdup(pair->value)); - mpd_return_pair(connection, pair); - } - - return list; -} - -GList * -mpdclient_get_artists(struct mpdclient *c) -{ - struct mpd_connection *connection = mpdclient_get_connection(c); - GList *list; - - if (connection == NULL) - return NULL; - - mpd_search_db_tags(connection, MPD_TAG_ARTIST); - mpd_search_commit(connection); - - list = recv_tag_values(connection, MPD_TAG_ARTIST); - - if (!mpdclient_finish_command(c)) - return string_list_free(list); - - return list; -} - -GList * -mpdclient_get_albums(struct mpdclient *c, const gchar *artist_utf8) -{ - struct mpd_connection *connection = mpdclient_get_connection(c); - GList *list; - - if (connection == NULL) - return NULL; - - mpd_search_db_tags(connection, MPD_TAG_ALBUM); - if (artist_utf8 != NULL) - mpd_search_add_tag_constraint(connection, - MPD_OPERATOR_DEFAULT, - MPD_TAG_ARTIST, artist_utf8); - mpd_search_commit(connection); - - list = recv_tag_values(connection, MPD_TAG_ALBUM); - - if (!mpdclient_finish_command(c)) - return string_list_free(list); - - return list; -} diff --git a/src/mpdclient.h b/src/mpdclient.h index d9b568f..6c8758f 100644 --- a/src/mpdclient.h +++ b/src/mpdclient.h @@ -125,10 +125,6 @@ mpdclient_cmd_delete_range(struct mpdclient *c, unsigned start, unsigned end); bool mpdclient_cmd_move(struct mpdclient *c, gint old_index, gint new_index); -/* list functions */ -GList *mpdclient_get_artists(struct mpdclient *c); -GList *mpdclient_get_albums(struct mpdclient *c, const gchar *artist_utf8); - /*** playlist functions **************************************************/ /* update the complete playlist */ diff --git a/src/screen_artist.c b/src/screen_artist.c index 57d9999..3278f8f 100644 --- a/src/screen_artist.c +++ b/src/screen_artist.c @@ -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); @@ -101,21 +102,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) { @@ -148,10 +134,22 @@ free_lists(void) } } +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); @@ -160,18 +158,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); - artist_list = g_list_to_ptr_array(list); + if (!mpd_response_finish(connection)) + mpdclient_handle_error(c); + } + + /* 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,11 +185,24 @@ 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); }