[gnome-builder] libide-core: add binding/signal/action groups
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder] libide-core: add binding/signal/action groups
- Date: Tue, 12 Jul 2022 06:39:07 +0000 (UTC)
commit 670a9e6584c2ff7a88b021693adefd502c78ed07
Author: Christian Hergert <chergert redhat com>
Date: Mon Jul 11 16:27:51 2022 -0700
libide-core: add binding/signal/action groups
These are a number of useful things from libdazzle that we want in-tree.
Once we port away from IdeSignalGroup/IdeBindinGroup to GSignalGroup and
GBindingGroup, we can drop these.
src/libide/core/ide-action-group.h | 221 ++++++++++
src/libide/core/ide-binding-group.c | 639 +++++++++++++++++++++++++++
src/libide/core/ide-binding-group.h | 70 +++
src/libide/core/ide-signal-group.c | 830 ++++++++++++++++++++++++++++++++++++
src/libide/core/ide-signal-group.h | 80 ++++
src/libide/core/libide-core.h | 3 +
6 files changed, 1843 insertions(+)
---
diff --git a/src/libide/core/ide-action-group.h b/src/libide/core/ide-action-group.h
new file mode 100644
index 000000000..d60c63af4
--- /dev/null
+++ b/src/libide/core/ide-action-group.h
@@ -0,0 +1,221 @@
+/* ide-action-group.h
+ *
+ * Copyright (C) 2017 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+#define IDE_DEFINE_ACTION_GROUP(Type, prefix, ...) \
+struct _##Type##ActionEntry { \
+ const gchar *name; \
+ void (*activate) (Type *self, GVariant *param); \
+ const gchar *parameter_type; \
+ const gchar *state; \
+ void (*change_state) (Type *self, GVariant *state); \
+} prefix##_actions[] = __VA_ARGS__; \
+ \
+typedef struct { \
+ GVariant *state; \
+ GVariant *state_hint; \
+ guint enabled : 1; \
+} Type##ActionInfo; \
+ \
+static gboolean \
+_##prefix##_has_action (GActionGroup *group, \
+ const gchar *name) \
+{ \
+ for (guint i = 0; i < G_N_ELEMENTS(prefix##_actions); i++) \
+ { \
+ if (g_strcmp0 (name, prefix##_actions[i].name) == 0) \
+ return TRUE; \
+ } \
+ return FALSE; \
+} \
+ \
+static gchar ** \
+_##prefix##_list_actions (GActionGroup *group) \
+{ \
+ GPtrArray *ar = g_ptr_array_new (); \
+ \
+ for (guint i = 0; i < G_N_ELEMENTS(prefix##_actions); i++) \
+ g_ptr_array_add (ar, g_strdup (prefix##_actions[i].name)); \
+ g_ptr_array_add (ar, NULL); \
+ \
+ return (gchar **)g_ptr_array_free (ar, FALSE); \
+} \
+ \
+static void \
+_##prefix##_action_info_free (gpointer data) \
+{ \
+ Type##ActionInfo *info = data; \
+ g_clear_pointer (&info->state, g_variant_unref); \
+ g_clear_pointer (&info->state_hint, g_variant_unref); \
+ g_slice_free (Type##ActionInfo, info); \
+} \
+ \
+static Type##ActionInfo * \
+_##prefix##_get_action_info (GActionGroup *group, \
+ const gchar *name) \
+{ \
+ g_autofree gchar *fullname = g_strdup_printf ("ACTION-INFO:%s", name); \
+ Type##ActionInfo *info = g_object_get_data (G_OBJECT (group), fullname); \
+ if (info == NULL) \
+ { \
+ info = g_slice_new0 (Type##ActionInfo); \
+ info->enabled = TRUE; \
+ for (guint i = 0; i < G_N_ELEMENTS(prefix##_actions); i++) \
+ { \
+ if (g_strcmp0 (prefix##_actions[i].name, name) == 0) \
+ { \
+ if (prefix##_actions[i].state != NULL) \
+ info->state = g_variant_parse ( \
+ NULL, prefix##_actions[i].state, NULL, NULL, NULL); \
+ break; \
+ } \
+ } \
+ g_object_set_data_full (G_OBJECT (group), fullname, info, \
+ _##prefix##_action_info_free); \
+ } \
+ return info; \
+} \
+ \
+static inline GVariant * \
+prefix##_get_action_state (Type *self, \
+ const gchar *name) \
+{ \
+ Type##ActionInfo *info = _##prefix##_get_action_info (G_ACTION_GROUP (self), \
+ name); \
+ return info->state; \
+} \
+ \
+static inline void \
+prefix##_set_action_state (Type *self, \
+ const gchar *name, \
+ GVariant *state) \
+{ \
+ Type##ActionInfo *info = _##prefix##_get_action_info (G_ACTION_GROUP (self), \
+ name); \
+ if (state != info->state) \
+ { \
+ g_clear_pointer (&info->state, g_variant_unref); \
+ info->state = state ? g_variant_ref_sink (state) : NULL; \
+ g_action_group_action_state_changed (G_ACTION_GROUP (self), name, state); \
+ } \
+} \
+ \
+static inline void \
+prefix##_set_action_enabled (Type *self, \
+ const gchar *name, \
+ gboolean enabled) \
+{ \
+ Type##ActionInfo *info = _##prefix##_get_action_info (G_ACTION_GROUP (self), \
+ name); \
+ if (enabled != info->enabled) \
+ { \
+ info->enabled = !!enabled; \
+ g_action_group_action_enabled_changed (G_ACTION_GROUP (self), \
+ name, enabled); \
+ } \
+} \
+ \
+static void \
+_##prefix##_change_action_state (GActionGroup *group, \
+ const gchar *name, \
+ GVariant *state) \
+{ \
+ for (guint i = 0; i < G_N_ELEMENTS(prefix##_actions); i++) \
+ { \
+ if (g_strcmp0 (name, prefix##_actions[i].name) == 0) \
+ { \
+ if (prefix##_actions[i].change_state) \
+ prefix##_actions[i].change_state ((Type*)group, state); \
+ return; \
+ } \
+ } \
+} \
+ \
+static void \
+_##prefix##_activate_action (GActionGroup *group, \
+ const gchar *name, \
+ GVariant *param) \
+{ \
+ for (guint i = 0; i < G_N_ELEMENTS(prefix##_actions); i++) \
+ { \
+ if (g_strcmp0 (name, prefix##_actions[i].name) == 0) \
+ { \
+ if (prefix##_actions[i].activate) \
+ prefix##_actions[i].activate ((Type*)group, param); \
+ return; \
+ } \
+ } \
+} \
+ \
+static gboolean \
+_##prefix##_query_action (GActionGroup *group, \
+ const gchar *name, \
+ gboolean *enabled, \
+ const GVariantType **parameter_type, \
+ const GVariantType **state_type, \
+ GVariant **state_hint, \
+ GVariant **state) \
+{ \
+ if (enabled) *enabled = FALSE; \
+ if (parameter_type) *parameter_type = NULL ; \
+ if (state_type) *state_type = NULL ; \
+ if (state_hint) *state_hint = NULL ; \
+ if (state) *state = NULL ; \
+ for (guint i = 0; i < G_N_ELEMENTS(prefix##_actions); i++) \
+ { \
+ if (g_strcmp0 (name, prefix##_actions[i].name) == 0) \
+ { \
+ Type##ActionInfo *info = _##prefix##_get_action_info(group, name); \
+ if (prefix##_actions[i].change_state && state_type) \
+ *state_type = prefix##_actions[i].parameter_type ? \
+ G_VARIANT_TYPE(prefix##_actions[i].parameter_type) : \
+ NULL; \
+ else if (prefix##_actions[i].activate && parameter_type) \
+ *parameter_type = prefix##_actions[i].parameter_type ? \
+ G_VARIANT_TYPE(prefix##_actions[i].parameter_type) :\
+ NULL; \
+ if (state_hint) \
+ *state_hint = info->state_hint != NULL ? \
+ g_variant_ref (info->state_hint) : NULL; \
+ if (state) \
+ *state = info->state != NULL ? \
+ g_variant_ref (info->state) : NULL; \
+ if (enabled) \
+ *enabled = info->enabled; \
+ return TRUE; \
+ } \
+ } \
+ return FALSE; \
+} \
+ \
+static void \
+prefix##_init_action_group (GActionGroupInterface *iface) \
+{ \
+ iface->has_action = _##prefix##_has_action; \
+ iface->list_actions = _##prefix##_list_actions; \
+ iface->change_action_state = _##prefix##_change_action_state; \
+ iface->activate_action = _##prefix##_activate_action; \
+ iface->query_action = _##prefix##_query_action; \
+}
+
+G_END_DECLS
diff --git a/src/libide/core/ide-binding-group.c b/src/libide/core/ide-binding-group.c
new file mode 100644
index 000000000..07d5e390f
--- /dev/null
+++ b/src/libide/core/ide-binding-group.c
@@ -0,0 +1,639 @@
+/* ide-binding-group.c
+ *
+ * Copyright (C) 2015 Christian Hergert <christian hergert me>
+ * Copyright (C) 2015 Garrett Regier <garrettregier gmail com>
+ *
+ * This file is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of the
+ * License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include <glib/gi18n.h>
+
+#include "ide-binding-group.h"
+
+/**
+ * SECTION:idebindinggroup
+ * @title: IdeBindingGroup
+ * @short_description: Manage a collection of #GBindings on
+ * a #GObject as a group.
+ *
+ * #IdeBindingGroup manages to simplify the process of binding
+ * many properties from a #GObject as a group. As such there is no API
+ * to unbind a property from the group.
+ *
+ * In particular, this allows you to change the source instance for the
+ * bindings. This automatically causes the unbinding of the properties
+ * from the old instance and binding to the new instance.
+ *
+ * This should not be confused with #GtkBindingGroup.
+ */
+
+struct _IdeBindingGroup
+{
+ GObject parent_instance;
+
+ GObject *source;
+ GPtrArray *lazy_bindings;
+};
+
+typedef struct
+{
+ IdeBindingGroup *group;
+ const gchar *source_property;
+ const gchar *target_property;
+ GObject *target;
+ GBinding *binding;
+ gpointer user_data;
+ GDestroyNotify user_data_destroy;
+ gpointer transform_to;
+ gpointer transform_from;
+ GBindingFlags binding_flags;
+ guint using_closures : 1;
+} LazyBinding;
+
+G_DEFINE_TYPE (IdeBindingGroup, ide_binding_group, G_TYPE_OBJECT)
+
+enum {
+ PROP_0,
+ PROP_SOURCE,
+ LAST_PROP
+};
+
+static GParamSpec *properties [LAST_PROP];
+
+/*#define DEBUG_BINDINGS 1*/
+
+#ifdef DEBUG_BINDINGS
+static gchar *
+_g_flags_to_string (GFlagsClass *flags_class,
+ guint value)
+{
+ GString *str;
+ GFlagsValue *flags_value;
+ gboolean first = TRUE;
+
+ str = g_string_new (NULL);
+
+ while ((first || value != 0) &&
+ (flags_value = g_flags_get_first_value (flags_class, value)) != NULL)
+ {
+ if (!first)
+ g_string_append (str, " | ");
+
+ g_string_append (str, flags_value->value_name);
+
+ first = FALSE;
+ value &= ~(flags_value->value);
+ }
+
+ return g_string_free (str, FALSE);
+}
+#endif
+
+static void
+ide_binding_group_connect (IdeBindingGroup *self,
+ LazyBinding *lazy_binding)
+{
+ GBinding *binding;
+
+ g_assert (IDE_IS_BINDING_GROUP (self));
+ g_assert (self->source != NULL);
+ g_assert (lazy_binding != NULL);
+ g_assert (lazy_binding->binding == NULL);
+ g_assert (lazy_binding->target != NULL);
+ g_assert (lazy_binding->target_property != NULL);
+ g_assert (lazy_binding->source_property != NULL);
+
+#ifdef DEBUG_BINDINGS
+ {
+ GFlagsClass *flags_class;
+ g_autofree gchar *flags_str = NULL;
+
+ flags_class = g_type_class_ref (G_TYPE_BINDING_FLAGS);
+ flags_str = _g_flags_to_string (flags_class,
+ lazy_binding->binding_flags);
+
+ g_print ("Binding %s(%p):%s to %s(%p):%s (flags=%s)\n",
+ G_OBJECT_TYPE_NAME (self->source),
+ self->source,
+ lazy_binding->source_property,
+ G_OBJECT_TYPE_NAME (lazy_binding->target),
+ lazy_binding->target,
+ lazy_binding->target_property,
+ flags_str);
+
+ g_type_class_unref (flags_class);
+ }
+#endif
+
+ if (!lazy_binding->using_closures)
+ {
+ binding = g_object_bind_property_full (self->source,
+ lazy_binding->source_property,
+ lazy_binding->target,
+ lazy_binding->target_property,
+ lazy_binding->binding_flags,
+ lazy_binding->transform_to,
+ lazy_binding->transform_from,
+ lazy_binding->user_data,
+ NULL);
+ }
+ else
+ {
+ binding = g_object_bind_property_with_closures (self->source,
+ lazy_binding->source_property,
+ lazy_binding->target,
+ lazy_binding->target_property,
+ lazy_binding->binding_flags,
+ lazy_binding->transform_to,
+ lazy_binding->transform_from);
+ }
+
+ lazy_binding->binding = binding;
+}
+
+static void
+ide_binding_group_disconnect (LazyBinding *lazy_binding)
+{
+ g_assert (lazy_binding != NULL);
+
+ if (lazy_binding->binding != NULL)
+ {
+ g_binding_unbind (lazy_binding->binding);
+ lazy_binding->binding = NULL;
+ }
+}
+
+static void
+ide_binding_group__source_weak_notify (gpointer data,
+ GObject *where_object_was)
+{
+ IdeBindingGroup *self = data;
+ gsize i;
+
+ g_assert (IDE_IS_BINDING_GROUP (self));
+
+ self->source = NULL;
+
+ for (i = 0; i < self->lazy_bindings->len; i++)
+ {
+ LazyBinding *lazy_binding;
+
+ lazy_binding = g_ptr_array_index (self->lazy_bindings, i);
+ lazy_binding->binding = NULL;
+ }
+}
+
+static void
+ide_binding_group__target_weak_notify (gpointer data,
+ GObject *where_object_was)
+{
+ IdeBindingGroup *self = data;
+ gsize i;
+
+ g_assert (IDE_IS_BINDING_GROUP (self));
+
+ for (i = 0; i < self->lazy_bindings->len; i++)
+ {
+ LazyBinding *lazy_binding;
+
+ lazy_binding = g_ptr_array_index (self->lazy_bindings, i);
+
+ if (lazy_binding->target == where_object_was)
+ {
+ lazy_binding->target = NULL;
+ lazy_binding->binding = NULL;
+
+ g_ptr_array_remove_index_fast (self->lazy_bindings, i);
+ break;
+ }
+ }
+}
+
+static void
+lazy_binding_free (gpointer data)
+{
+ LazyBinding *lazy_binding = data;
+
+ if (lazy_binding->target != NULL)
+ {
+ g_object_weak_unref (lazy_binding->target,
+ ide_binding_group__target_weak_notify,
+ lazy_binding->group);
+ lazy_binding->target = NULL;
+ }
+
+ ide_binding_group_disconnect (lazy_binding);
+
+ lazy_binding->group = NULL;
+ lazy_binding->source_property = NULL;
+ lazy_binding->target_property = NULL;
+
+ if (lazy_binding->user_data_destroy)
+ lazy_binding->user_data_destroy (lazy_binding->user_data);
+
+ if (lazy_binding->using_closures)
+ {
+ g_clear_pointer (&lazy_binding->transform_to, g_closure_unref);
+ g_clear_pointer (&lazy_binding->transform_from, g_closure_unref);
+ }
+
+ g_slice_free (LazyBinding, lazy_binding);
+}
+
+static void
+ide_binding_group_dispose (GObject *object)
+{
+ IdeBindingGroup *self = (IdeBindingGroup *)object;
+
+ g_assert (IDE_IS_BINDING_GROUP (self));
+
+ if (self->source != NULL)
+ {
+ g_object_weak_unref (self->source,
+ ide_binding_group__source_weak_notify,
+ self);
+ self->source = NULL;
+ }
+
+ if (self->lazy_bindings->len != 0)
+ g_ptr_array_remove_range (self->lazy_bindings, 0, self->lazy_bindings->len);
+
+ G_OBJECT_CLASS (ide_binding_group_parent_class)->dispose (object);
+}
+
+static void
+ide_binding_group_finalize (GObject *object)
+{
+ IdeBindingGroup *self = (IdeBindingGroup *)object;
+
+ g_assert (self->lazy_bindings != NULL);
+ g_assert (self->lazy_bindings->len == 0);
+
+ g_clear_pointer (&self->lazy_bindings, g_ptr_array_unref);
+
+ G_OBJECT_CLASS (ide_binding_group_parent_class)->finalize (object);
+}
+
+static void
+ide_binding_group_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ IdeBindingGroup *self = IDE_BINDING_GROUP (object);
+
+ switch (prop_id)
+ {
+ case PROP_SOURCE:
+ g_value_set_object (value, ide_binding_group_get_source (self));
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ }
+}
+
+static void
+ide_binding_group_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ IdeBindingGroup *self = IDE_BINDING_GROUP (object);
+
+ switch (prop_id)
+ {
+ case PROP_SOURCE:
+ ide_binding_group_set_source (self, g_value_get_object (value));
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ }
+}
+
+static void
+ide_binding_group_class_init (IdeBindingGroupClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->dispose = ide_binding_group_dispose;
+ object_class->finalize = ide_binding_group_finalize;
+ object_class->get_property = ide_binding_group_get_property;
+ object_class->set_property = ide_binding_group_set_property;
+
+ /**
+ * IdeBindingGroup:source
+ *
+ * The source object used for binding properties.
+ */
+ properties [PROP_SOURCE] =
+ g_param_spec_object ("source",
+ "Source",
+ "The source GObject used for binding properties.",
+ G_TYPE_OBJECT,
+ (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+ g_object_class_install_properties (object_class, LAST_PROP, properties);
+}
+
+static void
+ide_binding_group_init (IdeBindingGroup *self)
+{
+ self->lazy_bindings = g_ptr_array_new_with_free_func (lazy_binding_free);
+}
+
+/**
+ * ide_binding_group_new:
+ *
+ * Creates a new #IdeBindingGroup.
+ *
+ * Returns: a new #IdeBindingGroup
+ */
+IdeBindingGroup *
+ide_binding_group_new (void)
+{
+ return g_object_new (IDE_TYPE_BINDING_GROUP, NULL);
+}
+
+/**
+ * ide_binding_group_get_source:
+ * @self: the #IdeBindingGroup
+ *
+ * Gets the source object used for binding properties.
+ *
+ * Returns: (transfer none) (nullable): the source object.
+ */
+GObject *
+ide_binding_group_get_source (IdeBindingGroup *self)
+{
+ g_return_val_if_fail (IDE_IS_BINDING_GROUP (self), NULL);
+
+ return self->source;
+}
+
+static gboolean
+ide_binding_group_check_source (IdeBindingGroup *self,
+ gpointer source)
+{
+ gsize i;
+
+ for (i = 0; i < self->lazy_bindings->len; i++)
+ {
+ LazyBinding *lazy_binding;
+
+ lazy_binding = g_ptr_array_index (self->lazy_bindings, i);
+
+ g_return_val_if_fail (g_object_class_find_property (G_OBJECT_GET_CLASS (source),
+ lazy_binding->source_property) != NULL,
+ FALSE);
+ }
+
+ return TRUE;
+}
+
+/**
+ * ide_binding_group_set_source:
+ * @self: the #IdeBindingGroup
+ * @source: (type GObject) (nullable): the source #GObject
+ *
+ * Sets @source as the source object used for creating property
+ * bindings. If there is already a source object all bindings from it
+ * will be removed.
+ *
+ * Note: All properties that have been bound must exist on @source.
+ */
+void
+ide_binding_group_set_source (IdeBindingGroup *self,
+ gpointer source)
+{
+ g_return_if_fail (IDE_IS_BINDING_GROUP (self));
+ g_return_if_fail (!source || G_IS_OBJECT (source));
+ g_return_if_fail (source != (gpointer)self);
+
+ if (source == (gpointer)self->source)
+ return;
+
+ if (self->source != NULL)
+ {
+ gsize i;
+
+ g_object_weak_unref (self->source,
+ ide_binding_group__source_weak_notify,
+ self);
+ self->source = NULL;
+
+ for (i = 0; i < self->lazy_bindings->len; i++)
+ {
+ LazyBinding *lazy_binding;
+
+ lazy_binding = g_ptr_array_index (self->lazy_bindings, i);
+ ide_binding_group_disconnect (lazy_binding);
+ }
+ }
+
+ if (source != NULL && ide_binding_group_check_source (self, source))
+ {
+ gsize i;
+
+ self->source = source;
+ g_object_weak_ref (self->source,
+ ide_binding_group__source_weak_notify,
+ self);
+
+ for (i = 0; i < self->lazy_bindings->len; i++)
+ {
+ LazyBinding *lazy_binding;
+
+ lazy_binding = g_ptr_array_index (self->lazy_bindings, i);
+ ide_binding_group_connect (self, lazy_binding);
+ }
+ }
+
+ g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_SOURCE]);
+}
+
+static void
+ide_binding_group_bind_helper (IdeBindingGroup *self,
+ const gchar *source_property,
+ gpointer target,
+ const gchar *target_property,
+ GBindingFlags flags,
+ gpointer transform_to,
+ gpointer transform_from,
+ gpointer user_data,
+ GDestroyNotify user_data_destroy,
+ gboolean using_closures)
+{
+ LazyBinding *lazy_binding;
+
+ g_return_if_fail (IDE_IS_BINDING_GROUP (self));
+ g_return_if_fail (source_property != NULL);
+ g_return_if_fail (self->source == NULL ||
+ g_object_class_find_property (G_OBJECT_GET_CLASS (self->source),
+ source_property) != NULL);
+ g_return_if_fail (G_IS_OBJECT (target));
+ g_return_if_fail (target_property != NULL);
+ g_return_if_fail (g_object_class_find_property (G_OBJECT_GET_CLASS (target),
+ target_property) != NULL);
+ g_return_if_fail (target != (gpointer)self ||
+ strcmp (source_property, target_property) != 0);
+
+ lazy_binding = g_slice_new0 (LazyBinding);
+ lazy_binding->group = self;
+ lazy_binding->source_property = g_intern_string (source_property);
+ lazy_binding->target_property = g_intern_string (target_property);
+ lazy_binding->target = target;
+ lazy_binding->binding_flags = flags | G_BINDING_SYNC_CREATE;
+ lazy_binding->user_data = user_data;
+ lazy_binding->user_data_destroy = user_data_destroy;
+ lazy_binding->transform_to = transform_to;
+ lazy_binding->transform_from = transform_from;
+
+ if (using_closures)
+ {
+ lazy_binding->using_closures = TRUE;
+
+ if (transform_to != NULL)
+ g_closure_sink (g_closure_ref (transform_to));
+
+ if (transform_from != NULL)
+ g_closure_sink (g_closure_ref (transform_from));
+ }
+
+ g_object_weak_ref (target,
+ ide_binding_group__target_weak_notify,
+ self);
+
+ g_ptr_array_add (self->lazy_bindings, lazy_binding);
+
+ if (self->source != NULL)
+ ide_binding_group_connect (self, lazy_binding);
+}
+
+/**
+ * ide_binding_group_bind:
+ * @self: the #IdeBindingGroup
+ * @source_property: the property on the source to bind
+ * @target: (type GObject): the target #GObject
+ * @target_property: the property on @target to bind
+ * @flags: the flags used to create the #GBinding
+ *
+ * Creates a binding between @source_property on the source object
+ * and @target_property on @target. Whenever the @source_property
+ * is changed the @target_property is updated using the same value.
+ * The binding flags #G_BINDING_SYNC_CREATE is automatically specified.
+ *
+ * See: g_object_bind_property().
+ */
+void
+ide_binding_group_bind (IdeBindingGroup *self,
+ const gchar *source_property,
+ gpointer target,
+ const gchar *target_property,
+ GBindingFlags flags)
+{
+ ide_binding_group_bind_full (self, source_property,
+ target, target_property,
+ flags,
+ NULL, NULL,
+ NULL, NULL);
+}
+
+/**
+ * ide_binding_group_bind_full:
+ * @self: the #IdeBindingGroup
+ * @source_property: the property on the source to bind
+ * @target: (type GObject): the target #GObject
+ * @target_property: the property on @target to bind
+ * @flags: the flags used to create the #GBinding
+ * @transform_to: (scope notified) (nullable): the transformation function
+ * from the source object to the @target, or %NULL to use the default
+ * @transform_from: (scope notified) (nullable): the transformation function
+ * from the @target to the source object, or %NULL to use the default
+ * @user_data: custom data to be passed to the transformation
+ * functions, or %NULL
+ * @user_data_destroy: function to be called when disposing the binding,
+ * to free the resources used by the transformation functions
+ *
+ * Creates a binding between @source_property on the source object and
+ * @target_property on @target, allowing you to set the transformation
+ * functions to be used by the binding. The binding flags
+ * #G_BINDING_SYNC_CREATE is automatically specified.
+ *
+ * See: g_object_bind_property_full().
+ */
+void
+ide_binding_group_bind_full (IdeBindingGroup *self,
+ const gchar *source_property,
+ gpointer target,
+ const gchar *target_property,
+ GBindingFlags flags,
+ GBindingTransformFunc transform_to,
+ GBindingTransformFunc transform_from,
+ gpointer user_data,
+ GDestroyNotify user_data_destroy)
+{
+ ide_binding_group_bind_helper (self, source_property,
+ target, target_property,
+ flags,
+ transform_to, transform_from,
+ user_data, user_data_destroy,
+ FALSE);
+}
+
+/**
+ * ide_binding_group_bind_with_closures: (rename-to ide_binding_group_bind_full)
+ * @self: the #IdeBindingGroup
+ * @source_property: the property on the source to bind
+ * @target: (type GObject): the target #GObject
+ * @target_property: the property on @target to bind
+ * @flags: the flags used to create the #GBinding
+ * @transform_to: (nullable): a #GClosure wrapping the
+ * transformation function from the source object to the @target,
+ * or %NULL to use the default
+ * @transform_from: (nullable): a #GClosure wrapping the
+ * transformation function from the @target to the source object,
+ * or %NULL to use the default
+ *
+ * Creates a binding between @source_property on the source object and
+ * @target_property on @target, allowing you to set the transformation
+ * functions to be used by the binding. The binding flags
+ * #G_BINDING_SYNC_CREATE is automatically specified.
+ *
+ * This function is the language bindings friendly version of
+ * ide_binding_group_bind_property_full(), using #GClosures
+ * instead of function pointers.
+ *
+ * See: g_object_bind_property_with_closures().
+ */
+void
+ide_binding_group_bind_with_closures (IdeBindingGroup *self,
+ const gchar *source_property,
+ gpointer target,
+ const gchar *target_property,
+ GBindingFlags flags,
+ GClosure *transform_to,
+ GClosure *transform_from)
+{
+ ide_binding_group_bind_helper (self, source_property,
+ target, target_property,
+ flags,
+ transform_to, transform_from,
+ NULL, NULL,
+ TRUE);
+}
+
diff --git a/src/libide/core/ide-binding-group.h b/src/libide/core/ide-binding-group.h
new file mode 100644
index 000000000..bcf2639d6
--- /dev/null
+++ b/src/libide/core/ide-binding-group.h
@@ -0,0 +1,70 @@
+/* ide-binding-group.h
+ *
+ * Copyright (C) 2015 Christian Hergert <christian hergert me>
+ * Copyright (C) 2015 Garrett Regier <garrettregier gmail com>
+ *
+ * This file is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of the
+ * License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#if !defined (IDE_CORE_INSIDE) && !defined (IDE_CORE_COMPILATION)
+# error "Only <libide-core.h> can be included directly."
+#endif
+
+#include <glib-object.h>
+
+#include "ide-version-macros.h"
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_BINDING_GROUP (ide_binding_group_get_type())
+
+IDE_AVAILABLE_IN_ALL
+G_DECLARE_FINAL_TYPE (IdeBindingGroup, ide_binding_group, IDE, BINDING_GROUP, GObject)
+
+IDE_AVAILABLE_IN_ALL
+IdeBindingGroup *ide_binding_group_new (void);
+IDE_AVAILABLE_IN_ALL
+GObject *ide_binding_group_get_source (IdeBindingGroup *self);
+IDE_AVAILABLE_IN_ALL
+void ide_binding_group_set_source (IdeBindingGroup *self,
+ gpointer source);
+IDE_AVAILABLE_IN_ALL
+void ide_binding_group_bind (IdeBindingGroup *self,
+ const gchar *source_property,
+ gpointer target,
+ const gchar *target_property,
+ GBindingFlags flags);
+IDE_AVAILABLE_IN_ALL
+void ide_binding_group_bind_full (IdeBindingGroup *self,
+ const gchar *source_property,
+ gpointer target,
+ const gchar *target_property,
+ GBindingFlags flags,
+ GBindingTransformFunc transform_to,
+ GBindingTransformFunc transform_from,
+ gpointer user_data,
+ GDestroyNotify user_data_destroy);
+IDE_AVAILABLE_IN_ALL
+void ide_binding_group_bind_with_closures (IdeBindingGroup *self,
+ const gchar *source_property,
+ gpointer target,
+ const gchar *target_property,
+ GBindingFlags flags,
+ GClosure *transform_to,
+ GClosure *transform_from);
+
+G_END_DECLS
+
diff --git a/src/libide/core/ide-signal-group.c b/src/libide/core/ide-signal-group.c
new file mode 100644
index 000000000..21ac706e8
--- /dev/null
+++ b/src/libide/core/ide-signal-group.c
@@ -0,0 +1,830 @@
+/* ide-signal-group.c
+ *
+ * Copyright (C) 2015 Christian Hergert <christian hergert me>
+ * Copyright (C) 2015 Garrett Regier <garrettregier gmail com>
+ *
+ * This file is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of the
+ * License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define G_LOG_DOMAIN "ide-signal-group"
+
+#include "config.h"
+
+#include <glib/gi18n.h>
+
+#include "ide-signal-group.h"
+
+/**
+ * SECTION:idesignalgroup
+ * @title: IdeSignalGroup
+ * @short_description: Manage a collection of signals on a #GObject
+ *
+ * #IdeSignalGroup manages to simplify the process of connecting
+ * many signals to a #GObject as a group. As such there is no API
+ * to disconnect a signal from the group.
+ *
+ * In particular, this allows you to:
+ *
+ * - Change the target instance, which automatically causes disconnection
+ * of the signals from the old instance and connecting to the new instance.
+ * - Block and unblock signals as a group
+ * - Ensuring that blocked state transfers across target instances.
+ *
+ * One place you might want to use such a structure is with #GtkTextView and
+ * #GtkTextBuffer. Often times, you'll need to connect to many signals on
+ * #GtkTextBuffer from a #GtkTextView subclass. This allows you to create a
+ * signal group during instance construction, simply bind the
+ * #GtkTextView:buffer property to #IdeSignalGroup:target and connect
+ * all the signals you need. When the #GtkTextView:buffer property changes
+ * all of the signals will be transitioned correctly.
+ */
+
+struct _IdeSignalGroup
+{
+ GObject parent_instance;
+
+ GWeakRef target_ref;
+ GPtrArray *handlers;
+ GType target_type;
+ gsize block_count;
+
+ guint has_bound_at_least_once : 1;
+};
+
+struct _IdeSignalGroupClass
+{
+ GObjectClass parent_class;
+
+ void (*bind) (IdeSignalGroup *self,
+ GObject *target);
+};
+
+typedef struct
+{
+ IdeSignalGroup *group;
+ gulong handler_id;
+ GClosure *closure;
+ guint signal_id;
+ GQuark signal_detail;
+ guint connect_after : 1;
+} SignalHandler;
+
+G_DEFINE_TYPE (IdeSignalGroup, ide_signal_group, G_TYPE_OBJECT)
+
+enum {
+ PROP_0,
+ PROP_TARGET,
+ PROP_TARGET_TYPE,
+ LAST_PROP
+};
+
+enum {
+ BIND,
+ UNBIND,
+ LAST_SIGNAL
+};
+
+static GParamSpec *properties [LAST_PROP];
+static guint signals [LAST_SIGNAL];
+
+static void
+ide_signal_group_set_target_type (IdeSignalGroup *self,
+ GType target_type)
+{
+ g_assert (IDE_IS_SIGNAL_GROUP (self));
+ g_assert (g_type_is_a (target_type, G_TYPE_OBJECT));
+
+ self->target_type = target_type;
+
+ /* The class must be created at least once for the signals
+ * to be registered, otherwise g_signal_parse_name() will fail
+ */
+ if (G_TYPE_IS_INTERFACE (target_type))
+ {
+ if (g_type_default_interface_peek (target_type) == NULL)
+ g_type_default_interface_unref (g_type_default_interface_ref (target_type));
+ }
+ else
+ {
+ if (g_type_class_peek (target_type) == NULL)
+ g_type_class_unref (g_type_class_ref (target_type));
+ }
+}
+
+static void
+ide_signal_group_gc_handlers (IdeSignalGroup *self)
+{
+ g_assert (IDE_IS_SIGNAL_GROUP (self));
+
+ /*
+ * Remove any handlers for which the closures have become invalid. We do
+ * this cleanup lazily to avoid situations where we could have disposal
+ * active on both the signal group and the peer object.
+ */
+
+ for (guint i = self->handlers->len; i > 0; i--)
+ {
+ const SignalHandler *handler = g_ptr_array_index (self->handlers, i - 1);
+
+ g_assert (handler != NULL);
+ g_assert (handler->closure != NULL);
+
+ if (handler->closure->is_invalid)
+ g_ptr_array_remove_index (self->handlers, i - 1);
+ }
+}
+
+static void
+ide_signal_group__target_weak_notify (gpointer data,
+ GObject *where_object_was)
+{
+ IdeSignalGroup *self = data;
+
+ g_assert (IDE_IS_SIGNAL_GROUP (self));
+ g_assert (where_object_was != NULL);
+
+ g_weak_ref_set (&self->target_ref, NULL);
+
+ for (guint i = 0; i < self->handlers->len; i++)
+ {
+ SignalHandler *handler = g_ptr_array_index (self->handlers, i);
+
+ handler->handler_id = 0;
+ }
+
+ g_signal_emit (self, signals [UNBIND], 0);
+ g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_TARGET]);
+}
+
+static void
+ide_signal_group_bind_handler (IdeSignalGroup *self,
+ SignalHandler *handler,
+ GObject *target)
+{
+ g_assert (self != NULL);
+ g_assert (G_IS_OBJECT (target));
+ g_assert (handler != NULL);
+ g_assert (handler->signal_id != 0);
+ g_assert (handler->closure != NULL);
+ g_assert (handler->closure->is_invalid == 0);
+ g_assert (handler->handler_id == 0);
+
+ handler->handler_id = g_signal_connect_closure_by_id (target,
+ handler->signal_id,
+ handler->signal_detail,
+ handler->closure,
+ handler->connect_after);
+
+ g_assert (handler->handler_id != 0);
+
+ for (guint i = 0; i < self->block_count; i++)
+ g_signal_handler_block (target, handler->handler_id);
+}
+
+static void
+ide_signal_group_bind (IdeSignalGroup *self,
+ GObject *target)
+{
+ g_autoptr(GObject) hold = NULL;
+
+ g_assert (IDE_IS_SIGNAL_GROUP (self));
+ g_assert (!target || G_IS_OBJECT (target));
+
+ if (target == NULL)
+ return;
+
+ self->has_bound_at_least_once = TRUE;
+
+ hold = g_object_ref (target);
+
+ g_weak_ref_set (&self->target_ref, hold);
+ g_object_weak_ref (hold, ide_signal_group__target_weak_notify, self);
+
+ ide_signal_group_gc_handlers (self);
+
+ for (guint i = 0; i < self->handlers->len; i++)
+ {
+ SignalHandler *handler = g_ptr_array_index (self->handlers, i);
+
+ ide_signal_group_bind_handler (self, handler, hold);
+ }
+
+ g_signal_emit (self, signals [BIND], 0, hold);
+}
+
+static void
+ide_signal_group_unbind (IdeSignalGroup *self)
+{
+ g_autoptr(GObject) target = NULL;
+
+ g_return_if_fail (IDE_IS_SIGNAL_GROUP (self));
+
+ target = g_weak_ref_get (&self->target_ref);
+
+ /*
+ * Target may be NULL by this point, as we got notified of its destruction.
+ * However, if we're early enough, we may get a full reference back and can
+ * cleanly disconnect our connections.
+ */
+
+ if (target != NULL)
+ {
+ g_weak_ref_set (&self->target_ref, NULL);
+
+ /*
+ * Let go of our weak reference now that we have a full reference
+ * for the life of this function.
+ */
+ g_object_weak_unref (target,
+ ide_signal_group__target_weak_notify,
+ self);
+ }
+
+ ide_signal_group_gc_handlers (self);
+
+ for (guint i = 0; i < self->handlers->len; i++)
+ {
+ SignalHandler *handler;
+ gulong handler_id;
+
+ handler = g_ptr_array_index (self->handlers, i);
+
+ g_assert (handler != NULL);
+ g_assert (handler->signal_id != 0);
+ g_assert (handler->closure != NULL);
+
+ handler_id = handler->handler_id;
+ handler->handler_id = 0;
+
+ /*
+ * If @target is NULL, we lost a race to cleanup the weak
+ * instance and the signal connections have already been
+ * finalized and therefore nothing to do.
+ */
+
+ if (target != NULL && handler_id != 0)
+ g_signal_handler_disconnect (target, handler_id);
+ }
+
+ g_signal_emit (self, signals [UNBIND], 0);
+}
+
+static gboolean
+ide_signal_group_check_target_type (IdeSignalGroup *self,
+ gpointer target)
+{
+ if ((target != NULL) &&
+ !g_type_is_a (G_OBJECT_TYPE (target), self->target_type))
+ {
+ g_critical ("Failed to set IdeSignalGroup of target type %s "
+ "using target %p of type %s",
+ g_type_name (self->target_type),
+ target, G_OBJECT_TYPE_NAME (target));
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+/**
+ * ide_signal_group_block:
+ * @self: the #IdeSignalGroup
+ *
+ * Blocks all signal handlers managed by @self so they will not
+ * be called during any signal emissions. Must be unblocked exactly
+ * the same number of times it has been blocked to become active again.
+ *
+ * This blocked state will be kept across changes of the target instance.
+ *
+ * See: g_signal_handler_block().
+ */
+void
+ide_signal_group_block (IdeSignalGroup *self)
+{
+ g_autoptr(GObject) target = NULL;
+
+ g_return_if_fail (IDE_IS_SIGNAL_GROUP (self));
+ g_return_if_fail (self->block_count != G_MAXSIZE);
+
+ self->block_count++;
+
+ target = g_weak_ref_get (&self->target_ref);
+
+ if (target == NULL)
+ return;
+
+ for (guint i = 0; i < self->handlers->len; i++)
+ {
+ const SignalHandler *handler = g_ptr_array_index (self->handlers, i);
+
+ g_assert (handler != NULL);
+ g_assert (handler->signal_id != 0);
+ g_assert (handler->closure != NULL);
+ g_assert (handler->handler_id != 0);
+
+ g_signal_handler_block (target, handler->handler_id);
+ }
+}
+
+/**
+ * ide_signal_group_unblock:
+ * @self: the #IdeSignalGroup
+ *
+ * Unblocks all signal handlers managed by @self so they will be
+ * called again during any signal emissions unless it is blocked
+ * again. Must be unblocked exactly the same number of times it
+ * has been blocked to become active again.
+ *
+ * See: g_signal_handler_unblock().
+ */
+void
+ide_signal_group_unblock (IdeSignalGroup *self)
+{
+ g_autoptr(GObject) target = NULL;
+
+ g_return_if_fail (IDE_IS_SIGNAL_GROUP (self));
+ g_return_if_fail (self->block_count != 0);
+
+ self->block_count--;
+
+ target = g_weak_ref_get (&self->target_ref);
+
+ if (target == NULL)
+ return;
+
+ for (guint i = 0; i < self->handlers->len; i++)
+ {
+ const SignalHandler *handler = g_ptr_array_index (self->handlers, i);
+
+ g_assert (handler != NULL);
+ g_assert (handler->signal_id != 0);
+ g_assert (handler->closure != NULL);
+ g_assert (handler->handler_id != 0);
+
+ g_signal_handler_unblock (target, handler->handler_id);
+ }
+}
+
+/**
+ * ide_signal_group_get_target:
+ * @self: the #IdeSignalGroup
+ *
+ * Gets the target instance used when connecting signals.
+ *
+ * Returns: (nullable) (transfer none) (type GObject): The target instance.
+ */
+gpointer
+ide_signal_group_get_target (IdeSignalGroup *self)
+{
+ g_autoptr(GObject) target = NULL;
+
+ g_return_val_if_fail (IDE_IS_SIGNAL_GROUP (self), NULL);
+
+ target = g_weak_ref_get (&self->target_ref);
+
+ /*
+ * It is expected that this is called from a thread that owns a reference to
+ * the target, so we can pass back a borrowed reference. However, to ensure
+ * that we aren't racing in finalization of @target, we must ensure that the
+ * ref_count >= 2 (as our get just incremented by one).
+ */
+
+ if (target == NULL || target->ref_count < 2)
+ return NULL;
+
+ /* Unref and pass back a borrowed reference. This looks unsafe, but is safe
+ * because of our reference check above, so much as the assertion holds that
+ * the caller obeyed the ownership rules of this class.
+ */
+ return target;
+}
+
+/**
+ * ide_signal_group_set_target:
+ * @self: the #IdeSignalGroup.
+ * @target: (nullable) (type GObject): The target instance used
+ * when connecting signals.
+ *
+ * Sets the target instance used when connecting signals. Any signal
+ * that has been registered with ide_signal_group_connect_object() or
+ * similar functions will be connected to this object.
+ *
+ * If the target instance was previously set, signals will be
+ * disconnected from that object prior to connecting to @target.
+ */
+void
+ide_signal_group_set_target (IdeSignalGroup *self,
+ gpointer target)
+{
+ g_autoptr(GObject) object = NULL;
+
+ g_return_if_fail (IDE_IS_SIGNAL_GROUP (self));
+
+ object = g_weak_ref_get (&self->target_ref);
+
+ if (object == (GObject *)target)
+ return;
+
+ if (!ide_signal_group_check_target_type (self, target))
+ return;
+
+ /* Only emit unbind if we've ever called bind */
+ if (self->has_bound_at_least_once)
+ ide_signal_group_unbind (self);
+
+ ide_signal_group_bind (self, target);
+
+ g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_TARGET]);
+}
+
+static void
+signal_handler_free (gpointer data)
+{
+ SignalHandler *handler = data;
+
+ if (handler->closure != NULL)
+ g_closure_invalidate (handler->closure);
+
+ handler->handler_id = 0;
+ handler->signal_id = 0;
+ handler->signal_detail = 0;
+ g_clear_pointer (&handler->closure, g_closure_unref);
+ g_slice_free (SignalHandler, handler);
+}
+
+static void
+ide_signal_group_constructed (GObject *object)
+{
+ IdeSignalGroup *self = (IdeSignalGroup *)object;
+ g_autoptr(GObject) target = g_weak_ref_get (&self->target_ref);
+
+ if (!ide_signal_group_check_target_type (self, target))
+ ide_signal_group_set_target (self, NULL);
+
+ G_OBJECT_CLASS (ide_signal_group_parent_class)->constructed (object);
+}
+
+static void
+ide_signal_group_dispose (GObject *object)
+{
+ IdeSignalGroup *self = (IdeSignalGroup *)object;
+
+ ide_signal_group_gc_handlers (self);
+
+ if (self->has_bound_at_least_once)
+ ide_signal_group_unbind (self);
+
+ g_clear_pointer (&self->handlers, g_ptr_array_unref);
+
+ G_OBJECT_CLASS (ide_signal_group_parent_class)->dispose (object);
+}
+
+static void
+ide_signal_group_finalize (GObject *object)
+{
+ IdeSignalGroup *self = (IdeSignalGroup *)object;
+
+ g_weak_ref_clear (&self->target_ref);
+
+ G_OBJECT_CLASS (ide_signal_group_parent_class)->finalize (object);
+}
+
+static void
+ide_signal_group_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ IdeSignalGroup *self = IDE_SIGNAL_GROUP (object);
+
+ switch (prop_id)
+ {
+ case PROP_TARGET:
+ g_value_take_object (value, g_weak_ref_get (&self->target_ref));
+ break;
+
+ case PROP_TARGET_TYPE:
+ g_value_set_gtype (value, self->target_type);
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ }
+}
+
+static void
+ide_signal_group_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ IdeSignalGroup *self = IDE_SIGNAL_GROUP (object);
+
+ switch (prop_id)
+ {
+ case PROP_TARGET:
+ ide_signal_group_set_target (self, g_value_get_object (value));
+ break;
+
+ case PROP_TARGET_TYPE:
+ ide_signal_group_set_target_type (self, g_value_get_gtype (value));
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ }
+}
+
+static void
+ide_signal_group_class_init (IdeSignalGroupClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->constructed = ide_signal_group_constructed;
+ object_class->dispose = ide_signal_group_dispose;
+ object_class->finalize = ide_signal_group_finalize;
+ object_class->get_property = ide_signal_group_get_property;
+ object_class->set_property = ide_signal_group_set_property;
+
+ /**
+ * IdeSignalGroup:target
+ *
+ * The target instance used when connecting signals.
+ */
+ properties [PROP_TARGET] =
+ g_param_spec_object ("target",
+ "Target",
+ "The target instance used when connecting signals.",
+ G_TYPE_OBJECT,
+ (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
+
+ /**
+ * IdeSignalGroup:target-type
+ *
+ * The GType of the target property.
+ */
+ properties [PROP_TARGET_TYPE] =
+ g_param_spec_gtype ("target-type",
+ "Target Type",
+ "The GType of the target property.",
+ G_TYPE_OBJECT,
+ (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
+
+ g_object_class_install_properties (object_class, LAST_PROP, properties);
+
+ /**
+ * IdeSignalGroup::bind:
+ * @self: the #IdeSignalGroup
+ * @instance: a #GObject
+ *
+ * This signal is emitted when the target instance of @self
+ * is set to a new #GObject.
+ *
+ * This signal will only be emitted if the target of @self is non-%NULL.
+ */
+ signals [BIND] =
+ g_signal_new ("bind",
+ G_TYPE_FROM_CLASS (klass),
+ G_SIGNAL_RUN_LAST,
+ 0,
+ NULL, NULL, NULL,
+ G_TYPE_NONE,
+ 1,
+ G_TYPE_OBJECT);
+
+ /**
+ * IdeSignalGroup::unbind:
+ * @self: a #IdeSignalGroup
+ *
+ * This signal is emitted when the target instance of @self
+ * is set to a new #GObject.
+ *
+ * This signal will only be emitted if the previous target
+ * of @self is non-%NULL.
+ */
+ signals [UNBIND] =
+ g_signal_new ("unbind",
+ G_TYPE_FROM_CLASS (klass),
+ G_SIGNAL_RUN_LAST,
+ 0,
+ NULL, NULL, NULL,
+ G_TYPE_NONE,
+ 0);
+}
+
+static void
+ide_signal_group_init (IdeSignalGroup *self)
+{
+ self->handlers = g_ptr_array_new_with_free_func (signal_handler_free);
+ self->target_type = G_TYPE_OBJECT;
+}
+
+/**
+ * ide_signal_group_new:
+ * @target_type: the #GType of the target instance.
+ *
+ * Creates a new #IdeSignalGroup for target instances of @target_type.
+ *
+ * Returns: a new #IdeSignalGroup
+ */
+IdeSignalGroup *
+ide_signal_group_new (GType target_type)
+{
+ g_return_val_if_fail (g_type_is_a (target_type, G_TYPE_OBJECT), NULL);
+
+ return g_object_new (IDE_TYPE_SIGNAL_GROUP,
+ "target-type", target_type,
+ NULL);
+}
+
+static void
+ide_signal_group_connect_full (IdeSignalGroup *self,
+ const gchar *detailed_signal,
+ GCallback callback,
+ gpointer data,
+ GClosureNotify notify,
+ GConnectFlags flags,
+ gboolean is_object)
+{
+ g_autoptr(GObject) target = NULL;
+ SignalHandler *handler;
+ GClosure *closure;
+ guint signal_id;
+ GQuark signal_detail;
+
+ g_return_if_fail (IDE_IS_SIGNAL_GROUP (self));
+ g_return_if_fail (detailed_signal != NULL);
+ g_return_if_fail (g_signal_parse_name (detailed_signal, self->target_type,
+ &signal_id, &signal_detail, TRUE) != 0);
+ g_return_if_fail (callback != NULL);
+ g_return_if_fail (!is_object || G_IS_OBJECT (data));
+
+ if ((flags & G_CONNECT_SWAPPED) != 0)
+ closure = g_cclosure_new_swap (callback, data, notify);
+ else
+ closure = g_cclosure_new (callback, data, notify);
+
+ handler = g_slice_new0 (SignalHandler);
+ handler->group = self;
+ handler->signal_id = signal_id;
+ handler->signal_detail = signal_detail;
+ handler->closure = g_closure_ref (closure);
+ handler->connect_after = ((flags & G_CONNECT_AFTER) != 0);
+
+ g_closure_sink (closure);
+
+ if (is_object)
+ {
+ /* Set closure->is_invalid when data is disposed. We only track this to avoid
+ * reconnecting in the future. However, we do a round of cleanup when ever we
+ * connect a new object or the target changes to GC the old handlers.
+ */
+ g_object_watch_closure (data, closure);
+ }
+
+ g_ptr_array_add (self->handlers, handler);
+
+ target = g_weak_ref_get (&self->target_ref);
+ if (target != NULL)
+ ide_signal_group_bind_handler (self, handler, target);
+
+ /* Lazily remove any old handlers on connect */
+ ide_signal_group_gc_handlers (self);
+}
+
+/**
+ * ide_signal_group_connect_object: (skip)
+ * @self: a #IdeSignalGroup
+ * @detailed_signal: a string of the form "signal-name::detail"
+ * @c_handler: (scope notified): the #GCallback to connect
+ * @object: the #GObject to pass as data to @callback calls
+ *
+ * Connects @callback to the signal @detailed_signal
+ * on the target object of @self.
+ *
+ * Ensures that the @object stays alive during the call to @callback
+ * by temporarily adding a reference count. When the @object is destroyed
+ * the signal handler will automatically be removed.
+ *
+ * See: g_signal_connect_object().
+ */
+void
+ide_signal_group_connect_object (IdeSignalGroup *self,
+ const gchar *detailed_signal,
+ GCallback c_handler,
+ gpointer object,
+ GConnectFlags flags)
+{
+ g_return_if_fail (G_IS_OBJECT (object));
+
+ ide_signal_group_connect_full (self, detailed_signal, c_handler, object, NULL,
+ flags, TRUE);
+}
+
+/**
+ * ide_signal_group_connect_data:
+ * @self: a #IdeSignalGroup
+ * @detailed_signal: a string of the form "signal-name::detail"
+ * @c_handler: (scope notified) (closure data) (destroy notify): the #GCallback to connect
+ * @data: the data to pass to @callback calls
+ * @notify: function to be called when disposing of @self
+ * @flags: the flags used to create the signal connection
+ *
+ * Connects @callback to the signal @detailed_signal
+ * on the target instance of @self.
+ *
+ * See: g_signal_connect_data().
+ */
+void
+ide_signal_group_connect_data (IdeSignalGroup *self,
+ const gchar *detailed_signal,
+ GCallback c_handler,
+ gpointer data,
+ GClosureNotify notify,
+ GConnectFlags flags)
+{
+ ide_signal_group_connect_full (self, detailed_signal, c_handler, data, notify,
+ flags, FALSE);
+}
+
+/**
+ * ide_signal_group_connect: (skip)
+ * @self: a #IdeSignalGroup
+ * @detailed_signal: a string of the form "signal-name::detail"
+ * @c_handler: (scope notified): the #GCallback to connect
+ * @data: the data to pass to @callback calls
+ *
+ * Connects @callback to the signal @detailed_signal
+ * on the target instance of @self.
+ *
+ * See: g_signal_connect().
+ */
+void
+ide_signal_group_connect (IdeSignalGroup *self,
+ const gchar *detailed_signal,
+ GCallback c_handler,
+ gpointer data)
+{
+ ide_signal_group_connect_full (self, detailed_signal, c_handler, data, NULL,
+ 0, FALSE);
+}
+
+/**
+ * ide_signal_group_connect_after: (skip)
+ * @self: a #IdeSignalGroup
+ * @detailed_signal: a string of the form "signal-name::detail"
+ * @c_handler: (scope notified): the #GCallback to connect
+ * @data: the data to pass to @callback calls
+ *
+ * Connects @callback to the signal @detailed_signal
+ * on the target instance of @self.
+ *
+ * The @callback will be called after the default handler of the signal.
+ *
+ * See: g_signal_connect_after().
+ */
+void
+ide_signal_group_connect_after (IdeSignalGroup *self,
+ const gchar *detailed_signal,
+ GCallback c_handler,
+ gpointer data)
+{
+ ide_signal_group_connect_full (self, detailed_signal, c_handler,
+ data, NULL, G_CONNECT_AFTER, FALSE);
+}
+
+/**
+ * ide_signal_group_connect_swapped:
+ * @self: a #IdeSignalGroup
+ * @detailed_signal: a string of the form "signal-name::detail"
+ * @c_handler: (scope async): the #GCallback to connect
+ * @data: the data to pass to @callback calls
+ *
+ * Connects @callback to the signal @detailed_signal
+ * on the target instance of @self.
+ *
+ * The instance on which the signal is emitted and @data
+ * will be swapped when calling @callback.
+ *
+ * See: g_signal_connect_swapped().
+ */
+void
+ide_signal_group_connect_swapped (IdeSignalGroup *self,
+ const gchar *detailed_signal,
+ GCallback c_handler,
+ gpointer data)
+{
+ ide_signal_group_connect_full (self, detailed_signal, c_handler, data, NULL,
+ G_CONNECT_SWAPPED, FALSE);
+}
diff --git a/src/libide/core/ide-signal-group.h b/src/libide/core/ide-signal-group.h
new file mode 100644
index 000000000..c15630497
--- /dev/null
+++ b/src/libide/core/ide-signal-group.h
@@ -0,0 +1,80 @@
+/* ide-signal-group.h
+ *
+ * Copyright (C) 2015 Christian Hergert <christian hergert me>
+ * Copyright (C) 2015 Garrett Regier <garrettregier gmail com>
+ *
+ * This file is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of the
+ * License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#if !defined (IDE_CORE_INSIDE) && !defined (IDE_CORE_COMPILATION)
+# error "Only <libide-core.h> can be included directly."
+#endif
+
+#include <glib-object.h>
+
+#include "ide-version-macros.h"
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_SIGNAL_GROUP (ide_signal_group_get_type())
+
+IDE_AVAILABLE_IN_ALL
+G_DECLARE_FINAL_TYPE (IdeSignalGroup, ide_signal_group, IDE, SIGNAL_GROUP, GObject)
+
+IDE_AVAILABLE_IN_ALL
+IdeSignalGroup *ide_signal_group_new (GType target_type);
+
+IDE_AVAILABLE_IN_ALL
+void ide_signal_group_set_target (IdeSignalGroup *self,
+ gpointer target);
+IDE_AVAILABLE_IN_ALL
+gpointer ide_signal_group_get_target (IdeSignalGroup *self);
+
+IDE_AVAILABLE_IN_ALL
+void ide_signal_group_block (IdeSignalGroup *self);
+IDE_AVAILABLE_IN_ALL
+void ide_signal_group_unblock (IdeSignalGroup *self);
+
+IDE_AVAILABLE_IN_ALL
+void ide_signal_group_connect_object (IdeSignalGroup *self,
+ const gchar *detailed_signal,
+ GCallback c_handler,
+ gpointer object,
+ GConnectFlags flags);
+IDE_AVAILABLE_IN_ALL
+void ide_signal_group_connect_data (IdeSignalGroup *self,
+ const gchar *detailed_signal,
+ GCallback c_handler,
+ gpointer data,
+ GClosureNotify notify,
+ GConnectFlags flags);
+IDE_AVAILABLE_IN_ALL
+void ide_signal_group_connect (IdeSignalGroup *self,
+ const gchar *detailed_signal,
+ GCallback c_handler,
+ gpointer data);
+IDE_AVAILABLE_IN_ALL
+void ide_signal_group_connect_after (IdeSignalGroup *self,
+ const gchar *detailed_signal,
+ GCallback c_handler,
+ gpointer data);
+IDE_AVAILABLE_IN_ALL
+void ide_signal_group_connect_swapped (IdeSignalGroup *self,
+ const gchar *detailed_signal,
+ GCallback c_handler,
+ gpointer data);
+
+G_END_DECLS
diff --git a/src/libide/core/libide-core.h b/src/libide/core/libide-core.h
index 85cb7a9a0..bdb1f3fd5 100644
--- a/src/libide/core/libide-core.h
+++ b/src/libide/core/libide-core.h
@@ -24,6 +24,8 @@
#define IDE_CORE_INSIDE
+#include "ide-action-group.h"
+#include "ide-binding-group.h"
#include "ide-context.h"
#include "ide-context-addin.h"
#include "ide-debug.h"
@@ -35,6 +37,7 @@
#include "ide-object.h"
#include "ide-object-box.h"
#include "ide-settings.h"
+#include "ide-signal-group.h"
#include "ide-transfer.h"
#include "ide-transfer-manager.h"
#include "ide-version.h"
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]