a72655946d65fec61a5250a83585424f9ce32b46
[platform/upstream/at-spi2-core.git] / registryd / registry.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2008, Codethink Ltd.
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 <config.h>
26 #include <dbus/dbus-glib-lowlevel.h>
27
28 #include "paths.h"
29 #include "registry.h"
30
31 enum
32 {
33   REGISTRY_APPLICATION_REMOVE = 0,
34   REGISTRY_APPLICATION_ADD = 1
35 };
36
37 /*---------------------------------------------------------------------------*/
38
39 G_DEFINE_TYPE(SpiRegistry, spi_registry, G_TYPE_OBJECT)
40
41 static void
42 spi_registry_class_init (SpiRegistryClass *klass)
43 {
44   GObjectClass * object_class = (GObjectClass *) klass;
45
46   spi_registry_parent_class = g_type_class_ref (G_TYPE_OBJECT);
47 }
48
49 static void
50 spi_registry_init (SpiRegistry *registry)
51 {
52   registry->apps = g_sequence_new (g_free);
53 }
54
55 /*---------------------------------------------------------------------------*/
56
57 static void emit(SpiRegistry *reg, const char *itf, const char *name, const char *arg_types, ...)
58 {
59   va_list arg;
60
61   va_start(arg, arg_types);
62   dbind_emit_signal_va (reg->bus, SPI_DBUS_PATH_REGISTRY, itf, name, NULL, arg_types, arg);
63   va_end(arg);
64 }
65
66 /*---------------------------------------------------------------------------*/
67
68 static gint
69 data_str_cmp (gpointer a, gpointer b, gpointer data)
70 {
71   return g_strcmp0(a, b);
72 }
73
74 static gboolean
75 seq_add_string (GSequence *seq, gchar *str)
76 {
77   GSequenceIter *iter;
78   gchar *item;
79   gboolean res = FALSE;
80
81   iter = g_sequence_search (seq, str, (GCompareDataFunc) data_str_cmp, NULL);
82   iter = g_sequence_iter_prev (iter);
83
84   if (!g_sequence_iter_is_end (iter))
85     {
86       item = g_sequence_get (iter);
87       if (g_strcmp0 (item, str))
88         {
89           g_sequence_insert_sorted (seq, g_strdup(str), (GCompareDataFunc) data_str_cmp, NULL);
90           res = TRUE;
91         }
92     }
93   else
94     {
95       g_sequence_insert_sorted (seq, g_strdup(str), (GCompareDataFunc) data_str_cmp, NULL);
96       res = TRUE;
97     }
98
99   return res;
100 }
101
102 static gboolean
103 seq_remove_string (GSequence *seq, gchar *str)
104 {
105   GSequenceIter *iter;
106   gchar *item;
107   gboolean res = FALSE;
108
109   iter = g_sequence_search (seq, str, (GCompareDataFunc) data_str_cmp, NULL);
110   iter = g_sequence_iter_prev (iter);
111
112   if (!g_sequence_iter_is_end (iter))
113     {
114       item = g_sequence_get (iter);
115       if (!g_strcmp0 (item, str))
116         {
117           g_sequence_remove (iter);
118           res = TRUE;
119         }
120     }
121   return res;
122 }
123
124 static void
125 add_application (DBusConnection *bus, SpiRegistry *reg, gchar *app)
126 {
127   guint add = REGISTRY_APPLICATION_ADD;
128
129   if (seq_add_string (reg->apps, app))
130     {
131       emit (reg,
132             SPI_DBUS_INTERFACE_REGISTRY,
133             "updateApplications",
134             "is",
135             &add,
136             &app
137            );
138     }
139 }
140
141 static void
142 remove_application (DBusConnection *bus, SpiRegistry *reg, gchar *app)
143 {
144   guint remove = REGISTRY_APPLICATION_REMOVE;
145
146   if (seq_remove_string (reg->apps, app))
147     {
148       /*TODO spi_remove_device_listeners (registry->de_controller, old);*/
149       emit (reg,
150             SPI_DBUS_INTERFACE_REGISTRY,
151             "updateApplications",
152             "is",
153             &remove,
154             &app
155            );
156     }
157 }
158
159 /*---------------------------------------------------------------------------*/
160
161 static void
162 add_bus_name_cb (gpointer item, gpointer data)
163 {
164   DBusMessageIter *iter_array = (DBusMessageIter *) data;
165
166   dbus_message_iter_append_basic (iter_array, DBUS_TYPE_STRING, (gchar **) &item);
167 }
168
169 static DBusMessage *
170 impl_getApplications (DBusConnection *bus, DBusMessage *message, void *user_data)
171 {
172   DBusMessage *reply = NULL;
173   DBusMessageIter iter, iter_array;
174   SpiRegistry *reg = SPI_REGISTRY (user_data);
175
176   reply = dbus_message_new_method_return (message);
177
178   dbus_message_iter_init_append (reply, &iter);
179   dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &iter_array);
180   g_sequence_foreach (reg->apps, add_bus_name_cb, &iter_array);
181   dbus_message_iter_close_container(&iter, &iter_array);
182   return reply;
183 }
184
185 /*---------------------------------------------------------------------------*/
186
187 static DBusMessage*
188 impl_registerApplication (DBusConnection *bus, DBusMessage *message, void *user_data)
189 {
190   gchar *app_name;
191   SpiRegistry *reg = SPI_REGISTRY (user_data);
192
193   if (dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &app_name, DBUS_TYPE_INVALID))
194       add_application(bus, reg, app_name);
195   return NULL;
196 }
197
198 static DBusMessage*
199 impl_deregisterApplication (DBusConnection *bus, DBusMessage *message, void *user_data)
200 {
201   gchar *app_name;
202   SpiRegistry *reg = SPI_REGISTRY (user_data);
203
204   if (dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &app_name, DBUS_TYPE_INVALID))
205       remove_application(bus, reg, app_name);
206   return NULL;
207 }
208
209 /*---------------------------------------------------------------------------*/
210
211 static void
212 handle_disconnection (DBusConnection *bus, DBusMessage *message, void *user_data)
213 {
214   char *name, *old, *new;
215   SpiRegistry *reg = SPI_REGISTRY (user_data);
216
217   if (dbus_message_get_args (message, NULL,
218                              DBUS_TYPE_STRING, &name,
219                              DBUS_TYPE_STRING, &old,
220                              DBUS_TYPE_STRING, &new,
221                              DBUS_TYPE_INVALID))
222     {
223       if (*old != '\0' && *new == '\0')
224         {
225           remove_application(bus, reg, old);
226         }
227     }
228 }
229
230 /*---------------------------------------------------------------------------*/
231
232 static DBusHandlerResult
233 signal_filter (DBusConnection *bus, DBusMessage *message, void *user_data)
234 {
235   SpiRegistry *registry = SPI_REGISTRY (user_data);
236   guint res = DBUS_HANDLER_RESULT_HANDLED;
237   const char *iface = dbus_message_get_interface (message);
238   const char *member = dbus_message_get_member (message);
239
240   if (!g_strcmp0(iface, DBUS_INTERFACE_DBUS) && !g_strcmp0(member, "NameOwnerChanged"))
241       handle_disconnection (bus, message, user_data);
242   else
243       res = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
244
245   return res;
246 }
247
248 /*---------------------------------------------------------------------------*/
249
250 static gchar *app_sig_match_name_owner =
251        "type='signal', interface='org.freedesktop.DBus', member='NameOwnerChanged'";
252
253 static DRouteMethod dev_methods[] =
254 {
255   { impl_getApplications, "getApplications" },
256   { impl_registerApplication, "registerApplication" },
257   { impl_deregisterApplication, "deregisterApplication" },
258   { NULL, NULL }
259 };
260
261 SpiRegistry *
262 spi_registry_new (DBusConnection *bus, DRouteContext *droute)
263 {
264   SpiRegistry *reg = g_object_new (SPI_REGISTRY_TYPE, NULL);
265   DRoutePath *path;
266
267   reg->bus = bus;
268
269   dbus_bus_add_match (bus, app_sig_match_name_owner, NULL);
270   dbus_connection_add_filter (bus, signal_filter, reg, NULL);
271
272   path = droute_add_one (droute,
273                          SPI_DBUS_PATH_REGISTRY,
274                          reg);
275
276   droute_path_add_interface (path,
277                              SPI_DBUS_INTERFACE_REGISTRY,
278                              dev_methods,
279                              NULL);
280
281   return reg;
282 }
283
284 /*END------------------------------------------------------------------------*/