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	4 Dec 2003 15:50:41 -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,10 +1097,94 @@
 
 	gok_spy_find_children (&nodeRoot, type, anAccessibleRole);
 
+	gok_spy_resolve_namesakes (nodeRoot.pnext);
 	gok_log_leave();
 	return nodeRoot.pnext;
 }
 
+/* return a node (if exists) from the list with the given name */
+static AccessibleNode*
+gok_spy_node_get_node_named (AccessibleNode *node, gchar *name)
+{
+	while (node != NULL) {
+		if (strcmp (name, node->pname) == 0) {
+			return node;
+		}
+		node = node->pnext;
+	}
+	return NULL;
+}
+
+/* modify the name for the given node so that it is distinct from namesakes */
+static void
+gok_spy_distinguish_node_name (AccessibleNode *node)
+{
+	AccessibleRelation **relations;
+	Accessible *labelAccessible = NULL;
+	AccessibleRelationType type = -1;
+	char *s;
+	gint i,j, num_labels;
+	i = 0;
+	j = 0;
+	num_labels = 0;
+	
+	/* find the label for the given node and prepend it to the name */
+	relations = Accessible_getRelationSet(node->paccessible);
+	if (relations != NULL) {
+	while (relations[i]) {
+		type = AccessibleRelation_getRelationType(relations[i]);
+		if (type == SPI_RELATION_LABELED_BY) {
+fprintf(stderr,"\tfound a label\n");
+			num_labels= AccessibleRelation_getNTargets(relations[i]);
+			for (j=0; j < num_labels; j++) {
+				labelAccessible = 
+					AccessibleRelation_getTarget(relations[i], j);
+				if (labelAccessible != NULL) {
+					s = Accessible_getName (labelAccessible);
+					if (s != NULL) {
+						if ( strlen (s) != 0 )  {
+							node->pname = g_strconcat (s, ":", 
+														node->pname);
+							g_free(node->pname); 
+							SPI_freeString (s);
+							Accessible_unref (labelAccessible); /* needed? */
+							break; /* from for loop*/
+						}
+						else {
+							SPI_freeString(s);
+						}
+					}
+					Accessible_unref (labelAccessible); /* needed? */
+				}
+			}
+			break; /* from while loop (we found a label) */
+		}
+		i++;
+		}
+	}
+}
+
+/* disambiguate nodes in the given list that have the same name */
+static void
+gok_spy_resolve_namesakes (AccessibleNode *node)
+{
+	gchar name[1001];
+	AccessibleNode *namesake;
+	
+	/* for every node in the given list... */
+	while (node != NULL) {
+		namesake = node;
+		strncpy (name, node->pname, 1000);
+		/* for every node with the same name as the current node... */
+		while ((namesake = gok_spy_node_get_node_named ( namesake->pnext, name )) 
+				!= NULL) {
+fprintf(stderr,"distinguishing a node\n");
+			gok_spy_distinguish_node_name ( namesake );
+		}
+		node = node->pnext;
+	}
+}
+
 static gboolean
 gok_spy_add_node (AccessibleNode *prev, Accessible *accessible, gint i, char *name)
 {
