2004-02-11 Padraig O'Briain <padraig.obriain@sun.com>
[platform/core/uifw/at-spi2-atk.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 #include <locale.h>
31 #include "spi-private.h"
32
33 /* Our parent Gtk object type */
34 #define PARENT_TYPE SPI_ACCESSIBLE_TYPE
35
36 /* A pointer to our parent object class */
37 static SpiAccessibleClass *spi_application_parent_class;
38
39 static SpiApplication *the_app;
40
41 /* static methods */
42
43 static void notify_listeners (GList *listeners,
44                               SpiAccessible *source,
45                               Accessibility_Event *e);
46
47 /*
48  * Implemented GObject::finalize
49  */
50 static void
51 spi_accessible_application_finalize (GObject *object)
52 {
53   GList *l;
54   SpiApplication *application = (SpiApplication *) object;
55   CORBA_Environment ev;
56
57   CORBA_exception_init (&ev);
58
59   for (l = application->toolkit_listeners; l; l = l->next)
60     {
61       CORBA_Object_release ((CORBA_Object) l->data, &ev);
62     }
63
64   CORBA_exception_free (&ev);
65
66   g_list_free (application->toolkit_listeners);
67   application->toolkit_listeners = NULL;
68
69   g_print ("application finalize called\n");
70   (G_OBJECT_CLASS (spi_application_parent_class))->finalize (object);
71 }
72
73 static CORBA_string
74 impl_accessibility_application_get_toolkit_name (PortableServer_Servant servant,
75                                                  CORBA_Environment     *ev)
76 {
77   return CORBA_string_dup (atk_get_toolkit_name ());
78 }
79
80 static CORBA_string
81 impl_accessibility_application_get_version (PortableServer_Servant servant,
82                                             CORBA_Environment     *ev)
83 {
84   return CORBA_string_dup (atk_get_toolkit_version ());
85 }
86
87 static CORBA_long
88 impl_accessibility_application_get_id (PortableServer_Servant servant,
89                                        CORBA_Environment     *ev)
90 {
91   SpiApplication *application = SPI_APPLICATION (
92     bonobo_object_from_servant (servant));
93
94   return application->id;
95 }
96
97 static void
98 impl_accessibility_application_set_id (PortableServer_Servant servant,
99                                        const CORBA_long id,
100                                        CORBA_Environment *ev)
101 {
102   SpiApplication *application = SPI_APPLICATION (
103     bonobo_object_from_servant (servant));
104
105   application->id = id;
106 }
107
108 static AtkObject *
109 get_atk_object_ref (GObject *gobject)
110 {
111   AtkObject *aobject;
112
113   if (ATK_IS_IMPLEMENTOR (gobject))
114     {
115       aobject = atk_implementor_ref_accessible (ATK_IMPLEMENTOR (gobject));
116     }
117   else if (ATK_IS_OBJECT (gobject))
118     {
119       aobject = ATK_OBJECT (gobject);
120       g_object_ref (G_OBJECT (aobject));
121     }
122   else
123     {
124       aobject = NULL;
125       g_error ("received event from non-AtkImplementor");
126     }
127
128   return aobject;
129 }
130
131 static gboolean
132 spi_application_toolkit_event_listener (GSignalInvocationHint *signal_hint,
133                                         guint                  n_param_values,
134                                         const GValue          *param_values,
135                                         gpointer               data)
136 {
137   Accessibility_Event e;
138   AtkObject      *aobject;
139   SpiAccessible *source;
140   GSignalQuery   signal_query;
141   char          *event_name;
142
143   g_return_val_if_fail (the_app != NULL, FALSE);
144
145   g_signal_query (signal_hint->signal_id, &signal_query);
146
147   /* TODO: move GTK reference out of app.c into bridge */
148   event_name = g_strdup_printf ("Gtk:%s:%s",
149                                 g_type_name (signal_query.itype), 
150                                 signal_query.signal_name);
151
152 #ifdef SPI_DEBUG  
153   fprintf (stderr, "Received signal %s\n", event_name);
154 #endif
155   
156   aobject = get_atk_object_ref (g_value_get_object (param_values + 0));
157
158   source = spi_accessible_new (aobject);
159   e.type = CORBA_string_dup (event_name);
160   e.source = CORBA_OBJECT_NIL;
161   e.detail1 = 0;
162   e.detail2 = 0;
163   spi_init_any_nil (&e.any_data);
164   notify_listeners (the_app->toolkit_listeners, source, &e);
165
166   bonobo_object_unref (BONOBO_OBJECT (source));
167   g_object_unref (G_OBJECT (aobject));
168
169   g_free (event_name);
170
171   return TRUE;
172 }
173
174 static CORBA_string
175 impl_accessibility_application_get_locale (PortableServer_Servant servant,
176                                            Accessibility_LOCALE_TYPE lctype,
177                                            CORBA_Environment *ev)
178 {
179     int category;
180     switch (lctype) 
181     {
182         case Accessibility_LOCALE_TYPE_COLLATE:
183             category = LC_COLLATE;
184             break;
185         case Accessibility_LOCALE_TYPE_CTYPE:
186             category = LC_CTYPE;
187             break;
188         case Accessibility_LOCALE_TYPE_MONETARY:
189             category = LC_MONETARY;
190             break;
191         case Accessibility_LOCALE_TYPE_NUMERIC:
192             category = LC_NUMERIC;
193             break;
194         case Accessibility_LOCALE_TYPE_MESSAGES:
195         default:
196             category = LC_MESSAGES;
197             break;
198     }
199     return CORBA_string_dup (setlocale (category, NULL));
200 }
201
202 static void
203 impl_accessibility_application_register_toolkit_event_listener (PortableServer_Servant servant,
204                                                                 Accessibility_EventListener listener,
205                                                                 const CORBA_char *event_name,
206                                                                 CORBA_Environment *ev)
207 {
208   guint spi_listener_id;
209   spi_listener_id =
210      atk_add_global_event_listener (spi_application_toolkit_event_listener, event_name);
211   the_app->toolkit_listeners = g_list_append (the_app->toolkit_listeners,
212                                               CORBA_Object_duplicate (listener, ev));
213 #ifdef SPI_DEBUG
214   fprintf (stderr, "registered %d for toolkit events named: %s\n",
215            spi_listener_id,
216            event_name);
217 #endif
218 }
219
220 static void
221 notify_listeners (GList *listeners, SpiAccessible *source, Accessibility_Event *e)
222 {
223   GList *l;
224   CORBA_Environment ev;
225
226   CORBA_exception_init (&ev);
227
228   for (l = listeners; l; l = l->next)
229     {
230       Accessibility_EventListener listener = l->data;
231
232       e->source = bonobo_object_dup_ref (BONOBO_OBJREF (source), &ev);
233
234       Accessibility_EventListener_notifyEvent (listener, e, &ev);
235       /*
236        * when this (oneway) call completes, the CORBA refcount and
237        * Bonobo_Unknown refcount will be decremented by the recipient
238        */
239       CORBA_exception_free (&ev);
240     }
241 }
242
243 static void
244 init_toolkit_names (GHashTable **generic_event_names, GHashTable **toolkit_event_names)
245 {
246         *toolkit_event_names = g_hash_table_new (g_str_hash, g_str_equal);
247         *generic_event_names = g_hash_table_new (g_str_hash, g_str_equal);
248         g_hash_table_insert (*toolkit_event_names,
249                              "object:property-change",
250                              "Gtk:AtkObject:property-change");
251         g_hash_table_insert (*generic_event_names,
252                              "Gtk:AtkObject:property-change",
253                              "object:property-change");
254 #ifdef SPI_DEBUG
255         fprintf (stderr, "inserted spi_selection_changed hash\n");
256 #endif
257 }
258
259 static void
260 spi_application_class_init (SpiApplicationClass *klass)
261 {
262   GObjectClass * object_class = (GObjectClass *) klass;
263   POA_Accessibility_Application__epv *epv = &klass->epv;
264
265   spi_application_parent_class = g_type_class_ref (SPI_ACCESSIBLE_TYPE);
266
267   object_class->finalize = spi_accessible_application_finalize;
268
269   epv->_get_toolkitName = impl_accessibility_application_get_toolkit_name;
270   epv->_get_version = impl_accessibility_application_get_version;
271   epv->_get_id = impl_accessibility_application_get_id;
272   epv->_set_id = impl_accessibility_application_set_id;
273   epv->registerToolkitEventListener = impl_accessibility_application_register_toolkit_event_listener;
274   epv->getLocale = impl_accessibility_application_get_locale;
275   init_toolkit_names (&klass->generic_event_names, &klass->toolkit_event_names);
276 }
277
278 static void
279 spi_application_init (SpiApplication *application)
280 {
281   application->toolkit_listeners = NULL;
282   the_app = application;
283 }
284
285 BONOBO_TYPE_FUNC_FULL (SpiApplication,
286                        Accessibility_Application,
287                        PARENT_TYPE, spi_application)
288
289 SpiApplication *
290 spi_application_new (AtkObject *app_root)
291 {
292   return SPI_APPLICATION (spi_accessible_construct (
293                           SPI_APPLICATION_TYPE, app_root));
294 }