Merge branch 'master' of ssh://git.codethink.co.uk/git/atspi-dbus
[platform/core/uifw/at-spi2-atk.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 <spi-common/spi-dbus.h>
27 #include <dbus/dbus-glib-lowlevel.h>
28
29 #include "registry.h"
30
31 /*---------------------------------------------------------------------------*/
32
33 G_DEFINE_TYPE(SpiRegistry, spi_registry, G_TYPE_OBJECT)
34
35 static void
36 spi_registry_class_init (SpiRegistryClass *klass)
37 {
38   GObjectClass * object_class = (GObjectClass *) klass;
39
40   spi_registry_parent_class = g_type_class_ref (G_TYPE_OBJECT);
41 }
42
43 static void
44 spi_registry_init (SpiRegistry *registry)
45 {
46   registry->apps = g_sequence_new (g_free);
47 }
48
49 /*---------------------------------------------------------------------------*/
50
51 static void emit(SpiRegistry *reg, const char *itf, const char *name, int ftype, ...)
52 {
53   va_list arg;
54
55   va_start(arg, ftype);
56   spi_dbus_emit_valist(reg->bus, SPI_DBUS_PATH_DEC, itf, name, ftype, arg);
57   va_end(arg);
58 }
59
60 /*---------------------------------------------------------------------------*/
61
62 static void
63 add_bus_name_cb (gpointer item, gpointer data)
64 {
65   DBusMessageIter *iter_array = (DBusMessageIter *) data;
66
67   dbus_message_iter_append_basic (iter_array, DBUS_TYPE_STRING, (gchar **) &item);
68 }
69
70 static DBusMessage *
71 impl_getApplications (DBusConnection *bus, DBusMessage *message, void *user_data)
72 {
73   DBusMessage *reply;
74   DBusMessageIter iter, iter_array;
75   SpiRegistry *reg = SPI_REGISTRY (user_data);
76
77   reply = dbus_message_new_method_return (message);
78
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);
83   return reply;
84 }
85
86 static DBusHandlerResult
87 message_handler (DBusConnection *bus, DBusMessage *message, void *user_data)
88 {
89   DBusMessage *reply = NULL;
90
91   if (dbus_message_is_method_call (message, SPI_DBUS_INTERFACE_REGISTRY, "getApplications"))
92     {
93       reply = impl_getApplications (bus, message, user_data);
94       if (reply)
95         {
96           dbus_connection_send (bus, reply, NULL);
97           dbus_message_unref (reply);
98         }
99       return DBUS_HANDLER_RESULT_HANDLED;
100     }
101   else
102     {
103       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
104     }
105 }
106
107 /*---------------------------------------------------------------------------*/
108
109 static gint
110 data_str_cmp (gpointer a, gpointer b, gpointer data)
111 {
112   return g_strcmp0(a, b);
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 handle_register_application (SpiRegistry *reg, DBusMessage *message)
139 {
140   gchar *app_name;
141
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);
144 }
145
146 static void
147 handle_deregister_application (SpiRegistry *reg, DBusMessage *message)
148 {
149   gchar *app_name;
150
151   if (dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &app_name, DBUS_TYPE_INVALID))
152       seq_remove_string (reg->apps, app_name);
153 }
154
155 static void
156 handle_disconnection (SpiRegistry *reg, DBusMessage *message)
157 {
158   char *name, *old, *new;
159
160   if (dbus_message_get_args (message, NULL,
161                              DBUS_TYPE_STRING, &name,
162                              DBUS_TYPE_STRING, &old,
163                              DBUS_TYPE_STRING, &new,
164                              DBUS_TYPE_INVALID))
165   {
166     if (*old != '\0' && *new == '\0')
167     {
168       if (seq_remove_string (reg->apps, old))
169         {
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);*/
173         }
174     }
175   }
176 }
177
178 static DBusHandlerResult
179 signal_handler (DBusConnection *bus, DBusMessage *message, void *user_data)
180 {
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);
185
186   g_print ("\n%s", iface);
187   g_print ("\n%d", dbus_message_get_type (message));
188   g_print ("\n%s\n", member);
189
190 #if 0
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);
197   else
198       res = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
199 #endif
200
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);
207   else
208       res = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
209
210   return res;
211 }
212
213 /*---------------------------------------------------------------------------*/
214
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'";
217
218 static gchar *app_sig_match_blank = "";
219
220 static DBusObjectPathVTable reg_vtable =
221 {
222   NULL,
223   &message_handler,
224   NULL, NULL, NULL, NULL
225 };
226
227 SpiRegistry *
228 spi_registry_new (DBusConnection *bus)
229 {
230   SpiRegistry *reg = g_object_new (SPI_REGISTRY_TYPE, NULL);
231
232   reg->bus = bus;
233
234   dbus_connection_register_object_path(bus, SPI_DBUS_PATH_REGISTRY, &reg_vtable, reg);
235
236   //dbus_bus_add_match (bus, app_reg_sig_match, NULL);
237   //dbus_bus_add_match (bus, app_dereg_sig_match, NULL);
238   dbus_bus_add_match (bus, app_sig_match_blank, NULL);
239   dbus_connection_add_filter (bus, signal_handler, reg, NULL);
240
241   return reg;
242 }