[gnome-settings-daemon] power: Don't leak notifications



commit 721bed3174bfe6a8f5cb454332848b7ab6178782
Author: Chris Coulson <chris coulson canonical com>
Date:   Thu Apr 12 11:54:51 2012 +0100

    power: Don't leak notifications
    
    https://bugzilla.gnome.org/show_bug.cgi?id=673978

 plugins/power/gsd-power-manager.c |   54 +++++++++++++++++++++++-------------
 1 files changed, 34 insertions(+), 20 deletions(-)
---
diff --git a/plugins/power/gsd-power-manager.c b/plugins/power/gsd-power-manager.c
index a8aacb1..8b7d4d6 100644
--- a/plugins/power/gsd-power-manager.c
+++ b/plugins/power/gsd-power-manager.c
@@ -1201,6 +1201,28 @@ engine_device_removed_cb (UpClient *client, UpDevice *device, GsdPowerManager *m
 }
 
 static void
+on_notification_closed (NotifyNotification *notification, gpointer data)
+{
+    g_object_unref (notification);
+}
+
+static void
+create_notification (const char *summary,
+                     const char *body,
+                     const char *icon,
+                     NotifyNotification **weak_pointer_location)
+{
+        NotifyNotification *notification;
+
+        notification = notify_notification_new (summary, body, icon);
+        *weak_pointer_location = notification;
+        g_object_add_weak_pointer (G_OBJECT (notification),
+                                   (gpointer *) weak_pointer_location);
+        g_signal_connect (notification, "closed",
+                          G_CALLBACK (on_notification_closed), NULL);
+}
+
+static void
 engine_ups_discharging (GsdPowerManager *manager, UpDevice *device)
 {
         const gchar *title;
@@ -1246,9 +1268,9 @@ engine_ups_discharging (GsdPowerManager *manager, UpDevice *device)
         notify_close_if_showing (manager->priv->notification_discharging);
 
         /* create a new notification */
-        manager->priv->notification_discharging = notify_notification_new (title,
-                                                                           message->str,
-                                                                           get_first_themed_icon_name (icon));
+        create_notification (title, message->str,
+                             get_first_themed_icon_name (icon),
+                             &manager->priv->notification_discharging);
         notify_notification_set_timeout (manager->priv->notification_discharging,
                                          GSD_POWER_MANAGER_NOTIFY_TIMEOUT_LONG);
         notify_notification_set_urgency (manager->priv->notification_discharging,
@@ -1257,8 +1279,6 @@ engine_ups_discharging (GsdPowerManager *manager, UpDevice *device)
         notify_notification_set_app_name (manager->priv->notification_discharging, _("Power"));
         notify_notification_set_hint (manager->priv->notification_discharging,
                                       "transient", g_variant_new_boolean (TRUE));
-        g_object_add_weak_pointer (G_OBJECT (manager->priv->notification_discharging),
-                                   (gpointer) &manager->priv->notification_discharging);
 
         /* try to show */
         ret = notify_notification_show (manager->priv->notification_discharging,
@@ -1425,9 +1445,9 @@ engine_charge_low (GsdPowerManager *manager, UpDevice *device)
         notify_close_if_showing (manager->priv->notification_low);
 
         /* create a new notification */
-        manager->priv->notification_low = notify_notification_new (title,
-                                                                   message,
-                                                                   get_first_themed_icon_name (icon));
+        create_notification (title, message,
+                             get_first_themed_icon_name (icon),
+                             &manager->priv->notification_low);
         notify_notification_set_timeout (manager->priv->notification_low,
                                          GSD_POWER_MANAGER_NOTIFY_TIMEOUT_LONG);
         notify_notification_set_urgency (manager->priv->notification_low,
@@ -1435,8 +1455,6 @@ engine_charge_low (GsdPowerManager *manager, UpDevice *device)
         notify_notification_set_app_name (manager->priv->notification_low, _("Power"));
         notify_notification_set_hint (manager->priv->notification_low,
                                       "transient", g_variant_new_boolean (TRUE));
-        g_object_add_weak_pointer (G_OBJECT (manager->priv->notification_low),
-                                   (gpointer) &manager->priv->notification_low);
 
         /* try to show */
         ret = notify_notification_show (manager->priv->notification_low,
@@ -1606,16 +1624,14 @@ engine_charge_critical (GsdPowerManager *manager, UpDevice *device)
         notify_close_if_showing (manager->priv->notification_low);
 
         /* create a new notification */
-        manager->priv->notification_low = notify_notification_new (title,
-                                                                   message,
-                                                                   get_first_themed_icon_name (icon));
+        create_notification (title, message,
+                             get_first_themed_icon_name (icon),
+                             &manager->priv->notification_low);
         notify_notification_set_timeout (manager->priv->notification_low,
                                          GSD_POWER_MANAGER_NOTIFY_TIMEOUT_NEVER);
         notify_notification_set_urgency (manager->priv->notification_low,
                                          NOTIFY_URGENCY_CRITICAL);
         notify_notification_set_app_name (manager->priv->notification_low, _("Power"));
-        g_object_add_weak_pointer (G_OBJECT (manager->priv->notification_low),
-                                   (gpointer) &manager->priv->notification_low);
 
         /* try to show */
         ret = notify_notification_show (manager->priv->notification_low,
@@ -1755,16 +1771,14 @@ engine_charge_action (GsdPowerManager *manager, UpDevice *device)
         notify_close_if_showing (manager->priv->notification_low);
 
         /* create a new notification */
-        manager->priv->notification_low = notify_notification_new (title,
-                                                                   message,
-                                                                   get_first_themed_icon_name (icon));
+        create_notification (title, message,
+                             get_first_themed_icon_name (icon),
+                             &manager->priv->notification_low);
         notify_notification_set_timeout (manager->priv->notification_low,
                                          GSD_POWER_MANAGER_NOTIFY_TIMEOUT_NEVER);
         notify_notification_set_urgency (manager->priv->notification_low,
                                          NOTIFY_URGENCY_CRITICAL);
         notify_notification_set_app_name (manager->priv->notification_low, _("Power"));
-        g_object_add_weak_pointer (G_OBJECT (manager->priv->notification_low),
-                                   (gpointer) &manager->priv->notification_low);
 
         /* try to show */
         ret = notify_notification_show (manager->priv->notification_low,



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