/src/glib/gio/gfdonotificationbackend.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright © 2013 Lars Uebernickel |
3 | | * |
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. |
8 | | * |
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. |
13 | | * |
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/>. |
16 | | * |
17 | | * Authors: Lars Uebernickel <lars@uebernic.de> |
18 | | */ |
19 | | |
20 | | #include "config.h" |
21 | | |
22 | | #include "gnotificationbackend.h" |
23 | | |
24 | | #include "gapplication.h" |
25 | | #include "giomodule-priv.h" |
26 | | #include "gnotification-private.h" |
27 | | #include "gdbusconnection.h" |
28 | | #include "gdbusnamewatching.h" |
29 | | #include "gactiongroup.h" |
30 | | #include "gaction.h" |
31 | | #include "gthemedicon.h" |
32 | | #include "gfileicon.h" |
33 | | #include "gfile.h" |
34 | | #include "gdbusutils.h" |
35 | | |
36 | | #define G_TYPE_FDO_NOTIFICATION_BACKEND (g_fdo_notification_backend_get_type ()) |
37 | 0 | #define G_FDO_NOTIFICATION_BACKEND(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_FDO_NOTIFICATION_BACKEND, GFdoNotificationBackend)) |
38 | | |
39 | | typedef struct _GFdoNotificationBackend GFdoNotificationBackend; |
40 | | typedef GNotificationBackendClass GFdoNotificationBackendClass; |
41 | | |
42 | | struct _GFdoNotificationBackend |
43 | | { |
44 | | GNotificationBackend parent; |
45 | | |
46 | | guint bus_name_id; |
47 | | |
48 | | guint notify_subscription; |
49 | | GSList *notifications; |
50 | | }; |
51 | | |
52 | | GType g_fdo_notification_backend_get_type (void); |
53 | | |
54 | | G_DEFINE_TYPE_WITH_CODE (GFdoNotificationBackend, g_fdo_notification_backend, G_TYPE_NOTIFICATION_BACKEND, |
55 | | _g_io_modules_ensure_extension_points_registered (); |
56 | | g_io_extension_point_implement (G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME, |
57 | | g_define_type_id, "freedesktop", 0)) |
58 | | |
59 | | typedef struct |
60 | | { |
61 | | GFdoNotificationBackend *backend; |
62 | | gchar *id; |
63 | | guint32 notify_id; |
64 | | gchar *default_action; |
65 | | GVariant *default_action_target; |
66 | | } FreedesktopNotification; |
67 | | |
68 | | static void |
69 | | freedesktop_notification_free (gpointer data) |
70 | 0 | { |
71 | 0 | FreedesktopNotification *n = data; |
72 | |
|
73 | 0 | g_object_unref (n->backend); |
74 | 0 | g_free (n->id); |
75 | 0 | g_free (n->default_action); |
76 | 0 | if (n->default_action_target) |
77 | 0 | g_variant_unref (n->default_action_target); |
78 | |
|
79 | 0 | g_slice_free (FreedesktopNotification, n); |
80 | 0 | } |
81 | | |
82 | | static FreedesktopNotification * |
83 | | freedesktop_notification_new (GFdoNotificationBackend *backend, |
84 | | const gchar *id, |
85 | | GNotification *notification) |
86 | 0 | { |
87 | 0 | FreedesktopNotification *n; |
88 | |
|
89 | 0 | n = g_slice_new0 (FreedesktopNotification); |
90 | 0 | n->backend = g_object_ref (backend); |
91 | 0 | n->id = g_strdup (id); |
92 | 0 | n->notify_id = 0; |
93 | 0 | g_notification_get_default_action (notification, |
94 | 0 | &n->default_action, |
95 | 0 | &n->default_action_target); |
96 | |
|
97 | 0 | return n; |
98 | 0 | } |
99 | | |
100 | | static FreedesktopNotification * |
101 | | g_fdo_notification_backend_find_notification (GFdoNotificationBackend *backend, |
102 | | const gchar *id) |
103 | 0 | { |
104 | 0 | GSList *it; |
105 | |
|
106 | 0 | for (it = backend->notifications; it != NULL; it = it->next) |
107 | 0 | { |
108 | 0 | FreedesktopNotification *n = it->data; |
109 | 0 | if (g_str_equal (n->id, id)) |
110 | 0 | return n; |
111 | 0 | } |
112 | | |
113 | 0 | return NULL; |
114 | 0 | } |
115 | | |
116 | | static FreedesktopNotification * |
117 | | g_fdo_notification_backend_find_notification_by_notify_id (GFdoNotificationBackend *backend, |
118 | | guint32 id) |
119 | 0 | { |
120 | 0 | GSList *it; |
121 | |
|
122 | 0 | for (it = backend->notifications; it != NULL; it = it->next) |
123 | 0 | { |
124 | 0 | FreedesktopNotification *n = it->data; |
125 | 0 | if (n->notify_id == id) |
126 | 0 | return n; |
127 | 0 | } |
128 | | |
129 | 0 | return NULL; |
130 | 0 | } |
131 | | |
132 | | static void |
133 | | activate_action (GFdoNotificationBackend *backend, |
134 | | const gchar *name, |
135 | | GVariant *parameter) |
136 | 0 | { |
137 | 0 | GNotificationBackend *g_backend = G_NOTIFICATION_BACKEND (backend); |
138 | |
|
139 | 0 | if (name) |
140 | 0 | { |
141 | 0 | if (g_str_has_prefix (name, "app.")) |
142 | 0 | g_action_group_activate_action (G_ACTION_GROUP (g_backend->application), name + 4, parameter); |
143 | 0 | } |
144 | 0 | else |
145 | 0 | { |
146 | 0 | g_application_activate (g_backend->application); |
147 | 0 | } |
148 | 0 | } |
149 | | |
150 | | static void |
151 | | notify_signal (GDBusConnection *connection, |
152 | | const gchar *sender_name, |
153 | | const gchar *object_path, |
154 | | const gchar *interface_name, |
155 | | const gchar *signal_name, |
156 | | GVariant *parameters, |
157 | | gpointer user_data) |
158 | 0 | { |
159 | 0 | GFdoNotificationBackend *backend = user_data; |
160 | 0 | guint32 id = 0; |
161 | 0 | const gchar *action = NULL; |
162 | 0 | FreedesktopNotification *n; |
163 | |
|
164 | 0 | if (g_str_equal (signal_name, "NotificationClosed") && |
165 | 0 | g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(uu)"))) |
166 | 0 | { |
167 | 0 | g_variant_get (parameters, "(uu)", &id, NULL); |
168 | 0 | } |
169 | 0 | else if (g_str_equal (signal_name, "ActionInvoked") && |
170 | 0 | g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(us)"))) |
171 | 0 | { |
172 | 0 | g_variant_get (parameters, "(u&s)", &id, &action); |
173 | 0 | } |
174 | 0 | else |
175 | 0 | return; |
176 | | |
177 | 0 | n = g_fdo_notification_backend_find_notification_by_notify_id (backend, id); |
178 | 0 | if (n == NULL) |
179 | 0 | return; |
180 | | |
181 | 0 | if (action) |
182 | 0 | { |
183 | 0 | if (g_str_equal (action, "default")) |
184 | 0 | { |
185 | 0 | activate_action (backend, n->default_action, n->default_action_target); |
186 | 0 | } |
187 | 0 | else |
188 | 0 | { |
189 | 0 | gchar *name; |
190 | 0 | GVariant *target; |
191 | |
|
192 | 0 | if (g_action_parse_detailed_name (action, &name, &target, NULL)) |
193 | 0 | { |
194 | 0 | activate_action (backend, name, target); |
195 | 0 | g_free (name); |
196 | 0 | if (target) |
197 | 0 | g_variant_unref (target); |
198 | 0 | } |
199 | 0 | } |
200 | 0 | } |
201 | | |
202 | | /* Get the notification again in case the action redrew it */ |
203 | 0 | n = g_fdo_notification_backend_find_notification_by_notify_id (backend, id); |
204 | 0 | if (n != NULL) |
205 | 0 | { |
206 | 0 | backend->notifications = g_slist_remove (backend->notifications, n); |
207 | 0 | freedesktop_notification_free (n); |
208 | 0 | } |
209 | 0 | } |
210 | | |
211 | | static void |
212 | | name_vanished_handler_cb (GDBusConnection *connection, |
213 | | const gchar *name, |
214 | | gpointer user_data) |
215 | 0 | { |
216 | 0 | GFdoNotificationBackend *backend = user_data; |
217 | |
|
218 | 0 | if (backend->notifications) |
219 | 0 | { |
220 | 0 | g_slist_free_full (backend->notifications, freedesktop_notification_free); |
221 | 0 | backend->notifications = NULL; |
222 | 0 | } |
223 | 0 | } |
224 | | |
225 | | /* Converts a GNotificationPriority to an urgency level as defined by |
226 | | * the freedesktop spec (0: low, 1: normal, 2: critical). |
227 | | */ |
228 | | static guchar |
229 | | urgency_from_priority (GNotificationPriority priority) |
230 | 0 | { |
231 | 0 | switch (priority) |
232 | 0 | { |
233 | 0 | case G_NOTIFICATION_PRIORITY_LOW: |
234 | 0 | return 0; |
235 | | |
236 | 0 | default: |
237 | 0 | case G_NOTIFICATION_PRIORITY_NORMAL: |
238 | 0 | case G_NOTIFICATION_PRIORITY_HIGH: |
239 | 0 | return 1; |
240 | | |
241 | 0 | case G_NOTIFICATION_PRIORITY_URGENT: |
242 | 0 | return 2; |
243 | 0 | } |
244 | 0 | } |
245 | | |
246 | | static void |
247 | | call_notify (GDBusConnection *con, |
248 | | GApplication *app, |
249 | | guint32 replace_id, |
250 | | GNotification *notification, |
251 | | GAsyncReadyCallback callback, |
252 | | gpointer user_data) |
253 | 0 | { |
254 | 0 | GVariantBuilder action_builder; |
255 | 0 | guint n_buttons; |
256 | 0 | guint i; |
257 | 0 | GVariantBuilder hints_builder; |
258 | 0 | GIcon *icon; |
259 | 0 | GVariant *parameters; |
260 | 0 | const gchar *body; |
261 | 0 | guchar urgency; |
262 | |
|
263 | 0 | g_variant_builder_init (&action_builder, G_VARIANT_TYPE_STRING_ARRAY); |
264 | 0 | if (g_notification_get_default_action (notification, NULL, NULL)) |
265 | 0 | { |
266 | 0 | g_variant_builder_add (&action_builder, "s", "default"); |
267 | 0 | g_variant_builder_add (&action_builder, "s", ""); |
268 | 0 | } |
269 | |
|
270 | 0 | n_buttons = g_notification_get_n_buttons (notification); |
271 | 0 | for (i = 0; i < n_buttons; i++) |
272 | 0 | { |
273 | 0 | gchar *label; |
274 | 0 | gchar *action; |
275 | 0 | GVariant *target; |
276 | 0 | gchar *detailed_name; |
277 | |
|
278 | 0 | g_notification_get_button (notification, i, &label, &action, &target); |
279 | 0 | detailed_name = g_action_print_detailed_name (action, target); |
280 | | |
281 | | /* Actions named 'default' collide with libnotify's naming of the |
282 | | * default action. Rewriting them to something unique is enough, |
283 | | * because those actions can never be activated (they aren't |
284 | | * prefixed with 'app.'). |
285 | | */ |
286 | 0 | if (g_str_equal (detailed_name, "default")) |
287 | 0 | { |
288 | 0 | g_free (detailed_name); |
289 | 0 | detailed_name = g_dbus_generate_guid (); |
290 | 0 | } |
291 | |
|
292 | 0 | g_variant_builder_add_value (&action_builder, g_variant_new_take_string (detailed_name)); |
293 | 0 | g_variant_builder_add_value (&action_builder, g_variant_new_take_string (label)); |
294 | |
|
295 | 0 | g_free (action); |
296 | 0 | if (target) |
297 | 0 | g_variant_unref (target); |
298 | 0 | } |
299 | |
|
300 | 0 | g_variant_builder_init (&hints_builder, G_VARIANT_TYPE ("a{sv}")); |
301 | 0 | g_variant_builder_add (&hints_builder, "{sv}", "desktop-entry", |
302 | 0 | g_variant_new_string (g_application_get_application_id (app))); |
303 | 0 | urgency = urgency_from_priority (g_notification_get_priority (notification)); |
304 | 0 | g_variant_builder_add (&hints_builder, "{sv}", "urgency", g_variant_new_byte (urgency)); |
305 | 0 | icon = g_notification_get_icon (notification); |
306 | 0 | if (icon != NULL) |
307 | 0 | { |
308 | 0 | if (G_IS_FILE_ICON (icon)) |
309 | 0 | { |
310 | 0 | GFile *file; |
311 | |
|
312 | 0 | file = g_file_icon_get_file (G_FILE_ICON (icon)); |
313 | 0 | g_variant_builder_add (&hints_builder, "{sv}", "image-path", |
314 | 0 | g_variant_new_take_string (g_file_get_path (file))); |
315 | 0 | } |
316 | 0 | else if (G_IS_THEMED_ICON (icon)) |
317 | 0 | { |
318 | 0 | const gchar* const* icon_names = g_themed_icon_get_names(G_THEMED_ICON (icon)); |
319 | | /* Take first name from GThemedIcon */ |
320 | 0 | g_variant_builder_add (&hints_builder, "{sv}", "image-path", |
321 | 0 | g_variant_new_string (icon_names[0])); |
322 | 0 | } |
323 | 0 | } |
324 | |
|
325 | 0 | body = g_notification_get_body (notification); |
326 | |
|
327 | 0 | parameters = g_variant_new ("(susssasa{sv}i)", |
328 | 0 | "", /* app name */ |
329 | 0 | replace_id, |
330 | 0 | "", /* app icon */ |
331 | 0 | g_notification_get_title (notification), |
332 | 0 | body ? body : "", |
333 | 0 | &action_builder, |
334 | 0 | &hints_builder, |
335 | 0 | -1); /* expire_timeout */ |
336 | |
|
337 | 0 | g_dbus_connection_call (con, "org.freedesktop.Notifications", "/org/freedesktop/Notifications", |
338 | 0 | "org.freedesktop.Notifications", "Notify", |
339 | 0 | parameters, G_VARIANT_TYPE ("(u)"), |
340 | 0 | G_DBUS_CALL_FLAGS_NONE, -1, NULL, |
341 | 0 | callback, user_data); |
342 | 0 | } |
343 | | |
344 | | static void |
345 | | notification_sent (GObject *source_object, |
346 | | GAsyncResult *result, |
347 | | gpointer user_data) |
348 | 0 | { |
349 | 0 | FreedesktopNotification *n = user_data; |
350 | 0 | GVariant *val; |
351 | 0 | GError *error = NULL; |
352 | 0 | static gboolean warning_printed = FALSE; |
353 | |
|
354 | 0 | val = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), result, &error); |
355 | 0 | if (val) |
356 | 0 | { |
357 | 0 | GFdoNotificationBackend *backend = n->backend; |
358 | 0 | FreedesktopNotification *match; |
359 | |
|
360 | 0 | g_variant_get (val, "(u)", &n->notify_id); |
361 | 0 | g_variant_unref (val); |
362 | |
|
363 | 0 | match = g_fdo_notification_backend_find_notification_by_notify_id (backend, n->notify_id); |
364 | 0 | if (match != NULL) |
365 | 0 | { |
366 | 0 | backend->notifications = g_slist_remove (backend->notifications, match); |
367 | 0 | freedesktop_notification_free (match); |
368 | 0 | } |
369 | 0 | backend->notifications = g_slist_prepend (backend->notifications, n); |
370 | 0 | } |
371 | 0 | else |
372 | 0 | { |
373 | 0 | if (!warning_printed) |
374 | 0 | { |
375 | 0 | g_warning ("unable to send notifications through org.freedesktop.Notifications: %s", |
376 | 0 | error->message); |
377 | 0 | warning_printed = TRUE; |
378 | 0 | } |
379 | |
|
380 | 0 | freedesktop_notification_free (n); |
381 | 0 | g_error_free (error); |
382 | 0 | } |
383 | 0 | } |
384 | | |
385 | | static void |
386 | | g_fdo_notification_backend_dispose (GObject *object) |
387 | 0 | { |
388 | 0 | GFdoNotificationBackend *backend = G_FDO_NOTIFICATION_BACKEND (object); |
389 | |
|
390 | 0 | if (backend->bus_name_id) |
391 | 0 | { |
392 | 0 | g_bus_unwatch_name (backend->bus_name_id); |
393 | 0 | backend->bus_name_id = 0; |
394 | 0 | } |
395 | |
|
396 | 0 | if (backend->notify_subscription) |
397 | 0 | { |
398 | 0 | GDBusConnection *session_bus; |
399 | |
|
400 | 0 | session_bus = G_NOTIFICATION_BACKEND (backend)->dbus_connection; |
401 | 0 | g_dbus_connection_signal_unsubscribe (session_bus, backend->notify_subscription); |
402 | 0 | backend->notify_subscription = 0; |
403 | 0 | } |
404 | |
|
405 | 0 | if (backend->notifications) |
406 | 0 | { |
407 | 0 | g_slist_free_full (backend->notifications, freedesktop_notification_free); |
408 | 0 | backend->notifications = NULL; |
409 | 0 | } |
410 | |
|
411 | 0 | G_OBJECT_CLASS (g_fdo_notification_backend_parent_class)->dispose (object); |
412 | 0 | } |
413 | | |
414 | | static gboolean |
415 | | g_fdo_notification_backend_is_supported (void) |
416 | 0 | { |
417 | | /* This is the fallback backend with the lowest priority. To avoid an |
418 | | * unnecessary synchronous dbus call to check for |
419 | | * org.freedesktop.Notifications, this function always succeeds. A |
420 | | * warning will be printed when sending the first notification fails. |
421 | | */ |
422 | 0 | return TRUE; |
423 | 0 | } |
424 | | |
425 | | static void |
426 | | g_fdo_notification_backend_send_notification (GNotificationBackend *backend, |
427 | | const gchar *id, |
428 | | GNotification *notification) |
429 | 0 | { |
430 | 0 | GFdoNotificationBackend *self = G_FDO_NOTIFICATION_BACKEND (backend); |
431 | 0 | FreedesktopNotification *n, *tmp; |
432 | |
|
433 | 0 | if (self->bus_name_id == 0) |
434 | 0 | { |
435 | 0 | self->bus_name_id = g_bus_watch_name_on_connection (backend->dbus_connection, |
436 | 0 | "org.freedesktop.Notifications", |
437 | 0 | G_BUS_NAME_WATCHER_FLAGS_NONE, |
438 | 0 | NULL, |
439 | 0 | name_vanished_handler_cb, |
440 | 0 | backend, |
441 | 0 | NULL); |
442 | 0 | } |
443 | |
|
444 | 0 | if (self->notify_subscription == 0) |
445 | 0 | { |
446 | 0 | self->notify_subscription = |
447 | 0 | g_dbus_connection_signal_subscribe (backend->dbus_connection, |
448 | 0 | "org.freedesktop.Notifications", |
449 | 0 | "org.freedesktop.Notifications", NULL, |
450 | 0 | "/org/freedesktop/Notifications", NULL, |
451 | 0 | G_DBUS_SIGNAL_FLAGS_NONE, |
452 | 0 | notify_signal, backend, NULL); |
453 | 0 | } |
454 | |
|
455 | 0 | n = freedesktop_notification_new (self, id, notification); |
456 | |
|
457 | 0 | tmp = g_fdo_notification_backend_find_notification (self, id); |
458 | 0 | if (tmp) |
459 | 0 | n->notify_id = tmp->notify_id; |
460 | |
|
461 | 0 | call_notify (backend->dbus_connection, backend->application, n->notify_id, notification, notification_sent, n); |
462 | 0 | } |
463 | | |
464 | | static void |
465 | | g_fdo_notification_backend_withdraw_notification (GNotificationBackend *backend, |
466 | | const gchar *id) |
467 | 0 | { |
468 | 0 | GFdoNotificationBackend *self = G_FDO_NOTIFICATION_BACKEND (backend); |
469 | 0 | FreedesktopNotification *n; |
470 | |
|
471 | 0 | n = g_fdo_notification_backend_find_notification (self, id); |
472 | 0 | if (n) |
473 | 0 | { |
474 | 0 | if (n->notify_id > 0) |
475 | 0 | { |
476 | 0 | g_dbus_connection_call (backend->dbus_connection, |
477 | 0 | "org.freedesktop.Notifications", |
478 | 0 | "/org/freedesktop/Notifications", |
479 | 0 | "org.freedesktop.Notifications", "CloseNotification", |
480 | 0 | g_variant_new ("(u)", n->notify_id), NULL, |
481 | 0 | G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL); |
482 | 0 | } |
483 | |
|
484 | 0 | self->notifications = g_slist_remove (self->notifications, n); |
485 | 0 | freedesktop_notification_free (n); |
486 | 0 | } |
487 | 0 | } |
488 | | |
489 | | static void |
490 | | g_fdo_notification_backend_init (GFdoNotificationBackend *backend) |
491 | 0 | { |
492 | 0 | } |
493 | | |
494 | | static void |
495 | | g_fdo_notification_backend_class_init (GFdoNotificationBackendClass *class) |
496 | 0 | { |
497 | 0 | GObjectClass *object_class = G_OBJECT_CLASS (class); |
498 | 0 | GNotificationBackendClass *backend_class = G_NOTIFICATION_BACKEND_CLASS (class); |
499 | |
|
500 | 0 | object_class->dispose = g_fdo_notification_backend_dispose; |
501 | |
|
502 | 0 | backend_class->is_supported = g_fdo_notification_backend_is_supported; |
503 | 0 | backend_class->send_notification = g_fdo_notification_backend_send_notification; |
504 | 0 | backend_class->withdraw_notification = g_fdo_notification_backend_withdraw_notification; |
505 | 0 | } |