[gnome-builder] libide/tweaks: add object hierarchy
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder] libide/tweaks: add object hierarchy
- Date: Sat, 30 Jul 2022 03:02:50 +0000 (UTC)
commit 18eebd685f11fddc3946f9c0d3120c586e0efacb
Author: Christian Hergert <chergert redhat com>
Date: Fri Jul 29 20:02:45 2022 -0700
libide/tweaks: add object hierarchy
src/libide/tweaks/ide-tweaks-item.c | 210 +++++++++++++++++++++++++++++++++++-
src/libide/tweaks/ide-tweaks-item.h | 38 +++++--
2 files changed, 236 insertions(+), 12 deletions(-)
---
diff --git a/src/libide/tweaks/ide-tweaks-item.c b/src/libide/tweaks/ide-tweaks-item.c
index 4cabaa4ad..a5f48cb90 100644
--- a/src/libide/tweaks/ide-tweaks-item.c
+++ b/src/libide/tweaks/ide-tweaks-item.c
@@ -26,6 +26,9 @@
typedef struct
{
+ IdeTweaksItem *parent;
+ GList link;
+ GQueue children;
char *id;
char **keywords;
char *sort_key;
@@ -44,16 +47,29 @@ G_DEFINE_ABSTRACT_TYPE (IdeTweaksItem, ide_tweaks_item, G_TYPE_OBJECT)
static GParamSpec *properties [N_PROPS];
static void
-ide_tweaks_item_finalize (GObject *object)
+ide_tweaks_item_dispose (GObject *object)
{
IdeTweaksItem *self = (IdeTweaksItem *)object;
IdeTweaksItemPrivate *priv = ide_tweaks_item_get_instance_private (self);
+ while (priv->children.head != NULL)
+ {
+ IdeTweaksItem *child = g_queue_peek_head (&priv->children);
+
+ ide_tweaks_item_unparent (child);
+ }
+
+ g_assert (priv->children.length == 0);
+ g_assert (priv->children.head == NULL);
+ g_assert (priv->children.tail == NULL);
+
+ ide_tweaks_item_unparent (self);
+
g_clear_pointer (&priv->keywords, g_strfreev);
g_clear_pointer (&priv->sort_key, g_free);
g_clear_pointer (&priv->id, g_free);
- G_OBJECT_CLASS (ide_tweaks_item_parent_class)->finalize (object);
+ G_OBJECT_CLASS (ide_tweaks_item_parent_class)->dispose (object);
}
static void
@@ -115,7 +131,7 @@ ide_tweaks_item_class_init (IdeTweaksItemClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
- object_class->finalize = ide_tweaks_item_finalize;
+ object_class->dispose = ide_tweaks_item_dispose;
object_class->get_property = ide_tweaks_item_get_property;
object_class->set_property = ide_tweaks_item_set_property;
@@ -210,3 +226,191 @@ ide_tweaks_item_set_sort_key (IdeTweaksItem *self,
if (ide_set_string (&priv->sort_key, sort_key))
g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_SORT_KEY]);
}
+
+/**
+ * ide_tweaks_item_get_first_child:
+ * @self: a #IdeTweaksItem
+ *
+ * Gets the first child of @self.
+ *
+ * Returns: (transfer none) (nullable): a #IdeTweaksItem or %NULL
+ */
+IdeTweaksItem *
+ide_tweaks_item_get_first_child (IdeTweaksItem *self)
+{
+ IdeTweaksItemPrivate *priv = ide_tweaks_item_get_instance_private (self);
+
+ g_return_val_if_fail (IDE_IS_TWEAKS_ITEM (self), NULL);
+
+ return g_queue_peek_head (&priv->children);
+}
+
+/**
+ * ide_tweaks_item_get_last_child:
+ * @self: a #IdeTweaksItem
+ *
+ * Gets the last child of @self.
+ *
+ * Returns: (transfer none) (nullable): a #IdeTweaksItem or %NULL
+ */
+IdeTweaksItem *
+ide_tweaks_item_get_last_child (IdeTweaksItem *self)
+{
+ IdeTweaksItemPrivate *priv = ide_tweaks_item_get_instance_private (self);
+
+ g_return_val_if_fail (IDE_IS_TWEAKS_ITEM (self), NULL);
+
+ return g_queue_peek_tail (&priv->children);
+}
+
+/**
+ * ide_tweaks_item_get_previous_sibling:
+ * @self: a #IdeTweaksItem
+ *
+ * Gets the previous sibling within the parent.
+ *
+ * Returns: (transfer none) (nullable): A #IdeTweaksItem or %NULL
+ */
+IdeTweaksItem *
+ide_tweaks_item_get_previous_sibling (IdeTweaksItem *self)
+{
+ IdeTweaksItemPrivate *priv = ide_tweaks_item_get_instance_private (self);
+
+ g_return_val_if_fail (IDE_IS_TWEAKS_ITEM (self), NULL);
+
+ if (priv->link.prev != NULL)
+ return priv->link.prev->data;
+
+ return NULL;
+}
+
+/**
+ * ide_tweaks_item_get_next_sibling:
+ * @self: a #IdeTweaksItem
+ *
+ * Gets the next sibling within the parent.
+ *
+ * Returns: (transfer none) (nullable): A #IdeTweaksItem or %NULL
+ */
+IdeTweaksItem *
+ide_tweaks_item_get_next_sibling (IdeTweaksItem *self)
+{
+ IdeTweaksItemPrivate *priv = ide_tweaks_item_get_instance_private (self);
+
+ g_return_val_if_fail (IDE_IS_TWEAKS_ITEM (self), NULL);
+
+ if (priv->link.next != NULL)
+ return priv->link.next->data;
+
+ return NULL;
+}
+
+/*
+ * ide_tweaks_item_insert_after:
+ * @self: (transfer full): a #IdeTweaksItem
+ * @parent: the #IdeTweaksItem to add @self to
+ * @previous_sibling: (nullable): a #IdeTweaksItem or %NULL to append @self
+ *
+ * Adds @self to the children of @parent, immediately after @previous_sibling.
+ *
+ * If @previous_sibling is %NULL, then @self is appended.
+ */
+void
+ide_tweaks_item_insert_after (IdeTweaksItem *self,
+ IdeTweaksItem *parent,
+ IdeTweaksItem *previous_sibling)
+{
+ IdeTweaksItemPrivate *priv = ide_tweaks_item_get_instance_private (self);
+ IdeTweaksItemPrivate *parent_priv = ide_tweaks_item_get_instance_private (parent);
+ IdeTweaksItemPrivate *previous_priv = ide_tweaks_item_get_instance_private (previous_sibling);
+
+ g_return_if_fail (IDE_IS_TWEAKS_ITEM (self));
+ g_return_if_fail (IDE_IS_TWEAKS_ITEM (parent));
+ g_return_if_fail (!previous_sibling || IDE_IS_TWEAKS_ITEM (previous_sibling));
+ g_return_if_fail (!previous_sibling || ide_tweaks_item_get_parent (previous_sibling) == parent);
+ g_return_if_fail (priv->link.data == self);
+ g_return_if_fail (parent_priv->link.data == parent);
+ g_return_if_fail (priv->parent == NULL);
+
+ priv->parent = parent;
+
+ if (previous_sibling != NULL)
+ g_queue_insert_after_link (&parent_priv->children, &previous_priv->link, &priv->link);
+ else
+ g_queue_push_tail_link (&parent_priv->children, &priv->link);
+}
+
+/*
+ * ide_tweaks_item_insert_before:
+ * @self: (transfer full): a #IdeTweaksItem
+ * @parent: the #IdeTweaksItem to add @self to
+ * @next_sibling: (nullable): a #IdeTweaksItem or %NULL to append @self
+ *
+ * Adds @self to the children of @parent, immediately before @next_sibling.
+ *
+ * If @previous_sibling is %NULL, then @self is prepended.
+ */
+void
+ide_tweaks_item_insert_before (IdeTweaksItem *self,
+ IdeTweaksItem *parent,
+ IdeTweaksItem *next_sibling)
+{
+ IdeTweaksItemPrivate *priv = ide_tweaks_item_get_instance_private (self);
+ IdeTweaksItemPrivate *parent_priv = ide_tweaks_item_get_instance_private (parent);
+ IdeTweaksItemPrivate *next_priv = ide_tweaks_item_get_instance_private (next_sibling);
+
+ g_return_if_fail (IDE_IS_TWEAKS_ITEM (self));
+ g_return_if_fail (IDE_IS_TWEAKS_ITEM (parent));
+ g_return_if_fail (!next_sibling || IDE_IS_TWEAKS_ITEM (next_sibling));
+ g_return_if_fail (!next_sibling || ide_tweaks_item_get_parent (next_sibling) == parent);
+ g_return_if_fail (priv->link.data == self);
+ g_return_if_fail (parent_priv->link.data == parent);
+ g_return_if_fail (priv->parent == NULL);
+
+ priv->parent = parent;
+
+ if (next_sibling != NULL)
+ g_queue_insert_before_link (&parent_priv->children, &next_priv->link, &priv->link);
+ else
+ g_queue_push_head_link (&parent_priv->children, &priv->link);
+}
+
+/**
+ * ide_tweaks_item_get_parent:
+ * @self: a #IdeTweaksItem
+ *
+ * Gets the parent #IdeTweaksItem
+ *
+ * Returns: (transfer none) (nullable): the parent #IdeTweaksItem or %NULL
+ */
+IdeTweaksItem *
+ide_tweaks_item_get_parent (IdeTweaksItem *self)
+{
+ IdeTweaksItemPrivate *priv = ide_tweaks_item_get_instance_private (self);
+
+ g_return_val_if_fail (IDE_IS_TWEAKS_ITEM (self), NULL);
+
+ return priv->parent;
+}
+
+void
+ide_tweaks_item_unparent (IdeTweaksItem *self)
+{
+ IdeTweaksItemPrivate *priv = ide_tweaks_item_get_instance_private (self);
+ IdeTweaksItemPrivate *parent_priv;
+ IdeTweaksItem *parent;
+
+ g_return_if_fail (IDE_IS_TWEAKS_ITEM (self));
+ g_return_if_fail (priv->parent == NULL || IDE_IS_TWEAKS_ITEM (priv->parent));
+
+ if (priv->parent == NULL)
+ return;
+
+ parent = priv->parent;
+ parent_priv = ide_tweaks_item_get_instance_private (parent);
+
+ g_queue_unlink (&parent_priv->children, &priv->link);
+ priv->parent = NULL;
+
+ g_object_unref (self);
+}
diff --git a/src/libide/tweaks/ide-tweaks-item.h b/src/libide/tweaks/ide-tweaks-item.h
index a8c42167e..20fd79943 100644
--- a/src/libide/tweaks/ide-tweaks-item.h
+++ b/src/libide/tweaks/ide-tweaks-item.h
@@ -39,19 +39,39 @@ struct _IdeTweaksItemClass
};
IDE_AVAILABLE_IN_ALL
-const char *ide_tweaks_item_get_id (IdeTweaksItem *self);
+const char *ide_tweaks_item_get_id (IdeTweaksItem *self);
IDE_AVAILABLE_IN_ALL
-void ide_tweaks_item_set_id (IdeTweaksItem *self,
- const char *id);
+void ide_tweaks_item_set_id (IdeTweaksItem *self,
+ const char *id);
IDE_AVAILABLE_IN_ALL
-const char * const *ide_tweaks_item_get_keywords (IdeTweaksItem *self);
+const char * const *ide_tweaks_item_get_keywords (IdeTweaksItem *self);
IDE_AVAILABLE_IN_ALL
-void ide_tweaks_item_set_keywords (IdeTweaksItem *self,
- const char * const *keywords);
+void ide_tweaks_item_set_keywords (IdeTweaksItem *self,
+ const char * const *keywords);
IDE_AVAILABLE_IN_ALL
-const char *ide_tweaks_item_get_sort_key (IdeTweaksItem *self);
+const char *ide_tweaks_item_get_sort_key (IdeTweaksItem *self);
IDE_AVAILABLE_IN_ALL
-void ide_tweaks_item_set_sort_key (IdeTweaksItem *self,
- const char *sort_key);
+void ide_tweaks_item_set_sort_key (IdeTweaksItem *self,
+ const char *sort_key);
+IDE_AVAILABLE_IN_ALL
+IdeTweaksItem *ide_tweaks_item_get_parent (IdeTweaksItem *self);
+IDE_AVAILABLE_IN_ALL
+IdeTweaksItem *ide_tweaks_item_get_last_child (IdeTweaksItem *self);
+IDE_AVAILABLE_IN_ALL
+IdeTweaksItem *ide_tweaks_item_get_first_child (IdeTweaksItem *self);
+IDE_AVAILABLE_IN_ALL
+IdeTweaksItem *ide_tweaks_item_get_previous_sibling (IdeTweaksItem *self);
+IDE_AVAILABLE_IN_ALL
+IdeTweaksItem *ide_tweaks_item_get_next_sibling (IdeTweaksItem *self);
+IDE_AVAILABLE_IN_ALL
+void ide_tweaks_item_insert_after (IdeTweaksItem *self,
+ IdeTweaksItem *parent,
+ IdeTweaksItem *previous_sibling);
+IDE_AVAILABLE_IN_ALL
+void ide_tweaks_item_insert_before (IdeTweaksItem *self,
+ IdeTweaksItem *parent,
+ IdeTweaksItem *next_sibling);
+IDE_AVAILABLE_IN_ALL
+void ide_tweaks_item_unparent (IdeTweaksItem *self);
G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]