1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
---|
2 | /* |
---|
3 | * bonobo-object-client.c: |
---|
4 | * This handles the client-view of a remote Bonobo object. |
---|
5 | * |
---|
6 | * Author: |
---|
7 | * Miguel de Icaza (miguel@kernel.org) |
---|
8 | * |
---|
9 | * Copyright 1999 Helix Code, Inc. |
---|
10 | */ |
---|
11 | #include <bonobo/bonobo-object-client.h> |
---|
12 | #include <liboaf/liboaf.h> |
---|
13 | #include <liboaf/oaf-async.h> |
---|
14 | #include <bonobo/bonobo-stream.h> |
---|
15 | #include <bonobo/bonobo-exception.h> |
---|
16 | |
---|
17 | /* Internal data structure for bonobo_object_client_activate_async */ |
---|
18 | typedef struct { |
---|
19 | BonoboObjectClientAsyncCallback callback; |
---|
20 | gpointer user_data; |
---|
21 | } BonoboObjectClientAsyncCallbackData; |
---|
22 | |
---|
23 | static BonoboObjectClass *bonobo_object_client_parent_class; |
---|
24 | |
---|
25 | /** |
---|
26 | * bonobo_object_client_construct: |
---|
27 | * @object_client: The BonoboObjectClient object to construct |
---|
28 | * @corba_object: The remote CORBA object this object_client refers to |
---|
29 | * |
---|
30 | * Initializes @object_client with the CORBA object for the |
---|
31 | * Bonobo::Unknown interface provided in @corba_object. |
---|
32 | * |
---|
33 | * Returns: the initialized BonoboObjectClient object. |
---|
34 | */ |
---|
35 | BonoboObjectClient * |
---|
36 | bonobo_object_client_construct (BonoboObjectClient *object_client, CORBA_Object corba_object) |
---|
37 | { |
---|
38 | g_return_val_if_fail (BONOBO_IS_OBJECT_CLIENT (object_client), NULL); |
---|
39 | g_return_val_if_fail (corba_object != CORBA_OBJECT_NIL, NULL); |
---|
40 | |
---|
41 | BONOBO_OBJECT (object_client)->corba_objref = corba_object; |
---|
42 | |
---|
43 | return object_client; |
---|
44 | } |
---|
45 | |
---|
46 | /** |
---|
47 | * bonobo_object_activate: |
---|
48 | * @iid: an OAFIID |
---|
49 | * @oaf_flags: activation flags |
---|
50 | * |
---|
51 | * This activates the object from the IID using OAF; you probably |
---|
52 | * don't want to do this, you might want to use bonobo_get_object |
---|
53 | * which does object activation through the moniker system. |
---|
54 | * |
---|
55 | * You might also consider doing property-based activation using the |
---|
56 | * the OAF-based capabilities. |
---|
57 | * |
---|
58 | * Returns: An activated object client or NULL on failure. |
---|
59 | */ |
---|
60 | BonoboObjectClient * |
---|
61 | bonobo_object_activate (const char *iid, gint oaf_flags) |
---|
62 | { |
---|
63 | CORBA_Environment ev; |
---|
64 | BonoboObjectClient *object; |
---|
65 | Bonobo_Unknown corba_object; |
---|
66 | |
---|
67 | g_return_val_if_fail (iid != NULL, NULL); |
---|
68 | |
---|
69 | /* |
---|
70 | * We don't want this to do moniker activation too, since we'd |
---|
71 | * need to break the API to add 'requested_interface' and it's |
---|
72 | * a different paradigm. |
---|
73 | */ |
---|
74 | CORBA_exception_init (&ev); |
---|
75 | corba_object = oaf_activate_from_id ((gchar *) iid, oaf_flags, NULL, &ev); |
---|
76 | |
---|
77 | if (BONOBO_EX (&ev) || corba_object == CORBA_OBJECT_NIL) { |
---|
78 | CORBA_exception_free (&ev); |
---|
79 | return NULL; |
---|
80 | } |
---|
81 | |
---|
82 | CORBA_exception_free (&ev); |
---|
83 | |
---|
84 | object = gtk_type_new (bonobo_object_client_get_type ()); |
---|
85 | |
---|
86 | bonobo_object_client_construct (object, corba_object); |
---|
87 | |
---|
88 | return object; |
---|
89 | } |
---|
90 | |
---|
91 | static void |
---|
92 | oaf_activate_async_cb (CORBA_Object activated_object, |
---|
93 | const char *error, |
---|
94 | gpointer user_data) |
---|
95 | { |
---|
96 | BonoboObjectClientAsyncCallbackData *callback_data = |
---|
97 | (BonoboObjectClientAsyncCallbackData *) user_data; |
---|
98 | BonoboObjectClient *object; |
---|
99 | |
---|
100 | if (activated_object == CORBA_OBJECT_NIL) { |
---|
101 | callback_data->callback (NULL, error, callback_data->user_data); |
---|
102 | g_free(callback_data); |
---|
103 | return; |
---|
104 | } |
---|
105 | |
---|
106 | object = gtk_type_new (bonobo_object_client_get_type ()); |
---|
107 | bonobo_object_client_construct (object, activated_object); |
---|
108 | |
---|
109 | callback_data->callback (object, NULL, callback_data->user_data); |
---|
110 | |
---|
111 | g_free (callback_data); |
---|
112 | } |
---|
113 | |
---|
114 | /** |
---|
115 | * bonobo_object_activate_async: |
---|
116 | * @iid: an OAFIID |
---|
117 | * @oaf_flags: activation flags |
---|
118 | * @callback: a callback function |
---|
119 | * @user_data: user data |
---|
120 | * |
---|
121 | * This activates the object from the IID using OAF; you probably |
---|
122 | * don't want to do this; instead do capability based activation |
---|
123 | * using OAF directly. |
---|
124 | */ |
---|
125 | void |
---|
126 | bonobo_object_activate_async (const char *iid, |
---|
127 | gint oaf_flags, |
---|
128 | BonoboObjectClientAsyncCallback callback, |
---|
129 | gpointer user_data) |
---|
130 | { |
---|
131 | BonoboObjectClientAsyncCallbackData *callback_data; |
---|
132 | |
---|
133 | g_return_if_fail (iid != NULL); |
---|
134 | |
---|
135 | callback_data = g_new0 (BonoboObjectClientAsyncCallbackData, 1); |
---|
136 | callback_data->callback = callback; |
---|
137 | callback_data->user_data = user_data; |
---|
138 | |
---|
139 | oaf_activate_from_id_async ((gchar*) iid, oaf_flags, |
---|
140 | oaf_activate_async_cb, callback_data, NULL); |
---|
141 | } |
---|
142 | |
---|
143 | /** |
---|
144 | * bonobo_object_client_from_corba: |
---|
145 | * @corba_object: An existing CORBA object that implements Bonobo_Unknown. |
---|
146 | * |
---|
147 | * Wraps the @corba_object CORBA object reference in a BonoboObjectClient |
---|
148 | * object. This is typically used if you got a CORBA object yourself and not |
---|
149 | * through one of the activation routines and you want to have a BonoboObjectClient |
---|
150 | * handle to use in any of the Bonobo routines. |
---|
151 | * |
---|
152 | * Returns: A wrapped BonoboObjectClient. |
---|
153 | */ |
---|
154 | BonoboObjectClient * |
---|
155 | bonobo_object_client_from_corba (Bonobo_Unknown o) |
---|
156 | { |
---|
157 | BonoboObjectClient *object; |
---|
158 | |
---|
159 | g_return_val_if_fail (o != CORBA_OBJECT_NIL, NULL); |
---|
160 | |
---|
161 | object = gtk_type_new (bonobo_object_client_get_type ()); |
---|
162 | bonobo_object_client_construct (object, o); |
---|
163 | |
---|
164 | return object; |
---|
165 | } |
---|
166 | |
---|
167 | |
---|
168 | /** |
---|
169 | * bonobo_object_client_query_interface: |
---|
170 | * @object: object to query interface of |
---|
171 | * @interface_desc: interface description |
---|
172 | * @opt_ev: optional exception environment, or NULL |
---|
173 | * |
---|
174 | * Queries the object to see if it implements the interface |
---|
175 | * described by @interface_desc. Basically a thin |
---|
176 | * Bonobo_Unknown::query_interface wrapper. |
---|
177 | * |
---|
178 | * Return value: A valid Bonobo_Unknown reference or |
---|
179 | * CORBA_OBJECT_NIL if anything untoward happens. |
---|
180 | **/ |
---|
181 | Bonobo_Unknown |
---|
182 | bonobo_object_client_query_interface (BonoboObjectClient *object, |
---|
183 | const char *interface_desc, |
---|
184 | CORBA_Environment *opt_ev) |
---|
185 | { |
---|
186 | Bonobo_Unknown interface; |
---|
187 | CORBA_Environment *ev, real_ev; |
---|
188 | |
---|
189 | g_return_val_if_fail (BONOBO_IS_OBJECT_CLIENT (object), CORBA_OBJECT_NIL); |
---|
190 | g_return_val_if_fail (interface_desc != NULL, CORBA_OBJECT_NIL); |
---|
191 | |
---|
192 | if (opt_ev) |
---|
193 | ev = opt_ev; |
---|
194 | else { |
---|
195 | ev = &real_ev; |
---|
196 | CORBA_exception_init (ev); |
---|
197 | } |
---|
198 | |
---|
199 | interface = Bonobo_Unknown_queryInterface (BONOBO_OBJREF (object), |
---|
200 | interface_desc, ev); |
---|
201 | |
---|
202 | if (BONOBO_EX (ev)) { |
---|
203 | bonobo_object_check_env (BONOBO_OBJECT (object), |
---|
204 | BONOBO_OBJREF (object), ev); |
---|
205 | if (!opt_ev) |
---|
206 | CORBA_exception_free (ev); |
---|
207 | |
---|
208 | return CORBA_OBJECT_NIL; |
---|
209 | } |
---|
210 | |
---|
211 | if (!opt_ev) |
---|
212 | CORBA_exception_free (ev); |
---|
213 | |
---|
214 | return interface; |
---|
215 | } |
---|
216 | |
---|
217 | /** |
---|
218 | * bonobo_object_client_has_interface: |
---|
219 | * @object: object to query interface of |
---|
220 | * @interface_desc: interface description |
---|
221 | * @opt_ev: optional exception environment, or NULL |
---|
222 | * |
---|
223 | * Queries the object to see if it implements the interface |
---|
224 | * described by @interface_desc. Basically a thin |
---|
225 | * Bonobo_Unknown::query_interface wrapper. |
---|
226 | * |
---|
227 | * Return value: TRUE if the interface is available else FALSE. |
---|
228 | **/ |
---|
229 | gboolean |
---|
230 | bonobo_object_client_has_interface (BonoboObjectClient *object, |
---|
231 | const char *interface_desc, |
---|
232 | CORBA_Environment *opt_ev) |
---|
233 | { |
---|
234 | Bonobo_Unknown interface; |
---|
235 | |
---|
236 | /* safe type checking in query_interface */ |
---|
237 | interface = bonobo_object_client_query_interface (object, |
---|
238 | interface_desc, |
---|
239 | opt_ev); |
---|
240 | |
---|
241 | if (interface != CORBA_OBJECT_NIL) { |
---|
242 | CORBA_Environment *ev, real_ev; |
---|
243 | |
---|
244 | if (opt_ev) |
---|
245 | ev = opt_ev; |
---|
246 | else { |
---|
247 | ev = &real_ev; |
---|
248 | CORBA_exception_init (ev); |
---|
249 | } |
---|
250 | |
---|
251 | Bonobo_Unknown_unref (interface, ev); |
---|
252 | |
---|
253 | if (BONOBO_EX (ev)) { |
---|
254 | bonobo_object_check_env (BONOBO_OBJECT (object), |
---|
255 | BONOBO_OBJREF (object), ev); |
---|
256 | if (!opt_ev) |
---|
257 | CORBA_exception_free (ev); |
---|
258 | return FALSE; |
---|
259 | } |
---|
260 | |
---|
261 | CORBA_Object_release (interface, ev); |
---|
262 | |
---|
263 | if (BONOBO_EX (ev)) { |
---|
264 | bonobo_object_check_env (BONOBO_OBJECT (object), |
---|
265 | BONOBO_OBJREF (object), ev); |
---|
266 | if (!opt_ev) |
---|
267 | CORBA_exception_free (ev); |
---|
268 | return FALSE; |
---|
269 | } |
---|
270 | |
---|
271 | if (!opt_ev) |
---|
272 | CORBA_exception_free (ev); |
---|
273 | return TRUE; |
---|
274 | } else |
---|
275 | return FALSE; |
---|
276 | } |
---|
277 | |
---|
278 | /** |
---|
279 | * bonobo_object_client_ref: |
---|
280 | * @object_client: the object client |
---|
281 | * @opt_exception_obj: optional CORBA exception environment |
---|
282 | * |
---|
283 | * Increments the Bonobo ref count on the remote object. |
---|
284 | **/ |
---|
285 | void |
---|
286 | bonobo_object_client_ref (BonoboObjectClient *object_client, |
---|
287 | BonoboObject *opt_exception_obj) |
---|
288 | { |
---|
289 | CORBA_Environment ev; |
---|
290 | BonoboObject *object; |
---|
291 | |
---|
292 | g_return_if_fail (BONOBO_IS_OBJECT (object_client)); |
---|
293 | |
---|
294 | object = BONOBO_OBJECT (object_client); |
---|
295 | |
---|
296 | CORBA_exception_init (&ev); |
---|
297 | |
---|
298 | Bonobo_Unknown_ref (object->corba_objref, &ev); |
---|
299 | |
---|
300 | if (BONOBO_EX (&ev)) { |
---|
301 | CORBA_exception_free (&ev); |
---|
302 | bonobo_object_check_env (opt_exception_obj?opt_exception_obj:object, |
---|
303 | object->corba_objref, &ev); |
---|
304 | } |
---|
305 | |
---|
306 | CORBA_exception_free (&ev); |
---|
307 | } |
---|
308 | |
---|
309 | /** |
---|
310 | * bonobo_object_client_unref: |
---|
311 | * @object_client: the object client |
---|
312 | * @opt_exception_obj: optional CORBA exception environment |
---|
313 | * |
---|
314 | * Decrements the Bonobo ref count on the remote object. |
---|
315 | **/ |
---|
316 | void |
---|
317 | bonobo_object_client_unref (BonoboObjectClient *object_client, |
---|
318 | BonoboObject *opt_exception_obj) |
---|
319 | { |
---|
320 | CORBA_Environment ev; |
---|
321 | BonoboObject *object; |
---|
322 | |
---|
323 | g_return_if_fail (BONOBO_IS_OBJECT (object_client)); |
---|
324 | |
---|
325 | object = BONOBO_OBJECT (object_client); |
---|
326 | |
---|
327 | CORBA_exception_init (&ev); |
---|
328 | |
---|
329 | Bonobo_Unknown_unref (object->corba_objref, &ev); |
---|
330 | |
---|
331 | if (BONOBO_EX (&ev)) { |
---|
332 | CORBA_exception_free (&ev); |
---|
333 | bonobo_object_check_env (opt_exception_obj?opt_exception_obj:object, |
---|
334 | object->corba_objref, &ev); |
---|
335 | } |
---|
336 | |
---|
337 | CORBA_exception_free (&ev); |
---|
338 | } |
---|
339 | |
---|
340 | static void |
---|
341 | bonobo_object_client_destroy (GtkObject *object) |
---|
342 | { |
---|
343 | BonoboObject *bonobo_object = BONOBO_OBJECT (object); |
---|
344 | Bonobo_Unknown objref; |
---|
345 | |
---|
346 | objref = bonobo_object_corba_objref (bonobo_object); |
---|
347 | if (objref != CORBA_OBJECT_NIL) { |
---|
348 | CORBA_Environment ev; |
---|
349 | |
---|
350 | CORBA_exception_init (&ev); |
---|
351 | Bonobo_Unknown_unref (objref, &ev); |
---|
352 | CORBA_exception_free (&ev); |
---|
353 | } |
---|
354 | |
---|
355 | GTK_OBJECT_CLASS (bonobo_object_client_parent_class)->destroy (object); |
---|
356 | } |
---|
357 | |
---|
358 | static void |
---|
359 | bonobo_object_client_class_init (BonoboObjectClientClass *klass) |
---|
360 | { |
---|
361 | GtkObjectClass *object_class = (GtkObjectClass *) klass; |
---|
362 | |
---|
363 | bonobo_object_client_parent_class = gtk_type_class (bonobo_object_get_type ()); |
---|
364 | |
---|
365 | object_class->destroy = bonobo_object_client_destroy; |
---|
366 | } |
---|
367 | |
---|
368 | /** |
---|
369 | * bonobo_object_client_get_type: |
---|
370 | * |
---|
371 | * Returns: the GtkType for the BonoboObjectClient class. |
---|
372 | */ |
---|
373 | GtkType |
---|
374 | bonobo_object_client_get_type (void) |
---|
375 | { |
---|
376 | static GtkType type = 0; |
---|
377 | |
---|
378 | if (!type){ |
---|
379 | GtkTypeInfo info = { |
---|
380 | "Handle to remote Bonobo::Unknown", |
---|
381 | sizeof (BonoboObjectClient), |
---|
382 | sizeof (BonoboObjectClientClass), |
---|
383 | (GtkClassInitFunc) bonobo_object_client_class_init, |
---|
384 | (GtkObjectInitFunc) NULL, |
---|
385 | NULL, /* reserved 1 */ |
---|
386 | NULL, /* reserved 2 */ |
---|
387 | (GtkClassInitFunc) NULL |
---|
388 | }; |
---|
389 | |
---|
390 | type = gtk_type_unique (bonobo_object_get_type (), &info); |
---|
391 | } |
---|
392 | |
---|
393 | return type; |
---|
394 | } |
---|