2.54.2
[glib.git] / gio / gfdonotificationbackend.c
bloba0d4814335be58de2c99ab6f29d94b13149a3196
1 /*
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.1 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>
20 #include "config.h"
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"
29 #include "gaction.h"
30 #include "gthemedicon.h"
31 #include "gfileicon.h"
32 #include "gfile.h"
33 #include "gdbusutils.h"
35 #define G_TYPE_FDO_NOTIFICATION_BACKEND (g_fdo_notification_backend_get_type ())
36 #define G_FDO_NOTIFICATION_BACKEND(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_FDO_NOTIFICATION_BACKEND, GFdoNotificationBackend))
38 typedef struct _GFdoNotificationBackend GFdoNotificationBackend;
39 typedef GNotificationBackendClass GFdoNotificationBackendClass;
41 struct _GFdoNotificationBackend
43 GNotificationBackend parent;
45 guint notify_subscription;
46 GSList *notifications;
49 GType g_fdo_notification_backend_get_type (void);
51 G_DEFINE_TYPE_WITH_CODE (GFdoNotificationBackend, g_fdo_notification_backend, G_TYPE_NOTIFICATION_BACKEND,
52 _g_io_modules_ensure_extension_points_registered ();
53 g_io_extension_point_implement (G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME,
54 g_define_type_id, "freedesktop", 0))
56 typedef struct
58 GFdoNotificationBackend *backend;
59 gchar *id;
60 guint32 notify_id;
61 gchar *default_action;
62 GVariant *default_action_target;
63 } FreedesktopNotification;
66 static void
67 freedesktop_notification_free (gpointer data)
69 FreedesktopNotification *n = data;
71 g_free (n->id);
72 g_free (n->default_action);
73 if (n->default_action_target)
74 g_variant_unref (n->default_action_target);
76 g_slice_free (FreedesktopNotification, n);
79 static FreedesktopNotification *
80 g_fdo_notification_backend_find_notification (GFdoNotificationBackend *backend,
81 const gchar *id)
83 GSList *it;
85 for (it = backend->notifications; it != NULL; it = it->next)
87 FreedesktopNotification *n = it->data;
88 if (g_str_equal (n->id, id))
89 return n;
92 return NULL;
95 static FreedesktopNotification *
96 g_fdo_notification_backend_find_notification_by_notify_id (GFdoNotificationBackend *backend,
97 guint32 id)
99 GSList *it;
101 for (it = backend->notifications; it != NULL; it = it->next)
103 FreedesktopNotification *n = it->data;
104 if (n->notify_id == id)
105 return n;
108 return NULL;
111 static void
112 activate_action (GFdoNotificationBackend *backend,
113 const gchar *name,
114 GVariant *parameter)
116 GNotificationBackend *g_backend = G_NOTIFICATION_BACKEND (backend);
118 if (name)
120 if (g_str_has_prefix (name, "app."))
121 g_action_group_activate_action (G_ACTION_GROUP (g_backend->application), name + 4, parameter);
123 else
125 g_application_activate (g_backend->application);
129 static void
130 notify_signal (GDBusConnection *connection,
131 const gchar *sender_name,
132 const gchar *object_path,
133 const gchar *interface_name,
134 const gchar *signal_name,
135 GVariant *parameters,
136 gpointer user_data)
138 GFdoNotificationBackend *backend = user_data;
139 guint32 id = 0;
140 const gchar *action = NULL;
141 FreedesktopNotification *n;
143 if (g_str_equal (signal_name, "NotificationClosed") &&
144 g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(uu)")))
146 g_variant_get (parameters, "(uu)", &id, NULL);
148 else if (g_str_equal (signal_name, "ActionInvoked") &&
149 g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(us)")))
151 g_variant_get (parameters, "(u&s)", &id, &action);
153 else
154 return;
156 n = g_fdo_notification_backend_find_notification_by_notify_id (backend, id);
157 if (n == NULL)
158 return;
160 if (action)
162 if (g_str_equal (action, "default"))
164 activate_action (backend, n->default_action, n->default_action_target);
166 else
168 gchar *name;
169 GVariant *target;
171 if (g_action_parse_detailed_name (action, &name, &target, NULL))
173 activate_action (backend, name, target);
174 g_free (name);
175 if (target)
176 g_variant_unref (target);
181 /* Get the notification again in case the action redrew it */
182 n = g_fdo_notification_backend_find_notification_by_notify_id (backend, id);
183 if (n != NULL)
185 backend->notifications = g_slist_remove (backend->notifications, n);
186 freedesktop_notification_free (n);
190 /* Converts a GNotificationPriority to an urgency level as defined by
191 * the freedesktop spec (0: low, 1: normal, 2: critical).
193 static guchar
194 urgency_from_priority (GNotificationPriority priority)
196 switch (priority)
198 case G_NOTIFICATION_PRIORITY_LOW:
199 return 0;
201 default:
202 case G_NOTIFICATION_PRIORITY_NORMAL:
203 case G_NOTIFICATION_PRIORITY_HIGH:
204 return 1;
206 case G_NOTIFICATION_PRIORITY_URGENT:
207 return 2;
211 static void
212 call_notify (GDBusConnection *con,
213 GApplication *app,
214 guint32 replace_id,
215 GNotification *notification,
216 GAsyncReadyCallback callback,
217 gpointer user_data)
219 GVariantBuilder action_builder;
220 guint n_buttons;
221 guint i;
222 GVariantBuilder hints_builder;
223 GIcon *icon;
224 GVariant *parameters;
225 const gchar *body;
226 guchar urgency;
228 g_variant_builder_init (&action_builder, G_VARIANT_TYPE_STRING_ARRAY);
229 if (g_notification_get_default_action (notification, NULL, NULL))
231 g_variant_builder_add (&action_builder, "s", "default");
232 g_variant_builder_add (&action_builder, "s", "");
235 n_buttons = g_notification_get_n_buttons (notification);
236 for (i = 0; i < n_buttons; i++)
238 gchar *label;
239 gchar *action;
240 GVariant *target;
241 gchar *detailed_name;
243 g_notification_get_button (notification, i, &label, &action, &target);
244 detailed_name = g_action_print_detailed_name (action, target);
246 /* Actions named 'default' collide with libnotify's naming of the
247 * default action. Rewriting them to something unique is enough,
248 * because those actions can never be activated (they aren't
249 * prefixed with 'app.').
251 if (g_str_equal (detailed_name, "default"))
253 g_free (detailed_name);
254 detailed_name = g_dbus_generate_guid ();
257 g_variant_builder_add_value (&action_builder, g_variant_new_take_string (detailed_name));
258 g_variant_builder_add_value (&action_builder, g_variant_new_take_string (label));
260 g_free (action);
261 if (target)
262 g_variant_unref (target);
265 g_variant_builder_init (&hints_builder, G_VARIANT_TYPE ("a{sv}"));
266 g_variant_builder_add (&hints_builder, "{sv}", "desktop-entry",
267 g_variant_new_string (g_application_get_application_id (app)));
268 urgency = urgency_from_priority (g_notification_get_priority (notification));
269 g_variant_builder_add (&hints_builder, "{sv}", "urgency", g_variant_new_byte (urgency));
270 icon = g_notification_get_icon (notification);
271 if (icon != NULL)
273 if (G_IS_FILE_ICON (icon))
275 GFile *file;
277 file = g_file_icon_get_file (G_FILE_ICON (icon));
278 g_variant_builder_add (&hints_builder, "{sv}", "image-path",
279 g_variant_new_take_string (g_file_get_path (file)));
281 else if (G_IS_THEMED_ICON (icon))
283 const gchar* const* icon_names = g_themed_icon_get_names(G_THEMED_ICON (icon));
284 /* Take first name from GThemedIcon */
285 g_variant_builder_add (&hints_builder, "{sv}", "image-path",
286 g_variant_new_string (icon_names[0]));
290 body = g_notification_get_body (notification);
292 parameters = g_variant_new ("(susssasa{sv}i)",
293 "", /* app name */
294 replace_id,
295 "", /* app icon */
296 g_notification_get_title (notification),
297 body ? body : "",
298 &action_builder,
299 &hints_builder,
300 -1); /* expire_timeout */
302 g_dbus_connection_call (con, "org.freedesktop.Notifications", "/org/freedesktop/Notifications",
303 "org.freedesktop.Notifications", "Notify",
304 parameters, G_VARIANT_TYPE ("(u)"),
305 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
306 callback, user_data);
309 static void
310 notification_sent (GObject *source_object,
311 GAsyncResult *result,
312 gpointer user_data)
314 FreedesktopNotification *n = user_data;
315 GVariant *val;
316 GError *error = NULL;
317 static gboolean warning_printed = FALSE;
319 val = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), result, &error);
320 if (val)
322 g_variant_get (val, "(u)", &n->notify_id);
323 g_variant_unref (val);
325 else
327 if (!warning_printed)
329 g_warning ("unable to send notifications through org.freedesktop.Notifications: %s",
330 error->message);
331 warning_printed = TRUE;
334 n->backend->notifications = g_slist_remove (n->backend->notifications, n);
335 freedesktop_notification_free (n);
337 g_error_free (error);
341 static void
342 g_fdo_notification_backend_dispose (GObject *object)
344 GFdoNotificationBackend *backend = G_FDO_NOTIFICATION_BACKEND (object);
346 if (backend->notify_subscription)
348 GDBusConnection *session_bus;
350 session_bus = G_NOTIFICATION_BACKEND (backend)->dbus_connection;
351 g_dbus_connection_signal_unsubscribe (session_bus, backend->notify_subscription);
352 backend->notify_subscription = 0;
355 if (backend->notifications)
357 g_slist_free_full (backend->notifications, freedesktop_notification_free);
358 backend->notifications = NULL;
361 G_OBJECT_CLASS (g_fdo_notification_backend_parent_class)->dispose (object);
364 static gboolean
365 g_fdo_notification_backend_is_supported (void)
367 /* This is the fallback backend with the lowest priority. To avoid an
368 * unnecessary synchronous dbus call to check for
369 * org.freedesktop.Notifications, this function always succeeds. A
370 * warning will be printed when sending the first notification fails.
372 return TRUE;
375 static void
376 g_fdo_notification_backend_send_notification (GNotificationBackend *backend,
377 const gchar *id,
378 GNotification *notification)
380 GFdoNotificationBackend *self = G_FDO_NOTIFICATION_BACKEND (backend);
381 FreedesktopNotification *n;
383 if (self->notify_subscription == 0)
385 self->notify_subscription =
386 g_dbus_connection_signal_subscribe (backend->dbus_connection,
387 "org.freedesktop.Notifications",
388 "org.freedesktop.Notifications", NULL,
389 "/org/freedesktop/Notifications", NULL,
390 G_DBUS_SIGNAL_FLAGS_NONE,
391 notify_signal, backend, NULL);
394 n = g_fdo_notification_backend_find_notification (self, id);
395 if (n == NULL)
397 n = g_slice_new0 (FreedesktopNotification);
398 n->backend = self;
399 n->id = g_strdup (id);
400 n->notify_id = 0;
402 n->backend->notifications = g_slist_prepend (n->backend->notifications, n);
404 else
406 /* Only clear default action. All other fields are still valid */
407 g_clear_pointer (&n->default_action, g_free);
408 g_clear_pointer (&n->default_action_target, g_variant_unref);
411 g_notification_get_default_action (notification, &n->default_action, &n->default_action_target);
413 call_notify (backend->dbus_connection, backend->application, n->notify_id, notification, notification_sent, n);
416 static void
417 g_fdo_notification_backend_withdraw_notification (GNotificationBackend *backend,
418 const gchar *id)
420 GFdoNotificationBackend *self = G_FDO_NOTIFICATION_BACKEND (backend);
421 FreedesktopNotification *n;
423 n = g_fdo_notification_backend_find_notification (self, id);
424 if (n)
426 if (n->notify_id > 0)
428 g_dbus_connection_call (backend->dbus_connection,
429 "org.freedesktop.Notifications",
430 "/org/freedesktop/Notifications",
431 "org.freedesktop.Notifications", "CloseNotification",
432 g_variant_new ("(u)", n->notify_id), NULL,
433 G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
436 self->notifications = g_slist_remove (self->notifications, n);
437 freedesktop_notification_free (n);
441 static void
442 g_fdo_notification_backend_init (GFdoNotificationBackend *backend)
446 static void
447 g_fdo_notification_backend_class_init (GFdoNotificationBackendClass *class)
449 GObjectClass *object_class = G_OBJECT_CLASS (class);
450 GNotificationBackendClass *backend_class = G_NOTIFICATION_BACKEND_CLASS (class);
452 object_class->dispose = g_fdo_notification_backend_dispose;
454 backend_class->is_supported = g_fdo_notification_backend_is_supported;
455 backend_class->send_notification = g_fdo_notification_backend_send_notification;
456 backend_class->withdraw_notification = g_fdo_notification_backend_withdraw_notification;