883ad9c400e0ffd7532d9823720e11372800c921
braney
  Thu Nov 6 13:28:13 2014 -0800
add "hubClear" url variable to tell the browser to connect theassociated hub, and to disconnect from all the hubs with the same
prefix.  #13382

diff --git src/hg/lib/hubConnect.c src/hg/lib/hubConnect.c
index fa1d8b5..6d3df05 100644
--- src/hg/lib/hubConnect.c
+++ src/hg/lib/hubConnect.c
@@ -502,46 +502,81 @@
 if ((id = getHubId(url, errorMessage)) == 0)
     errAbort("inserted new hubUrl %s, but cannot find it", url);
 
 return id;
 }
 
 // global to hold hubUrl we added if any
 struct hubConnectStatus  *gNewHub;
 
 struct hubConnectStatus  *hubConnectNewHub()
 /* return the hub for the hubUrl we added (if any) */
 {
 return gNewHub;
 }
 
+static void disconnectHubsSamePrefix(struct cart *cart, char *url)
+/* disconnect all the hubs with the same prefix */
+{
+char *prefix = cloneString(url);
+char *ptr = strrchr(prefix, '/');
+if (ptr == NULL)
+    return;
+*ptr = 0;
+
+char query[2048];
+sqlSafef(query, sizeof(query), "select id from %s where hubUrl like  \"%s%%\"", getHubStatusTableName(), prefix);
+
+struct sqlConnection *conn = hConnectCentral();
+struct sqlResult *sr = sqlGetResult(conn, query);
+char **row;
+while ((row = sqlNextRow(sr)) != NULL)
+    {
+    int id = sqlUnsigned(row[0]);
+    char buffer[512];
+
+    safef(buffer, sizeof(buffer), "hgHubConnect.hub.%d", id);
+    cartRemove(cart, buffer);
+    }
+sqlFreeResult(&sr);
+
+hDisconnectCentral(&conn);
+}
+
 static char  *checkForNew( struct cart *cart)
 /* see if the user just typed in a new hub url, return id if so */
 {
 struct hubConnectStatus *hub;
-char *url = cartOptionalString(cart, hgHubDataText);
-char *newDatabase = NULL;
+char *url = cartOptionalString(cart, hgHubDataClearText);
+
+if (url != NULL)
+    disconnectHubsSamePrefix(cart, url);
+else
+    url = cartOptionalString(cart, hgHubDataText);
 
 if (url == NULL)
     return NULL;
 
 trimSpaces(url);
 
 gNewHub = hub = getAndSetHubStatus( cart, url, TRUE);
+    
+cartRemove(cart, hgHubDataClearText);
 cartRemove(cart, hgHubDataText);
 
 char *wantFirstDb = cartOptionalString(cart, hgHubDoFirstDb);
+char *newDatabase = NULL;
 if ((wantFirstDb != NULL) && (hub->trackHub != NULL))
     newDatabase = hub->trackHub->defaultDb;
 
 cartRemove(cart, hgHubDoFirstDb);
 return newDatabase;
 }
 
 unsigned hubResetError(char *url)
 /* clear the error for this url in the hubStatus table,return the id */
 {
 struct sqlConnection *conn = hConnectCentral();
 char query[512];
 
 sqlSafef(query, sizeof(query), "select id from %s where hubUrl = \"%s\"", getHubStatusTableName(), url);
 unsigned id = sqlQuickNum(conn, query);