Update to new dc_queuecmd syntax.
[doldaconnect.git] / clients / gtk2 / main.c
index 05a2a0b..88d68b9 100644 (file)
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
 
+/* This file is a complete and total mess, mostly because of my
+ * inability to structure GUI programs properly. Looking at it too
+ * closely may cause ocular hemorrhaging. */
+
+#include <stdlib.h>
 #include <stdio.h>
 #include <unistd.h>
 #include <string.h>
-#include <malloc.h>
 #include <stdarg.h>
 #include <gtk/gtk.h>
 #include <sys/socket.h>
 #include <regex.h>
 #include <signal.h>
 #include <time.h>
+#include <sys/time.h>
 #include <pwd.h>
 #include <locale.h>
 #include <libintl.h>
 #include <assert.h>
 
+/* "Programming with libxml2 is like the thrilling embrace of an
+ * exotic strangler."
+ *    --Me */
+#include <libxml/parser.h>
+#include <libxml/tree.h>
+
 #ifdef HAVE_CONFIG_H
 #include <config.h>
 #endif
 #include "progressbar.h"
 
+#define TRHISTSIZE 10
+#define PHO_INIT 0
+#define PHO_DATA 1
+#define PHO_EOF 2
+#define PHO_FINI 3
+
+struct trdata
+{
+    size_t poshist[TRHISTSIZE];
+    double timehist[TRHISTSIZE];
+    int hc;
+};
 
 struct fndata
 {
@@ -66,7 +89,8 @@ struct knownspeed
 };
 
 GtkWidget *inpdialog;
-GtkListStore *fnmodel, *ulmodel, *dlmodel, *pubhubmodel, *reslist;
+GtkListStore *fnmodel, *ulmodel, *dlmodel, *reslist;
+int (*pubhubhandler)(int, char *, size_t *) = NULL;
 GtkTreeStore *srchmodel;
 GtkTreeModelFilter *srchmodelfilter;
 GtkTextTagTable *chattags;
@@ -123,6 +147,8 @@ gboolean cb_reslist_list_keypress(GtkWidget *widget, GdkEventKey *event, gpointe
 void dcfdcallback(gpointer data, gint source, GdkInputCondition condition);
 void srchstatupdate(void);
 void transnicebytefunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data);
+void transnicebytefunc2(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data);
+void transspeedinfo(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data);
 void transerrorinfo(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data);
 void percentagefunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data);
 void hidezerofunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data);
@@ -154,6 +180,14 @@ void updatewrite(void)
     }
 }
 
+double ntime(void)
+{
+    struct timeval tv;
+    
+    gettimeofday(&tv, NULL);
+    return((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
+}
+
 void fndestroycb(struct dc_fnetnode *fn)
 {
     struct fndata *data;
@@ -182,6 +216,45 @@ void addfndata(struct dc_fnetnode *fn)
     fn->udata = data;
 }
 
+void trdestroycb(struct dc_transfer *tr)
+{
+    free(tr->udata);
+}
+
+void addtrdata(struct dc_transfer *tr)
+{
+    struct trdata *data;
+    
+    if(tr->udata != NULL)
+       return;
+    tr->destroycb = trdestroycb;
+    data = smalloc(sizeof(*data));
+    memset(data, 0, sizeof(*data));
+    tr->udata = data;
+}
+
+void updatetrdata(struct dc_transfer *tr)
+{
+    int i;
+    struct trdata *data;
+    
+    data = tr->udata;
+    if(data->hc < TRHISTSIZE)
+    {
+       data->poshist[data->hc] = tr->curpos;
+       data->timehist[data->hc] = ntime();
+       data->hc++;
+    } else {
+       for(i = 0; i < TRHISTSIZE - 1; i++)
+       {
+           data->poshist[i] = data->poshist[i + 1];
+           data->timehist[i] = data->timehist[i + 1];
+       }
+       data->poshist[i] = tr->curpos;
+       data->timehist[i] = ntime();
+    }
+}
+
 char *getfnstatestock(int state)
 {
     if(state == DC_FNN_STATE_SYN)
@@ -260,6 +333,30 @@ void updatehublist(void)
     }
 }
 
+char *bytes2si(long long bytes)
+{
+    int i;
+    double b;
+    char *sd;
+    static char ret[64];
+    
+    b = bytes;
+    for(i = 0; (b >= 1024) && (i < 4); i++)
+       b /= 1024;
+    if(i == 0)
+       sd = "B";
+    else if(i == 1)
+       sd = "kiB";
+    else if(i == 2)
+       sd = "MiB";
+    else if(i == 3)
+       sd = "GiB";
+    else
+       sd = "TiB";
+    snprintf(ret, 64, "%.1f %s", b, sd);
+    return(ret);
+}
+
 void percentagefunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
 {
     int colnum;
@@ -286,6 +383,21 @@ void transnicebytefunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeMod
     g_object_set(rend, "text", buf, NULL);
 }
 
+void transnicebytefunc2(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
+{
+    int colnum;
+    long long val;
+    char buf[64];
+    
+    colnum = (int)data;
+    gtk_tree_model_get(model, iter, colnum, &val, -1);
+    if(val >= 0)
+       strcpy(buf, bytes2si(val));
+    else
+       strcpy(buf, _("Unknown"));
+    g_object_set(rend, "text", buf, NULL);
+}
+
 void hidezerofunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
 {
     int colnum, val;
@@ -321,6 +433,33 @@ void speedtimefunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *
     g_object_set(rend, "text", buf, NULL);
 }
 
+void transspeedinfo(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
+{
+    int id;
+    struct dc_transfer *tr;
+    struct trdata *d;
+    char buf[64];
+    int speed;
+    
+    gtk_tree_model_get(model, iter, 0, &id, -1);
+    if((tr = dc_findtransfer(id)) != NULL)
+    {
+       d = tr->udata;
+       if((tr->state != DC_TRNS_MAIN) || (d == NULL))
+       {
+           buf[0] = 0;
+       } else if(d->hc < 2) {
+           strcpy(buf, "...");
+       } else {
+           speed = (((double)(d->poshist[d->hc - 1] - d->poshist[0])) / (d->timehist[d->hc - 1] - d->timehist[0]));
+           snprintf(buf, 64, "%s/s", bytes2si(speed));
+       }
+    } else {
+       buf[0] = 0;
+    }
+    g_object_set(rend, "text", buf, NULL);
+}
+
 void transerrorinfo(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
 {
     int error;
@@ -355,6 +494,24 @@ char *gettrstatestock(int state)
     return(NULL);
 }
 
+gint updatetransfers(gpointer data)
+{
+    struct dc_transfer *tr;
+    struct trdata *d;
+    double now;
+    
+    now = ntime();
+    for(tr = dc_transfers; tr != NULL; tr = tr->next)
+    {
+       if((d = tr->udata) != NULL)
+       {
+           if((d->hc > 0) && ((now - d->timehist[d->hc - 1]) > 2))
+               updatetrdata(tr);
+       }
+    }
+    return(TRUE);
+}
+
 void updatetransferlists(void)
 {
     int i;
@@ -399,7 +556,11 @@ void updatetransferlists(void)
                        if(size != transfer->size)
                            gtk_list_store_set(stores[i], &iter, 6, transfer->size, -1);
                        if(curpos != transfer->curpos)
+                       {
                            gtk_list_store_set(stores[i], &iter, 7, transfer->curpos, -1);
+                           if(transfer->udata != NULL)
+                               updatetrdata(transfer);
+                       }
                        if(error != transfer->error)
                            gtk_list_store_set(stores[i], &iter, 10, transfer->error, -1);
                        if(errortime != transfer->errortime)
@@ -459,6 +620,7 @@ void updatetransferlists(void)
                    free(path);
                if(transfer->hash != NULL)
                    free(hash);
+               addtrdata(transfer);
            }
        }
     }
@@ -881,7 +1043,7 @@ gint ksupdatecb(gpointer data)
            }
        }
        addtobuf(users, NULL);
-       ksquerytag = dc_queuecmd(NULL, NULL, L"filtercmd", L"userspeeda", L"%%a", users, NULL);
+       ksquerytag = dc_queuecmd(NULL, NULL, L"filtercmd", L"userspeeda", L"%a", users, NULL);
        dc_freewcsarr(users);
     }
     return(TRUE);
@@ -905,16 +1067,20 @@ void handleresps(void)
     {
        if(!wcscmp(resp->cmdname, L".connect"))
        {
-           if(resp->code == 200)
+           if(resp->code != 201)
            {
+               msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("The server refused the connection"));
+               dc_disconnect();
+               dcdisconnected();
+           } else if(dc_checkprotocol(resp, DC_LATEST)) {
+               msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Server protocol revision mismatch"));
+               dc_disconnect();
+               dcdisconnected();
+           } else {
                tosbuf = 0x10; /* Minimum cost */
                setsockopt(dcfd, SOL_IP, IP_TOS, &tosbuf, sizeof(tosbuf));
                updatesbar(_("Connected"));
                dc_loginasync(connectas, 1, loginconv, logincallback, NULL);
-           } else {
-               msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("The server refused the connection"));
-               dc_disconnect();
-               dcdisconnected();
            }
        } else if(!wcscmp(resp->cmdname, L".notify")) {
            dc_uimisc_handlenotify(resp);
@@ -979,7 +1145,7 @@ void handleresps(void)
                    if(ires->argv[0].val.num == nextsrch)
                    {
                        if(cursrch != -1)
-                           dc_queuecmd(NULL, NULL, L"cansrch", L"%%i", cursrch, NULL);
+                           dc_queuecmd(NULL, NULL, L"cansrch", L"%i", cursrch, NULL);
                        cursrch = nextsrch;
                        nextsrch = -1;
                        gtk_widget_set_sensitive(main_realsrch, TRUE);
@@ -1199,7 +1365,7 @@ void cb_main_lsres_activate(GtkWidget *widget, gpointer data)
 
 void dcconnect(char *host)
 {
-    dcfd = dc_connect(host, -1);
+    dcfd = dc_connect(host);
     if(dcfd < 0)
     {
        msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect:\n\n%s"), strerror(errno));
@@ -1266,7 +1432,7 @@ void cb_main_fnaddr_activate(GtkWidget *widget, gpointer data)
        toks[0] = srealloc(toks[0], (wcslen(toks[0]) + 5) * sizeof(wchar_t));
        wcscat(toks[0], L":411");
     }
-    tag = dc_queuecmd(NULL, NULL, L"cnct", L"dc", L"%%a", toks, NULL);
+    tag = dc_queuecmd(NULL, NULL, L"cnct", L"dc", L"%a", toks, NULL);
     dc_freewcsarr(toks);
     if((resp = dc_gettaggedrespsync(tag)) != NULL)
     {
@@ -1274,103 +1440,329 @@ void cb_main_fnaddr_activate(GtkWidget *widget, gpointer data)
            msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
        if(resp->code == 509)
            msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("The server could not parse that address"));
+       if(resp->code == 515)
+           msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("There are too many hubs connected"));
        dc_freeresp(resp);
     }
     gtk_entry_set_text(GTK_ENTRY(main_fnaddr), "");
     handleresps();
 }
 
-void pubhubfdcallback(gpointer data, gint source, GdkInputCondition condition)
+void setpubhubmodel(GtkTreeModel *model, int sortcol, int numcols, int *cols, char **names)
 {
-    static char buf[65536];
-    static int bufpos = 0;
-    int ret, i;
-    char *p, *p2;
-    char *fields[4];
-    wchar_t *wbuf;
-    GtkTreeIter iter;
-    int sorted, sortcol;
-    GtkSortType sortorder;
+    GtkTreeViewColumn *col;
+    GtkCellRenderer *rnd;
     GtkTreeModel *sortmodel;
+    int i;
     
-    if(!(condition & GDK_INPUT_READ))
-       return;
-    if(bufpos == 1024)
-       bufpos = 0;
-    ret = read(pubhubfd, buf + bufpos, sizeof(buf) - bufpos);
-    if(ret <= 0)
-    {
-       if(ret < 0)
-           msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not read from public hub listing process: %s"), strerror(errno));
-       close(pubhubfd);
-       gdk_input_remove(pubhubtag);
-       kill(pubhubproc, SIGINT);
-       pubhubfd = pubhubtag = -1;
-       pubhubproc = 0;
-       bufpos = 0;
-       if(filterpubhub)
+    while((col = gtk_tree_view_get_column(GTK_TREE_VIEW(main_phublist), 0)) != NULL)
+       gtk_tree_view_remove_column(GTK_TREE_VIEW(main_phublist), col);
+    for(i = 0; i < numcols; i++) {
+       if(gtk_tree_model_get_column_type(model, cols[i]) == G_TYPE_INT64)
        {
-           regfree(&pubhubfilter);
-           filterpubhub = 0;
+           col = gtk_tree_view_column_new();
+           gtk_tree_view_column_set_title(col, names[i]);
+           rnd = gtk_cell_renderer_text_new();
+           gtk_tree_view_column_pack_start(col, rnd, TRUE);
+           gtk_tree_view_column_set_cell_data_func(col, rnd, transnicebytefunc2, (gpointer)cols[i], NULL);
+       } else {
+           col = gtk_tree_view_column_new_with_attributes(names[i], gtk_cell_renderer_text_new(), "text", cols[i], NULL);
        }
-       return;
+       gtk_tree_view_column_set_sort_column_id(col, cols[i]);
+       gtk_tree_view_column_set_resizable(col, TRUE);
+       gtk_tree_view_append_column(GTK_TREE_VIEW(main_phublist), col);
+    }
+    sortmodel = gtk_tree_model_sort_new_with_model(model);
+    gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(sortmodel), sortcol, GTK_SORT_DESCENDING);
+    gtk_tree_view_set_model(GTK_TREE_VIEW(main_phublist), sortmodel);
+    g_object_unref(sortmodel);
+}
+
+xmlNodePtr findnode(xmlNodePtr node, char *name)
+{
+    for(; node != NULL; node = node->next)
+    {
+       if(!strcmp((char *)node->name, name))
+           break;
     }
-    bufpos += ret;
-    sortmodel = gtk_tree_view_get_model(GTK_TREE_VIEW(main_phublist));
-    sorted = gtk_tree_sortable_get_sort_column_id(GTK_TREE_SORTABLE(sortmodel), &sortcol, &sortorder);
-    gtk_tree_view_set_model(GTK_TREE_VIEW(main_phublist), NULL);
-    while((p = memchr(buf, '\n', bufpos)) != NULL)
+    return(node);
+}
+
+int pubhubxmlhandler(int op, char *buf, size_t *len)
+{
+    static xmlParserCtxtPtr ctxt = NULL;
+    int i, match;
+    xmlNodePtr dr, r, cr, c, n;
+    int numcols, *cols, sortcol;
+    GType type, *types;
+    char **names, *name, *stype, *attr;
+    GtkListStore *model;
+    GtkTreeIter iter;
+    
+    numcols = 0;
+    names = NULL;
+    types = NULL;
+    switch(op)
     {
-       *(p++) = 0;
-       if(!filterpubhub || !regexec(&pubhubfilter, buf, 0, NULL, 0))
+    case PHO_INIT:
+       break;
+    case PHO_DATA:
+       if(ctxt == NULL) {
+           ctxt = xmlCreatePushParserCtxt(NULL, NULL, buf, *len, pubhubaddr);
+           *len = 0;
+           if(ctxt == NULL)
+               return(1);
+       } else {
+           xmlParseChunk(ctxt, buf, *len, 0);
+           *len = 0;
+       }
+       break;
+    case PHO_EOF:
+       xmlParseChunk(ctxt, NULL, 0, 1);
+       if(!ctxt->wellFormed)
+       {
+           msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("The hub list at %s is not valid"), pubhubaddr);
+           break;
+       }
+       dr = r = cr = NULL;
+       dr = xmlDocGetRootElement(ctxt->myDoc);
+       if(dr != NULL)
+           r = findnode(dr->children, "Hubs");
+       if(r != NULL)
+           cr = findnode(r->children, "Columns");
+       if(cr == NULL)
+       {
+           msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("The hub list at %s cannot be understood"), pubhubaddr);
+           break;
+       }
+       for(c = findnode(cr->children, "Column"); c != NULL; c = findnode(c->next, "Column"))
+       {
+           name = (char *)xmlGetProp(c, (xmlChar *)"Name");
+           stype = (char *)xmlGetProp(c, (xmlChar *)"Type");
+           type = G_TYPE_INVALID;
+           if(stype != NULL)
+           {
+               if(!strcmp(stype, "string"))
+                   type = G_TYPE_STRING;
+               else if(!strcmp(stype, "int"))
+                   type = G_TYPE_INT;
+               else if(!strcmp(stype, "bytes"))
+                   type = G_TYPE_INT64;
+           }
+           if((name != NULL) && (type != G_TYPE_INVALID))
+           {
+               names = srealloc(names, (numcols + 1) * sizeof(*names));
+               types = srealloc(types, (numcols + 1) * sizeof(*names));
+               names[numcols] = sstrdup(name);
+               types[numcols] = type;
+               numcols++;
+           }
+           if(name != NULL)
+               xmlFree(name);
+           if(stype != NULL)
+               xmlFree(stype);
+       }
+       if(numcols == 0)
+       {
+           msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("The hub list at %s did not contain any columns"), pubhubaddr);
+           break;
+       }
+       for(i = 0; i < numcols; i++)
        {
-           p2 = buf;
-           for(i = 0; i < 4; i++)
+           if(!strcmp(names[i], "Address"))
            {
+               name = names[0];
+               names[0] = names[i];
+               names[i] = name;
+               type = types[0];
+               types[0] = types[i];
+               types[i] = type;
+               break;
+           }
+       }
+       if(i == numcols)
+       {
+           msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("The hub list at %s did not contain the address to any hubs"));
+           break;
+       }
+       model = gtk_list_store_newv(numcols, types);
+       for(n = findnode(r->children, "Hub"); n != NULL; n = findnode(n->next, "Hub"))
+       {
+           if(!xmlHasProp(n, (xmlChar *)"Address") || !xmlHasProp(n, (xmlChar *)"Name"))
+               continue;
+           if(filterpubhub)
+           {
+               match = 0;
+               attr = (char *)xmlGetProp(n, (xmlChar *)"Name");
+               if(!regexec(&pubhubfilter, attr, 0, NULL, 0))
+                   match = 1;
+               xmlFree(attr);
+               if((attr = (char *)xmlGetProp(n, (xmlChar *)"Description")) != NULL)
+               {
+                   if(!regexec(&pubhubfilter, attr, 0, NULL, 0))
+                       match = 1;
+                   xmlFree(attr);
+               }
+               if(!match)
+                   continue;
+           }
+           gtk_list_store_append(model, &iter);
+           for(i = 0; i < numcols; i++)
+           {
+               attr = (char *)xmlGetProp(n, (xmlChar *)names[i]);
+               if(attr != NULL)
+               {
+                   if(types[i] == G_TYPE_STRING)
+                       gtk_list_store_set(model, &iter, i, attr, -1);
+                   else if(types[i] == G_TYPE_INT)
+                       gtk_list_store_set(model, &iter, i, atoi(attr), -1);
+                   else if(types[i] == G_TYPE_INT64)
+                       gtk_list_store_set(model, &iter, i, strtoll(attr, NULL, 0), -1);
+                   xmlFree(attr);
+               }
+           }
+       }
+       cols = smalloc((numcols - 1) * sizeof(*cols));
+       for(i = 1; i < numcols; i++)
+           cols[i - 1] = i;
+       sortcol = 0;
+       for(i = 0; i < numcols; i++)
+       {
+           if(!strcmp(names[i], "Users"))
+               sortcol = i;
+       }
+       setpubhubmodel(GTK_TREE_MODEL(model), sortcol, numcols - 1, cols, names + 1);
+       free(cols);
+       g_object_unref(model);
+       break;
+    case PHO_FINI:
+       if(ctxt != NULL)
+       {
+           if(ctxt->myDoc != NULL)
+               xmlFreeDoc(ctxt->myDoc);
+           xmlFreeParserCtxt(ctxt);
+           ctxt = NULL;
+       }
+       break;
+    }
+    if(numcols != 0)
+    {
+       for(i = 0; i < numcols; i++)
+           free(names[i]);
+       free(names);
+       free(types);
+    }
+    return(0);
+}
+
+int pubhuboldhandler(int op, char *buf, size_t *len)
+{
+    static GtkListStore *model = NULL;
+    int i;
+    char *p, *p2;
+    wchar_t *wbuf;
+    char *fields[4], *names[3];
+    int cols[3];
+    GtkTreeIter iter;
+    
+    switch(op)
+    {
+    case PHO_INIT:
+       model = gtk_list_store_new(4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT);
+       break;
+    case PHO_DATA:
+       while((p = memchr(buf, '\n', *len)) != NULL)
+       {
+           *(p++) = 0;
+           for(i = 0, p2 = buf; i < 4; i++) {
                fields[i] = p2;
                if((p2 = strchr(p2, '|')) == NULL)
                    break;
                *(p2++) = 0;
            }
-           if(i == 4)
-           {
-               for(i = 0; i < 4; i++)
-               {
-                   if((wbuf = icsmbstowcs(fields[i], DCCHARSET, NULL)) == NULL)
-                   {
-                       /*
-                       msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not decode hublist - aborting at this point: %s"), strerror(errno));
-                       kill(pubhubproc, SIGINT);
-                       break;
-                       */
+           if(i == 4) {
+               for(i = 0; i < 4; i++) {
+                   if((wbuf = icsmbstowcs(fields[i], DCCHARSET, NULL)) == NULL) {
                        fields[i] = sstrdup(_("(Invalid character)"));
                    } else {
                        if((fields[i] = icwcstombs(wbuf, "UTF-8")) == NULL)
                            break;
                    }
                }
-               if(i == 4)
-               {
-                   gtk_list_store_append(pubhubmodel, &iter);
-                   gtk_list_store_set(pubhubmodel, &iter, 0, fields[0], 1, fields[1], 2, fields[2], 3, atoi(fields[3]), -1);
+               if(i == 4) {
+                   if(!filterpubhub || !regexec(&pubhubfilter, fields[0], 0, NULL, 0) || !regexec(&pubhubfilter, fields[2], 0, NULL, 0)) {
+                       gtk_list_store_append(model, &iter);
+                       gtk_list_store_set(model, &iter, 0, fields[1], 1, fields[0], 2, fields[2], 3, atoi(fields[3]), -1);
+                   }
                }
                for(i--; i >= 0; i--)
                    free(fields[i]);
            }
+           memmove(buf, p, *len -= p - buf);
+       }
+       break;
+    case PHO_EOF:
+       cols[0] = 3; names[0] = _("# users");
+       cols[1] = 1; names[1] = _("Name");
+       cols[2] = 2; names[2] = _("Description");
+       setpubhubmodel(GTK_TREE_MODEL(model), 3, 3, cols, names);
+       break;
+    case PHO_FINI:
+       if(model != NULL)
+           g_object_unref(model);
+       model = NULL;
+       break;
+    }
+    return(0);
+}
+
+void pubhubfdcallback(gpointer data, gint source, GdkInputCondition condition)
+{
+    static char buf[65536];
+    static size_t bufpos = 0;
+    int ret, reset;
+    
+    if(!(condition & GDK_INPUT_READ))
+       return;
+    if(bufpos == sizeof(buf))
+       bufpos = 0;
+    ret = read(pubhubfd, buf + bufpos, sizeof(buf) - bufpos);
+    reset = 0;
+    if(ret <= 0)
+    {
+       if(ret < 0)
+           msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not read from public hub listing process: %s"), strerror(errno));
+       else
+           pubhubhandler(PHO_EOF, NULL, NULL);
+       reset = 1;
+    } else {
+       bufpos += ret;
+       if(pubhubhandler(PHO_DATA, buf, &bufpos))
+           reset = 1;
+    }
+    if(reset)
+    {
+       pubhubhandler(PHO_FINI, NULL, NULL);
+       pubhubhandler = NULL;
+       gdk_input_remove(pubhubtag);
+       close(pubhubfd);
+       kill(pubhubproc, SIGINT);
+       pubhubfd = pubhubtag = -1;
+       pubhubproc = 0;
+       bufpos = 0;
+       if(filterpubhub)
+       {
+           regfree(&pubhubfilter);
+           filterpubhub = 0;
        }
-       memmove(buf, p, bufpos -= p - buf);
     }
-    sortmodel = gtk_tree_model_sort_new_with_model(GTK_TREE_MODEL(pubhubmodel));
-    if(sorted)
-       gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(sortmodel), sortcol, sortorder);
-    gtk_tree_view_set_model(GTK_TREE_VIEW(main_phublist), sortmodel);
 }
 
 void cb_main_pubhubfilter_activate(GtkWidget *widget, gpointer data)
 {
     int pipe1[2], pipe2[2];
     int len, err;
-    const char *buf;
+    const char *buf, *p;
     char errbuf[1024];
     
     if(pubhubtag >= 0)
@@ -1379,6 +1771,11 @@ void cb_main_pubhubfilter_activate(GtkWidget *widget, gpointer data)
        close(pubhubfd);
     if(pubhubproc > 0)
        kill(pubhubproc, SIGINT);
+    if(pubhubhandler != NULL)
+    {
+       pubhubhandler(PHO_FINI, NULL, NULL);
+       pubhubhandler = NULL;
+    }
     if(filterpubhub)
     {
        regfree(&pubhubfilter);
@@ -1397,7 +1794,6 @@ void cb_main_pubhubfilter_activate(GtkWidget *widget, gpointer data)
        }
        filterpubhub = 1;
     }
-    gtk_list_store_clear(pubhubmodel);
     pipe(pipe1);
     if((pubhubproc = fork()) == 0)
     {
@@ -1411,8 +1807,11 @@ void cb_main_pubhubfilter_activate(GtkWidget *widget, gpointer data)
     close(pipe1[1]);
     pubhubfd = pipe1[0];
     len = strlen(pubhubaddr);
-    if((len > 4) && !strcmp(pubhubaddr + len - 4, ".bz2"))
+    p = pubhubaddr + len;
+    if((len > 4) && !strncmp(p - 4, ".bz2", 4))
     {
+       p -= 4;
+       len -= 4;
        pipe(pipe2);
        if(fork() == 0)
        {
@@ -1429,6 +1828,15 @@ void cb_main_pubhubfilter_activate(GtkWidget *widget, gpointer data)
        close(pipe2[1]);
        pubhubfd = pipe2[0];
     }
+    if((len > 4) && !strncmp(p - 4, ".xml", 4))
+    {
+       p -= 4;
+       len -= 4;
+       pubhubhandler = pubhubxmlhandler;
+    } else {
+       pubhubhandler = pubhuboldhandler;
+    }
+    pubhubhandler(PHO_INIT, NULL, NULL);
     pubhubtag = gdk_input_add(pubhubfd, GDK_INPUT_READ, pubhubfdcallback, NULL);
 }
 
@@ -1449,7 +1857,7 @@ void cb_main_dcnctbtn_clicked(GtkWidget *widget, gpointer data)
        return;
     }
     gtk_tree_model_get(GTK_TREE_MODEL(fnmodel), &iter, 0, &id, -1);
-    tag = dc_queuecmd(NULL, NULL, L"dcnct", L"%%i", id, NULL);
+    tag = dc_queuecmd(NULL, NULL, L"dcnct", L"%i", id, NULL);
     if((resp = dc_gettaggedrespsync(tag)) != NULL)
     {
        if(resp->code == 502)
@@ -1467,7 +1875,7 @@ void cb_main_phublist_cchange(GtkWidget *widget, gpointer data)
     
     if(!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(main_phublist)), &model, &iter))
        return;
-    gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, 1, &addr, -1);
+    gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, 0, &addr, -1);
     gtk_entry_set_text(GTK_ENTRY(main_fnaddr), addr);
     g_free(addr);
 }
@@ -1488,13 +1896,13 @@ void cb_main_phublist_activate(GtkWidget *widget, GtkTreePath *path, GtkTreeView
     model = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
     if(!gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter, path))
        return;
-    gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, 1, &buf, -1);
+    gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, 0, &buf, -1);
     if(strchr(buf, ':') == NULL)
     {
        buf = g_realloc(buf, strlen(buf) + 5);
        strcat(buf, ":411");
     }
-    tag = dc_queuecmd(NULL, NULL, L"cnct", L"dc", L"%%s", buf, NULL);
+    tag = dc_queuecmd(NULL, NULL, L"cnct", L"dc", L"%s", buf, NULL);
     g_free(buf);
     gtk_entry_set_text(GTK_ENTRY(main_fnaddr), "");
     if((resp = dc_gettaggedrespsync(tag)) != NULL)
@@ -1503,6 +1911,8 @@ void cb_main_phublist_activate(GtkWidget *widget, GtkTreePath *path, GtkTreeView
            msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
        if(resp->code == 509)
            msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("The server could not parse that address"));
+       if(resp->code == 515)
+           msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("There are too many hubs connected"));
        dc_freeresp(resp);
     }
     handleresps();
@@ -1553,7 +1963,7 @@ void cb_main_chatstr_activate(GtkWidget *widget, gpointer data)
        return;
     }
     buf = gtk_entry_get_text(GTK_ENTRY(main_chatstr));
-    tag = dc_queuecmd(NULL, NULL, L"sendchat", L"%%i", curchat, L"1", L"", L"%%s", buf, NULL);
+    tag = dc_queuecmd(NULL, NULL, L"sendchat", L"%i", curchat, L"1", L"", L"%s", buf, NULL);
     if((resp = dc_gettaggedrespsync(tag)) != NULL)
     {
        if(resp->code == 502)
@@ -1643,7 +2053,7 @@ void cb_main_srchbtn_clicked(GtkWidget *widget, gpointer data)
        msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Please enter a search expression before searching"));
        return;
     }
-    tag = dc_queuecmd(NULL, NULL, L"search", L"all", L"%%a", toks, NULL);
+    tag = dc_queuecmd(NULL, NULL, L"search", L"all", L"%a", toks, NULL);
     dc_freewcsarr(toks);
     if((resp = dc_gettaggedrespsync(tag)) != NULL)
     {
@@ -1678,7 +2088,7 @@ void cb_main_srchcanbtn_clicked(GtkWidget *widget, gpointer data)
 {
     if(nextsrch == -1)
        return;
-    dc_queuecmd(NULL, NULL, L"cansrch", L"%%i", nextsrch, NULL);
+    dc_queuecmd(NULL, NULL, L"cansrch", L"%i", nextsrch, NULL);
     nextsrch = -1;
     gtk_widget_set_sensitive(main_realsrch, TRUE);
     gtk_widget_set_sensitive(main_simplesrch, TRUE);
@@ -1701,7 +2111,7 @@ gboolean cb_main_trlist_keypress(GtkWidget *widget, GdkEventKey *event, gpointer
        if(gtk_tree_selection_get_selected(sel, &model, &iter))
        {
            gtk_tree_model_get(model, &iter, 0, &id, -1);
-           tag = dc_queuecmd(NULL, NULL, L"cancel", L"%%i", id, NULL);
+           tag = dc_queuecmd(NULL, NULL, L"cancel", L"%i", id, NULL);
            if((resp = dc_gettaggedrespsync(tag)) != NULL)
            {
                if(resp->code == 502)
@@ -1765,9 +2175,9 @@ void cb_main_srchres_activate(GtkWidget *widget, GtkTreePath *path, GtkTreeViewC
     g_free(tfilename);
     arg = (char *)gtk_entry_get_text(GTK_ENTRY(main_dlarg));
     if(*arg)
-       tag = dc_queuecmd(NULL, NULL, L"download", fnet, L"%%ls", peerid, L"%%ls", filename, L"%%i", size, L"hash", L"%%ls", (hash == NULL)?L"":hash, L"user", L"%%s", arg, NULL);
+       tag = dc_queuecmd(NULL, NULL, L"download", fnet, L"%ls", peerid, L"%ls", filename, L"%i", size, L"hash", L"%ls", (hash == NULL)?L"":hash, L"user", L"%s", arg, NULL);
     else
-       tag = dc_queuecmd(NULL, NULL, L"download", fnet, L"%%ls", peerid, L"%%ls", filename, L"%%i", size, L"hash", L"%%ls", (hash == NULL)?L"":hash, NULL);
+       tag = dc_queuecmd(NULL, NULL, L"download", fnet, L"%ls", peerid, L"%ls", filename, L"%i", size, L"hash", L"%ls", (hash == NULL)?L"":hash, NULL);
     free(fnet);
     free(peerid);
     free(filename);
@@ -1899,7 +2309,7 @@ void cb_main_trcancel_activate(GtkWidget *widget, gpointer data)
     if(gtk_tree_selection_get_selected(sel, &model, &iter))
     {
        gtk_tree_model_get(model, &iter, 0, &id, -1);
-       tag = dc_queuecmd(NULL, NULL, L"cancel", L"%%i", id, NULL);
+       tag = dc_queuecmd(NULL, NULL, L"cancel", L"%i", id, NULL);
        if((resp = dc_gettaggedrespsync(tag)) != NULL)
        {
            if(resp->code == 502)
@@ -2005,7 +2415,7 @@ int rmres(char *id)
     struct dc_response *resp;
     
     ret = -1;
-    tag = dc_queuecmd(NULL, NULL, L"filtercmd", L"rmres", L"%%s", id, NULL);
+    tag = dc_queuecmd(NULL, NULL, L"filtercmd", L"rmres", L"%s", id, NULL);
     if((resp = dc_gettaggedrespsync(tag)) != NULL)
     {
        if(resp->numlines > 0)
@@ -2168,8 +2578,8 @@ int main(int argc, char **argv)
     gtk_init(&argc, &argv);
     dc_init();
     signal(SIGCHLD, SIG_IGN);
-    pubhubaddr = sstrdup("http://www.hublist.org/PublicHubList.config.bz2");
-    dcserver = sstrdup("localhost");
+    pubhubaddr = sstrdup("http://www.hublist.org/PublicHubList.xml.bz2");
+    dcserver = sstrdup("");
     if((pwent = getpwuid(getuid())) == NULL)
     {
        fprintf(stderr, "could not get your passwd data");
@@ -2188,12 +2598,6 @@ int main(int argc, char **argv)
     reslist = gtk_list_store_new(6, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_STRING);
     gtk_tree_view_set_model(GTK_TREE_VIEW(reslist_list), GTK_TREE_MODEL(reslist));
     
-    pubhubmodel = gtk_list_store_new(4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT);
-    sortmodel = gtk_tree_model_sort_new_with_model(GTK_TREE_MODEL(pubhubmodel));
-    gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(sortmodel), 3, GTK_SORT_DESCENDING);
-    gtk_tree_view_set_model(GTK_TREE_VIEW(main_phublist), GTK_TREE_MODEL(sortmodel));
-    g_object_unref(sortmodel);
-
     dlmodel = gtk_list_store_new(13, G_TYPE_INT, /* id */
                                 G_TYPE_INT,     /* dir */
                                 G_TYPE_INT,     /* state */
@@ -2239,6 +2643,7 @@ int main(int argc, char **argv)
        dcconnect(dcserver);
     g_timeout_add(500, srchstatupdatecb, NULL);
     g_timeout_add(5000, ksupdatecb, NULL);
+    g_timeout_add(1000, updatetransfers, NULL);
     gtk_main();
     return(0);
 }