From 32d712c21359013b68848acd82a4890141943663 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Mon, 22 Aug 2016 10:15:09 -0400 Subject: [PATCH] gui/filter: Remember search text when switching playlists Signed-off-by: Anna Schumaker --- gui/filter.c | 46 +++++++++++++++++++++++++++++++------------- gui/playlist.c | 9 ++++++++- gui/queue.c | 3 --- gui/view.c | 3 +-- include/gui/filter.h | 7 +++++++ tests/gui/filter.c | 16 ++++++++++++--- tests/gui/queue.c | 1 - tests/gui/view.c | 2 +- 8 files changed, 63 insertions(+), 24 deletions(-) diff --git a/gui/filter.c b/gui/filter.c index 8a36b406..33f4a212 100644 --- a/gui/filter.c +++ b/gui/filter.c @@ -6,8 +6,6 @@ #include static GtkTreeModelFilter *filter_model = NULL; -static gchar **filter_search = NULL; - static inline GtkTreePath *__gui_filter_convert_path(GtkTreePath *path) { @@ -42,15 +40,16 @@ static gboolean __gui_filter_visible_func(GtkTreeModel *model, GtkTreeIter *iter, gpointer data) { - struct track *track; unsigned int i, how = gtk_combo_box_get_active(gui_filter_how()); + gchar **search = gui_model_get_playlist()->pl_private; + struct track *track; - if (!filter_search) + if (!search) return TRUE; track = gui_model_iter_get_track(iter); - for (i = 0; filter_search[i]; i++) { - if (!__gui_filter_match_token(track, filter_search[i], how)) + for (i = 0; search[i]; i++) { + if (!__gui_filter_match_token(track, search[i], how)) return FALSE; } return TRUE; @@ -58,16 +57,16 @@ static gboolean __gui_filter_visible_func(GtkTreeModel *model, void __gui_filter_search_changed(GtkSearchEntry *search, gpointer data) { - const gchar *text = gtk_entry_get_text(GTK_ENTRY(search)); + const gchar *text = gtk_entry_get_text(GTK_ENTRY(search)); + struct playlist *playlist = gui_model_get_playlist(); - if (!gui_model_get_playlist()) + if (!playlist) return; - if (strlen(text) > 0) - filter_search = g_str_tokenize_and_fold(text, NULL, NULL); - gtk_tree_model_filter_refilter(gui_filter_get()); - g_strfreev(filter_search); - filter_search = NULL; + gui_filter_clear_search(playlist); + if (strlen(text) > 0) + playlist->pl_private = g_str_tokenize_and_fold(text, NULL, NULL); + gtk_tree_model_filter_refilter(gui_filter_get()); } void __gui_filter_how_changed(int n) @@ -91,6 +90,27 @@ void gui_filter_deinit() g_object_unref(G_OBJECT(filter_model)); } +void gui_filter_clear_search(struct playlist *playlist) +{ + gchar **text; + + if (playlist && playlist->pl_private) { + text = playlist->pl_private; + g_strfreev(text); + playlist->pl_private = NULL; + } +} + +void gui_filter_set_playlist(struct playlist *playlist) +{ + gchar **search = playlist ? (gchar **)playlist->pl_private : NULL; + gchar *text = search ? g_strjoinv(" ", search) : g_strdup(""); + + gui_model_set_playlist(playlist); + gtk_entry_set_text(GTK_ENTRY(gui_filter_search()), text); + g_free(text); +} + GtkTreeModelFilter *gui_filter_get() { return filter_model; diff --git a/gui/playlist.c b/gui/playlist.c index de04ec91..c8f59e84 100644 --- a/gui/playlist.c +++ b/gui/playlist.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -257,6 +258,12 @@ static void *__playlist_init(struct queue *queue, void *data) return gui_queue_alloc(playlist, queue, playlist->pl_name, flags); } +static void __playlist_deinit(struct queue *queue) +{ + gui_filter_clear_search(gui_queue(queue)->gq_playlist); + gui_queue_free(queue); +} + static void __playlist_added(struct queue *queue, unsigned int row) { gui_model_add(gui_queue(queue)->gq_playlist, row); @@ -481,7 +488,7 @@ void gui_playlist_add_user(struct playlist *playlist) struct queue_ops playlist_ops = { .qop_init = __playlist_init, - .qop_deinit = gui_queue_free, + .qop_deinit = __playlist_deinit, .qop_added = __playlist_added, .qop_erase = __playlist_erase, .qop_removed = __playlist_removed, diff --git a/gui/queue.c b/gui/queue.c index b3ec6c04..598dc729 100644 --- a/gui/queue.c +++ b/gui/queue.c @@ -67,7 +67,6 @@ void gui_queue_free(struct queue *queue) void gui_queue_show(struct gui_queue *queue) { GtkButton *random = GTK_BUTTON(gui_builder_widget("o_random")); - GtkEntry *search = GTK_ENTRY(gui_filter_search()); bool has_random = false; gq_queue = queue; @@ -81,6 +80,4 @@ void gui_queue_show(struct gui_queue *queue) gui_view_set_playlist(NULL); gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(random), has_random); - gtk_widget_set_sensitive(GTK_WIDGET(search), queue != NULL); - gtk_entry_set_text(search, ""); } diff --git a/gui/view.c b/gui/view.c index 4efc2aec..6f4e3e8b 100644 --- a/gui/view.c +++ b/gui/view.c @@ -6,7 +6,6 @@ #include #include #include -#include #include #include #include @@ -396,7 +395,7 @@ void gui_view_init() void gui_view_set_playlist(struct playlist *playlist) { - gui_model_set_playlist(playlist); + gui_filter_set_playlist(playlist); view_sort_count = 0; __view_display_sorting(""); diff --git a/include/gui/filter.h b/include/gui/filter.h index f26750cd..26c1da5f 100644 --- a/include/gui/filter.h +++ b/include/gui/filter.h @@ -3,6 +3,7 @@ */ #ifndef OCARINA_GUI_FILTER_H #define OCARINA_GUI_FILTER_H +#include #include enum gui_filter_how { @@ -19,6 +20,12 @@ void gui_filter_init(); /* Called to deinitialize the filter model. */ void gui_filter_deinit(); +/* Called to clear any saved search text. */ +void gui_filter_clear_search(struct playlist *); + +/* Called to set the current playlist. */ +void gui_filter_set_playlist(struct playlist *); + /* Called to get the filter model. */ GtkTreeModelFilter *gui_filter_get(); diff --git a/tests/gui/filter.c b/tests/gui/filter.c index 5ea3b511..fb32ac13 100644 --- a/tests/gui/filter.c +++ b/tests/gui/filter.c @@ -10,7 +10,7 @@ void *test_queue_init(struct queue *queue, void *data) { return NULL; } void test_queue_deinit(struct queue *queue) - { } + { gui_filter_clear_search(queue->q_private); } void test_queue_add(struct queue *queue, unsigned int n) { gui_model_add(queue->q_private, n); } void test_queue_remove(struct queue *queue, unsigned int n) @@ -38,6 +38,7 @@ struct core_init_data init_data = { void test_filter() { struct track *track; + GtkEntry *entry; GtkTreeModel *model; GtkTreePath *path; GtkTreeIter iter; @@ -48,12 +49,13 @@ void test_filter() g_assert(gtk_tree_model_filter_get_model(gui_filter_get()) == GTK_TREE_MODEL(gui_model_get())); + entry = GTK_ENTRY(gui_filter_search()); model = GTK_TREE_MODEL(gui_filter_get()); playlist_get_queue(PL_SYSTEM, "Collection")->q_private = playlist_get(PL_SYSTEM, "Collection"); g_assert_false(gtk_tree_model_get_iter_first(model, &iter)); - gui_model_set_playlist(playlist_get(PL_SYSTEM, "Collection")); + gui_filter_set_playlist(playlist_get(PL_SYSTEM, "Collection")); g_assert_false(gtk_tree_model_get_iter_first(model, &iter)); playlist_new(PL_LIBRARY, "tests/Music/Hyrule Symphony"); @@ -72,12 +74,20 @@ void test_filter() g_assert_null(gui_filter_path_get_track(path)); gtk_tree_path_free(path); - gtk_entry_set_text(GTK_ENTRY(gui_filter_search()), "Hyrule"); + gtk_entry_set_text(entry, "Hyrule"); g_assert_cmpuint(gtk_tree_model_iter_n_children(model, NULL), ==, 13); gtk_combo_box_set_active(gui_filter_how(), GUI_FILTER_TITLE); g_assert_cmpuint(gtk_tree_model_iter_n_children(model, NULL), ==, 2); + gui_filter_set_playlist(playlist_get(PL_SYSTEM, "Unplayed")); + g_assert_cmpuint(gtk_tree_model_iter_n_children(model, NULL), ==, 13); + g_assert_cmpstr(gtk_entry_get_text(entry), ==, ""); + + gui_filter_set_playlist(playlist_get(PL_SYSTEM, "Collection")); + g_assert_cmpuint(gtk_tree_model_iter_n_children(model, NULL), ==, 2); + g_assert_cmpstr(gtk_entry_get_text(entry), ==, "hyrule"); + gtk_combo_box_set_active(gui_filter_how(), GUI_FILTER_DEFAULT); g_assert_cmpuint(gtk_tree_model_iter_n_children(model, NULL), ==, 13); } diff --git a/tests/gui/queue.c b/tests/gui/queue.c index c174bdc9..4890e809 100644 --- a/tests/gui/queue.c +++ b/tests/gui/queue.c @@ -81,7 +81,6 @@ static void test_queue() /* Attempt to show a NULL pointer */ gui_queue_show(NULL); - g_assert_false(gtk_widget_get_sensitive(GTK_WIDGET(search))); g_assert_false(gtk_widget_get_sensitive(GTK_WIDGET(random))); g_assert_false(gtk_toggle_button_get_active(random)); diff --git a/tests/gui/view.c b/tests/gui/view.c index d89507ac..b23df3a1 100644 --- a/tests/gui/view.c +++ b/tests/gui/view.c @@ -54,7 +54,7 @@ static void test_treeview() playlist_new(PL_LIBRARY, "tests/Music/Hyrule Symphony"); while (idle_run_task() == true) {} - gui_model_set_playlist(playlist_get(PL_SYSTEM, "Collection")); + gui_filter_set_playlist(playlist_get(PL_SYSTEM, "Collection")); filter = GTK_TREE_MODEL(gui_filter_get()); treeview = GTK_TREE_VIEW(gui_builder_widget("o_treeview"));