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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * 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);
185 call_notify (GDBusConnection *con,
188 GNotification *notification,
189 GAsyncReadyCallback callback,
192 GVariantBuilder action_builder;
195 GVariantBuilder hints_builder;
197 GVariant *parameters;
200 g_variant_builder_init (&action_builder, G_VARIANT_TYPE_STRING_ARRAY);
201 if (g_notification_get_default_action (notification, NULL, NULL))
203 g_variant_builder_add (&action_builder, "s", "default");
204 g_variant_builder_add (&action_builder, "s", "");
207 n_buttons = g_notification_get_n_buttons (notification);
208 for (i = 0; i < n_buttons; i++)
213 gchar *detailed_name;
215 g_notification_get_button (notification, i, &label, &action, &target);
216 detailed_name = g_action_print_detailed_name (action, target);
218 /* Actions named 'default' collide with libnotify's naming of the
219 * default action. Rewriting them to something unique is enough,
220 * because those actions can never be activated (they aren't
221 * prefixed with 'app.').
223 if (g_str_equal (detailed_name, "default"))
225 g_free (detailed_name);
226 detailed_name = g_dbus_generate_guid ();
229 g_variant_builder_add_value (&action_builder, g_variant_new_take_string (detailed_name));
230 g_variant_builder_add_value (&action_builder, g_variant_new_take_string (label));
234 g_variant_unref (target);
237 g_variant_builder_init (&hints_builder, G_VARIANT_TYPE ("a{sv}"));
238 g_variant_builder_add (&hints_builder, "{sv}", "desktop-entry",
239 g_variant_new_string (g_application_get_application_id (app)));
240 if (g_notification_get_urgent (notification))
241 g_variant_builder_add (&hints_builder, "{sv}", "urgency", g_variant_new_byte (2));
242 icon = g_notification_get_icon (notification);
243 if (icon != NULL && G_IS_FILE_ICON (icon))
247 file = g_file_icon_get_file (G_FILE_ICON (icon));
248 g_variant_builder_add (&hints_builder, "{sv}", "image-path",
249 g_variant_new_take_string (g_file_get_path (file)));
252 body = g_notification_get_body (notification);
254 parameters = g_variant_new ("(susssasa{sv}i)",
258 g_notification_get_title (notification),
262 -1); /* expire_timeout */
264 g_dbus_connection_call (con, "org.freedesktop.Notifications", "/org/freedesktop/Notifications",
265 "org.freedesktop.Notifications", "Notify",
266 parameters, G_VARIANT_TYPE ("(u)"),
267 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
268 callback, user_data);
272 notification_sent (GObject *source_object,
273 GAsyncResult *result,
276 FreedesktopNotification *n = user_data;
278 GError *error = NULL;
279 static gboolean warning_printed = FALSE;
281 val = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), result, &error);
284 g_variant_get (val, "(u)", &n->notify_id);
285 g_variant_unref (val);
289 if (!warning_printed)
291 g_warning ("unable to send notifications through org.freedesktop.Notifications: %s",
293 warning_printed = TRUE;
296 n->backend->notifications = g_slist_remove (n->backend->notifications, n);
297 freedesktop_notification_free (n);
299 g_error_free (error);
304 g_fdo_notification_backend_dispose (GObject *object)
306 GFdoNotificationBackend *backend = G_FDO_NOTIFICATION_BACKEND (object);
308 if (backend->notify_subscription)
310 GDBusConnection *session_bus;
312 session_bus = G_NOTIFICATION_BACKEND (backend)->dbus_connection;
313 g_dbus_connection_signal_unsubscribe (session_bus, backend->notify_subscription);
314 backend->notify_subscription = 0;
317 if (backend->notifications)
319 g_slist_free_full (backend->notifications, freedesktop_notification_free);
320 backend->notifications = NULL;
323 G_OBJECT_CLASS (g_fdo_notification_backend_parent_class)->dispose (object);
327 g_fdo_notification_backend_is_supported (void)
329 /* This is the fallback backend with the lowest priority. To avoid an
330 * unnecessary synchronous dbus call to check for
331 * org.freedesktop.Notifications, this function always succeeds. A
332 * warning will be printed when sending the first notification fails.
338 g_fdo_notification_backend_send_notification (GNotificationBackend *backend,
340 GNotification *notification)
342 GFdoNotificationBackend *self = G_FDO_NOTIFICATION_BACKEND (backend);
343 FreedesktopNotification *n;
345 if (self->notify_subscription == 0)
347 self->notify_subscription =
348 g_dbus_connection_signal_subscribe (backend->dbus_connection,
349 "org.freedesktop.Notifications",
350 "org.freedesktop.Notifications", NULL,
351 "/org/freedesktop/Notifications", NULL,
352 G_DBUS_SIGNAL_FLAGS_NONE,
353 notify_signal, backend, NULL);
356 n = g_fdo_notification_backend_find_notification (self, id);
359 n = g_slice_new0 (FreedesktopNotification);
361 n->id = g_strdup (id);
364 n->backend->notifications = g_slist_prepend (n->backend->notifications, n);
368 /* Only clear default action. All other fields are still valid */
369 g_clear_pointer (&n->default_action, g_free);
370 g_clear_pointer (&n->default_action_target, g_variant_unref);
373 g_notification_get_default_action (notification, &n->default_action, &n->default_action_target);
375 call_notify (backend->dbus_connection, backend->application, n->notify_id, notification, notification_sent, n);
379 g_fdo_notification_backend_withdraw_notification (GNotificationBackend *backend,
382 GFdoNotificationBackend *self = G_FDO_NOTIFICATION_BACKEND (backend);
383 FreedesktopNotification *n;
385 n = g_fdo_notification_backend_find_notification (self, id);
388 if (n->notify_id > 0)
390 g_dbus_connection_call (backend->dbus_connection,
391 "org.freedesktop.Notifications",
392 "/org/freedesktop/Notifications",
393 "org.freedesktop.Notifications", "CloseNotification",
394 g_variant_new ("(u)", n->id), NULL,
395 G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
398 self->notifications = g_slist_remove (self->notifications, n);
399 freedesktop_notification_free (n);
404 g_fdo_notification_backend_init (GFdoNotificationBackend *backend)
409 g_fdo_notification_backend_class_init (GFdoNotificationBackendClass *class)
411 GObjectClass *object_class = G_OBJECT_CLASS (class);
412 GNotificationBackendClass *backend_class = G_NOTIFICATION_BACKEND_CLASS (class);
414 object_class->dispose = g_fdo_notification_backend_dispose;
416 backend_class->is_supported = g_fdo_notification_backend_is_supported;
417 backend_class->send_notification = g_fdo_notification_backend_send_notification;
418 backend_class->withdraw_notification = g_fdo_notification_backend_withdraw_notification;