f7209202dd2f639b32bc72c93544ce1f0224a30d
[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 <string.h>
27 #include <dbus/dbus-glib-lowlevel.h>
28
29 #include "paths.h"
30 #include "registry.h"
31
32 enum
33 {
34   REGISTRY_APPLICATION_REMOVE = 0,
35   REGISTRY_APPLICATION_ADD = 1
36 };
37
38 /*---------------------------------------------------------------------------*/
39
40 G_DEFINE_TYPE(SpiRegistry, spi_registry, G_TYPE_OBJECT)
41
42 static void
43 spi_registry_class_init (SpiRegistryClass *klass)
44 {
45   GObjectClass * object_class = (GObjectClass *) klass;
46
47   spi_registry_parent_class = g_type_class_ref (G_TYPE_OBJECT);
48 }
49
50 static void
51 spi_registry_init (SpiRegistry *registry)
52 {
53   registry->apps = g_sequence_new (g_free);
54 }
55
56 /*---------------------------------------------------------------------------*/
57
58 static void emit_update_applications(SpiRegistry *reg, guint sigtype, const char *app)
59 {
60   DBusMessage *msg = NULL;
61   DBusError error;
62
63   dbus_error_init (&error);
64
65   if ((msg = dbus_message_new_signal (SPI_DBUS_PATH_REGISTRY,
66                                       SPI_DBUS_INTERFACE_REGISTRY,
67                                       "UpdateApplications"))) {
68     dbus_message_append_args(msg, DBUS_TYPE_INT32, &sigtype,
69                                   DBUS_TYPE_STRING, &app, DBUS_TYPE_INVALID);
70
71     dbus_connection_send (reg->bus, msg, NULL);
72
73     dbus_message_unref (msg);
74   }
75
76   dbus_error_free (&error);
77 }
78
79
80 /*---------------------------------------------------------------------------*/
81
82 static gint
83 data_str_cmp (gpointer a, gpointer b, gpointer data)
84 {
85   return g_strcmp0(a, b);
86 }
87
88 static gboolean
89 seq_add_string (GSequence *seq, gchar *str)
90 {
91   GSequenceIter *iter;
92   gchar *item;
93   gboolean res = FALSE;
94
95   iter = g_sequence_search (seq, str, (GCompareDataFunc) data_str_cmp, NULL);
96   iter = g_sequence_iter_prev (iter);
97
98   if (!g_sequence_iter_is_end (iter))
99     {
100       item = g_sequence_get (iter);
101       if (g_strcmp0 (item, str))
102         {
103           g_sequence_insert_sorted (seq, g_strdup(str), (GCompareDataFunc) data_str_cmp, NULL);
104           res = TRUE;
105         }
106     }
107   else
108     {
109       g_sequence_insert_sorted (seq, g_strdup(str), (GCompareDataFunc) data_str_cmp, NULL);
110       res = TRUE;
111     }
112
113   return res;
114 }
115
116 static gboolean
117 seq_remove_string (GSequence *seq, gchar *str)
118 {
119   GSequenceIter *iter;
120   gchar *item;
121   gboolean res = FALSE;
122
123   iter = g_sequence_search (seq, str, (GCompareDataFunc) data_str_cmp, NULL);
124   iter = g_sequence_iter_prev (iter);
125
126   if (!g_sequence_iter_is_end (iter))
127     {
128       item = g_sequence_get (iter);
129       if (!g_strcmp0 (item, str))
130         {
131           g_sequence_remove (iter);
132           res = TRUE;
133         }
134     }
135   return res;
136 }
137
138 static void
139 add_application (DBusConnection *bus, SpiRegistry *reg, gchar *app)
140 {
141   if (seq_add_string (reg->apps, app))
142     {
143       emit_update_applications (reg, REGISTRY_APPLICATION_ADD, app);
144     }
145 }
146
147 static void
148 remove_application (DBusConnection *bus, SpiRegistry *reg, gchar *app)
149 {
150   if (seq_remove_string (reg->apps, app))
151     {
152       /*TODO spi_remove_device_listeners (registry->de_controller, old);*/
153       emit_update_applications (reg, REGISTRY_APPLICATION_REMOVE, app);
154     }
155 }
156
157 /*---------------------------------------------------------------------------*/
158
159 static void
160 add_bus_name_cb (gpointer item, gpointer data)
161 {
162   DBusMessageIter *iter_array = (DBusMessageIter *) data;
163
164   dbus_message_iter_append_basic (iter_array, DBUS_TYPE_STRING, (gchar **) &item);
165 }
166
167 static DBusMessage *
168 impl_GetApplications (DBusConnection *bus, DBusMessage *message, void *user_data)
169 {
170   DBusMessage *reply = NULL;
171   DBusMessageIter iter, iter_array;
172   SpiRegistry *reg = SPI_REGISTRY (user_data);
173
174   reply = dbus_message_new_method_return (message);
175
176   dbus_message_iter_init_append (reply, &iter);
177   dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &iter_array);
178   g_sequence_foreach (reg->apps, add_bus_name_cb, &iter_array);
179   dbus_message_iter_close_container(&iter, &iter_array);
180   return reply;
181 }
182
183 /*---------------------------------------------------------------------------*/
184
185 static DBusMessage*
186 impl_RegisterApplication (DBusConnection *bus, DBusMessage *message, void *user_data)
187 {
188   gchar *app_name;
189   SpiRegistry *reg = SPI_REGISTRY (user_data);
190
191   if (dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &app_name, DBUS_TYPE_INVALID))
192       add_application(bus, reg, app_name);
193   return NULL;
194 }
195
196 static DBusMessage*
197 impl_DeregisterApplication (DBusConnection *bus, DBusMessage *message, void *user_data)
198 {
199   gchar *app_name;
200   SpiRegistry *reg = SPI_REGISTRY (user_data);
201
202   if (dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &app_name, DBUS_TYPE_INVALID))
203       remove_application(bus, reg, app_name);
204   return NULL;
205 }
206
207 /*---------------------------------------------------------------------------*/
208
209 static void
210 handle_disconnection (DBusConnection *bus, DBusMessage *message, void *user_data)
211 {
212   char *name, *old, *new;
213   SpiRegistry *reg = SPI_REGISTRY (user_data);
214
215   if (dbus_message_get_args (message, NULL,
216                              DBUS_TYPE_STRING, &name,
217                              DBUS_TYPE_STRING, &old,
218                              DBUS_TYPE_STRING, &new,
219                              DBUS_TYPE_INVALID))
220     {
221       if (*old != '\0' && *new == '\0')
222         {
223           remove_application(bus, reg, old);
224         }
225     }
226 }
227
228 /*---------------------------------------------------------------------------*/
229
230 static DBusHandlerResult
231 signal_filter (DBusConnection *bus, DBusMessage *message, void *user_data)
232 {
233   SpiRegistry *registry = SPI_REGISTRY (user_data);
234   guint res = DBUS_HANDLER_RESULT_HANDLED;
235   const char *iface = dbus_message_get_interface (message);
236   const char *member = dbus_message_get_member (message);
237
238   if (!g_strcmp0(iface, DBUS_INTERFACE_DBUS) && !g_strcmp0(member, "NameOwnerChanged"))
239       handle_disconnection (bus, message, user_data);
240   else
241       res = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
242
243   return res;
244 }
245
246 /*---------------------------------------------------------------------------*/
247
248 #define _SPI_DEBUG(format, args...) g_print (format , ## args)
249
250 static gchar *app_sig_match_name_owner =
251        "type='signal', interface='org.freedesktop.DBus', member='NameOwnerChanged'";
252
253 static DBusHandlerResult
254 handle_registry_method (DBusConnection *bus, DBusMessage *message, void *user_data)
255 {
256   const gchar *iface   = dbus_message_get_interface (message);
257   const gchar *member  = dbus_message_get_member (message);
258   const gint   type    = dbus_message_get_type (message);
259
260   DBusMessage *reply = NULL;
261
262   /* Check for basic reasons not to handle */
263   if (type   != DBUS_MESSAGE_TYPE_METHOD_CALL ||
264       member == NULL ||
265       iface  == NULL)
266       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
267
268   if (strcmp (iface, SPI_DBUS_INTERFACE_REGISTRY))
269       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
270
271   if      (!strcmp (member, "GetApplications"))
272       reply = impl_GetApplications (bus, message, user_data);
273   else if (!strcmp (member, "RegisterApplication"))
274       reply = impl_RegisterApplication (bus, message, user_data);
275   else if (!strcmp (member, "DeregisterApplication"))
276       reply = impl_DeregisterApplication (bus, message, user_data);
277   else
278       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
279
280   if (!reply)
281     {
282       reply = dbus_message_new_method_return (message);
283     }
284   dbus_connection_send (bus, reply, NULL);
285   dbus_message_unref (reply);
286   
287   return DBUS_HANDLER_RESULT_HANDLED;
288 }
289
290 static DBusObjectPathVTable registry_vtable =
291 {
292   NULL,
293   &handle_registry_method,
294   NULL, NULL, NULL, NULL
295 };
296
297 SpiRegistry *
298 spi_registry_new (DBusConnection *bus)
299 {
300   SpiRegistry *reg = g_object_new (SPI_REGISTRY_TYPE, NULL);
301
302   reg->bus = bus;
303
304   dbus_bus_add_match (bus, app_sig_match_name_owner, NULL);
305   dbus_connection_add_filter (bus, signal_filter, reg, NULL);
306
307   dbus_connection_register_object_path (bus, SPI_DBUS_PATH_REGISTRY, &registry_vtable, reg);
308
309   return reg;
310 }
311
312 /*END------------------------------------------------------------------------*/