File: bonobo-generic-factory.c

package info (click to toggle)
bonobo 1.0.22-2.2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 13,412 kB
  • ctags: 5,445
  • sloc: ansic: 51,714; sh: 7,733; makefile: 1,425; yacc: 318; xml: 266; sed: 16
file content (353 lines) | stat: -rw-r--r-- 10,972 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * bonobo-generic-factory.c: a GenericFactory object.
 *
 * The BonoboGenericFactory object is used to instantiate new
 * Bonobo::GenericFactory objects.  It acts as a wrapper for the
 * Bonobo::GenericFactory CORBA interface, and dispatches to
 * a user-specified factory function whenever its create_object()
 * method is invoked.
 *
 * Author:
 *   Miguel de Icaza (miguel@kernel.org)
 *
 * Copyright 1999 Helix Code, Inc.
 */
#include <config.h>
#include <gtk/gtksignal.h>
#include <gtk/gtkmarshal.h>
#include <bonobo/Bonobo.h>
#include <bonobo/bonobo-main.h>
#include <bonobo/bonobo-exception.h>
#include <bonobo/bonobo-generic-factory.h>
#include <bonobo/bonobo-running-context.h>
#include <liboaf/liboaf.h>

POA_GNOME_ObjectFactory__vepv bonobo_generic_factory_vepv;

static BonoboObjectClass *bonobo_generic_factory_parent_class = NULL;

static CORBA_boolean
impl_Bonobo_ObjectFactory_manufactures (PortableServer_Servant  servant,
					 const CORBA_char       *obj_oaf_iid,
					 CORBA_Environment      *ev)
{
	BonoboGenericFactory *factory = BONOBO_GENERIC_FACTORY (bonobo_object_from_servant (servant));

	if (! strcmp (obj_oaf_iid, factory->oaf_iid))
		return CORBA_TRUE;

	return CORBA_FALSE;
}

static CORBA_Object
impl_Bonobo_ObjectFactory_create_object (PortableServer_Servant   servant,
					  const CORBA_char        *obj_oaf_iid,
					  const GNOME_stringlist *params,
					  CORBA_Environment       *ev)
{
	BonoboGenericFactoryClass *class;
	BonoboGenericFactory      *factory;
	BonoboObject              *object;

	factory = BONOBO_GENERIC_FACTORY (bonobo_object_from_servant (servant));

	class = BONOBO_GENERIC_FACTORY_CLASS (GTK_OBJECT (factory)->klass);
	object = (*class->new_generic) (factory, obj_oaf_iid);

	if (!object)
		return CORBA_OBJECT_NIL;
	
	return CORBA_Object_duplicate (bonobo_object_corba_objref (BONOBO_OBJECT (object)), ev);
}

CORBA_Object
bonobo_generic_factory_corba_object_create (BonoboObject *object, 
					    gpointer shlib_id)
{
	POA_GNOME_ObjectFactory *servant;
	CORBA_Environment ev;
	
	CORBA_exception_init (&ev);

	servant = (POA_GNOME_ObjectFactory *) g_new0 (BonoboObjectServant, 1);
	servant->vepv = &bonobo_generic_factory_vepv;

	POA_GNOME_ObjectFactory__init ((PortableServer_Servant) servant, &ev);
	if (BONOBO_EX (&ev)) {
		g_free (servant);
		CORBA_exception_free (&ev);
		return CORBA_OBJECT_NIL;
	}

	CORBA_exception_free (&ev);
	
	return bonobo_object_activate_servant_full (object, servant, shlib_id);
}


BonoboGenericFactory *
bonobo_generic_factory_construct_noregister (const char             *oaf_iid,
					     BonoboGenericFactory   *c_factory,
					     CORBA_Object            corba_factory,
					     BonoboGenericFactoryFn  factory,
					     GnomeFactoryCallback    factory_cb,
					     gpointer                user_data)
{
	g_return_val_if_fail (c_factory != NULL, NULL);
	g_return_val_if_fail (BONOBO_IS_GENERIC_FACTORY (c_factory), NULL);
	g_return_val_if_fail (corba_factory != CORBA_OBJECT_NIL, NULL);

	bonobo_object_construct (BONOBO_OBJECT (c_factory), corba_factory);

	bonobo_running_context_ignore_object (corba_factory);

	c_factory->factory         = factory;
	c_factory->factory_cb      = factory_cb;
	c_factory->factory_closure = user_data;
	c_factory->oaf_iid         = g_strdup (oaf_iid);

	return c_factory;
}


/**
 * bonobo_generic_factory_construct:
 * @oaf_iid: The GOAD id that the new factory will implement.
 * @c_factory: The object to be initialized.
 * @corba_factory: The CORBA object which supports the
 * Bonobo::GenericFactory interface and which will be used to
 * construct this BonoboGenericFactory Gtk object.
 * @factory: A callback which is used to create new GnomeGeneric object instances.
 * @factory_cb: A Multi object factory callback.
 * @user_data: The closure data to be passed to the @factory callback routine.
 *
 * Initializes @c_factory with the command-line arguments and registers
 * the new factory in the name server.
 *
 * Returns: The initialized BonoboGenericFactory object.
 */
BonoboGenericFactory *
bonobo_generic_factory_construct (const char             *oaf_iid,
				  BonoboGenericFactory   *c_factory,
				  CORBA_Object            corba_factory,
				  BonoboGenericFactoryFn  factory,
				  GnomeFactoryCallback    factory_cb,
				  gpointer                user_data)
{
	int ret;
	
	g_return_val_if_fail (c_factory != NULL, NULL);
	g_return_val_if_fail (BONOBO_IS_GENERIC_FACTORY (c_factory), NULL);
	g_return_val_if_fail (corba_factory != CORBA_OBJECT_NIL, NULL);

	bonobo_generic_factory_construct_noregister (
		oaf_iid, c_factory, corba_factory, factory, factory_cb, user_data);

	ret = oaf_active_server_register (c_factory->oaf_iid, corba_factory);

	if (ret != OAF_REG_SUCCESS) {
		bonobo_object_unref (BONOBO_OBJECT (c_factory));
		return NULL;
	} 
	
	return c_factory;
}

/**
 * bonobo_generic_factory_new:
 * @oaf_iid: The GOAD id that this factory implements
 * @factory: A callback which is used to create new BonoboObject instances.
 * @user_data: The closure data to be passed to the @factory callback routine.
 *
 * This is a helper routine that simplifies the creation of factory
 * objects for GNOME objects.  The @factory function will be
 * invoked by the CORBA server when a request arrives to create a new
 * instance of an object supporting the Bonobo::Generic interface.
 * The factory callback routine is passed the @data pointer to provide
 * the creation function with some state information.
 *
 * Returns: A BonoboGenericFactory object that has an activated
 * Bonobo::GenericFactory object that has registered with the GNOME
 * name server.
 */
BonoboGenericFactory *
bonobo_generic_factory_new (const char             *oaf_iid,
			    BonoboGenericFactoryFn  factory,
			    gpointer                user_data)
{
	BonoboGenericFactory *c_factory;
	GNOME_ObjectFactory corba_factory;

	g_return_val_if_fail (factory != NULL, NULL);
	
	c_factory = gtk_type_new (bonobo_generic_factory_get_type ());

	corba_factory = bonobo_generic_factory_corba_object_create (BONOBO_OBJECT (c_factory), factory);
	if (corba_factory == CORBA_OBJECT_NIL) {
		bonobo_object_unref (BONOBO_OBJECT (c_factory));
		return NULL;
	}
	
	return bonobo_generic_factory_construct (
		oaf_iid, c_factory, corba_factory, factory, NULL, user_data);
}

/**
 * bonobo_generic_factory_new:
 * @oaf_iid: The GOAD id that this factory implements
 * @factory_cb: A callback which is used to create new BonoboObject instances.
 * @data: The closure data to be passed to the @factory callback routine.
 *
 * This is a helper routine that simplifies the creation of factory
 * objects for GNOME objects.  The @factory function will be
 * invoked by the CORBA server when a request arrives to create a new
 * instance of an object supporting the Bonobo::Generic interface.
 * The factory callback routine is passed the @data pointer to provide
 * the creation function with some state information.
 *
 * Returns: A BonoboGenericFactory object that has an activated
 * Bonobo::GenericFactory object that has registered with the GNOME
 * name server.
 */
BonoboGenericFactory *bonobo_generic_factory_new_multi (
	const char *oaf_iid,
	GnomeFactoryCallback factory_cb,
	gpointer data)
{
	BonoboGenericFactory *c_factory;
	GNOME_ObjectFactory corba_factory;

	g_return_val_if_fail (factory_cb != NULL, NULL);
	g_return_val_if_fail (oaf_iid != NULL, NULL);
	
	c_factory = gtk_type_new (bonobo_generic_factory_get_type ());

	corba_factory = bonobo_generic_factory_corba_object_create (BONOBO_OBJECT (c_factory), factory_cb);
	if (corba_factory == CORBA_OBJECT_NIL) {
		bonobo_object_unref (BONOBO_OBJECT (c_factory));
		return NULL;
	}
	
	return bonobo_generic_factory_construct (
		oaf_iid, c_factory, corba_factory, NULL, factory_cb, data);
}


static void
bonobo_generic_factory_finalize (GtkObject *object)
{
	BonoboGenericFactory *c_factory G_GNUC_UNUSED = BONOBO_GENERIC_FACTORY (object);
	CORBA_Environment ev;

	CORBA_exception_init (&ev);
	oaf_active_server_unregister (c_factory->oaf_iid,
				      BONOBO_OBJECT (c_factory)->corba_objref);
	CORBA_exception_free (&ev);
	g_free (c_factory->oaf_iid);
	
	GTK_OBJECT_CLASS (bonobo_generic_factory_parent_class)->finalize (object);
}

static BonoboObject *
bonobo_generic_factory_new_generic (BonoboGenericFactory *factory,
				    const char           *oaf_iid)
{
	g_return_val_if_fail (factory != NULL, NULL);
	g_return_val_if_fail (BONOBO_IS_GENERIC_FACTORY (factory), NULL);

	if (factory->factory_cb)
		return factory->factory_cb (factory, oaf_iid,
					    factory->factory_closure);
	else
		return factory->factory    (factory, factory->factory_closure);
}

static void
init_generic_factory_corba_class (void)
{
	bonobo_generic_factory_vepv.GNOME_ObjectFactory_epv = bonobo_generic_factory_get_epv ();
}

static void
bonobo_generic_factory_class_init (BonoboGenericFactoryClass *klass)
{
	GtkObjectClass *object_class = (GtkObjectClass *) klass;

	bonobo_generic_factory_parent_class = gtk_type_class (bonobo_object_get_type ());

	object_class->finalize = bonobo_generic_factory_finalize;

	klass->new_generic = bonobo_generic_factory_new_generic;

	init_generic_factory_corba_class ();
}

/**
 * bonobo_generic_factory_get_type:
 *
 * Returns: The GtkType of the BonoboGenericFactory class.
 */
GtkType
bonobo_generic_factory_get_type (void)
{
	static GtkType type = 0;

	if (!type){
		GtkTypeInfo info = {
			"BonoboGenericFactory",
			sizeof (BonoboGenericFactory),
			sizeof (BonoboGenericFactoryClass),
			(GtkClassInitFunc) bonobo_generic_factory_class_init,
			(GtkObjectInitFunc) NULL,
			NULL, /* reserved 1 */
			NULL, /* reserved 2 */
			(GtkClassInitFunc) NULL
		};

		type = gtk_type_unique (bonobo_object_get_type (), &info);
	}

	return type;
}

/**
 * bonobo_generic_factory_set:
 * @c_factory: The BonoboGenericFactory object whose callback will be set.
 * @factory: A callback routine which is used to create new object instances.
 * @data: The closure data to be passed to the @factory callback.
 *
 * Sets the callback and callback closure for @c_factory to
 * @factory and @data, respectively.
 */
void
bonobo_generic_factory_set (BonoboGenericFactory     *c_factory,
			      BonoboGenericFactoryFn  factory,
			      void                   *data)
{
	g_return_if_fail (c_factory != NULL);
	g_return_if_fail (BONOBO_IS_GENERIC_FACTORY (c_factory));
	g_return_if_fail (factory != NULL);

	c_factory->factory = factory;
	c_factory->factory_closure = data;
}


/**
 * bonobo_generic_factory_get_epv:
 *
 * Returns: The EPV for the default BonoboGenericFactory implementation.  
 */
POA_GNOME_ObjectFactory__epv *
bonobo_generic_factory_get_epv (void)
{
	POA_GNOME_ObjectFactory__epv *epv;

	epv = g_new0 (POA_GNOME_ObjectFactory__epv, 1);

	epv->manufactures  = impl_Bonobo_ObjectFactory_manufactures;
	epv->create_object = impl_Bonobo_ObjectFactory_create_object;

	return epv;
}