Code

screen_artist.c: use libmpdclient to obtain artist/album list
authorMax Kellermann <max@duempel.org>
Sat, 10 Oct 2009 16:30:21 +0000 (18:30 +0200)
committerMax Kellermann <max@duempel.org>
Sat, 10 Oct 2009 16:30:21 +0000 (18:30 +0200)
Moved the mpdclient_get_artists/albums() code from mpdclient.c to
screen_artist.c.

src/mpdclient.c
src/mpdclient.h
src/screen_artist.c

index 9b896e2691b6613ffa42e680f17be09b8fa701d6..30e0fe68fe9a9d5ce0a33e198223d6b9de33b9ff 100644 (file)
@@ -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;
-}
index d9b568f0c82a10c8e2e04425138bdf0e485823ad..6c8758f6674d4becdb22a8915d50dd947254c606 100644 (file)
@@ -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 */
index 57d99994e696e1962792952a5e07d345813a290a..3278f8fbf63926b98432dbb97388e6d58133ff14 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);
@@ -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);
 }