summaryrefslogtreecommitdiff
path: root/gtk/a11y/gtkatspiaction.c
blob: 2c28c0fbbd2cf4f1b833aa1912739f33fad12182 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
/* gtkatspiaction.c: ATSPI Action implementation
 *
 * Copyright 2020  GNOME Foundation
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "gtkatspiactionprivate.h"

#include "gtkatspicontextprivate.h"
#include "gtkatcontextprivate.h"

#include "a11y/atspi/atspi-action.h"

#include "gtkactionable.h"
#include "gtkactionmuxerprivate.h"
#include "gtkbutton.h"
#include "gtkentryprivate.h"
#include "gtkexpander.h"
#include "gtkpasswordentryprivate.h"
#include "gtkswitch.h"
#include "gtkwidgetprivate.h"

#include <glib/gi18n-lib.h>

typedef struct _Action  Action;

struct _Action
{
  const char *name;
  const char *localized_name;
  const char *description;
  const char *keybinding;

  gboolean (* is_enabled) (GtkAtSpiContext *context);
  gboolean (* activate) (GtkAtSpiContext *context);
};

static void
action_handle_method (GtkAtSpiContext        *self,
                      const char             *method_name,
                      GVariant               *parameters,
                      GDBusMethodInvocation  *invocation,
                      const Action           *actions,
                      int                     n_actions)
{
  if (g_strcmp0 (method_name, "GetName") == 0)
    {
      int idx = -1;

      g_variant_get (parameters, "(i)", &idx);

      const Action *action = &actions[idx];

      if (idx >= 0 && idx < n_actions)
        g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", action->name));
      else
        g_dbus_method_invocation_return_error (invocation,
                                               G_IO_ERROR,
                                               G_IO_ERROR_INVALID_ARGUMENT,
                                               "Unknown action %d",
                                               idx);
    }
  else if (g_strcmp0 (method_name, "GetLocalizedName") == 0)
    {
      int idx = -1;

      g_variant_get (parameters, "(i)", &idx);

      if (idx >= 0 && idx < n_actions)
        {
          const Action *action = &actions[idx];
          const char *s = g_dpgettext2 (GETTEXT_PACKAGE, "accessibility", action->localized_name);

          g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", s));
        }
      else
        {
          g_dbus_method_invocation_return_error (invocation,
                                                 G_IO_ERROR,
                                                 G_IO_ERROR_INVALID_ARGUMENT,
                                                 "Unknown action %d",
                                                 idx);
        }
    }
  else if (g_strcmp0 (method_name, "GetDescription") == 0)
    {
      int idx = -1;

      g_variant_get (parameters, "(i)", &idx);

      if (idx >= 0 && idx < n_actions)
        {
          const Action *action = &actions[idx];
          const char *s = g_dpgettext2 (GETTEXT_PACKAGE, "accessibility", action->description);

          g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", s));
        }
      else
        {
          g_dbus_method_invocation_return_error (invocation,
                                                 G_IO_ERROR,
                                                 G_IO_ERROR_INVALID_ARGUMENT,
                                                 "Unknown action %d",
                                                 idx);
        }
    }
  else if (g_strcmp0 (method_name, "GetKeyBinding") == 0)
    {
      int idx = -1;

      g_variant_get (parameters, "(i)", &idx);

      const Action *action = &actions[idx];

      if (idx >= 0 && idx < n_actions)
        g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", action->keybinding));
      else
        g_dbus_method_invocation_return_error (invocation,
                                               G_IO_ERROR,
                                               G_IO_ERROR_INVALID_ARGUMENT,
                                               "Unknown action %d",
                                               idx);
    }
  else if (g_strcmp0 (method_name, "GetActions") == 0)
    {
      GVariantBuilder builder = G_VARIANT_BUILDER_INIT (G_VARIANT_TYPE ("a(sss)"));

      for (int i = 0; i < n_actions; i++)
        {
          const Action *action = &actions[i];

          if (action->is_enabled != NULL && !action->is_enabled (self))
            continue;

          g_variant_builder_add (&builder, "(sss)",
                                 g_dpgettext2 (GETTEXT_PACKAGE, "accessibility", action->localized_name),
                                 g_dpgettext2 (GETTEXT_PACKAGE, "accessibility", action->description),
                                 action->keybinding);
        }

      g_dbus_method_invocation_return_value (invocation, g_variant_new ("(a(sss))", &builder));
    }
  else if (g_strcmp0 (method_name, "DoAction") == 0)
    {
      GtkAccessible *accessible = gtk_at_context_get_accessible (GTK_AT_CONTEXT (self));
      GtkWidget *widget = GTK_WIDGET (accessible);
      int idx = -1;

      if (!gtk_widget_is_sensitive (widget) || !gtk_widget_get_visible (widget))
        {
          g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", FALSE));
          return;
        }

      g_variant_get (parameters, "(i)", &idx);

      if (idx >= 0 && idx < n_actions)
        {
          const Action *action = &actions[idx];

          if (action->is_enabled == NULL || action->is_enabled (self))
            {
              gboolean res = TRUE;

              if (action->activate == NULL)
                {
                  gtk_widget_activate (widget);
                }
              else
                {
                  res = action->activate (self);
                }

              g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", res));
            }
          else
            {
              g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", FALSE));
            }
        }
      else
        {
          g_dbus_method_invocation_return_error (invocation,
                                                 G_IO_ERROR,
                                                 G_IO_ERROR_INVALID_ARGUMENT,
                                                 "Unknown action %d",
                                                 idx);
        }
    }
}

static GVariant *
action_handle_get_property (GtkAtSpiContext  *self,
                            const char       *property_name,
                            GError          **error,
                            const Action     *actions,
                            int               n_actions)
{
  if (g_strcmp0 (property_name, "NActions") == 0)
    {
      int n_valid_actions = 0;

      for (int i = 0; i < n_actions; i++)
        {
          const Action *action = &actions[i];

          if (action->is_enabled == NULL || action->is_enabled (self))
            n_valid_actions += 1;
        }

      return g_variant_new_int32 (n_valid_actions);
    }

  g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
               "Unknown property '%s'", property_name);

  return NULL;
}

/* {{{ GtkButton */
static Action button_actions[] = {
  {
    .name = "click",
    .localized_name = NC_("accessibility", "Click"),
    .description = NC_("accessibility", "Clicks the button"),
    .keybinding = "<Space>",
  },
};

static void
button_handle_method (GDBusConnection       *connection,
                      const gchar           *sender,
                      const gchar           *object_path,
                      const gchar           *interface_name,
                      const gchar           *method_name,
                      GVariant              *parameters,
                      GDBusMethodInvocation *invocation,
                      gpointer               user_data)
{
  GtkAtSpiContext *self = user_data;

  action_handle_method (self, method_name, parameters, invocation,
                        button_actions,
                        G_N_ELEMENTS (button_actions));
}

static GVariant *
button_handle_get_property (GDBusConnection  *connection,
                            const gchar      *sender,
                            const gchar      *object_path,
                            const gchar      *interface_name,
                            const gchar      *property_name,
                            GError          **error,
                            gpointer          user_data)
{
  GtkAtSpiContext *self = user_data;

  return action_handle_get_property (self, property_name, error,
                                     button_actions,
                                     G_N_ELEMENTS (button_actions));
}

static const GDBusInterfaceVTable button_action_vtable = {
  button_handle_method,
  button_handle_get_property,
  NULL,
};

/* }}} */

/* {{{ GtkSwitch */

static const Action switch_actions[] = {
  {
    .name = "toggle",
    .localized_name = NC_("accessibility", "Toggle"),
    .description = NC_("accessibility", "Toggles the switch"),
    .keybinding = "<Space>",
  },
};

static void
switch_handle_method (GDBusConnection       *connection,
                      const gchar           *sender,
                      const gchar           *object_path,
                      const gchar           *interface_name,
                      const gchar           *method_name,
                      GVariant              *parameters,
                      GDBusMethodInvocation *invocation,
                      gpointer               user_data)
{
  GtkAtSpiContext *self = user_data;

  action_handle_method (self, method_name, parameters, invocation,
                        switch_actions,
                        G_N_ELEMENTS (switch_actions));
}

static GVariant *
switch_handle_get_property (GDBusConnection  *connection,
                            const gchar      *sender,
                            const gchar      *object_path,
                            const gchar      *interface_name,
                            const gchar      *property_name,
                            GError          **error,
                            gpointer          user_data)
{
  GtkAtSpiContext *self = user_data;

  return action_handle_get_property (self, property_name, error,
                                     switch_actions,
                                     G_N_ELEMENTS (switch_actions));
}

static const GDBusInterfaceVTable switch_action_vtable = {
  switch_handle_method,
  switch_handle_get_property,
  NULL,
};

/* }}} */

/* {{{ GtkExpander */

static const Action expander_actions[] = {
  {
    .name = "activate",
    .localized_name = NC_("accessibility", "Activate"),
    .description = NC_("accessibility", "Activates the expander"),
    .keybinding = "<Space>",
  },
};

static void
expander_handle_method (GDBusConnection       *connection,
                        const gchar           *sender,
                        const gchar           *object_path,
                        const gchar           *interface_name,
                        const gchar           *method_name,
                        GVariant              *parameters,
                        GDBusMethodInvocation *invocation,
                        gpointer               user_data)
{
  GtkAtSpiContext *self = user_data;

  action_handle_method (self, method_name, parameters, invocation,
                        expander_actions,
                        G_N_ELEMENTS (expander_actions));
}

static GVariant *
expander_handle_get_property (GDBusConnection  *connection,
                              const gchar      *sender,
                              const gchar      *object_path,
                              const gchar      *interface_name,
                              const gchar      *property_name,
                              GError          **error,
                              gpointer          user_data)
{
  GtkAtSpiContext *self = user_data;

  return action_handle_get_property (self, property_name, error,
                                     expander_actions,
                                     G_N_ELEMENTS (expander_actions));
}

static const GDBusInterfaceVTable expander_action_vtable = {
  expander_handle_method,
  expander_handle_get_property,
  NULL,
};

/* }}} */

/* {{{ GtkEntry */

static gboolean is_primary_icon_enabled (GtkAtSpiContext *self);
static gboolean is_secondary_icon_enabled (GtkAtSpiContext *self);
static gboolean activate_primary_icon (GtkAtSpiContext *self);
static gboolean activate_secondary_icon (GtkAtSpiContext *self);

static const Action entry_actions[] = {
  {
    .name = "activate",
    .localized_name = NC_("accessibility", "Activate"),
    .description = NC_("accessibility", "Activates the entry"),
    .keybinding = "<Return>",
    .is_enabled = NULL,
    .activate = NULL,
  },
  {
    .name = "activate-primary-icon",
    .localized_name = NC_("accessibility", "Activate primary icon"),
    .description = NC_("accessibility", "Activates the primary icon of the entry"),
    .keybinding = "<VoidSymbol>",
    .is_enabled = is_primary_icon_enabled,
    .activate = activate_primary_icon,
  },
  {
    .name = "activate-secondary-icon",
    .localized_name = NC_("accessibility", "Activate secondary icon"),
    .description = NC_("accessibility", "Activates the secondary icon of the entry"),
    .keybinding = "<VoidSymbol>",
    .is_enabled = is_secondary_icon_enabled,
    .activate = activate_secondary_icon,
  },
};

static gboolean
is_primary_icon_enabled (GtkAtSpiContext *self)
{
  GtkAccessible *accessible = gtk_at_context_get_accessible (GTK_AT_CONTEXT (self));
  GtkEntry *entry = GTK_ENTRY (accessible);

  return gtk_entry_get_icon_storage_type (entry, GTK_ENTRY_ICON_PRIMARY) != GTK_IMAGE_EMPTY;
}

static gboolean
activate_primary_icon (GtkAtSpiContext *self)
{
  GtkAccessible *accessible = gtk_at_context_get_accessible (GTK_AT_CONTEXT (self));
  GtkEntry *entry = GTK_ENTRY (accessible);

  return gtk_entry_activate_icon (entry, GTK_ENTRY_ICON_PRIMARY);
}

static gboolean
is_secondary_icon_enabled (GtkAtSpiContext *self)
{
  GtkAccessible *accessible = gtk_at_context_get_accessible (GTK_AT_CONTEXT (self));
  GtkEntry *entry = GTK_ENTRY (accessible);

  return gtk_entry_get_icon_storage_type (entry, GTK_ENTRY_ICON_SECONDARY) != GTK_IMAGE_EMPTY;
}

static gboolean
activate_secondary_icon (GtkAtSpiContext *self)
{
  GtkAccessible *accessible = gtk_at_context_get_accessible (GTK_AT_CONTEXT (self));
  GtkEntry *entry = GTK_ENTRY (accessible);

  return gtk_entry_activate_icon (entry, GTK_ENTRY_ICON_SECONDARY);
}

static void
entry_handle_method (GDBusConnection       *connection,
                     const gchar           *sender,
                     const gchar           *object_path,
                     const gchar           *interface_name,
                     const gchar           *method_name,
                     GVariant              *parameters,
                     GDBusMethodInvocation *invocation,
                     gpointer               user_data)
{
  GtkAtSpiContext *self = user_data;

  action_handle_method (self, method_name, parameters, invocation,
                        entry_actions,
                        G_N_ELEMENTS (entry_actions));
}

static GVariant *
entry_handle_get_property (GDBusConnection  *connection,
                           const gchar      *sender,
                           const gchar      *object_path,
                           const gchar      *interface_name,
                           const gchar      *property_name,
                           GError          **error,
                           gpointer          user_data)
{
  GtkAtSpiContext *self = user_data;

  return action_handle_get_property (self, property_name, error,
                                     entry_actions,
                                     G_N_ELEMENTS (entry_actions));
}

static const GDBusInterfaceVTable entry_action_vtable = {
  entry_handle_method,
  entry_handle_get_property,
  NULL,
};

/* }}} */

/* {{{ GtkPasswordEntry */

static gboolean is_peek_enabled (GtkAtSpiContext *self);
static gboolean activate_peek (GtkAtSpiContext *self);

static const Action password_entry_actions[] = {
  {
    .name = "activate",
    .localized_name = NC_("accessibility", "Activate"),
    .description = NC_("accessibility", "Activates the entry"),
    .keybinding = "<Return>",
    .is_enabled = NULL,
    .activate = NULL,
  },
  {
    .name = "peek",
    .localized_name = NC_("accessibility", "Peek"),
    .description = NC_("accessibility", "Shows the contents of the password entry"),
    .keybinding = "<VoidSymbol>",
    .is_enabled = is_peek_enabled,
    .activate = activate_peek,
  },
};

static gboolean
is_peek_enabled (GtkAtSpiContext *self)
{
  GtkAccessible *accessible = gtk_at_context_get_accessible (GTK_AT_CONTEXT (self));
  GtkPasswordEntry *entry = GTK_PASSWORD_ENTRY (accessible);

  if (!gtk_password_entry_get_show_peek_icon (entry))
    return FALSE;

  return TRUE;
}

static gboolean
activate_peek (GtkAtSpiContext *self)
{
  GtkAccessible *accessible = gtk_at_context_get_accessible (GTK_AT_CONTEXT (self));
  GtkPasswordEntry *entry = GTK_PASSWORD_ENTRY (accessible);

  gtk_password_entry_toggle_peek (entry);

  return TRUE;
}

static void
password_entry_handle_method (GDBusConnection       *connection,
                              const gchar           *sender,
                              const gchar           *object_path,
                              const gchar           *interface_name,
                              const gchar           *method_name,
                              GVariant              *parameters,
                              GDBusMethodInvocation *invocation,
                              gpointer               user_data)
{
  GtkAtSpiContext *self = user_data;

  action_handle_method (self, method_name, parameters, invocation,
                        password_entry_actions,
                        G_N_ELEMENTS (password_entry_actions));
}

static GVariant *
password_entry_handle_get_property (GDBusConnection  *connection,
                                    const gchar      *sender,
                                    const gchar      *object_path,
                                    const gchar      *interface_name,
                                    const gchar      *property_name,
                                    GError          **error,
                                    gpointer          user_data)
{
  GtkAtSpiContext *self = user_data;

  return action_handle_get_property (self, property_name, error,
                                     password_entry_actions,
                                     G_N_ELEMENTS (password_entry_actions));
}

static const GDBusInterfaceVTable password_entry_action_vtable = {
  password_entry_handle_method,
  password_entry_handle_get_property,
  NULL,
};

/* }}} */

static gboolean
is_valid_action (GtkActionMuxer *muxer,
                 const char     *action_name)
{
  const GVariantType *param_type = NULL;
  gboolean enabled = FALSE;

  /* Skip disabled or parametrized actions */
  if (!gtk_action_muxer_query_action (muxer, action_name,
                                      &enabled,
                                      &param_type, NULL,
                                      NULL, NULL))
    return FALSE;

  if (!enabled || param_type != NULL)
    return FALSE;

  return TRUE;
}

static void
add_muxer_actions (GtkActionMuxer   *muxer,
                   char            **actions,
                   int               n_actions,
                   GVariantBuilder  *builder)
{
  for (int i = 0; i < n_actions; i++)
    {
      if (!is_valid_action (muxer, actions[i]))
        continue;

      g_variant_builder_add (builder, "(sss)",
                             actions[i],
                             actions[i],
                             "<VoidSymbol>");
    }
}

static const char *
get_action_at_index (GtkActionMuxer  *muxer,
                     char           **actions,
                     int              n_actions,
                     int              pos)
{
  int real_pos = 0;

  for (int i = 0; i < n_actions; i++)
    {
      if (!is_valid_action (muxer, actions[i]))
        continue;

      if (real_pos == pos)
        return actions[i];

      real_pos += 1;
    }

  return NULL;
}

static int
get_valid_actions (GtkActionMuxer  *muxer,
                   char           **actions,
                   int              n_actions)
{
  int n_enabled_actions = 0;

  for (int i = 0; i < n_actions; i++)
    {
      if (!is_valid_action (muxer, actions[i]))
        continue;

      n_enabled_actions += 1;
    }

  return n_enabled_actions;
}

static void
widget_handle_method (GDBusConnection       *connection,
                      const gchar           *sender,
                      const gchar           *object_path,
                      const gchar           *interface_name,
                      const gchar           *method_name,
                      GVariant              *parameters,
                      GDBusMethodInvocation *invocation,
                      gpointer               user_data)
{
  GtkAtSpiContext *self = user_data;
  GtkAccessible *accessible = gtk_at_context_get_accessible (GTK_AT_CONTEXT (self));
  GtkWidget *widget = GTK_WIDGET (accessible);
  GtkWidget *parent = gtk_widget_get_parent (widget);
  GtkActionMuxer *muxer = _gtk_widget_get_action_muxer (widget, FALSE);
  GtkActionMuxer *parent_muxer = parent ? _gtk_widget_get_action_muxer (parent, FALSE) : NULL;

  if (muxer == NULL)
    return;

  char **actions = NULL;

  if (muxer != parent_muxer)
    actions = gtk_action_muxer_list_actions (muxer, TRUE);

  int n_actions = actions != NULL ? g_strv_length (actions) : 0;

  /* XXX: We need more fields in the action API */
  if (g_strcmp0 (method_name, "GetName") == 0 ||
      g_strcmp0 (method_name, "GetLocalizedName") == 0 ||
      g_strcmp0 (method_name, "GetDescription") == 0)
    {
      int action_idx;

      g_variant_get (parameters, "(i)", &action_idx);

      const char *action = get_action_at_index (muxer, actions, n_actions, action_idx);

      if (action != NULL && gtk_widget_is_sensitive (widget))
        g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", action));
      else
        g_dbus_method_invocation_return_error (invocation,
                                               G_IO_ERROR,
                                               G_IO_ERROR_INVALID_ARGUMENT,
                                               "No action with index %d",
                                               action_idx);
    }
  else if (g_strcmp0 (method_name, "DoAction") == 0)
    {
      int action_idx;

      g_variant_get (parameters, "(i)", &action_idx);

      const char *action = get_action_at_index (muxer, actions, n_actions, action_idx);

      if (action != NULL && gtk_widget_is_sensitive (widget))
        {
          gboolean res = gtk_widget_activate_action_variant (widget, action, NULL);

          g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", res));
        }
      else
        {
          g_dbus_method_invocation_return_error (invocation,
                                                 G_IO_ERROR,
                                                 G_IO_ERROR_INVALID_ARGUMENT,
                                                 "No action with index %d",
                                                 action_idx);
        }
    }
  else if (g_strcmp0 (method_name, "GetKeyBinding") == 0)
    {
      int action_idx;

      g_variant_get (parameters, "(i)", &action_idx);

      const char *action = get_action_at_index (muxer, actions, n_actions, action_idx);

      if (action != NULL && gtk_widget_is_sensitive (widget))
        g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", "<VoidSymbol>"));
      else
        g_dbus_method_invocation_return_error (invocation,
                                               G_IO_ERROR,
                                               G_IO_ERROR_INVALID_ARGUMENT,
                                               "No action with index %d",
                                               action_idx);
    }
  else if (g_strcmp0 (method_name, "GetActions") == 0)
    {
      GVariantBuilder builder = G_VARIANT_BUILDER_INIT (G_VARIANT_TYPE ("a(sss)"));

      if (n_actions >= 0 && gtk_widget_is_sensitive (widget))
        add_muxer_actions (muxer, actions, n_actions, &builder);

      g_dbus_method_invocation_return_value (invocation, g_variant_new ("(a(sss))", &builder));
    }

  g_strfreev (actions);
}

static GVariant *
widget_handle_get_property (GDBusConnection  *connection,
                            const gchar      *sender,
                            const gchar      *object_path,
                            const gchar      *interface_name,
                            const gchar      *property_name,
                            GError          **error,
                            gpointer          user_data)
{
  GtkAtSpiContext *self = user_data;
  GtkAccessible *accessible = gtk_at_context_get_accessible (GTK_AT_CONTEXT (self));
  GtkWidget *widget = GTK_WIDGET (accessible);
  GtkWidget *parent = gtk_widget_get_parent (widget);
  GtkActionMuxer *muxer = _gtk_widget_get_action_muxer (widget, FALSE);
  GtkActionMuxer *parent_muxer = parent ? _gtk_widget_get_action_muxer (parent, FALSE) : NULL;
  GVariant *res = NULL;

  if (muxer == NULL)
    return res;

  char **actions = NULL;

  if (muxer != parent_muxer)
    actions = gtk_action_muxer_list_actions (muxer, TRUE);

  int n_actions = actions != NULL ? g_strv_length (actions) : 0;

  if (g_strcmp0 (property_name, "NActions") == 0)
    res = g_variant_new ("i", get_valid_actions (muxer, actions, n_actions));
  else
    g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
                 "Unknown property '%s'", property_name);

  return res;
}

static const GDBusInterfaceVTable widget_action_vtable = {
  widget_handle_method,
  widget_handle_get_property,
  NULL,
};

const GDBusInterfaceVTable *
gtk_atspi_get_action_vtable (GtkAccessible *accessible)
{
  if (GTK_IS_BUTTON (accessible))
    return &button_action_vtable;
  else if (GTK_IS_ENTRY (accessible))
    return &entry_action_vtable;
  else if (GTK_IS_EXPANDER (accessible))
    return &expander_action_vtable;
  else if (GTK_IS_PASSWORD_ENTRY (accessible))
    return &password_entry_action_vtable;
  else if (GTK_IS_SWITCH (accessible))
    return &switch_action_vtable;
  else if (GTK_IS_WIDGET (accessible))
    return &widget_action_vtable;

  return NULL;
}