gui/model: Various cleanups to code and test

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2016-08-20 12:22:56 -04:00
parent cdbe96c575
commit e522475d38
2 changed files with 60 additions and 77 deletions

View File

@ -7,12 +7,11 @@
#include <gui/builder.h> #include <gui/builder.h>
#include <gui/model.h> #include <gui/model.h>
static const GInterfaceInfo gui_tree_model; static gboolean __gui_model_iter_nth_child(GtkTreeModel *, GtkTreeIter *,
static const GTypeInfo gui_model_type_info; GtkTreeIter *, gint);
static GObjectClass *parent_class = NULL; static GObjectClass *parent_class = NULL;
static GuiModel *queue_model = NULL; static GuiModel *queue_model = NULL;
static gchar *font_current = "bold";
static gchar *font_regular = "";
static GType gui_model_columns[GUI_MODEL_N_COLUMNS] = { static GType gui_model_columns[GUI_MODEL_N_COLUMNS] = {
[GUI_MODEL_TRACK_NR] = G_TYPE_UINT, [GUI_MODEL_TRACK_NR] = G_TYPE_UINT,
@ -28,19 +27,6 @@ static GType gui_model_columns[GUI_MODEL_N_COLUMNS] = {
[GUI_MODEL_FONT] = G_TYPE_STRING, [GUI_MODEL_FONT] = G_TYPE_STRING,
}; };
static gboolean __gui_model_iter_nth(GuiModel *model, GtkTreeIter *iter, gint n)
{
if (!model->gm_queue || n >= queue_size(model->gm_queue))
return FALSE;
queue_iter_set(model->gm_queue, &model->gm_iter, n);
iter->stamp = model->gm_stamp;
iter->user_data = &model->gm_iter;
iter->user_data2 = queue_iter_val(&model->gm_iter);
return TRUE;
}
static GtkTreeModelFlags __gui_model_get_flags(GtkTreeModel *model) static GtkTreeModelFlags __gui_model_get_flags(GtkTreeModel *model)
{ {
return GTK_TREE_MODEL_LIST_ONLY; return GTK_TREE_MODEL_LIST_ONLY;
@ -53,8 +39,8 @@ static gint __gui_model_get_n_columns(GtkTreeModel *model)
static GType __gui_model_get_column_type(GtkTreeModel *model, gint index) static GType __gui_model_get_column_type(GtkTreeModel *model, gint index)
{ {
g_return_val_if_fail(index >= 0 && index < GUI_MODEL_N_COLUMNS, g_return_val_if_fail(index >= 0, G_TYPE_INVALID);
G_TYPE_INVALID); g_return_val_if_fail(index < GUI_MODEL_N_COLUMNS, G_TYPE_INVALID);
return gui_model_columns[index]; return gui_model_columns[index];
} }
@ -67,7 +53,7 @@ static gboolean __gui_model_get_iter(GtkTreeModel *model, GtkTreeIter *iter,
indices = gtk_tree_path_get_indices_with_depth(path, &depth); indices = gtk_tree_path_get_indices_with_depth(path, &depth);
g_assert(depth == 1); g_assert(depth == 1);
return __gui_model_iter_nth(GUI_MODEL(model), iter, indices[0]); return __gui_model_iter_nth_child(model, iter, NULL, indices[0]);
} }
static GtkTreePath *__gui_model_get_path(GtkTreeModel *model, GtkTreeIter *iter) static GtkTreePath *__gui_model_get_path(GtkTreeModel *model, GtkTreeIter *iter)
@ -131,8 +117,8 @@ static void __gui_model_get_value(GtkTreeModel *model, GtkTreeIter *iter,
g_free(str); g_free(str);
break; break;
case GUI_MODEL_FONT: case GUI_MODEL_FONT:
str = (track == audio_cur_track()) ? font_current : font_regular; str = (track == audio_cur_track()) ? "bold" : "";
g_value_set_static_string(value, str); g_value_take_string(value, g_strdup(str));
break; break;
} }
} }
@ -158,9 +144,7 @@ static gboolean __gui_model_iter_next(GtkTreeModel *model, GtkTreeIter *iter)
static gboolean __gui_model_iter_children(GtkTreeModel *model, GtkTreeIter *iter, static gboolean __gui_model_iter_children(GtkTreeModel *model, GtkTreeIter *iter,
GtkTreeIter *parent) GtkTreeIter *parent)
{ {
if (parent) return __gui_model_iter_nth_child(model, iter, parent, 0);
return FALSE;
return __gui_model_iter_nth(GUI_MODEL(model), iter, 0);
} }
static gboolean __gui_model_iter_has_child(GtkTreeModel *model, GtkTreeIter *iter) static gboolean __gui_model_iter_has_child(GtkTreeModel *model, GtkTreeIter *iter)
@ -180,9 +164,15 @@ static gboolean __gui_model_iter_nth_child(GtkTreeModel *model,
GtkTreeIter *parent, GtkTreeIter *parent,
gint n) gint n)
{ {
if (parent) if (parent || !queue_model->gm_queue ||
n >= queue_size(queue_model->gm_queue))
return FALSE; return FALSE;
return __gui_model_iter_nth(GUI_MODEL(model), iter, n);
queue_iter_set(queue_model->gm_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);
return TRUE;
} }
static gboolean __gui_model_iter_parent(GtkTreeModel *model, GtkTreeIter *iter, static gboolean __gui_model_iter_parent(GtkTreeModel *model, GtkTreeIter *iter,
@ -203,12 +193,10 @@ static void __gui_model_finalize(GObject *object)
static void __gui_model_class_init(GuiModelClass *class) static void __gui_model_class_init(GuiModelClass *class)
{ {
GObjectClass *object_class; GObjectClass *object_class = (GObjectClass *)class;
parent_class = g_type_class_peek_parent(class);
object_class = (GObjectClass *)class;
object_class->finalize = __gui_model_finalize; object_class->finalize = __gui_model_finalize;
parent_class = g_type_class_peek_parent(class);
} }
static void __gui_tree_model_init(GtkTreeModelIface *iface) static void __gui_tree_model_init(GtkTreeModelIface *iface)
@ -227,6 +215,24 @@ static void __gui_tree_model_init(GtkTreeModelIface *iface)
iface->iter_parent = __gui_model_iter_parent; iface->iter_parent = __gui_model_iter_parent;
} }
static const GTypeInfo gui_model_type_info = {
.class_size = sizeof(GuiModelClass),
.base_init = NULL,
.base_finalize = NULL,
.class_init = (GClassInitFunc)__gui_model_class_init,
.class_finalize = NULL,
.class_data = NULL,
.instance_size = sizeof(GuiModel),
.n_preallocs = 0,
.instance_init = (GInstanceInitFunc)__gui_model_init,
};
static const GInterfaceInfo gui_tree_model = {
.interface_init = (GInterfaceInitFunc)__gui_tree_model_init,
.interface_finalize = NULL,
.interface_data = NULL,
};
static void __gui_model_set_runtime(void) static void __gui_model_set_runtime(void)
{ {
@ -307,7 +313,7 @@ void gui_model_clear(struct queue *queue, unsigned int n)
GtkTreePath *path; GtkTreePath *path;
unsigned int i; unsigned int i;
if (queue != queue_model->gm_queue) if (!queue_model || queue != queue_model->gm_queue)
return; return;
path = gtk_tree_path_new_from_indices(n - 1, -1); path = gtk_tree_path_new_from_indices(n - 1, -1);
@ -351,7 +357,7 @@ void gui_model_set_queue(struct queue *queue)
struct queue *gui_model_get_queue(void) struct queue *gui_model_get_queue(void)
{ {
return queue_model->gm_queue; return queue_model ? queue_model->gm_queue : NULL;
} }
struct track * gui_model_path_get_track(GtkTreePath *path) struct track * gui_model_path_get_track(GtkTreePath *path)
@ -361,22 +367,3 @@ struct track * gui_model_path_get_track(GtkTreePath *path)
__gui_model_get_iter(GTK_TREE_MODEL(queue_model), &iter, path); __gui_model_get_iter(GTK_TREE_MODEL(queue_model), &iter, path);
return gui_model_iter_get_track(&iter); return gui_model_iter_get_track(&iter);
} }
static const GTypeInfo gui_model_type_info = {
.class_size = sizeof(GuiModelClass),
.base_init = NULL,
.base_finalize = NULL,
.class_init = (GClassInitFunc)__gui_model_class_init,
.class_finalize = NULL,
.class_data = NULL,
.instance_size = sizeof(GuiModel),
.n_preallocs = 0,
.instance_init = (GInstanceInitFunc)__gui_model_init,
};
static const GInterfaceInfo gui_tree_model = {
.interface_init = (GInterfaceInitFunc)__gui_tree_model_init,
.interface_finalize = NULL,
.interface_data = NULL,
};

View File

@ -55,20 +55,18 @@ struct core_init_data init_data = {
static void test_init() static void test_init()
{ {
GtkTreeModel *treemodel; GuiModel *model = gui_model_get();
GuiModel *model; GtkTreeModel *treemodel = GTK_TREE_MODEL(model);
GType type; GType type;
gui_model_init();
model = gui_model_get();
g_assert_null(gui_model_get_queue()); g_assert_null(gui_model_get_queue());
gui_model_set_queue(playlist_get_queue(PL_SYSTEM, "Collection")); gui_model_set_queue(playlist_get_queue(PL_SYSTEM, "Collection"));
g_assert_nonnull(model); g_assert_nonnull(model);
g_assert_true(GTK_IS_TREE_MODEL(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_queue() == playlist_get_queue(PL_SYSTEM, "Collection"));
treemodel = GTK_TREE_MODEL(model); g_assert_cmpuint(gtk_tree_model_get_flags(treemodel), ==,
g_assert_cmpuint(gtk_tree_model_get_flags(treemodel), ==, GTK_TREE_MODEL_LIST_ONLY); GTK_TREE_MODEL_LIST_ONLY);
g_assert_cmpuint(gtk_tree_model_get_n_columns(treemodel), ==, 11); g_assert_cmpuint(gtk_tree_model_get_n_columns(treemodel), ==, 11);
type = gtk_tree_model_get_column_type(treemodel, GUI_MODEL_TRACK_NR); type = gtk_tree_model_get_column_type(treemodel, GUI_MODEL_TRACK_NR);
@ -93,20 +91,15 @@ static void test_init()
g_assert_cmpuint(type, ==, G_TYPE_STRING); g_assert_cmpuint(type, ==, G_TYPE_STRING);
type = gtk_tree_model_get_column_type(treemodel, GUI_MODEL_FONT); type = gtk_tree_model_get_column_type(treemodel, GUI_MODEL_FONT);
g_assert_cmpuint(type, ==, G_TYPE_STRING); g_assert_cmpuint(type, ==, G_TYPE_STRING);
gui_model_deinit();
g_assert_false(G_IS_OBJECT(model));
} }
static void __test_empty_subprocess() static void __test_empty_subprocess()
{ {
GtkTreeModel *model; GtkTreeModel *model = GTK_TREE_MODEL(gui_model_get());
GtkTreeIter iter; GtkTreeIter iter;
GValue value; GValue value;
GType type; GType type;
gui_model_init();
model = GTK_TREE_MODEL(gui_model_get());
gui_model_set_queue(playlist_get_queue(PL_SYSTEM, "Collection")); gui_model_set_queue(playlist_get_queue(PL_SYSTEM, "Collection"));
memset(&value, 0, sizeof(GValue)); memset(&value, 0, sizeof(GValue));
@ -148,8 +141,6 @@ static void __test_empty_subprocess()
g_assert_cmpuint(gtk_tree_model_iter_n_children(model, NULL), ==, 0); g_assert_cmpuint(gtk_tree_model_iter_n_children(model, NULL), ==, 0);
g_assert_false(gtk_tree_model_iter_nth_child(model, &iter, NULL, 3)); g_assert_false(gtk_tree_model_iter_nth_child(model, &iter, NULL, 3));
g_assert_false(gtk_tree_model_iter_parent(model, &iter, &iter)); g_assert_false(gtk_tree_model_iter_parent(model, &iter, &iter));
gui_model_deinit();
} }
static void test_empty() static void test_empty()
@ -165,16 +156,14 @@ static void test_empty()
static void test_model() static void test_model()
{ {
GtkLabel *label = GTK_LABEL(gui_builder_widget("o_runtime"));
GtkTreeModel *model = GTK_TREE_MODEL(gui_model_get());
struct db_entry *dbe, *next; struct db_entry *dbe, *next;
struct track *track; struct track *track;
GtkTreeModel *model;
GtkTreePath *path; GtkTreePath *path;
GtkTreeIter iter; GtkTreeIter iter;
GtkLabel *label;
GValue value; GValue value;
model = GTK_TREE_MODEL(gui_model_get());
label = GTK_LABEL(gui_builder_widget("o_runtime"));
gui_model_set_queue(playlist_get_queue(PL_SYSTEM, "Collection")); gui_model_set_queue(playlist_get_queue(PL_SYSTEM, "Collection"));
g_assert_cmpstr(gtk_label_get_text(label), ==, ""); g_assert_cmpstr(gtk_label_get_text(label), ==, "");
memset(&value, 0, sizeof(GValue)); memset(&value, 0, sizeof(GValue));
@ -285,13 +274,12 @@ static void test_model()
db_for_each(dbe, next, track_db_get()) db_for_each(dbe, next, track_db_get())
playlist_remove(PL_SYSTEM, "Collection", TRACK(dbe)); playlist_remove(PL_SYSTEM, "Collection", TRACK(dbe));
g_assert_cmpuint(count_delete, ==, 29); g_assert_cmpuint(count_delete, ==, 29);
core_deinit();
gui_model_deinit();
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int ret;
gtk_init(&argc, NULL); gtk_init(&argc, NULL);
core_init(&argc, NULL, &init_data); core_init(&argc, NULL, &init_data);
gui_builder_init("share/ocarina/ocarina.ui"); gui_builder_init("share/ocarina/ocarina.ui");
@ -299,8 +287,16 @@ int main(int argc, char **argv)
while (idle_run_task()) {}; while (idle_run_task()) {};
g_test_init(&argc, &argv, NULL); g_test_init(&argc, &argv, NULL);
g_test_add_func("/Gui/Queue Model/Init", test_init); g_test_add_func("/Gui/Model/Init", test_init);
g_test_add_func("/Gui/Queue Model/Empty", test_empty); g_test_add_func("/Gui/Model/Empty", test_empty);
g_test_add_func("/Gui/Queue Model", test_model); g_test_add_func("/Gui/Model/Tracks", test_model);
return g_test_run(); ret = g_test_run();
gui_model_deinit();
gui_builder_deinit();
core_deinit();
g_assert_false(G_IS_OBJECT(gui_model_get()));
g_assert_null(gui_model_get_queue());
return ret;
} }