File: bonobo-event-source.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 (453 lines) | stat: -rw-r--r-- 11,137 bytes parent folder | download
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * bonobo-event-source.c: Generic event emitter.
 *
 * Author:
 *	Alex Graveley (alex@helixcode.com)
 *	Iain Holmes   (iain@helixcode.com)
 *      docs, Miguel de Icaza (miguel@helixcode.com)
 *
 * Copyright (C) 2000, Helix Code, Inc.
 */
#include <config.h>
#include <gtk/gtksignal.h>
#include <bonobo/bonobo-listener.h>
#include <bonobo/bonobo-exception.h>
#include <bonobo/bonobo-event-source.h>
#include <bonobo/bonobo-running-context.h>
#include <time.h>

#define PARENT_TYPE BONOBO_X_OBJECT_TYPE

static GtkObjectClass *bonobo_event_source_parent_class;

struct _BonoboEventSourcePrivate {
	GSList  *listeners;  /* CONTAINS: ListenerDesc* */
	gboolean ignore;
	gint     counter;    /* to create unique listener Ids */
};

typedef struct {
	Bonobo_Listener listener;
	Bonobo_EventSource_ListenerId id;
	gchar **event_masks; /* send all events if NULL */
} ListenerDesc;

/*
 * tries to make a unique connection Id. Adding the time make an
 * accidental remove of a listener more unlikely.
 */
static Bonobo_EventSource_ListenerId
create_listener_id (BonoboEventSource *source)
{
	guint32 id;

	source->priv->counter = source->priv->counter++ & 0x0000ffff;

	if (!source->priv->counter) source->priv->counter++;

	id = source->priv->counter | (time(NULL) << 16);

	return id;
}

static inline BonoboEventSource * 
bonobo_event_source_from_servant (PortableServer_Servant servant)
{
	return BONOBO_EVENT_SOURCE (bonobo_object_from_servant (servant));
}

static void
desc_free (ListenerDesc *desc, CORBA_Environment *ev)
{
	if (desc) {
		g_strfreev (desc->event_masks);
		bonobo_object_release_unref (desc->listener, ev);
		g_free (desc);
	}
}

static Bonobo_EventSource_ListenerId
impl_Bonobo_EventSource_addListenerWithMask (PortableServer_Servant servant,
					     const Bonobo_Listener  l,
					     const CORBA_char      *event_mask,
					     CORBA_Environment     *ev)
{
	BonoboEventSource *event_source;
	ListenerDesc      *desc;

	g_return_val_if_fail (!CORBA_Object_is_nil (l, ev), 0);

	event_source = bonobo_event_source_from_servant (servant);

	if (event_source->priv->ignore) /* Hook for running context */
		bonobo_running_context_ignore_object (l);

	desc = g_new0 (ListenerDesc, 1);
	desc->listener = bonobo_object_dup_ref (l, ev);
	desc->id = create_listener_id (event_source);

	if (event_mask)
		desc->event_masks = g_strsplit (event_mask, ",", 0);

	event_source->priv->listeners = g_slist_prepend (
		event_source->priv->listeners, desc);

	return desc->id;
}

static Bonobo_EventSource_ListenerId
impl_Bonobo_EventSource_addListener (PortableServer_Servant servant,
				     const Bonobo_Listener  l,
				     CORBA_Environment     *ev)
{
	return impl_Bonobo_EventSource_addListenerWithMask (
		servant, l, NULL, ev);
}

static void
impl_Bonobo_EventSource_removeListener (PortableServer_Servant servant,
					const Bonobo_EventSource_ListenerId id,
					CORBA_Environment     *ev)
{
	GSList                   *l, *next;
	BonoboEventSourcePrivate *priv;

	priv = bonobo_event_source_from_servant (servant)->priv;

	for (l = priv->listeners; l; l = next) {
		ListenerDesc *desc = l->data;

		next = l->next;

		if (desc->id == id) {
			priv->listeners = g_slist_remove_link (
				priv->listeners, l);
			g_slist_free_1 (l);
			desc_free (desc, ev);
			return;
		}
	}

	CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
			     ex_Bonobo_EventSource_UnknownListener, 
			     NULL);
}

/*
 * if the mask starts with a '=', we do exact compares - else we only check 
 * if the mask is a prefix of name.
 */
static gboolean
event_match (const char *name, gchar **event_masks)
{
	int i = 0, j = 0;

	while (event_masks[j]) {
		char *mask = event_masks[j];
		
		if (mask [0] == '=')
			if (!strcmp (name, mask + 1))
				return TRUE;

		while (name [i] && mask [i] && name [i] == mask [i])
			i++;
		
		if (mask [i] == '\0')
			return TRUE;

		j++;
	}
	
	return FALSE;
} 

/**
 * bonobo_event_source_notify_listeners:
 * @event_source: the Event Source that will emit the event.
 * @event_name: Name of the event being emitted
 * @value: A CORBA_any value that contains the data that is passed to interested clients
 * @opt_ev: A CORBA_Environment where a failure code can be returned, can be NULL.
 *
 * This will notify all clients that have registered with this EventSource
 * (through the addListener or addListenerWithMask methods) of the availability
 * of the event named @event_name.  The @value CORBA::any value is passed to
 * all listeners.
 *
 * @event_name can not contain comma separators, as commas are used to
 * separate the various event names. 
 */
void
bonobo_event_source_notify_listeners (BonoboEventSource *event_source,
				      const char        *event_name,
				      const CORBA_any   *value,
				      CORBA_Environment *opt_ev)
{
	GSList *l, *notify;
	CORBA_Environment ev, *my_ev;

	if (!opt_ev) {
		CORBA_exception_init (&ev);
		my_ev = &ev;
	} else
		my_ev = opt_ev;

	notify = NULL;

	for (l = event_source->priv->listeners; l; l = l->next) {
		ListenerDesc *desc = (ListenerDesc *) l->data;

		if (desc->event_masks == NULL ||
		    event_match (event_name, desc->event_masks)) {
			CORBA_Object_duplicate (desc->listener, my_ev);
			notify = g_slist_prepend (notify, desc->listener);
		}
	}

	bonobo_object_ref (BONOBO_OBJECT (event_source));

	for (l = notify; l; l = l->next) {
		Bonobo_Listener_event (l->data, event_name, value, my_ev);
		CORBA_Object_release (l->data, my_ev);
	}

	bonobo_object_unref (BONOBO_OBJECT (event_source));

	g_slist_free (notify);

	if (!opt_ev)
		CORBA_exception_free (&ev);
}

void
bonobo_event_source_notify_listeners_full (BonoboEventSource *event_source,
					   const char        *path,
					   const char        *type,
					   const char        *subtype,
					   const CORBA_any   *value,                          
					   CORBA_Environment *opt_ev)
{
	char *event_name;

	event_name = bonobo_event_make_name (path, type, subtype);

	bonobo_event_source_notify_listeners (event_source, event_name,
					      value, opt_ev);

	g_free (event_name);
}

static void
bonobo_event_source_destroy (GtkObject *object)
{
	CORBA_Environment         ev;
	BonoboEventSourcePrivate *priv;
	
	priv = BONOBO_EVENT_SOURCE (object)->priv;

	CORBA_exception_init (&ev);
	
	while (priv->listeners) {
		ListenerDesc *d = priv->listeners->data;

		priv->listeners = g_slist_remove (
			priv->listeners, d);

		desc_free (d, &ev);
	}

	CORBA_exception_free (&ev);

	g_free (priv);

	bonobo_event_source_parent_class->destroy (object);
}

static void
bonobo_event_source_class_init (BonoboEventSourceClass *klass)
{
	GtkObjectClass *oclass = (GtkObjectClass *) klass;
	POA_Bonobo_EventSource__epv *epv = &klass->epv;

	bonobo_event_source_parent_class = gtk_type_class (PARENT_TYPE);

	oclass->destroy = bonobo_event_source_destroy;

	epv->addListener         = impl_Bonobo_EventSource_addListener;
	epv->addListenerWithMask = impl_Bonobo_EventSource_addListenerWithMask;
	epv->removeListener      = impl_Bonobo_EventSource_removeListener;
}

static void
bonobo_event_source_init (GtkObject *object)
{
	BonoboEventSource *event_source;

	event_source = BONOBO_EVENT_SOURCE (object);
	event_source->priv = g_new0 (BonoboEventSourcePrivate, 1);
	event_source->priv->listeners = NULL;
}

BONOBO_X_TYPE_FUNC_FULL (BonoboEventSource, 
			   Bonobo_EventSource,
			   PARENT_TYPE,
			   bonobo_event_source);

/**
 * bonobo_event_source_new:
 *
 * Creates a new BonoboEventSource object.  Typically this
 * object will be exposed to clients through CORBA and they
 * will register and unregister functions to be notified
 * of events that this EventSource generates.
 * 
 * To notify clients of an event, use the bonobo_event_source_notify_listeners()
 * function.
 *
 * Returns: A new #BonoboEventSource server object.
 */
BonoboEventSource *
bonobo_event_source_new (void)
{
	return gtk_type_new (BONOBO_EVENT_SOURCE_TYPE);
}

/**
 * bonobo_event_source_ignore_listeners:
 * @event_source: 
 * 
 *  Instructs the event source to de-register any listeners
 * that are added from the global running context.
 **/
void
bonobo_event_source_ignore_listeners (BonoboEventSource *event_source)
{
	g_return_if_fail (BONOBO_IS_EVENT_SOURCE (event_source));

	event_source->priv->ignore = TRUE;
}

void
bonobo_event_source_client_remove_listener (Bonobo_Unknown  object,
					    Bonobo_EventSource_ListenerId id,
					    CORBA_Environment *opt_ev)
{
	CORBA_Environment ev, *my_ev;
	Bonobo_Unknown es;

	g_return_if_fail (object != CORBA_OBJECT_NIL);
	g_return_if_fail (id != 0);

	if (!opt_ev) {
		CORBA_exception_init (&ev);
		my_ev = &ev;
	} else
		my_ev = opt_ev;

	if (CORBA_Object_is_a (object, "IDL:Bonobo/Property:1.0", my_ev)) {

		Bonobo_Property_removeListener (object, id, my_ev);

	} else { 

		es = Bonobo_Unknown_queryInterface (object, 
		       "IDL:Bonobo/EventSource:1.0", my_ev);

		if (BONOBO_EX(my_ev) || !es)
			goto remove_listener_end;

		Bonobo_EventSource_removeListener (es, id, my_ev);

		Bonobo_Unknown_unref (es, my_ev);
	}

 remove_listener_end:

	if (!opt_ev) {
		if (BONOBO_EX (my_ev)) {
			char *text;
			
			text = bonobo_exception_get_text (my_ev);
			g_warning ("remove_listener failed '%s'", text);
			g_free (text);
		}
		
		CORBA_exception_free (&ev);
	}
}

Bonobo_EventSource_ListenerId
bonobo_event_source_client_add_listener (Bonobo_Unknown object,
					 BonoboListenerCallbackFn event_callback,
					 const char *opt_mask,
					 CORBA_Environment *opt_ev,
					 gpointer user_data)
{
	CORBA_Environment ev, *my_ev;
	BonoboListener *listener = NULL;
	Bonobo_Listener corba_listener;
	Bonobo_EventSource_ListenerId id = 0;
	Bonobo_Unknown es;

	g_return_val_if_fail (object != CORBA_OBJECT_NIL, 0);
	g_return_val_if_fail (event_callback != NULL, 0);
	
	if (!opt_ev) {
		CORBA_exception_init (&ev);
		my_ev = &ev;
	} else
		my_ev = opt_ev;

	if (CORBA_Object_is_a (object, "IDL:Bonobo/Property:1.0", my_ev)) {
		if (!(listener = bonobo_listener_new (event_callback, 
						      user_data)))
			goto add_listener_end;
	      
		corba_listener = BONOBO_OBJREF (listener);

		id = Bonobo_Property_addListener (object, corba_listener, 
						  my_ev);

		bonobo_object_unref (BONOBO_OBJECT (listener));

	} else {
		es = Bonobo_Unknown_queryInterface (object, 
		        "IDL:Bonobo/EventSource:1.0", my_ev);

		if (BONOBO_EX(my_ev) || !es)
			goto add_listener_end;

		if (!(listener = bonobo_listener_new (event_callback, 
						      user_data)))
			goto add_listener_end;

		corba_listener = BONOBO_OBJREF (listener);
	
		if (opt_mask)
			id = Bonobo_EventSource_addListenerWithMask (es, 
			        corba_listener, opt_mask, my_ev);
		else 
			id = Bonobo_EventSource_addListener (es, 
				corba_listener, my_ev);

		bonobo_object_unref (BONOBO_OBJECT (listener));
		Bonobo_Unknown_unref (es, my_ev);
	}

 add_listener_end:

	if (!opt_ev) {
		if (BONOBO_EX (my_ev)) {
			char *text;
			
			text = bonobo_exception_get_text (my_ev);
			g_warning ("add_listener failed '%s'", text);
			g_free (text);
		}
		
		CORBA_exception_free (&ev);
	}

	return id;
}