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 <math.h>
26

            
27
#define ATK_DISABLE_DEPRECATION_WARNINGS
28
#include "bridge.h"
29
#include <atk/atk.h>
30
#include <droute/droute.h>
31

            
32
#include "introspection.h"
33
#include "spi-dbus.h"
34

            
35
static dbus_bool_t
36
impl_get_Version (DBusMessageIter *iter, void *user_data)
37
{
38
  return droute_return_v_uint32 (iter, SPI_DBUS_VALUE_VERSION);
39
}
40

            
41
static dbus_bool_t
42
1
impl_get_MinimumValue (DBusMessageIter *iter, void *user_data)
43
{
44
1
  AtkValue *value = (AtkValue *) user_data;
45
1
  GValue src = { 0 };
46
1
  GValue dest = { 0 };
47
  gdouble dub;
48

            
49
1
  g_return_val_if_fail (ATK_IS_VALUE (user_data), FALSE);
50
1
  AtkValueIface *iface = ATK_VALUE_GET_IFACE (value);
51
1
  if (iface->get_range)
52
    {
53
1
      AtkRange *range = atk_value_get_range (value);
54
1
      dub = atk_range_get_lower_limit (range);
55
1
      atk_range_free (range);
56
1
      return droute_return_v_double (iter, dub);
57
    }
58

            
59
  g_value_init (&src, G_TYPE_DOUBLE);
60
  atk_value_get_minimum_value (value, &src);
61
  g_value_init (&dest, G_TYPE_DOUBLE);
62

            
63
  if (g_value_transform (&src, &dest))
64
    {
65
      dub = g_value_get_double (&dest);
66
      return droute_return_v_double (iter, dub);
67
    }
68
  else
69
    {
70
      return FALSE;
71
    }
72
}
73

            
74
static dbus_bool_t
75
1
impl_get_MaximumValue (DBusMessageIter *iter, void *user_data)
76
{
77
1
  AtkValue *value = (AtkValue *) user_data;
78
1
  GValue src = { 0 };
79
1
  GValue dest = { 0 };
80
1
  gdouble dub = 0;
81

            
82
1
  g_return_val_if_fail (ATK_IS_VALUE (user_data), FALSE);
83

            
84
1
  AtkValueIface *iface = ATK_VALUE_GET_IFACE (value);
85
1
  if (iface->get_range)
86
    {
87
1
      AtkRange *range = atk_value_get_range (value);
88
1
      dub = atk_range_get_upper_limit (range);
89
1
      atk_range_free (range);
90
1
      return droute_return_v_double (iter, dub);
91
    }
92

            
93
  g_value_init (&src, G_TYPE_DOUBLE);
94
  atk_value_get_maximum_value (value, &src);
95
  g_value_init (&dest, G_TYPE_DOUBLE);
96

            
97
  if (g_value_transform (&src, &dest))
98
    {
99
      dub = g_value_get_double (&dest);
100
    }
101
  return droute_return_v_double (iter, dub);
102
}
103

            
104
static dbus_bool_t
105
1
impl_get_MinimumIncrement (DBusMessageIter *iter, void *user_data)
106
{
107
1
  AtkValue *value = (AtkValue *) user_data;
108
1
  GValue src = { 0 };
109
1
  GValue dest = { 0 };
110
1
  gdouble dub = 0;
111

            
112
1
  g_return_val_if_fail (ATK_IS_VALUE (user_data), FALSE);
113

            
114
1
  AtkValueIface *iface = ATK_VALUE_GET_IFACE (value);
115
1
  if (iface->get_increment)
116
    {
117
1
      dub = atk_value_get_increment (value);
118
1
      return droute_return_v_double (iter, dub);
119
    }
120

            
121
  g_value_init (&src, G_TYPE_DOUBLE);
122
  atk_value_get_minimum_increment (value, &src);
123
  g_value_init (&dest, G_TYPE_DOUBLE);
124

            
125
  if (g_value_transform (&src, &dest))
126
    {
127
      dub = g_value_get_double (&dest);
128
    }
129
  return droute_return_v_double (iter, dub);
130
}
131

            
132
static dbus_bool_t
133
2
impl_get_CurrentValue (DBusMessageIter *iter, void *user_data)
134
{
135
2
  AtkValue *value = (AtkValue *) user_data;
136
2
  GValue src = { 0 };
137
2
  GValue dest = { 0 };
138
2
  gdouble dub = 0;
139

            
140
2
  g_return_val_if_fail (ATK_IS_VALUE (user_data), FALSE);
141

            
142
2
  AtkValueIface *iface = ATK_VALUE_GET_IFACE (value);
143
2
  if (iface->get_value_and_text)
144
    {
145
2
      gchar *text = NULL;
146
2
      atk_value_get_value_and_text (value, &dub, &text);
147
2
      return droute_return_v_double (iter, dub);
148
    }
149

            
150
  g_value_init (&src, G_TYPE_DOUBLE);
151
  atk_value_get_current_value (value, &src);
152
  g_value_init (&dest, G_TYPE_DOUBLE);
153

            
154
  if (g_value_transform (&src, &dest))
155
    {
156
      dub = g_value_get_double (&dest);
157
    }
158
  return droute_return_v_double (iter, dub);
159
}
160

            
161
static dbus_bool_t
162
1
impl_set_CurrentValue (DBusMessageIter *iter, void *user_data)
163
{
164
1
  AtkValue *value = (AtkValue *) user_data;
165
1
  GValue src = { 0 };
166
1
  GValue dest = { 0 };
167
  gdouble dub;
168
  DBusMessageIter iter_variant;
169

            
170
1
  g_return_val_if_fail (ATK_IS_VALUE (user_data), FALSE);
171

            
172
1
  dbus_message_iter_recurse (iter, &iter_variant);
173
1
  if (dbus_message_iter_get_arg_type (&iter_variant) != DBUS_TYPE_DOUBLE)
174
    {
175
      g_warning ("TODO: Support setting value from a non-double");
176
      return FALSE;
177
    }
178
1
  dbus_message_iter_get_basic (&iter_variant, &dub);
179

            
180
1
  AtkValueIface *iface = ATK_VALUE_GET_IFACE (value);
181
1
  if (iface->set_value)
182
    {
183
1
      atk_value_set_value (value, dub);
184
1
      return TRUE;
185
    }
186

            
187
  g_value_init (&src, G_TYPE_DOUBLE);
188
  g_value_set_double (&src, dub);
189

            
190
  atk_value_get_current_value (value, &dest);
191

            
192
  if (g_value_transform (&src, &dest))
193
    {
194
      atk_value_set_current_value (value, &dest);
195
      return TRUE;
196
    }
197
  else
198
    {
199
      return FALSE;
200
    }
201
}
202

            
203
/* keeping this method around for backwards-compatibility for now; see
204
 *  * BGO#652596 */
205
static DBusMessage *
206
impl_SetCurrentValue (DBusConnection *bus, DBusMessage *message, void *user_data)
207
{
208
  AtkValue *value = (AtkValue *) user_data;
209
  dbus_bool_t rv;
210
  DBusMessage *reply;
211
  gdouble dub = 0;
212
  GValue new_value = { 0 };
213

            
214
  g_return_val_if_fail (ATK_IS_VALUE (value),
215
                        droute_not_yet_handled_error (message));
216

            
217
  if (!dbus_message_get_args (message, NULL, DBUS_TYPE_DOUBLE, &dub, DBUS_TYPE_INVALID))
218
    {
219
      return droute_invalid_arguments_error (message);
220
    }
221

            
222
  g_value_init (&new_value, G_TYPE_DOUBLE);
223
  g_value_set_double (&new_value, dub);
224
  rv = atk_value_set_current_value (value, &new_value);
225

            
226
  reply = dbus_message_new_method_return (message);
227
  if (reply)
228
    {
229
      dbus_message_append_args (reply, DBUS_TYPE_BOOLEAN, &rv,
230
                                DBUS_TYPE_INVALID);
231
    }
232
  return reply;
233
}
234

            
235
static dbus_bool_t
236
1
impl_get_Text (DBusMessageIter *iter, void *user_data)
237
{
238
1
  AtkValue *value = (AtkValue *) user_data;
239
  gdouble dub;
240
1
  gchar *text = NULL;
241
  dbus_bool_t ret;
242

            
243
1
  g_return_val_if_fail (ATK_IS_VALUE (user_data), FALSE);
244

            
245
1
  AtkValueIface *iface = ATK_VALUE_GET_IFACE (value);
246
1
  if (iface->get_value_and_text)
247
    {
248
1
      atk_value_get_value_and_text (value, &dub, &text);
249
1
      ret = droute_return_v_string (iter, text);
250
1
      g_free (text);
251
1
      return ret;
252
    }
253

            
254
  return droute_return_v_string (iter, "");
255
}
256

            
257
static DRouteMethod methods[] = {
258
  { impl_SetCurrentValue, "SetCurrentValue" },
259
  { NULL, NULL }
260
};
261

            
262
static DRouteProperty properties[] = {
263
  { impl_get_MinimumValue, NULL, "MinimumValue" },
264
  { impl_get_MaximumValue, NULL, "MaximumValue" },
265
  { impl_get_MinimumIncrement, NULL, "MinimumIncrement" },
266
  { impl_get_CurrentValue, impl_set_CurrentValue, "CurrentValue" },
267
  { impl_get_Text, NULL, "Text" },
268
  { impl_get_Version, NULL, "version" },
269
  { NULL, NULL, NULL }
270
};
271

            
272
void
273
161
spi_initialize_value (DRoutePath *path)
274
{
275
161
  spi_atk_add_interface (path,
276
                         ATSPI_DBUS_INTERFACE_VALUE,
277
                         spi_org_a11y_atspi_Value,
278
                         methods, properties);
279
161
};