diff options
Diffstat (limited to 'lib/src/turns-turn-order.c')
| -rw-r--r-- | lib/src/turns-turn-order.c | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/lib/src/turns-turn-order.c b/lib/src/turns-turn-order.c index 9a08d2e..abc22c4 100644 --- a/lib/src/turns-turn-order.c +++ b/lib/src/turns-turn-order.c @@ -13,7 +13,8 @@ enum { - PROP_RUNNING = 1, + PROP_EMPTY = 1, + PROP_RUNNING, PROP_SORT_MODE, N_PROPERTIES, }; @@ -79,6 +80,8 @@ static void clear_participants(TurnsTurnOrder * self) g_slist_free_full(self->participants, g_object_unref); self->participants = nullptr; + + g_object_notify_by_pspec(G_OBJECT(self), properties[PROP_EMPTY]); } static void set_running(TurnsTurnOrder * self, gboolean value) @@ -105,6 +108,9 @@ static void turns_turn_order_get_property(GObject * self, guint id, GValue * val switch (id) { + case PROP_EMPTY: + g_value_set_boolean(value, turns_turn_order_get_empty(instance)); + return; case PROP_RUNNING: g_value_set_boolean(value, turns_turn_order_get_running(instance)); return; @@ -154,6 +160,12 @@ static void turns_turn_order_class_init(TurnsTurnOrderClass * klass) object_class->get_property = turns_turn_order_get_property; object_class->set_property = turns_turn_order_set_property; + properties[PROP_EMPTY] = g_param_spec_boolean("empty", + "Empty", + "Whether the turn order is empty.", + TRUE, + G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY); + properties[PROP_RUNNING] = g_param_spec_boolean("running", "Running", "Whether or not the turn order is running (e.g. has been started)", @@ -207,10 +219,16 @@ void turns_turn_order_add(TurnsTurnOrder * self, TurnsParticipant * participant) auto sort_mode = turns_turn_order_get_sort_mode(self); + auto old_head = self->participants; self->participants = g_slist_insert_sorted_with_data(self->participants, g_object_ref(participant), compare_participant, (void *)sort_mode); auto position = g_slist_index(self->participants, participant); + if (old_head == nullptr && !turns_turn_order_get_empty(self)) + { + g_object_notify_by_pspec(G_OBJECT(self), properties[PROP_EMPTY]); + } + g_list_model_items_changed(G_LIST_MODEL(self), position, 0, 1); } @@ -242,11 +260,24 @@ void turns_turn_order_remove_at(TurnsTurnOrder * self, guint position) } g_object_unref(element->data); + auto old_head = self->participants; self->participants = g_slist_delete_link(self->participants, element); + if (old_head != nullptr && turns_turn_order_get_empty(self)) + { + g_object_notify_by_pspec(G_OBJECT(self), properties[PROP_EMPTY]); + set_running(self, false); + } + g_list_model_items_changed(G_LIST_MODEL(self), position, 1, 0); } +gboolean turns_turn_order_get_empty(TurnsTurnOrder const * self) +{ + g_return_val_if_fail(TURNS_IS_TURN_ORDER((TurnsTurnOrder *)self), true); + return self->participants == nullptr; +} + gsize turns_turn_order_get_participant_count(TurnsTurnOrder const * self) { g_return_val_if_fail(TURNS_IS_TURN_ORDER((TurnsTurnOrder *)self), 0); |
