BGO#652596: Allow setting CurrentValue via dbus properties interface again
[platform/core/uifw/at-spi2-atk.git] / atk-adaptor / adaptors / value-adaptor.c
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 Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 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  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 #include <math.h>
26
27 #include <atk/atk.h>
28 #include <droute/droute.h>
29
30 #include "common/spi-dbus.h"
31 #include "introspection.h"
32
33 static dbus_bool_t
34 impl_get_MinimumValue (DBusMessageIter * iter, void *user_data)
35 {
36   AtkValue *value = (AtkValue *) user_data;
37   GValue src = { 0 };
38   GValue dest = { 0 };
39   gdouble dub;
40
41   g_return_val_if_fail (ATK_IS_VALUE (user_data), FALSE);
42
43   atk_value_get_minimum_value (value, &src);
44   g_value_init (&dest, G_TYPE_DOUBLE);
45
46   if (g_value_transform (&src, &dest))
47     {
48       dub = g_value_get_double (&dest);
49       return droute_return_v_double (iter, dub);
50     }
51   else
52     {
53       return FALSE;
54     }
55 }
56
57 static dbus_bool_t
58 impl_get_MaximumValue (DBusMessageIter * iter, void *user_data)
59 {
60   AtkValue *value = (AtkValue *) user_data;
61   GValue src = { 0 };
62   GValue dest = { 0 };
63   gdouble dub = 0;
64
65   g_return_val_if_fail (ATK_IS_VALUE (user_data), FALSE);
66
67   atk_value_get_maximum_value (value, &src);
68   g_value_init (&dest, G_TYPE_DOUBLE);
69
70   if (g_value_transform (&src, &dest))
71     {
72       dub = g_value_get_double (&dest);
73     }
74   return droute_return_v_double (iter, dub);
75 }
76
77 static dbus_bool_t
78 impl_get_MinimumIncrement (DBusMessageIter * iter, void *user_data)
79 {
80   AtkValue *value = (AtkValue *) user_data;
81   GValue src = { 0 };
82   GValue dest = { 0 };
83   gdouble dub = 0;
84
85   g_return_val_if_fail (ATK_IS_VALUE (user_data), FALSE);
86
87   atk_value_get_minimum_increment (value, &src);
88   g_value_init (&dest, G_TYPE_DOUBLE);
89
90   if (g_value_transform (&src, &dest))
91     {
92       dub = g_value_get_double (&dest);
93     }
94   return droute_return_v_double (iter, dub);
95 }
96
97 static dbus_bool_t
98 impl_get_CurrentValue (DBusMessageIter * iter, void *user_data)
99 {
100   AtkValue *value = (AtkValue *) user_data;
101   GValue src = { 0 };
102   GValue dest = { 0 };
103   gdouble dub = 0;
104
105   g_return_val_if_fail (ATK_IS_VALUE (user_data), FALSE);
106
107   atk_value_get_current_value (value, &src);
108   g_value_init (&dest, G_TYPE_DOUBLE);
109
110   if (g_value_transform (&src, &dest))
111     {
112       dub = g_value_get_double (&dest);
113     }
114   return droute_return_v_double (iter, dub);
115 }
116
117 static dbus_bool_t
118 impl_set_CurrentValue (DBusMessageIter * iter, void *user_data)
119 {
120   AtkValue *value = (AtkValue *) user_data;
121   GValue src = { 0 };
122   GValue dest = { 0 };
123   gdouble dub;
124   DBusMessageIter iter_variant;
125
126   g_return_val_if_fail (ATK_IS_VALUE (user_data), FALSE);
127
128   dbus_message_iter_recurse (iter, &iter_variant);
129   if (dbus_message_iter_get_arg_type (&iter_variant) != DBUS_TYPE_DOUBLE)
130     {
131       g_warning ("TODO: Support setting value from a non-double");
132       return FALSE;
133     }
134   dbus_message_iter_get_basic (&iter_variant, &dub);
135   g_value_init (&src, G_TYPE_DOUBLE);
136   g_value_set_double (&src, dub);
137
138   atk_value_get_current_value (value, &dest);
139
140   if (g_value_transform (&src, &dest))
141     {
142       atk_value_set_current_value (value, &dest);
143       return TRUE;
144     }
145   else
146     {
147       return FALSE;
148     }
149 }
150
151 /* keeping this method around for backwards-compatibility for now; see
152  *  * BGO#652596 */
153 static DBusMessage *
154 impl_SetCurrentValue (DBusConnection * bus, DBusMessage * message,
155                        void *user_data)
156 {
157   AtkValue *value = (AtkValue *) user_data;
158   dbus_bool_t rv;
159   DBusError error;
160   DBusMessage *reply;
161   gdouble dub = 0;
162   GValue new_value = { 0 };
163
164   g_return_val_if_fail (ATK_IS_VALUE (value),
165                         droute_not_yet_handled_error (message));
166
167   dbus_error_init (&error);
168   if (!dbus_message_get_args
169       (message, &error, DBUS_TYPE_DOUBLE, &dub, DBUS_TYPE_INVALID))
170     {
171       return droute_invalid_arguments_error (message);
172     }
173
174   g_value_init (&new_value, G_TYPE_DOUBLE);
175   g_value_set_double (&new_value, dub);
176   rv = atk_value_set_current_value (value, &new_value);
177
178   reply = dbus_message_new_method_return (message);
179   if (reply)
180     {
181       dbus_message_append_args (reply, DBUS_TYPE_BOOLEAN, &rv,
182                                 DBUS_TYPE_INVALID);
183     }
184   return reply;
185 }
186
187 static DRouteMethod methods[] = {
188   {impl_SetCurrentValue, "SetCurrentValue"},
189   {NULL, NULL}
190 };
191
192 static DRouteProperty properties[] = {
193   {impl_get_MinimumValue, NULL, "MinimumValue"},
194   {impl_get_MaximumValue, NULL, "MaximumValue"},
195   {impl_get_MinimumIncrement, NULL, "MinimumIncrement"},
196   {impl_get_CurrentValue, impl_set_CurrentValue, "CurrentValue"},
197   {NULL, NULL, NULL}
198 };
199
200 void
201 spi_initialize_value (DRoutePath * path)
202 {
203   droute_path_add_interface (path,
204                              SPI_DBUS_INTERFACE_VALUE, spi_org_a11y_atspi_Value, methods, properties);
205 };