Initial commit.
[platform/core/uifw/at-spi2-atk.git] / libspi / hyperlink.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 AtkHyperlink *
28 get_hyperlink (DBusMessage * message)
29 {
30   AtkObject *obj = spi_dbus_get_object (dbus_message_get_path (message));
31   if (!obj)
32     return NULL;
33   return ATK_HYPERLINK (obj);
34 }
35
36 static AtkHyperlink *
37 get_hyperlink_from_path (const char *path, void *user_data)
38 {
39   AtkObject *obj = spi_dbus_get_object (path);
40   if (!obj)
41     return NULL;
42   return ATK_HYPERLINK (obj);
43 }
44
45 static dbus_bool_t
46 impl_get_nAnchors (const char *path, DBusMessageIter * iter, void *user_data)
47 {
48   AtkHyperlink *link = get_hyperlink_from_path (path, user_data);
49   if (!link)
50     return FALSE;
51   return droute_return_v_int32 (iter, atk_hyperlink_get_n_anchors (link));
52 }
53
54 static char *
55 impl_get_nAnchors_str (void *datum)
56 {
57   g_assert (ATK_IS_HYPERLINK (datum));
58   return g_strdup_printf ("%d",
59                           atk_hyperlink_get_n_anchors ((AtkHyperlink *)
60                                                        datum));
61 }
62
63 static dbus_bool_t
64 impl_get_startIndex (const char *path, DBusMessageIter * iter,
65                      void *user_data)
66 {
67   AtkHyperlink *link = get_hyperlink_from_path (path, user_data);
68   if (!link)
69     return FALSE;
70   return droute_return_v_int32 (iter, atk_hyperlink_get_start_index (link));
71 }
72
73 static char *
74 impl_get_startIndex_str (void *datum)
75 {
76   g_assert (ATK_IS_HYPERLINK (datum));
77   return g_strdup_printf ("%d",
78                           atk_hyperlink_get_start_index ((AtkHyperlink *)
79                                                          datum));
80 }
81
82 static dbus_bool_t
83 impl_get_endIndex (const char *path, DBusMessageIter * iter, void *user_data)
84 {
85   AtkHyperlink *link = get_hyperlink_from_path (path, user_data);
86   if (!link)
87     return FALSE;
88   return droute_return_v_int32 (iter, atk_hyperlink_get_end_index (link));
89 }
90
91 static char *
92 impl_get_endIndex_str (void *datum)
93 {
94   g_assert (ATK_IS_HYPERLINK (datum));
95   return g_strdup_printf ("%d",
96                           atk_hyperlink_get_end_index ((AtkHyperlink *)
97                                                        datum));
98 }
99
100 static DBusMessage *
101 impl_getObject (DBusConnection * bus, DBusMessage * message, void *user_data)
102 {
103   AtkHyperlink *link = get_hyperlink (message);
104   DBusError error;
105   dbus_int32_t i;
106   AtkObject *atk_object;
107
108   if (!link)
109     return spi_dbus_general_error (message);
110   dbus_error_init (&error);
111   if (!dbus_message_get_args
112       (message, &error, DBUS_TYPE_INT32, &i, DBUS_TYPE_INVALID))
113     {
114       return SPI_DBUS_RETURN_ERROR (message, &error);
115     }
116   atk_object = atk_hyperlink_get_object (link, i);
117   return spi_dbus_return_object (message, atk_object, FALSE);
118 }
119
120 static DBusMessage *
121 impl_getURI (DBusConnection * bus, DBusMessage * message, void *user_data)
122 {
123   AtkHyperlink *link = get_hyperlink (message);
124   dbus_int32_t i;
125   DBusError error;
126   gchar *rv;
127   DBusMessage *reply;
128
129   if (!link)
130     return spi_dbus_general_error (message);
131   dbus_error_init (&error);
132   if (!dbus_message_get_args
133       (message, &error, DBUS_TYPE_INT32, &i, DBUS_TYPE_INT32, &i,
134        DBUS_TYPE_INVALID))
135     {
136       return SPI_DBUS_RETURN_ERROR (message, &error);
137     }
138
139   rv = atk_hyperlink_get_uri (link, i);
140   if (!rv)
141     rv = g_strdup ("");
142   reply = dbus_message_new_method_return (message);
143   if (reply)
144     {
145       dbus_message_append_args (reply, DBUS_TYPE_STRING, &rv,
146                                 DBUS_TYPE_INVALID);
147     }
148   g_free (rv);
149   return reply;
150 }
151
152 static DBusMessage *
153 impl_isValid (DBusConnection * bus, DBusMessage * message, void *user_data)
154 {
155   AtkHyperlink *link = get_hyperlink (message);
156   dbus_bool_t rv;
157   DBusMessage *reply;
158
159   if (!link)
160     return spi_dbus_general_error (message);
161
162   rv = atk_hyperlink_is_valid (link);
163   reply = dbus_message_new_method_return (message);
164   if (reply)
165     {
166       dbus_message_append_args (reply, DBUS_TYPE_BOOLEAN, &rv,
167                                 DBUS_TYPE_INVALID);
168     }
169   return reply;
170 }
171
172 static DRouteMethod methods[] = {
173   {DROUTE_METHOD, impl_getObject, "getObject", "i,i,i:o,,o"},
174   {DROUTE_METHOD, impl_getURI, "getURI", "i,i,i:s,,o"},
175   {DROUTE_METHOD, impl_isValid, "isValid", "b,,o"},
176   {0, NULL, NULL, NULL}
177 };
178
179 static DRouteProperty properties[] = {
180   {impl_get_nAnchors, impl_get_nAnchors_str, NULL, NULL, "nAnchors"},
181   {impl_get_startIndex, impl_get_startIndex_str, NULL, NULL, "startIndex"},
182   {impl_get_endIndex, impl_get_endIndex_str, NULL, NULL, "endIndex"},
183   {NULL, NULL, NULL, NULL, NULL}
184 };
185
186 void
187 spi_initialize_hyperlink (DRouteData * data)
188 {
189   droute_add_interface (data, "org.freedesktop.accessibility.Hyperlink",
190                         methods, properties,
191                         (DRouteGetDatumFunction) get_hyperlink_from_path,
192                         NULL);
193 };