1
/*
2
 * AT-SPI - Assistive Technology Service Provider Interface
3
 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4
 *
5
 * Copyright 2008 Novell, Inc.
6
 * Copyright 2001, 2002 Sun Microsystems Inc.,
7
 * Copyright 2001, 2002 Ximian, Inc.
8
 *
9
 * This library is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public
11
 * License as published by the Free Software Foundation; either
12
 * version 2.1 of the License, or (at your option) any later version.
13
 *
14
 * This library is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public
20
 * License along with this library; if not, write to the
21
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22
 * Boston, MA 02110-1301, USA.
23
 */
24

            
25
#include "bridge.h"
26
#include <atk/atk.h>
27
#include <droute/droute.h>
28

            
29
#include "spi-dbus.h"
30

            
31
#include "introspection.h"
32

            
33
static dbus_bool_t
34
impl_get_Version (DBusMessageIter *iter, void *user_data)
35
{
36
  return droute_return_v_uint32 (iter, SPI_DBUS_ACTION_VERSION);
37
}
38

            
39
static dbus_bool_t
40
1
impl_get_NActions (DBusMessageIter *iter, void *user_data)
41
{
42
1
  AtkAction *action = (AtkAction *) user_data;
43

            
44
1
  g_return_val_if_fail (ATK_IS_ACTION (user_data), FALSE);
45
1
  return droute_return_v_int32 (iter, atk_action_get_n_actions (action));
46
}
47

            
48
static DBusMessage *
49
1
impl_get_description (DBusConnection *bus, DBusMessage *message, void *user_data)
50
{
51
1
  AtkAction *action = (AtkAction *) user_data;
52
  DBusMessage *reply;
53
  dbus_int32_t index;
54
  const char *desc;
55

            
56
1
  g_return_val_if_fail (ATK_IS_ACTION (user_data),
57
                        droute_not_yet_handled_error (message));
58
1
  if (!dbus_message_get_args (message, NULL, DBUS_TYPE_INT32, &index, DBUS_TYPE_INVALID))
59
    {
60
      return droute_invalid_arguments_error (message);
61
    }
62
1
  desc = atk_action_get_description (action, index);
63
1
  if (!desc)
64
    desc = "";
65
1
  reply = dbus_message_new_method_return (message);
66
1
  if (reply)
67
    {
68
1
      dbus_message_append_args (reply, DBUS_TYPE_STRING, &desc,
69
                                DBUS_TYPE_INVALID);
70
    }
71
1
  return reply;
72
}
73

            
74
static DBusMessage *
75
1
impl_get_name (DBusConnection *bus, DBusMessage *message, void *user_data)
76
{
77
  DBusMessage *reply;
78
  dbus_int32_t index;
79
  const char *name;
80
1
  AtkAction *action = (AtkAction *) user_data;
81

            
82
1
  g_return_val_if_fail (ATK_IS_ACTION (user_data),
83
                        droute_not_yet_handled_error (message));
84
1
  if (!dbus_message_get_args (message, NULL, DBUS_TYPE_INT32, &index, DBUS_TYPE_INVALID))
85
    {
86
      return droute_invalid_arguments_error (message);
87
    }
88
1
  name = atk_action_get_name (action, index);
89
1
  if (!name)
90
    name = "";
91
1
  reply = dbus_message_new_method_return (message);
92
1
  if (reply)
93
    {
94
1
      dbus_message_append_args (reply, DBUS_TYPE_STRING, &name,
95
                                DBUS_TYPE_INVALID);
96
    }
97
1
  return reply;
98
}
99

            
100
static DBusMessage *
101
1
impl_get_localized_name (DBusConnection *bus, DBusMessage *message, void *user_data)
102
{
103
  DBusMessage *reply;
104
  dbus_int32_t index;
105
  const char *name;
106
1
  AtkAction *action = (AtkAction *) user_data;
107

            
108
1
  g_return_val_if_fail (ATK_IS_ACTION (user_data),
109
                        droute_not_yet_handled_error (message));
110
1
  if (!dbus_message_get_args (message, NULL, DBUS_TYPE_INT32, &index, DBUS_TYPE_INVALID))
111
    {
112
      return droute_invalid_arguments_error (message);
113
    }
114
1
  name = atk_action_get_localized_name (action, index);
115
1
  if (!name)
116
    name = "";
117
1
  reply = dbus_message_new_method_return (message);
118
1
  if (reply)
119
    {
120
1
      dbus_message_append_args (reply, DBUS_TYPE_STRING, &name,
121
                                DBUS_TYPE_INVALID);
122
    }
123
1
  return reply;
124
}
125

            
126
static DBusMessage *
127
1
impl_get_keybinding (DBusConnection *bus, DBusMessage *message, void *user_data)
128
{
129
  DBusMessage *reply;
130
  dbus_int32_t index;
131
  const char *kb;
132
1
  AtkAction *action = (AtkAction *) user_data;
133

            
134
1
  g_return_val_if_fail (ATK_IS_ACTION (user_data),
135
                        droute_not_yet_handled_error (message));
136
1
  if (!dbus_message_get_args (message, NULL, DBUS_TYPE_INT32, &index, DBUS_TYPE_INVALID))
137
    {
138
      return droute_invalid_arguments_error (message);
139
    }
140
1
  kb = atk_action_get_keybinding (action, index);
141
1
  if (!kb)
142
    kb = "";
143
1
  reply = dbus_message_new_method_return (message);
144
1
  if (reply)
145
    {
146
1
      dbus_message_append_args (reply, DBUS_TYPE_STRING, &kb,
147
                                DBUS_TYPE_INVALID);
148
    }
149
1
  return reply;
150
}
151

            
152
static DBusMessage *
153
impl_GetActions (DBusConnection *bus, DBusMessage *message, void *user_data)
154
{
155
  AtkAction *action = (AtkAction *) user_data;
156
  DBusMessage *reply;
157
  gint count;
158
  gint i;
159
  DBusMessageIter iter, iter_array, iter_struct;
160

            
161
  g_return_val_if_fail (ATK_IS_ACTION (user_data),
162
                        droute_not_yet_handled_error (message));
163
  count = atk_action_get_n_actions (action);
164
  reply = dbus_message_new_method_return (message);
165
  if (!reply)
166
    goto oom;
167
  dbus_message_iter_init_append (reply, &iter);
168
  if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, "(sss)", &iter_array))
169
    goto oom;
170
  for (i = 0; i < count; i++)
171
    {
172
      const char *lname = atk_action_get_localized_name (action, i);
173
      const char *desc = atk_action_get_description (action, i);
174
      const char *kb = atk_action_get_keybinding (action, i);
175
      if (!lname)
176
        lname = "";
177
      if (!desc)
178
        desc = "";
179
      if (!kb)
180
        kb = "";
181
      if (!dbus_message_iter_open_container (&iter_array, DBUS_TYPE_STRUCT, NULL, &iter_struct))
182
        goto oom;
183
      dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &lname);
184
      dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &desc);
185
      dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &kb);
186
      if (!dbus_message_iter_close_container (&iter_array, &iter_struct))
187
        goto oom;
188
    }
189
  if (!dbus_message_iter_close_container (&iter, &iter_array))
190
    goto oom;
191
  return reply;
192
oom:
193
  // TODO: handle out-of-memory
194
  return reply;
195
}
196

            
197
static DBusMessage *
198
1
impl_DoAction (DBusConnection *bus, DBusMessage *message, void *user_data)
199
{
200
1
  AtkAction *action = (AtkAction *) user_data;
201
  dbus_int32_t index;
202
1
  dbus_bool_t rv = TRUE;
203
  DBusMessage *reply;
204

            
205
1
  g_return_val_if_fail (ATK_IS_ACTION (user_data),
206
                        droute_not_yet_handled_error (message));
207
1
  if (!dbus_message_get_args (message, NULL, DBUS_TYPE_INT32, &index, DBUS_TYPE_INVALID))
208
    {
209
      return droute_invalid_arguments_error (message);
210
    }
211
1
  reply = dbus_message_new_method_return (message);
212
1
  if (reply)
213
    {
214
1
      dbus_message_append_args (reply, DBUS_TYPE_BOOLEAN, &rv,
215
                                DBUS_TYPE_INVALID);
216
    }
217
1
  dbus_connection_send (bus, reply, NULL);
218
1
  dbus_message_unref (reply);
219
1
  atk_action_do_action (action, index);
220
1
  return NULL;
221
}
222

            
223
DRouteMethod methods[] = {
224
  { impl_get_description, "GetDescription" },
225
  { impl_get_name, "GetName" },
226
  { impl_get_localized_name, "GetLocalizedName" },
227
  { impl_get_keybinding, "GetKeyBinding" },
228
  { impl_GetActions, "GetActions" },
229
  { impl_DoAction, "DoAction" },
230
  { NULL, NULL }
231
};
232

            
233
static DRouteProperty properties[] = {
234
  { impl_get_NActions, NULL, "NActions" },
235
  { impl_get_Version, NULL, "version" },
236
  { NULL, NULL }
237
};
238

            
239
void
240
161
spi_initialize_action (DRoutePath *path)
241
{
242
161
  spi_atk_add_interface (path,
243
                         ATSPI_DBUS_INTERFACE_ACTION,
244
                         spi_org_a11y_atspi_Action, methods, properties);
245
161
};