Coverage Report

Created: 2025-07-01 07:09

/src/glib/gio/gdbusactiongroup.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright © 2010 Codethink Limited
3
 * Copyright © 2011 Canonical Limited
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General
16
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17
 *
18
 * Authors: Ryan Lortie <desrt@desrt.ca>
19
 */
20
21
#include "config.h"
22
23
#include "gdbusactiongroup-private.h"
24
25
#include "gremoteactiongroup.h"
26
#include "gdbusconnection.h"
27
#include "gactiongroup.h"
28
29
/**
30
 * SECTION:gdbusactiongroup
31
 * @title: GDBusActionGroup
32
 * @short_description: A D-Bus GActionGroup implementation
33
 * @include: gio/gio.h
34
 * @see_also: [GActionGroup exporter][gio-GActionGroup-exporter]
35
 *
36
 * #GDBusActionGroup is an implementation of the #GActionGroup
37
 * interface that can be used as a proxy for an action group
38
 * that is exported over D-Bus with g_dbus_connection_export_action_group().
39
 */
40
41
/**
42
 * GDBusActionGroup:
43
 *
44
 * #GDBusActionGroup is an opaque data structure and can only be accessed
45
 * using the following functions.
46
 */
47
48
struct _GDBusActionGroup
49
{
50
  GObject parent_instance;
51
52
  GDBusConnection *connection;
53
  gchar           *bus_name;
54
  gchar           *object_path;
55
  guint            subscription_id;
56
  GHashTable      *actions;
57
58
  /* The 'strict' flag indicates that the non-existence of at least one
59
   * action has potentially been observed through the API.  This means
60
   * that we should always emit 'action-added' signals for all new
61
   * actions.
62
   *
63
   * The user can observe the non-existence of an action by listing the
64
   * actions or by performing a query (such as parameter type) on a
65
   * non-existent action.
66
   *
67
   * If the user has no way of knowing that a given action didn't
68
   * already exist then we can skip emitting 'action-added' signals
69
   * since they have no way of knowing that it wasn't there from the
70
   * start.
71
   */
72
  gboolean         strict;
73
};
74
75
typedef GObjectClass GDBusActionGroupClass;
76
77
typedef struct
78
{
79
  gchar        *name;
80
  GVariantType *parameter_type;
81
  gboolean      enabled;
82
  GVariant     *state;
83
} ActionInfo;
84
85
static void
86
action_info_free (gpointer user_data)
87
0
{
88
0
  ActionInfo *info = user_data;
89
90
0
  g_free (info->name);
91
92
0
  if (info->state)
93
0
    g_variant_unref (info->state);
94
95
0
  if (info->parameter_type)
96
0
    g_variant_type_free (info->parameter_type);
97
98
0
  g_slice_free (ActionInfo, info);
99
0
}
100
101
static ActionInfo *
102
action_info_new_from_iter (GVariantIter *iter)
103
0
{
104
0
  const gchar *param_str;
105
0
  ActionInfo *info;
106
0
  gboolean enabled;
107
0
  GVariant *state;
108
0
  gchar *name;
109
110
0
  if (!g_variant_iter_next (iter, "{s(b&g@av)}", &name,
111
0
                            &enabled, &param_str, &state))
112
0
    return NULL;
113
114
0
  info = g_slice_new (ActionInfo);
115
0
  info->name = name;
116
0
  info->enabled = enabled;
117
118
0
  if (g_variant_n_children (state))
119
0
    g_variant_get_child (state, 0, "v", &info->state);
120
0
  else
121
0
    info->state = NULL;
122
0
  g_variant_unref (state);
123
124
0
  if (param_str[0])
125
0
    info->parameter_type = g_variant_type_copy ((GVariantType *) param_str);
126
0
  else
127
0
    info->parameter_type = NULL;
128
129
0
  return info;
130
0
}
131
132
static void g_dbus_action_group_remote_iface_init (GRemoteActionGroupInterface *iface);
133
static void g_dbus_action_group_iface_init        (GActionGroupInterface       *iface);
134
G_DEFINE_TYPE_WITH_CODE (GDBusActionGroup, g_dbus_action_group, G_TYPE_OBJECT,
135
  G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, g_dbus_action_group_iface_init)
136
  G_IMPLEMENT_INTERFACE (G_TYPE_REMOTE_ACTION_GROUP, g_dbus_action_group_remote_iface_init))
137
138
static void
139
g_dbus_action_group_changed (GDBusConnection *connection,
140
                             const gchar     *sender,
141
                             const gchar     *object_path,
142
                             const gchar     *interface_name,
143
                             const gchar     *signal_name,
144
                             GVariant        *parameters,
145
                             gpointer         user_data)
146
0
{
147
0
  GDBusActionGroup *group = user_data;
148
0
  GActionGroup *g_group = user_data;
149
150
  /* make sure that we've been fully initialised */
151
0
  if (group->actions == NULL)
152
0
    return;
153
154
0
  if (g_str_equal (signal_name, "Changed") &&
155
0
      g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(asa{sb}a{sv}a{s(bgav)})")))
156
0
    {
157
      /* Removes */
158
0
      {
159
0
        GVariantIter *iter;
160
0
        const gchar *name;
161
162
0
        g_variant_get_child (parameters, 0, "as", &iter);
163
0
        while (g_variant_iter_next (iter, "&s", &name))
164
0
          {
165
0
            if (g_hash_table_lookup (group->actions, name))
166
0
              {
167
0
                g_hash_table_remove (group->actions, name);
168
0
                g_action_group_action_removed (g_group, name);
169
0
              }
170
0
          }
171
0
        g_variant_iter_free (iter);
172
0
      }
173
174
      /* Enable changes */
175
0
      {
176
0
        GVariantIter *iter;
177
0
        const gchar *name;
178
0
        gboolean enabled;
179
180
0
        g_variant_get_child (parameters, 1, "a{sb}", &iter);
181
0
        while (g_variant_iter_next (iter, "{&sb}", &name, &enabled))
182
0
          {
183
0
            ActionInfo *info;
184
185
0
            info = g_hash_table_lookup (group->actions, name);
186
187
0
            if (info && info->enabled != enabled)
188
0
              {
189
0
                info->enabled = enabled;
190
0
                g_action_group_action_enabled_changed (g_group, name, enabled);
191
0
              }
192
0
          }
193
0
        g_variant_iter_free (iter);
194
0
      }
195
196
      /* State changes */
197
0
      {
198
0
        GVariantIter *iter;
199
0
        const gchar *name;
200
0
        GVariant *state;
201
202
0
        g_variant_get_child (parameters, 2, "a{sv}", &iter);
203
0
        while (g_variant_iter_next (iter, "{&sv}", &name, &state))
204
0
          {
205
0
            ActionInfo *info;
206
207
0
            info = g_hash_table_lookup (group->actions, name);
208
209
0
            if (info && info->state && !g_variant_equal (state, info->state) &&
210
0
                g_variant_is_of_type (state, g_variant_get_type (info->state)))
211
0
              {
212
0
                g_variant_unref (info->state);
213
0
                info->state = g_variant_ref (state);
214
215
0
                g_action_group_action_state_changed (g_group, name, state);
216
0
              }
217
218
0
            g_variant_unref (state);
219
0
          }
220
0
        g_variant_iter_free (iter);
221
0
      }
222
223
      /* Additions */
224
0
      {
225
0
        GVariantIter *iter;
226
0
        ActionInfo *info;
227
228
0
        g_variant_get_child (parameters, 3, "a{s(bgav)}", &iter);
229
0
        while ((info = action_info_new_from_iter (iter)))
230
0
          {
231
0
            if (!g_hash_table_lookup (group->actions, info->name))
232
0
              {
233
0
                g_hash_table_insert (group->actions, info->name, info);
234
235
0
                if (group->strict)
236
0
                  g_action_group_action_added (g_group, info->name);
237
0
              }
238
0
            else
239
0
              action_info_free (info);
240
0
          }
241
0
        g_variant_iter_free (iter);
242
0
      }
243
0
    }
244
0
}
245
246
247
static void
248
g_dbus_action_group_describe_all_done (GObject      *source,
249
                                       GAsyncResult *result,
250
                                       gpointer      user_data)
251
0
{
252
0
  GDBusActionGroup *group= user_data;
253
0
  GVariant *reply;
254
255
0
  g_assert (group->actions == NULL);
256
0
  group->actions = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, action_info_free);
257
258
0
  g_assert (group->connection == (gpointer) source);
259
0
  reply = g_dbus_connection_call_finish (group->connection, result, NULL);
260
261
0
  if (reply != NULL)
262
0
    {
263
0
      GVariantIter *iter;
264
0
      ActionInfo *action;
265
266
0
      g_variant_get (reply, "(a{s(bgav)})", &iter);
267
0
      while ((action = action_info_new_from_iter (iter)))
268
0
        {
269
0
          g_hash_table_insert (group->actions, action->name, action);
270
271
0
          if (group->strict)
272
0
            g_action_group_action_added (G_ACTION_GROUP (group), action->name);
273
0
        }
274
0
      g_variant_iter_free (iter);
275
0
      g_variant_unref (reply);
276
0
    }
277
278
0
  g_object_unref (group);
279
0
}
280
281
282
static void
283
g_dbus_action_group_async_init (GDBusActionGroup *group)
284
0
{
285
0
  if (group->subscription_id != 0)
286
0
    return;
287
288
0
  group->subscription_id =
289
0
    g_dbus_connection_signal_subscribe (group->connection, group->bus_name, "org.gtk.Actions", "Changed", group->object_path,
290
0
                                        NULL, G_DBUS_SIGNAL_FLAGS_NONE, g_dbus_action_group_changed, group, NULL);
291
292
0
  g_dbus_connection_call (group->connection, group->bus_name, group->object_path, "org.gtk.Actions", "DescribeAll", NULL,
293
0
                          G_VARIANT_TYPE ("(a{s(bgav)})"), G_DBUS_CALL_FLAGS_NONE, -1, NULL,
294
0
                          g_dbus_action_group_describe_all_done, g_object_ref (group));
295
0
}
296
297
static gchar **
298
g_dbus_action_group_list_actions (GActionGroup *g_group)
299
0
{
300
0
  GDBusActionGroup *group = G_DBUS_ACTION_GROUP (g_group);
301
0
  gchar **keys;
302
303
0
  if (group->actions != NULL)
304
0
    {
305
0
      GHashTableIter iter;
306
0
      gint n, i = 0;
307
0
      gpointer key;
308
309
0
      n = g_hash_table_size (group->actions);
310
0
      keys = g_new (gchar *, n + 1);
311
312
0
      g_hash_table_iter_init (&iter, group->actions);
313
0
      while (g_hash_table_iter_next (&iter, &key, NULL))
314
0
        keys[i++] = g_strdup (key);
315
0
      g_assert_cmpint (i, ==, n);
316
0
      keys[n] = NULL;
317
0
    }
318
0
  else
319
0
    {
320
0
      g_dbus_action_group_async_init (group);
321
0
      keys = g_new0 (gchar *, 1);
322
0
    }
323
324
0
  group->strict = TRUE;
325
326
0
  return keys;
327
0
}
328
329
static gboolean
330
g_dbus_action_group_query_action (GActionGroup        *g_group,
331
                                  const gchar         *action_name,
332
                                  gboolean            *enabled,
333
                                  const GVariantType **parameter_type,
334
                                  const GVariantType **state_type,
335
                                  GVariant           **state_hint,
336
                                  GVariant           **state)
337
0
{
338
0
  GDBusActionGroup *group = G_DBUS_ACTION_GROUP (g_group);
339
0
  ActionInfo *info;
340
341
0
  if (group->actions != NULL)
342
0
    {
343
0
      info = g_hash_table_lookup (group->actions, action_name);
344
345
0
      if (info == NULL)
346
0
        {
347
0
          group->strict = TRUE;
348
0
          return FALSE;
349
0
        }
350
351
0
      if (enabled)
352
0
        *enabled = info->enabled;
353
354
0
      if (parameter_type)
355
0
        *parameter_type = info->parameter_type;
356
357
0
      if (state_type)
358
0
        *state_type = info->state ? g_variant_get_type (info->state) : NULL;
359
360
0
      if (state_hint)
361
0
        *state_hint = NULL;
362
363
0
      if (state)
364
0
        *state = info->state ? g_variant_ref (info->state) : NULL;
365
366
0
      return TRUE;
367
0
    }
368
0
  else
369
0
    {
370
0
      g_dbus_action_group_async_init (group);
371
0
      group->strict = TRUE;
372
373
0
      return FALSE;
374
0
    }
375
0
}
376
377
static void
378
g_dbus_action_group_activate_action_full (GRemoteActionGroup *remote,
379
                                          const gchar        *action_name,
380
                                          GVariant           *parameter,
381
                                          GVariant           *platform_data)
382
0
{
383
0
  GDBusActionGroup *group = G_DBUS_ACTION_GROUP (remote);
384
0
  GVariantBuilder builder;
385
386
0
  g_variant_builder_init (&builder, G_VARIANT_TYPE ("av"));
387
388
0
  if (parameter)
389
0
    g_variant_builder_add (&builder, "v", parameter);
390
391
0
  g_dbus_connection_call (group->connection, group->bus_name, group->object_path, "org.gtk.Actions", "Activate",
392
0
                          g_variant_new ("(sav@a{sv})", action_name, &builder, platform_data),
393
0
                          NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
394
0
}
395
396
static void
397
g_dbus_action_group_change_action_state_full (GRemoteActionGroup *remote,
398
                                              const gchar        *action_name,
399
                                              GVariant           *value,
400
                                              GVariant           *platform_data)
401
0
{
402
0
  GDBusActionGroup *group = G_DBUS_ACTION_GROUP (remote);
403
404
0
  g_dbus_connection_call (group->connection, group->bus_name, group->object_path, "org.gtk.Actions", "SetState",
405
0
                          g_variant_new ("(sv@a{sv})", action_name, value, platform_data),
406
0
                          NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
407
0
}
408
409
static void
410
g_dbus_action_group_change_state (GActionGroup *group,
411
                                  const gchar  *action_name,
412
                                  GVariant     *value)
413
0
{
414
0
  g_dbus_action_group_change_action_state_full (G_REMOTE_ACTION_GROUP (group),
415
0
                                                action_name, value, g_variant_new ("a{sv}", NULL));
416
0
}
417
418
static void
419
g_dbus_action_group_activate (GActionGroup *group,
420
                              const gchar  *action_name,
421
                              GVariant     *parameter)
422
0
{
423
0
  g_dbus_action_group_activate_action_full (G_REMOTE_ACTION_GROUP (group),
424
0
                                            action_name, parameter, g_variant_new ("a{sv}", NULL));
425
0
}
426
427
static void
428
g_dbus_action_group_finalize (GObject *object)
429
0
{
430
0
  GDBusActionGroup *group = G_DBUS_ACTION_GROUP (object);
431
432
0
  if (group->subscription_id)
433
0
    g_dbus_connection_signal_unsubscribe (group->connection, group->subscription_id);
434
435
0
  if (group->actions)
436
0
    g_hash_table_unref (group->actions);
437
438
0
  g_object_unref (group->connection);
439
0
  g_free (group->object_path);
440
0
  g_free (group->bus_name);
441
442
0
  G_OBJECT_CLASS (g_dbus_action_group_parent_class)
443
0
    ->finalize (object);
444
0
}
445
446
static void
447
g_dbus_action_group_init (GDBusActionGroup *group)
448
0
{
449
0
}
450
451
static void
452
g_dbus_action_group_class_init (GDBusActionGroupClass *class)
453
0
{
454
0
  GObjectClass *object_class = G_OBJECT_CLASS (class);
455
456
0
  object_class->finalize = g_dbus_action_group_finalize;
457
0
}
458
459
static void
460
g_dbus_action_group_remote_iface_init (GRemoteActionGroupInterface *iface)
461
0
{
462
0
  iface->activate_action_full = g_dbus_action_group_activate_action_full;
463
0
  iface->change_action_state_full = g_dbus_action_group_change_action_state_full;
464
0
}
465
466
static void
467
g_dbus_action_group_iface_init (GActionGroupInterface *iface)
468
0
{
469
0
  iface->list_actions = g_dbus_action_group_list_actions;
470
0
  iface->query_action = g_dbus_action_group_query_action;
471
0
  iface->change_action_state = g_dbus_action_group_change_state;
472
0
  iface->activate_action = g_dbus_action_group_activate;
473
0
}
474
475
/**
476
 * g_dbus_action_group_get:
477
 * @connection: A #GDBusConnection
478
 * @bus_name: (nullable): the bus name which exports the action
479
 *     group or %NULL if @connection is not a message bus connection
480
 * @object_path: the object path at which the action group is exported
481
 *
482
 * Obtains a #GDBusActionGroup for the action group which is exported at
483
 * the given @bus_name and @object_path.
484
 *
485
 * The thread default main context is taken at the time of this call.
486
 * All signals on the menu model (and any linked models) are reported
487
 * with respect to this context.  All calls on the returned menu model
488
 * (and linked models) must also originate from this same context, with
489
 * the thread default main context unchanged.
490
 *
491
 * This call is non-blocking.  The returned action group may or may not
492
 * already be filled in.  The correct thing to do is connect the signals
493
 * for the action group to monitor for changes and then to call
494
 * g_action_group_list_actions() to get the initial list.
495
 *
496
 * Returns: (transfer full): a #GDBusActionGroup
497
 *
498
 * Since: 2.32
499
 */
500
GDBusActionGroup *
501
g_dbus_action_group_get (GDBusConnection *connection,
502
                         const gchar     *bus_name,
503
                         const gchar     *object_path)
504
0
{
505
0
  GDBusActionGroup *group;
506
507
0
  g_return_val_if_fail (bus_name != NULL || g_dbus_connection_get_unique_name (connection) == NULL, NULL);
508
509
0
  group = g_object_new (G_TYPE_DBUS_ACTION_GROUP, NULL);
510
0
  group->connection = g_object_ref (connection);
511
0
  group->bus_name = g_strdup (bus_name);
512
0
  group->object_path = g_strdup (object_path);
513
514
0
  return group;
515
0
}
516
517
gboolean
518
g_dbus_action_group_sync (GDBusActionGroup  *group,
519
                          GCancellable      *cancellable,
520
                          GError           **error)
521
0
{
522
0
  GVariant *reply;
523
524
0
  g_assert (group->subscription_id == 0);
525
526
0
  group->subscription_id =
527
0
    g_dbus_connection_signal_subscribe (group->connection, group->bus_name, "org.gtk.Actions", "Changed", group->object_path,
528
0
                                        NULL, G_DBUS_SIGNAL_FLAGS_NONE, g_dbus_action_group_changed, group, NULL);
529
530
0
  reply = g_dbus_connection_call_sync (group->connection, group->bus_name, group->object_path, "org.gtk.Actions",
531
0
                                       "DescribeAll", NULL, G_VARIANT_TYPE ("(a{s(bgav)})"),
532
0
                                       G_DBUS_CALL_FLAGS_NONE, -1, cancellable, error);
533
534
0
  if (reply != NULL)
535
0
    {
536
0
      GVariantIter *iter;
537
0
      ActionInfo *action;
538
539
0
      g_assert (group->actions == NULL);
540
0
      group->actions = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, action_info_free);
541
542
0
      g_variant_get (reply, "(a{s(bgav)})", &iter);
543
0
      while ((action = action_info_new_from_iter (iter)))
544
0
        g_hash_table_insert (group->actions, action->name, action);
545
0
      g_variant_iter_free (iter);
546
0
      g_variant_unref (reply);
547
0
    }
548
549
0
  return reply != NULL;
550
0
}