Fixes for 98836, 98842, added slots to IDL for ABI freeze.
[platform/upstream/at-spi2-core.git] / libspi / application.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2001, 2002 Sun Microsystems Inc.,
6  * Copyright 2001, 2002 Ximian, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /* application.c: implements SpiApplication.idl */
25
26 #include <string.h>
27 #include <config.h>
28 #include <atk/atkutil.h>
29 #include <libspi/application.h>
30
31 /* Our parent Gtk object type */
32 #define PARENT_TYPE SPI_ACCESSIBLE_TYPE
33
34 /* A pointer to our parent object class */
35 static SpiAccessibleClass *spi_application_parent_class;
36
37 static SpiApplication *the_app;
38
39 /* static methods */
40
41 static void notify_listeners (GList *listeners,
42                               SpiAccessible *source,
43                               Accessibility_Event *e);
44
45 static const char *reverse_lookup_name_for_toolkit_event (char *toolkit_name);
46
47 static const char *
48 lookup_toolkit_event_for_name (const char *generic_name)
49 {
50   char *toolkit_specific_name;
51   SpiApplicationClass *klass = g_type_class_peek (SPI_APPLICATION_TYPE);
52 #ifdef SPI_DEBUG
53   fprintf (stderr, "looking for %s in hash table.\n", generic_name);
54 #endif
55   toolkit_specific_name =
56     (char *) g_hash_table_lookup (klass->toolkit_event_names, generic_name);
57 #ifdef SPI_DEBUG
58   fprintf (stderr, "generic event %s converted to %s\n", generic_name, toolkit_specific_name);
59 #endif
60   return toolkit_specific_name;
61 }
62
63 /*
64  * Implemented GObject::finalize
65  */
66 static void
67 spi_accessible_application_finalize (GObject *object)
68 {
69   GList *l;
70   SpiApplication *application = (SpiApplication *) object;
71   CORBA_Environment ev;
72
73   CORBA_exception_init (&ev);
74
75   for (l = application->toolkit_listeners; l; l = l->next)
76     {
77       CORBA_Object_release ((CORBA_Object) l->data, &ev);
78     }
79
80   CORBA_exception_free (&ev);
81
82   g_list_free (application->toolkit_listeners);
83   application->toolkit_listeners = NULL;
84
85   g_print ("application finalize called\n");
86   (G_OBJECT_CLASS (spi_application_parent_class))->finalize (object);
87 }
88
89 static CORBA_string
90 impl_accessibility_application_get_toolkit_name (PortableServer_Servant servant,
91                                                  CORBA_Environment     *ev)
92 {
93   return CORBA_string_dup (atk_get_toolkit_name ());
94 }
95
96 static CORBA_string
97 impl_accessibility_application_get_version (PortableServer_Servant servant,
98                                             CORBA_Environment     *ev)
99 {
100   return CORBA_string_dup (atk_get_toolkit_version ());
101 }
102
103 static CORBA_long
104 impl_accessibility_application_get_id (PortableServer_Servant servant,
105                                        CORBA_Environment     *ev)
106 {
107   SpiApplication *application = SPI_APPLICATION (
108     bonobo_object_from_servant (servant));
109
110   return application->id;
111 }
112
113 static void
114 impl_accessibility_application_set_id (PortableServer_Servant servant,
115                                        const CORBA_long id,
116                                        CORBA_Environment *ev)
117 {
118   SpiApplication *application = SPI_APPLICATION (
119     bonobo_object_from_servant (servant));
120
121   application->id = id;
122 }
123
124 static AtkObject *
125 get_atk_object_ref (GObject *gobject)
126 {
127   AtkObject *aobject;
128
129   if (ATK_IS_IMPLEMENTOR (gobject))
130     {
131       aobject = atk_implementor_ref_accessible (ATK_IMPLEMENTOR (gobject));
132     }
133   else if (ATK_IS_OBJECT (gobject))
134     {
135       aobject = ATK_OBJECT (gobject);
136       g_object_ref (G_OBJECT (aobject));
137     }
138   else
139     {
140       aobject = NULL;
141       g_error ("received event from non-AtkImplementor");
142     }
143
144   return aobject;
145 }
146
147 static gboolean
148 spi_application_object_event_listener (GSignalInvocationHint *signal_hint,
149                                        guint                   n_param_values,
150                                        const GValue           *param_values,
151                                        gpointer                data)
152 {
153   Accessibility_Event e;
154   AtkObject     *aobject;
155   SpiAccessible *source;
156   GSignalQuery   signal_query;
157   gchar         *event_name;
158   const char    *generic_name;
159
160   g_return_val_if_fail (the_app != NULL, FALSE);
161   
162   g_signal_query (signal_hint->signal_id, &signal_query);
163
164   /* TODO: move GTK reference out of app.c into bridge */
165   event_name = g_strdup_printf ("Gtk:%s:%s",
166                                 g_type_name (signal_query.itype),
167                                 signal_query.signal_name);
168
169   generic_name = reverse_lookup_name_for_toolkit_event (event_name);
170
171   fprintf (stderr, "Received (object) signal %s maps to '%s'\n",
172            event_name, generic_name);
173
174   g_free (event_name);
175
176   g_return_val_if_fail (generic_name, FALSE);
177
178   aobject = get_atk_object_ref (g_value_get_object (param_values + 0));
179
180   source = spi_accessible_new (aobject);
181   e.type = CORBA_string_dup (generic_name);
182   e.source = CORBA_OBJECT_NIL;
183   e.detail1 = 0;
184   e.detail2 = 0;
185   spi_init_any_nil (&e.any_data);
186   notify_listeners (the_app->toolkit_listeners, source, &e);
187
188   bonobo_object_unref (BONOBO_OBJECT (source));
189
190   g_object_unref (G_OBJECT (aobject));
191
192   return TRUE;
193 }
194
195 static gboolean
196 spi_application_toolkit_event_listener (GSignalInvocationHint *signal_hint,
197                                         guint                  n_param_values,
198                                         const GValue          *param_values,
199                                         gpointer               data)
200 {
201   Accessibility_Event e;
202   AtkObject      *aobject;
203   SpiAccessible *source;
204   GSignalQuery   signal_query;
205   char          *event_name;
206
207   g_return_val_if_fail (the_app != NULL, FALSE);
208
209   g_signal_query (signal_hint->signal_id, &signal_query);
210
211   /* TODO: move GTK reference out of app.c into bridge */
212   event_name = g_strdup_printf ("Gtk:%s:%s",
213                                 g_type_name (signal_query.itype), 
214                                 signal_query.signal_name);
215
216 #ifdef SPI_DEBUG  
217   fprintf (stderr, "Received signal %s\n", event_name);
218 #endif
219   
220   aobject = get_atk_object_ref (g_value_get_object (param_values + 0));
221
222   source = spi_accessible_new (aobject);
223   e.type = CORBA_string_dup (event_name);
224   e.source = CORBA_OBJECT_NIL;
225   e.detail1 = 0;
226   e.detail2 = 0;
227   spi_init_any_nil (&e.any_data);
228   notify_listeners (the_app->toolkit_listeners, source, &e);
229
230   bonobo_object_unref (BONOBO_OBJECT (source));
231   g_object_unref (G_OBJECT (aobject));
232
233   g_free (event_name);
234
235   return TRUE;
236 }
237
238 static void
239 impl_accessibility_application_register_toolkit_event_listener (PortableServer_Servant servant,
240                                                                 Accessibility_EventListener listener,
241                                                                 const CORBA_char *event_name,
242                                                                 CORBA_Environment *ev)
243 {
244   guint spi_listener_id;
245   spi_listener_id =
246      atk_add_global_event_listener (spi_application_toolkit_event_listener, event_name);
247   the_app->toolkit_listeners = g_list_append (the_app->toolkit_listeners,
248                                               CORBA_Object_duplicate (listener, ev));
249 #ifdef SPI_DEBUG
250   fprintf (stderr, "registered %d for toolkit events named: %s\n",
251            spi_listener_id,
252            event_name);
253 #endif
254 }
255
256 static void
257 impl_accessibility_application_register_object_event_listener (PortableServer_Servant servant,
258                                                                Accessibility_EventListener listener,
259                                                                const CORBA_char *event_name,
260                                                                CORBA_Environment *ev)
261 {
262   guint spi_listener_id = 0;
263   const char *toolkit_specific_event_name =
264           lookup_toolkit_event_for_name (event_name);
265   if (toolkit_specific_event_name)
266   {
267     spi_listener_id =
268        atk_add_global_event_listener (spi_application_object_event_listener,
269                                       toolkit_specific_event_name);
270     the_app->toolkit_listeners = g_list_append (the_app->toolkit_listeners,
271                                               CORBA_Object_duplicate (listener, ev));
272   }
273 #ifdef SPI_DEBUG
274   fprintf (stderr, "registered %d for object events named: %s\n",
275            spi_listener_id,
276            event_name);
277 #endif
278 }
279
280 static void
281 notify_listeners (GList *listeners, SpiAccessible *source, Accessibility_Event *e)
282 {
283   GList *l;
284   CORBA_Environment ev;
285
286   CORBA_exception_init (&ev);
287
288   for (l = listeners; l; l = l->next)
289     {
290       Accessibility_EventListener listener = l->data;
291
292       e->source = bonobo_object_dup_ref (BONOBO_OBJREF (source), &ev);
293
294       Accessibility_EventListener_notifyEvent (listener, e, &ev);
295       /*
296        * when this (oneway) call completes, the CORBA refcount and
297        * Bonobo_Unknown refcount will be decremented by the recipient
298        */
299       CORBA_exception_free (&ev);
300     }
301 }
302
303 static const char *
304 reverse_lookup_name_for_toolkit_event (char *toolkit_specific_name)
305 {
306     const char *generic_name;
307     SpiApplicationClass *klass = g_type_class_peek (SPI_APPLICATION_TYPE);
308 #ifdef SPI_DEBUG
309     fprintf (stderr, "(reverse lookup) looking for %s in hash table.\n", toolkit_specific_name);
310 #endif
311     generic_name =
312             (const char *) g_hash_table_lookup (klass->generic_event_names, toolkit_specific_name);
313 #ifdef SPI_DEBUG
314     fprintf (stderr, "toolkit event %s converted to %s\n", toolkit_specific_name, generic_name);
315 #endif
316     return generic_name;
317 }
318
319 static void
320 init_toolkit_names (GHashTable **generic_event_names, GHashTable **toolkit_event_names)
321 {
322         *toolkit_event_names = g_hash_table_new (g_str_hash, g_str_equal);
323         *generic_event_names = g_hash_table_new (g_str_hash, g_str_equal);
324         g_hash_table_insert (*toolkit_event_names,
325                              "object:property-change",
326                              "Gtk:AtkObject:property-change");
327         g_hash_table_insert (*generic_event_names,
328                              "Gtk:AtkObject:property-change",
329                              "object:property-change");
330 #ifdef SPI_DEBUG
331         fprintf (stderr, "inserted spi_selection_changed hash\n");
332 #endif
333 }
334
335 static void
336 spi_application_class_init (SpiApplicationClass *klass)
337 {
338   GObjectClass * object_class = (GObjectClass *) klass;
339   POA_Accessibility_Application__epv *epv = &klass->epv;
340
341   spi_application_parent_class = g_type_class_ref (SPI_ACCESSIBLE_TYPE);
342
343   object_class->finalize = spi_accessible_application_finalize;
344
345   epv->_get_toolkitName = impl_accessibility_application_get_toolkit_name;
346   epv->_get_version = impl_accessibility_application_get_version;
347   epv->_get_id = impl_accessibility_application_get_id;
348   epv->_set_id = impl_accessibility_application_set_id;
349   epv->registerToolkitEventListener = impl_accessibility_application_register_toolkit_event_listener;
350   init_toolkit_names (&klass->generic_event_names, &klass->toolkit_event_names);
351 }
352
353 static void
354 spi_application_init (SpiApplication *application)
355 {
356   application->toolkit_listeners = NULL;
357   the_app = application;
358 }
359
360 BONOBO_TYPE_FUNC_FULL (SpiApplication,
361                        Accessibility_Application,
362                        PARENT_TYPE, spi_application);
363
364 SpiApplication *
365 spi_application_new (AtkObject *app_root)
366 {
367   return SPI_APPLICATION (spi_accessible_construct (
368         SPI_APPLICATION_TYPE, app_root));
369 }