Filter:   InfoImg
download bonobo-ui-xml.c
Language: C
Copyright: Copyright 2000 Ximian, Inc.
LOC: 704
Project Info
libbonoboui
Server: Gnome
Type: cvs
...onoboui\libbonoboui\bonobo\
   .cvsignore
   bonobo-a11y.c
   bonobo-a11y.h
   bonobo-canvas-component.c
   bonobo-canvas-component.h
   bonobo-canvas-item.c
   bonobo-canvas-item.h
   bonobo-control-frame.c
   bonobo-control-frame.h
   bonobo-control-internal.h
   bonobo-control.c
   bonobo-control.h
   bonobo-dock-band.c
   bonobo-dock-band.h
   bonobo-dock-item-grip.c
   bonobo-dock-item-grip.h
   bonobo-dock-item.c
   bonobo-dock-item.h
   bonobo-dock-layout.c
   bonobo-dock-layout.h
   bonobo-dock.c
   bonobo-dock.h
   ...bo-embeddable-factory.h
   ...bo-file-selector-util.c
   ...bo-file-selector-util.h
   ...bo-insert-component.xpm
   bonobo-plug.c
   bonobo-plug.h
   bonobo-property-control.c
   bonobo-property-control.h
   bonobo-selector-widget.c
   bonobo-selector-widget.h
   bonobo-selector.c
   bonobo-selector.h
   bonobo-socket.c
   bonobo-socket.h
   bonobo-ui-component.c
   bonobo-ui-component.h
   bonobo-ui-config-widget.c
   bonobo-ui-config-widget.h
   bonobo-ui-container.c
   bonobo-ui-container.h
   bonobo-ui-engine-config.c
   bonobo-ui-engine-config.h
   bonobo-ui-engine-private.h
   bonobo-ui-engine.c
   bonobo-ui-engine.h
   bonobo-ui-init-gtk.c
   ...o-ui-internal-toolbar.c
   bonobo-ui-main.c
   bonobo-ui-main.h
   bonobo-ui-marshal-main.c
   bonobo-ui-marshal.list
   bonobo-ui-node-private.h
   bonobo-ui-node.c
   bonobo-ui-node.h
   bonobo-ui-preferences.c
   bonobo-ui-preferences.h
   bonobo-ui-private.h
   bonobo-ui-sync-keys.c
   bonobo-ui-sync-keys.h
   bonobo-ui-sync-menu.c
   bonobo-ui-sync-menu.h
   bonobo-ui-sync-status.c
   bonobo-ui-sync-status.h
   bonobo-ui-sync-toolbar.c
   bonobo-ui-sync-toolbar.h
   bonobo-ui-sync.c
   bonobo-ui-sync.h
   ...i-toolbar-button-item.c
   ...i-toolbar-button-item.h
   ...-toolbar-control-item.c
   ...-toolbar-control-item.h
   bonobo-ui-toolbar-item.c
   bonobo-ui-toolbar-item.h
   ...ui-toolbar-popup-item.c
   ...ui-toolbar-popup-item.h
   ...ar-toggle-button-item.c
   ...ar-toggle-button-item.h
   bonobo-ui-toolbar.c
   bonobo-ui-toolbar.h
   bonobo-ui-util.c
   bonobo-ui-util.h
   bonobo-ui-win32.c
   bonobo-ui-xml.c
   bonobo-ui-xml.h
   bonobo-widget.c
   bonobo-widget.h
   bonobo-window.c
   bonobo-window.h
   bonobo-wrapper.h
   bonobo-zoomable-frame.c
   bonobo-zoomable-frame.h
   bonobo-zoomable.c
   bonobo-zoomable.h
   ...i-2.0-uninstalled.pc.in
   libbonoboui-2.0.pc.in
   Makefile.am

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
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * bonobo-ui-xml.c: A module for merging, overlaying and de-merging XML 
 *
 * Author:
 *	Michael Meeks (michael@ximian.com)
 *
 * Copyright 2000 Ximian, Inc.
 */
#include "config.h"
#include <string.h>
#include <glib-object.h>
#include <bonobo/bonobo-ui-xml.h>
#include <bonobo/bonobo-ui-marshal.h>
#include <bonobo/bonobo-ui-node-private.h>

#undef UI_XML_DEBUG
#undef BONOBO_UI_XML_DUMP

#ifdef BONOBO_UI_XML_DUMP
#	define DUMP_XML(a,b,c) (bonobo_ui_xml_dump ((a), (b), (c)))
#else
#	define DUMP_XML(a,b,c)
#endif

static GQuark pos_id = 0;
static GQuark name_id = 0;
static GQuark placeholder_id = 0;

static void watch_update  (BonoboUIXml  *tree,
			   BonoboUINode *node);
static void watch_destroy (gpointer      data);

static GObjectClass *parent_class = NULL;

enum {
	OVERRIDE,
	REPLACE_OVERRIDE,
	REINSTATE,
	RENAME,
	REMOVE,
	LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };

inline static gboolean
identical (BonoboUIXml *tree, gpointer a, gpointer b)
{
	gboolean val;

	if (tree->compare)
		val = tree->compare (a, b);
	else
		val = (a == b);

/*	fprintf (stderr, "Identical ? '%p' '%p' : %d\n", a, b, val);*/

	return val;
}

/**
 * bonobo_ui_xml_get_data:
 * @tree: the tree
 * @node: the node
 * 
 * This function gets the data pointer associated with @node
 * and if there is no data pointer, constructs it using a user supplied
 * callback.
 * 
 * Return value: a valid Data pointer - never NULL.
 **/
gpointer
bonobo_ui_xml_get_data (BonoboUIXml *tree, BonoboUINode *node)
{
	if (!bonobo_ui_node_get_data (node)) {
		if (tree && tree->data_new)
			bonobo_ui_node_set_data (node, tree->data_new ());
		else {
			g_warning ("Error: No tree, and no data on node; leaking");
			bonobo_ui_node_set_data (node, g_new0 (BonoboUIXmlData, 1));
		}
	}

	return bonobo_ui_node_get_data (node);
}

/**
 * bonobo_ui_xml_clean:
 * @tree: the tree
 * @node: the node
 * 
 * This function marks the entire @tree from @node down
 * ( all its child nodes ) as being clean.
 **/
void
bonobo_ui_xml_clean (BonoboUIXml  *tree,
		     BonoboUINode *node)
{
	BonoboUIXmlData *data;
	BonoboUINode    *l;

	data = bonobo_ui_xml_get_data (tree, node);
	data->dirty = FALSE;

	for (l = bonobo_ui_node_children (node); l;
	     l = bonobo_ui_node_next (l))
		bonobo_ui_xml_clean (tree, l);
}

static void
set_children_dirty (BonoboUIXml *tree, BonoboUINode *node)
{
	BonoboUINode *l;

	if (!node)
		return;

	for (l = bonobo_ui_node_children (node); l;
	     l = bonobo_ui_node_next (l)) {
		BonoboUIXmlData *data;

		data = bonobo_ui_xml_get_data (tree, l);
		data->dirty = TRUE;
		
		set_children_dirty (tree, l);
	}
}

/**
 * bonobo_ui_xml_set_dirty:
 * @tree: the tree
 * @node: the node
 * 
 * This function sets a node as being dirty, along with all
 * its children. However more than this, it also sets its parent
 * dirty, and bubbles this up while the parent is a placeholder,
 * so as to allow a re-generate to be forced for its real visible
 * parent.
 **/
void
bonobo_ui_xml_set_dirty (BonoboUIXml *tree, BonoboUINode *node)
{
	int i;
	BonoboUINode *l;

	l = node;
	for (i = 0; (i < 2) && l; i++) {
		BonoboUIXmlData *data;

		/*
		 * FIXME: the placeholder functionality is broken and should
		 * live in bonobo-window.c for cleanliness and never in this
		 * more generic code.
		 */
		if (l->name_id == placeholder_id)
			i--;

		data = bonobo_ui_xml_get_data (tree, l);
		data->dirty = TRUE;

		l = bonobo_ui_node_parent (l);
	}

	/* Too conservative in some cases.*/
	set_children_dirty (tree, node);
}

/**
 * bonobo_ui_xml_get_parent_path:
 * @path: the path
 * 
 * This function lops one level off a path, much
 * like appending '..' to a Unix directory path.
 * 
 * Return value: the parent's path, use g_free to release it
 **/
char *
bonobo_ui_xml_get_parent_path (const char *path)
{
	const char *p;
	char *ret;

	if ((p = strrchr (path, '/')))
		ret = g_strndup (path, p - path);
	else
		ret = g_strdup (path);

	return ret;
}

static void node_free (BonoboUIXml *tree, BonoboUINode *node);

static void
free_nodedata (BonoboUIXml *tree, BonoboUIXmlData *data,
	       gboolean do_overrides)
{
	if (data) {
		if (data->overridden) {
			if (do_overrides) {
				GSList *l;

				for (l = data->overridden; l; l = l->next)
					node_free (tree, l->data);
				g_slist_free (data->overridden);
			} else 
				/*
				 *  This indicates a serious error in the
				 * overriding logic.
				 */
				g_warning ("Leaking overridden nodes");
		}

		if (tree->data_free)
			tree->data_free (data);
		else
			g_free (data);
	}
}

static void
free_nodedata_tree (BonoboUIXml *tree, BonoboUINode *node, gboolean do_overrides)
{
	BonoboUINode *l;

	if (node == NULL)
		return;

	free_nodedata (tree, bonobo_ui_node_get_data (node), do_overrides);
	bonobo_ui_node_set_data (node, NULL);

	for (l = bonobo_ui_node_children (node); l;
             l = bonobo_ui_node_next (l))
		free_nodedata_tree (tree, l, do_overrides);
}

static void
node_free (BonoboUIXml *tree, BonoboUINode *node)
{
	free_nodedata_tree (tree, node, TRUE);
	bonobo_ui_node_unlink (node);
	bonobo_ui_node_unref (node);
}

static void
do_set_id (BonoboUIXml *tree, BonoboUINode *node, gpointer id)
{
	BonoboUIXmlData *data;

	data = bonobo_ui_xml_get_data (tree, node);
	data->id = id;

#ifdef UI_XML_DEBUG
	/* Do some basic validation here ? */
	{
		const char *p, *name;
		
		if ((name = bonobo_ui_node_get_attr_by_id (node, name_id))) {
			for (p = name; *p; p++)
				g_assert (*p != '/' && *p != '#');
		}
	}
#endif

	for (node = node->children; node; node = node->next)
		do_set_id (tree, node, id);
}

static void
set_id (BonoboUIXml *tree, BonoboUINode *node, gpointer id)
{
	for (; node; node = node->next)
		do_set_id (tree, node, id);
}

static void
dump_internals (BonoboUIXml *tree, BonoboUINode *node)
{
	int i;
	BonoboUINode *l;
	const char *txt;
	static int indent = -4;
	BonoboUIXmlData *data = bonobo_ui_xml_get_data (tree, node);

	indent += 2;

	for (i = 0; i < indent; i++)
		fprintf (stderr, " ");

	fprintf (stderr, "%16s name=\"%10s\" ", bonobo_ui_node_get_name (node),
		 (txt = bonobo_ui_node_peek_attr (node, "name")) ? txt : "NULL");

	fprintf (stderr, "%d len %u", data->dirty,
		 g_slist_length (data->overridden));
	if (tree->dump)
		tree->dump (tree, node);
	else
		fprintf (stderr, "\n");

	if (data->overridden) {
		GSList *list;
		int     old_indent;
		old_indent = indent;
		for (list = data->overridden; list; list = list->next) {
			for (i = 0; i < indent; i++)
				fprintf (stderr, " ");
			fprintf (stderr, "`--->");
			dump_internals (tree, list->data);
			indent += 4;
		}
		indent = old_indent;
	}

	for (l = bonobo_ui_node_children (node); l; l = bonobo_ui_node_next (l))
		dump_internals (tree, l);

	indent -= 2;
}

/**
 * bonobo_ui_xml_dump:
 * @tree: the tree node
 * @bnode: the base node to start dumping from
 * @descr: a description string to print.
 * 
 * This debug function dumps the contents of a BonoboUIXml tree
 * to stderr, it is used by BonoboUIEngine to provide some of the
 * builtin BonoboUIDump verb functionality.
 **/
void
bonobo_ui_xml_dump (BonoboUIXml  *tree,
		    BonoboUINode *node,
		    const char   *descr)
{
	gchar *str;

	str = bonobo_ui_node_to_string (node, TRUE);
	fprintf (stderr, "Dump '%s':\n%s\n", descr, str);
	g_free (str);

	fprintf (stderr, "--- Internals ---\n");
	dump_internals (tree, node);
	fprintf (stderr, "---\n");
}

static void
prune_overrides_by_id (BonoboUIXml *tree, BonoboUIXmlData *data, gpointer id)
{
	GSList *l, *next;

	for (l = data->overridden; l; l = next) {
		BonoboUIXmlData *o_data;
				
		next = l->next;
		o_data = bonobo_ui_xml_get_data (tree, l->data);

		if (identical (tree, o_data->id, id)) {
			node_free (tree, l->data);

			data->overridden =
				g_slist_remove_link (data->overridden, l);
			g_slist_free_1 (l);
		}
	}
}

static void merge (BonoboUIXml *tree, BonoboUINode *current, BonoboUINode **new);

static void
override_node_with (BonoboUIXml *tree, BonoboUINode *old, BonoboUINode *new)
{
	BonoboUIXmlData *data = bonobo_ui_xml_get_data (tree, new);
	BonoboUIXmlData *old_data = bonobo_ui_xml_get_data (tree, old);
	gboolean         same, transparent, override;

	/* Is it just a path / grouping simplifying entry with no content ? */
	transparent = bonobo_ui_node_transparent (new);

	same = identical (tree, data->id, old_data->id);

	g_assert (data->id);

	override = !same && !transparent;

	if (override) {

		g_signal_emit (tree, signals [OVERRIDE], 0, new, old);

		data->overridden = g_slist_prepend (old_data->overridden, old);
		prune_overrides_by_id (tree, data, data->id);
	} else {
		if (transparent)
			data->id = old_data->id;

		data->overridden = old_data->overridden;
		g_signal_emit (tree, signals [REPLACE_OVERRIDE], 0, new, old);

/*		fprintf (stderr, "Replace override of '%s' '%s' with '%s' '%s'",
			 old->name, bonobo_ui_node_peek_attr (old, "name"),
			 new->name, bonobo_ui_node_peek_attr (new, "name"));*/
	}

	old_data->overridden = NULL;

 	if (bonobo_ui_node_children (new))
 		merge (tree, old, &new->children);

	bonobo_ui_node_move_children (old, new);

	bonobo_ui_node_replace (old, new);

	g_assert (bonobo_ui_node_children (old) == NULL);

	if (transparent)
		bonobo_ui_node_copy_attrs (old, new);

	bonobo_ui_xml_set_dirty (tree, new);

	if (!override)
		node_free (tree, old);

	watch_update (tree, new);
}

static void
reinstate_old_node (BonoboUIXml *tree, BonoboUINode *node)
{
	BonoboUIXmlData *data = bonobo_ui_xml_get_data (tree, node);
	BonoboUINode  *old;

	g_return_if_fail (data != NULL);

	if (data->overridden) { /* Something to re-instate */
		BonoboUIXmlData *old_data;

		g_return_if_fail (data->overridden->data != NULL);

		/* Get Old node from overridden list */
		old = data->overridden->data;
		old_data = bonobo_ui_xml_get_data (tree, old);

/*		fprintf (stderr, "Reinstating override '%s' '%s' with '%s' '%s'",
			 node->name, xmlGetProp (node, "name"),
			 old->name, xmlGetProp (old, "name"));*/
		
		/* Update Overridden list */
		old_data->overridden = g_slist_next (data->overridden);
		g_slist_free_1 (data->overridden);
		data->overridden = NULL;

		/* Fire remove while still in tree */
		g_signal_emit (tree, signals [REMOVE], 0, node);
		
		/* Move children across */
		bonobo_ui_node_move_children (node, old);

		/* Switch node back into tree */
		bonobo_ui_node_replace (node, old);

		/* Mark dirty */
		bonobo_ui_xml_set_dirty (tree, old);

		g_signal_emit (tree, signals [REINSTATE], 0, old);

		watch_update (tree, old);

	} else if (bonobo_ui_node_children (node)) { /* We need to leave the node here */
		/* Re-tag the node */
		BonoboUIXmlData *child_data = 
			bonobo_ui_xml_get_data (tree, bonobo_ui_node_children (node));
		data->id = child_data->id;

		/* Mark dirty */
		bonobo_ui_xml_set_dirty (tree, node);
		
		g_signal_emit (tree, signals [RENAME], 0, node);
		return;
	} else {
		/* Mark parent & up dirty */
		bonobo_ui_xml_set_dirty (tree, node);

		g_signal_emit (tree, signals [REMOVE], 0, node);
	}

/*		fprintf (stderr, "destroying node '%s' '%s'\n",
		node->name, bonobo_ui_node_peek_attr (node, "name"));*/
			
	bonobo_ui_node_unlink (node);
	
	if (node == tree->root) /* Ugly special case */
		tree->root = NULL;

	/* Destroy the old node */
	node_free (tree, node);
}

static BonoboUINode *
xml_get_path (BonoboUIXml *tree, const char *path,
	      gboolean allow_wild, gboolean *wildcard)
{
	BonoboUINode *ret;
	char   **names, *copy;
	int      i, j, slashes;
	
	g_return_val_if_fail (tree != NULL, NULL);
	g_return_val_if_fail (!allow_wild || wildcard != NULL, NULL);

#ifdef UI_XML_DEBUG
	fprintf (stderr, "Find path '%s'\n", path);
#endif
/*	DUMP_XML (tree, tree->root, "Before find path"); */

	if (allow_wild)
		*wildcard = FALSE;
	if (!path || path [0] == '\0')
		return tree->root;

	if (path [0] != '/')
		g_warning ("non-absolute path brokenness '%s'", path);

	for (i = slashes = 0; path [i]; i++)
		if (path [i] == '/')
			slashes++;

	names = g_newa (char *, slashes + 2);
	names [0] = copy = g_alloca (i + 1);

	for (i = j = 0; path [i]; i++) {
		if (path [i] == '/') {
			copy [i] = '\0';
			names [++j] = &copy [i + 1];
		} else
			copy [i] = path [i];
	}
	copy [i] = '\0';
	names [++j] = NULL;

/*	names = bonobo_ui_xml_path_split (path); */

	ret = tree->root;
	for (i = 0; names && names [i]; i++) {
		if (names [i] [0] == '\0')
			continue;

/*		g_warning ("Path element '%s'", names [i]); */

		if (allow_wild &&
		    names [i] [0] == '*' &&
		    names [i] [1] == '\0')
			*wildcard = TRUE;

		else if (!(ret = bonobo_ui_node_get_path_child (ret, names [i]))) {
/*			bonobo_ui_xml_path_freev (names); */
			return NULL;
		}
	}
		
/*	bonobo_ui_xml_path_freev (names); */

/*	DUMP_XML (tree, tree->root, "After clean find path"); */

	return ret;
}

/**
 * bonobo_ui_xml_get_path:
 * @tree: the tree
 * @path: the path
 * 
 * This function returns the node at path @path inside the
 * internal XML tree.
 * 
 * Return value: a pointer to the node at @path
 **/
BonoboUINode *
bonobo_ui_xml_get_path (BonoboUIXml *tree, const char *path)
{
	return xml_get_path (tree, path, FALSE, NULL);
}

/**
 * bonobo_ui_xml_get_path_wildcard:
 * @tree: the tree
 * @path: the path
 * @wildcard: whether to allow '*' as a wildcard
 * 
 * This does a wildcard path match, the only
 * wildcard character is '*'. This is only really
 * used by the _rm command and the _exists functionality.
 * 
 * Return value: TRUE if the path matches.
 **/
BonoboUINode *
bonobo_ui_xml_get_path_wildcard (BonoboUIXml *tree, const char *path,
				 gboolean    *wildcard)
{
	return xml_get_path (tree, path, TRUE, wildcard);
}

/**
 * bonobo_ui_xml_make_path:
 * @node: the node.
 * 
 * This generates a path name for a node in a tree.
 * 
 * Return value: the path name, use g_free to release.
 **/
char *
bonobo_ui_xml_make_path (BonoboUINode *node)
{
	GString    *path;
	char       *ret;

	g_return_val_if_fail (node != NULL, NULL);

	path = g_string_new (NULL);
	while (node && node->parent) {
		const char *tmp;

		if ((tmp = bonobo_ui_node_get_attr_by_id (node, name_id))) {
			g_string_prepend (path, tmp);
			g_string_prepend (path, "/");
		} else {
			g_string_prepend (path, bonobo_ui_node_get_name (node));
			g_string_prepend (path, "/");
		}

		node = node->parent;
	}

	ret = path->str;
	g_string_free (path, FALSE);

/*	fprintf (stderr, "Make path: '%s'\n", tmp);*/
	return ret;
}

static void
reinstate_node (BonoboUIXml *tree, BonoboUINode *node,
		gpointer id, gboolean nail_me)
{
	BonoboUINode *l, *next;
			
	for (l = bonobo_ui_node_children (node); l; l = next) {
		next = bonobo_ui_node_next (l);
		reinstate_node (tree, l, id, TRUE);
	}

	if (nail_me) {
		BonoboUIXmlData *data;

		data = bonobo_ui_xml_get_data (tree, node);

		if (identical (tree, data->id, id))
			reinstate_old_node (tree, node);
		else
			prune_overrides_by_id (tree, data, id);
	}
}

static void
do_insert (BonoboUINode *parent,
	   BonoboUINode *child,
	   BonoboUINode *insert)
{
	const char *pos;
	gboolean    added = FALSE;

	if ((pos = bonobo_ui_node_get_attr_by_id (child, pos_id))) {
		if (pos [0] == 't') {
			bonobo_ui_node_insert_before (
				bonobo_ui_node_children (parent),
				child);
			added = TRUE;
		}
	}

	if (!added) {
		if (insert)
			bonobo_ui_node_insert_before (
				insert, child);
		else
			bonobo_ui_node_add_child (parent, child);
	}
}

static void
merge (BonoboUIXml *tree, BonoboUINode *current, BonoboUINode **new)
{
	BonoboUINode *a, *b, *nexta, *nextb, *insert = NULL;

	for (a = current->children; a; a = nexta) {
		BonoboUINode *result;
		const xmlChar *a_name;
		const xmlChar *b_name = NULL;
			
		nexta = a->next;
		nextb = NULL;

		a_name = bonobo_ui_node_get_attr_by_id (a, name_id);

		for (b = *new; b; b = nextb) {
			nextb = b->next;

			b_name = NULL;

/*			printf ("'%s' '%s' with '%s' '%s'\n",
				a->name, bonobo_ui_node_peek_attr (a, "name"),
				b->name, bonobo_ui_node_peek_attr (b, "name"));*/
			
			b_name = bonobo_ui_node_get_attr_by_id (b, name_id);

			if (!a_name && !b_name && a->name_id == b->name_id)
				break;

			if (!a_name || !b_name)
				continue;

			if (!strcmp (a_name, b_name))
				break;
		}

		if (b == *new)
			*new = nextb;

		if (b) /* Merger candidate */
			override_node_with (tree, a, b);

		result = b ? b : a;

		if (!insert && !a_name && result->name_id == placeholder_id)
			insert = result;
	}

	for (b = *new; b; b = nextb) {
		BonoboUIXmlData *data;
		
		nextb = b->next;
		
/*		fprintf (stderr, "Transfering '%s' '%s' into '%s' '%s'\n",
			 b->name, bonobo_ui_node_peek_attr (b, "name"),
			 current->name, bonobo_ui_node_peek_attr (current, "name"));*/

		bonobo_ui_node_unlink (b);

		do_insert (current, b, insert);

		if (tree->add_node)
			tree->add_node (current, b, tree->user_data);
		
		bonobo_ui_xml_set_dirty (tree, b);

		/* FIXME: this looks redundant */
		data = bonobo_ui_xml_get_data (tree, current);
		data->dirty = TRUE;

		watch_update (tree, b);

/*		DUMP_XML (tree, current, "After transfer");*/
	}

	*new = NULL;
/*	DUMP_XML (tree, current, "After all"); */
}

/**
 * bonobo_ui_xml_merge:
 * @tree: the tree
 * @path: the path to merge into
 * @nodes: the nodes
 * @id: the id to merge with.
 * 
 * Merges new xml nodes into the internal tree, overriding
 * where appropriate. Merging is by id, ie. overriding only
 * occurs where there is an id mismatch.
 * 
 * Return value: an error flag.
 **/
BonoboUIError
bonobo_ui_xml_merge (BonoboUIXml  *tree,
		     const char   *path,
		     BonoboUINode *nodes,
		     gpointer      id)
{
	BonoboUINode *current;

	g_return_val_if_fail (BONOBO_IS_UI_XML (tree), BONOBO_UI_ERROR_BAD_PARAM);

	if (nodes == NULL)
		return BONOBO_UI_ERROR_OK;

	current = bonobo_ui_xml_get_path (tree, path);
	if (!current) {
		BonoboUINode *l, *next;

		for (l = nodes; l; l = next) {
			next = l->next;
			node_free (tree, l);
		}

		return BONOBO_UI_ERROR_INVALID_PATH;
	}

	set_id (tree, nodes, id);

#ifdef UI_XML_DEBUG
	fprintf (stderr, "\n\n\nPATH: '%s' '%s\n",
		 g_quark_to_string (current->name_id),
		 bonobo_ui_node_peek_attr (current, "name"));
#endif

	DUMP_XML (tree, tree->root, "Merging in");
	DUMP_XML (tree, nodes, "this load");

	merge (tree, current, &nodes);

#ifdef UI_XML_DEBUG
	bonobo_ui_xml_dump (tree, tree->root, "Merged to");
#endif

	return BONOBO_UI_ERROR_OK;
}

BonoboUIError
bonobo_ui_xml_rm (BonoboUIXml *tree,
		  const char  *path,
		  gpointer     id)
{
	BonoboUINode *current;
	gboolean      wildcard;

	current = bonobo_ui_xml_get_path_wildcard (
		tree, path, &wildcard);

/*	fprintf (stderr, "remove stuff from '%s' (%d) -> '%p'\n",
	path, wildcard, current); */

	if (current)
		reinstate_node (tree, current, id, !wildcard);
	else
		return BONOBO_UI_ERROR_INVALID_PATH;

	DUMP_XML (tree, tree->root, "After remove");

	return BONOBO_UI_ERROR_OK;
}

static void
bonobo_ui_xml_dispose (GObject *object)
{
	GSList      *l;
	BonoboUIXml *tree = (BonoboUIXml *) object;

	for (l = tree->watches; l; l = l->next)
		watch_destroy (l->data);
	g_slist_free (tree->watches);
	tree->watches = NULL;

	parent_class->dispose (object);
}

static void
bonobo_ui_xml_finalize (GObject *object)
{
	BonoboUIXml *tree = (BonoboUIXml *) object;
	
	if (tree->root) {
		free_nodedata_tree (tree, tree->root, TRUE);
		bonobo_ui_node_unref (tree->root);
		tree->root = NULL;
	}

	parent_class->finalize (object);
}

static void
bonobo_ui_xml_class_init (BonoboUIXmlClass *klass)
{
	GObjectClass *object_class = (GObjectClass *) klass;
	
	parent_class = g_type_class_peek_parent (klass);

	pos_id         = g_quark_from_static_string ("pos");
 	name_id        = g_quark_from_static_string ("name");
 	placeholder_id = g_quark_from_static_string ("placeholder");

	object_class->dispose  = bonobo_ui_xml_dispose;
	object_class->finalize = bonobo_ui_xml_finalize;

	signals [OVERRIDE] =
		g_signal_new ("override",
			      G_TYPE_FROM_CLASS (object_class),
			      G_SIGNAL_RUN_FIRST,
			      G_STRUCT_OFFSET (BonoboUIXmlClass, override),
			      NULL, NULL,
			      bonobo_ui_marshal_VOID__POINTER_POINTER,
			      G_TYPE_NONE, 2,
			      G_TYPE_POINTER, G_TYPE_POINTER);
	signals [REPLACE_OVERRIDE] =
		g_signal_new ("replace_override",
			      G_TYPE_FROM_CLASS (object_class),
			      G_SIGNAL_RUN_FIRST,
			      G_STRUCT_OFFSET (BonoboUIXmlClass, replace_override),
			      NULL, NULL,
			      bonobo_ui_marshal_VOID__POINTER_POINTER,
			      G_TYPE_NONE, 2,
			      G_TYPE_POINTER, G_TYPE_POINTER);
	signals [REINSTATE] =
		g_signal_new ("reinstate",
			      G_TYPE_FROM_CLASS (object_class),
			      G_SIGNAL_RUN_FIRST,
			      G_STRUCT_OFFSET (BonoboUIXmlClass, reinstate),
			      NULL, NULL,
			      g_cclosure_marshal_VOID__POINTER,
			      G_TYPE_NONE, 1,
			      G_TYPE_POINTER);
	signals [RENAME] =
		g_signal_new ("rename",
			      G_TYPE_FROM_CLASS (object_class),
			      G_SIGNAL_RUN_FIRST,
			      G_STRUCT_OFFSET (BonoboUIXmlClass, rename),
			      NULL, NULL,
			      g_cclosure_marshal_VOID__POINTER,
			      G_TYPE_NONE, 1,
			      G_TYPE_POINTER);
	signals [REMOVE] =
		g_signal_new ("remove",
			      G_TYPE_FROM_CLASS (object_class),
			      G_SIGNAL_RUN_FIRST,
			      G_STRUCT_OFFSET (BonoboUIXmlClass, remove),
			      NULL, NULL,
			      g_cclosure_marshal_VOID__POINTER,
			      G_TYPE_NONE, 1,
			      G_TYPE_POINTER);
}

/**
 * bonobo_cmd_model_get_type:
 *
 * Returns the GtkType for the BonoboCmdModel class.
 */
GType
bonobo_ui_xml_get_type (void)
{
	static GType type = 0;

	if (!type) {
		GTypeInfo info = {
			sizeof (BonoboUIXmlClass),
			(GBaseInitFunc) NULL,
			(GBaseFinalizeFunc) NULL,
			(GClassInitFunc) bonobo_ui_xml_class_init,
			NULL, /* class_finalize */
			NULL, /* class_data */
			sizeof (BonoboUIXml),
			0, /* n_preallocs */
			(GInstanceInitFunc) NULL
		};

		type = g_type_register_static (G_TYPE_OBJECT, "BonoboUIXml",
					       &info, 0);
	}

	return type;
}

BonoboUIXml *
bonobo_ui_xml_new (BonoboUIXmlCompareFn   compare,
		   BonoboUIXmlDataNewFn   data_new,
		   BonoboUIXmlDataFreeFn  data_free,
		   BonoboUIXmlDumpFn      dump,
		   BonoboUIXmlAddNode     add_node,
		   gpointer               user_data)
{
	BonoboUIXml *tree;

	tree = g_object_new (BONOBO_TYPE_UI_XML, NULL);

	tree->compare = compare;
	tree->data_new = data_new;
	tree->data_free = data_free;
	tree->dump = dump;
	tree->add_node = add_node;
	tree->user_data = user_data;

	tree->root = bonobo_ui_node_new ("Root");

	tree->watches = NULL;

	return tree;
}

typedef struct {
	char    *path;
	gpointer user_data;
} Watch;

static void
watch_update (BonoboUIXml *tree, BonoboUINode *node)
{
	GSList *l;
	char   *path;

	if (!tree->watch)
		return;

	/* FIXME: for speed we only check root nodes for now */
	if (bonobo_ui_node_parent (node) !=
	    tree->root)
		return</