2 * Copyright © 2013 Lars Uebernickel
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Authors: Lars Uebernickel <lars@uebernic.de>
24 #include "gnotificationbackend.h"
26 #include "gapplication.h"
27 #include "giomodule-priv.h"
28 #include "gnotification-private.h"
29 #include "gdbusconnection.h"
30 #include "gdbusnamewatching.h"
31 #include "gactiongroup.h"
33 #include "gthemedicon.h"
34 #include "gfileicon.h"
36 #include "gdbusutils.h"
38 #define G_TYPE_FDO_NOTIFICATION_BACKEND (g_fdo_notification_backend_get_type ())
39 #define G_FDO_NOTIFICATION_BACKEND(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_FDO_NOTIFICATION_BACKEND, GFdoNotificationBackend))
41 typedef struct _GFdoNotificationBackend GFdoNotificationBackend;
42 typedef GNotificationBackendClass GFdoNotificationBackendClass;
44 struct _GFdoNotificationBackend
46 GNotificationBackend parent;
50 guint notify_subscription;
51 GSList *notifications;
54 GType g_fdo_notification_backend_get_type (void);
56 G_DEFINE_TYPE_WITH_CODE (GFdoNotificationBackend, g_fdo_notification_backend, G_TYPE_NOTIFICATION_BACKEND,
57 _g_io_modules_ensure_extension_points_registered ();
58 g_io_extension_point_implement (G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME,
59 g_define_type_id, "freedesktop", 0))
63 GFdoNotificationBackend *backend;
66 gchar *default_action; /* (nullable) (owned) */
67 GVariant *default_action_target; /* (nullable) (owned), not floating */
68 } FreedesktopNotification;
71 freedesktop_notification_free (gpointer data)
73 FreedesktopNotification *n = data;
75 g_object_unref (n->backend);
77 g_free (n->default_action);
78 if (n->default_action_target)
79 g_variant_unref (n->default_action_target);
81 g_slice_free (FreedesktopNotification, n);
84 static FreedesktopNotification *
85 freedesktop_notification_new (GFdoNotificationBackend *backend,
87 GNotification *notification)
89 FreedesktopNotification *n;
91 n = g_slice_new0 (FreedesktopNotification);
92 n->backend = g_object_ref (backend);
93 n->id = g_strdup (id);
95 g_notification_get_default_action (notification,
97 &n->default_action_target);
102 static FreedesktopNotification *
103 g_fdo_notification_backend_find_notification (GFdoNotificationBackend *backend,
108 for (it = backend->notifications; it != NULL; it = it->next)
110 FreedesktopNotification *n = it->data;
111 if (g_str_equal (n->id, id))
118 static FreedesktopNotification *
119 g_fdo_notification_backend_find_notification_by_notify_id (GFdoNotificationBackend *backend,
124 for (it = backend->notifications; it != NULL; it = it->next)
126 FreedesktopNotification *n = it->data;
127 if (n->notify_id == id)
135 activate_action (GFdoNotificationBackend *backend,
139 GNotificationBackend *g_backend = G_NOTIFICATION_BACKEND (backend);
141 /* Callers should not provide a floating variant here */
142 g_assert (parameter == NULL || !g_variant_is_floating (parameter));
145 g_str_has_prefix (name, "app."))
147 const GVariantType *parameter_type = NULL;
148 const gchar *action_name = name + strlen ("app.");
150 /* @name and @parameter come as untrusted input over D-Bus, so validate them first */
151 if (g_action_group_query_action (G_ACTION_GROUP (g_backend->application),
152 action_name, NULL, ¶meter_type,
154 ((parameter_type == NULL && parameter == NULL) ||
155 (parameter_type != NULL && parameter != NULL && g_variant_is_of_type (parameter, parameter_type))))
157 g_action_group_activate_action (G_ACTION_GROUP (g_backend->application), action_name, parameter);
161 else if (name == NULL)
163 g_application_activate (g_backend->application);
171 notify_signal (GDBusConnection *connection,
172 const gchar *sender_name,
173 const gchar *object_path,
174 const gchar *interface_name,
175 const gchar *signal_name,
176 GVariant *parameters,
179 GFdoNotificationBackend *backend = user_data;
181 const gchar *action = NULL;
182 FreedesktopNotification *n;
183 gboolean notification_closed = TRUE;
185 if (g_str_equal (signal_name, "NotificationClosed") &&
186 g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(uu)")))
188 g_variant_get (parameters, "(uu)", &id, NULL);
190 else if (g_str_equal (signal_name, "ActionInvoked") &&
191 g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(us)")))
193 g_variant_get (parameters, "(u&s)", &id, &action);
198 n = g_fdo_notification_backend_find_notification_by_notify_id (backend, id);
204 if (g_str_equal (action, "default"))
206 if (!activate_action (backend, n->default_action, n->default_action_target))
207 notification_closed = FALSE;
212 GVariant *target = NULL;
214 if (!g_action_parse_detailed_name (action, &name, &target, NULL) ||
215 !activate_action (backend, name, target))
216 notification_closed = FALSE;
219 g_clear_pointer (&target, g_variant_unref);
223 /* Remove the notification, as it’s either been explicitly closed
224 * (`NotificationClosed` signal) or has been closed as a result of activating
225 * an action successfully. GLib doesn’t currently support the `resident` hint
226 * on notifications which would allow them to stay around after having an
227 * action invoked on them (see
228 * https://specifications.freedesktop.org/notification-spec/notification-spec-latest.html#idm45877717456448)
230 * First, get the notification again in case the action redrew it */
231 if (notification_closed)
233 n = g_fdo_notification_backend_find_notification_by_notify_id (backend, id);
236 backend->notifications = g_slist_remove (backend->notifications, n);
237 freedesktop_notification_free (n);
243 name_vanished_handler_cb (GDBusConnection *connection,
247 GFdoNotificationBackend *backend = user_data;
249 if (backend->notifications)
251 g_slist_free_full (backend->notifications, freedesktop_notification_free);
252 backend->notifications = NULL;
256 /* Converts a GNotificationPriority to an urgency level as defined by
257 * the freedesktop spec (0: low, 1: normal, 2: critical).
260 urgency_from_priority (GNotificationPriority priority)
264 case G_NOTIFICATION_PRIORITY_LOW:
268 case G_NOTIFICATION_PRIORITY_NORMAL:
269 case G_NOTIFICATION_PRIORITY_HIGH:
272 case G_NOTIFICATION_PRIORITY_URGENT:
278 call_notify (GDBusConnection *con,
281 GNotification *notification,
282 GAsyncReadyCallback callback,
285 GVariantBuilder action_builder;
288 GVariantBuilder hints_builder;
290 GVariant *parameters;
291 const gchar *app_name;
295 g_variant_builder_init (&action_builder, G_VARIANT_TYPE_STRING_ARRAY);
296 if (g_notification_get_default_action (notification, NULL, NULL))
298 g_variant_builder_add (&action_builder, "s", "default");
299 g_variant_builder_add (&action_builder, "s", "");
302 n_buttons = g_notification_get_n_buttons (notification);
303 for (i = 0; i < n_buttons; i++)
308 gchar *detailed_name;
310 g_notification_get_button (notification, i, &label, &action, &target);
311 detailed_name = g_action_print_detailed_name (action, target);
313 /* Actions named 'default' collide with libnotify's naming of the
314 * default action. Rewriting them to something unique is enough,
315 * because those actions can never be activated (they aren't
316 * prefixed with 'app.').
318 if (g_str_equal (detailed_name, "default"))
320 g_free (detailed_name);
321 detailed_name = g_dbus_generate_guid ();
324 g_variant_builder_add_value (&action_builder, g_variant_new_take_string (detailed_name));
325 g_variant_builder_add_value (&action_builder, g_variant_new_take_string (label));
329 g_variant_unref (target);
332 g_variant_builder_init (&hints_builder, G_VARIANT_TYPE ("a{sv}"));
333 g_variant_builder_add (&hints_builder, "{sv}", "desktop-entry",
334 g_variant_new_string (g_application_get_application_id (app)));
335 urgency = urgency_from_priority (g_notification_get_priority (notification));
336 g_variant_builder_add (&hints_builder, "{sv}", "urgency", g_variant_new_byte (urgency));
337 if (g_notification_get_category (notification))
339 g_variant_builder_add (&hints_builder, "{sv}", "category",
340 g_variant_new_string (g_notification_get_category (notification)));
343 icon = g_notification_get_icon (notification);
346 if (G_IS_FILE_ICON (icon))
350 file = g_file_icon_get_file (G_FILE_ICON (icon));
351 g_variant_builder_add (&hints_builder, "{sv}", "image-path",
352 g_variant_new_take_string (g_file_get_path (file)));
354 else if (G_IS_THEMED_ICON (icon))
356 const gchar* const* icon_names = g_themed_icon_get_names(G_THEMED_ICON (icon));
357 /* Take first name from GThemedIcon */
358 g_variant_builder_add (&hints_builder, "{sv}", "image-path",
359 g_variant_new_string (icon_names[0]));
363 app_name = g_get_application_name ();
364 body = g_notification_get_body (notification);
366 parameters = g_variant_new ("(susssasa{sv}i)",
367 app_name ? app_name : "",
370 g_notification_get_title (notification),
374 -1); /* expire_timeout */
376 g_dbus_connection_call (con, "org.freedesktop.Notifications", "/org/freedesktop/Notifications",
377 "org.freedesktop.Notifications", "Notify",
378 parameters, G_VARIANT_TYPE ("(u)"),
379 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
380 callback, user_data);
384 notification_sent (GObject *source_object,
385 GAsyncResult *result,
388 FreedesktopNotification *n = user_data;
390 GError *error = NULL;
391 static gboolean warning_printed = FALSE;
393 val = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), result, &error);
396 GFdoNotificationBackend *backend = n->backend;
397 FreedesktopNotification *match;
399 g_variant_get (val, "(u)", &n->notify_id);
400 g_variant_unref (val);
402 match = g_fdo_notification_backend_find_notification_by_notify_id (backend, n->notify_id);
405 backend->notifications = g_slist_remove (backend->notifications, match);
406 freedesktop_notification_free (match);
408 backend->notifications = g_slist_prepend (backend->notifications, n);
412 if (!warning_printed)
414 g_warning ("unable to send notifications through org.freedesktop.Notifications: %s",
416 warning_printed = TRUE;
419 freedesktop_notification_free (n);
420 g_error_free (error);
425 g_fdo_notification_backend_dispose (GObject *object)
427 GFdoNotificationBackend *backend = G_FDO_NOTIFICATION_BACKEND (object);
429 if (backend->bus_name_id)
431 g_bus_unwatch_name (backend->bus_name_id);
432 backend->bus_name_id = 0;
435 if (backend->notify_subscription)
437 GDBusConnection *session_bus;
439 session_bus = G_NOTIFICATION_BACKEND (backend)->dbus_connection;
440 g_dbus_connection_signal_unsubscribe (session_bus, backend->notify_subscription);
441 backend->notify_subscription = 0;
444 if (backend->notifications)
446 g_slist_free_full (backend->notifications, freedesktop_notification_free);
447 backend->notifications = NULL;
450 G_OBJECT_CLASS (g_fdo_notification_backend_parent_class)->dispose (object);
454 g_fdo_notification_backend_is_supported (void)
456 /* This is the fallback backend with the lowest priority. To avoid an
457 * unnecessary synchronous dbus call to check for
458 * org.freedesktop.Notifications, this function always succeeds. A
459 * warning will be printed when sending the first notification fails.
465 g_fdo_notification_backend_send_notification (GNotificationBackend *backend,
467 GNotification *notification)
469 GFdoNotificationBackend *self = G_FDO_NOTIFICATION_BACKEND (backend);
470 FreedesktopNotification *n, *tmp;
472 if (self->bus_name_id == 0)
474 self->bus_name_id = g_bus_watch_name_on_connection (backend->dbus_connection,
475 "org.freedesktop.Notifications",
476 G_BUS_NAME_WATCHER_FLAGS_NONE,
478 name_vanished_handler_cb,
483 if (self->notify_subscription == 0)
485 self->notify_subscription =
486 g_dbus_connection_signal_subscribe (backend->dbus_connection,
487 "org.freedesktop.Notifications",
488 "org.freedesktop.Notifications", NULL,
489 "/org/freedesktop/Notifications", NULL,
490 G_DBUS_SIGNAL_FLAGS_NONE,
491 notify_signal, backend, NULL);
494 n = freedesktop_notification_new (self, id, notification);
496 tmp = g_fdo_notification_backend_find_notification (self, id);
498 n->notify_id = tmp->notify_id;
500 call_notify (backend->dbus_connection, backend->application, n->notify_id, notification, notification_sent, n);
504 g_fdo_notification_backend_withdraw_notification (GNotificationBackend *backend,
507 GFdoNotificationBackend *self = G_FDO_NOTIFICATION_BACKEND (backend);
508 FreedesktopNotification *n;
510 n = g_fdo_notification_backend_find_notification (self, id);
513 if (n->notify_id > 0)
515 g_dbus_connection_call (backend->dbus_connection,
516 "org.freedesktop.Notifications",
517 "/org/freedesktop/Notifications",
518 "org.freedesktop.Notifications", "CloseNotification",
519 g_variant_new ("(u)", n->notify_id), NULL,
520 G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
523 self->notifications = g_slist_remove (self->notifications, n);
524 freedesktop_notification_free (n);
529 g_fdo_notification_backend_init (GFdoNotificationBackend *backend)
534 g_fdo_notification_backend_class_init (GFdoNotificationBackendClass *class)
536 GObjectClass *object_class = G_OBJECT_CLASS (class);
537 GNotificationBackendClass *backend_class = G_NOTIFICATION_BACKEND_CLASS (class);
539 object_class->dispose = g_fdo_notification_backend_dispose;
541 backend_class->is_supported = g_fdo_notification_backend_is_supported;
542 backend_class->send_notification = g_fdo_notification_backend_send_notification;
543 backend_class->withdraw_notification = g_fdo_notification_backend_withdraw_notification;