Coverage Report

Created: 2025-06-13 06:55

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