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
#define ATK_DISABLE_DEPRECATION_WARNINGS
26
#include "bridge.h"
27
#include <atk/atk.h>
28
#include <droute/droute.h>
29

            
30
#include "spi-dbus.h"
31

            
32
#include "introspection.h"
33
#include "object.h"
34

            
35
static dbus_bool_t
36
impl_get_CurrentPageNumber (DBusMessageIter *iter, void *user_data)
37
{
38
  AtkDocument *document = (AtkDocument *) user_data;
39
  g_return_val_if_fail (ATK_IS_DOCUMENT (user_data), FALSE);
40
  return droute_return_v_int32 (iter, atk_document_get_current_page_number (document));
41
}
42

            
43
static dbus_bool_t
44
impl_get_PageCount (DBusMessageIter *iter, void *user_data)
45
{
46
  AtkDocument *document = (AtkDocument *) user_data;
47
  g_return_val_if_fail (ATK_IS_DOCUMENT (user_data), FALSE);
48
  return droute_return_v_int32 (iter, atk_document_get_page_count (document));
49
}
50

            
51
static DBusMessage *
52
1
impl_GetLocale (DBusConnection *bus, DBusMessage *message, void *user_data)
53
{
54
1
  AtkDocument *document = (AtkDocument *) user_data;
55
  const gchar *lc;
56
  DBusMessage *reply;
57

            
58
1
  g_return_val_if_fail (ATK_IS_DOCUMENT (user_data),
59
                        droute_not_yet_handled_error (message));
60
1
  lc = atk_document_get_locale (document);
61
1
  if (!lc)
62
    lc = "";
63
1
  reply = dbus_message_new_method_return (message);
64
1
  if (reply)
65
    {
66
1
      dbus_message_append_args (reply, DBUS_TYPE_STRING, &lc,
67
                                DBUS_TYPE_INVALID);
68
    }
69
1
  return reply;
70
}
71

            
72
static DBusMessage *
73
2
impl_GetAttributeValue (DBusConnection *bus, DBusMessage *message, void *user_data)
74
{
75
2
  AtkDocument *document = (AtkDocument *) user_data;
76
  gchar *attributename;
77
  const gchar *atr;
78
  DBusMessage *reply;
79

            
80
2
  g_return_val_if_fail (ATK_IS_DOCUMENT (user_data),
81
                        droute_not_yet_handled_error (message));
82
2
  if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &attributename, DBUS_TYPE_INVALID))
83
    {
84
      return droute_invalid_arguments_error (message);
85
    }
86
2
  atr = atk_document_get_attribute_value (document, attributename);
87
2
  if (!atr)
88
    atr = "";
89
2
  reply = dbus_message_new_method_return (message);
90
2
  if (reply)
91
    {
92
2
      dbus_message_append_args (reply, DBUS_TYPE_STRING, &atr,
93
                                DBUS_TYPE_INVALID);
94
    }
95
2
  return reply;
96
}
97

            
98
static DBusMessage *
99
1
impl_GetAttributes (DBusConnection *bus, DBusMessage *message, void *user_data)
100
{
101
1
  AtkDocument *document = (AtkDocument *) user_data;
102
  DBusMessage *reply;
103
  AtkAttributeSet *attributes;
104
  DBusMessageIter iter;
105

            
106
1
  g_return_val_if_fail (ATK_IS_DOCUMENT (user_data),
107
                        droute_not_yet_handled_error (message));
108

            
109
1
  attributes = atk_document_get_attributes (document);
110

            
111
1
  reply = dbus_message_new_method_return (message);
112
1
  if (reply)
113
    {
114
1
      dbus_message_iter_init_append (reply, &iter);
115
1
      spi_object_append_attribute_set (&iter, attributes);
116
    }
117

            
118
1
  if (attributes)
119
1
    atk_attribute_set_free (attributes);
120
1
  return reply;
121
}
122

            
123
static DBusMessage *
124
2
impl_GetTextSelections (DBusConnection *bus, DBusMessage *message, void *user_data)
125
{
126
2
  AtkDocument *document = (AtkDocument *) user_data;
127
  GArray *selections;
128
  gint i, count;
129
  DBusMessageIter iter, iter_array, iter_struct;
130
  DBusMessage *reply;
131

            
132
2
  g_return_val_if_fail (ATK_IS_DOCUMENT (user_data),
133
                        droute_not_yet_handled_error (message));
134

            
135
2
  selections = atk_document_get_text_selections (document);
136
2
  count = (selections ? selections->len : 0);
137

            
138
2
  reply = dbus_message_new_method_return (message);
139
2
  if (!reply)
140
    goto done;
141
2
  dbus_message_iter_init_append (reply, &iter);
142
2
  dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, "((so)i(so)ib)", &iter_array);
143
4
  for (i = 0; i < count; i++)
144
    {
145
2
      dbus_message_iter_open_container (&iter_array, DBUS_TYPE_STRUCT, NULL, &iter_struct);
146
2
      AtkTextSelection *item = &g_array_index (selections, AtkTextSelection, i);
147
2
      spi_object_append_reference (&iter_struct, item->start_object);
148
2
      dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_INT32, &item->start_offset);
149
2
      spi_object_append_reference (&iter_struct, item->end_object);
150
2
      dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_INT32, &item->end_offset);
151
2
      dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_BOOLEAN, &item->start_is_active);
152
2
      dbus_message_iter_close_container (&iter_array, &iter_struct);
153
    }
154
2
  dbus_message_iter_close_container (&iter, &iter_array);
155

            
156
2
done:
157
2
  if (selections)
158
1
    g_array_free (selections, TRUE);
159
2
  return reply;
160
}
161

            
162
static DBusMessage *
163
1
impl_SetTextSelections (DBusConnection *bus, DBusMessage *message, void *user_data)
164
{
165
1
  AtkDocument *document = (AtkDocument *) user_data;
166
  GArray *selections;
167
  DBusMessageIter iter, iter_array, iter_struct;
168
  gboolean ret;
169
  DBusMessage *reply;
170

            
171
1
  g_return_val_if_fail (ATK_IS_DOCUMENT (user_data),
172
                        droute_not_yet_handled_error (message));
173

            
174
1
  if (strcmp (dbus_message_get_signature (message), "a((so)i(so)ib)") != 0)
175
    {
176
      return droute_invalid_arguments_error (message);
177
    }
178

            
179
1
  selections = g_array_new (FALSE, TRUE, sizeof (AtkTextSelection));
180
1
  dbus_message_iter_init (message, &iter);
181
1
  dbus_message_iter_recurse (&iter, &iter_array);
182

            
183
3
  while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
184
    {
185
      AtkTextSelection selection;
186
2
      dbus_message_iter_recurse (&iter_array, &iter_struct);
187
2
      selection.start_object = ATK_OBJECT (spi_dbus_get_object_from_iter (&iter_struct));
188
2
      dbus_message_iter_get_basic (&iter_struct, &selection.start_offset);
189
2
      dbus_message_iter_next (&iter_struct);
190
2
      selection.end_object = ATK_OBJECT (spi_dbus_get_object_from_iter (&iter_struct));
191
2
      dbus_message_iter_get_basic (&iter_struct, &selection.end_offset);
192
2
      dbus_message_iter_next (&iter_struct);
193
2
      dbus_message_iter_get_basic (&iter_struct, &selection.start_is_active);
194
2
      g_array_append_val (selections, selection);
195
2
      dbus_message_iter_next (&iter_array);
196
    }
197

            
198
1
  ret = atk_document_set_text_selections (document, selections);
199
1
  g_array_free (selections, TRUE);
200

            
201
1
  reply = dbus_message_new_method_return (message);
202
1
  if (reply)
203
1
    dbus_message_append_args (reply, DBUS_TYPE_BOOLEAN, &ret, DBUS_TYPE_INVALID);
204
1
  return reply;
205
}
206

            
207
static DRouteMethod methods[] = {
208
  { impl_GetLocale, "GetLocale" },
209
  { impl_GetAttributeValue, "GetAttributeValue" },
210
  { impl_GetAttributes, "GetAttributes" },
211
  { impl_GetTextSelections, "GetTextSelections" },
212
  { impl_SetTextSelections, "SetTextSelections" },
213
  { NULL, NULL }
214
};
215

            
216
static DRouteProperty properties[] = {
217
  { impl_get_CurrentPageNumber, NULL, "CurrentPageNumber" },
218
  { impl_get_PageCount, NULL, "PageCount" },
219
  { NULL, NULL, NULL }
220
};
221

            
222
void
223
161
spi_initialize_document (DRoutePath *path)
224
{
225
161
  droute_path_add_interface (path,
226
                             ATSPI_DBUS_INTERFACE_DOCUMENT, spi_org_a11y_atspi_Document, methods, properties);
227
161
};