Coverage Report

Created: 2026-11-12 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/glib/gio/gfdonotificationbackend.c
Line
Count
Source
1
/*
2
 * Copyright © 2013 Lars Uebernickel
3
 *
4
 * SPDX-License-Identifier: LGPL-2.1-or-later
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General
17
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18
 *
19
 * Authors: Lars Uebernickel <lars@uebernic.de>
20
 */
21
22
#include "config.h"
23
24
#include "gnotificationbackend.h"
25
26
#include "gapplication.h"
27
#include "giomodule-priv.h"
28
#include "gnotification-private.h"
29
#include "gdbusconnection.h"
30
#include "gdbusnamewatching.h"
31
#include "gactiongroup.h"
32
#include "gaction.h"
33
#include "gthemedicon.h"
34
#include "gfileicon.h"
35
#include "gfile.h"
36
#include "gdbusutils.h"
37
38
#define G_TYPE_FDO_NOTIFICATION_BACKEND  (g_fdo_notification_backend_get_type ())
39
0
#define G_FDO_NOTIFICATION_BACKEND(o)    (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_FDO_NOTIFICATION_BACKEND, GFdoNotificationBackend))
40
41
typedef struct _GFdoNotificationBackend GFdoNotificationBackend;
42
typedef GNotificationBackendClass       GFdoNotificationBackendClass;
43
44
struct _GFdoNotificationBackend
45
{
46
  GNotificationBackend parent;
47
48
  guint   bus_name_id;
49
50
  guint   notify_subscription;
51
  GSList *notifications;
52
};
53
54
GType g_fdo_notification_backend_get_type (void);
55
56
0
G_DEFINE_TYPE_WITH_CODE (GFdoNotificationBackend, g_fdo_notification_backend, G_TYPE_NOTIFICATION_BACKEND,
57
0
  _g_io_modules_ensure_extension_points_registered ();
58
0
  g_io_extension_point_implement (G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME,
59
0
                                 g_define_type_id, "freedesktop", 0))
60
0
61
0
typedef struct
62
0
{
63
0
  GFdoNotificationBackend *backend;
64
0
  gchar *id;
65
0
  guint32 notify_id;
66
0
  gchar *default_action;  /* (nullable) (owned) */
67
0
  GVariant *default_action_target;  /* (nullable) (owned), not floating */
68
0
  gchar *activation_token; /* (nullable) (owned) */
69
0
} FreedesktopNotification;
70
0
71
0
static void
72
0
freedesktop_notification_free (gpointer data)
73
0
{
74
0
  FreedesktopNotification *n = data;
75
76
0
  g_object_unref (n->backend);
77
0
  g_free (n->id);
78
0
  g_free (n->default_action);
79
0
  if (n->default_action_target)
80
0
    g_variant_unref (n->default_action_target);
81
0
  g_free (n->activation_token);
82
83
0
  g_slice_free (FreedesktopNotification, n);
84
0
}
85
86
static FreedesktopNotification *
87
freedesktop_notification_new (GFdoNotificationBackend *backend,
88
                              const gchar             *id,
89
                              GNotification           *notification)
90
0
{
91
0
  FreedesktopNotification *n;
92
93
0
  n = g_slice_new0 (FreedesktopNotification);
94
0
  n->backend = g_object_ref (backend);
95
0
  n->id = g_strdup (id);
96
0
  n->notify_id = 0;
97
0
  g_notification_get_default_action (notification,
98
0
                                     &n->default_action,
99
0
                                     &n->default_action_target);
100
101
0
  return n;
102
0
}
103
104
static FreedesktopNotification *
105
g_fdo_notification_backend_find_notification (GFdoNotificationBackend *backend,
106
                                              const gchar             *id)
107
0
{
108
0
  GSList *it;
109
110
0
  for (it = backend->notifications; it != NULL; it = it->next)
111
0
    {
112
0
      FreedesktopNotification *n = it->data;
113
0
      if (g_str_equal (n->id, id))
114
0
        return n;
115
0
    }
116
117
0
  return NULL;
118
0
}
119
120
static FreedesktopNotification *
121
g_fdo_notification_backend_find_notification_by_notify_id (GFdoNotificationBackend *backend,
122
                                                           guint32                  id)
123
0
{
124
0
  GSList *it;
125
126
0
  for (it = backend->notifications; it != NULL; it = it->next)
127
0
    {
128
0
      FreedesktopNotification *n = it->data;
129
0
      if (n->notify_id == id)
130
0
        return n;
131
0
    }
132
133
0
  return NULL;
134
0
}
135
136
static gboolean
137
activate_action (GFdoNotificationBackend *backend,
138
                 const gchar             *name,
139
                 GVariant                *parameter,
140
                 const gchar             *activation_token)
141
0
{
142
0
  GNotificationBackend *g_backend = G_NOTIFICATION_BACKEND (backend);
143
144
0
  GApplication *application = g_notification_backend_dup_application (g_backend);
145
0
  g_assert (application != NULL);
146
147
  /* Callers should not provide a floating variant here */
148
0
  g_assert (parameter == NULL || !g_variant_is_floating (parameter));
149
150
0
  gboolean retval = FALSE;
151
152
0
  GVariantBuilder builder;
153
0
  GVariant *platform_data;
154
155
0
  g_variant_builder_init_static (&builder, G_VARIANT_TYPE ("a{sv}"));
156
0
  if (activation_token)
157
0
    {
158
0
      g_variant_builder_add (&builder, "{sv}", "desktop-startup-id", g_variant_new_string (activation_token));
159
0
      g_variant_builder_add (&builder, "{sv}", "activation-token", g_variant_new_string (activation_token));
160
0
    }
161
0
  platform_data = g_variant_ref_sink (g_variant_builder_end (&builder));
162
163
0
  if (name != NULL &&
164
0
      g_str_has_prefix (name, "app."))
165
0
    {
166
0
      const GVariantType *parameter_type = NULL;
167
0
      const gchar *action_name = name + strlen ("app.");
168
169
      /* @name and @parameter come as untrusted input over D-Bus, so validate them first */
170
0
      if (g_action_group_query_action (G_ACTION_GROUP (application),
171
0
                                       action_name, NULL, &parameter_type,
172
0
                                       NULL, NULL, NULL) &&
173
0
          ((parameter_type == NULL && parameter == NULL) ||
174
0
           (parameter_type != NULL && parameter != NULL && g_variant_is_of_type (parameter, parameter_type))))
175
0
        {
176
0
          G_APPLICATION_GET_CLASS (application)->before_emit (application, platform_data);
177
0
          g_action_group_activate_action (G_ACTION_GROUP (application), action_name, parameter);
178
0
          G_APPLICATION_GET_CLASS (application)->after_emit (application, platform_data);
179
0
          retval = TRUE;
180
0
        }
181
0
    }
182
0
  else if (name == NULL)
183
0
    {
184
0
      G_APPLICATION_GET_CLASS (application)->before_emit (application, platform_data);
185
0
      g_signal_emit_by_name (application, "activate");
186
0
      G_APPLICATION_GET_CLASS (application)->after_emit (application, platform_data);
187
0
      retval = TRUE;
188
0
    }
189
190
0
  g_variant_unref (platform_data);
191
0
  g_object_unref (application);
192
193
0
  return retval;
194
0
}
195
196
static void
197
notify_signal (GDBusConnection *connection,
198
               const gchar     *sender_name,
199
               const gchar     *object_path,
200
               const gchar     *interface_name,
201
               const gchar     *signal_name,
202
               GVariant        *parameters,
203
               gpointer         user_data)
204
0
{
205
0
  GFdoNotificationBackend *backend = user_data;
206
0
  guint32 id = 0;
207
0
  const gchar *action = NULL;
208
0
  const gchar *activation_token = NULL;
209
0
  FreedesktopNotification *n;
210
0
  gboolean notification_closed = TRUE;
211
212
0
  if (g_str_equal (signal_name, "NotificationClosed") &&
213
0
      g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(uu)")))
214
0
    {
215
0
      g_variant_get (parameters, "(uu)", &id, NULL);
216
0
    }
217
0
  else if (g_str_equal (signal_name, "ActionInvoked") &&
218
0
           g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(us)")))
219
0
    {
220
0
      g_variant_get (parameters, "(u&s)", &id, &action);
221
0
    }
222
0
  else if (g_str_equal (signal_name, "ActivationToken") &&
223
0
           g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(us)")))
224
0
    {
225
0
      g_variant_get (parameters, "(u&s)", &id, &activation_token);
226
0
    }
227
0
  else
228
0
    return;
229
230
0
  n = g_fdo_notification_backend_find_notification_by_notify_id (backend, id);
231
0
  if (n == NULL)
232
0
    return;
233
234
0
  if (activation_token)
235
0
    {
236
0
      g_free (n->activation_token);
237
0
      n->activation_token = g_strdup (activation_token);
238
0
      return;
239
0
    }
240
241
0
  if (action)
242
0
    {
243
0
      if (g_str_equal (action, "default"))
244
0
        {
245
0
          if (!activate_action (backend, n->default_action, n->default_action_target, n->activation_token))
246
0
            notification_closed = FALSE;
247
0
        }
248
0
      else
249
0
        {
250
0
          gchar *name = NULL;
251
0
          GVariant *target = NULL;
252
253
0
          if (!g_action_parse_detailed_name (action, &name, &target, NULL) ||
254
0
              !activate_action (backend, name, target, n->activation_token))
255
0
            notification_closed = FALSE;
256
257
0
          g_free (name);
258
0
          g_clear_pointer (&target, g_variant_unref);
259
0
        }
260
0
    }
261
262
  /* Remove the notification, as it’s either been explicitly closed
263
   * (`NotificationClosed` signal) or has been closed as a result of activating
264
   * an action successfully. GLib doesn’t currently support the `resident` hint
265
   * on notifications which would allow them to stay around after having an
266
   * action invoked on them (see
267
   * https://specifications.freedesktop.org/notification-spec/notification-spec-latest.html#idm45877717456448)
268
   *
269
   * First, get the notification again in case the action redrew it */
270
0
  if (notification_closed)
271
0
    {
272
0
      n = g_fdo_notification_backend_find_notification_by_notify_id (backend, id);
273
0
      if (n != NULL)
274
0
        {
275
0
          backend->notifications = g_slist_remove (backend->notifications, n);
276
0
          freedesktop_notification_free (n);
277
0
        }
278
0
    }
279
0
}
280
281
static void
282
name_vanished_handler_cb (GDBusConnection *connection,
283
                          const gchar     *name,
284
                          gpointer         user_data)
285
0
{
286
0
  GFdoNotificationBackend *backend = user_data;
287
288
0
  if (backend->notifications)
289
0
    {
290
0
      g_slist_free_full (backend->notifications, freedesktop_notification_free);
291
0
      backend->notifications = NULL;
292
0
    }
293
0
}
294
295
/* Converts a GNotificationPriority to an urgency level as defined by
296
 * the freedesktop spec (0: low, 1: normal, 2: critical).
297
 */
298
static guchar
299
urgency_from_priority (GNotificationPriority priority)
300
0
{
301
0
  switch (priority)
302
0
    {
303
0
    case G_NOTIFICATION_PRIORITY_LOW:
304
0
      return 0;
305
306
0
    default:
307
0
    case G_NOTIFICATION_PRIORITY_NORMAL:
308
0
    case G_NOTIFICATION_PRIORITY_HIGH:
309
0
      return 1;
310
311
0
    case G_NOTIFICATION_PRIORITY_URGENT:
312
0
      return 2;
313
0
    }
314
0
}
315
316
static void
317
call_notify (GDBusConnection     *con,
318
             GApplication        *app,
319
             guint32              replace_id,
320
             GNotification       *notification,
321
             GAsyncReadyCallback  callback,
322
             gpointer             user_data)
323
0
{
324
0
  GVariantBuilder action_builder;
325
0
  guint n_buttons;
326
0
  guint i;
327
0
  GVariantBuilder hints_builder;
328
0
  GIcon *icon;
329
0
  GVariant *parameters;
330
0
  const gchar *app_name;
331
0
  const gchar *body;
332
0
  guchar urgency;
333
334
0
  g_variant_builder_init_static (&action_builder, G_VARIANT_TYPE_STRING_ARRAY);
335
0
  if (g_notification_get_default_action (notification, NULL, NULL))
336
0
    {
337
0
      g_variant_builder_add (&action_builder, "s", "default");
338
0
      g_variant_builder_add (&action_builder, "s", "");
339
0
    }
340
341
0
  n_buttons = g_notification_get_n_buttons (notification);
342
0
  for (i = 0; i < n_buttons; i++)
343
0
    {
344
0
      gchar *label;
345
0
      gchar *action;
346
0
      GVariant *target;
347
0
      gchar *detailed_name;
348
349
0
      g_notification_get_button (notification, i, &label, &action, &target);
350
0
      detailed_name = g_action_print_detailed_name (action, target);
351
352
      /* Actions named 'default' collide with libnotify's naming of the
353
       * default action. Rewriting them to something unique is enough,
354
       * because those actions can never be activated (they aren't
355
       * prefixed with 'app.').
356
       */
357
0
      if (g_str_equal (detailed_name, "default"))
358
0
        {
359
0
          g_free (detailed_name);
360
0
          detailed_name = g_dbus_generate_guid ();
361
0
        }
362
363
0
      g_variant_builder_add_value (&action_builder, g_variant_new_take_string (detailed_name));
364
0
      g_variant_builder_add_value (&action_builder, g_variant_new_take_string (label));
365
366
0
      g_free (action);
367
0
      if (target)
368
0
        g_variant_unref (target);
369
0
    }
370
371
0
  g_variant_builder_init_static (&hints_builder, G_VARIANT_TYPE ("a{sv}"));
372
0
  g_variant_builder_add (&hints_builder, "{sv}", "desktop-entry",
373
0
                         g_variant_new_string (g_application_get_application_id (app)));
374
0
  urgency = urgency_from_priority (g_notification_get_priority (notification));
375
0
  g_variant_builder_add (&hints_builder, "{sv}", "urgency", g_variant_new_byte (urgency));
376
0
  if (g_notification_get_category (notification))
377
0
    {
378
0
      g_variant_builder_add (&hints_builder, "{sv}", "category",
379
0
                             g_variant_new_string (g_notification_get_category (notification)));
380
0
    }
381
382
0
  icon = g_notification_get_icon (notification);
383
0
  if (icon != NULL)
384
0
    {
385
0
      if (G_IS_FILE_ICON (icon))
386
0
        {
387
0
           GFile *file;
388
389
0
           file = g_file_icon_get_file (G_FILE_ICON (icon));
390
0
           g_variant_builder_add (&hints_builder, "{sv}", "image-path",
391
0
                                  g_variant_new_take_string (g_file_get_path (file)));
392
0
        }
393
0
      else if (G_IS_THEMED_ICON (icon))
394
0
        {
395
0
           const gchar* const* icon_names = g_themed_icon_get_names(G_THEMED_ICON (icon));
396
           /* Take first name from GThemedIcon */
397
0
           g_variant_builder_add (&hints_builder, "{sv}", "image-path",
398
0
                                  g_variant_new_string (icon_names[0]));
399
0
        }
400
0
    }
401
402
0
  app_name = g_get_application_name ();
403
0
  body = g_notification_get_body (notification);
404
405
0
  parameters = g_variant_new ("(susssasa{sv}i)",
406
0
                              app_name ? app_name : "",
407
0
                              replace_id,
408
0
                              "",           /* app icon */
409
0
                              g_notification_get_title (notification),
410
0
                              body ? body : "",
411
0
                              &action_builder,
412
0
                              &hints_builder,
413
0
                              -1);          /* expire_timeout */
414
415
0
  g_dbus_connection_call (con, "org.freedesktop.Notifications", "/org/freedesktop/Notifications",
416
0
                          "org.freedesktop.Notifications", "Notify",
417
0
                          parameters, G_VARIANT_TYPE ("(u)"),
418
0
                          G_DBUS_CALL_FLAGS_NONE, -1, NULL,
419
0
                          callback, user_data);
420
0
}
421
422
static void
423
notification_sent (GObject      *source_object,
424
                   GAsyncResult *result,
425
                   gpointer      user_data)
426
0
{
427
0
  FreedesktopNotification *n = user_data;
428
0
  GVariant *val;
429
0
  GError *error = NULL;
430
0
  static gboolean warning_printed = FALSE;
431
432
0
  val = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), result, &error);
433
0
  if (val)
434
0
    {
435
0
      GFdoNotificationBackend *backend = n->backend;
436
0
      FreedesktopNotification *match;
437
438
0
      g_variant_get (val, "(u)", &n->notify_id);
439
0
      g_variant_unref (val);
440
441
0
      match = g_fdo_notification_backend_find_notification_by_notify_id (backend, n->notify_id);
442
0
      if (match != NULL)
443
0
        {
444
0
          backend->notifications = g_slist_remove (backend->notifications, match);
445
0
          freedesktop_notification_free (match);
446
0
        }
447
0
      backend->notifications = g_slist_prepend (backend->notifications, n);
448
0
    }
449
0
  else
450
0
    {
451
0
      if (!warning_printed)
452
0
        {
453
0
          g_warning ("unable to send notifications through org.freedesktop.Notifications: %s",
454
0
                     error->message);
455
0
          warning_printed = TRUE;
456
0
        }
457
458
0
      freedesktop_notification_free (n);
459
0
      g_error_free (error);
460
0
    }
461
0
}
462
463
static void
464
g_fdo_notification_backend_dispose (GObject *object)
465
0
{
466
0
  GFdoNotificationBackend *backend = G_FDO_NOTIFICATION_BACKEND (object);
467
468
0
  if (backend->bus_name_id)
469
0
    {
470
0
      g_bus_unwatch_name (backend->bus_name_id);
471
0
      backend->bus_name_id = 0;
472
0
    }
473
474
0
  if (backend->notify_subscription)
475
0
    {
476
0
      GDBusConnection *session_bus;
477
478
0
      session_bus = G_NOTIFICATION_BACKEND (backend)->dbus_connection;
479
0
      g_dbus_connection_signal_unsubscribe (session_bus, g_steal_handle_id (&backend->notify_subscription));
480
0
    }
481
482
0
  if (backend->notifications)
483
0
    {
484
0
      g_slist_free_full (backend->notifications, freedesktop_notification_free);
485
0
      backend->notifications = NULL;
486
0
    }
487
488
0
  G_OBJECT_CLASS (g_fdo_notification_backend_parent_class)->dispose (object);
489
0
}
490
491
static gboolean
492
g_fdo_notification_backend_is_supported (void)
493
0
{
494
  /* This is the fallback backend with the lowest priority. To avoid an
495
   * unnecessary synchronous dbus call to check for
496
   * org.freedesktop.Notifications, this function always succeeds. A
497
   * warning will be printed when sending the first notification fails.
498
   */
499
0
  return TRUE;
500
0
}
501
502
static void
503
g_fdo_notification_backend_send_notification (GNotificationBackend *backend,
504
                                              const gchar          *id,
505
                                              GNotification        *notification)
506
0
{
507
0
  GFdoNotificationBackend *self = G_FDO_NOTIFICATION_BACKEND (backend);
508
0
  FreedesktopNotification *n, *tmp;
509
510
0
  GApplication *application = g_notification_backend_dup_application (backend);
511
0
  g_assert (application != NULL);
512
513
0
  if (self->bus_name_id == 0)
514
0
    {
515
0
      self->bus_name_id = g_bus_watch_name_on_connection (backend->dbus_connection,
516
0
                                                          "org.freedesktop.Notifications",
517
0
                                                          G_BUS_NAME_WATCHER_FLAGS_NONE,
518
0
                                                          NULL,
519
0
                                                          name_vanished_handler_cb,
520
0
                                                          backend,
521
0
                                                          NULL);
522
0
    }
523
524
0
  if (self->notify_subscription == 0)
525
0
    {
526
0
      self->notify_subscription =
527
0
        g_dbus_connection_signal_subscribe (backend->dbus_connection,
528
0
                                            "org.freedesktop.Notifications",
529
0
                                            "org.freedesktop.Notifications", NULL,
530
0
                                            "/org/freedesktop/Notifications", NULL,
531
0
                                            G_DBUS_SIGNAL_FLAGS_NONE,
532
0
                                            notify_signal, backend, NULL);
533
0
    }
534
535
0
  n = freedesktop_notification_new (self, id, notification);
536
537
0
  tmp = g_fdo_notification_backend_find_notification (self, id);
538
0
  if (tmp)
539
0
    n->notify_id = tmp->notify_id;
540
541
0
  call_notify (backend->dbus_connection, application, n->notify_id, notification, notification_sent, n);
542
543
0
  g_object_unref (application);
544
0
}
545
546
static void
547
g_fdo_notification_backend_withdraw_notification (GNotificationBackend *backend,
548
                                                  const gchar          *id)
549
0
{
550
0
  GFdoNotificationBackend *self = G_FDO_NOTIFICATION_BACKEND (backend);
551
0
  FreedesktopNotification *n;
552
553
0
  n = g_fdo_notification_backend_find_notification (self, id);
554
0
  if (n)
555
0
    {
556
0
      if (n->notify_id > 0)
557
0
        {
558
0
          g_dbus_connection_call (backend->dbus_connection,
559
0
                                  "org.freedesktop.Notifications",
560
0
                                  "/org/freedesktop/Notifications",
561
0
                                  "org.freedesktop.Notifications", "CloseNotification",
562
0
                                  g_variant_new ("(u)", n->notify_id), NULL,
563
0
                                  G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
564
0
        }
565
566
0
      self->notifications = g_slist_remove (self->notifications, n);
567
0
      freedesktop_notification_free (n);
568
0
    }
569
0
}
570
571
static void
572
g_fdo_notification_backend_init (GFdoNotificationBackend *backend)
573
0
{
574
0
}
575
576
static void
577
g_fdo_notification_backend_class_init (GFdoNotificationBackendClass *class)
578
0
{
579
0
  GObjectClass *object_class = G_OBJECT_CLASS (class);
580
0
  GNotificationBackendClass *backend_class = G_NOTIFICATION_BACKEND_CLASS (class);
581
582
0
  object_class->dispose = g_fdo_notification_backend_dispose;
583
584
0
  backend_class->is_supported = g_fdo_notification_backend_is_supported;
585
0
  backend_class->send_notification = g_fdo_notification_backend_send_notification;
586
0
  backend_class->withdraw_notification = g_fdo_notification_backend_withdraw_notification;
587
0
}