[glib/wip/muktupavels/notifications: 5/5] gfdonotificationbackend: remove notifications when bus name vanishes



commit 4a9d142b9491a15a0a46646bb93c89cbdd4a92ba
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date:   Mon Jan 13 15:13:28 2020 +0200

    gfdonotificationbackend: remove notifications when bus name vanishes
    
    Notification id (notify_id) is generated by notification daemon and
    is valid only while daemon is running. If notification backend will
    resend/reuse existing notification id (replace_id) after notification
    daemon has been restarted it could replace wrong notification as same
    id now can be used by different notification.

 gio/gfdonotificationbackend.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
---
diff --git a/gio/gfdonotificationbackend.c b/gio/gfdonotificationbackend.c
index c4fa0dc1d..b6fbfd2e2 100644
--- a/gio/gfdonotificationbackend.c
+++ b/gio/gfdonotificationbackend.c
@@ -25,6 +25,7 @@
 #include "giomodule-priv.h"
 #include "gnotification-private.h"
 #include "gdbusconnection.h"
+#include "gdbusnamewatching.h"
 #include "gactiongroup.h"
 #include "gaction.h"
 #include "gthemedicon.h"
@@ -42,6 +43,8 @@ struct _GFdoNotificationBackend
 {
   GNotificationBackend parent;
 
+  guint   bus_name_id;
+
   guint   notify_subscription;
   GSList *notifications;
 };
@@ -205,6 +208,20 @@ notify_signal (GDBusConnection *connection,
     }
 }
 
+static void
+name_vanished_handler_cb (GDBusConnection *connection,
+                          const gchar     *name,
+                          gpointer         user_data)
+{
+  GFdoNotificationBackend *backend = user_data;
+
+  if (backend->notifications)
+    {
+      g_slist_free_full (backend->notifications, freedesktop_notification_free);
+      backend->notifications = NULL;
+    }
+}
+
 /* Converts a GNotificationPriority to an urgency level as defined by
  * the freedesktop spec (0: low, 1: normal, 2: critical).
  */
@@ -370,6 +387,12 @@ g_fdo_notification_backend_dispose (GObject *object)
 {
   GFdoNotificationBackend *backend = G_FDO_NOTIFICATION_BACKEND (object);
 
+  if (backend->bus_name_id)
+    {
+      g_bus_unwatch_name (backend->bus_name_id);
+      backend->bus_name_id = 0;
+    }
+
   if (backend->notify_subscription)
     {
       GDBusConnection *session_bus;
@@ -407,6 +430,17 @@ g_fdo_notification_backend_send_notification (GNotificationBackend *backend,
   GFdoNotificationBackend *self = G_FDO_NOTIFICATION_BACKEND (backend);
   FreedesktopNotification *n, *tmp;
 
+  if (self->bus_name_id == 0)
+    {
+      self->bus_name_id = g_bus_watch_name_on_connection (backend->dbus_connection,
+                                                          "org.freedesktop.Notifications",
+                                                          G_BUS_NAME_WATCHER_FLAGS_NONE,
+                                                          NULL,
+                                                          name_vanished_handler_cb,
+                                                          backend,
+                                                          NULL);
+    }
+
   if (self->notify_subscription == 0)
     {
       self->notify_subscription =


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