2009-08-21 Mark Doffman <mark.doffman@codethink.co.uk>
[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_update_applications(SpiRegistry *reg, guint sigtype, const char *app)
58 {
59   DBusMessage *msg = NULL;
60   DBusError error;
61
62   dbus_error_init (&error);
63
64   if ((msg = dbus_message_new_signal (SPI_DBUS_PATH_REGISTRY,
65                                       SPI_DBUS_INTERFACE_REGISTRY,
66                                       "updateApplications"))) {
67     dbus_message_append_args(msg, DBUS_TYPE_INT32, &sigtype,
68                                   DBUS_TYPE_STRING, &app, DBUS_TYPE_INVALID);
69
70     dbus_connection_send (reg->bus, msg, NULL);
71
72     dbus_message_unref (msg);
73   }
74
75   dbus_error_free (&error);
76 }
77
78
79 /*---------------------------------------------------------------------------*/
80
81 static gint
82 data_str_cmp (gpointer a, gpointer b, gpointer data)
83 {
84   return g_strcmp0(a, b);
85 }
86
87 static gboolean
88 seq_add_string (GSequence *seq, gchar *str)
89 {
90   GSequenceIter *iter;
91   gchar *item;
92   gboolean res = FALSE;
93
94   iter = g_sequence_search (seq, str, (GCompareDataFunc) data_str_cmp, NULL);
95   iter = g_sequence_iter_prev (iter);
96
97   if (!g_sequence_iter_is_end (iter))
98     {
99       item = g_sequence_get (iter);
100       if (g_strcmp0 (item, str))
101         {
102           g_sequence_insert_sorted (seq, g_strdup(str), (GCompareDataFunc) data_str_cmp, NULL);
103           res = TRUE;
104         }
105     }
106   else
107     {
108       g_sequence_insert_sorted (seq, g_strdup(str), (GCompareDataFunc) data_str_cmp, NULL);
109       res = TRUE;
110     }
111
112   return res;
113 }
114
115 static gboolean
116 seq_remove_string (GSequence *seq, gchar *str)
117 {
118   GSequenceIter *iter;
119   gchar *item;
120   gboolean res = FALSE;
121
122   iter = g_sequence_search (seq, str, (GCompareDataFunc) data_str_cmp, NULL);
123   iter = g_sequence_iter_prev (iter);
124
125   if (!g_sequence_iter_is_end (iter))
126     {
127       item = g_sequence_get (iter);
128       if (!g_strcmp0 (item, str))
129         {
130           g_sequence_remove (iter);
131           res = TRUE;
132         }
133     }
134   return res;
135 }
136
137 static void
138 add_application (DBusConnection *bus, SpiRegistry *reg, gchar *app)
139 {
140   if (seq_add_string (reg->apps, app))
141     {
142       emit_update_applications (reg, REGISTRY_APPLICATION_ADD, app);
143     }
144 }
145
146 static void
147 remove_application (DBusConnection *bus, SpiRegistry *reg, gchar *app)
148 {
149   if (seq_remove_string (reg->apps, app))
150     {
151       /*TODO spi_remove_device_listeners (registry->de_controller, old);*/
152       emit_update_applications (reg, REGISTRY_APPLICATION_REMOVE, app);
153     }
154 }
155
156 /*---------------------------------------------------------------------------*/
157
158 static void
159 add_bus_name_cb (gpointer item, gpointer data)
160 {
161   DBusMessageIter *iter_array = (DBusMessageIter *) data;
162
163   dbus_message_iter_append_basic (iter_array, DBUS_TYPE_STRING, (gchar **) &item);
164 }
165
166 static DBusMessage *
167 impl_getApplications (DBusConnection *bus, DBusMessage *message, void *user_data)
168 {
169   DBusMessage *reply = NULL;
170   DBusMessageIter iter, iter_array;
171   SpiRegistry *reg = SPI_REGISTRY (user_data);
172
173   reply = dbus_message_new_method_return (message);
174
175   dbus_message_iter_init_append (reply, &iter);
176   dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &iter_array);
177   g_sequence_foreach (reg->apps, add_bus_name_cb, &iter_array);
178   dbus_message_iter_close_container(&iter, &iter_array);
179   return reply;
180 }
181
182 /*---------------------------------------------------------------------------*/
183
184 static DBusMessage*
185 impl_registerApplication (DBusConnection *bus, DBusMessage *message, void *user_data)
186 {
187   gchar *app_name;
188   SpiRegistry *reg = SPI_REGISTRY (user_data);
189
190   if (dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &app_name, DBUS_TYPE_INVALID))
191       add_application(bus, reg, app_name);
192   return NULL;
193 }
194
195 static DBusMessage*
196 impl_deregisterApplication (DBusConnection *bus, DBusMessage *message, void *user_data)
197 {
198   gchar *app_name;
199   SpiRegistry *reg = SPI_REGISTRY (user_data);
200
201   if (dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &app_name, DBUS_TYPE_INVALID))
202       remove_application(bus, reg, app_name);
203   return NULL;
204 }
205
206 /*---------------------------------------------------------------------------*/
207
208 static void
209 handle_disconnection (DBusConnection *bus, DBusMessage *message, void *user_data)
210 {
211   char *name, *old, *new;
212   SpiRegistry *reg = SPI_REGISTRY (user_data);
213
214   if (dbus_message_get_args (message, NULL,
215                              DBUS_TYPE_STRING, &name,
216                              DBUS_TYPE_STRING, &old,
217                              DBUS_TYPE_STRING, &new,
218                              DBUS_TYPE_INVALID))
219     {
220       if (*old != '\0' && *new == '\0')
221         {
222           remove_application(bus, reg, old);
223         }
224     }
225 }
226
227 /*---------------------------------------------------------------------------*/
228
229 static DBusHandlerResult
230 signal_filter (DBusConnection *bus, DBusMessage *message, void *user_data)
231 {
232   SpiRegistry *registry = SPI_REGISTRY (user_data);
233   guint res = DBUS_HANDLER_RESULT_HANDLED;
234   const char *iface = dbus_message_get_interface (message);
235   const char *member = dbus_message_get_member (message);
236
237   if (!g_strcmp0(iface, DBUS_INTERFACE_DBUS) && !g_strcmp0(member, "NameOwnerChanged"))
238       handle_disconnection (bus, message, user_data);
239   else
240       res = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
241
242   return res;
243 }
244
245 /*---------------------------------------------------------------------------*/
246
247 static gchar *app_sig_match_name_owner =
248        "type='signal', interface='org.freedesktop.DBus', member='NameOwnerChanged'";
249
250 static DRouteMethod dev_methods[] =
251 {
252   { impl_getApplications, "getApplications" },
253   { impl_registerApplication, "registerApplication" },
254   { impl_deregisterApplication, "deregisterApplication" },
255   { NULL, NULL }
256 };
257
258 SpiRegistry *
259 spi_registry_new (DBusConnection *bus, DRouteContext *droute)
260 {
261   SpiRegistry *reg = g_object_new (SPI_REGISTRY_TYPE, NULL);
262   DRoutePath *path;
263
264   reg->bus = bus;
265
266   dbus_bus_add_match (bus, app_sig_match_name_owner, NULL);
267   dbus_connection_add_filter (bus, signal_filter, reg, NULL);
268
269   path = droute_add_one (droute,
270                          SPI_DBUS_PATH_REGISTRY,
271                          reg);
272
273   droute_path_add_interface (path,
274                              SPI_DBUS_INTERFACE_REGISTRY,
275                              dev_methods,
276                              NULL);
277
278   return reg;
279 }
280
281 /*END------------------------------------------------------------------------*/