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.
This commit is contained in:
Alberts Muktupāvels 2020-01-13 15:13:28 +02:00
parent 6f34e84002
commit 5d791352f2

View File

@ -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 =