Changes to introspection generation to remove DOCTYPE and XML
[platform/core/uifw/at-spi2-atk.git] / libspi / action.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 "accessible.h"
26
27 static AtkAction *
28 get_action (DBusMessage * message)
29 {
30   AtkObject *obj = spi_dbus_get_object (dbus_message_get_path (message));
31   if (!obj)
32     return NULL;
33   return ATK_ACTION (obj);
34 }
35
36 static AtkAction *
37 get_action_from_path (const char *path, void *user_data)
38 {
39   AtkObject *obj = spi_dbus_get_object (path);
40   if (!obj || !ATK_IS_ACTION(obj))
41     return NULL;
42   return ATK_ACTION (obj);
43 }
44
45 static DBusMessage *impl_getActions(DBusConnection *bus, DBusMessage *message, void *user_data)
46 {
47   AtkAction *action = get_action(message);
48   DBusMessage *reply;
49   gint count;
50   gint i;
51   DBusMessageIter iter, iter_array, iter_struct;
52
53   if (!action)
54     return spi_dbus_general_error (message);
55   count = atk_action_get_n_actions(action);
56   reply = dbus_message_new_method_return (message);
57   if (!reply) goto oom;
58   dbus_message_iter_init_append (reply, &iter);
59   if (!dbus_message_iter_open_container
60       (&iter, DBUS_TYPE_ARRAY, "(sss)", &iter_array))
61     goto oom;
62   for (i = 0; i < count; i++)
63     {
64       const char *name = atk_action_get_name(action, i);
65       const char *desc = atk_action_get_description(action, i);
66       const char *kb = atk_action_get_keybinding(action, i);
67       if (!name) name = "";
68       if (!desc) desc = "";
69       if (!kb) kb = "";
70       if (!dbus_message_iter_open_container(&iter_array, DBUS_TYPE_STRUCT, NULL, &iter_struct)) goto oom;
71       dbus_message_iter_append_basic(&iter_struct, DBUS_TYPE_STRING, &name);
72       dbus_message_iter_append_basic(&iter_struct, DBUS_TYPE_STRING, &desc);
73       dbus_message_iter_append_basic(&iter_struct, DBUS_TYPE_STRING, &kb);
74       if (!dbus_message_iter_close_container(&iter_array, &iter_struct)) goto oom;
75     }
76   if (!dbus_message_iter_close_container (&iter, &iter_array))
77     goto oom;
78   return reply;
79 oom:
80   // TODO: handle out-of-memory
81   return reply;
82 }
83
84 static DBusMessage *impl_doAction(DBusConnection *bus, DBusMessage *message, void *user_data)
85 {
86   AtkAction *action = get_action(message);
87   DBusError error;
88   dbus_int32_t index;
89   dbus_bool_t rv;
90   DBusMessage *reply;
91
92   if (!action)
93     return spi_dbus_general_error (message);
94   dbus_error_init (&error);
95   if (!dbus_message_get_args
96       (message, &error, DBUS_TYPE_INT32, &index, DBUS_TYPE_INVALID))
97     {
98       return SPI_DBUS_RETURN_ERROR (message, &error);
99     }
100   rv = atk_action_do_action(action, index);
101   reply = dbus_message_new_method_return (message);
102   if (reply)
103     {
104       dbus_message_append_args (reply, DBUS_TYPE_BOOLEAN, &rv, DBUS_TYPE_INVALID);
105     }
106   return reply;
107 }
108
109 DRouteMethod methods[] =
110 {
111   {impl_getActions, "getActions"},
112   {impl_doAction, "doAction"},
113   {NULL, NULL }
114 };
115
116 void
117 spi_initialize_action (DRouteData * data)
118 {
119   droute_add_interface (data, "org.freedesktop.atspi.Action",
120                         methods, NULL,
121                         (DRouteGetDatumFunction) get_action_from_path,
122                         NULL);
123 };