From 010969c7b3d6f4b3c67cb29bc0c3e7a1d90e10c7 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Sat, 20 Aug 2016 21:01:19 -0400 Subject: [PATCH] gui/model: Convert set_queue() -> set_playlist() Ocarina is moving in a playlist oriented direction, so move away from using the queue directly in favor of using the playlist. Signed-off-by: Anna Schumaker --- gui/model.c | 59 ++++++++++++++++++++++----------------------- gui/playlist.c | 13 +++++++--- gui/queue.c | 6 ++--- gui/view.c | 19 +++++++++------ include/gui/model.h | 15 ++++++------ include/gui/view.h | 2 +- tests/gui/model.c | 37 +++++++++++++++++----------- tests/gui/queue.c | 18 ++++++++++---- tests/gui/view.c | 4 +-- 9 files changed, 98 insertions(+), 75 deletions(-) diff --git a/gui/model.c b/gui/model.c index 5d4d2f05..f68f0cf5 100644 --- a/gui/model.c +++ b/gui/model.c @@ -2,7 +2,7 @@ * Copyright 2016 (c) Anna Schumaker. */ #include -#include +#include #include #include #include @@ -10,9 +10,10 @@ static gboolean __gui_model_iter_nth_child(GtkTreeModel *, GtkTreeIter *, GtkTreeIter *, gint); -static GObjectClass *parent_class = NULL; -static GuiModel *queue_model = NULL; -static GType gui_model_type = 0; +static struct playlist *cur_playlist = NULL; +static GObjectClass *parent_class = NULL; +static GuiModel *queue_model = NULL; +static GType gui_model_type = 0; static GType gui_model_columns[GUI_MODEL_N_COLUMNS] = { [GUI_MODEL_TRACK_NR] = G_TYPE_UINT, @@ -155,9 +156,9 @@ static gboolean __gui_model_iter_has_child(GtkTreeModel *model, GtkTreeIter *ite static gint __gui_model_iter_n_children(GtkTreeModel *model, GtkTreeIter *iter) { - if (iter != NULL || !queue_model->gm_queue) + if (iter != NULL || !cur_playlist) return 0; - return queue_size(GUI_MODEL(model)->gm_queue); + return queue_size(&cur_playlist->pl_queue); } static gboolean __gui_model_iter_nth_child(GtkTreeModel *model, @@ -165,11 +166,11 @@ static gboolean __gui_model_iter_nth_child(GtkTreeModel *model, GtkTreeIter *parent, gint n) { - if (parent || !queue_model->gm_queue || - n >= queue_size(queue_model->gm_queue)) + if (parent || !cur_playlist || + n >= queue_size(&cur_playlist->pl_queue)) return FALSE; - queue_iter_set(queue_model->gm_queue, &queue_model->gm_iter, n); + queue_iter_set(&cur_playlist->pl_queue, &queue_model->gm_iter, n); iter->stamp = queue_model->gm_stamp; iter->user_data = &queue_model->gm_iter; iter->user_data2 = queue_iter_val(&queue_model->gm_iter); @@ -245,7 +246,6 @@ void gui_model_init(void) queue_model = g_object_new(gui_model_type, NULL); g_assert(queue_model != NULL); - queue_model->gm_queue = NULL; } void gui_model_deinit(void) @@ -253,14 +253,15 @@ void gui_model_deinit(void) g_object_unref(queue_model); gui_model_type = 0; queue_model = NULL; + cur_playlist = NULL; } static void __gui_model_set_runtime(void) { gchar *len = NULL; - if (queue_model->gm_queue) - len = string_sec2str_long(queue_model->gm_queue->q_length); + if (cur_playlist) + len = string_sec2str_long(cur_playlist->pl_queue.q_length); gtk_label_set_text(GTK_LABEL(gui_builder_widget("o_runtime")), len); g_free(len); @@ -276,12 +277,12 @@ GType gui_model_get_type() return gui_model_type; } -void gui_model_add(struct queue *queue, unsigned int row) +void gui_model_add(struct playlist *playlist, unsigned int row) { GtkTreePath *path; GtkTreeIter iter; - if (!queue_model || queue != queue_model->gm_queue) + if (cur_playlist != playlist) return; path = gtk_tree_path_new_from_indices(row, -1); @@ -291,11 +292,11 @@ void gui_model_add(struct queue *queue, unsigned int row) gtk_tree_path_free(path); } -void gui_model_remove(struct queue *queue, unsigned int row) +void gui_model_remove(struct playlist *playlist, unsigned int row) { GtkTreePath *path; - if (queue != queue_model->gm_queue) + if (cur_playlist != playlist) return; path = gtk_tree_path_new_from_indices(row, -1); @@ -304,12 +305,12 @@ void gui_model_remove(struct queue *queue, unsigned int row) gtk_tree_path_free(path); } -void gui_model_clear(struct queue *queue, unsigned int n) +void gui_model_clear(struct playlist *playlist, unsigned int n) { GtkTreePath *path; unsigned int i; - if (!queue_model || queue != queue_model->gm_queue) + if (!queue_model || cur_playlist != playlist) return; path = gtk_tree_path_new_from_indices(n - 1, -1); @@ -322,12 +323,12 @@ void gui_model_clear(struct queue *queue, unsigned int n) gtk_tree_path_free(path); } -void gui_model_update(struct queue *queue, unsigned int row) +void gui_model_update(struct playlist *playlist, unsigned int row) { GtkTreePath *path; GtkTreeIter iter; - if (queue != queue_model->gm_queue) + if (cur_playlist != playlist) return; path = gtk_tree_path_new_from_indices(row, -1); @@ -337,23 +338,21 @@ void gui_model_update(struct queue *queue, unsigned int row) gtk_tree_path_free(path); } -void gui_model_set_queue(struct queue *queue) +void gui_model_set_playlist(struct playlist *playlist) { - struct queue *cur = queue_model->gm_queue; + if (cur_playlist) + gui_model_clear(cur_playlist, queue_size(&cur_playlist->pl_queue)); - if (cur) - gui_model_clear(cur, queue_size(cur)); - - queue_model->gm_queue = queue; + cur_playlist = playlist; __gui_model_set_runtime(); - if (queue && queue_size(queue) > 0) - gui_model_add(queue, 0); + if (playlist && queue_size(&playlist->pl_queue) > 0) + gui_model_add(playlist, 0); } -struct queue *gui_model_get_queue(void) +struct playlist *gui_model_get_playlist(void) { - return queue_model ? queue_model->gm_queue : NULL; + return cur_playlist; } struct track * gui_model_path_get_track(GtkTreePath *path) diff --git a/gui/playlist.c b/gui/playlist.c index fecb87b6..de04ec91 100644 --- a/gui/playlist.c +++ b/gui/playlist.c @@ -259,22 +259,27 @@ static void *__playlist_init(struct queue *queue, void *data) static void __playlist_added(struct queue *queue, unsigned int row) { - gui_model_add(queue, row); + gui_model_add(gui_queue(queue)->gq_playlist, row); __playlist_update_sizes(queue); } static void __playlist_removed(struct queue *queue, unsigned int row) { - gui_model_remove(queue, row); + gui_model_remove(gui_queue(queue)->gq_playlist, row); __playlist_update_sizes(queue); } static void __playlist_cleared(struct queue *queue, unsigned int n) { - gui_model_clear(queue, n); + gui_model_clear(gui_queue(queue)->gq_playlist, n); __playlist_update_sizes(queue); } +static void __playlist_updated(struct queue *queue, unsigned int n) +{ + gui_model_update(gui_queue(queue)->gq_playlist, n); +} + static bool __playlist_erase(struct queue *queue, struct track *track) { enum playlist_type_t type = gui_queue(queue)->gq_playlist->pl_type; @@ -481,5 +486,5 @@ struct queue_ops playlist_ops = { .qop_erase = __playlist_erase, .qop_removed = __playlist_removed, .qop_cleared = __playlist_cleared, - .qop_updated = gui_model_update, + .qop_updated = __playlist_updated, }; diff --git a/gui/queue.c b/gui/queue.c index b4733016..9360fd44 100644 --- a/gui/queue.c +++ b/gui/queue.c @@ -140,7 +140,7 @@ void gui_queue_free(struct queue *queue) queue->q_private = NULL; if (gq_queue == gq) - gui_view_set_queue(NULL); + gui_view_set_playlist(NULL); if (gq->gq_search) g_strfreev(gq->gq_search); g_free(gq->gq_text); @@ -160,9 +160,9 @@ void gui_queue_show(struct gui_queue *queue) if (queue) { has_random = queue_has_flag(queue->gq_queue, Q_RANDOM); - gui_view_set_queue(queue->gq_queue); + gui_view_set_playlist(queue->gq_playlist); } else - gui_view_set_queue(NULL); + 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); diff --git a/gui/view.c b/gui/view.c index 2161d4a9..8212e9fe 100644 --- a/gui/view.c +++ b/gui/view.c @@ -95,8 +95,8 @@ static int __view_dec_sort(gpointer data) static void __view_set_column_sort_indicator(GtkTreeViewColumn *col, unsigned int index) { - struct queue *queue = gui_model_get_queue(); - GSList *cur = queue ? queue->q_sort : NULL; + struct playlist *playlist = gui_model_get_playlist(); + GSList *cur = playlist ? playlist->pl_queue.q_sort : NULL; unsigned int order = GTK_SORT_ASCENDING; bool show = false; int field; @@ -135,7 +135,7 @@ void __view_row_activated(GtkTreeView *treeview, GtkTreePath *path, { view_no_scroll = true; audio_load(__view_filter_get_track(path)); - queue_selected(gui_model_get_queue(), + queue_selected(&gui_model_get_playlist()->pl_queue, gtk_tree_path_get_indices(path)[0]); view_no_scroll = false; } @@ -150,7 +150,8 @@ void __view_column_resized(GtkTreeViewColumn *col, GParamSpec *pspec, void __view_column_clicked(GtkTreeViewColumn *col, gpointer data) { - struct queue *queue = gui_model_get_queue(); + struct playlist *playlist = gui_model_get_playlist(); + struct queue *queue = playlist ? &playlist->pl_queue : NULL; unsigned int index = __view_get_column_index(col); bool reset = view_sort_count == 0; gchar *text; @@ -190,7 +191,8 @@ static void __view_add_to_playlist(GtkTreeModel *model, GtkTreePath *path, static void __view_delete_selection(GtkTreeSelection *selection) { - struct queue *queue = gui_model_get_queue(); + struct playlist *playlist = gui_model_get_playlist(); + struct queue *queue = playlist ? &playlist->pl_queue : NULL; GList *rows = gtk_tree_selection_get_selected_rows(selection, NULL); GList *cur = g_list_reverse(rows); @@ -422,9 +424,9 @@ GtkTreeModelFilter *gui_view_get_filter(void) return view_filter; } -void gui_view_set_queue(struct queue *queue) +void gui_view_set_playlist(struct playlist *playlist) { - gui_model_set_queue(queue); + gui_model_set_playlist(playlist); view_sort_count = 0; __view_display_sorting(""); @@ -435,7 +437,8 @@ void gui_view_set_queue(struct queue *queue) void gui_view_scroll() { - struct queue *queue = gui_model_get_queue(); + struct playlist *playlist = gui_model_get_playlist(); + struct queue *queue = playlist ? &playlist->pl_queue : NULL; GtkTreePath *real, *path; if (!queue || (int)queue->q_cur.it_pos < 0 || view_no_scroll) diff --git a/include/gui/model.h b/include/gui/model.h index c6ea36c6..d01cda85 100644 --- a/include/gui/model.h +++ b/include/gui/model.h @@ -3,7 +3,7 @@ */ #ifndef OCARINA_GUI_MODEL_H #define OCARINA_GUI_MODEL_H -#include +#include #include #define GUI_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), \ @@ -29,7 +29,6 @@ struct gui_model { GObject gm_parent; /* This MUST be the first member. */ gint gm_stamp; /* This is used to check iter validity. */ - struct queue *gm_queue; /* The model's associated queue. */ struct queue_iter gm_iter; /* The current _q_iter. */ }; typedef struct gui_model GuiModel; @@ -54,22 +53,22 @@ GuiModel *gui_model_get(void); GType gui_model_get_type(); /* Called to add a row to the model */ -void gui_model_add(struct queue *, unsigned int); +void gui_model_add(struct playlist *, unsigned int); /* Called to remove a row from the model */ -void gui_model_remove(struct queue *, unsigned int); +void gui_model_remove(struct playlist *, unsigned int); /* Called to remove all rows from the model */ -void gui_model_clear(struct queue *, unsigned int); +void gui_model_clear(struct playlist *, unsigned int); /* Called to update a row in the model */ -void gui_model_update(struct queue *, unsigned int); +void gui_model_update(struct playlist *, unsigned int); /* Called to change the queue represented by the model. */ -void gui_model_set_queue(struct queue *); +void gui_model_set_playlist(struct playlist *); /* Called to get the queue currently attached to the model. */ -struct queue *gui_model_get_queue(void); +struct playlist *gui_model_get_playlist(void); /* Called to convert a GtkTreeIter into a struct track */ diff --git a/include/gui/view.h b/include/gui/view.h index 7fdf7004..a21d8a3e 100644 --- a/include/gui/view.h +++ b/include/gui/view.h @@ -16,6 +16,6 @@ void gui_view_scroll(); GtkTreeModelFilter *gui_view_get_filter(void); /* Called to set the currently displayed model. */ -void gui_view_set_queue(struct queue *); +void gui_view_set_playlist(struct playlist *); #endif /* OCARINA_GUI_VIEW_H */ diff --git a/tests/gui/model.c b/tests/gui/model.c index d9459e5d..e2c002c1 100644 --- a/tests/gui/model.c +++ b/tests/gui/model.c @@ -27,7 +27,15 @@ void *test_queue_init(struct queue *queue, void *data) { return NULL; } void test_queue_deinit(struct queue *queue) { } +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) + { gui_model_remove(queue->q_private, n); } +void test_queue_clear(struct queue *queue, unsigned int n) + { gui_model_clear(queue->q_private, n); } void test_queue_save(struct queue *queue, enum queue_flags flag) {} +void test_queue_update(struct queue *queue, unsigned int n) + { gui_model_update(queue->q_private, n); } void test_on_load(struct track *track) {} void test_on_state_change(GstState state) {} void test_on_config_pause(int count) {} @@ -35,11 +43,11 @@ void test_on_config_pause(int count) {} struct queue_ops test_ops = { .qop_init = test_queue_init, .qop_deinit = test_queue_deinit, - .qop_added = gui_model_add, - .qop_removed = gui_model_remove, - .qop_cleared = gui_model_clear, + .qop_added = test_queue_add, + .qop_removed = test_queue_remove, + .qop_cleared = test_queue_clear, .qop_save = test_queue_save, - .qop_updated = gui_model_update, + .qop_updated = test_queue_update, }; struct audio_ops test_audio_ops = { @@ -59,11 +67,11 @@ static void test_init() GtkTreeModel *treemodel = GTK_TREE_MODEL(model); GType type; - g_assert_null(gui_model_get_queue()); - gui_model_set_queue(playlist_get_queue(PL_SYSTEM, "Collection")); + g_assert_null(gui_model_get_playlist()); + gui_model_set_playlist(playlist_get(PL_SYSTEM, "Collection")); g_assert_nonnull(model); g_assert_true(GTK_IS_TREE_MODEL(model)); - g_assert(gui_model_get_queue() == playlist_get_queue(PL_SYSTEM, "Collection")); + g_assert(gui_model_get_playlist() == playlist_get(PL_SYSTEM, "Collection")); g_assert_cmpuint(gtk_tree_model_get_flags(treemodel), ==, GTK_TREE_MODEL_LIST_ONLY); @@ -100,7 +108,7 @@ static void __test_empty_subprocess() GValue value; GType type; - gui_model_set_queue(playlist_get_queue(PL_SYSTEM, "Collection")); + gui_model_set_playlist(playlist_get(PL_SYSTEM, "Collection")); memset(&value, 0, sizeof(GValue)); g_assert_false(gtk_tree_model_get_iter_first(model, &iter)); @@ -164,7 +172,7 @@ static void test_model() GtkTreeIter iter; GValue value; - gui_model_set_queue(playlist_get_queue(PL_SYSTEM, "Collection")); + gui_model_set_playlist(playlist_get(PL_SYSTEM, "Collection")); g_assert_cmpstr(gtk_label_get_text(label), ==, ""); memset(&value, 0, sizeof(GValue)); @@ -173,7 +181,8 @@ static void test_model() g_signal_connect(model, "row-changed", (GCallback)on_row_changed, NULL); /* Okay, now scan a directory ... */ - playlist_get_queue(PL_SYSTEM, "Collection")->q_private = model; + playlist_get_queue(PL_SYSTEM, "Collection")->q_private = + playlist_get(PL_SYSTEM, "Collection"); playlist_new(PL_LIBRARY, "tests/Music/Hyrule Symphony"); while (idle_run_task() == true) {} g_assert_cmpuint(playlist_size(PL_SYSTEM, "Collection"), ==, 13); @@ -248,19 +257,19 @@ static void test_model() g_assert_cmpuint(gtk_tree_model_iter_n_children(model, &iter), ==, 0); g_assert_cmpuint(gtk_tree_model_iter_n_children(model, NULL), ==, 13); - gui_model_set_queue(playlist_get_queue(PL_SYSTEM, "Favorites")); + gui_model_set_playlist(playlist_get(PL_SYSTEM, "Favorites")); g_assert_cmpuint(count_delete, ==, 13); g_assert_cmpuint(count_insert, ==, 14); g_assert_cmpuint(gtk_tree_model_iter_n_children(model, NULL), ==, 3); g_assert_cmpstr(gtk_label_get_text(label), ==, "10 minutes, 46 seconds"); - gui_model_set_queue(NULL); + gui_model_set_playlist(NULL); g_assert_cmpuint(count_delete, ==, 16); g_assert_cmpuint(count_insert, ==, 14); g_assert_cmpuint(gtk_tree_model_iter_n_children(model, NULL), ==, 0); g_assert_cmpstr(gtk_label_get_text(label), ==, ""); - gui_model_set_queue(playlist_get_queue(PL_SYSTEM, "Collection")); + gui_model_set_playlist(playlist_get(PL_SYSTEM, "Collection")); g_assert_cmpuint(count_delete, ==, 16); g_assert_cmpuint(count_insert, ==, 15); g_assert_cmpstr(gtk_label_get_text(label), ==, "42 minutes, 45 seconds"); @@ -297,7 +306,7 @@ int main(int argc, char **argv) core_deinit(); g_assert_false(G_IS_OBJECT(gui_model_get())); - g_assert_null(gui_model_get_queue()); + g_assert_null(gui_model_get_playlist()); g_assert_cmpuint(gui_model_get_type(), ==, 0); return ret; } diff --git a/tests/gui/queue.c b/tests/gui/queue.c index 2146765b..bb8a1b2f 100644 --- a/tests/gui/queue.c +++ b/tests/gui/queue.c @@ -11,19 +11,27 @@ static void *test_queue_init(struct queue *queue, void *data) { - return gui_queue_alloc(NULL, queue, "Test Queue", GQ_CAN_RANDOM); + return gui_queue_alloc(data, queue, "Test Queue", GQ_CAN_RANDOM); } +void test_queue_add(struct queue *queue, unsigned int n) + { gui_model_add(gui_queue(queue)->gq_playlist, n); } +void test_queue_remove(struct queue *queue, unsigned int n) + { gui_model_remove(gui_queue(queue)->gq_playlist, n); } +void test_queue_clear(struct queue *queue, unsigned int n) + { gui_model_clear(gui_queue(queue)->gq_playlist, n); } static void test_queue_save(struct queue *queue, unsigned int row) {} +void test_queue_update(struct queue *queue, unsigned int n) + { gui_model_update(gui_queue(queue)->gq_playlist, n); } static struct queue_ops test_ops = { .qop_init = test_queue_init, .qop_deinit = gui_queue_free, - .qop_cleared = gui_model_clear, - .qop_added = gui_model_add, - .qop_removed = gui_model_remove, + .qop_cleared = test_queue_clear, + .qop_added = test_queue_add, + .qop_removed = test_queue_remove, .qop_save = test_queue_save, - .qop_updated = gui_model_update, + .qop_updated = test_queue_update, }; struct core_init_data init_data = { diff --git a/tests/gui/view.c b/tests/gui/view.c index b7d98fff..10ae75ea 100644 --- a/tests/gui/view.c +++ b/tests/gui/view.c @@ -53,7 +53,7 @@ static void test_treeview() playlist_new(PL_LIBRARY, "tests/Music/Hyrule Symphony"); while (idle_run_task() == true) {} - gui_model_set_queue(playlist_get_queue(PL_SYSTEM, "Collection")); + gui_model_set_playlist(playlist_get(PL_SYSTEM, "Collection")); filter = GTK_TREE_MODEL(gui_view_get_filter()); treeview = GTK_TREE_VIEW(gui_builder_widget("o_treeview")); @@ -98,7 +98,7 @@ static void test_treeview() g_assert_cmpuint(load_count, ==, 1); gtk_tree_path_free(path); - gui_view_set_queue(NULL); + gui_view_set_playlist(NULL); gui_test_deinit(); }