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>
33 REGISTRY_APPLICATION_REMOVE = 0,
34 REGISTRY_APPLICATION_ADD = 1
37 /*---------------------------------------------------------------------------*/
39 G_DEFINE_TYPE(SpiRegistry, spi_registry, G_TYPE_OBJECT)
42 spi_registry_class_init (SpiRegistryClass *klass)
44 GObjectClass * object_class = (GObjectClass *) klass;
46 spi_registry_parent_class = g_type_class_ref (G_TYPE_OBJECT);
50 spi_registry_init (SpiRegistry *registry)
52 registry->apps = g_sequence_new (g_free);
55 /*---------------------------------------------------------------------------*/
57 static void emit(SpiRegistry *reg, const char *itf, const char *name, int ftype, ...)
62 spi_dbus_emit_valist(reg->bus, SPI_DBUS_PATH_REGISTRY, itf, name, ftype, arg);
66 /*---------------------------------------------------------------------------*/
69 data_str_cmp (gpointer a, gpointer b, gpointer data)
71 return g_strcmp0(a, b);
75 seq_add_string (GSequence *seq, gchar *str)
81 iter = g_sequence_search (seq, str, (GCompareDataFunc) data_str_cmp, NULL);
82 iter = g_sequence_iter_prev (iter);
84 if (!g_sequence_iter_is_end (iter))
86 item = g_sequence_get (iter);
87 if (g_strcmp0 (item, str))
89 g_sequence_insert_sorted (seq, g_strdup(str), (GCompareDataFunc) data_str_cmp, NULL);
95 g_sequence_insert_sorted (seq, g_strdup(str), (GCompareDataFunc) data_str_cmp, NULL);
103 seq_remove_string (GSequence *seq, gchar *str)
107 gboolean res = FALSE;
109 iter = g_sequence_search (seq, str, (GCompareDataFunc) data_str_cmp, NULL);
110 iter = g_sequence_iter_prev (iter);
112 if (!g_sequence_iter_is_end (iter))
114 item = g_sequence_get (iter);
115 if (!g_strcmp0 (item, str))
117 g_sequence_remove (iter);
125 add_application (DBusConnection *bus, SpiRegistry *reg, gchar *app)
127 guint add = REGISTRY_APPLICATION_ADD;
129 if (seq_add_string (reg->apps, app))
132 SPI_DBUS_INTERFACE_REGISTRY,
133 "updateApplications",
143 remove_application (DBusConnection *bus, SpiRegistry *reg, gchar *app)
145 guint remove = REGISTRY_APPLICATION_REMOVE;
147 if (seq_remove_string (reg->apps, app))
149 /*TODO spi_remove_device_listeners (registry->de_controller, old);*/
151 SPI_DBUS_INTERFACE_REGISTRY,
152 "updateApplications",
161 /*---------------------------------------------------------------------------*/
164 add_bus_name_cb (gpointer item, gpointer data)
166 DBusMessageIter *iter_array = (DBusMessageIter *) data;
168 dbus_message_iter_append_basic (iter_array, DBUS_TYPE_STRING, (gchar **) &item);
172 impl_getApplications (DBusConnection *bus, DBusMessage *message, void *user_data)
174 DBusMessage *reply = NULL;
175 DBusMessageIter iter, iter_array;
176 SpiRegistry *reg = SPI_REGISTRY (user_data);
178 reply = dbus_message_new_method_return (message);
180 dbus_message_iter_init_append (reply, &iter);
181 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &iter_array);
182 g_sequence_foreach (reg->apps, add_bus_name_cb, &iter_array);
183 dbus_message_iter_close_container(&iter, &iter_array);
187 /*---------------------------------------------------------------------------*/
190 impl_registerApplication (DBusConnection *bus, DBusMessage *message, void *user_data)
193 SpiRegistry *reg = SPI_REGISTRY (user_data);
195 if (dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &app_name, DBUS_TYPE_INVALID))
196 add_application(bus, reg, app_name);
201 impl_deregisterApplication (DBusConnection *bus, DBusMessage *message, void *user_data)
204 SpiRegistry *reg = SPI_REGISTRY (user_data);
206 if (dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &app_name, DBUS_TYPE_INVALID))
207 remove_application(bus, reg, app_name);
211 /*---------------------------------------------------------------------------*/
214 handle_disconnection (DBusConnection *bus, DBusMessage *message, void *user_data)
216 char *name, *old, *new;
217 SpiRegistry *reg = SPI_REGISTRY (user_data);
219 if (dbus_message_get_args (message, NULL,
220 DBUS_TYPE_STRING, &name,
221 DBUS_TYPE_STRING, &old,
222 DBUS_TYPE_STRING, &new,
225 if (*old != '\0' && *new == '\0')
227 remove_application(bus, reg, old);
232 /*---------------------------------------------------------------------------*/
234 static DBusHandlerResult
235 signal_filter (DBusConnection *bus, DBusMessage *message, void *user_data)
237 SpiRegistry *registry = SPI_REGISTRY (user_data);
238 guint res = DBUS_HANDLER_RESULT_HANDLED;
239 const char *iface = dbus_message_get_interface (message);
240 const char *member = dbus_message_get_member (message);
242 if (!g_strcmp0(iface, DBUS_INTERFACE_DBUS) && !g_strcmp0(member, "NameOwnerChanged"))
243 handle_disconnection (bus, message, user_data);
245 res = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
250 /*---------------------------------------------------------------------------*/
252 static gchar *app_sig_match_name_owner =
253 "type='signal', interface='org.freedesktop.DBus', member='NameOwnerChanged'";
255 static DRouteMethod dev_methods[] =
257 { impl_getApplications, "getApplications" },
258 { impl_registerApplication, "registerApplication" },
259 { impl_deregisterApplication, "deregisterApplication" },
264 spi_registry_new (DBusConnection *bus, DRouteContext *droute)
266 SpiRegistry *reg = g_object_new (SPI_REGISTRY_TYPE, NULL);
271 dbus_bus_add_match (bus, app_sig_match_name_owner, NULL);
272 dbus_connection_add_filter (bus, signal_filter, reg, NULL);
274 path = droute_add_one (droute,
275 SPI_DBUS_PATH_REGISTRY,
278 droute_path_add_interface (path,
279 SPI_DBUS_INTERFACE_REGISTRY,
286 /*END------------------------------------------------------------------------*/