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"
29 #include "gfileicon.h"
31 #include "gdbusutils.h"
33 #define G_TYPE_FDO_NOTIFICATION_BACKEND (g_fdo_notification_backend_get_type ())
34 #define G_FDO_NOTIFICATION_BACKEND(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_FDO_NOTIFICATION_BACKEND, GFdoNotificationBackend))
36 typedef struct _GFdoNotificationBackend GFdoNotificationBackend;
37 typedef GNotificationBackendClass GFdoNotificationBackendClass;
39 struct _GFdoNotificationBackend
41 GNotificationBackend parent;
43 guint notify_subscription;
44 GSList *notifications;
47 GType g_fdo_notification_backend_get_type (void);
49 G_DEFINE_TYPE_WITH_CODE (GFdoNotificationBackend, g_fdo_notification_backend, G_TYPE_NOTIFICATION_BACKEND,
50 _g_io_modules_ensure_extension_points_registered ();
51 g_io_extension_point_implement (G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME,
52 g_define_type_id, "freedesktop", 0))
56 GFdoNotificationBackend *backend;
59 gchar *default_action;
60 GVariant *default_action_target;
61 } FreedesktopNotification;
65 freedesktop_notification_free (gpointer data)
67 FreedesktopNotification *n = data;
70 g_free (n->default_action);
71 if (n->default_action_target)
72 g_variant_unref (n->default_action_target);
74 g_slice_free (FreedesktopNotification, n);
77 static FreedesktopNotification *
78 g_fdo_notification_backend_find_notification (GFdoNotificationBackend *backend,
83 for (it = backend->notifications; it != NULL; it = it->next)
85 FreedesktopNotification *n = it->data;
86 if (g_str_equal (n->id, id))
93 static FreedesktopNotification *
94 g_fdo_notification_backend_find_notification_by_notify_id (GFdoNotificationBackend *backend,
99 for (it = backend->notifications; it != NULL; it = it->next)
101 FreedesktopNotification *n = it->data;
102 if (n->notify_id == id)
110 notify_signal (GDBusConnection *connection,
111 const gchar *sender_name,
112 const gchar *object_path,
113 const gchar *interface_name,
114 const gchar *signal_name,
115 GVariant *parameters,
118 GFdoNotificationBackend *backend = user_data;
120 const gchar *action = NULL;
121 FreedesktopNotification *n;
123 if (g_str_equal (signal_name, "NotificationClosed") &&
124 g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(uu)")))
126 g_variant_get (parameters, "(uu)", &id, NULL);
128 else if (g_str_equal (signal_name, "ActionInvoked") &&
129 g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(us)")))
131 g_variant_get (parameters, "(u&s)", &id, &action);
136 n = g_fdo_notification_backend_find_notification_by_notify_id (backend, id);
142 if (g_str_equal (action, "default"))
144 g_notification_backend_activate_action (G_NOTIFICATION_BACKEND (backend),
146 n->default_action_target);
153 if (g_action_parse_detailed_name (action, &name, &target, NULL))
155 g_notification_backend_activate_action (G_NOTIFICATION_BACKEND (backend), name, target);
158 g_variant_unref (target);
163 backend->notifications = g_slist_remove (backend->notifications, n);
164 freedesktop_notification_free (n);
168 call_notify (GDBusConnection *con,
171 GNotification *notification,
172 GAsyncReadyCallback callback,
175 GVariantBuilder action_builder;
178 GVariantBuilder hints_builder;
180 GVariant *parameters;
183 g_variant_builder_init (&action_builder, G_VARIANT_TYPE_STRING_ARRAY);
184 if (g_notification_get_default_action (notification, NULL, NULL))
186 g_variant_builder_add (&action_builder, "s", "default");
187 g_variant_builder_add (&action_builder, "s", "");
190 n_buttons = g_notification_get_n_buttons (notification);
191 for (i = 0; i < n_buttons; i++)
196 gchar *detailed_name;
198 g_notification_get_button (notification, i, &label, &action, &target);
199 detailed_name = g_action_print_detailed_name (action, target);
201 /* Actions named 'default' collide with libnotify's naming of the
202 * default action. Rewriting them to something unique is enough,
203 * because those actions can never be activated (they aren't
204 * prefixed with 'app.').
206 if (g_str_equal (detailed_name, "default"))
208 g_free (detailed_name);
209 detailed_name = g_dbus_generate_guid ();
212 g_variant_builder_add_value (&action_builder, g_variant_new_take_string (detailed_name));
213 g_variant_builder_add_value (&action_builder, g_variant_new_take_string (label));
217 g_variant_unref (target);
220 g_variant_builder_init (&hints_builder, G_VARIANT_TYPE ("a{sv}"));
221 g_variant_builder_add (&hints_builder, "{sv}", "desktop-entry",
222 g_variant_new_string (g_application_get_application_id (app)));
223 if (g_notification_get_urgent (notification))
224 g_variant_builder_add (&hints_builder, "{sv}", "urgency", g_variant_new_byte (2));
225 icon = g_notification_get_icon (notification);
226 if (icon != NULL && G_IS_FILE_ICON (icon))
230 file = g_file_icon_get_file (G_FILE_ICON (icon));
231 g_variant_builder_add (&hints_builder, "{sv}", "image-path",
232 g_variant_new_take_string (g_file_get_uri (file)));
235 body = g_notification_get_body (notification);
237 parameters = g_variant_new ("(susssasa{sv}i)",
241 g_notification_get_title (notification),
245 -1); /* expire_timeout */
247 g_dbus_connection_call (con, "org.freedesktop.Notifications", "/org/freedesktop/Notifications",
248 "org.freedesktop.Notifications", "Notify",
249 parameters, G_VARIANT_TYPE ("(u)"),
250 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
251 callback, user_data);
255 notification_sent (GObject *source_object,
256 GAsyncResult *result,
259 FreedesktopNotification *n = user_data;
261 GError *error = NULL;
262 static gboolean warning_printed = FALSE;
264 val = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), result, &error);
267 g_variant_get (val, "(u)", &n->notify_id);
268 g_variant_unref (val);
272 if (!warning_printed)
274 g_warning ("unable to send notifications through org.freedesktop.Notifications: %s",
276 warning_printed = TRUE;
279 n->backend->notifications = g_slist_remove (n->backend->notifications, n);
280 freedesktop_notification_free (n);
282 g_error_free (error);
287 g_fdo_notification_backend_dispose (GObject *object)
289 GFdoNotificationBackend *backend = G_FDO_NOTIFICATION_BACKEND (object);
291 if (backend->notify_subscription)
293 GDBusConnection *session_bus;
295 session_bus = g_notification_backend_get_dbus_connection (G_NOTIFICATION_BACKEND (backend));
296 g_dbus_connection_signal_unsubscribe (session_bus, backend->notify_subscription);
297 backend->notify_subscription = 0;
300 if (backend->notifications)
302 g_slist_free_full (backend->notifications, freedesktop_notification_free);
303 backend->notifications = NULL;
306 G_OBJECT_CLASS (g_fdo_notification_backend_parent_class)->dispose (object);
310 g_fdo_notification_backend_is_supported (void)
312 /* This is the fallback backend with the lowest priority. To avoid an
313 * unnecessary synchronous dbus call to check for
314 * org.freedesktop.Notifications, this function always succeeds. A
315 * warning will be printed when sending the first notification fails.
321 g_fdo_notification_backend_send_notification (GNotificationBackend *backend,
323 GNotification *notification)
325 GFdoNotificationBackend *self = G_FDO_NOTIFICATION_BACKEND (backend);
326 FreedesktopNotification *n;
328 if (self->notify_subscription == 0)
330 self->notify_subscription =
331 g_dbus_connection_signal_subscribe (g_notification_backend_get_dbus_connection (backend),
332 "org.freedesktop.Notifications",
333 "org.freedesktop.Notifications", NULL,
334 "/org/freedesktop/Notifications", NULL,
335 G_DBUS_SIGNAL_FLAGS_NONE,
336 notify_signal, backend, NULL);
339 n = g_fdo_notification_backend_find_notification (self, id);
342 n = g_slice_new0 (FreedesktopNotification);
344 n->id = g_strdup (id);
347 n->backend->notifications = g_slist_prepend (n->backend->notifications, n);
351 /* Only clear default action. All other fields are still valid */
352 g_clear_pointer (&n->default_action, g_free);
353 g_clear_pointer (&n->default_action_target, g_variant_unref);
356 g_notification_get_default_action (notification, &n->default_action, &n->default_action_target);
358 call_notify (g_notification_backend_get_dbus_connection (backend),
359 g_notification_backend_get_application (backend),
360 n->notify_id, notification, notification_sent, n);
364 g_fdo_notification_backend_withdraw_notification (GNotificationBackend *backend,
367 GFdoNotificationBackend *self = G_FDO_NOTIFICATION_BACKEND (backend);
368 FreedesktopNotification *n;
370 n = g_fdo_notification_backend_find_notification (self, id);
373 if (n->notify_id > 0)
375 g_dbus_connection_call (g_notification_backend_get_dbus_connection (backend),
376 "org.freedesktop.Notifications",
377 "/org/freedesktop/Notifications",
378 "org.freedesktop.Notifications", "CloseNotification",
379 g_variant_new ("(u)", n->id), NULL,
380 G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
383 self->notifications = g_slist_remove (self->notifications, n);
384 freedesktop_notification_free (n);
389 g_fdo_notification_backend_init (GFdoNotificationBackend *backend)
394 g_fdo_notification_backend_class_init (GFdoNotificationBackendClass *class)
396 GObjectClass *object_class = G_OBJECT_CLASS (class);
397 GNotificationBackendClass *backend_class = G_NOTIFICATION_BACKEND_CLASS (class);
399 object_class->dispose = g_fdo_notification_backend_dispose;
401 backend_class->is_supported = g_fdo_notification_backend_is_supported;
402 backend_class->send_notification = g_fdo_notification_backend_send_notification;
403 backend_class->withdraw_notification = g_fdo_notification_backend_withdraw_notification;