2 * Copyright © 2013 Lars Uebernickel
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 * Authors: Lars Uebernickel <lars@uebernic.de>
22 #include "gnotificationbackend.h"
24 #include "gapplication.h"
25 #include "giomodule-priv.h"
26 #include "gnotification-private.h"
27 #include "gdbusconnection.h"
28 #include "gactiongroup.h"
30 #include "gfileicon.h"
32 #include "gdbusutils.h"
34 #define G_TYPE_FDO_NOTIFICATION_BACKEND (g_fdo_notification_backend_get_type ())
35 #define G_FDO_NOTIFICATION_BACKEND(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_FDO_NOTIFICATION_BACKEND, GFdoNotificationBackend))
37 typedef struct _GFdoNotificationBackend GFdoNotificationBackend;
38 typedef GNotificationBackendClass GFdoNotificationBackendClass;
40 struct _GFdoNotificationBackend
42 GNotificationBackend parent;
44 guint notify_subscription;
45 GSList *notifications;
48 GType g_fdo_notification_backend_get_type (void);
50 G_DEFINE_TYPE_WITH_CODE (GFdoNotificationBackend, g_fdo_notification_backend, G_TYPE_NOTIFICATION_BACKEND,
51 _g_io_modules_ensure_extension_points_registered ();
52 g_io_extension_point_implement (G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME,
53 g_define_type_id, "freedesktop", 0))
57 GFdoNotificationBackend *backend;
60 gchar *default_action;
61 GVariant *default_action_target;
62 } FreedesktopNotification;
66 freedesktop_notification_free (gpointer data)
68 FreedesktopNotification *n = data;
71 g_free (n->default_action);
72 if (n->default_action_target)
73 g_variant_unref (n->default_action_target);
75 g_slice_free (FreedesktopNotification, n);
78 static FreedesktopNotification *
79 g_fdo_notification_backend_find_notification (GFdoNotificationBackend *backend,
84 for (it = backend->notifications; it != NULL; it = it->next)
86 FreedesktopNotification *n = it->data;
87 if (g_str_equal (n->id, id))
94 static FreedesktopNotification *
95 g_fdo_notification_backend_find_notification_by_notify_id (GFdoNotificationBackend *backend,
100 for (it = backend->notifications; it != NULL; it = it->next)
102 FreedesktopNotification *n = it->data;
103 if (n->notify_id == id)
111 activate_action (GFdoNotificationBackend *backend,
115 GNotificationBackend *g_backend = G_NOTIFICATION_BACKEND (backend);
119 if (g_str_has_prefix (name, "app."))
120 g_action_group_activate_action (G_ACTION_GROUP (g_backend->application), name + 4, parameter);
124 g_application_activate (g_backend->application);
129 notify_signal (GDBusConnection *connection,
130 const gchar *sender_name,
131 const gchar *object_path,
132 const gchar *interface_name,
133 const gchar *signal_name,
134 GVariant *parameters,
137 GFdoNotificationBackend *backend = user_data;
139 const gchar *action = NULL;
140 FreedesktopNotification *n;
142 if (g_str_equal (signal_name, "NotificationClosed") &&
143 g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(uu)")))
145 g_variant_get (parameters, "(uu)", &id, NULL);
147 else if (g_str_equal (signal_name, "ActionInvoked") &&
148 g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(us)")))
150 g_variant_get (parameters, "(u&s)", &id, &action);
155 n = g_fdo_notification_backend_find_notification_by_notify_id (backend, id);
161 if (g_str_equal (action, "default"))
163 activate_action (backend, n->default_action, n->default_action_target);
170 if (g_action_parse_detailed_name (action, &name, &target, NULL))
172 activate_action (backend, name, target);
175 g_variant_unref (target);
180 backend->notifications = g_slist_remove (backend->notifications, n);
181 freedesktop_notification_free (n);
184 /* Converts a GNotificationPriority to an urgency level as defined by
185 * the freedesktop spec (0: low, 1: normal, 2: critical).
188 urgency_from_priority (GNotificationPriority priority)
192 case G_NOTIFICATION_PRIORITY_LOW:
196 case G_NOTIFICATION_PRIORITY_NORMAL:
199 case G_NOTIFICATION_PRIORITY_HIGH:
200 case G_NOTIFICATION_PRIORITY_URGENT:
206 call_notify (GDBusConnection *con,
209 GNotification *notification,
210 GAsyncReadyCallback callback,
213 GVariantBuilder action_builder;
216 GVariantBuilder hints_builder;
218 GVariant *parameters;
222 g_variant_builder_init (&action_builder, G_VARIANT_TYPE_STRING_ARRAY);
223 if (g_notification_get_default_action (notification, NULL, NULL))
225 g_variant_builder_add (&action_builder, "s", "default");
226 g_variant_builder_add (&action_builder, "s", "");
229 n_buttons = g_notification_get_n_buttons (notification);
230 for (i = 0; i < n_buttons; i++)
235 gchar *detailed_name;
237 g_notification_get_button (notification, i, &label, &action, &target);
238 detailed_name = g_action_print_detailed_name (action, target);
240 /* Actions named 'default' collide with libnotify's naming of the
241 * default action. Rewriting them to something unique is enough,
242 * because those actions can never be activated (they aren't
243 * prefixed with 'app.').
245 if (g_str_equal (detailed_name, "default"))
247 g_free (detailed_name);
248 detailed_name = g_dbus_generate_guid ();
251 g_variant_builder_add_value (&action_builder, g_variant_new_take_string (detailed_name));
252 g_variant_builder_add_value (&action_builder, g_variant_new_take_string (label));
256 g_variant_unref (target);
259 g_variant_builder_init (&hints_builder, G_VARIANT_TYPE ("a{sv}"));
260 g_variant_builder_add (&hints_builder, "{sv}", "desktop-entry",
261 g_variant_new_string (g_application_get_application_id (app)));
262 urgency = urgency_from_priority (g_notification_get_priority (notification));
263 g_variant_builder_add (&hints_builder, "{sv}", "urgency", g_variant_new_byte (urgency));
264 icon = g_notification_get_icon (notification);
265 if (icon != NULL && G_IS_FILE_ICON (icon))
269 file = g_file_icon_get_file (G_FILE_ICON (icon));
270 g_variant_builder_add (&hints_builder, "{sv}", "image-path",
271 g_variant_new_take_string (g_file_get_path (file)));
274 body = g_notification_get_body (notification);
276 parameters = g_variant_new ("(susssasa{sv}i)",
280 g_notification_get_title (notification),
284 -1); /* expire_timeout */
286 g_dbus_connection_call (con, "org.freedesktop.Notifications", "/org/freedesktop/Notifications",
287 "org.freedesktop.Notifications", "Notify",
288 parameters, G_VARIANT_TYPE ("(u)"),
289 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
290 callback, user_data);
294 notification_sent (GObject *source_object,
295 GAsyncResult *result,
298 FreedesktopNotification *n = user_data;
300 GError *error = NULL;
301 static gboolean warning_printed = FALSE;
303 val = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), result, &error);
306 g_variant_get (val, "(u)", &n->notify_id);
307 g_variant_unref (val);
311 if (!warning_printed)
313 g_warning ("unable to send notifications through org.freedesktop.Notifications: %s",
315 warning_printed = TRUE;
318 n->backend->notifications = g_slist_remove (n->backend->notifications, n);
319 freedesktop_notification_free (n);
321 g_error_free (error);
326 g_fdo_notification_backend_dispose (GObject *object)
328 GFdoNotificationBackend *backend = G_FDO_NOTIFICATION_BACKEND (object);
330 if (backend->notify_subscription)
332 GDBusConnection *session_bus;
334 session_bus = G_NOTIFICATION_BACKEND (backend)->dbus_connection;
335 g_dbus_connection_signal_unsubscribe (session_bus, backend->notify_subscription);
336 backend->notify_subscription = 0;
339 if (backend->notifications)
341 g_slist_free_full (backend->notifications, freedesktop_notification_free);
342 backend->notifications = NULL;
345 G_OBJECT_CLASS (g_fdo_notification_backend_parent_class)->dispose (object);
349 g_fdo_notification_backend_is_supported (void)
351 /* This is the fallback backend with the lowest priority. To avoid an
352 * unnecessary synchronous dbus call to check for
353 * org.freedesktop.Notifications, this function always succeeds. A
354 * warning will be printed when sending the first notification fails.
360 g_fdo_notification_backend_send_notification (GNotificationBackend *backend,
362 GNotification *notification)
364 GFdoNotificationBackend *self = G_FDO_NOTIFICATION_BACKEND (backend);
365 FreedesktopNotification *n;
367 if (self->notify_subscription == 0)
369 self->notify_subscription =
370 g_dbus_connection_signal_subscribe (backend->dbus_connection,
371 "org.freedesktop.Notifications",
372 "org.freedesktop.Notifications", NULL,
373 "/org/freedesktop/Notifications", NULL,
374 G_DBUS_SIGNAL_FLAGS_NONE,
375 notify_signal, backend, NULL);
378 n = g_fdo_notification_backend_find_notification (self, id);
381 n = g_slice_new0 (FreedesktopNotification);
383 n->id = g_strdup (id);
386 n->backend->notifications = g_slist_prepend (n->backend->notifications, n);
390 /* Only clear default action. All other fields are still valid */
391 g_clear_pointer (&n->default_action, g_free);
392 g_clear_pointer (&n->default_action_target, g_variant_unref);
395 g_notification_get_default_action (notification, &n->default_action, &n->default_action_target);
397 call_notify (backend->dbus_connection, backend->application, n->notify_id, notification, notification_sent, n);
401 g_fdo_notification_backend_withdraw_notification (GNotificationBackend *backend,
404 GFdoNotificationBackend *self = G_FDO_NOTIFICATION_BACKEND (backend);
405 FreedesktopNotification *n;
407 n = g_fdo_notification_backend_find_notification (self, id);
410 if (n->notify_id > 0)
412 g_dbus_connection_call (backend->dbus_connection,
413 "org.freedesktop.Notifications",
414 "/org/freedesktop/Notifications",
415 "org.freedesktop.Notifications", "CloseNotification",
416 g_variant_new ("(u)", n->id), NULL,
417 G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
420 self->notifications = g_slist_remove (self->notifications, n);
421 freedesktop_notification_free (n);
426 g_fdo_notification_backend_init (GFdoNotificationBackend *backend)
431 g_fdo_notification_backend_class_init (GFdoNotificationBackendClass *class)
433 GObjectClass *object_class = G_OBJECT_CLASS (class);
434 GNotificationBackendClass *backend_class = G_NOTIFICATION_BACKEND_CLASS (class);
436 object_class->dispose = g_fdo_notification_backend_dispose;
438 backend_class->is_supported = g_fdo_notification_backend_is_supported;
439 backend_class->send_notification = g_fdo_notification_backend_send_notification;
440 backend_class->withdraw_notification = g_fdo_notification_backend_withdraw_notification;