Branch data Line data Source code
1 : : // SPDX-License-Identifier: GPL-3.0-or-later
2 : : // SPDX-FileCopyrightText: Andy Holmes <andrew.g.r.holmes@gmail.com>
3 : :
4 : : #define G_LOG_DOMAIN "valent-fdo-notifications"
5 : :
6 : : #include "config.h"
7 : :
8 : : #include <gdk-pixbuf/gdk-pixbuf.h>
9 : : #include <gio/gio.h>
10 : : #include <valent.h>
11 : :
12 : : #include "valent-fdo-notifications.h"
13 : :
14 : :
15 : : struct _ValentFdoNotifications
16 : : {
17 : : ValentNotificationsAdapter parent_instance;
18 : :
19 : : GDBusInterfaceVTable vtable;
20 : : GDBusNodeInfo *node_info;
21 : : GDBusInterfaceInfo *iface_info;
22 : : GDBusConnection *monitor;
23 : : unsigned int monitor_id;
24 : : char *name_owner;
25 : : unsigned int name_owner_id;
26 : : GDBusConnection *session;
27 : : unsigned int closed_id;
28 : : };
29 : :
30 : : static void g_async_initable_iface_init (GAsyncInitableIface *iface);
31 : :
32 [ + + + - ]: 9 : G_DEFINE_FINAL_TYPE_WITH_CODE (ValentFdoNotifications, valent_fdo_notifications, VALENT_TYPE_NOTIFICATIONS_ADAPTER,
33 : : G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, g_async_initable_iface_init))
34 : :
35 : :
36 : : /*
37 : : * Map of notification-spec urgency to GNotificationPriority
38 : : *
39 : : * See: https://developer-old.gnome.org/notification-spec/#urgency-levels
40 : : */
41 : : static const unsigned int urgencies[] = {
42 : : G_NOTIFICATION_PRIORITY_LOW,
43 : : G_NOTIFICATION_PRIORITY_NORMAL,
44 : : G_NOTIFICATION_PRIORITY_URGENT
45 : : };
46 : :
47 : :
48 : : /*
49 : : * GDBusInterfaceSkeleton
50 : : */
51 : : static const char interface_xml[] =
52 : : "<node>"
53 : : " <interface name='org.freedesktop.Notifications'>"
54 : : " <method name='Notify'>"
55 : : " <arg name='appName' type='s' direction='in'/>"
56 : : " <arg name='replacesId' type='u' direction='in'/>"
57 : : " <arg name='iconName' type='s' direction='in'/>"
58 : : " <arg name='summary' type='s' direction='in'/>"
59 : : " <arg name='body' type='s' direction='in'/>"
60 : : " <arg name='actions' type='as' direction='in'/>"
61 : : " <arg name='hints' type='a{sv}' direction='in'/>"
62 : : " <arg name='timeout' type='i' direction='in'/>"
63 : : " </method>"
64 : : " </interface>"
65 : : "</node>";
66 : :
67 : : static const char *interface_matches[] = {
68 : : "interface='org.freedesktop.Notifications',member='Notify',type='method_call'",
69 : : NULL
70 : : };
71 : :
72 : :
73 : : static GIcon *
74 : 1 : _g_icon_new_for_variant (GVariant *image_data)
75 : : {
76 : 1 : GdkPixbuf *pixbuf;
77 : 1 : int32_t width, height, rowstride;
78 : 1 : gboolean has_alpha;
79 : 1 : int32_t bits_per_sample, n_channels;
80 : 2 : g_autoptr (GVariant) data_variant = NULL;
81 : 1 : unsigned char *data = NULL;
82 : 1 : size_t data_len = 0;
83 : 1 : size_t expected_len = 0;
84 : :
85 : 1 : g_variant_get (image_data, "(iiibii@ay)",
86 : : &width,
87 : : &height,
88 : : &rowstride,
89 : : &has_alpha,
90 : : &bits_per_sample,
91 : : &n_channels,
92 : : &data_variant);
93 : :
94 : 1 : data_len = g_variant_get_size (data_variant);
95 : 1 : expected_len = (height - 1) * rowstride + width
96 : 1 : * ((n_channels * bits_per_sample + 7) / 8);
97 : :
98 [ - + ]: 1 : if (expected_len != data_len)
99 : : {
100 : 0 : g_warning ("Expected image data to be of length %zu not %zu",
101 : : expected_len,
102 : : data_len);
103 : 0 : return NULL;
104 : : }
105 : :
106 : 1 : data = g_memdup2 (g_variant_get_data (data_variant), data_len);
107 : 1 : pixbuf = gdk_pixbuf_new_from_data (data,
108 : : GDK_COLORSPACE_RGB,
109 : : has_alpha,
110 : : bits_per_sample,
111 : : width,
112 : : height,
113 : : rowstride,
114 : : (GdkPixbufDestroyNotify)(GCallback)g_free,
115 : : NULL);
116 : :
117 : 1 : return (GIcon *)pixbuf;
118 : : }
119 : :
120 : : static void
121 : 1 : _notification_closed (ValentNotificationsAdapter *adapter,
122 : : GVariant *parameters)
123 : : {
124 : 1 : uint32_t id, reason;
125 : 2 : g_autofree char *id_str = NULL;
126 : :
127 : 1 : g_variant_get (parameters, "(uu)", &id, &reason);
128 : :
129 : 1 : id_str = g_strdup_printf ("%u", id);
130 : 1 : valent_notifications_adapter_notification_removed (adapter, id_str);
131 : 1 : }
132 : :
133 : : static void
134 : 2 : _notify (ValentNotificationsAdapter *adapter,
135 : : GVariant *parameters)
136 : : {
137 : 4 : g_autoptr (ValentNotification) notification = NULL;
138 [ + - ]: 2 : g_autoptr (GIcon) icon = NULL;
139 : :
140 : 2 : const char *app_name;
141 : 2 : uint32_t replaces_id;
142 : 2 : const char *app_icon;
143 : 2 : const char *summary;
144 : 2 : const char *body;
145 [ + - ]: 2 : g_autoptr (GVariant) actions = NULL;
146 [ + - ]: 2 : g_autoptr (GVariant) hints = NULL;
147 : 2 : int32_t expire_timeout;
148 : :
149 [ + - ]: 2 : g_autofree char *replaces_id_str = NULL;
150 : 2 : g_autoptr (GVariant) image_data = NULL;
151 : 2 : const char *image_path;
152 : 2 : unsigned char urgency;
153 : :
154 : : /* Extract what we need from the parameters */
155 : 2 : g_variant_get (parameters, "(&su&s&s&s@as@a{sv}i)",
156 : : &app_name,
157 : : &replaces_id,
158 : : &app_icon,
159 : : &summary,
160 : : &body,
161 : : &actions,
162 : : &hints,
163 : : &expire_timeout);
164 : :
165 : 2 : replaces_id_str = g_strdup_printf ("%u", replaces_id);
166 : :
167 : : /* Deserialize GNotification into ValentNotification */
168 : 2 : notification = valent_notification_new (NULL);
169 : 2 : valent_notification_set_id (notification, replaces_id_str);
170 : 2 : valent_notification_set_application (notification, app_name);
171 : 2 : valent_resource_set_title (VALENT_RESOURCE (notification), summary);
172 : 2 : valent_notification_set_body (notification, body);
173 : :
174 : : /* This bizarre ordering is required by the specification.
175 : : * See: https://developer-old.gnome.org/notification-spec/#icons-and-images
176 : : */
177 [ + + - + ]: 3 : if (g_variant_lookup (hints, "image-data", "@(iiibiiay)", &image_data) ||
178 : 1 : g_variant_lookup (hints, "image_data", "@(iiibiiay)", &image_data))
179 : : {
180 : 1 : icon = _g_icon_new_for_variant (image_data);
181 : 1 : valent_notification_set_icon (notification, icon);
182 : : }
183 [ + - - + ]: 2 : else if (g_variant_lookup (hints, "image-path", "&s", &image_path) ||
184 : 1 : g_variant_lookup (hints, "image_path", "&s", &image_path))
185 : : {
186 : 0 : icon = g_icon_new_for_string (image_path, NULL);
187 : 0 : valent_notification_set_icon (notification, icon);
188 : : }
189 [ + - ]: 1 : else if (*app_icon != '\0')
190 : : {
191 : 1 : icon = g_icon_new_for_string (app_icon, NULL);
192 : 1 : valent_notification_set_icon (notification, icon);
193 : : }
194 [ # # ]: 0 : else if (g_variant_lookup (hints, "icon_data", "@(iiibiiay)", &image_data))
195 : : {
196 : 0 : icon = _g_icon_new_for_variant (image_data);
197 : 0 : valent_notification_set_icon (notification, icon);
198 : : }
199 : :
200 : : /* Map libnotify urgency to GNotification priority */
201 [ + - + - ]: 2 : if (g_variant_lookup (hints, "urgency", "y", &urgency) && urgency < G_N_ELEMENTS (urgencies))
202 : 2 : valent_notification_set_priority (notification, urgencies[urgency]);
203 : : else
204 : 0 : valent_notification_set_priority (notification, G_NOTIFICATION_PRIORITY_NORMAL);
205 : :
206 : : /* Set a timestamp */
207 : 2 : valent_notification_set_time (notification, valent_timestamp_ms ());
208 : :
209 [ + + ]: 2 : valent_notifications_adapter_notification_added (adapter, notification);
210 : 2 : }
211 : :
212 : : static void
213 : 2 : valent_fdo_notifications_method_call (GDBusConnection *connection,
214 : : const char *sender,
215 : : const char *object_path,
216 : : const char *interface_name,
217 : : const char *method_name,
218 : : GVariant *parameters,
219 : : GDBusMethodInvocation *invocation,
220 : : gpointer user_data)
221 : : {
222 : 2 : ValentNotificationsAdapter *adapter = VALENT_NOTIFICATIONS_ADAPTER (user_data);
223 : 2 : ValentFdoNotifications *self = VALENT_FDO_NOTIFICATIONS (user_data);
224 : 2 : GDBusMessage *message;
225 : 2 : const char *destination;
226 : :
227 [ + - ]: 2 : g_assert (VALENT_IS_NOTIFICATIONS_ADAPTER (adapter));
228 [ - + ]: 2 : g_assert (VALENT_IS_FDO_NOTIFICATIONS (self));
229 : :
230 : 2 : message = g_dbus_method_invocation_get_message (invocation);
231 : 2 : destination = g_dbus_message_get_destination (message);
232 : :
233 : : // TODO: accepting notifications from the well-known name causes duplicates on
234 : : // GNOME Shell where a proxy daemon is run.
235 [ - + - - ]: 2 : if (g_strcmp0 ("org.freedesktop.Notifications", destination) != 0 &&
236 : 0 : g_strcmp0 (self->name_owner, destination) != 0)
237 : 0 : goto out;
238 : :
239 [ - + ]: 2 : if (g_strcmp0 (method_name, "Notify") == 0)
240 : 2 : _notify (adapter, parameters);
241 : :
242 : 0 : out:
243 : 2 : g_object_unref (invocation);
244 : 2 : }
245 : :
246 : : static void
247 : 1 : on_notification_closed (GDBusConnection *connection,
248 : : const char *sender_name,
249 : : const char *object_path,
250 : : const char *interface_name,
251 : : const char *signal_name,
252 : : GVariant *parameters,
253 : : gpointer user_data)
254 : : {
255 : 1 : ValentNotificationsAdapter *adapter = VALENT_NOTIFICATIONS_ADAPTER (user_data);
256 : :
257 [ + - ]: 1 : g_assert (VALENT_IS_NOTIFICATIONS_ADAPTER (adapter));
258 [ - + ]: 1 : g_assert (g_strcmp0 (signal_name, "NotificationClosed") == 0);
259 : :
260 : 1 : _notification_closed (adapter, parameters);
261 : 1 : }
262 : :
263 : : /*
264 : : * Setup
265 : : */
266 : : static void
267 : 1 : on_name_appeared (GDBusConnection *connection,
268 : : const char *name,
269 : : const char *name_owner,
270 : : gpointer user_data)
271 : : {
272 : 1 : ValentFdoNotifications *self = VALENT_FDO_NOTIFICATIONS (user_data);
273 : :
274 [ - + ]: 1 : self->name_owner = g_strdup (name_owner);
275 : 1 : g_set_object (&self->session, connection);
276 : :
277 [ + - ]: 1 : if (self->closed_id == 0)
278 : : {
279 : 1 : self->closed_id =
280 : 1 : g_dbus_connection_signal_subscribe (self->session,
281 : : "org.freedesktop.Notifications",
282 : : "org.freedesktop.Notifications",
283 : : "NotificationClosed",
284 : : "/org/freedesktop/Notifications",
285 : : NULL,
286 : : G_DBUS_SIGNAL_FLAGS_NONE,
287 : : on_notification_closed,
288 : : self, NULL);
289 : : }
290 : :
291 : 1 : valent_extension_plugin_state_changed (VALENT_EXTENSION (self),
292 : : VALENT_PLUGIN_STATE_ACTIVE,
293 : : NULL);
294 : 1 : }
295 : :
296 : : static void
297 : 0 : on_name_vanished (GDBusConnection *connection,
298 : : const char *name,
299 : : gpointer user_data)
300 : : {
301 : 0 : ValentFdoNotifications *self = VALENT_FDO_NOTIFICATIONS (user_data);
302 : :
303 [ # # ]: 0 : g_clear_pointer (&self->name_owner, g_free);
304 : :
305 [ # # ]: 0 : if (self->closed_id > 0)
306 : : {
307 : 0 : g_dbus_connection_signal_unsubscribe (self->session, self->closed_id);
308 : 0 : self->closed_id = 0;
309 : : }
310 : :
311 : 0 : valent_extension_plugin_state_changed (VALENT_EXTENSION (self),
312 : : VALENT_PLUGIN_STATE_INACTIVE,
313 : : NULL);
314 : 0 : }
315 : :
316 : : static void
317 : 1 : become_monitor_cb (GDBusConnection *connection,
318 : : GAsyncResult *result,
319 : : gpointer user_data)
320 : : {
321 : 1 : g_autoptr (GTask) task = G_TASK (user_data);
322 : 1 : ValentFdoNotifications *self = g_task_get_source_object (task);
323 [ - - + - ]: 1 : g_autoptr (GVariant) reply = NULL;
324 [ - - ]: 1 : g_autoptr (GError) error = NULL;
325 : :
326 : 1 : reply = g_dbus_connection_call_finish (connection, result, &error);
327 : :
328 [ - + ]: 1 : if (reply == NULL)
329 : : {
330 : 0 : valent_extension_plugin_state_changed (VALENT_EXTENSION (self),
331 : : VALENT_PLUGIN_STATE_ERROR,
332 : : error);
333 [ # # ]: 0 : g_clear_object (&self->monitor);
334 : 0 : g_dbus_error_strip_remote_error (error);
335 [ # # ]: 0 : return g_task_return_error (task, g_steal_pointer (&error));
336 : : }
337 : :
338 : : /* Watch the true name owner */
339 : 1 : self->name_owner_id = g_bus_watch_name (G_BUS_TYPE_SESSION,
340 : : "org.freedesktop.Notifications",
341 : : G_BUS_NAME_WATCHER_FLAGS_NONE,
342 : : on_name_appeared,
343 : : on_name_vanished,
344 : : self, NULL);
345 : :
346 : :
347 : : /* Report the adapter as active */
348 : 1 : valent_extension_plugin_state_changed (VALENT_EXTENSION (self),
349 : : VALENT_PLUGIN_STATE_ACTIVE,
350 : : NULL);
351 [ - + ]: 1 : g_task_return_boolean (task, TRUE);
352 : : }
353 : :
354 : : static void
355 : 1 : new_for_address_cb (GObject *object,
356 : : GAsyncResult *result,
357 : : gpointer user_data)
358 : : {
359 : 1 : g_autoptr (GTask) task = G_TASK (user_data);
360 : 1 : ValentFdoNotifications *self = g_task_get_source_object (task);
361 : 1 : GCancellable *cancellable = g_task_get_cancellable (task);
362 [ - - ]: 1 : g_autoptr (GError) error = NULL;
363 : :
364 : 1 : self->monitor = g_dbus_connection_new_for_address_finish (result, &error);
365 : :
366 [ - + ]: 1 : if (self->monitor == NULL)
367 : : {
368 : 0 : valent_extension_plugin_state_changed (VALENT_EXTENSION (self),
369 : : VALENT_PLUGIN_STATE_ERROR,
370 : : error);
371 : 0 : g_dbus_error_strip_remote_error (error);
372 : 0 : return g_task_return_error (task, g_steal_pointer (&error));
373 : : }
374 : :
375 : : /* Export the monitor interface */
376 : 2 : self->monitor_id =
377 : 1 : g_dbus_connection_register_object (self->monitor,
378 : : "/org/freedesktop/Notifications",
379 : : self->iface_info,
380 : 1 : &self->vtable,
381 : : self, NULL,
382 : : &error);
383 : :
384 [ - + ]: 1 : if (self->monitor_id == 0)
385 : : {
386 : 0 : valent_extension_plugin_state_changed (VALENT_EXTENSION (self),
387 : : VALENT_PLUGIN_STATE_ERROR,
388 : : error);
389 [ # # ]: 0 : g_clear_object (&self->monitor);
390 : 0 : g_dbus_error_strip_remote_error (error);
391 : 0 : return g_task_return_error (task, g_steal_pointer (&error));
392 : : }
393 : :
394 : : /* Become a monitor for notifications */
395 [ - + ]: 1 : g_dbus_connection_call (self->monitor,
396 : : "org.freedesktop.DBus",
397 : : "/org/freedesktop/DBus",
398 : : "org.freedesktop.DBus.Monitoring",
399 : : "BecomeMonitor",
400 : : g_variant_new ("(^asu)", interface_matches, 0),
401 : : NULL,
402 : : G_DBUS_CALL_FLAGS_NONE,
403 : : -1,
404 : : cancellable,
405 : : (GAsyncReadyCallback)become_monitor_cb,
406 : : g_steal_pointer (&task));
407 : : }
408 : :
409 : :
410 : : /*
411 : : * GAsyncInitable
412 : : */
413 : : static void
414 : 1 : valent_fdo_notifications_init_async (GAsyncInitable *initable,
415 : : int io_priority,
416 : : GCancellable *cancellable,
417 : : GAsyncReadyCallback callback,
418 : : gpointer user_data)
419 : : {
420 : 1 : g_autoptr (GTask) task = NULL;
421 [ - - ]: 1 : g_autofree char *address = NULL;
422 : 1 : g_autoptr (GError) error = NULL;
423 : :
424 [ + - ]: 1 : g_assert (VALENT_IS_FDO_NOTIFICATIONS (initable));
425 : :
426 : 1 : task = g_task_new (initable, cancellable, callback, user_data);
427 : 1 : g_task_set_priority (task, io_priority);
428 [ + - ]: 1 : g_task_set_source_tag (task, valent_fdo_notifications_init_async);
429 : :
430 : : /* Get a bus address */
431 : 1 : address = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SESSION,
432 : : cancellable,
433 : : &error);
434 : :
435 [ - + ]: 1 : if (address == NULL)
436 : : {
437 : 0 : valent_extension_plugin_state_changed (VALENT_EXTENSION (initable),
438 : : VALENT_PLUGIN_STATE_ERROR,
439 : : error);
440 [ # # ]: 0 : return g_task_return_error (task, g_steal_pointer (&error));
441 : : }
442 : :
443 : : /* Get a dedicated connection for monitoring */
444 [ - + ]: 1 : g_dbus_connection_new_for_address (address,
445 : : G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
446 : : G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
447 : : NULL,
448 : : cancellable,
449 : : (GAsyncReadyCallback)new_for_address_cb,
450 : : g_steal_pointer (&task));
451 : : }
452 : :
453 : : static void
454 : 2 : g_async_initable_iface_init (GAsyncInitableIface *iface)
455 : : {
456 : 2 : iface->init_async = valent_fdo_notifications_init_async;
457 : 2 : }
458 : :
459 : : /*
460 : : * ValentObject
461 : : */
462 : : static void
463 : 2 : valent_fdo_notifications_destroy (ValentObject *object)
464 : : {
465 : 2 : ValentFdoNotifications *self = VALENT_FDO_NOTIFICATIONS (object);
466 : :
467 [ + + ]: 2 : if (self->closed_id > 0)
468 : : {
469 : 1 : g_dbus_connection_signal_unsubscribe (self->session, self->closed_id);
470 : 1 : self->closed_id = 0;
471 : : }
472 : :
473 [ + + ]: 2 : if (self->name_owner_id > 0)
474 : : {
475 : 1 : g_clear_handle_id (&self->name_owner_id, g_bus_unwatch_name);
476 [ + - ]: 1 : g_clear_pointer (&self->name_owner, g_free);
477 : : }
478 : :
479 [ + + ]: 2 : if (self->monitor_id != 0)
480 : : {
481 : 1 : g_dbus_connection_unregister_object (self->monitor, self->monitor_id);
482 : 1 : self->monitor_id = 0;
483 : : }
484 : :
485 [ + + ]: 2 : g_clear_object (&self->monitor);
486 [ + + ]: 2 : g_clear_object (&self->session);
487 : :
488 : 2 : VALENT_OBJECT_CLASS (valent_fdo_notifications_parent_class)->destroy (object);
489 : 2 : }
490 : :
491 : : /*
492 : : * GObject
493 : : */
494 : : static void
495 : 1 : valent_fdo_notifications_finalize (GObject *object)
496 : : {
497 : 1 : ValentFdoNotifications *self = VALENT_FDO_NOTIFICATIONS (object);
498 : :
499 [ + - ]: 1 : g_clear_pointer (&self->node_info, g_dbus_node_info_unref);
500 : :
501 : 1 : G_OBJECT_CLASS (valent_fdo_notifications_parent_class)->finalize (object);
502 : 1 : }
503 : :
504 : : static void
505 : 2 : valent_fdo_notifications_class_init (ValentFdoNotificationsClass *klass)
506 : : {
507 : 2 : GObjectClass *object_class = G_OBJECT_CLASS (klass);
508 : 2 : ValentObjectClass *vobject_class = VALENT_OBJECT_CLASS (klass);
509 : :
510 : 2 : object_class->finalize = valent_fdo_notifications_finalize;
511 : :
512 : 2 : vobject_class->destroy = valent_fdo_notifications_destroy;
513 : : }
514 : :
515 : : static void
516 : 1 : valent_fdo_notifications_init (ValentFdoNotifications *self)
517 : : {
518 : 1 : self->node_info = g_dbus_node_info_new_for_xml (interface_xml, NULL);
519 : 1 : self->iface_info = self->node_info->interfaces[0];
520 : :
521 : 1 : self->vtable.method_call = valent_fdo_notifications_method_call;
522 : 1 : self->vtable.get_property = NULL;
523 : 1 : self->vtable.set_property = NULL;
524 : 1 : }
525 : :
|