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>
24 #include "gnotificationbackend.h"
26 #include "gapplication.h"
27 #include "giomodule-priv.h"
28 #include "gnotification-private.h"
29 #include "gdbusconnection.h"
30 #include "gactiongroup.h"
32 #include "gfileicon.h"
34 #include "gdbusutils.h"
36 #define G_TYPE_FDO_NOTIFICATION_BACKEND (g_fdo_notification_backend_get_type ())
37 #define G_FDO_NOTIFICATION_BACKEND(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_FDO_NOTIFICATION_BACKEND, GFdoNotificationBackend))
39 typedef struct _GFdoNotificationBackend GFdoNotificationBackend;
40 typedef GNotificationBackendClass GFdoNotificationBackendClass;
42 struct _GFdoNotificationBackend
44 GNotificationBackend parent;
46 guint notify_subscription;
47 GSList *notifications;
50 GType g_fdo_notification_backend_get_type (void);
52 G_DEFINE_TYPE_WITH_CODE (GFdoNotificationBackend, g_fdo_notification_backend, G_TYPE_NOTIFICATION_BACKEND,
53 _g_io_modules_ensure_extension_points_registered ();
54 g_io_extension_point_implement (G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME,
55 g_define_type_id, "freedesktop", 0))
59 GFdoNotificationBackend *backend;
62 gchar *default_action;
63 GVariant *default_action_target;
64 } FreedesktopNotification;
68 freedesktop_notification_free (gpointer data)
70 FreedesktopNotification *n = data;
73 g_free (n->default_action);
74 if (n->default_action_target)
75 g_variant_unref (n->default_action_target);
77 g_slice_free (FreedesktopNotification, n);
80 static FreedesktopNotification *
81 g_fdo_notification_backend_find_notification (GFdoNotificationBackend *backend,
86 for (it = backend->notifications; it != NULL; it = it->next)
88 FreedesktopNotification *n = it->data;
89 if (g_str_equal (n->id, id))
96 static FreedesktopNotification *
97 g_fdo_notification_backend_find_notification_by_notify_id (GFdoNotificationBackend *backend,
102 for (it = backend->notifications; it != NULL; it = it->next)
104 FreedesktopNotification *n = it->data;
105 if (n->notify_id == id)
113 activate_action (GFdoNotificationBackend *backend,
117 GNotificationBackend *g_backend = G_NOTIFICATION_BACKEND (backend);
121 if (g_str_has_prefix (name, "app."))
122 g_action_group_activate_action (G_ACTION_GROUP (g_backend->application), name + 4, parameter);
126 g_application_activate (g_backend->application);
131 notify_signal (GDBusConnection *connection,
132 const gchar *sender_name,
133 const gchar *object_path,
134 const gchar *interface_name,
135 const gchar *signal_name,
136 GVariant *parameters,
139 GFdoNotificationBackend *backend = user_data;
141 const gchar *action = NULL;
142 FreedesktopNotification *n;
144 if (g_str_equal (signal_name, "NotificationClosed") &&
145 g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(uu)")))
147 g_variant_get (parameters, "(uu)", &id, NULL);
149 else if (g_str_equal (signal_name, "ActionInvoked") &&
150 g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(us)")))
152 g_variant_get (parameters, "(u&s)", &id, &action);
157 n = g_fdo_notification_backend_find_notification_by_notify_id (backend, id);
163 if (g_str_equal (action, "default"))
165 activate_action (backend, n->default_action, n->default_action_target);
172 if (g_action_parse_detailed_name (action, &name, &target, NULL))
174 activate_action (backend, name, target);
177 g_variant_unref (target);
182 backend->notifications = g_slist_remove (backend->notifications, n);
183 freedesktop_notification_free (n);
187 call_notify (GDBusConnection *con,
190 GNotification *notification,
191 GAsyncReadyCallback callback,
194 GVariantBuilder action_builder;
197 GVariantBuilder hints_builder;
199 GVariant *parameters;
202 g_variant_builder_init (&action_builder, G_VARIANT_TYPE_STRING_ARRAY);
203 if (g_notification_get_default_action (notification, NULL, NULL))
205 g_variant_builder_add (&action_builder, "s", "default");
206 g_variant_builder_add (&action_builder, "s", "");
209 n_buttons = g_notification_get_n_buttons (notification);
210 for (i = 0; i < n_buttons; i++)
215 gchar *detailed_name;
217 g_notification_get_button (notification, i, &label, &action, &target);
218 detailed_name = g_action_print_detailed_name (action, target);
220 /* Actions named 'default' collide with libnotify's naming of the
221 * default action. Rewriting them to something unique is enough,
222 * because those actions can never be activated (they aren't
223 * prefixed with 'app.').
225 if (g_str_equal (detailed_name, "default"))
227 g_free (detailed_name);
228 detailed_name = g_dbus_generate_guid ();
231 g_variant_builder_add_value (&action_builder, g_variant_new_take_string (detailed_name));
232 g_variant_builder_add_value (&action_builder, g_variant_new_take_string (label));
236 g_variant_unref (target);
239 g_variant_builder_init (&hints_builder, G_VARIANT_TYPE ("a{sv}"));
240 g_variant_builder_add (&hints_builder, "{sv}", "desktop-entry",
241 g_variant_new_string (g_application_get_application_id (app)));
242 if (g_notification_get_urgent (notification))
243 g_variant_builder_add (&hints_builder, "{sv}", "urgency", g_variant_new_byte (2));
244 icon = g_notification_get_icon (notification);
245 if (icon != NULL && G_IS_FILE_ICON (icon))
249 file = g_file_icon_get_file (G_FILE_ICON (icon));
250 g_variant_builder_add (&hints_builder, "{sv}", "image-path",
251 g_variant_new_take_string (g_file_get_path (file)));
254 body = g_notification_get_body (notification);
256 parameters = g_variant_new ("(susssasa{sv}i)",
260 g_notification_get_title (notification),
264 -1); /* expire_timeout */
266 g_dbus_connection_call (con, "org.freedesktop.Notifications", "/org/freedesktop/Notifications",
267 "org.freedesktop.Notifications", "Notify",
268 parameters, G_VARIANT_TYPE ("(u)"),
269 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
270 callback, user_data);
274 notification_sent (GObject *source_object,
275 GAsyncResult *result,
278 FreedesktopNotification *n = user_data;
280 GError *error = NULL;
281 static gboolean warning_printed = FALSE;
283 val = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), result, &error);
286 g_variant_get (val, "(u)", &n->notify_id);
287 g_variant_unref (val);
291 if (!warning_printed)
293 g_warning ("unable to send notifications through org.freedesktop.Notifications: %s",
295 warning_printed = TRUE;
298 n->backend->notifications = g_slist_remove (n->backend->notifications, n);
299 freedesktop_notification_free (n);
301 g_error_free (error);
306 g_fdo_notification_backend_dispose (GObject *object)
308 GFdoNotificationBackend *backend = G_FDO_NOTIFICATION_BACKEND (object);
310 if (backend->notify_subscription)
312 GDBusConnection *session_bus;
314 session_bus = G_NOTIFICATION_BACKEND (backend)->dbus_connection;
315 g_dbus_connection_signal_unsubscribe (session_bus, backend->notify_subscription);
316 backend->notify_subscription = 0;
319 if (backend->notifications)
321 g_slist_free_full (backend->notifications, freedesktop_notification_free);
322 backend->notifications = NULL;
325 G_OBJECT_CLASS (g_fdo_notification_backend_parent_class)->dispose (object);
329 g_fdo_notification_backend_is_supported (void)
331 /* This is the fallback backend with the lowest priority. To avoid an
332 * unnecessary synchronous dbus call to check for
333 * org.freedesktop.Notifications, this function always succeeds. A
334 * warning will be printed when sending the first notification fails.
340 g_fdo_notification_backend_send_notification (GNotificationBackend *backend,
342 GNotification *notification)
344 GFdoNotificationBackend *self = G_FDO_NOTIFICATION_BACKEND (backend);
345 FreedesktopNotification *n;
347 if (self->notify_subscription == 0)
349 self->notify_subscription =
350 g_dbus_connection_signal_subscribe (backend->dbus_connection,
351 "org.freedesktop.Notifications",
352 "org.freedesktop.Notifications", NULL,
353 "/org/freedesktop/Notifications", NULL,
354 G_DBUS_SIGNAL_FLAGS_NONE,
355 notify_signal, backend, NULL);
358 n = g_fdo_notification_backend_find_notification (self, id);
361 n = g_slice_new0 (FreedesktopNotification);
363 n->id = g_strdup (id);
366 n->backend->notifications = g_slist_prepend (n->backend->notifications, n);
370 /* Only clear default action. All other fields are still valid */
371 g_clear_pointer (&n->default_action, g_free);
372 g_clear_pointer (&n->default_action_target, g_variant_unref);
375 g_notification_get_default_action (notification, &n->default_action, &n->default_action_target);
377 call_notify (backend->dbus_connection, backend->application, n->notify_id, notification, notification_sent, n);
381 g_fdo_notification_backend_withdraw_notification (GNotificationBackend *backend,
384 GFdoNotificationBackend *self = G_FDO_NOTIFICATION_BACKEND (backend);
385 FreedesktopNotification *n;
387 n = g_fdo_notification_backend_find_notification (self, id);
390 if (n->notify_id > 0)
392 g_dbus_connection_call (backend->dbus_connection,
393 "org.freedesktop.Notifications",
394 "/org/freedesktop/Notifications",
395 "org.freedesktop.Notifications", "CloseNotification",
396 g_variant_new ("(u)", n->id), NULL,
397 G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
400 self->notifications = g_slist_remove (self->notifications, n);
401 freedesktop_notification_free (n);
406 g_fdo_notification_backend_init (GFdoNotificationBackend *backend)
411 g_fdo_notification_backend_class_init (GFdoNotificationBackendClass *class)
413 GObjectClass *object_class = G_OBJECT_CLASS (class);
414 GNotificationBackendClass *backend_class = G_NOTIFICATION_BACKEND_CLASS (class);
416 object_class->dispose = g_fdo_notification_backend_dispose;
418 backend_class->is_supported = g_fdo_notification_backend_is_supported;
419 backend_class->send_notification = g_fdo_notification_backend_send_notification;
420 backend_class->withdraw_notification = g_fdo_notification_backend_withdraw_notification;