diff --git a/gui/playlist.c b/gui/playlist.c index d6ca74cd..8bdbe2ea 100644 --- a/gui/playlist.c +++ b/gui/playlist.c @@ -322,13 +322,12 @@ void gui_playlist_init() "document-open-recent"); /* Add "Playlists" header and playlists. */ - gui_sidebar_iter_next(&parent); + gui_sidebar_iter_find(&parent, "Playlists", PL_MAX_TYPE); __playlist_add(&parent, "Favorites", "emblem-favorite", PL_SYSTEM); __playlist_add(&parent, "Hidden", "window-close", PL_SYSTEM); /* Add "Dynamic" header. */ - gui_sidebar_iter_next(&parent); - gui_sidebar_iter_next(&parent); + gui_sidebar_iter_find(&parent, "Dynamic", PL_MAX_TYPE); __playlist_add(&parent, "Most Played", "go-up", PL_SYSTEM); __playlist_add(&parent, "Least Played", "go-down", PL_SYSTEM); __playlist_add(&parent, "Unplayed", "audio-x-generic", PL_SYSTEM); @@ -348,16 +347,9 @@ gchar *gui_playlist_cur() void gui_playlist_add_library(struct library *library) { GtkTreeIter parent; - gchar *name; - - gtk_tree_model_get_iter_first(gui_sidebar_model(), &parent); - while (gtk_tree_model_iter_next(gui_sidebar_model(), &parent)) { - name = gui_sidebar_iter_name(&parent); - if (string_match(name, "Library")) - __playlist_add(&parent, library->li_path, "folder", PL_LIBRARY); - g_free(name); - } - + gui_sidebar_iter_first(&parent); + if (gui_sidebar_iter_find(&parent, "Library", PL_MAX_TYPE)) + __playlist_add(&parent, library->li_path, "folder", PL_LIBRARY); gtk_tree_view_expand_all(gui_sidebar_treeview()); } @@ -367,15 +359,8 @@ void gui_playlist_add_artist(struct artist *artist) gchar *name; bool match; - gtk_tree_model_get_iter_first(gui_sidebar_model(), &parent); - do { - name = gui_sidebar_iter_name(&parent); - match = string_match(name, "Collection"); - g_free(name); - - if (match) - break; - } while (gtk_tree_model_iter_next(gui_sidebar_model(), &parent)); + gui_sidebar_iter_first(&parent); + gui_sidebar_iter_find(&parent, "Collection", PL_SYSTEM); if (!gtk_tree_model_iter_children(gui_sidebar_model(), &sibling, &parent)) { __playlist_add(&parent, artist->ar_name, "system-users", PL_ARTIST); @@ -404,15 +389,8 @@ void gui_playlist_add_user(struct playlist *playlist) gchar *name; bool match; - gtk_tree_model_get_iter_first(gui_sidebar_model(), &parent); - do { - name = gui_sidebar_iter_name(&parent); - match = string_match(name, "Playlists"); - g_free(name); - - if (match) - break; - } while (gtk_tree_model_iter_next(gui_sidebar_model(), &parent)); + gui_sidebar_iter_first(&parent); + gui_sidebar_iter_find(&parent, "Playlists", PL_MAX_TYPE); if (!gtk_tree_model_iter_children(gui_sidebar_model(), &sibling, &parent)) { __playlist_add(&parent, playlist->pl_name, "text-x-generic", PL_USER); diff --git a/gui/sidebar.c b/gui/sidebar.c index bac48528..bf59ba86 100644 --- a/gui/sidebar.c +++ b/gui/sidebar.c @@ -2,6 +2,7 @@ * Copyright 2015 (c) Anna Schumaker. */ #include +#include #include enum sidebar_columns { @@ -116,3 +117,24 @@ void gui_sidebar_iter_add(GtkTreeIter *iter, struct playlist *playlist, __gui_sidebar_set(&new, text, image, playlist->pl_type); g_free(text); } + +gboolean gui_sidebar_iter_find(GtkTreeIter *iter, const gchar *name, + enum playlist_type_t type) +{ + bool match; + gchar *cur; + + do { + if (gui_sidebar_iter_type(iter) != type) + continue; + + cur = gui_sidebar_iter_name(iter); + match = string_match(cur, name); + g_free(cur); + + if (match) + return TRUE; + } while (gui_sidebar_iter_next(iter)); + + return FALSE; +} diff --git a/include/gui/sidebar.h b/include/gui/sidebar.h index 2ce4dbc3..169f5cb8 100644 --- a/include/gui/sidebar.h +++ b/include/gui/sidebar.h @@ -27,6 +27,13 @@ enum playlist_type_t gui_sidebar_iter_type(GtkTreeIter *); /* Called to add a playlist at the current iterator. */ void gui_sidebar_iter_add(GtkTreeIter *, struct playlist *, const gchar *); +/* + * Called to find a playlist with the given name and type, + * starting from the current iterator position. + */ +gboolean gui_sidebar_iter_find(GtkTreeIter *, const gchar *, + enum playlist_type_t); + /* Called to get the sidebar widget. */ static inline GtkPaned *gui_sidebar() { diff --git a/tests/gui/sidebar.c b/tests/gui/sidebar.c index 488362eb..b1cd7ac9 100644 --- a/tests/gui/sidebar.c +++ b/tests/gui/sidebar.c @@ -45,6 +45,17 @@ static void test_sidebar() g_assert_true(gui_sidebar_iter_next(&iter)); } g_assert_false(gui_sidebar_iter_next(&iter)); + + gui_sidebar_iter_first(&iter); + g_assert_true(gui_sidebar_iter_find(&iter, "Playlists", PL_MAX_TYPE)); + g_assert_cmpstr_free(gui_sidebar_iter_name(&iter), ==, "Playlists"); + g_assert_cmpuint(gui_sidebar_iter_type(&iter), ==, PL_MAX_TYPE); + + g_assert_false(gui_sidebar_iter_find(&iter, "History", PL_SYSTEM)); + gui_sidebar_iter_first(&iter); + g_assert_true(gui_sidebar_iter_find(&iter, "History", PL_SYSTEM)); + g_assert_cmpstr_free(gui_sidebar_iter_name(&iter), ==, "History"); + g_assert_cmpuint(gui_sidebar_iter_type(&iter), ==, PL_SYSTEM); } static void test_sidebar_pos()