From a28b5c4ec566a0c191741444059e5635d30afbae Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 2 Aug 2016 10:13:36 -0400 Subject: [PATCH] gui/model: Create a static, shared GuiQueueModel Now that we can switch between different queues we no longer need to allocate multiple models. Implements #72: Only allocate a single GuiQueueModel for all playlists Signed-off-by: Anna Schumaker --- gui/model.c | 19 ++++++++++++++----- gui/ocarina.c | 4 +++- gui/queue.c | 5 +++-- include/gui/model.h | 10 ++++++++-- tests/gui/model.c | 20 ++++++++++++++------ tests/gui/queue.c | 1 + tests/gui/view.c | 5 ++++- 7 files changed, 47 insertions(+), 17 deletions(-) diff --git a/gui/model.c b/gui/model.c index 2ff4f20b..214f5b21 100644 --- a/gui/model.c +++ b/gui/model.c @@ -9,6 +9,7 @@ static const GTypeInfo queue_type_info; static const GInterfaceInfo queue_tree_model; static GObjectClass *parent_class = NULL; +static GuiQueueModel *queue_model = NULL; static gchar *font_current = "bold"; static gchar *font_regular = ""; @@ -232,14 +233,22 @@ static void _queue_tree_model_init(GtkTreeModelIface *iface) } -GuiQueueModel *gui_queue_model_new(struct queue *queue) +void gui_queue_model_init(void) { - GuiQueueModel *model = g_object_new(GUI_QUEUE_MODEL_TYPE, NULL); + queue_model = g_object_new(GUI_QUEUE_MODEL_TYPE, NULL); + g_assert(queue_model != NULL); + queue_model->gqm_queue = NULL; +} - g_assert(model != NULL); - model->gqm_queue = queue; +void gui_queue_model_deinit(void) +{ + g_object_unref(queue_model); + queue_model = NULL; +} - return model; +GuiQueueModel *gui_queue_model_get(void) +{ + return queue_model; } GType gui_queue_model_get_type() diff --git a/gui/ocarina.c b/gui/ocarina.c index 5754d3ea..c887a4d2 100644 --- a/gui/ocarina.c +++ b/gui/ocarina.c @@ -53,8 +53,9 @@ static void __ocarina_startup(GApplication *application, gpointer data) gchar *icon = find_file_path("ocarina.png"); gui_builder_init(ui); - core_init(&startup_argc, &startup_argv, &init_data); gui_settings_init(); + gui_queue_model_init(); + core_init(&startup_argc, &startup_argv, &init_data); gui_view_init(); gui_window_init(icon); gui_sidebar_init(); @@ -74,6 +75,7 @@ static void __ocarina_shutdown(GApplication *application, gpointer data) core_deinit(); gui_window_deinit(); + gui_queue_model_deinit(); gui_settings_deinit(); gui_builder_deinit(); } diff --git a/gui/queue.c b/gui/queue.c index 1f378a51..21c03f63 100644 --- a/gui/queue.c +++ b/gui/queue.c @@ -151,12 +151,12 @@ struct gui_queue *gui_queue_alloc(struct playlist *playlist, struct queue *queue gq->gq_flags = flags; gq->gq_text = g_strdup(text); gq->gq_search = NULL; - gq->gq_model = gui_queue_model_new(queue); + gq->gq_model = g_object_ref(gui_queue_model_get()); + gui_queue_model_set_queue(gq->gq_model, queue); gq->gq_filter = gtk_tree_model_filter_new( GTK_TREE_MODEL(gq->gq_model), NULL); gq->gq_playlist = playlist; gq->gq_queue = queue; - gq->gq_search = NULL; gtk_tree_model_filter_set_visible_func( GTK_TREE_MODEL_FILTER(gq->gq_filter), @@ -202,6 +202,7 @@ void gui_queue_show(struct gui_queue *queue) has_random = queue_has_flag(queue->gq_queue, Q_RANDOM); has_repeat = queue_has_flag(queue->gq_queue, Q_REPEAT); is_enabled = queue_has_flag(queue->gq_queue, Q_ENABLED); + gui_queue_model_set_queue(queue->gq_model, queue->gq_queue); gui_view_set_model(GTK_TREE_MODEL_FILTER(queue->gq_filter)); __queue_set_runtime(queue); } else diff --git a/include/gui/model.h b/include/gui/model.h index 3bd0aa48..cbcf619c 100644 --- a/include/gui/model.h +++ b/include/gui/model.h @@ -42,8 +42,14 @@ struct _gui_queue_model_class { }; -/* Called to allocate a new GuiQueueModel */ -GuiQueueModel *gui_queue_model_new(struct queue *); +/* Called to initialize the GuiQueueModel */ +void gui_queue_model_init(void); + +/* Called to deinitialize the GuiQueueModel */ +void gui_queue_model_deinit(void); + +/* Called to get the GuiQueueModel */ +GuiQueueModel *gui_queue_model_get(void); /* Called to find the GType of the GuiQueueModel */ GType gui_queue_model_get_type(); diff --git a/tests/gui/model.c b/tests/gui/model.c index 5ec4584d..7b6ced0e 100644 --- a/tests/gui/model.c +++ b/tests/gui/model.c @@ -68,7 +68,9 @@ static void test_init() GuiQueueModel *model; GType type; - model = gui_queue_model_new(playlist_get_queue(PL_SYSTEM, "Collection")); + gui_queue_model_init(); + model = gui_queue_model_get(); + gui_queue_model_set_queue(model, playlist_get_queue(PL_SYSTEM, "Collection")); test_not_equal((void *)model, NULL); test_equal(GTK_IS_TREE_MODEL(model), true); test_equal((void *)model->gqm_queue, (void *)playlist_get_queue(PL_SYSTEM, "Collection")); @@ -104,7 +106,7 @@ static void test_init() type = gtk_tree_model_get_column_type(treemodel, Q_MODEL_N_COLUMNS); test_equal(type, G_TYPE_INVALID); - g_object_unref(model); + gui_queue_model_deinit(); test_equal(G_IS_OBJECT(model), false); } @@ -114,7 +116,10 @@ static void test_empty() GtkTreeIter iter; GValue value; - model = GTK_TREE_MODEL(gui_queue_model_new(playlist_get_queue(PL_SYSTEM, "Collection"))); + gui_queue_model_init(); + model = GTK_TREE_MODEL(gui_queue_model_get()); + gui_queue_model_set_queue(GUI_QUEUE_MODEL(model), + playlist_get_queue(PL_SYSTEM, "Collection")); memset(&value, 0, sizeof(GValue)); test_equal(gtk_tree_model_get_iter_first(model, &iter), false); @@ -154,7 +159,7 @@ static void test_empty() test_equal(gtk_tree_model_iter_nth_child(model, &iter, NULL, 3), false); test_equal(gtk_tree_model_iter_parent(model, &iter, &iter), false); - g_object_unref(model); + gui_queue_model_deinit(); } static void test_model() @@ -168,9 +173,12 @@ static void test_model() int argc = 0; core_init(&argc, NULL, &init_data); + gui_queue_model_init(); while (idle_run_task() == true) {} - model = GTK_TREE_MODEL(gui_queue_model_new(playlist_get_queue(PL_SYSTEM, "Collection"))); + model = GTK_TREE_MODEL(gui_queue_model_get()); + gui_queue_model_set_queue(GUI_QUEUE_MODEL(model), + playlist_get_queue(PL_SYSTEM, "Collection")); memset(&value, 0, sizeof(GValue)); g_signal_connect(model, "row-inserted", (GCallback)on_row_inserted, NULL); @@ -274,7 +282,7 @@ static void test_model() test_equal(count_delete, 29); core_deinit(); - g_object_unref(model); + gui_queue_model_deinit(); } DECLARE_UNIT_TESTS( diff --git a/tests/gui/queue.c b/tests/gui/queue.c index 52786e74..1c639adf 100644 --- a/tests/gui/queue.c +++ b/tests/gui/queue.c @@ -50,6 +50,7 @@ static void test_queue() gtk_init(&argc, NULL); gui_builder_init("share/ocarina/ocarina6.glade"); + gui_queue_model_init(); gui_view_init(); while (idle_run_task()) {}; diff --git a/tests/gui/view.c b/tests/gui/view.c index f905b19c..c14d5303 100644 --- a/tests/gui/view.c +++ b/tests/gui/view.c @@ -69,12 +69,15 @@ static void test_treeview() core_init(&argc, NULL, &init_data); gui_builder_init("share/ocarina/ocarina6.glade"); gui_settings_init(); + gui_queue_model_init(); gui_view_init(); while (idle_run_task()) {} playlist_new(PL_LIBRARY, "tests/Music/Hyrule Symphony"); while (idle_run_task() == true) {} - model = GTK_TREE_MODEL(gui_queue_model_new(playlist_get_queue(PL_SYSTEM, "Collection"))); + model = GTK_TREE_MODEL(gui_queue_model_get()); + gui_queue_model_set_queue(GUI_QUEUE_MODEL(model), + playlist_get_queue(PL_SYSTEM, "Collection")); filter = gtk_tree_model_filter_new(model, NULL); treeview = GTK_TREE_VIEW(gui_builder_widget("o_treeview"));