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