[Xfce4-commits] [panel-plugins/xfce4-statusnotifier-plugin] 02/02: Add weak signal handler

noreply at xfce.org noreply at xfce.org
Sat Jul 22 18:31:09 CEST 2017


This is an automated email from the git hooks/post-receive script.

n   i   n   e   t   l   s       p   u   s   h   e   d       a       c   o   m   m   i   t       t   o       b   r   a   n   c   h       m   a   s   t   e   r   
   in repository panel-plugins/xfce4-statusnotifier-plugin.

commit 8375c6c919e185045890c46c9cb9be9f89e31858
Author: Viktor Odintsev <ninetls at xfce.org>
Date:   Sat Jul 22 16:39:34 2017 +0300

    Add weak signal handler
---
 panel-plugin/Makefile.am   |   4 +-
 panel-plugin/sn-box.c      |  26 ++-------
 panel-plugin/sn-button.c   |  21 ++-----
 panel-plugin/sn-icon-box.c |  47 +++------------
 panel-plugin/sn-util.c     | 138 +++++++++++++++++++++++++++++++++++++++++++++
 panel-plugin/sn-util.h     |  38 +++++++++++++
 6 files changed, 196 insertions(+), 78 deletions(-)

diff --git a/panel-plugin/Makefile.am b/panel-plugin/Makefile.am
index 163fac8..3934f46 100644
--- a/panel-plugin/Makefile.am
+++ b/panel-plugin/Makefile.am
@@ -32,7 +32,9 @@ libstatusnotifier_la_SOURCES = \
 	sn-item.c \
 	sn-item.h \
 	sn-plugin.c \
-	sn-plugin.h
+	sn-plugin.h \
+	sn-util.c \
+	sn-util.h
 
 libstatusnotifier_la_CFLAGS = \
 	$(GTK_CFLAGS) \
diff --git a/panel-plugin/sn-box.c b/panel-plugin/sn-box.c
index 90ec3ee..dadbc1f 100644
--- a/panel-plugin/sn-box.c
+++ b/panel-plugin/sn-box.c
@@ -30,6 +30,7 @@
 
 #include "sn-box.h"
 #include "sn-button.h"
+#include "sn-util.h"
 
 
 
@@ -80,9 +81,6 @@ struct _SnBox
 
   /* in theory it's possible to have multiple items with same name */
   GHashTable          *children;
-
-  gulong               config_collect_known_items_handler;
-  gulong               config_items_list_changed_handler;
 };
 
 G_DEFINE_TYPE (SnBox, sn_box, GTK_TYPE_CONTAINER)
@@ -130,18 +128,6 @@ sn_box_finalize (GObject *object)
 {
   SnBox *box = XFCE_SN_BOX (object);
 
-  if (box->config_collect_known_items_handler != 0)
-    {
-      g_signal_handler_disconnect (box->config, box->config_collect_known_items_handler);
-      box->config_collect_known_items_handler = 0;
-    }
-
-  if (box->config_items_list_changed_handler != 0)
-    {
-      g_signal_handler_disconnect (box->config, box->config_items_list_changed_handler);
-      box->config_items_list_changed_handler = 0;
-    }
-
   g_hash_table_destroy (box->children);
 
   G_OBJECT_CLASS (sn_box_parent_class)->finalize (object);
@@ -156,12 +142,10 @@ sn_box_new (SnConfig *config)
 
   box->config = config;
 
-  box->config_collect_known_items_handler =
-    g_signal_connect_swapped (G_OBJECT (box->config), "collect-known-items",
-                              G_CALLBACK (sn_box_collect_known_items), box);
-  box->config_items_list_changed_handler =
-    g_signal_connect_swapped (G_OBJECT (box->config), "items-list-changed",
-                              G_CALLBACK (sn_box_list_changed), box);
+  sn_signal_connect_weak_swapped (G_OBJECT (box->config), "collect-known-items",
+                                  G_CALLBACK (sn_box_collect_known_items), box);
+  sn_signal_connect_weak_swapped (G_OBJECT (box->config), "items-list-changed",
+                                  G_CALLBACK (sn_box_list_changed), box);
 
   return GTK_WIDGET (box);
 }
diff --git a/panel-plugin/sn-button.c b/panel-plugin/sn-button.c
index 56d2ba4..a243310 100644
--- a/panel-plugin/sn-button.c
+++ b/panel-plugin/sn-button.c
@@ -28,6 +28,7 @@
 
 #include "sn-button.h"
 #include "sn-icon-box.h"
+#include "sn-util.h"
 
 
 
@@ -74,8 +75,6 @@ struct _SnButton
 
   GtkWidget           *box;
 
-  guint                item_tooltip_changed_handler;
-  guint                item_menu_changed_handler;
   guint                menu_deactivate_handler;
 };
 
@@ -132,8 +131,6 @@ sn_button_init (SnButton *button)
 
   button->box = NULL;
 
-  button->item_tooltip_changed_handler = 0;
-  button->item_menu_changed_handler = 0;
   button->menu_deactivate_handler = 0;
 
   gtk_widget_set_halign (GTK_WIDGET (button), GTK_ALIGN_FILL);
@@ -186,12 +183,10 @@ sn_button_new (SnItem              *item,
   g_object_set (G_OBJECT (button), "has-tooltip", TRUE, NULL);
   g_signal_connect (button, "query-tooltip",
                     G_CALLBACK (sn_button_query_tooltip), NULL);
-  button->item_tooltip_changed_handler = 
-    g_signal_connect_swapped (item, "tooltip-changed",
-                              G_CALLBACK (gtk_widget_trigger_tooltip_query), button);
-  button->item_menu_changed_handler = 
-    g_signal_connect_swapped (item, "menu-changed",
-                              G_CALLBACK (sn_button_menu_changed), button);
+  sn_signal_connect_weak_swapped (item, "tooltip-changed",
+                                  G_CALLBACK (gtk_widget_trigger_tooltip_query), button);
+  sn_signal_connect_weak_swapped (item, "menu-changed",
+                                  G_CALLBACK (sn_button_menu_changed), button);
   sn_button_menu_changed (GTK_WIDGET (button), item);
 
   return GTK_WIDGET (button);
@@ -204,12 +199,6 @@ sn_button_finalize (GObject *object)
 {
   SnButton *button = XFCE_SN_BUTTON (object);
 
-  if (button->item_tooltip_changed_handler != 0)
-    g_signal_handler_disconnect (button->item, button->item_tooltip_changed_handler);
-
-  if (button->item_menu_changed_handler != 0)
-    g_signal_handler_disconnect (button->item, button->item_menu_changed_handler);
-
   if (button->menu_deactivate_handler != 0)
     g_signal_handler_disconnect (button->menu, button->menu_deactivate_handler);
 
diff --git a/panel-plugin/sn-icon-box.c b/panel-plugin/sn-icon-box.c
index f2a740f..9dde545 100644
--- a/panel-plugin/sn-icon-box.c
+++ b/panel-plugin/sn-icon-box.c
@@ -28,11 +28,10 @@
 #include <libxfce4panel/libxfce4panel.h>
 
 #include "sn-icon-box.h"
+#include "sn-util.h"
 
 
 
-static void                  sn_icon_box_finalize                    (GObject                 *object);
-
 static void                  sn_icon_box_icon_changed                (GtkWidget               *widget);
 
 static void                  sn_icon_box_get_preferred_width         (GtkWidget               *widget,
@@ -71,10 +70,6 @@ struct _SnIconBox
 
   GtkWidget           *icon;
   GtkWidget           *overlay;
-
-  guint                item_icon_changed_handler;
-  guint                config_notify_icon_size_handler;
-  guint                config_notify_symbolic_icons_handler;
 };
 
 G_DEFINE_TYPE (SnIconBox, sn_icon_box, GTK_TYPE_CONTAINER)
@@ -84,13 +79,9 @@ G_DEFINE_TYPE (SnIconBox, sn_icon_box, GTK_TYPE_CONTAINER)
 static void
 sn_icon_box_class_init (SnIconBoxClass *klass)
 {
-  GObjectClass      *object_class;
   GtkWidgetClass    *widget_class;
   GtkContainerClass *container_class;
 
-  object_class = G_OBJECT_CLASS (klass);
-  object_class->finalize = sn_icon_box_finalize;
-
   widget_class = GTK_WIDGET_CLASS (klass);
   widget_class->get_preferred_width = sn_icon_box_get_preferred_width;
   widget_class->get_preferred_height = sn_icon_box_get_preferred_height;
@@ -118,10 +109,6 @@ sn_icon_box_init (SnIconBox *box)
 
   box->icon = NULL;
   box->overlay = NULL;
-
-  box->item_icon_changed_handler = 0;
-  box->config_notify_icon_size_handler = 0;
-  box->config_notify_symbolic_icons_handler = 0;
 }
 
 
@@ -198,15 +185,12 @@ sn_icon_box_new (SnItem   *item,
   gtk_widget_set_parent (box->overlay, GTK_WIDGET (box));
   gtk_widget_show (box->overlay);
 
-  box->config_notify_icon_size_handler =
-    g_signal_connect_swapped (config, "notify::icon-size",
-                              G_CALLBACK (sn_icon_box_icon_changed), box);
-  box->config_notify_symbolic_icons_handler =
-    g_signal_connect_swapped (config, "notify::symbolic-icons",
-                              G_CALLBACK (sn_icon_box_icon_changed), box);
-  box->item_icon_changed_handler =
-    g_signal_connect_swapped (item, "icon-changed",
-                              G_CALLBACK (sn_icon_box_icon_changed), box);
+  sn_signal_connect_weak_swapped (config, "notify::icon-size",
+                                  G_CALLBACK (sn_icon_box_icon_changed), box);
+  sn_signal_connect_weak_swapped (config, "notify::symbolic-icons",
+                                  G_CALLBACK (sn_icon_box_icon_changed), box);
+  sn_signal_connect_weak_swapped (item, "icon-changed",
+                                  G_CALLBACK (sn_icon_box_icon_changed), box);
   sn_icon_box_icon_changed (GTK_WIDGET (box));
 
   return GTK_WIDGET (box);
@@ -215,23 +199,6 @@ sn_icon_box_new (SnItem   *item,
 
 
 static void
-sn_icon_box_finalize (GObject *object)
-{
-  SnIconBox *box = XFCE_SN_ICON_BOX (object);
-
-  if (box->item_icon_changed_handler != 0)
-    g_signal_handler_disconnect (box->item, box->item_icon_changed_handler);
-  if (box->config_notify_icon_size_handler != 0)
-    g_signal_handler_disconnect (box->config, box->config_notify_icon_size_handler);
-  if (box->config_notify_symbolic_icons_handler != 0)
-    g_signal_handler_disconnect (box->config, box->config_notify_symbolic_icons_handler);
-
-  G_OBJECT_CLASS (sn_icon_box_parent_class)->finalize (object);
-}
-
-
-
-static void
 sn_icon_box_apply_icon (GtkWidget    *image,
                         GtkIconTheme *icon_theme,
                         GtkIconTheme *icon_theme_from_path,
diff --git a/panel-plugin/sn-util.c b/panel-plugin/sn-util.c
new file mode 100644
index 0000000..a738d71
--- /dev/null
+++ b/panel-plugin/sn-util.c
@@ -0,0 +1,138 @@
+/*
+ *  Copyright (c) 2017 Viktor Odintsev <ninetls at xfce.org>
+ *
+ *  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 2 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 Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+
+#include "sn-util.h"
+
+
+
+static void                  sn_weak_handler_destroy_data            (gpointer                 data,
+                                                                      GObject                 *where_the_object_was);
+
+static void                  sn_weak_handler_destroy_instance        (gpointer                 data,
+                                                                      GObject                 *where_the_object_was);
+
+
+
+typedef struct
+{
+  gpointer instance;
+  gpointer data;
+  gulong   handler;
+}
+WeakHandler;
+
+
+
+static void
+sn_weak_handler_destroy_data (gpointer  data,
+                              GObject  *where_the_object_was)
+{
+  WeakHandler *weak_handler = data;
+
+  g_signal_handler_disconnect (weak_handler->instance,
+                               weak_handler->handler);
+  g_object_weak_unref (G_OBJECT (weak_handler->instance),
+                       sn_weak_handler_destroy_instance,
+                       weak_handler);
+
+  g_free (data);
+}
+
+
+
+static void
+sn_weak_handler_destroy_instance (gpointer  data,
+                                  GObject  *where_the_object_was)
+{
+  WeakHandler *weak_handler = data;
+
+  g_object_weak_unref (G_OBJECT (weak_handler->data),
+                       sn_weak_handler_destroy_data,
+                       weak_handler);
+
+  g_free (data);
+}
+
+
+
+static gulong
+sn_signal_connect_weak_internal (gpointer      instance,
+                                 const gchar  *detailed_signal,
+                                 GCallback     c_handler,
+                                 gpointer      data,
+                                 GConnectFlags connect_flags)
+{
+  gulong       handler;
+  WeakHandler *weak_handler;
+
+  g_return_val_if_fail (G_IS_OBJECT (data), 0);
+
+  handler = g_signal_connect_data (instance, detailed_signal,
+                                   c_handler, data,
+                                   NULL, connect_flags);
+
+  if (handler != 0 && instance != data)
+    {
+      weak_handler = g_new0 (WeakHandler, 1);
+      weak_handler->instance = instance;
+      weak_handler->data = data;
+      weak_handler->handler = handler;
+
+      g_object_weak_ref (G_OBJECT (data),
+                         sn_weak_handler_destroy_data,
+                         weak_handler);
+      g_object_weak_ref (G_OBJECT (instance),
+                         sn_weak_handler_destroy_instance,
+                         weak_handler);
+    }
+
+  return handler;
+}
+
+
+
+gulong
+sn_signal_connect_weak (gpointer     instance,
+                        const gchar *detailed_signal,
+                        GCallback    c_handler,
+                        gpointer     data)
+{
+  return sn_signal_connect_weak_internal (instance, detailed_signal,
+                                          c_handler, data, 0);
+}
+
+
+
+gulong
+sn_signal_connect_weak_swapped (gpointer     instance,
+                                const gchar *detailed_signal,
+                                GCallback    c_handler,
+                                gpointer     data)
+{
+  return sn_signal_connect_weak_internal (instance, detailed_signal,
+                                          c_handler, data, G_CONNECT_SWAPPED);
+}
diff --git a/panel-plugin/sn-util.h b/panel-plugin/sn-util.h
new file mode 100644
index 0000000..15e0baa
--- /dev/null
+++ b/panel-plugin/sn-util.h
@@ -0,0 +1,38 @@
+/*
+ *  Copyright (c) 2017 Viktor Odintsev <ninetls at xfce.org>
+ *
+ *  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 2 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 Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __SN_UTIL_H__
+#define __SN_UTIL_H__
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+gulong                 sn_signal_connect_weak                  (gpointer                 instance,
+                                                                const gchar             *detailed_signal,
+                                                                GCallback                c_handler,
+                                                                gpointer                 data);
+
+gulong                 sn_signal_connect_weak_swapped          (gpointer                 instance,
+                                                                const gchar             *detailed_signal,
+                                                                GCallback                c_handler,
+                                                                gpointer                 data);
+
+G_END_DECLS
+
+#endif /* !__SN_UTIL_H__ */

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Xfce4-commits mailing list