File: bonobo-property-bag.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 (984 lines) | stat: -rw-r--r-- 25,138 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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
/*
 * bonobo-property-bag.c: property bag object implementation.
 *
 * Authors:
 *   Nat Friedman  (nat@helixcode.com)
 *   Michael Meeks (michael@helixcode.com)
 *
 * Copyright 1999, 2000 Helix Code, Inc.
 */
#include <config.h>
#include <bonobo/Bonobo.h>
#include <bonobo/bonobo-main.h>
#include <bonobo/bonobo-exception.h>
#include <bonobo/bonobo-property-bag.h>
#include <bonobo/bonobo-property.h>
#include <bonobo/bonobo-persist-stream.h>
#include <bonobo/bonobo-transient.h>

#define PARENT_TYPE BONOBO_X_OBJECT_TYPE

static GtkObjectClass *parent_class = NULL;
         

/*
 * Internal data structures.
 */
struct _BonoboPropertyBagPrivate {
	GHashTable         *props;

	BonoboPropertySetFn set_prop;
	BonoboPropertyGetFn get_prop;
	gpointer            user_data;

	BonoboTransient    *transient;
};


/* BonoboPropertyBag CORBA methods.*/
static void
bonobo_property_bag_foreach_create_list (gpointer key, 
					 gpointer value,
					 gpointer data)
{
	GList **l = (GList **) data;

	*l = g_list_prepend (*l, value);
}


/**
 * bonobo_property_bag_get_prop_list:
 * @pb: A #BonoboPropertyBag.
 *
 * Returns a #GList of #BonoboProperty structures.  This function is
 * private and should only be used internally, or in a PropertyBag
 * persistence implementation.  You should not touch the
 * #BonoboProperty structure unless you know what you're doing.
 */
GList *
bonobo_property_bag_get_prop_list (BonoboPropertyBag *pb)
{
	GList *l;

	g_return_val_if_fail (pb != NULL, NULL);
	g_return_val_if_fail (BONOBO_IS_PROPERTY_BAG (pb), NULL);

	l = NULL;

	g_hash_table_foreach (pb->priv->props,
			      bonobo_property_bag_foreach_create_list,
			      &l);

	return l;
}

static Bonobo_EventSource
impl_Bonobo_PropertyBag_getEventSource (PortableServer_Servant servant,
					CORBA_Environment      *ev)
{
	BonoboPropertyBag   *pb = BONOBO_PROPERTY_BAG (bonobo_object_from_servant (servant));

	return bonobo_object_dup_ref (BONOBO_OBJREF (pb->es), ev);
}

static Bonobo_PropertyList *
impl_Bonobo_PropertyBag_getProperties (PortableServer_Servant  servant,
				       CORBA_Environment      *ev)
{
	BonoboPropertyBag   *pb = BONOBO_PROPERTY_BAG (bonobo_object_from_servant (servant));
	Bonobo_PropertyList *prop_list;
	GList		   *props;
	GList		   *curr;
	int		    len;
	int		    i;

	/*
	 * Create the PropertyList and allocate space for the
	 * properties.
	 */
	len = g_hash_table_size (pb->priv->props);

	prop_list = Bonobo_PropertyList__alloc ();
	prop_list->_length = len;

	if (len == 0)
		return prop_list;

	prop_list->_buffer = CORBA_sequence_Bonobo_Property_allocbuf (len);

	/*
	 * Create a list of Object references for the properties.
	 */
	props = bonobo_property_bag_get_prop_list (pb);

	i = 0;
	for (curr = props; curr != NULL; curr = curr->next) {
		BonoboProperty *prop = curr->data;

		prop_list->_buffer [i] =  bonobo_transient_create_objref (
			pb->priv->transient, "IDL:Bonobo/Property:1.0",
			prop->name, ev);

		if (BONOBO_EX (ev)) {
			g_warning ("BonoboPropertyBag: Could not create property objref!");
			g_list_free (props);
			CORBA_free (prop_list);
			return CORBA_OBJECT_NIL;
		}

		i++;
		
	}

	g_list_free (props);

	return prop_list;
}

static Bonobo_Property
impl_Bonobo_PropertyBag_getPropertyByName (PortableServer_Servant servant,
					   const CORBA_char      *name,
					   CORBA_Environment     *ev)
{
	BonoboPropertyBag *pb = BONOBO_PROPERTY_BAG (bonobo_object_from_servant (servant));
	Bonobo_Property    prop;

	if (g_hash_table_lookup (pb->priv->props, name) == NULL) {

		CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
				     ex_Bonobo_PropertyBag_NotFound,
				     NULL);

		return CORBA_OBJECT_NIL;
	}

	prop = bonobo_transient_create_objref (pb->priv->transient, 
					       "IDL:Bonobo/Property:1.0", 
					        name, ev);

	return prop;
}

static Bonobo_PropertyNames *
impl_Bonobo_PropertyBag_getPropertyNames (PortableServer_Servant servant,
					  CORBA_Environment     *ev)
{
	BonoboPropertyBag        *pb = BONOBO_PROPERTY_BAG (bonobo_object_from_servant (servant));
	Bonobo_PropertyNames	*name_list;
	GList			*props;
	GList			*curr;
	int                      len;
	int			 i;

	/*
	 * Create the PropertyNames list and allocate space for the
	 * names.
	 */
	len = g_hash_table_size (pb->priv->props);

	name_list = Bonobo_PropertyNames__alloc ();
	name_list->_length = len;

	if (len == 0)
		return name_list;

	name_list->_buffer = CORBA_sequence_CORBA_string_allocbuf (len);

	/*
	 * Create the list of property names.
	 */
	props = bonobo_property_bag_get_prop_list (pb);

	i = 0;
	for (curr = props; curr != NULL; curr = curr->next) {
		BonoboProperty *prop = curr->data;

		name_list->_buffer [i] = CORBA_string_dup (prop->name);
		i ++;
	}

	g_list_free (props);

	return name_list;
}

static Bonobo_PropertySet *
impl_Bonobo_PropertyBag_getValues (PortableServer_Servant  servant,
				   CORBA_Environment      *ev)
{
	BonoboPropertyBag   *pb = BONOBO_PROPERTY_BAG (bonobo_object_from_servant (servant));
	Bonobo_PropertySet *set;
	GList		   *props;
	GList		   *curr;
	int		    len;
	int		    i;

	/*
	 * Create the PropertyList and allocate space for the
	 * properties.
	 */
	len = g_hash_table_size (pb->priv->props);

	set = Bonobo_PropertySet__alloc ();
	set->_length = len;

	if (len == 0)
		return set;

	set->_buffer = CORBA_sequence_Bonobo_Pair_allocbuf (len);
	CORBA_sequence_set_release (set, TRUE);

	/*
	 * Create a list of Object references for the properties.
	 */
	props = bonobo_property_bag_get_prop_list (pb);

	i = 0;
	for (curr = props; curr != NULL; curr = curr->next) {
		BonoboProperty *prop = curr->data;
		BonoboArg *arg;

		set->_buffer [i].name =  CORBA_string_dup (prop->name);

		arg = bonobo_arg_new (prop->type);
	
		prop->get_prop (pb, arg, prop->idx, ev, 
				prop->user_data);

		set->_buffer [i].value = *arg;

		i++;		
	}

	g_list_free (props);

	return set;
}

static void
impl_Bonobo_PropertyBag_setValues (PortableServer_Servant    servant,
				   const Bonobo_PropertySet *set,
				   CORBA_Environment        *ev)
{
	BonoboPropertyBag   *pb = BONOBO_PROPERTY_BAG (bonobo_object_from_servant (servant));
	int i;

	for (i = 0; i < set->_length; i++) {

		bonobo_property_bag_set_value (pb, set->_buffer [i].name,
					       &set->_buffer [i].value, ev);
	}
}


/*
 * BonoboPropertyBag construction/deconstruction functions. 
 */

/**
 * bonobo_property_bag_construct:
 * @pb: #BonoboPropertyBag to construct
 * @get_prop: the property get callback
 * @set_prop: the property set callback
 * @es: an event source to aggregate
 * @user_data: user data for the callbacks
 * 
 * Constructor, only for use in wrappers and object derivation, please
 * refer to the #bonobo_property_bag_new for normal use.
 *
 * This function returns @pb, or %NULL in case of error.  If it returns %NULL,
 * the passed in @pb is unrefed.
 *
 * Returns:  #BonoboPropertyBag pointer or %NULL.
 */
BonoboPropertyBag *
bonobo_property_bag_construct (BonoboPropertyBag   *pb,
			       BonoboPropertyGetFn  get_prop,
			       BonoboPropertySetFn  set_prop,
			       BonoboEventSource   *es,
			       gpointer             user_data)
{
	pb->es              = es;
	pb->priv->set_prop  = set_prop;
	pb->priv->get_prop  = get_prop;
	pb->priv->user_data = user_data;

	bonobo_object_add_interface (BONOBO_OBJECT (pb), BONOBO_OBJECT (es));

	if (!(pb->priv->transient = bonobo_transient_new (NULL, bonobo_property_servant_new, bonobo_property_servant_destroy, pb))) {
		bonobo_object_unref (BONOBO_OBJECT (pb));
		return NULL;
	}
	
	return pb;
}

/**
 * bonobo_property_bag_new_full:
 * @get_prop: the property get callback
 * @set_prop: the property set callback
 * @es: an event source to aggregate
 * @user_data: user data for the callbacks
 *
 * Creates a new property bag with the specified callbacks.
 *
 * Returns: A new #BonoboPropertyBag object.
 */
BonoboPropertyBag *
bonobo_property_bag_new_full (BonoboPropertyGetFn  get_prop,
			      BonoboPropertySetFn  set_prop,
			      BonoboEventSource   *es,
			      gpointer             user_data)
{
	BonoboPropertyBag *pb;

	g_return_val_if_fail (es != NULL, NULL);

	pb = gtk_type_new (BONOBO_PROPERTY_BAG_TYPE);

	return bonobo_property_bag_construct (pb, get_prop, set_prop, es, 
					      user_data);
}

/**
 * bonobo_property_bag_new:
 * @get_prop: the property get callback
 * @set_prop: the property set callback
 * @user_data: user data for the callbacks
 *
 * Creates a new property bag with the specified callbacks.
 *
 * Returns: A new #BonoboPropertyBag object.
 */
BonoboPropertyBag *
bonobo_property_bag_new (BonoboPropertyGetFn get_prop,
			 BonoboPropertySetFn set_prop,
			 gpointer            user_data)
{
	BonoboEventSource *es;

	es = bonobo_event_source_new ();

	return bonobo_property_bag_new_full (get_prop, set_prop, es, 
					     user_data);
}

static void
bonobo_property_destroy (BonoboProperty *prop)
{
	g_free (prop->name);
	prop->idx = -1;

	bonobo_arg_release (prop->default_value);

	g_free (prop->docstring);

	g_free (prop);
}

static gboolean
bonobo_property_bag_foreach_remove_prop (gpointer key, 
					 gpointer value,
					 gpointer user_data)
{
	bonobo_property_destroy (value);

	return TRUE;
}

static void
bonobo_property_bag_destroy (GtkObject *object)
{
	BonoboPropertyBag *pb = BONOBO_PROPERTY_BAG (object);
	
	/* Destroy the transient POA */
	gtk_object_unref (GTK_OBJECT (pb->priv->transient));

	/* Destroy all properties. */
	g_hash_table_foreach_remove (pb->priv->props,
				     bonobo_property_bag_foreach_remove_prop,
				     NULL);
	g_hash_table_destroy (pb->priv->props);

	g_free (pb->priv);

	parent_class->destroy (object);
}


/*
 * BonoboPropertyBag property manipulation API.
 */

/**
 * bonobo_property_bag_add_full:
 * @pb: property bag to add to
 * @name: name of new property
 * @idx: integer index for fast callback switch statement - NB.
 * this value is opaque to the implementation, and is not used
 * for keying properties.
 * @type: the CORBA type eg. TC_long
 * @default_value: the default value or NULL
 * @docstring: the translated documentation string
 * @flags: various flags
 * @get_prop: a per property get callback
 * @set_prop: a per property set callback
 * @user_data: user data for the callbacks
 * 
 * This adds a property to @pb at the full tilt of complexity.
 **/
void
bonobo_property_bag_add_full (BonoboPropertyBag  *pb,
			      const char         *name,
			      int                 idx,
			      BonoboArgType       type,
			      BonoboArg          *default_value,
			      const char         *docstring,
			      BonoboPropertyFlags flags,
			      BonoboPropertyGetFn get_prop,
			      BonoboPropertySetFn set_prop,
			      gpointer            user_data)
{
	BonoboProperty *prop;

	g_return_if_fail (pb != NULL);
	g_return_if_fail (BONOBO_IS_PROPERTY_BAG (pb));
	g_return_if_fail (name != NULL);
	g_return_if_fail (type != NULL);
	g_return_if_fail (g_hash_table_lookup (pb->priv->props, name) == NULL);

	if (flags == 0) { /* Compatibility hack */
		flags = BONOBO_PROPERTY_READABLE |
			BONOBO_PROPERTY_WRITEABLE;
	}
			    
	if (((flags & BONOBO_PROPERTY_READABLE)  && !get_prop) ||
	    ((flags & BONOBO_PROPERTY_WRITEABLE) && !set_prop)) {
		g_warning ("Serious property error, missing get/set fn. "
			   "on %s", name);
		return;
	}

	if (!(flags & BONOBO_PROPERTY_READABLE) && default_value)
		g_warning ("Assigning a default value to a non readable "
			   "property '%s'", name);

	prop = g_new0 (BonoboProperty, 1);

	prop->name          = g_strdup (name);
	prop->idx           = idx;
	prop->type          = type;
	prop->docstring     = g_strdup (docstring);
	prop->flags         = flags;
	prop->get_prop      = get_prop;
	prop->set_prop      = set_prop;
	prop->user_data     = user_data;

	if (default_value)
		prop->default_value = bonobo_arg_copy (default_value);

	g_hash_table_insert (pb->priv->props, prop->name, prop);
}

static BonoboPropertyFlags
flags_gtk_to_bonobo (guint flags)
{
	BonoboPropertyFlags f = 0;

	if (!flags & GTK_ARG_READABLE)
		f |= BONOBO_PROPERTY_READABLE;

	if (!flags & GTK_ARG_WRITABLE)
		f |= BONOBO_PROPERTY_WRITEABLE;

	return f;
}

#define BONOBO_GTK_MAP_KEY "BonoboGtkMapKey"

static void
get_prop (BonoboPropertyBag *bag,
	  BonoboArg         *arg,
	  guint              arg_id,
	  CORBA_Environment *ev,
	  gpointer           user_data)
{
	GtkArg *gtk_arg = user_data;
	GtkArg  new;
	GtkObject *obj;

	if (!(obj = gtk_object_get_data (GTK_OBJECT (bag), 
					 BONOBO_GTK_MAP_KEY))) {
		bonobo_exception_set (ev, ex_Bonobo_PropertyBag_NotFound);
		return;
	}
	
/*	g_warning ("Get prop ... %d: %s", arg_id, gtk_arg->name);*/

	new.type = gtk_arg->type;
	new.name = gtk_arg->name;
	gtk_object_getv (obj, 1, &new);

	bonobo_arg_from_gtk (arg, &new);

	if (new.type == GTK_TYPE_STRING &&
	    GTK_VALUE_STRING (new))
		g_free (GTK_VALUE_STRING (new));
}

static void
set_prop (BonoboPropertyBag *bag,
	  const BonoboArg   *arg,
	  guint              arg_id,
	  CORBA_Environment *ev,
	  gpointer           user_data)
{
	GtkArg *gtk_arg = user_data;
	GtkArg  new;
	GtkObject *obj;

	if (!(obj = gtk_object_get_data (GTK_OBJECT (bag),
					 BONOBO_GTK_MAP_KEY))) {
		bonobo_exception_set (ev, ex_Bonobo_PropertyBag_NotFound);
		return;
	}
		
/*	g_warning ("Set prop ... %d: %s", arg_id, gtk_arg->name);*/

	new.type = gtk_arg->type;
	new.name = gtk_arg->name;
	bonobo_arg_to_gtk (&new, arg);

	gtk_object_setv (obj, 1, &new);
}

/**
 * bonobo_property_bag_add_gtk_args:
 * @pb: destination property bag
 * @object: a generic Gtk Object
 * 
 * Transfers GtkArgs from the object to the property bag,
 * and maps between the two objects property systems.
 **/
void
bonobo_property_bag_add_gtk_args (BonoboPropertyBag  *pb,
				  GtkObject          *object)
{
	GtkArg  *args, *arg;
	guint32 *arg_flags;
	guint    nargs = 0;
	int      i;

	g_return_if_fail (pb != NULL);
	g_return_if_fail (BONOBO_IS_PROPERTY_BAG (pb));
	g_return_if_fail (object != NULL);
	g_return_if_fail (GTK_IS_OBJECT (object));

	if (gtk_object_get_data (GTK_OBJECT (pb),
				 BONOBO_GTK_MAP_KEY)) {
		g_warning ("Cannot proxy two gtk objects in the same bag yet");
		return;
	}

	gtk_object_set_data (GTK_OBJECT (pb),
			     BONOBO_GTK_MAP_KEY, object);
	/*
	 * FIXME: we should do this on a per class basis perhaps.
	 */
	args = gtk_object_query_args (GTK_OBJECT_TYPE (object),
				      &arg_flags, &nargs);
	
	if (!nargs) {
		g_warning ("Strange, no Gtk arguments to map to Bonobo");
		return;
	}

	arg = args;
	/* Setup types, and names */
	for (i = 0; i < nargs; arg++, i++) {
		BonoboPropertyFlags flags;
		BonoboArgType       type;
		char               *desc;

		type = bonobo_arg_type_from_gtk (arg->type);
		if (!type) {
			g_warning ("Can't handle type '%s' on arg '%s'",
				   gtk_type_name (arg->type),
				   arg->name);
			continue;
		}

		flags = flags_gtk_to_bonobo (arg_flags [i]);

		desc = g_strconcat (arg->name, " is a ",
				    gtk_type_name (arg->type), NULL);

		g_warning ("Mapping '%s'", desc);
		bonobo_property_bag_add_full (pb, arg->name, i, type,
					      NULL, desc, flags,
					      get_prop, set_prop, arg);
		g_free (desc);
	}

/* FIXME: leaks like a privatised water company */
/*	g_free (args);*/
	g_free (arg_flags);
}

/**
 * bonobo_property_bag_add:
 * @pb: property bag to add to
 * @name: name of new property
 * @idx: integer index for fast callback switch statement
 * this value is opaque to the implementation, and is not used
 * for keying properties.
 * @type: the CORBA type eg. TC_long
 * @default_value: the default value or NULL
 * @docstring: the translated documentation string
 * @flags: various flags
 * 
 *  Adds a property to the property bag.
 **/
void
bonobo_property_bag_add (BonoboPropertyBag  *pb,
			 const char         *name,
			 int                 idx,
			 BonoboArgType       type,
			 BonoboArg          *default_value,
			 const char         *docstring,
			 BonoboPropertyFlags flags)
{
	g_return_if_fail (pb != NULL);

	bonobo_property_bag_add_full (pb, name, idx, type,
				      default_value, docstring, flags,
				      pb->priv->get_prop,
				      pb->priv->set_prop,
				      pb->priv->user_data);
}

static void
notify_listeners (BonoboPropertyBag *pb,
		  BonoboProperty    *prop,
		  const BonoboArg   *new_value,
		  CORBA_Environment *opt_ev)
{
	if (prop->flags & BONOBO_PROPERTY_NO_LISTENING)
		return;
	
	bonobo_event_source_notify_listeners_full (pb->es,
						   "Bonobo/Property",
						   "change", prop->name,
						   new_value, opt_ev);
}

/**
 * bonobo_property_bag_notify_listeners:
 * @pb: the property bag
 * @name: the name of the property that changed value
 * @new_value: the new value
 * @opt_ev: optional CORBA exception environment or NULL
 * 
 * This function is used by the implementation of the property
 * proper, to signal to the property bag that the value of the
 * property has changed.
 * NB. There is no need to call this when you do a set_value.
 **/
void
bonobo_property_bag_notify_listeners (BonoboPropertyBag *pb,
				      const char        *name,
				      const BonoboArg   *new_value,
				      CORBA_Environment *opt_ev)
{
	BonoboProperty *prop;

	bonobo_return_if_fail (pb != NULL, opt_ev);
	bonobo_return_if_fail (BONOBO_IS_PROPERTY_BAG (pb), opt_ev);
	bonobo_return_if_fail (name != NULL, opt_ev);
	bonobo_return_if_fail (pb->priv != NULL, opt_ev);
 	bonobo_return_if_fail (new_value != NULL, opt_ev);

	if (!(prop = g_hash_table_lookup (pb->priv->props, name))) {
		bonobo_exception_set (opt_ev, ex_Bonobo_PropertyBag_NotFound);
		return;
	}

	if (!bonobo_arg_type_is_equal (prop->type, new_value->_type, opt_ev)) {
		bonobo_exception_set (opt_ev, ex_Bonobo_Property_InvalidValue);
		return;
	}

	notify_listeners (pb, prop, new_value, opt_ev);
}

/**
 * bonobo_property_bag_set_value:
 * @pb: the property bag
 * @name: the name of the property
 * @value: the new value to set to 
 * @opt_ev: optional CORBA exception environment or NULL
 * 
 * This method sets the value of the property with @name
 * to @value.
 **/
void
bonobo_property_bag_set_value (BonoboPropertyBag *pb,
			       const char        *name,
			       const BonoboArg   *value,
			       CORBA_Environment *opt_ev)
{
	BonoboProperty *prop;
	CORBA_Environment ev, *my_ev;

	bonobo_return_if_fail (pb != NULL, opt_ev);
	bonobo_return_if_fail (BONOBO_IS_PROPERTY_BAG (pb), opt_ev);
	bonobo_return_if_fail (name != NULL, opt_ev);
	bonobo_return_if_fail (pb->priv != NULL, opt_ev);
	bonobo_return_if_fail (value != NULL, opt_ev);
	
	prop = g_hash_table_lookup (pb->priv->props, name);

	if (!prop || !prop->set_prop) {
		bonobo_exception_set (opt_ev, ex_Bonobo_PropertyBag_NotFound);
		return;
	}

	if (!(prop->flags & BONOBO_PROPERTY_WRITEABLE)) {
		bonobo_exception_set (opt_ev, ex_Bonobo_Property_ReadOnlyProperty);
		return;
	}

	if (!bonobo_arg_type_is_equal (prop->type, value->_type, opt_ev)) {
		bonobo_exception_set (opt_ev, ex_Bonobo_Property_InvalidValue);
		return;
	}

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

	prop->set_prop (pb, value, prop->idx, my_ev, prop->user_data);

	if (!BONOBO_EX (my_ev))
		notify_listeners (pb, prop, value, my_ev);

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

/**
 * bonobo_property_bag_get_value:
 * @pb: the property bag
 * @name: the name of the property
 * @opt_ev: optional CORBA exception environment or NULL
 * 
 * Return value: the value of the property with name @name or NULL
 * on exception.
 **/
BonoboArg *
bonobo_property_bag_get_value (BonoboPropertyBag *pb, 
			       const char        *name,
			       CORBA_Environment *opt_ev)
{
	BonoboProperty    *prop;
	BonoboArg         *arg;
	CORBA_Environment  ev, *my_ev;

	bonobo_return_val_if_fail (pb != NULL, NULL, opt_ev);
	bonobo_return_val_if_fail (BONOBO_IS_PROPERTY_BAG (pb), NULL, opt_ev);
	bonobo_return_val_if_fail (name != NULL, NULL, opt_ev);
	bonobo_return_val_if_fail (pb->priv != NULL, NULL, opt_ev);

	prop = g_hash_table_lookup (pb->priv->props, name);

	if (!prop || !prop->get_prop) {
		bonobo_exception_set (opt_ev, ex_Bonobo_PropertyBag_NotFound);
		return NULL;
	}

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

	arg = bonobo_arg_new (prop->type);

	prop->get_prop (pb, arg, prop->idx, my_ev, prop->user_data);

	if (!opt_ev)
		CORBA_exception_free (&ev);

	return arg;
}

/**
 * bonobo_property_bag_get_property_type:
 * @pb: the property bag
 * @name: the name of the property
 * @opt_ev: optional CORBA exception environment or NULL
 * 
 * Return value: the type of the property with name @name
 **/
BonoboArgType
bonobo_property_bag_get_property_type (BonoboPropertyBag *pb, 
				       const char        *name,
				       CORBA_Environment *opt_ev)
{
	BonoboProperty *prop;

	bonobo_return_val_if_fail (pb != NULL, NULL, opt_ev);
	bonobo_return_val_if_fail (BONOBO_IS_PROPERTY_BAG (pb), NULL, opt_ev);
	bonobo_return_val_if_fail (name != NULL, NULL, opt_ev);
	bonobo_return_val_if_fail (pb->priv != NULL, NULL, opt_ev);

	if (!(prop = g_hash_table_lookup (pb->priv->props, name))) {
		bonobo_exception_set (opt_ev, ex_Bonobo_PropertyBag_NotFound);
		return NULL;
	}

	return prop->type;
}

/**
 * bonobo_property_bag_get_default:
 * @pb: the property bag
 * @name: the name of the property
 * @opt_ev: optional CORBA exception environment or NULL
 * 
 * Return value: the default value of the property with name @name
 **/
BonoboArg *
bonobo_property_bag_get_default (BonoboPropertyBag *pb, 
				 const char        *name,
				 CORBA_Environment *opt_ev)
{
	BonoboProperty *prop;

	bonobo_return_val_if_fail (pb != NULL, NULL, opt_ev);
	bonobo_return_val_if_fail (BONOBO_IS_PROPERTY_BAG (pb), NULL, opt_ev);
	bonobo_return_val_if_fail (name != NULL, NULL, opt_ev);
	bonobo_return_val_if_fail (pb->priv != NULL, NULL, opt_ev);

	if (!(prop = g_hash_table_lookup (pb->priv->props, name))) {
		bonobo_exception_set (opt_ev, ex_Bonobo_PropertyBag_NotFound);
		return NULL;
	}

	if (prop->default_value)
		return bonobo_arg_copy (prop->default_value);
	else {
		BonoboArg *a = bonobo_arg_new (prop->type);
		return a;
	}
}

/**
 * bonobo_property_bag_get_default:
 * @pb: the property bag
 * @name: the name of the property
 * 
 * Return value: TRUE if the bag has a property of this name
 **/
gboolean
bonobo_property_bag_has_property (BonoboPropertyBag *pb, 
				  const char        *name)
{
	g_return_val_if_fail (pb != NULL, FALSE);
	g_return_val_if_fail (BONOBO_IS_PROPERTY_BAG (pb), FALSE);
	g_return_val_if_fail (name != NULL, FALSE);
	g_return_val_if_fail (pb->priv != NULL, FALSE);

	if (g_hash_table_lookup (pb->priv->props, name) == NULL)
		return FALSE;

	return TRUE;
}

/**
 * bonobo_property_bag_get_docstring:
 * @pb: the property bag
 * @name: the name of the property
 * @opt_ev: optional CORBA exception environment or NULL
 * 
 * Return value: the documentation string for this property.
 **/
const char *
bonobo_property_bag_get_docstring (BonoboPropertyBag *pb, 
				   const char        *name,
				   CORBA_Environment *opt_ev)
{
	BonoboProperty *prop;

	bonobo_return_val_if_fail (pb != NULL, NULL, opt_ev);
	bonobo_return_val_if_fail (BONOBO_IS_PROPERTY_BAG (pb), NULL, opt_ev);
	bonobo_return_val_if_fail (name != NULL, NULL, opt_ev);
	bonobo_return_val_if_fail (pb->priv != NULL, NULL, opt_ev);

	if (!(prop = g_hash_table_lookup (pb->priv->props, name))) {
		bonobo_exception_set (opt_ev, ex_Bonobo_PropertyBag_NotFound);
		return NULL;
	}

	return prop->docstring;
}

/**
 * bonobo_property_bag_get_flags:
 */
const BonoboPropertyFlags
bonobo_property_bag_get_flags (BonoboPropertyBag *pb, 
			       const char        *name,
			       CORBA_Environment *opt_ev)
{
	BonoboProperty *prop;

	bonobo_return_val_if_fail (pb != NULL, 0, opt_ev);
	bonobo_return_val_if_fail (BONOBO_IS_PROPERTY_BAG (pb), 0, opt_ev);
	bonobo_return_val_if_fail (name != NULL, 0, opt_ev);
	bonobo_return_val_if_fail (pb->priv != NULL, 0, opt_ev);

	if (!(prop = g_hash_table_lookup (pb->priv->props, name))) {
		bonobo_exception_set (opt_ev, ex_Bonobo_PropertyBag_NotFound);
		return 0;
	}

	return prop->flags;
}


/* Class/object initialization functions. */

static void
bonobo_property_bag_class_init (BonoboPropertyBagClass *klass)
{
	GtkObjectClass *object_class = (GtkObjectClass *) klass;
	POA_Bonobo_PropertyBag__epv *epv = &klass->epv;

	parent_class = gtk_type_class (PARENT_TYPE);

	object_class->destroy = bonobo_property_bag_destroy;

	epv->getProperties        = impl_Bonobo_PropertyBag_getProperties;
	epv->getPropertyByName    = impl_Bonobo_PropertyBag_getPropertyByName;
	epv->getPropertyNames     = impl_Bonobo_PropertyBag_getPropertyNames;
	epv->getEventSource       = impl_Bonobo_PropertyBag_getEventSource;
	epv->getValues            = impl_Bonobo_PropertyBag_getValues;
	epv->setValues            = impl_Bonobo_PropertyBag_setValues;
}

static void
bonobo_property_bag_init (BonoboPropertyBag *pb)
{
	pb->priv = g_new0 (BonoboPropertyBagPrivate, 1);

	pb->priv->props = g_hash_table_new (g_str_hash, g_str_equal);
}

BONOBO_X_TYPE_FUNC_FULL (BonoboPropertyBag, 
			 Bonobo_PropertyBag,
			 PARENT_TYPE,
			 bonobo_property_bag);