diff --git a/gui/playlists/user.c b/gui/playlists/user.c index 1a897f13..4c9e04bd 100644 --- a/gui/playlists/user.c +++ b/gui/playlists/user.c @@ -14,6 +14,13 @@ static bool __gui_pl_user_header(GtkTreeIter *iter) return false; } +static gint __gui_pl_user_compare(gconstpointer a, gconstpointer b) +{ + struct playlist *pl_a = (struct playlist *)a; + struct playlist *pl_b = (struct playlist *)b; + return g_utf8_collate(pl_a->pl_name, pl_b->pl_name); +} + static bool __gui_pl_user_init_idle() { struct db_entry *user, *next; @@ -88,3 +95,18 @@ void gui_pl_user_update(struct playlist *playlist) if (gui_sidebar_iter_find(&child, playlist->pl_name, playlist->pl_type)) gui_sidebar_iter_update(&child); } + +GList *gui_pl_user_list(void) +{ + struct db_entry *user, *next; + struct playlist *playlist; + GList *list = NULL; + + db_for_each(user, next, pl_user_db_get()) { + playlist = &USER_PLAYLIST(user)->pl_playlist; + list = g_list_insert_sorted(list, playlist, + __gui_pl_user_compare); + } + + return list; +} diff --git a/gui/view.c b/gui/view.c index 3ca83e63..e422405d 100644 --- a/gui/view.c +++ b/gui/view.c @@ -143,16 +143,8 @@ static void __view_rc_add_other(GtkWidget *item, gpointer data) &vad_data); } -static gint __view_cmp_pl(gconstpointer a, gconstpointer b) -{ - struct playlist *pl_a = (struct playlist *)a; - struct playlist *pl_b = (struct playlist *)b; - return g_utf8_collate(pl_a->pl_name, pl_b->pl_name); -} - static GtkWidget *__view_rc_build_submenu(void) { - struct db_entry *dbe, *next; struct playlist *playlist; GtkWidget *submenu, *item; GList *iter, *list = NULL; @@ -160,12 +152,8 @@ static GtkWidget *__view_rc_build_submenu(void) if (pl_user_db_get()->db_size == 0) return NULL; - db_for_each(dbe, next, pl_user_db_get()) { - playlist = &USER_PLAYLIST(dbe)->pl_playlist; - list = g_list_insert_sorted(list, playlist, __view_cmp_pl); - } - submenu = gtk_menu_new(); + list = gui_pl_user_list(); iter = g_list_first(list); while (iter) { playlist = (struct playlist *)iter->data; @@ -176,6 +164,7 @@ static GtkWidget *__view_rc_build_submenu(void) iter = g_list_next(iter); } + g_list_free(list); return submenu; } diff --git a/include/gui/playlists/user.h b/include/gui/playlists/user.h index 2d2085a0..0d2b26d4 100644 --- a/include/gui/playlists/user.h +++ b/include/gui/playlists/user.h @@ -16,4 +16,10 @@ struct playlist *gui_pl_user_add_dialog(void); /* Called to update a user playlist. */ void gui_pl_user_update(struct playlist *); +/* + * Called to get a (sorted) list of user playlists. + * Note: The caller is responsible for freeing the list with g_list_free(). + */ +GList *gui_pl_user_list(void); + #endif /* OCARINA_GUI_PLAYLISTS_USER_H */ diff --git a/tests/gui/playlists/user.c b/tests/gui/playlists/user.c index 6820daad..6943aeda 100644 --- a/tests/gui/playlists/user.c +++ b/tests/gui/playlists/user.c @@ -17,8 +17,10 @@ struct core_init_data init_data = { static void test_user() { - GtkTreeModel *model = gui_sidebar_model(); - GtkTreeIter iter, child; + GtkTreeModel *model = gui_sidebar_model(); + struct playlist *playlist; + GtkTreeIter iter, child; + GList *list = NULL; gui_sidebar_iter_first(&iter); gui_sidebar_iter_find(&iter, "Playlists", PL_MAX_TYPE); @@ -47,6 +49,22 @@ static void test_user() g_assert_true(gui_sidebar_iter_next(&child)); g_assert_cmpstr_free(gui_sidebar_iter_name(&child), ==, "Test 2"); g_assert_cmpuint(gui_sidebar_iter_type(&child), ==, PL_USER); + + list = gui_pl_user_list(); + g_assert_nonnull(list); + g_list_length(list); + + playlist = g_list_nth_data(list, 0); + g_assert_nonnull(playlist); + g_assert_cmpuint(playlist->pl_type, ==, PL_USER); + g_assert_cmpstr(playlist->pl_name, ==, "Test 1"); + + playlist = g_list_nth_data(list, 1); + g_assert_nonnull(playlist); + g_assert_cmpuint(playlist->pl_type, ==, PL_USER); + g_assert_cmpstr(playlist->pl_name, ==, "Test 2"); + + g_list_free(list); } int main(int argc, char **argv)