[tepl] Implement small utility TeplSignalGroup



commit f81eba5a4190571a4c1afcd662f7286b5820970d
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Jul 21 10:29:02 2017 +0200

    Implement small utility TeplSignalGroup

 docs/reference/Makefile.am |    3 +-
 po/POTFILES.in             |    1 +
 tepl/Makefile.am           |    6 ++-
 tepl/tepl-signal-group.c   |   98 ++++++++++++++++++++++++++++++++++++++++++++
 tepl/tepl-signal-group.h   |   41 ++++++++++++++++++
 5 files changed, 146 insertions(+), 3 deletions(-)
---
diff --git a/docs/reference/Makefile.am b/docs/reference/Makefile.am
index 2ea6015..86b4a0c 100644
--- a/docs/reference/Makefile.am
+++ b/docs/reference/Makefile.am
@@ -32,7 +32,8 @@ IGNORE_HFILES =                                       \
        tepl-encoding-private.h                 \
        tepl-file-content-loader.h              \
        tepl-io-error-info-bar.h                \
-       tepl-progress-info-bar.h
+       tepl-progress-info-bar.h                \
+       tepl-signal-group.h
 
 # Extra options to supply to gtkdoc-mkdb
 MKDB_OPTIONS = --xml-mode --output-format=xml
diff --git a/po/POTFILES.in b/po/POTFILES.in
index f9b92f0..236ba41 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -27,6 +27,7 @@ tepl/tepl-iter.c
 tepl/tepl-menu-shell.c
 tepl/tepl-metadata-manager.c
 tepl/tepl-notebook.c
+tepl/tepl-signal-group.c
 tepl/tepl-tab.c
 tepl/tepl-tab-group.c
 tepl/tepl-utils.c
diff --git a/tepl/Makefile.am b/tepl/Makefile.am
index 86084f7..85f9832 100644
--- a/tepl/Makefile.am
+++ b/tepl/Makefile.am
@@ -62,7 +62,8 @@ tepl_private_headers =                                \
        tepl-encoding-private.h                 \
        tepl-file-content-loader.h              \
        tepl-io-error-info-bar.h                \
-       tepl-progress-info-bar.h
+       tepl-progress-info-bar.h                \
+       tepl-signal-group.h
 
 tepl_private_c_files =                         \
        tepl-application-window-actions.c       \
@@ -71,7 +72,8 @@ tepl_private_c_files =                                \
        tepl-file-content-loader.c              \
        tepl-init.c                             \
        tepl-io-error-info-bar.c                \
-       tepl-progress-info-bar.c
+       tepl-progress-info-bar.c                \
+       tepl-signal-group.c
 
 tepl_built_public_headers =            \
        tepl-enum-types.h
diff --git a/tepl/tepl-signal-group.c b/tepl/tepl-signal-group.c
new file mode 100644
index 0000000..c6d836e
--- /dev/null
+++ b/tepl/tepl-signal-group.c
@@ -0,0 +1,98 @@
+/*
+ * This file is part of Tepl, a text editor library.
+ *
+ * Copyright 2017 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * Tepl 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.
+ *
+ * Tepl 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 Lesser General Public License
+ * along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "tepl-signal-group.h"
+
+/* Small utility to disconnect signal handlers without the need to keep around
+ * the GObject instance where the signals were connected.
+ *
+ * It is similar to DzlSignalGroup from libdazzle. TeplSignalGroup is less
+ * convenient to use, it contains less features, but this has the advantage that
+ * the implementation is *much* simpler, and I can thus be more confident that
+ * it is bug-free (which is more important to me). If one day DzlSignalGroup is
+ * included in GObject or GIO (after being properly reviewed), then I'll
+ * probably start using it. -- swilmet
+ */
+
+struct _TeplSignalGroup
+{
+       /* The GObject that the signal handlers are connected to.
+        * Weak ref.
+        */
+       GObject *object;
+
+       /* The IDs of the signal handlers. Element-type: gulong. */
+       GArray *handler_ids;
+};
+
+TeplSignalGroup *
+_tepl_signal_group_new (GObject *object)
+{
+       TeplSignalGroup *group;
+
+       g_return_val_if_fail (G_IS_OBJECT (object), NULL);
+
+       group = g_new0 (TeplSignalGroup, 1);
+
+       group->object = object;
+       g_object_add_weak_pointer (object, (gpointer *) &group->object);
+
+       group->handler_ids = g_array_new (FALSE, TRUE, sizeof (gulong));
+
+       return group;
+}
+
+void
+_tepl_signal_group_free (TeplSignalGroup *group)
+{
+       if (group == NULL)
+       {
+               return;
+       }
+
+       if (group->object != NULL)
+       {
+               guint i;
+
+               /* Disconnect all signal handlers. */
+               for (i = 0; i < group->handler_ids->len; i++)
+               {
+                       gulong handler_id;
+
+                       handler_id = g_array_index (group->handler_ids, gulong, i);
+
+                       g_signal_handler_disconnect (group->object, handler_id);
+               }
+
+               g_object_remove_weak_pointer (group->object, (gpointer *) &group->object);
+               group->object = NULL;
+       }
+
+       g_array_free (group->handler_ids, TRUE);
+       g_free (group);
+}
+
+void
+_tepl_signal_group_add_handler_id (TeplSignalGroup *group,
+                                  gulong           signal_handler_id)
+{
+       g_return_if_fail (group != NULL);
+
+       g_array_append_val (group->handler_ids, signal_handler_id);
+}
diff --git a/tepl/tepl-signal-group.h b/tepl/tepl-signal-group.h
new file mode 100644
index 0000000..10c7a6a
--- /dev/null
+++ b/tepl/tepl-signal-group.h
@@ -0,0 +1,41 @@
+/*
+ * This file is part of Tepl, a text editor library.
+ *
+ * Copyright 2017 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * Tepl 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.
+ *
+ * Tepl 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 Lesser General Public License
+ * along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TEPL_SIGNAL_GROUP_H
+#define TEPL_SIGNAL_GROUP_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+typedef struct _TeplSignalGroup TeplSignalGroup;
+
+G_GNUC_INTERNAL
+TeplSignalGroup *      _tepl_signal_group_new                  (GObject *object);
+
+G_GNUC_INTERNAL
+void                   _tepl_signal_group_free                 (TeplSignalGroup *group);
+
+G_GNUC_INTERNAL
+void                   _tepl_signal_group_add_handler_id       (TeplSignalGroup *group,
+                                                                gulong           signal_handler_id);
+
+G_END_DECLS
+
+#endif /* TEPL_SIGNAL_GROUP_H */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]