Coverage Report

Created: 2025-06-13 06:55

/src/glib/gio/gsimpleaction.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright © 2010 Codethink Limited
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: Ryan Lortie <desrt@desrt.ca>
20
 */
21
22
#include "config.h"
23
24
#include "gsimpleaction.h"
25
26
#include "gaction.h"
27
#include "glibintl.h"
28
29
/**
30
 * SECTION:gsimpleaction
31
 * @title: GSimpleAction
32
 * @short_description: A simple GAction implementation
33
 * @include: gio/gio.h
34
 *
35
 * A #GSimpleAction is the obvious simple implementation of the #GAction
36
 * interface. This is the easiest way to create an action for purposes of
37
 * adding it to a #GSimpleActionGroup.
38
 *
39
 * See also #GtkAction.
40
 */
41
42
/**
43
 * GSimpleAction:
44
 *
45
 * #GSimpleAction is an opaque data structure and can only be accessed
46
 * using the following functions.
47
 **/
48
49
struct _GSimpleAction
50
{
51
  GObject       parent_instance;
52
53
  gchar        *name;
54
  GVariantType *parameter_type;
55
  gboolean      enabled;
56
  GVariant     *state;
57
  GVariant     *state_hint;
58
  gboolean      state_set_already;
59
};
60
61
typedef GObjectClass GSimpleActionClass;
62
63
static void g_simple_action_iface_init (GActionInterface *iface);
64
G_DEFINE_TYPE_WITH_CODE (GSimpleAction, g_simple_action, G_TYPE_OBJECT,
65
  G_IMPLEMENT_INTERFACE (G_TYPE_ACTION, g_simple_action_iface_init))
66
67
enum
68
{
69
  PROP_NONE,
70
  PROP_NAME,
71
  PROP_PARAMETER_TYPE,
72
  PROP_ENABLED,
73
  PROP_STATE_TYPE,
74
  PROP_STATE
75
};
76
77
enum
78
{
79
  SIGNAL_CHANGE_STATE,
80
  SIGNAL_ACTIVATE,
81
  NR_SIGNALS
82
};
83
84
static guint g_simple_action_signals[NR_SIGNALS];
85
86
static const gchar *
87
g_simple_action_get_name (GAction *action)
88
0
{
89
0
  GSimpleAction *simple = G_SIMPLE_ACTION (action);
90
91
0
  return simple->name;
92
0
}
93
94
static const GVariantType *
95
g_simple_action_get_parameter_type (GAction *action)
96
0
{
97
0
  GSimpleAction *simple = G_SIMPLE_ACTION (action);
98
99
0
  return simple->parameter_type;
100
0
}
101
102
static const GVariantType *
103
g_simple_action_get_state_type (GAction *action)
104
0
{
105
0
  GSimpleAction *simple = G_SIMPLE_ACTION (action);
106
107
0
  if (simple->state != NULL)
108
0
    return g_variant_get_type (simple->state);
109
0
  else
110
0
    return NULL;
111
0
}
112
113
static GVariant *
114
g_simple_action_get_state_hint (GAction *action)
115
0
{
116
0
  GSimpleAction *simple = G_SIMPLE_ACTION (action);
117
118
0
  if (simple->state_hint != NULL)
119
0
    return g_variant_ref (simple->state_hint);
120
0
  else
121
0
    return NULL;
122
0
}
123
124
static gboolean
125
g_simple_action_get_enabled (GAction *action)
126
0
{
127
0
  GSimpleAction *simple = G_SIMPLE_ACTION (action);
128
129
0
  return simple->enabled;
130
0
}
131
132
static void
133
g_simple_action_change_state (GAction  *action,
134
                              GVariant *value)
135
0
{
136
0
  GSimpleAction *simple = G_SIMPLE_ACTION (action);
137
138
  /* If the user connected a signal handler then they are responsible
139
   * for handling state changes.
140
   */
141
0
  if (g_signal_has_handler_pending (action, g_simple_action_signals[SIGNAL_CHANGE_STATE], 0, TRUE))
142
0
    g_signal_emit (action, g_simple_action_signals[SIGNAL_CHANGE_STATE], 0, value);
143
144
  /* If not, then the default behaviour is to just set the state. */
145
0
  else
146
0
    g_simple_action_set_state (simple, value);
147
0
}
148
149
/**
150
 * g_simple_action_set_state:
151
 * @simple: a #GSimpleAction
152
 * @value: the new #GVariant for the state
153
 *
154
 * Sets the state of the action.
155
 *
156
 * This directly updates the 'state' property to the given value.
157
 *
158
 * This should only be called by the implementor of the action.  Users
159
 * of the action should not attempt to directly modify the 'state'
160
 * property.  Instead, they should call g_action_change_state() to
161
 * request the change.
162
 *
163
 * If the @value GVariant is floating, it is consumed.
164
 *
165
 * Since: 2.30
166
 **/
167
void
168
g_simple_action_set_state (GSimpleAction *simple,
169
                           GVariant      *value)
170
0
{
171
0
  g_return_if_fail (G_IS_SIMPLE_ACTION (simple));
172
0
  g_return_if_fail (value != NULL);
173
174
0
  {
175
0
    const GVariantType *state_type;
176
177
0
    state_type = simple->state ?
178
0
                   g_variant_get_type (simple->state) : NULL;
179
0
    g_return_if_fail (state_type != NULL);
180
0
    g_return_if_fail (g_variant_is_of_type (value, state_type));
181
0
  }
182
183
0
  g_variant_ref_sink (value);
184
185
0
  if (!simple->state || !g_variant_equal (simple->state, value))
186
0
    {
187
0
      if (simple->state)
188
0
        g_variant_unref (simple->state);
189
190
0
      simple->state = g_variant_ref (value);
191
192
0
      g_object_notify (G_OBJECT (simple), "state");
193
0
    }
194
195
0
  g_variant_unref (value);
196
0
}
197
198
static GVariant *
199
g_simple_action_get_state (GAction *action)
200
0
{
201
0
  GSimpleAction *simple = G_SIMPLE_ACTION (action);
202
203
0
  return simple->state ? g_variant_ref (simple->state) : NULL;
204
0
}
205
206
static void
207
g_simple_action_activate (GAction  *action,
208
                          GVariant *parameter)
209
0
{
210
0
  GSimpleAction *simple = G_SIMPLE_ACTION (action);
211
212
0
  g_return_if_fail (simple->parameter_type == NULL ?
213
0
                      parameter == NULL :
214
0
                    (parameter != NULL &&
215
0
                     g_variant_is_of_type (parameter,
216
0
                                           simple->parameter_type)));
217
218
0
  if (parameter != NULL)
219
0
    g_variant_ref_sink (parameter);
220
221
0
  if (simple->enabled)
222
0
    {
223
      /* If the user connected a signal handler then they are responsible
224
       * for handling activation.
225
       */
226
0
      if (g_signal_has_handler_pending (action, g_simple_action_signals[SIGNAL_ACTIVATE], 0, TRUE))
227
0
        g_signal_emit (action, g_simple_action_signals[SIGNAL_ACTIVATE], 0, parameter);
228
229
      /* If not, do some reasonable defaults for stateful actions. */
230
0
      else if (simple->state)
231
0
        {
232
          /* If we have no parameter and this is a boolean action, toggle. */
233
0
          if (parameter == NULL && g_variant_is_of_type (simple->state, G_VARIANT_TYPE_BOOLEAN))
234
0
            {
235
0
              gboolean was_enabled = g_variant_get_boolean (simple->state);
236
0
              g_simple_action_change_state (action, g_variant_new_boolean (!was_enabled));
237
0
            }
238
239
          /* else, if the parameter and state type are the same, do a change-state */
240
0
          else if (g_variant_is_of_type (simple->state, g_variant_get_type (parameter)))
241
0
            g_simple_action_change_state (action, parameter);
242
0
        }
243
0
    }
244
245
0
  if (parameter != NULL)
246
0
    g_variant_unref (parameter);
247
0
}
248
249
static void
250
g_simple_action_set_property (GObject    *object,
251
                              guint       prop_id,
252
                              const GValue     *value,
253
                              GParamSpec *pspec)
254
0
{
255
0
  GSimpleAction *action = G_SIMPLE_ACTION (object);
256
257
0
  switch (prop_id)
258
0
    {
259
0
    case PROP_NAME:
260
0
      action->name = g_strdup (g_value_get_string (value));
261
0
      break;
262
263
0
    case PROP_PARAMETER_TYPE:
264
0
      action->parameter_type = g_value_dup_boxed (value);
265
0
      break;
266
267
0
    case PROP_ENABLED:
268
0
      action->enabled = g_value_get_boolean (value);
269
0
      break;
270
271
0
    case PROP_STATE:
272
      /* The first time we see this (during construct) we should just
273
       * take the state as it was handed to us.
274
       *
275
       * After that, we should make sure we go through the same checks
276
       * as the C API.
277
       */
278
0
      if (!action->state_set_already)
279
0
        {
280
0
          action->state = g_value_dup_variant (value);
281
0
          action->state_set_already = TRUE;
282
0
        }
283
0
      else
284
0
        g_simple_action_set_state (action, g_value_get_variant (value));
285
286
0
      break;
287
288
0
    default:
289
0
      g_assert_not_reached ();
290
0
    }
291
0
}
292
293
static void
294
g_simple_action_get_property (GObject    *object,
295
                              guint       prop_id,
296
                              GValue     *value,
297
                              GParamSpec *pspec)
298
0
{
299
0
  GAction *action = G_ACTION (object);
300
301
0
  switch (prop_id)
302
0
    {
303
0
    case PROP_NAME:
304
0
      g_value_set_string (value, g_simple_action_get_name (action));
305
0
      break;
306
307
0
    case PROP_PARAMETER_TYPE:
308
0
      g_value_set_boxed (value, g_simple_action_get_parameter_type (action));
309
0
      break;
310
311
0
    case PROP_ENABLED:
312
0
      g_value_set_boolean (value, g_simple_action_get_enabled (action));
313
0
      break;
314
315
0
    case PROP_STATE_TYPE:
316
0
      g_value_set_boxed (value, g_simple_action_get_state_type (action));
317
0
      break;
318
319
0
    case PROP_STATE:
320
0
      g_value_take_variant (value, g_simple_action_get_state (action));
321
0
      break;
322
323
0
    default:
324
0
      g_assert_not_reached ();
325
0
    }
326
0
}
327
328
static void
329
g_simple_action_finalize (GObject *object)
330
0
{
331
0
  GSimpleAction *simple = G_SIMPLE_ACTION (object);
332
333
0
  g_free (simple->name);
334
0
  if (simple->parameter_type)
335
0
    g_variant_type_free (simple->parameter_type);
336
0
  if (simple->state)
337
0
    g_variant_unref (simple->state);
338
0
  if (simple->state_hint)
339
0
    g_variant_unref (simple->state_hint);
340
341
0
  G_OBJECT_CLASS (g_simple_action_parent_class)
342
0
    ->finalize (object);
343
0
}
344
345
void
346
g_simple_action_init (GSimpleAction *simple)
347
0
{
348
0
  simple->enabled = TRUE;
349
0
}
350
351
void
352
g_simple_action_iface_init (GActionInterface *iface)
353
0
{
354
0
  iface->get_name = g_simple_action_get_name;
355
0
  iface->get_parameter_type = g_simple_action_get_parameter_type;
356
0
  iface->get_state_type = g_simple_action_get_state_type;
357
0
  iface->get_state_hint = g_simple_action_get_state_hint;
358
0
  iface->get_enabled = g_simple_action_get_enabled;
359
0
  iface->get_state = g_simple_action_get_state;
360
0
  iface->change_state = g_simple_action_change_state;
361
0
  iface->activate = g_simple_action_activate;
362
0
}
363
364
void
365
g_simple_action_class_init (GSimpleActionClass *class)
366
0
{
367
0
  GObjectClass *object_class = G_OBJECT_CLASS (class);
368
369
0
  object_class->set_property = g_simple_action_set_property;
370
0
  object_class->get_property = g_simple_action_get_property;
371
0
  object_class->finalize = g_simple_action_finalize;
372
373
  /**
374
   * GSimpleAction::activate:
375
   * @simple: the #GSimpleAction
376
   * @parameter: (nullable): the parameter to the activation, or %NULL if it has
377
   *   no parameter
378
   *
379
   * Indicates that the action was just activated.
380
   *
381
   * @parameter will always be of the expected type, i.e. the parameter type
382
   * specified when the action was created. If an incorrect type is given when
383
   * activating the action, this signal is not emitted.
384
   *
385
   * Since GLib 2.40, if no handler is connected to this signal then the
386
   * default behaviour for boolean-stated actions with a %NULL parameter
387
   * type is to toggle them via the #GSimpleAction::change-state signal.
388
   * For stateful actions where the state type is equal to the parameter
389
   * type, the default is to forward them directly to
390
   * #GSimpleAction::change-state.  This should allow almost all users
391
   * of #GSimpleAction to connect only one handler or the other.
392
   *
393
   * Since: 2.28
394
   */
395
0
  g_simple_action_signals[SIGNAL_ACTIVATE] =
396
0
    g_signal_new (I_("activate"),
397
0
                  G_TYPE_SIMPLE_ACTION,
398
0
                  G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
399
0
                  0, NULL, NULL,
400
0
                  NULL,
401
0
                  G_TYPE_NONE, 1,
402
0
                  G_TYPE_VARIANT);
403
404
  /**
405
   * GSimpleAction::change-state:
406
   * @simple: the #GSimpleAction
407
   * @value: (nullable): the requested value for the state
408
   *
409
   * Indicates that the action just received a request to change its
410
   * state.
411
   *
412
   * @value will always be of the correct state type, i.e. the type of the
413
   * initial state passed to g_simple_action_new_stateful(). If an incorrect
414
   * type is given when requesting to change the state, this signal is not
415
   * emitted.
416
   *
417
   * If no handler is connected to this signal then the default
418
   * behaviour is to call g_simple_action_set_state() to set the state
419
   * to the requested value. If you connect a signal handler then no
420
   * default action is taken. If the state should change then you must
421
   * call g_simple_action_set_state() from the handler.
422
   *
423
   * An example of a 'change-state' handler:
424
   * |[<!-- language="C" -->
425
   * static void
426
   * change_volume_state (GSimpleAction *action,
427
   *                      GVariant      *value,
428
   *                      gpointer       user_data)
429
   * {
430
   *   gint requested;
431
   *
432
   *   requested = g_variant_get_int32 (value);
433
   *
434
   *   // Volume only goes from 0 to 10
435
   *   if (0 <= requested && requested <= 10)
436
   *     g_simple_action_set_state (action, value);
437
   * }
438
   * ]|
439
   *
440
   * The handler need not set the state to the requested value.
441
   * It could set it to any value at all, or take some other action.
442
   *
443
   * Since: 2.30
444
   */
445
0
  g_simple_action_signals[SIGNAL_CHANGE_STATE] =
446
0
    g_signal_new (I_("change-state"),
447
0
                  G_TYPE_SIMPLE_ACTION,
448
0
                  G_SIGNAL_RUN_LAST | G_SIGNAL_MUST_COLLECT,
449
0
                  0, NULL, NULL,
450
0
                  NULL,
451
0
                  G_TYPE_NONE, 1,
452
0
                  G_TYPE_VARIANT);
453
454
  /**
455
   * GSimpleAction:name:
456
   *
457
   * The name of the action. This is mostly meaningful for identifying
458
   * the action once it has been added to a #GSimpleActionGroup.
459
   *
460
   * Since: 2.28
461
   **/
462
0
  g_object_class_install_property (object_class, PROP_NAME,
463
0
                                   g_param_spec_string ("name",
464
0
                                                        P_("Action Name"),
465
0
                                                        P_("The name used to invoke the action"),
466
0
                                                        NULL,
467
0
                                                        G_PARAM_READWRITE |
468
0
                                                        G_PARAM_CONSTRUCT_ONLY |
469
0
                                                        G_PARAM_STATIC_STRINGS));
470
471
  /**
472
   * GSimpleAction:parameter-type:
473
   *
474
   * The type of the parameter that must be given when activating the
475
   * action.
476
   *
477
   * Since: 2.28
478
   **/
479
0
  g_object_class_install_property (object_class, PROP_PARAMETER_TYPE,
480
0
                                   g_param_spec_boxed ("parameter-type",
481
0
                                                       P_("Parameter Type"),
482
0
                                                       P_("The type of GVariant passed to activate()"),
483
0
                                                       G_TYPE_VARIANT_TYPE,
484
0
                                                       G_PARAM_READWRITE |
485
0
                                                       G_PARAM_CONSTRUCT_ONLY |
486
0
                                                       G_PARAM_STATIC_STRINGS));
487
488
  /**
489
   * GSimpleAction:enabled:
490
   *
491
   * If @action is currently enabled.
492
   *
493
   * If the action is disabled then calls to g_action_activate() and
494
   * g_action_change_state() have no effect.
495
   *
496
   * Since: 2.28
497
   **/
498
0
  g_object_class_install_property (object_class, PROP_ENABLED,
499
0
                                   g_param_spec_boolean ("enabled",
500
0
                                                         P_("Enabled"),
501
0
                                                         P_("If the action can be activated"),
502
0
                                                         TRUE,
503
0
                                                         G_PARAM_READWRITE |
504
0
                                                         G_PARAM_STATIC_STRINGS));
505
506
  /**
507
   * GSimpleAction:state-type:
508
   *
509
   * The #GVariantType of the state that the action has, or %NULL if the
510
   * action is stateless.
511
   *
512
   * Since: 2.28
513
   **/
514
0
  g_object_class_install_property (object_class, PROP_STATE_TYPE,
515
0
                                   g_param_spec_boxed ("state-type",
516
0
                                                       P_("State Type"),
517
0
                                                       P_("The type of the state kept by the action"),
518
0
                                                       G_TYPE_VARIANT_TYPE,
519
0
                                                       G_PARAM_READABLE |
520
0
                                                       G_PARAM_STATIC_STRINGS));
521
522
  /**
523
   * GSimpleAction:state:
524
   *
525
   * The state of the action, or %NULL if the action is stateless.
526
   *
527
   * Since: 2.28
528
   **/
529
0
  g_object_class_install_property (object_class, PROP_STATE,
530
0
                                   g_param_spec_variant ("state",
531
0
                                                         P_("State"),
532
0
                                                         P_("The state the action is in"),
533
0
                                                         G_VARIANT_TYPE_ANY,
534
0
                                                         NULL,
535
0
                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
536
0
                                                         G_PARAM_STATIC_STRINGS));
537
0
}
538
539
/**
540
 * g_simple_action_set_enabled:
541
 * @simple: a #GSimpleAction
542
 * @enabled: whether the action is enabled
543
 *
544
 * Sets the action as enabled or not.
545
 *
546
 * An action must be enabled in order to be activated or in order to
547
 * have its state changed from outside callers.
548
 *
549
 * This should only be called by the implementor of the action.  Users
550
 * of the action should not attempt to modify its enabled flag.
551
 *
552
 * Since: 2.28
553
 **/
554
void
555
g_simple_action_set_enabled (GSimpleAction *simple,
556
                             gboolean       enabled)
557
0
{
558
0
  g_return_if_fail (G_IS_SIMPLE_ACTION (simple));
559
560
0
  enabled = !!enabled;
561
562
0
  if (simple->enabled != enabled)
563
0
    {
564
0
      simple->enabled = enabled;
565
0
      g_object_notify (G_OBJECT (simple), "enabled");
566
0
    }
567
0
}
568
569
/**
570
 * g_simple_action_set_state_hint:
571
 * @simple: a #GSimpleAction
572
 * @state_hint: (nullable): a #GVariant representing the state hint
573
 *
574
 * Sets the state hint for the action.
575
 *
576
 * See g_action_get_state_hint() for more information about
577
 * action state hints.
578
 *
579
 * Since: 2.44
580
 **/
581
void
582
g_simple_action_set_state_hint (GSimpleAction *simple,
583
                                GVariant      *state_hint)
584
0
{
585
0
  g_return_if_fail (G_IS_SIMPLE_ACTION (simple));
586
587
0
  if (simple->state_hint != NULL)
588
0
    {
589
0
      g_variant_unref (simple->state_hint);
590
0
      simple->state_hint = NULL;
591
0
    }
592
593
0
  if (state_hint != NULL)
594
0
    simple->state_hint = g_variant_ref (state_hint);
595
0
}
596
597
/**
598
 * g_simple_action_new:
599
 * @name: the name of the action
600
 * @parameter_type: (nullable): the type of parameter that will be passed to
601
 *   handlers for the #GSimpleAction::activate signal, or %NULL for no parameter
602
 *
603
 * Creates a new action.
604
 *
605
 * The created action is stateless. See g_simple_action_new_stateful() to create
606
 * an action that has state.
607
 *
608
 * Returns: a new #GSimpleAction
609
 *
610
 * Since: 2.28
611
 **/
612
GSimpleAction *
613
g_simple_action_new (const gchar        *name,
614
                     const GVariantType *parameter_type)
615
0
{
616
0
  g_return_val_if_fail (name != NULL, NULL);
617
618
0
  return g_object_new (G_TYPE_SIMPLE_ACTION,
619
0
                       "name", name,
620
0
                       "parameter-type", parameter_type,
621
0
                       NULL);
622
0
}
623
624
/**
625
 * g_simple_action_new_stateful:
626
 * @name: the name of the action
627
 * @parameter_type: (nullable): the type of the parameter that will be passed to
628
 *   handlers for the #GSimpleAction::activate signal, or %NULL for no parameter
629
 * @state: the initial state of the action
630
 *
631
 * Creates a new stateful action.
632
 *
633
 * All future state values must have the same #GVariantType as the initial
634
 * @state.
635
 *
636
 * If the @state #GVariant is floating, it is consumed.
637
 *
638
 * Returns: a new #GSimpleAction
639
 *
640
 * Since: 2.28
641
 **/
642
GSimpleAction *
643
g_simple_action_new_stateful (const gchar        *name,
644
                              const GVariantType *parameter_type,
645
                              GVariant           *state)
646
0
{
647
0
  return g_object_new (G_TYPE_SIMPLE_ACTION,
648
0
                       "name", name,
649
0
                       "parameter-type", parameter_type,
650
0
                       "state", state,
651
0
                       NULL);
652
0
}