2002-03-14 Marc Mulcahy <marc.mulcahy@sun.com>
[platform/core/uifw/at-spi2-atk.git] / registryd / desktop.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2001 Sun Microsystems Inc., Ximian Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /* desktop.c: implements SpiDesktop.idl */
24
25 #include <config.h>
26 #include <stdio.h>
27 #include <libbonobo.h>
28 #include "desktop.h"
29
30 /* SpiDesktop signals */
31 enum {
32   APPLICATION_ADDED,
33   APPLICATION_REMOVED,  
34 LAST_SIGNAL
35 };
36 static guint spi_desktop_signals[LAST_SIGNAL];
37
38
39 /* Our parent Gtk object type */
40 #define PARENT_TYPE SPI_ACCESSIBLE_TYPE
41
42 typedef struct {
43         SpiDesktop *desktop;
44         Accessibility_Application ref;
45 } Application;
46
47 /* A pointer to our parent object class */
48 static SpiAccessibleClass *parent_class;
49
50 static void
51 spi_desktop_init (SpiDesktop *desktop)
52 {
53   spi_base_construct (SPI_BASE (desktop), g_object_new (ATK_TYPE_OBJECT, NULL));
54
55   desktop->applications = NULL;
56
57   atk_object_set_name (ATK_OBJECT (SPI_BASE (desktop)->gobj), "main");
58 }
59
60 static void
61 spi_desktop_dispose (GObject *object)
62 {
63   SpiDesktop *desktop = (SpiDesktop *) object;
64
65   while (desktop->applications)
66     {
67       Application *app = (Application *) desktop->applications;
68       spi_desktop_remove_application (desktop, app->ref);
69     }
70
71   G_OBJECT_CLASS (parent_class)->dispose (object); 
72 }
73
74 static CORBA_long
75 impl_desktop_get_child_count (PortableServer_Servant servant,
76                               CORBA_Environment     *ev)
77 {
78   SpiDesktop *desktop = SPI_DESKTOP (bonobo_object_from_servant (servant));
79
80   if (desktop->applications)
81     {
82       return g_list_length (desktop->applications);
83     }
84   else
85     {
86       return 0;
87     }
88 }
89
90 static Accessibility_Accessible
91 impl_desktop_get_child_at_index (PortableServer_Servant servant,
92                                  const CORBA_long       index,
93                                  CORBA_Environment     *ev)
94 {
95   SpiDesktop  *desktop = SPI_DESKTOP (bonobo_object_from_servant (servant));
96   CORBA_Object retval;
97   Application *app;
98
99   app = g_list_nth_data (desktop->applications, index);
100
101   if (app)
102     {
103       retval = bonobo_object_dup_ref (app->ref, ev);
104       if (BONOBO_EX (ev))
105         {
106           retval = CORBA_OBJECT_NIL;
107         }
108     }
109   else
110     {
111       retval = CORBA_OBJECT_NIL;
112     }
113
114   return (Accessibility_Accessible) retval;
115 }
116
117 static void
118 spi_desktop_class_init (SpiDesktopClass *klass)
119 {
120   GObjectClass * object_class = (GObjectClass *) klass;
121   SpiAccessibleClass * spi_accessible_class = (SpiAccessibleClass *) klass;
122   POA_Accessibility_Accessible__epv *epv = &spi_accessible_class->epv;
123
124   object_class->dispose = spi_desktop_dispose;
125   
126   parent_class = g_type_class_ref (SPI_ACCESSIBLE_TYPE);
127
128   spi_desktop_signals[APPLICATION_ADDED] =
129     g_signal_new ("application_added",
130                   G_TYPE_FROM_CLASS (klass),
131                   G_SIGNAL_RUN_LAST,
132                   G_STRUCT_OFFSET (SpiDesktopClass, application_added),
133                   NULL, NULL,
134                   g_cclosure_marshal_VOID__UINT,
135                   G_TYPE_NONE,
136                   1, G_TYPE_UINT);
137   spi_desktop_signals[APPLICATION_REMOVED] =
138     g_signal_new ("application_removed",
139                   G_TYPE_FROM_CLASS (klass),
140                   G_SIGNAL_RUN_LAST,
141                   G_STRUCT_OFFSET (SpiDesktopClass, application_removed),
142                   NULL, NULL,
143                   g_cclosure_marshal_VOID__UINT,
144                   G_TYPE_NONE,
145                   1, G_TYPE_UINT);
146   epv->_get_childCount = impl_desktop_get_child_count;
147   epv->getChildAtIndex = impl_desktop_get_child_at_index;
148 }
149
150 BONOBO_TYPE_FUNC_FULL (SpiDesktop,
151                        Accessibility_Desktop,
152                        PARENT_TYPE,
153                        spi_desktop);
154
155 SpiDesktop *
156 spi_desktop_new (void)
157 {
158   SpiDesktop *retval = g_object_new (SPI_DESKTOP_TYPE, NULL);
159
160   return retval;
161 }
162
163 static void
164 abnormal_application_termination (gpointer object, Application *app)
165 {
166   g_return_if_fail (SPI_IS_DESKTOP (app->desktop));
167
168   spi_desktop_remove_application (app->desktop, app->ref);
169 }
170
171 void
172 spi_desktop_add_application (SpiDesktop *desktop,
173                              const Accessibility_Application application)
174 {
175   CORBA_Environment ev;
176   Application       *app;
177   Accessibility_Application ref;
178
179   g_return_if_fail (SPI_IS_DESKTOP (desktop));
180
181   spi_desktop_remove_application (desktop, application);
182
183   CORBA_exception_init (&ev);
184
185   ref = bonobo_object_dup_ref (application, &ev);
186
187   if (!BONOBO_EX (&ev))
188     {
189       app = g_new (Application, 1);
190       app->desktop = desktop;
191       app->ref = ref;
192
193       desktop->applications = g_list_append (desktop->applications, app);
194       g_signal_emit (G_OBJECT (desktop), spi_desktop_signals[APPLICATION_ADDED], 0,
195                      g_list_index (desktop->applications, app));
196
197       ORBit_small_listen_for_broken (app->ref, G_CALLBACK (abnormal_application_termination), app);
198     }
199
200   CORBA_exception_free (&ev);
201 }
202
203 void
204 spi_desktop_remove_application (SpiDesktop *desktop,
205                                 const Accessibility_Application app_ref)
206 {
207   GList *l;
208   CORBA_Environment ev;
209
210   g_return_if_fail (SPI_IS_DESKTOP (desktop));
211
212   CORBA_exception_init (&ev);
213
214   for (l = desktop->applications; l; l = l->next)
215     {
216       Application *app = (Application *) l->data;
217
218       if (CORBA_Object_is_equivalent (app->ref, app_ref, &ev))
219         {
220           break;
221         }
222     }
223
224   CORBA_exception_free (&ev);
225
226   if (l)
227     {
228       Application *app = (Application *) l->data;
229
230       g_signal_emit (G_OBJECT (desktop), spi_desktop_signals[APPLICATION_REMOVED], 0,
231                      g_list_index (desktop->applications, l));
232       desktop->applications = g_list_delete_link (desktop->applications, l);
233
234       ORBit_small_unlisten_for_broken (app->ref, G_CALLBACK (abnormal_application_termination));
235       bonobo_object_release_unref (app->ref, NULL);
236       g_free (app);
237     }
238 }