File: bonobo-shlib-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 (285 lines) | stat: -rw-r--r-- 8,500 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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/**
 * bonobo-shlib-factory.c: a ShlibFactory object.
 *
 * The BonoboShlibFactory object is used to instantiate new
 * Bonobo::ShlibFactory objects.  It acts as a wrapper for the
 * Bonobo::ShlibFactory 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-shlib-factory.h>
#include <bonobo/bonobo-running-context.h>
#include <liboaf/liboaf.h>

POA_GNOME_ObjectFactory__vepv bonobo_shlib_factory_vepv;

static BonoboObjectClass *bonobo_shlib_factory_parent_class = NULL;

/**
 * bonobo_shlib_factory_construct:
 * @c_factory: The object to be initialized.
 * @oaf_iid: The GOAD id that the new factory will implement.
 * @oaf_impl_ptr: Oaf shlib handle
 * @corba_factory: The CORBA object which supports the
 * Bonobo::ShlibFactory interface and which will be used to
 * construct this BonoboShlibFactory Gtk object.
 * @factory: A callback which is used to create new GnomeShlib object instances.
 * @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 BonoboShlibFactory object.
 */
BonoboShlibFactory *
bonobo_shlib_factory_construct (BonoboShlibFactory    *c_factory,
				const char            *oaf_iid,
				PortableServer_POA     poa,
				gpointer               oaf_impl_ptr,
				CORBA_Object           corba_factory,
				BonoboGenericFactoryFn factory,
				GnomeFactoryCallback   factory_cb,
				gpointer               user_data)
{
	BonoboGenericFactory *r_factory;

	g_return_val_if_fail (c_factory != NULL, NULL);
	g_return_val_if_fail (BONOBO_IS_SHLIB_FACTORY (c_factory), NULL);
	g_return_val_if_fail (corba_factory != CORBA_OBJECT_NIL, NULL);

	c_factory->live_objects = 0;
	c_factory->oaf_impl_ptr = oaf_impl_ptr;

        oaf_plugin_use (poa, oaf_impl_ptr);

	r_factory = bonobo_generic_factory_construct_noregister (
		oaf_iid, BONOBO_GENERIC_FACTORY (c_factory),
		corba_factory, factory, factory_cb, user_data);

	return (BonoboShlibFactory *) r_factory;
}

/**
 * bonobo_shlib_factory_new:
 * @oaf_iid: The GOAD id that this factory implements
 * @poa: the poa.
 * @oaf_impl_ptr: Oaf shlib handle
 * @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::Shlib interface.
 * The factory callback routine is passed the @data pointer to provide
 * the creation function with some state information.
 *
 * Returns: A BonoboShlibFactory object that has an activated
 * Bonobo::ShlibFactory object that has registered with the GNOME
 * name server.
 */
BonoboShlibFactory *
bonobo_shlib_factory_new (const char            *oaf_iid,
			  PortableServer_POA     poa,
			  gpointer               oaf_impl_ptr,
			  BonoboGenericFactoryFn factory,
			  gpointer               user_data)
{
	BonoboShlibFactory *c_factory;
	GNOME_ObjectFactory corba_factory;

	g_return_val_if_fail (factory != NULL, NULL);
	
	c_factory = gtk_type_new (bonobo_shlib_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_shlib_factory_construct (
		c_factory, oaf_iid, poa, oaf_impl_ptr,
		corba_factory, factory, NULL, user_data);
}

/**
 * bonobo_shlib_factory_new_multi:
 * @oaf_iid: The GOAD id that this factory implements
 * @poa: the poa.
 * @oaf_impl_ptr: Oaf shlib handle
 * @factory_cb: 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::Shlib interface.
 * The factory callback routine is passed the @data pointer to provide
 * the creation function with some state information.
 *
 * Returns: A BonoboShlibFactory object that has an activated
 * Bonobo::ShlibFactory object that has registered with the GNOME
 * name server.
 */
BonoboShlibFactory *bonobo_shlib_factory_new_multi (
	const char          *oaf_iid,
	PortableServer_POA   poa,
	gpointer             oaf_impl_ptr,
	GnomeFactoryCallback factory_cb,
	gpointer             user_data)
{
	BonoboShlibFactory *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_shlib_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_shlib_factory_construct (
		c_factory, oaf_iid, poa, oaf_impl_ptr,
		corba_factory, NULL, factory_cb, user_data);
}

static void
bonobo_shlib_factory_finalize (GtkObject *object)
{
/*	BonoboShlibFactory *c_factory = BONOBO_SHLIB_FACTORY (object);*/

	/*
	 * We pray this happens only when we have released our
	 * last ref and no one is holding pointers into the soon
	 * to be unloaded shlib, particularly the stack.
	 *
	 * This is achieved by an idle unref handler.
	 */

	/* we dont unload it because of a problem with the gtk type system */
	/* oaf_plugin_unuse (c_factory->oaf_impl_ptr); */

	GTK_OBJECT_CLASS (bonobo_shlib_factory_parent_class)->finalize (object);
}

static BonoboObject *
bonobo_shlib_factory_new_generic (BonoboGenericFactory *factory,
				  const char           *oaf_iid)
{
	BonoboObject *retval;

	retval = BONOBO_GENERIC_FACTORY_CLASS (
		bonobo_shlib_factory_parent_class)->new_generic (factory, oaf_iid);

	return retval;
}

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

	bonobo_shlib_factory_parent_class = gtk_type_class (
		bonobo_generic_factory_get_type ());

	klass->new_generic = bonobo_shlib_factory_new_generic;

	object_class->finalize = bonobo_shlib_factory_finalize;
}

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

	if (!type) {
		GtkTypeInfo info = {
			"BonoboShlibFactory",
			sizeof (BonoboShlibFactory),
			sizeof (BonoboShlibFactoryClass),
			(GtkClassInitFunc)  bonobo_shlib_factory_class_init,
			(GtkObjectInitFunc) NULL,
			NULL, /* reserved 1 */
			NULL, /* reserved 2 */
			(GtkClassInitFunc) NULL
		};

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

	return type;
}

void
bonobo_shlib_factory_inc_live (BonoboShlibFactory *factory)
{
	g_return_if_fail (BONOBO_IS_SHLIB_FACTORY (factory));

	factory->live_objects++;
}


static gboolean
bonobo_shlib_factory_dec_live_cb (BonoboShlibFactory *factory)
{
	factory->live_objects--;

	if (factory->live_objects <= 0)
		bonobo_object_unref (BONOBO_OBJECT (factory));

	return FALSE;
}

void
bonobo_shlib_factory_dec_live (BonoboShlibFactory *factory)
{
	g_return_if_fail (BONOBO_IS_SHLIB_FACTORY (factory));

	g_idle_add ((GSourceFunc) bonobo_shlib_factory_dec_live_cb, factory);
}

static void
destroy_handler (GtkObject *object, BonoboShlibFactory *factory)
{
	bonobo_shlib_factory_dec_live (factory);
}

void
bonobo_shlib_factory_track_object (BonoboShlibFactory *factory,
				   BonoboObject       *object)
{
	g_return_if_fail (BONOBO_IS_OBJECT (object));
	g_return_if_fail (BONOBO_IS_SHLIB_FACTORY (factory));

	bonobo_shlib_factory_inc_live (factory);

	gtk_signal_connect (GTK_OBJECT (object), "destroy",
			    destroy_handler, factory);
}