From cdbf8b173618159eeb3ac010c9c11d4eaa21725f Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Wed, 21 Sep 2016 16:49:02 -0400 Subject: [PATCH] core/playlist: Rename pl_private -> pl_search And change its type to gchar **. This lets the playlist code manage setting and freeing search strings. The UI is still responsible for how this string is used. This patch also lets me remove the now-unused queue_deinit() function and associated callback. Signed-off-by: Anna Schumaker --- core/playlist.c | 13 +++++++++++++ core/playlists/generic.c | 5 +++-- core/queue.c | 11 ----------- gui/filter.c | 26 ++++---------------------- gui/playlist.c | 6 ------ include/core/playlist.h | 4 ++++ include/core/playlists/playlist.h | 2 +- include/core/queue.h | 6 ------ include/gui/filter.h | 3 --- tests/core/playlist.c | 10 ++++++++++ tests/core/queue.c | 13 ------------- tests/gui/filter.c | 3 --- tests/gui/model.c | 3 --- 13 files changed, 35 insertions(+), 70 deletions(-) diff --git a/core/playlist.c b/core/playlist.c index d6573bae..7f664e99 100644 --- a/core/playlist.c +++ b/core/playlist.c @@ -207,3 +207,16 @@ bool playlist_sort(struct playlist *playlist, enum compare_t sort) playlist_types[playlist->pl_type]->pl_save(); return g_slist_length(playlist->pl_sort) > 0; } + +void playlist_set_search(struct playlist *playlist, const gchar *text) +{ + gchar **tokens = NULL; + + if (!playlist) + return; + if (playlist->pl_search) + g_strfreev(playlist->pl_search); + if (strlen(text) > 0) + tokens = g_str_tokenize_and_fold(text, NULL, NULL); + playlist->pl_search = tokens; +} diff --git a/core/playlists/generic.c b/core/playlists/generic.c index e96cec5e..74ef47f5 100644 --- a/core/playlists/generic.c +++ b/core/playlists/generic.c @@ -44,18 +44,19 @@ void playlist_generic_init(struct playlist *playlist, struct queue_ops *ops) queue_init(&playlist->pl_queue, ops, playlist); playlist->pl_sort = NULL; playlist->pl_current = NULL; + playlist->pl_search = NULL; playlist_generic_sort(playlist, COMPARE_ARTIST); playlist_generic_sort(playlist, COMPARE_YEAR); playlist_generic_sort(playlist, COMPARE_TRACK); - playlist->pl_private = NULL; } void playlist_generic_deinit(struct playlist *playlist) { if (playlist) { - queue_deinit(&playlist->pl_queue); playlist_generic_clear(playlist); playlist_clear_sort(playlist); + g_strfreev(playlist->pl_search); + playlist->pl_search = NULL; playlist->pl_length = 0; } } diff --git a/core/queue.c b/core/queue.c index fdf4a7f4..d280a0be 100644 --- a/core/queue.c +++ b/core/queue.c @@ -13,19 +13,8 @@ static inline void *__queue_init(struct queue *queue, void *data) return NULL; } -static inline void __queue_deinit(struct queue *queue) -{ - if (queue->q_ops) - queue->q_ops->qop_deinit(queue); -} - void queue_init(struct queue *queue, const struct queue_ops *ops, void *data) { queue->q_ops = ops; queue->q_private = __queue_init(queue, data); } - -void queue_deinit(struct queue *queue) -{ - __queue_deinit(queue); -} diff --git a/gui/filter.c b/gui/filter.c index b9ac11d1..9a089cf1 100644 --- a/gui/filter.c +++ b/gui/filter.c @@ -42,7 +42,7 @@ static gboolean __gui_filter_visible_func(GtkTreeModel *model, gpointer data) { unsigned int i, how = gtk_combo_box_get_active(gui_filter_how()); - gchar **search = gui_model_get_playlist()->pl_private; + gchar **search = gui_model_get_playlist()->pl_search; struct track *track; if (!search) @@ -58,15 +58,8 @@ 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)); - struct playlist *playlist = gui_model_get_playlist(); - - if (!playlist) - return; - - gui_filter_clear_search(playlist); - if (strlen(text) > 0) - playlist->pl_private = g_str_tokenize_and_fold(text, NULL, NULL); + playlist_set_search(gui_model_get_playlist(), + gtk_entry_get_text(GTK_ENTRY(search))); gtk_tree_model_filter_refilter(gui_filter_get()); } @@ -91,20 +84,9 @@ 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 **search = playlist ? (gchar **)playlist->pl_search : NULL; gchar *text = search ? g_strjoinv(" ", search) : g_strdup(""); gui_model_set_playlist(playlist); diff --git a/gui/playlist.c b/gui/playlist.c index 1bf2242b..16875744 100644 --- a/gui/playlist.c +++ b/gui/playlist.c @@ -37,11 +37,6 @@ static void *__gui_playlist_init(struct queue *queue, void *data) return playlist; } -static void __gui_playlist_deinit(struct queue *queue) -{ - gui_filter_clear_search(queue->q_private); -} - static void __gui_playlist_added(struct playlist *playlist, struct track *track) { gui_model_add(playlist, track); @@ -58,7 +53,6 @@ static void __gui_playlist_removed(struct playlist *playlist, struct track *trac struct queue_ops playlist_ops = { .qop_init = __gui_playlist_init, - .qop_deinit = __gui_playlist_deinit, }; struct playlist_callbacks playlist_cb = { diff --git a/include/core/playlist.h b/include/core/playlist.h index 65ddef3f..f22901c2 100644 --- a/include/core/playlist.h +++ b/include/core/playlist.h @@ -70,4 +70,8 @@ void playlist_set_random(struct playlist *, bool); /* Called to change the sort order of the playlist. */ bool playlist_sort(struct playlist *, enum compare_t); + +/* Called to set the playlist's search text */ +void playlist_set_search(struct playlist *, const gchar *); + #endif /* OCARINA_CORE_PLAYLIST_H */ diff --git a/include/core/playlists/playlist.h b/include/core/playlists/playlist.h index 1f326f93..a85ea2cd 100644 --- a/include/core/playlists/playlist.h +++ b/include/core/playlists/playlist.h @@ -53,7 +53,7 @@ struct playlist { bool pl_random; /* This playlist's random setting. */ playlist_iter pl_current; /* This playlist's current track. */ GSList *pl_sort; /* This playlist's sort order. */ - void *pl_private; /* This playlist's private data. */ + gchar **pl_search; /* This playlist's search text. */ struct queue pl_queue; /* This playlist's queue of tracks. */ const struct playlist_ops *pl_ops; /* This playlist's supported operations. */ diff --git a/include/core/queue.h b/include/core/queue.h index 8c1c36ef..bcdec092 100644 --- a/include/core/queue.h +++ b/include/core/queue.h @@ -17,9 +17,6 @@ struct queue; struct queue_ops { /* Called to tell a higher layer that a queue has been initialized. */ void *(*qop_init)(struct queue *, void *); - - /* Called to tell a higher layer that a queue is deinitializing. */ - void (*qop_deinit)(struct queue *); }; @@ -33,7 +30,4 @@ struct queue { /* Called to initialize a queue. */ void queue_init(struct queue *, const struct queue_ops *, void *); -/* Called to deinitialize a queue. */ -void queue_deinit(struct queue *); - #endif /* OCARINA_CORE_QUEUE_H */ diff --git a/include/gui/filter.h b/include/gui/filter.h index bcccbb3f..f7722085 100644 --- a/include/gui/filter.h +++ b/include/gui/filter.h @@ -20,9 +20,6 @@ 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 *); diff --git a/tests/core/playlist.c b/tests/core/playlist.c index cfdb4544..61b8188d 100644 --- a/tests/core/playlist.c +++ b/tests/core/playlist.c @@ -69,6 +69,8 @@ static void test_null() playlist_generic_resort(NULL); playlist_clear_sort(NULL); + playlist_set_search(NULL, NULL); + g_assert_false(playlist_generic_add(NULL, NULL)); g_assert_false(playlist_generic_add_front(NULL, NULL)); g_assert_false(playlist_generic_remove(NULL, NULL)); @@ -169,11 +171,19 @@ static void test_playlist() g_assert_cmpuint(p.pl_length, ==, 0); g_assert_null(p.pl_current); + /* Set search text. */ + playlist_set_search(&p, "test"); + g_assert_cmpstr(p.pl_search[0], ==, "test"); + playlist_set_search(&p, "Test Text"); + g_assert_cmpstr(p.pl_search[0], ==, "test"); + g_assert_cmpstr(p.pl_search[1], ==, "text"); + /* Deinitialize the playlist. */ playlist_generic_deinit(&p); g_assert_cmpuint(playlist_size(&p), ==, 0); g_assert_cmpuint(p.pl_length, ==, 0); g_assert_null(p.pl_current); + g_assert_null(p.pl_search); g_assert_null(p.pl_sort); } diff --git a/tests/core/queue.c b/tests/core/queue.c index 0833ca3c..a85440eb 100644 --- a/tests/core/queue.c +++ b/tests/core/queue.c @@ -8,7 +8,6 @@ unsigned int count_init = 0; -unsigned int count_deinit = 0; static void *queue_op_init(struct queue *queue, void *data) @@ -17,15 +16,9 @@ static void *queue_op_init(struct queue *queue, void *data) return GUINT_TO_POINTER(count_init); } -static void queue_op_deinit(struct queue *queue) -{ - count_deinit++; -} - static const struct queue_ops test_ops = { .qop_init = queue_op_init, - .qop_deinit = queue_op_deinit, }; @@ -39,17 +32,11 @@ static void test_init() g_assert_null(q.q_ops); - queue_deinit(&q); - g_assert_cmpuint(count_deinit, ==, 0); - queue_init(&q, &test_ops, NULL); g_assert_cmpuint(count_init, ==, 1); g_assert_cmpuint(GPOINTER_TO_UINT(q.q_private), ==, 1); g_assert(q.q_ops == &test_ops); - - queue_deinit(&q); - g_assert_cmpuint(count_deinit, ==, 1); } int main(int argc, char **argv) diff --git a/tests/gui/filter.c b/tests/gui/filter.c index fbe4fada..9db2abb9 100644 --- a/tests/gui/filter.c +++ b/tests/gui/filter.c @@ -10,12 +10,9 @@ 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); } struct queue_ops test_ops = { .qop_init = test_queue_init, - .qop_deinit = test_queue_deinit, }; void test_on_load(struct track *track) {} diff --git a/tests/gui/model.c b/tests/gui/model.c index 1dab9dd7..f99abcc4 100644 --- a/tests/gui/model.c +++ b/tests/gui/model.c @@ -25,15 +25,12 @@ void on_row_changed(GtkTreeModel *model, GtkTreePath *path, void *test_queue_init(struct queue *queue, void *data) { return NULL; } -void test_queue_deinit(struct queue *queue) - { } void test_on_load(struct track *track) {} void test_on_state_change(GstState state) {} void test_on_config_pause(int count) {} struct queue_ops test_ops = { .qop_init = test_queue_init, - .qop_deinit = test_queue_deinit, }; struct playlist_callbacks test_cb = {