summaryrefslogtreecommitdiff
path: root/atk-adaptor/adaptors/cache-adaptor.c
blob: c2a6de9557ff4feb03cfc02f448fe4d6aa429c9b (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
/*
 * AT-SPI - Assistive Technology Service Provider Interface
 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
 *
 * Copyright 2008 Novell, Inc.
 * Copyright 2001, 2002 Sun Microsystems Inc.,
 * Copyright 2001, 2002 Ximian, Inc.
 * Copyright 2008, 2009 Codethink Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <string.h>

#include <atk/atk.h>
#include <droute/droute.h>

#include "spi-dbus.h"
#include "accessible-stateset.h"
#include "accessible-cache.h"
#include "bridge.h"
#include "object.h"
#include "introspection.h"

/* TODO - This should possibly be a common define */
#define SPI_OBJECT_PREFIX "/org/a11y/atspi"
#define SPI_CACHE_OBJECT_SUFFIX "/cache"
#define SPI_CACHE_OBJECT_PATH SPI_OBJECT_PREFIX SPI_CACHE_OBJECT_SUFFIX

#define SPI_OBJECT_REFERENCE_SIGNATURE "(" \
                                          DBUS_TYPE_STRING_AS_STRING \
                                          DBUS_TYPE_OBJECT_PATH_AS_STRING \
                                       ")"
                                          
#define SPI_CACHE_ITEM_SIGNATURE "(" \
                                   SPI_OBJECT_REFERENCE_SIGNATURE \
                                   SPI_OBJECT_REFERENCE_SIGNATURE \
                                   SPI_OBJECT_REFERENCE_SIGNATURE \
                                   DBUS_TYPE_INT32_AS_STRING \
                                   DBUS_TYPE_INT32_AS_STRING \
                                   DBUS_TYPE_ARRAY_AS_STRING \
                                     DBUS_TYPE_STRING_AS_STRING \
                                   DBUS_TYPE_STRING_AS_STRING \
                                   DBUS_TYPE_UINT32_AS_STRING \
                                   DBUS_TYPE_STRING_AS_STRING \
                                   DBUS_TYPE_ARRAY_AS_STRING \
                                     DBUS_TYPE_UINT32_AS_STRING \
                                 ")"

/*---------------------------------------------------------------------------*/

static const char *
get_toolkit_name (AtkObject *obj)
{
  static const char *toolkit_name = NULL;

  if (!toolkit_name)
    toolkit_name = atk_get_toolkit_name ();

  if (!toolkit_name)
    return "no toolkit name set yet";

  /* TODO: query object attributes */
  return toolkit_name;
}

static gboolean
should_call_index_in_parent (AtkObject *obj, AtkStateSet *set)
{
  if (atk_state_set_contains_state (set, ATK_STATE_TRANSIENT))
    return FALSE;

  if (!strcmp (get_toolkit_name (obj), "gtk") &&
      atk_object_get_role (obj) == ATK_ROLE_MENU_ITEM)
    return FALSE;

  return TRUE;
}

static gboolean
should_cache_children (AtkObject *obj, AtkStateSet *set)
{
  if (atk_state_set_contains_state (set, ATK_STATE_MANAGES_DESCENDANTS) ||
      atk_state_set_contains_state (set, ATK_STATE_DEFUNCT))
    return FALSE;

  if (!strcmp (get_toolkit_name (obj), "gtk") &&
      atk_object_get_role (obj) == ATK_ROLE_MENU)
    return FALSE;

  return TRUE;
}

/*
 * Marshals the given AtkObject into the provided D-Bus iterator.
 *
 * The object is marshalled including all its client side cache data.
 * The format of the structure is (o(so)iiassusau).
 */
static void
append_cache_item (AtkObject * obj, gpointer data)
{
  DBusMessageIter iter_struct, iter_sub_array;
  dbus_uint32_t states[2];
  dbus_int32_t count, index;
  AtkStateSet *set;
  DBusMessageIter *iter_array = (DBusMessageIter *) data;
  const char *name, *desc;
  dbus_uint32_t role;

  set = atk_object_ref_state_set (obj);
    AtkObject *application, *parent;

  dbus_message_iter_open_container (iter_array, DBUS_TYPE_STRUCT, NULL,
                                    &iter_struct);

  /* Marshal object path */
  spi_object_append_reference (&iter_struct, obj);

  role = spi_accessible_role_from_atk_role (atk_object_get_role (obj));

  /* Marshal application */
  application = spi_global_app_data->root;
  spi_object_append_reference (&iter_struct, application);

  /* Marshal parent */
  parent = atk_object_get_parent (obj);
  if (parent == NULL)
    {
      /* TODO, move in to a 'Plug' wrapper. */
      if (ATK_IS_PLUG (obj))
        {
          char *id = g_object_get_data (G_OBJECT (obj), "dbus-plug-parent");
          char *bus_parent;
          char *path_parent;

          if (id)
            {
              bus_parent = g_strdup (id);
              if (bus_parent && (path_parent = g_utf8_strchr (bus_parent + 1, -1, ':')))
                {
                  DBusMessageIter iter_parent;
                  *(path_parent++) = '\0';
                  dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_STRUCT, NULL,
                                                    &iter_parent);
                  dbus_message_iter_append_basic (&iter_parent, DBUS_TYPE_STRING, &bus_parent);
                  dbus_message_iter_append_basic (&iter_parent, DBUS_TYPE_OBJECT_PATH, &path_parent);
                  dbus_message_iter_close_container (&iter_struct, &iter_parent);
                }
              else
                {
                  spi_object_append_null_reference (&iter_struct);
                }
            }
          else
            {
              spi_object_append_null_reference (&iter_struct);
            }
        }
      else if (role != ATSPI_ROLE_APPLICATION)
        spi_object_append_null_reference (&iter_struct);
      else
        spi_object_append_desktop_reference (&iter_struct);
    }
  else
    {
      spi_object_append_reference (&iter_struct, parent);
    }

  /* Marshal index in parent */
  index = (should_call_index_in_parent (obj, set)
           ? atk_object_get_index_in_parent (obj) : -1);
  dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_INT32, &index);

  /* marshal child count */
  count = (should_cache_children (obj, set)
           ? atk_object_get_n_accessible_children (obj) : -1);

  if (ATK_IS_SOCKET (obj) && atk_socket_is_occupied (ATK_SOCKET (obj)))
    count = 1;
  dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_INT32, &count);

  /* Marshal interfaces */
  dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "s",
                                    &iter_sub_array);
  spi_object_append_interfaces (&iter_sub_array, obj);
  dbus_message_iter_close_container (&iter_struct, &iter_sub_array);

  /* Marshal name */
  name = atk_object_get_name (obj);
  if (!name)
    name = "";
  dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &name);

  /* Marshal role */
  dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_UINT32, &role);

  /* Marshal description */
  desc = atk_object_get_description (obj);
  if (!desc)
    desc = "";
  dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &desc);

  /* Marshal state set */
  spi_atk_state_set_to_dbus_array (set, states);
  dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "u",
                                    &iter_sub_array);
  for (count = 0; count < 2; count++)
    {
      dbus_message_iter_append_basic (&iter_sub_array, DBUS_TYPE_UINT32,
                                      &states[count]);
    }
  dbus_message_iter_close_container (&iter_struct, &iter_sub_array);

  dbus_message_iter_close_container (iter_array, &iter_struct);
  g_object_unref (set);
}

/*---------------------------------------------------------------------------*/

static void
ref_accessible_hf (gpointer key, gpointer obj_data, gpointer data)
{
  g_object_ref (key);
}

/* For use as a GHFunc */
static void
append_accessible_hf (gpointer key, gpointer obj_data, gpointer data)
{
  /* Make sure it isn't a hyperlink */
  if (ATK_IS_OBJECT (key))
    append_cache_item (ATK_OBJECT (key), data);
}

static void
add_to_list_hf (gpointer key, gpointer obj_data, gpointer data)
{
  GSList **listptr = data;
  *listptr = g_slist_prepend (*listptr, key);
}

/*---------------------------------------------------------------------------*/

static void
emit_cache_remove (SpiCache *cache, GObject * obj)
{
  DBusMessage *message;

  if ((message = dbus_message_new_signal (SPI_CACHE_OBJECT_PATH,
                                          ATSPI_DBUS_INTERFACE_CACHE,
                                          "RemoveAccessible")))
    {
      DBusMessageIter iter;

      dbus_message_iter_init_append (message, &iter);

      spi_object_append_reference (&iter, ATK_OBJECT (obj));

      dbus_connection_send (spi_global_app_data->bus, message, NULL);

      dbus_message_unref (message);
    }
}

static void
emit_cache_add (SpiCache *cache, GObject * obj)
{
  AtkObject *accessible = ATK_OBJECT (obj);
  DBusMessage *message;

  if ((message = dbus_message_new_signal (SPI_CACHE_OBJECT_PATH,
                                          ATSPI_DBUS_INTERFACE_CACHE,
                                          "AddAccessible")))
    {
      DBusMessageIter iter;

      dbus_message_iter_init_append (message, &iter);
      g_object_ref (accessible);
      append_cache_item (accessible, &iter);
      g_object_unref (accessible);

      dbus_connection_send (spi_global_app_data->bus, message, NULL);

      dbus_message_unref (message);
    }
}


/*---------------------------------------------------------------------------*/

static DBusMessage *
impl_GetRoot (DBusConnection * bus, DBusMessage * message, void *user_data)
{
  return spi_object_return_reference (message, spi_global_app_data->root);
}

/*---------------------------------------------------------------------------*/

static DBusMessage *
impl_GetItems (DBusConnection * bus, DBusMessage * message, void *user_data)
{
  DBusMessage *reply;
  DBusMessageIter iter, iter_array;
  GSList *pending_unrefs = NULL;

  if (bus == spi_global_app_data->bus)
    spi_atk_add_client (dbus_message_get_sender (message));

  reply = dbus_message_new_method_return (message);

  dbus_message_iter_init_append (reply, &iter);
  dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
                                    SPI_CACHE_ITEM_SIGNATURE, &iter_array);
  spi_cache_foreach (spi_global_cache, ref_accessible_hf, NULL);
  spi_cache_foreach (spi_global_cache, append_accessible_hf, &iter_array);
  spi_cache_foreach (spi_global_cache, add_to_list_hf, &pending_unrefs);
  g_slist_free_full (pending_unrefs, g_object_unref);
  dbus_message_iter_close_container (&iter, &iter_array);
  return reply;
}

/*---------------------------------------------------------------------------*/

static DRouteMethod methods[] = {
  {impl_GetRoot, "GetRoot"},
  {impl_GetItems, "GetItems"},
  {NULL, NULL}
};

void
spi_initialize_cache (DRoutePath * path)
{
  droute_path_add_interface (path, ATSPI_DBUS_INTERFACE_CACHE, spi_org_a11y_atspi_Cache, methods, NULL);

  g_signal_connect (spi_global_cache, "object-added",
                    (GCallback) emit_cache_add, NULL);

  g_signal_connect (spi_global_cache, "object-removed",
                    (GCallback) emit_cache_remove, NULL);
};

/*END------------------------------------------------------------------------*/