2 * AT-SPI - Assistive Technology Service Provider Interface
3 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
5 * Copyright 2008, Codethink Ltd.
6 * Copyright 2001, 2002 Sun Microsystems Inc.,
7 * Copyright 2001, 2002 Ximian, Inc.
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.
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.
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.
26 #include <spi-common/spi-dbus.h>
27 #include <dbus/dbus-glib-lowlevel.h>
31 /*---------------------------------------------------------------------------*/
33 G_DEFINE_TYPE(SpiRegistry, spi_registry, G_TYPE_OBJECT)
36 spi_registry_class_init (SpiRegistryClass *klass)
38 GObjectClass * object_class = (GObjectClass *) klass;
40 spi_registry_parent_class = g_type_class_ref (G_TYPE_OBJECT);
44 spi_registry_init (SpiRegistry *registry)
46 registry->apps = g_sequence_new (g_free);
49 /*---------------------------------------------------------------------------*/
51 static void emit(SpiRegistry *reg, const char *itf, const char *name, int ftype, ...)
56 spi_dbus_emit_valist(reg->bus, SPI_DBUS_PATH_DEC, itf, name, ftype, arg);
60 /*---------------------------------------------------------------------------*/
63 add_bus_name_cb (gpointer item, gpointer data)
65 DBusMessageIter *iter_array = (DBusMessageIter *) data;
67 dbus_message_iter_append_basic (iter_array, DBUS_TYPE_STRING, (gchar *) item);
71 impl_getApplications (DBusConnection *bus, DBusMessage *message, void *user_data)
74 DBusMessageIter iter, iter_array;
75 SpiRegistry *reg = SPI_REGISTRY (user_data);
77 reply = dbus_message_new_method_return (message);
79 dbus_message_iter_init_append (reply, &iter);
80 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &iter_array);
81 g_sequence_foreach (reg->apps, add_bus_name_cb, &iter_array);
82 dbus_message_iter_close_container(&iter, &iter_array);
86 static DBusHandlerResult
87 message_handler (DBusConnection *bus, DBusMessage *message, void *user_data)
89 DBusMessage *reply = NULL;
91 if (dbus_message_is_method_call (message, SPI_DBUS_INTERFACE_REGISTRY, "getApplications"))
93 reply = impl_getApplications (bus, message, user_data);
96 dbus_connection_send (bus, reply, NULL);
97 dbus_message_unref (reply);
99 return DBUS_HANDLER_RESULT_HANDLED;
103 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
107 /*---------------------------------------------------------------------------*/
110 data_str_cmp (gpointer a, gpointer b, gpointer data)
112 return g_strcmp0(a, b);
116 seq_remove_string (GSequence *seq, gchar *str)
120 gboolean res = FALSE;
122 iter = g_sequence_search (seq, str, (GCompareDataFunc) data_str_cmp, NULL);
123 iter = g_sequence_iter_prev (iter);
125 if (!g_sequence_iter_is_end (iter))
127 item = g_sequence_get (iter);
128 if (!g_strcmp0 (item, str))
130 g_sequence_remove (iter);
138 handle_register_application (SpiRegistry *reg, DBusMessage *message)
142 if (dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &app_name, DBUS_TYPE_INVALID))
143 g_sequence_insert_sorted (reg->apps, app_name, (GCompareDataFunc) data_str_cmp, NULL);
147 handle_deregister_application (SpiRegistry *reg, DBusMessage *message)
151 if (dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &app_name, DBUS_TYPE_INVALID))
152 seq_remove_string (reg->apps, app_name);
156 handle_disconnection (SpiRegistry *reg, DBusMessage *message)
158 char *name, *old, *new;
160 if (dbus_message_get_args (message, NULL,
161 DBUS_TYPE_STRING, &name,
162 DBUS_TYPE_STRING, &old,
163 DBUS_TYPE_STRING, &new,
166 if (*old != '\0' && *new == '\0')
168 if (seq_remove_string (reg->apps, old))
170 /*Emit deregistered signal here*/
171 emit (reg, SPI_DBUS_INTERFACE_TREE, "deregisterApplication", DBUS_TYPE_STRING, old);
172 /*TODO spi_remove_device_listeners (registry->de_controller, old);*/
178 static DBusHandlerResult
179 signal_handler (DBusConnection *bus, DBusMessage *message, void *user_data)
181 SpiRegistry *registry = SPI_REGISTRY (user_data);
182 guint res = DBUS_HANDLER_RESULT_HANDLED;
183 const char *iface = dbus_message_get_interface (message);
184 const char *member = dbus_message_get_member (message);
186 g_print ("\n%s", iface);
187 g_print ("\n%d", dbus_message_get_type (message));
188 g_print ("\n%s\n", member);
191 if (dbus_message_is_signal (message, DBUS_INTERFACE_DBUS, "NameOwnerChanged"))
192 handle_disconnection (registry, message);
193 else if (dbus_message_is_signal (message, SPI_DBUS_INTERFACE_TREE, "registerApplication"))
194 handle_register_application (registry, message);
195 else if (dbus_message_is_signal (message, SPI_DBUS_INTERFACE_TREE, "deregisterApplication"))
196 handle_deregister_application (registry, message);
198 res = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
201 if (!g_strcmp0(iface, DBUS_INTERFACE_DBUS) && !g_strcmp0(member, "NameOwnerChanged"))
202 handle_disconnection (registry, message);
203 else if (!g_strcmp0(iface, SPI_DBUS_INTERFACE_TREE) && !g_strcmp0(member, "registerApplication"))
204 handle_register_application (registry, message);
205 else if (!g_strcmp0(iface, SPI_DBUS_INTERFACE_TREE) && !g_strcmp0(member, "deregisterApplication"))
206 handle_deregister_application (registry, message);
208 res = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
213 /*---------------------------------------------------------------------------*/
215 static gchar *app_reg_sig_match = "type='signal', interface='org.freedesktop.atspi.Tree', member='registerApplication'";
216 static gchar *app_dereg_sig_match = "type='signal', interface='org.freedesktop.atspi.Tree', member='deregisterApplication'";
218 static DBusObjectPathVTable reg_vtable =
222 NULL, NULL, NULL, NULL
226 spi_registry_new (DBusConnection *bus)
228 SpiRegistry *reg = g_object_new (SPI_REGISTRY_TYPE, NULL);
232 dbus_connection_register_object_path(bus, SPI_DBUS_PATH_REGISTRY, ®_vtable, reg);
234 dbus_bus_add_match (bus, app_reg_sig_match, NULL);
235 dbus_bus_add_match (bus, app_dereg_sig_match, NULL);
236 dbus_connection_add_filter (bus, signal_handler, reg, NULL);