Index: gok/gok-spy.c
===================================================================
RCS file: /cvs/gnome/gok/gok/gok-spy.c,v
retrieving revision 1.98
diff -u -r1.98 gok-spy.c
--- gok/gok-spy.c	15 Nov 2003 17:25:45 -0000	1.98
+++ gok/gok-spy.c	3 Dec 2003 21:27:05 -0000
@@ -107,6 +107,8 @@
                                                 void *user_data);
 
 static gboolean gok_spy_is_visible (Accessible *accessible);
+
+static void gok_spy_resolve_duplicate_names (AccessibleNode *node);
 												
 
 /*
@@ -1095,11 +1097,91 @@
 
 	gok_spy_find_children (&nodeRoot, type, anAccessibleRole);
 
+	/* note: resolving names has to be a separate step unless we keep duplicate
+	   name strings stored after subsequent calls to append_node */
+	gok_spy_resolve_duplicate_names (&nodeRoot);
 	gok_log_leave();
 	return nodeRoot.pnext;
 }
 
 static gboolean
+gok_spy_is_duplicated_name(gchar* name, AccessibleNode *node)
+{
+fprintf(stderr,"\t\t->gok_spy_is_duplicated_name\n");	
+	while (node != NULL) {
+		if (strcmp (name, node->pname) == 0) {
+fprintf(stderr,"\t\t<- true gok_spy_is_duplicated_name\n");	
+			return TRUE;
+		}
+		node = node->pnext;
+	}
+fprintf(stderr,"\t\t<- false gok_spy_is_duplicated_name\n");	
+	return FALSE;
+}
+
+static void
+gok_spy_resolve_duplicate_names (AccessibleNode *node)
+{
+	gchar name[1001];
+	AccessibleNode *node_inner;
+	AccessibleRelation** relations = NULL;
+	Accessible* targetAccessible = NULL;
+	AccessibleRelationType type = -1;
+	char *s;
+	gint i,j, ntargets;
+	i = 0;
+	j = 0;
+	ntargets = 0;
+	
+fprintf(stderr,"->gok_spy_resolve_duplicate_names\n");	
+	while ((node = node->pnext) != NULL) {
+		strncpy (name, node->pname, 1000);
+		if (gok_spy_is_duplicated_name ( name, node->pnext )) {
+			/* use labelled-by if it exists */
+			node_inner = node;
+			while (node_inner != NULL) {
+fprintf(stderr,"\tlooping through inner\n");	
+				if (strcmp (node_inner->pname, name) == 0) {
+					relations = Accessible_getRelationSet(node_inner->paccessible);
+					if (relations != NULL)
+					while (relations[i]) {
+fprintf(stderr,"\tlooping through relations\n");	
+						type = AccessibleRelation_getRelationType(relations[i]);
+						if (type == SPI_RELATION_LABELED_BY) {
+							ntargets = AccessibleRelation_getNTargets(relations[i]);
+							for (j=0; j < ntargets; j++) {
+								targetAccessible = 
+									AccessibleRelation_getTarget(relations[i], j);
+								if (targetAccessible != NULL) {
+									s = Accessible_getName (targetAccessible);
+									
+									if (s != NULL) {
+										if ( strlen (s) != 0 )  {
+fprintf(stderr,"\tthere is a better name\n");	
+											g_free(node_inner->pname);
+											node_inner->pname = g_strconcat (s, ":", name);
+											SPI_freeString (s);
+											break; /* from for loop*/
+										}
+										else {
+											SPI_freeString(s);
+										}
+									}
+								}
+							}
+							break; /* from while loop */
+						}
+						i++;
+					}
+				}
+				node_inner = node_inner->pnext;
+			}
+		}
+	}
+fprintf(stderr,"<-gok_spy_resolve_duplicate_names\n");	
+}
+
+static gboolean
 gok_spy_add_node (AccessibleNode *prev, Accessible *accessible, gint i, char *name)
 {
 	AccessibleNode *newnode = NULL;
