Typo.
[doldaconnect.git] / clients / gtk2 / main.c
1 /*
2  *  Dolda Connect - Modular multiuser Direct Connect-style client
3  *  Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com)
4  *  
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *  
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *  
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <malloc.h>
24 #include <stdarg.h>
25 #include <gtk/gtk.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <doldaconnect/uilib.h>
30 #include <doldaconnect/uimisc.h>
31 #include <doldaconnect/utils.h>
32 #include <errno.h>
33 #include <regex.h>
34 #include <signal.h>
35 #include <time.h>
36 #include <sys/time.h>
37 #include <pwd.h>
38 #include <locale.h>
39 #include <libintl.h>
40 #include <assert.h>
41
42 #ifdef HAVE_CONFIG_H
43 #include <config.h>
44 #endif
45 #include "progressbar.h"
46
47 #define TRHISTSIZE 10
48
49 struct trdata
50 {
51     size_t poshist[TRHISTSIZE];
52     double timehist[TRHISTSIZE];
53     int hc;
54 };
55
56 struct fndata
57 {
58     GtkTextBuffer *textbuf;
59 };
60
61 struct srchsize
62 {
63     int size;
64     int num;
65     int slots;
66     double resptime;
67     GtkTreeRowReference *ref;
68 };
69
70 struct knownspeed
71 {
72     char *userid;
73     int speed, seq;
74     time_t fetched;
75 };
76
77 GtkWidget *inpdialog;
78 GtkListStore *fnmodel, *ulmodel, *dlmodel, *pubhubmodel, *reslist;
79 GtkTreeStore *srchmodel;
80 GtkTreeModelFilter *srchmodelfilter;
81 GtkTextTagTable *chattags;
82 int dcfd = -1, gdkread = -1, gdkwrite = -1;
83 int pubhubfd = -1, pubhubtag = -1, filterpubhub = 0;
84 int curchat = -1;
85 regex_t pubhubfilter;
86 pid_t pubhubproc = 0;
87 char *pubhubaddr = NULL;
88 char *connectas = NULL;
89 char *dcserver = NULL;
90 int autoconn = 0;
91 int srchautoupdate = 0;
92 int cursrch = -1, nextsrch = -1;
93 time_t srcheta;
94 struct srchsize *srchsizes = NULL;
95 struct knownspeed *knownspeeds = NULL;
96 int numsizes = 0, numspeeds = 0, ksqueryseq = -1, ksquerytag = -1, lsrestag = -1;
97
98 gboolean initdeath(GtkWidget *, gpointer);
99 void cb_main_connmenu_activate(GtkWidget *widget, gpointer data);
100 void cb_main_dconnmenu_activate(GtkWidget *widget, gpointer data);
101 void cb_main_prefmenu_activate(GtkWidget *widget, gpointer data);
102 void cb_main_lsres_activate(GtkWidget *widget, gpointer data);
103 void cb_main_sdmenu_activate(GtkWidget *widget, gpointer data);
104 void cb_inpdialog_entry_activate(GtkWidget *widget, gpointer data);
105 void cb_main_fnaddr_activate(GtkWidget *widget, gpointer data);
106 void cb_main_pubhubfilter_activate(GtkWidget *widget, gpointer data);
107 void cb_main_dcnctbtn_clicked(GtkWidget *widget, gpointer data);
108 void cb_main_phublist_cchange(GtkWidget *widget, gpointer data);
109 void cb_main_phublist_activate(GtkWidget *widget, GtkTreePath *path, GtkTreeViewColumn *col, gpointer data);
110 void cb_main_chatnodes_activate(GtkWidget *widget, GtkTreePath *path, GtkTreeViewColumn *col, gpointer data);
111 void cb_main_srchres_activate(GtkWidget *widget, GtkTreePath *path, GtkTreeViewColumn *col, gpointer data);
112 void cb_main_chatstr_activate(GtkWidget *widget, gpointer data);
113 void cb_main_simplesrch_changed(GtkWidget *widget, gpointer data);
114 void cb_main_realsrch_changed(GtkWidget *widget, gpointer data);
115 void cb_main_srchbtn_clicked(GtkWidget *widget, gpointer data);
116 void cb_main_srchcanbtn_clicked(GtkWidget *widget, gpointer data);
117 gboolean cb_main_trlist_keypress(GtkWidget *widget, GdkEventKey *event, gpointer data);
118 void cb_main_filternoslots_toggled(GtkToggleButton *widget, gpointer data);
119 void cb_main_srhash_activate(GtkWidget *widget, gpointer data);
120 void cb_main_srcopy_activate(GtkWidget *widget, gpointer data);
121 void cb_main_trhash_activate(GtkWidget *widget, gpointer data);
122 void cb_main_trcopy_activate(GtkWidget *widget, gpointer data);
123 void cb_main_trcancel_activate(GtkWidget *widget, gpointer data);
124 gboolean cb_main_srpopup(GtkWidget *widget, GdkEventButton *event, gpointer data);
125 gboolean cb_main_trpopup(GtkWidget *widget, GdkEventButton *event, gpointer data);
126 void cb_reslist_reload_clicked(GtkWidget *widget, gpointer data);
127 void cb_reslist_delete_clicked(GtkWidget *widget, gpointer data);
128 void cb_reslist_search_clicked(GtkWidget *widget, gpointer data);
129 void cb_reslist_list_cchange(GtkWidget *widget, gpointer data);
130 void cb_reslist_list_activate(GtkWidget *widget, GtkTreePath *path, GtkTreeViewColumn *col, gpointer data);
131 gboolean cb_reslist_list_keypress(GtkWidget *widget, GdkEventKey *event, gpointer data);
132 void dcfdcallback(gpointer data, gint source, GdkInputCondition condition);
133 void srchstatupdate(void);
134 void transnicebytefunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data);
135 void transspeedinfo(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data);
136 void transerrorinfo(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data);
137 void percentagefunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data);
138 void hidezerofunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data);
139 void speedtimefunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data);
140
141 #define DCCHARSET "windows-1252"
142
143 #define _(text) gettext(text)
144
145 #include "mainwnd.gtk"
146 #include "inpdialog.gtk"
147 #include "pref.gtk"
148 #include "reslist.gtk"
149
150 void updatewrite(void)
151 {
152     if(dcfd < 0)
153         return;
154     if(dc_wantwrite())
155     {
156         if(gdkwrite == -1)
157             gdkwrite = gdk_input_add(dcfd, GDK_INPUT_WRITE, dcfdcallback, NULL);
158     } else {
159         if(gdkwrite != -1)
160         {
161             gdk_input_remove(gdkwrite);
162             gdkwrite = -1;
163         }
164     }
165 }
166
167 double ntime(void)
168 {
169     struct timeval tv;
170     
171     gettimeofday(&tv, NULL);
172     return((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
173 }
174
175 void fndestroycb(struct dc_fnetnode *fn)
176 {
177     struct fndata *data;
178     GtkTextBuffer *textbuf;
179     
180     data = fn->udata;
181     g_object_unref(data->textbuf);
182     free(data);
183     if(curchat == fn->id)
184     {
185         textbuf = gtk_text_buffer_new(chattags);
186         gtk_text_view_set_buffer(GTK_TEXT_VIEW(main_chatview), textbuf);
187         g_object_unref(textbuf);
188     }
189 }
190
191 void addfndata(struct dc_fnetnode *fn)
192 {
193     struct fndata *data;
194     
195     if(fn->udata != NULL)
196         return;
197     fn->destroycb = fndestroycb;
198     data = smalloc(sizeof(*data));
199     data->textbuf = gtk_text_buffer_new(chattags);
200     fn->udata = data;
201 }
202
203 void trdestroycb(struct dc_transfer *tr)
204 {
205     free(tr->udata);
206 }
207
208 void addtrdata(struct dc_transfer *tr)
209 {
210     struct trdata *data;
211     
212     if(tr->udata != NULL)
213         return;
214     tr->destroycb = trdestroycb;
215     data = smalloc(sizeof(*data));
216     memset(data, 0, sizeof(*data));
217     tr->udata = data;
218 }
219
220 void updatetrdata(struct dc_transfer *tr)
221 {
222     int i;
223     struct trdata *data;
224     
225     data = tr->udata;
226     if(data->hc < TRHISTSIZE)
227     {
228         data->poshist[data->hc] = tr->curpos;
229         data->timehist[data->hc] = ntime();
230         data->hc++;
231     } else {
232         for(i = 0; i < TRHISTSIZE - 1; i++)
233         {
234             data->poshist[i] = data->poshist[i + 1];
235             data->timehist[i] = data->timehist[i + 1];
236         }
237         data->poshist[i] = tr->curpos;
238         data->timehist[i] = ntime();
239     }
240 }
241
242 char *getfnstatestock(int state)
243 {
244     if(state == DC_FNN_STATE_SYN)
245         return("gtk-jump-to");
246     if(state == DC_FNN_STATE_HS)
247         return("gtk-execute");
248     if(state == DC_FNN_STATE_EST)
249         return("gtk-yes");
250     if(state == DC_FNN_STATE_DEAD)
251         return("gtk-cancel");
252     return(NULL);
253 }
254
255 void updatehublist(void)
256 {
257     int done;
258     struct dc_fnetnode *fn;
259     GtkTreeIter iter;
260     int id;
261     char *buf;
262     char *name;
263     int state, numusers;
264     
265     for(fn = dc_fnetnodes; fn != NULL; fn = fn->next)
266         fn->found = 0;
267     done = 0;
268     while(!done)
269     {
270         done = 1;
271         if(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(fnmodel), &iter))
272         {
273             do
274             {
275                 gtk_tree_model_get(GTK_TREE_MODEL(fnmodel), &iter, 0, &id, -1);
276                 if((fn = dc_findfnetnode(id)) == NULL)
277                 {
278                     /* I can't seem to get a sensible reply fromp
279                      * gtk_list_store, so I'm just doing this
280                      * instead. */
281                     gtk_list_store_remove(fnmodel, &iter);
282                     done = 0;
283                     break;
284                 } else {
285                     gtk_tree_model_get(GTK_TREE_MODEL(fnmodel), &iter, 1, &name, 2, &state, 3, &numusers, -1);
286                     if(fn->name == NULL)
287                         buf = _("Unknown");
288                     else
289                         buf = icswcstombs(fn->name, "UTF-8", NULL);
290                     if(strcmp(buf, name))
291                         gtk_list_store_set(fnmodel, &iter, 1, buf, -1);
292                     if(state != fn->state)
293                     {
294                         gtk_list_store_set(fnmodel, &iter, 2, fn->state, -1);
295                         gtk_list_store_set(fnmodel, &iter, 4, getfnstatestock(fn->state), -1);
296                     }
297                     if(numusers != fn->numusers)
298                         gtk_list_store_set(fnmodel, &iter, 3, fn->numusers, -1);
299                     g_free(name);
300                     fn->found = 1;
301                 }
302             } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(fnmodel), &iter));
303         }
304     }
305     for(fn = dc_fnetnodes; fn != NULL; fn = fn->next)
306     {
307         if(!fn->found)
308         {
309             if(fn->name == NULL)
310                 buf = _("Unknown");
311             else
312                 buf = icswcstombs(fn->name, "UTF-8", NULL);
313             gtk_list_store_append(fnmodel, &iter);
314             gtk_list_store_set(fnmodel, &iter, 0, fn->id, 1, buf, 2, fn->state, 3, fn->numusers, 4, getfnstatestock(fn->state), -1);
315             addfndata(fn);
316         }
317     }
318 }
319
320 char *bytes2si(int bytes)
321 {
322     int i;
323     double b;
324     char *sd;
325     static char ret[64];
326     
327     b = bytes;
328     for(i = 0; (b > 1024) && (i < 4); i++)
329         b /= 1024;
330     if(i == 0)
331         sd = "B";
332     else if(i == 1)
333         sd = "kiB";
334     else if(i == 2)
335         sd = "MiB";
336     else if(i == 3)
337         sd = "GiB";
338     else
339         sd = "TiB";
340     snprintf(ret, 64, "%.1f %s", b, sd);
341     return(ret);
342 }
343
344 void percentagefunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
345 {
346     int colnum;
347     float val;
348     char buf[64];
349     
350     colnum = (int)data;
351     gtk_tree_model_get(model, iter, colnum, &val, -1);
352     snprintf(buf, 64, "%.2f%%", (double)(val * 100.0));
353     g_object_set(rend, "text", buf, NULL);
354 }
355
356 void transnicebytefunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
357 {
358     int colnum, val;
359     char buf[64];
360     
361     colnum = (int)data;
362     gtk_tree_model_get(model, iter, colnum, &val, -1);
363 /*
364     if(val >= 0)
365         strcpy(buf, bytes2si(val));
366 */
367     if(val >= 0)
368         snprintf(buf, 64, "%'i", val);
369     else
370         strcpy(buf, _("Unknown"));
371     g_object_set(rend, "text", buf, NULL);
372 }
373
374 void hidezerofunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
375 {
376     int colnum, val;
377     char buf[64];
378     
379     colnum = (int)data;
380     gtk_tree_model_get(model, iter, colnum, &val, -1);
381     if(val > 0)
382         snprintf(buf, 64, "%i", val);
383     else
384         strcpy(buf, "");
385     g_object_set(rend, "text", buf, NULL);
386 }
387
388 void speedtimefunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
389 {
390     int speed, size, time;
391     char buf[64];
392     
393     gtk_tree_model_get(model, iter, 4, &size, 8, &speed, -1);
394     if(speed > 0)
395     {
396         time = (size / speed) / 60;
397         if(time < 1)
398             snprintf(buf, 64, "%'i (<00:01)", speed);
399         else
400             snprintf(buf, 64, "%'i (%02i:%02i)", speed, time / 60, time % 60);
401     } else if(speed == 0) {
402         strcpy(buf, "0");
403     } else {
404         strcpy(buf, _("Unknown"));
405     }
406     g_object_set(rend, "text", buf, NULL);
407 }
408
409 void transspeedinfo(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
410 {
411     int id;
412     struct dc_transfer *tr;
413     struct trdata *d;
414     char buf[64];
415     int speed;
416     
417     gtk_tree_model_get(model, iter, 0, &id, -1);
418     if((tr = dc_findtransfer(id)) != NULL)
419     {
420         d = tr->udata;
421         if((tr->state != DC_TRNS_MAIN) || (d == NULL))
422         {
423             buf[0] = 0;
424         } else if(d->hc < 2) {
425             strcpy(buf, "...");
426         } else {
427             speed = (((double)(d->poshist[d->hc - 1] - d->poshist[0])) / (d->timehist[d->hc - 1] - d->timehist[0]));
428             snprintf(buf, 64, "%s/s", bytes2si(speed));
429         }
430     } else {
431         buf[0] = 0;
432     }
433     g_object_set(rend, "text", buf, NULL);
434 }
435
436 void transerrorinfo(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
437 {
438     int error;
439     time_t errortime;
440     char finbuf[64], tbuf[64], *errstr;
441     
442     gtk_tree_model_get(model, iter, 10, &error, 11, &errortime, -1);
443     if(error != DC_TRNSE_NOERROR)
444     {
445         if(error == DC_TRNSE_NOTFOUND)
446             errstr = _("Not found");
447         else if(error == DC_TRNSE_NOSLOTS)
448             errstr = _("No slots");
449         strftime(tbuf, 64, _("%H:%M:%S"), localtime(&errortime));
450         snprintf(finbuf, 64, _("%s (reported at %s)"), errstr, tbuf);
451     } else {
452         *finbuf = 0;
453     }
454     g_object_set(rend, "text", finbuf, NULL);
455 }
456
457 char *gettrstatestock(int state)
458 {
459     if(state == DC_TRNS_WAITING)
460         return("gtk-jump-to");
461     if(state == DC_TRNS_HS)
462         return("gtk-execute");
463     if(state == DC_TRNS_MAIN)
464         return("gtk-network");
465     if(state == DC_TRNS_DONE)
466         return("gtk-yes");
467     return(NULL);
468 }
469
470 gint updatetransfers(gpointer data)
471 {
472     struct dc_transfer *tr;
473     struct trdata *d;
474     double now;
475     
476     now = ntime();
477     for(tr = dc_transfers; tr != NULL; tr = tr->next)
478     {
479         if((d = tr->udata) != NULL)
480         {
481             if((d->hc > 0) && ((now - d->timehist[d->hc - 1]) > 2))
482                 updatetrdata(tr);
483         }
484     }
485     return(TRUE);
486 }
487
488 void updatetransferlists(void)
489 {
490     int i;
491     int done;
492     struct dc_transfer *transfer;
493     GtkTreeIter iter;
494     int id;
495     char *buf;
496     char *peerid, *peernick, *path, *hash;
497     int state, dir, size, curpos, error;
498     time_t errortime;
499     GtkListStore *stores[3];
500     
501     for(transfer = dc_transfers; transfer != NULL; transfer = transfer->next)
502         transfer->found = 0;
503     stores[DC_TRNSD_UNKNOWN] = NULL;
504     stores[DC_TRNSD_UP] = ulmodel;
505     stores[DC_TRNSD_DOWN] = dlmodel;
506     for(i = 0; i < 3; i++)
507     {
508         if(stores[i] == NULL)
509             continue;
510         done = 0;
511         while(!done)
512         {
513             done = 1;
514             if(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(stores[i]), &iter))
515             {
516                 do
517                 {
518                     gtk_tree_model_get(GTK_TREE_MODEL(stores[i]), &iter, 0, &id, 1, &dir, -1);
519                     if(((transfer = dc_findtransfer(id)) == NULL) || (transfer->dir != dir))
520                     {
521                         gtk_list_store_remove(stores[i], &iter);
522                         done = 0;
523                         break;
524                     } else {
525                         transfer->found = 1;
526                         gtk_tree_model_get(GTK_TREE_MODEL(stores[i]), &iter, 2, &state, 3, &peerid, 4, &peernick, 5, &path, 6, &size, 7, &curpos, 10, &error, 11, &errortime, 12, &hash, -1);
527                         if(state != transfer->state)
528                             gtk_list_store_set(stores[i], &iter, 2, transfer->state, 8, gettrstatestock(transfer->state), -1);
529                         if(size != transfer->size)
530                             gtk_list_store_set(stores[i], &iter, 6, transfer->size, -1);
531                         if(curpos != transfer->curpos)
532                         {
533                             gtk_list_store_set(stores[i], &iter, 7, transfer->curpos, -1);
534                             if(transfer->udata != NULL)
535                                 updatetrdata(transfer);
536                         }
537                         if(error != transfer->error)
538                             gtk_list_store_set(stores[i], &iter, 10, transfer->error, -1);
539                         if(errortime != transfer->errortime)
540                             gtk_list_store_set(stores[i], &iter, 11, transfer->errortime, -1);
541                         if((transfer->size > 0) && (transfer->curpos > 0))
542                             gtk_list_store_set(stores[i], &iter, 9, (float)transfer->curpos / (float)transfer->size, -1);
543                         buf = icswcstombs(transfer->peerid, "UTF-8", NULL);
544                         if(strcmp(buf, peerid))
545                             gtk_list_store_set(stores[i], &iter, 3, buf, -1);
546                         buf = icswcstombs(((transfer->peernick == NULL) || (transfer->peernick[0] == L'\0'))?transfer->peerid:transfer->peernick, "UTF-8", NULL);
547                         if(strcmp(buf, peernick))
548                             gtk_list_store_set(stores[i], &iter, 4, buf, -1);
549                         buf = (transfer->path == NULL)?_("Unknown"):icswcstombs(transfer->path, "UTF-8", NULL);
550                         if(strcmp(buf, path))
551                             gtk_list_store_set(stores[i], &iter, 5, buf, -1);
552                         buf = (transfer->hash == NULL)?"":icswcstombs(transfer->hash, "UTF-8", NULL);
553                         if(strcmp(buf, path))
554                             gtk_list_store_set(stores[i], &iter, 12, buf, -1);
555                         g_free(hash);
556                         g_free(peerid);
557                         g_free(peernick);
558                         g_free(path);
559                     }
560                 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(stores[i]), &iter));
561             }
562         }
563     }
564     for(transfer = dc_transfers; transfer != NULL; transfer = transfer->next)
565     {
566         if(!transfer->found)
567         {
568             if(stores[transfer->dir] != NULL)
569             {
570                 peerid = icwcstombs(transfer->peerid, "UTF-8");
571                 peernick = icwcstombs(((transfer->peernick == NULL) || (transfer->peernick[0] == L'\0'))?transfer->peerid:transfer->peernick, "UTF-8");
572                 path = (transfer->path == NULL)?_("Unknown"):icwcstombs(transfer->path, "UTF-8");
573                 hash = (transfer->hash == NULL)?"":icwcstombs(transfer->hash, "UTF-8");
574                 gtk_list_store_append(stores[transfer->dir], &iter);
575                 gtk_list_store_set(stores[transfer->dir], &iter,
576                                    0, transfer->id,
577                                    1, transfer->dir,
578                                    2, transfer->state,
579                                    3, peerid,
580                                    4, peernick,
581                                    5, path,
582                                    6, transfer->size,
583                                    7, transfer->curpos,
584                                    8, gettrstatestock(transfer->state),
585                                    9, 0.0,
586                                    10, transfer->error,
587                                    11, transfer->errortime,
588                                    12, hash,
589                                    -1);
590                 free(peerid);
591                 free(peernick);
592                 if(transfer->path != NULL)
593                     free(path);
594                 if(transfer->hash != NULL)
595                     free(hash);
596                 addtrdata(transfer);
597             }
598         }
599     }
600 }
601
602 void updatesbar(char *msg)
603 {
604     gtk_statusbar_pop(GTK_STATUSBAR(main_statusbar), 0);
605     gtk_statusbar_push(GTK_STATUSBAR(main_statusbar), 0, msg);
606 }
607
608 void freesrchsizes(void)
609 {
610     int i;
611     
612     for(i = 0; i < numsizes; i++)
613     {
614         if(srchsizes[i].ref != NULL)
615             gtk_tree_row_reference_free(srchsizes[i].ref);
616     }
617     if(srchsizes != NULL)
618         free(srchsizes);
619     srchsizes = NULL;
620     numsizes = 0;
621 }
622
623 void dcdisconnected(void)
624 {
625     if(gdkread != -1)
626     {
627         gdk_input_remove(gdkread);
628         gdkread = -1;
629     }
630     dcfd = -1;
631     updatehublist();
632     updatetransferlists();
633     cursrch = nextsrch = -1;
634     gtk_tree_store_clear(srchmodel);
635     freesrchsizes();
636     gtk_widget_set_sensitive(main_connmenu, TRUE);
637     gtk_widget_set_sensitive(main_dconnmenu, FALSE);
638     gtk_widget_set_sensitive(main_simplesrch, TRUE);
639     gtk_widget_set_sensitive(main_realsrch, TRUE);
640     gtk_widget_set_sensitive(main_srchbtn, TRUE);
641     gtk_widget_set_sensitive(main_srchcanbtn, FALSE);
642     updatesbar(_("Disconnected"));
643 }
644
645 char *inputbox(char *title, char *prompt, char *def, int echo)
646 {
647     int resp;
648     GtkWidget *swnd;
649     char *buf;
650     
651     inpdialog = gtk_dialog_new_with_buttons(title, GTK_WINDOW(main_wnd), GTK_DIALOG_MODAL, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL);
652     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(inpdialog)->vbox), swnd = create_inpdialog_wnd(), TRUE, TRUE, 0);
653     gtk_widget_show(swnd);
654     if(!echo)
655         gtk_entry_set_visibility(GTK_ENTRY(inpdialog_entry), FALSE);
656     gtk_label_set_text(GTK_LABEL(inpdialog_prompt), prompt);
657     gtk_entry_set_text(GTK_ENTRY(inpdialog_entry), def);
658     resp = gtk_dialog_run(GTK_DIALOG(inpdialog));
659     if(!echo)
660         gtk_entry_set_visibility(GTK_ENTRY(inpdialog_entry), TRUE);
661     if(resp == GTK_RESPONSE_ACCEPT)
662         buf = strdup(gtk_entry_get_text(GTK_ENTRY(inpdialog_entry)));
663     else
664         buf = NULL;
665     gtk_widget_destroy(inpdialog);
666     updatewrite();
667     return(buf);
668 }
669
670 int msgbox(int type, int buttons, char *format, ...)
671 {
672     GtkWidget *swnd;
673     va_list args;
674     char *buf;
675     int resp;
676     
677     va_start(args, format);
678     buf = vsprintf2(format, args);
679     va_end(args);
680     swnd = gtk_message_dialog_new(GTK_WINDOW(main_wnd), GTK_DIALOG_MODAL, type, buttons, "%s", buf);
681     resp = gtk_dialog_run(GTK_DIALOG(swnd));
682     gtk_widget_destroy(swnd);
683     free(buf);
684     return(resp);
685 }
686
687 void readconfigfile(void)
688 {
689     FILE *cfgfile;
690     char *homedir, *buf, *p;
691     int w, h;
692     
693     if((homedir = getenv("HOME")) == NULL)
694     {
695         fprintf(stderr, "warning: could not find home directory!\n");
696         return;
697     }
698     buf = sprintf2("%s/.dolconrc", homedir);
699     if((cfgfile = fopen(buf, "r")) == NULL)
700     {
701         if(errno != ENOENT)
702             perror(buf);
703         free(buf);
704         return;
705     }
706     free(buf);
707     buf = smalloc(1024);
708     while(fgets(buf, 1024, cfgfile) != NULL)
709     {
710         if(strlen(buf) < 1)
711             continue;
712         p = buf + strlen(buf);
713         if(p[-1] == '\n')
714             *(--p) = 0;
715         if((p = strchr(buf, ':')) == NULL)
716             continue;
717         *(p++) = 0;
718         while((*p == ' ') || (*p == '\t'))
719             p++;
720         if(!strcmp(buf, "wnd-width"))
721         {
722             w = atoi(p);
723         } else if(!strcmp(buf, "wnd-height")) {
724             h = atoi(p);
725         } else if(!strcmp(buf, "pane1-pos")) {
726             gtk_paned_set_position(GTK_PANED(main_pane1), atoi(p));
727         } else if(!strcmp(buf, "pane2-pos")) {
728             gtk_paned_set_position(GTK_PANED(main_pane2), atoi(p));
729         } else if(!strcmp(buf, "pane3-pos")) {
730             gtk_paned_set_position(GTK_PANED(main_pane3), atoi(p));
731         } else if(!strcmp(buf, "pubhubaddr")) {
732             free(pubhubaddr);
733             pubhubaddr = sstrdup(p);
734         } else if(!strcmp(buf, "dcserver")) {
735             free(dcserver);
736             dcserver = sstrdup(p);
737         } else if(!strcmp(buf, "advexpanded")) {
738             gtk_expander_set_expanded(GTK_EXPANDER(main_advexp), atoi(p));
739         } else if(!strcmp(buf, "connectas")) {
740             free(connectas);
741             connectas = sstrdup(p);
742         } else if(!strcmp(buf, "autoconn")) {
743             autoconn = atoi(p);
744         } else if(!strcmp(buf, "filternoslots")) {
745             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(main_filternoslots), atoi(p));
746         }
747     }
748     free(buf);
749     fclose(cfgfile);
750 /*
751     if(w != 1589)
752         abort();
753 */
754     gtk_window_resize(GTK_WINDOW(main_wnd), w, h);
755 }
756
757 void updateconfigfile(void)
758 {
759     FILE *cfgfile;
760     char *homedir, *buf;
761     int w, h;
762     
763     if((homedir = getenv("HOME")) == NULL)
764     {
765         msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not get your home directory!"));
766         return;
767     }
768     buf = sprintf2("%s/.dolconrc", homedir);
769     if((cfgfile = fopen(buf, "w")) == NULL)
770     {
771         free(buf);
772         msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not open configuration file for writing: %s"), strerror(errno));
773         return;
774     }
775     free(buf);
776     gtk_window_get_size(GTK_WINDOW(main_wnd), &w, &h);
777     fprintf(cfgfile, "wnd-width: %i\n", w);
778     fprintf(cfgfile, "wnd-height: %i\n", h);
779     fprintf(cfgfile, "pane1-pos: %i\n", gtk_paned_get_position(GTK_PANED(main_pane1)));
780     fprintf(cfgfile, "pane2-pos: %i\n", gtk_paned_get_position(GTK_PANED(main_pane2)));
781     fprintf(cfgfile, "pane3-pos: %i\n", gtk_paned_get_position(GTK_PANED(main_pane3)));
782     fprintf(cfgfile, "pubhubaddr: %s\n", pubhubaddr);
783     fprintf(cfgfile, "dcserver: %s\n", dcserver);
784     fprintf(cfgfile, "advexpanded: %i\n", gtk_expander_get_expanded(GTK_EXPANDER(main_advexp)));
785     fprintf(cfgfile, "connectas: %s\n", connectas);
786     fprintf(cfgfile, "autoconn: %i\n", autoconn);
787     fprintf(cfgfile, "filternoslots: %i\n", gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(main_filternoslots)));
788     fclose(cfgfile);
789 }
790
791 gboolean initdeath(GtkWidget *widget, gpointer data)
792 {
793     updateconfigfile();
794     gtk_main_quit();
795     return(TRUE);
796 }
797
798 void cb_inpdialog_entry_activate(GtkWidget *widget, gpointer data)
799 {
800     gtk_dialog_response(GTK_DIALOG(inpdialog), GTK_RESPONSE_ACCEPT);
801 }
802
803 int loginconv(int type, wchar_t *prompt, char **resp, void *data)
804 {
805     int ret;
806     char *buf;
807     
808     ret = 0;
809     buf = icwcstombs(prompt, "UTF-8");
810     switch(type)
811     {
812     case DC_LOGIN_CONV_NOECHO:
813         if((*resp = inputbox(_("Login"), buf, "", 0)) == NULL)
814             ret = 1;
815         break;
816     case DC_LOGIN_CONV_ECHO:
817         if((*resp = inputbox(_("Login"), buf, "", 1)) == NULL)
818             ret = 1;
819         break;
820     case DC_LOGIN_CONV_INFO:
821         msgbox(GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "%s", buf);
822         break;
823     case DC_LOGIN_CONV_ERROR:
824         msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", buf);
825         break;
826     }
827     free(buf);
828     updatewrite();
829     return(ret);
830 }
831
832 void getfnlistcallback(int resp, void *data)
833 {
834     updatehublist();
835 }
836
837 void gettrlistcallback(int resp, void *data)
838 {
839     updatetransferlists();
840 }
841
842 void logincallback(int err, wchar_t *reason, void *data)
843 {
844     switch(err)
845     {
846     case DC_LOGIN_ERR_SUCCESS:
847         dc_queuecmd(NULL, NULL, L"notify", L"all", L"on", NULL);
848         dc_getfnlistasync(getfnlistcallback, NULL);
849         dc_gettrlistasync(gettrlistcallback, NULL);
850         updatesbar("Authenticated");
851         break;
852     case DC_LOGIN_ERR_NOLOGIN:
853         msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not negotiate an acceptable authentication mechanism"));
854         dc_disconnect();
855         dcdisconnected();
856         break;
857     case DC_LOGIN_ERR_SERVER:
858         msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("The server has encountered an error"));
859         dc_disconnect();
860         dcdisconnected();
861         break;
862     case DC_LOGIN_ERR_USER:
863         msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Internal client error"));
864         dc_disconnect();
865         dcdisconnected();
866         break;
867     case DC_LOGIN_ERR_CONV:
868         dc_disconnect();
869         dcdisconnected();
870         break;
871     case DC_LOGIN_ERR_AUTHFAIL:
872         msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Login attempt failed!"));
873         dc_disconnect();
874         dcdisconnected();
875         break;
876     }
877     updatewrite();
878 }
879
880 GtkTreeIter *ref2iter(GtkTreeRowReference *ref)
881 {
882     static GtkTreeIter iter;
883     GtkTreePath *path;
884     
885     assert((path = gtk_tree_row_reference_get_path(ref)) != NULL);
886     assert(gtk_tree_model_get_iter(GTK_TREE_MODEL(srchmodel), &iter, path));
887     gtk_tree_path_free(path);
888     return(&iter);
889 }
890
891 GtkTreeRowReference *iter2ref(GtkTreeIter *iter)
892 {
893     GtkTreePath *path;
894     GtkTreeRowReference *ref;
895     
896     assert((path = gtk_tree_model_get_path(GTK_TREE_MODEL(srchmodel), iter)) != NULL);
897     assert((ref = gtk_tree_row_reference_new(GTK_TREE_MODEL(srchmodel), path)) != NULL);
898     gtk_tree_path_free(path);
899     return(ref);
900 }
901
902 struct srchsize *finddiscsize(void)
903 {
904     int i;
905     GtkTreeIter iter;
906     
907     for(i = 0; i < numsizes; i++)
908     {
909         if(srchsizes[i].size == -1)
910             return(&srchsizes[i]);
911     }
912     srchsizes = srealloc(srchsizes, sizeof(*srchsizes) * ++numsizes);
913     srchsizes[i].size = -1;
914     srchsizes[i].num = 1;
915     srchsizes[i].slots = 0;
916     srchsizes[i].resptime = 0.0;
917     gtk_tree_store_append(srchmodel, &iter, NULL);
918     gtk_tree_store_set(srchmodel, &iter, 3, _("Discrete sizes"), 7, 1, -1);
919     srchsizes[i].ref = iter2ref(&iter);
920     return(&srchsizes[i]);
921 }
922
923 struct knownspeed *findksentbyname(char *userid)
924 {
925     int i;
926     
927     for(i = 0; i < numspeeds; i++)
928     {
929         if(!strcmp(knownspeeds[i].userid, userid))
930             return(&knownspeeds[i]);
931     }
932     return(NULL);
933 }
934
935 struct knownspeed *findksentbyseq(int seq)
936 {
937     int i;
938     
939     for(i = 0; i < numspeeds; i++)
940     {
941         if(knownspeeds[i].seq == seq)
942             return(&knownspeeds[i]);
943     }
944     return(NULL);
945 }
946
947 gboolean ksupdaterow(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
948 {
949     struct knownspeed *ks;
950     char *userid;
951     
952     gtk_tree_model_get(GTK_TREE_MODEL(model), iter, 1, &userid, -1);
953     if(userid == NULL)
954         return(FALSE);
955     ks = findksentbyname(userid);
956     if(ks == NULL)
957     {
958         knownspeeds = srealloc(knownspeeds, (numspeeds + 1) * sizeof(*knownspeeds));
959         ks = &knownspeeds[numspeeds];
960         numspeeds++;
961         ks->userid = sstrdup(userid);
962         ks->speed = -1;
963         ks->seq = -2;
964         ksqueryseq = -2;
965     }
966     g_free(userid);
967     if(ks->speed != -1)
968         gtk_tree_store_set(GTK_TREE_STORE(model), iter, 8, ks->speed, -1);
969     return(FALSE);
970 }
971
972 gint ksupdatecb(gpointer data)
973 {
974     int i, oldnum;
975     time_t now;
976     wchar_t **users, *buf;
977     size_t userssize, usersdata;
978     
979     if(ksquerytag != -1)
980         return(TRUE);
981     now = time(NULL);
982     oldnum = numspeeds;
983     for(i = 0; i < numspeeds;)
984     {
985         if(now - knownspeeds[i].fetched > 60)
986         {
987             free(knownspeeds[i].userid);
988             memmove(&knownspeeds[i], &knownspeeds[i + 1], (--numspeeds - i) * sizeof(*knownspeeds));
989         } else {
990             i++;
991         }
992     }
993     if(oldnum != numspeeds)
994     {
995         if(numspeeds == 0)
996         {
997             free(knownspeeds);
998             knownspeeds = NULL;
999         } else {
1000             knownspeeds = srealloc(knownspeeds, numspeeds * sizeof(*knownspeeds));
1001         }
1002     }
1003     gtk_tree_model_foreach(GTK_TREE_MODEL(srchmodel), ksupdaterow, NULL);
1004     if(ksqueryseq == -2)
1005     {
1006         users = NULL;
1007         userssize = usersdata = 0;
1008         ksqueryseq = 0;
1009         for(i = 0; i < numspeeds; i++)
1010         {
1011             if(knownspeeds[i].seq == -2)
1012             {
1013                 assert((buf = icmbstowcs(knownspeeds[i].userid, "UTF-8")) != NULL);
1014                 knownspeeds[i].seq = ksqueryseq++;
1015                 addtobuf(users, buf);
1016             }
1017         }
1018         addtobuf(users, NULL);
1019         ksquerytag = dc_queuecmd(NULL, NULL, L"filtercmd", L"userspeeda", L"%%a", users, NULL);
1020         dc_freewcsarr(users);
1021     }
1022     return(TRUE);
1023 }
1024
1025 void handleresps(void)
1026 {
1027     int i;
1028     struct dc_response *resp;
1029     struct dc_intresp *ires;
1030     struct dc_fnetnode *fn;
1031     struct fndata *fndata;
1032     GtkTextIter iter;
1033     GtkTreeIter titer, piter;
1034     char *buf, *p;
1035     int tosbuf;
1036     struct srchsize *ss;
1037     struct knownspeed *ks;
1038     
1039     while((resp = dc_getresp()) != NULL)
1040     {
1041         if(!wcscmp(resp->cmdname, L".connect"))
1042         {
1043             if(resp->code == 200)
1044             {
1045                 tosbuf = 0x10; /* Minimum cost */
1046                 setsockopt(dcfd, SOL_IP, IP_TOS, &tosbuf, sizeof(tosbuf));
1047                 updatesbar(_("Connected"));
1048                 dc_loginasync(connectas, 1, loginconv, logincallback, NULL);
1049             } else {
1050                 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("The server refused the connection"));
1051                 dc_disconnect();
1052                 dcdisconnected();
1053             }
1054         } else if(!wcscmp(resp->cmdname, L".notify")) {
1055             dc_uimisc_handlenotify(resp);
1056             switch(resp->code)
1057             {
1058             case 600:
1059                 if((ires = dc_interpret(resp)) != NULL)
1060                 {
1061                     if((fn = dc_findfnetnode(ires->argv[0].val.num)) != NULL)
1062                     {
1063                         fndata = fn->udata;
1064                         gtk_text_buffer_get_end_iter(fndata->textbuf, &iter);
1065                         if((buf = icwcstombs(ires->argv[3].val.str, "UTF-8")) != NULL)
1066                         {
1067                             gtk_text_buffer_insert_with_tags_by_name(fndata->textbuf, &iter, "<", -1, "sender", NULL);
1068                             gtk_text_buffer_insert_with_tags_by_name(fndata->textbuf, &iter, buf, -1, "sender", NULL);
1069                             gtk_text_buffer_insert_with_tags_by_name(fndata->textbuf, &iter, ">", -1, "sender", NULL);
1070                             gtk_text_buffer_insert(fndata->textbuf, &iter, " ", -1);
1071                             free(buf);
1072                         }
1073                         if((buf = icwcstombs(ires->argv[4].val.str, "UTF-8")) != NULL)
1074                         {
1075                             gtk_text_buffer_insert(fndata->textbuf, &iter, buf, -1);
1076                             gtk_text_buffer_insert(fndata->textbuf, &iter, "\n", -1);
1077                             free(buf);
1078                             if(curchat == fn->id)
1079                                 gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(main_chatview), &iter, 0, 0, 0, 0);
1080                         }
1081                     }
1082                     dc_freeires(ires);
1083                 }
1084                 break;
1085             case 601:
1086             case 602:
1087             case 603:
1088             case 604:
1089             case 605:
1090                 updatehublist();
1091                 break;
1092             case 610:
1093             case 611:
1094             case 612:
1095             case 613:
1096             case 614:
1097             case 615:
1098             case 616:
1099             case 617:
1100             case 618:
1101                 updatetransferlists();
1102                 break;
1103             case 620:
1104                 if((ires = dc_interpret(resp)) != NULL)
1105                 {
1106                     if(ires->argv[0].val.num == nextsrch)
1107                         srcheta = time(NULL) + ires->argv[0].val.num;
1108                     dc_freeires(ires);
1109                 }
1110                 break;
1111             case 621:
1112                 if((ires = dc_interpret(resp)) != NULL)
1113                 {
1114                     if(ires->argv[0].val.num == nextsrch)
1115                     {
1116                         if(cursrch != -1)
1117                             dc_queuecmd(NULL, NULL, L"cansrch", L"%%i", cursrch, NULL);
1118                         cursrch = nextsrch;
1119                         nextsrch = -1;
1120                         gtk_widget_set_sensitive(main_realsrch, TRUE);
1121                         gtk_widget_set_sensitive(main_simplesrch, TRUE);
1122                         gtk_widget_set_sensitive(main_srchbtn, TRUE);
1123                         gtk_widget_set_sensitive(main_srchcanbtn, FALSE);
1124                         srchstatupdate();
1125                         gtk_entry_set_text(GTK_ENTRY(main_realsrch), "");
1126                         gtk_entry_set_text(GTK_ENTRY(main_simplesrch), "");
1127                         gtk_tree_store_clear(srchmodel);
1128                         freesrchsizes();
1129                     }
1130                     dc_freeires(ires);
1131                 }
1132                 break;
1133             case 622:
1134                 if((ires = dc_interpret(resp)) != NULL)
1135                 {
1136                     if(ires->argv[0].val.num == cursrch)
1137                     {
1138                         for(i = 0; i < numsizes; i++)
1139                         {
1140                             if(srchsizes[i].size == ires->argv[4].val.num)
1141                                 break;
1142                         }
1143                         if(i == numsizes)
1144                         {
1145                             srchsizes = srealloc(srchsizes, sizeof(*srchsizes) * ++numsizes);
1146                             srchsizes[i].size = ires->argv[4].val.num;
1147                             srchsizes[i].num = 1;
1148                             srchsizes[i].slots = ires->argv[5].val.num;
1149                             srchsizes[i].resptime = ires->argv[7].val.flnum;
1150                             ss = finddiscsize();
1151                             ss->slots += ires->argv[5].val.num;
1152                             if((ss->resptime == 0.0) || (ss->resptime > ires->argv[7].val.flnum))
1153                                 ss->resptime = ires->argv[7].val.flnum;
1154                             piter = *ref2iter(ss->ref);
1155                             gtk_tree_store_set(srchmodel, &piter, 5, ss->slots, 6, ss->resptime, -1);
1156                             gtk_tree_store_append(srchmodel, &titer, &piter);
1157                             srchsizes[i].ref = iter2ref(&titer);
1158                         } else if(srchsizes[i].num == 1) {
1159                             char *filename, *peername, *fnetname, *hash;
1160                             int slots, speed;
1161                             double resptime;
1162                             
1163                             gtk_tree_model_get(GTK_TREE_MODEL(srchmodel), ref2iter(srchsizes[i].ref), 0, &fnetname, 1, &peername, 3, &filename, 5, &slots, 6, &resptime, 8, &speed, 9, &hash, -1);
1164                             gtk_tree_store_remove(srchmodel, ref2iter(srchsizes[i].ref));
1165                             gtk_tree_row_reference_free(srchsizes[i].ref);
1166                             ss = finddiscsize();
1167                             ss->slots -= slots;
1168                             gtk_tree_store_set(srchmodel, ref2iter(ss->ref), 5, ss->slots, -1);
1169                             gtk_tree_store_append(srchmodel, &piter, NULL);
1170                             srchsizes[i].slots = ires->argv[5].val.num + slots;
1171                             srchsizes[i].resptime = (ires->argv[7].val.flnum < resptime)?ires->argv[7].val.flnum:resptime;
1172                             srchsizes[i].num = 2;
1173                             srchsizes[i].ref = iter2ref(&piter);
1174                             gtk_tree_store_set(srchmodel, &piter, 4, srchsizes[i].size, 5, srchsizes[i].slots, 6, srchsizes[i].resptime, 7, 2, -1);
1175                             if((buf = icwcstombs(ires->argv[1].val.str, "UTF-8")) != NULL)
1176                             {
1177                                 p = buf;
1178                                 /* XXX: Too NMDC-specific! */
1179                                 if(strrchr(p, '\\') != NULL)
1180                                     p = strrchr(p, '\\') + 1;
1181                                 gtk_tree_store_set(srchmodel, &piter, 3, p, -1);
1182                                 free(buf);
1183                             }
1184                             gtk_tree_store_append(srchmodel, &titer, &piter);
1185                             gtk_tree_store_set(srchmodel, &titer, 0, fnetname, 1, peername, 2, peername, 3, filename, 4, srchsizes[i].size, 5, slots, 6, resptime, 8, speed, 9, hash, -1);
1186                             g_free(filename); g_free(peername); g_free(fnetname); g_free(hash);
1187                             gtk_tree_store_append(srchmodel, &titer, &piter);
1188                         } else {
1189                             srchsizes[i].num++;
1190                             srchsizes[i].slots += ires->argv[5].val.num;
1191                             if(ires->argv[7].val.flnum < srchsizes[i].resptime)
1192                                 srchsizes[i].resptime = ires->argv[7].val.flnum;
1193                             piter = *ref2iter(srchsizes[i].ref);
1194                             gtk_tree_store_set(srchmodel, &piter, 5, srchsizes[i].slots, 6, srchsizes[i].resptime, 7, srchsizes[i].num, -1);
1195                             gtk_tree_store_append(srchmodel, &titer, &piter);
1196                         }
1197                         if((buf = icwcstombs(ires->argv[1].val.str, "UTF-8")) != NULL)
1198                         {
1199                             gtk_tree_store_set(srchmodel, &titer, 3, buf, -1);
1200                             free(buf);
1201                         }
1202                         if((buf = icwcstombs(ires->argv[2].val.str, "UTF-8")) != NULL)
1203                         {
1204                             gtk_tree_store_set(srchmodel, &titer, 0, buf, -1);
1205                             free(buf);
1206                         }
1207                         if((buf = icwcstombs(ires->argv[3].val.str, "UTF-8")) != NULL)
1208                         {
1209                             gtk_tree_store_set(srchmodel, &titer, 1, buf, -1);
1210                             gtk_tree_store_set(srchmodel, &titer, 2, buf, -1);
1211                             free(buf);
1212                         }
1213                         if((buf = icwcstombs(ires->argv[8].val.str, "UTF-8")) != NULL)
1214                         {
1215                             gtk_tree_store_set(srchmodel, &titer, 9, buf, -1);
1216                             free(buf);
1217                         }
1218                         gtk_tree_store_set(srchmodel, &titer, 4, ires->argv[4].val.num, 5, ires->argv[5].val.num, 6, ires->argv[7].val.flnum, 8, -1, -1);
1219                     }
1220                     dc_freeires(ires);
1221                 }
1222                 break;
1223             default:
1224                 break;
1225             }
1226         } else if(!wcscmp(resp->cmdname, L"filtercmd")) {
1227             if((ksquerytag >= 0) && (ksquerytag == resp->tag))
1228             {
1229                 for(i = 0; i < resp->numlines; i++)
1230                 {
1231                     assert((ks = findksentbyseq(i)) != NULL);
1232                     ks->speed = wcstol(resp->rlines[i].argv[1], NULL, 10);
1233                     ks->seq = -1;
1234                     ks->fetched = time(NULL);
1235                 }
1236                 ksquerytag = -1;
1237                 ksupdatecb(NULL);
1238             } else if((lsrestag >= 0) && (lsrestag == resp->tag)) {
1239                 for(i = 0; i < resp->numlines; i++)
1240                 {
1241                     if(!wcsncmp(resp->rlines[i].argv[1], L"id:", 3))
1242                     {
1243                         gtk_list_store_append(reslist, &titer);
1244                         gtk_list_store_set(reslist, &titer, 0, icswcstombs(resp->rlines[i].argv[1] + 3, "UTF-8", NULL), -1);
1245                     } else if(!wcsncmp(resp->rlines[i].argv[1], L"size:", 5)) {
1246                         gtk_list_store_set(reslist, &titer, 1, wcstol(resp->rlines[i].argv[1] + 5, NULL, 10), -1);
1247                     } else if(!wcsncmp(resp->rlines[i].argv[1], L"prog:", 5)) {
1248                         gtk_list_store_set(reslist, &titer, 2, wcstol(resp->rlines[i].argv[1] + 5, NULL, 10), -1);
1249                     } else if(!wcsncmp(resp->rlines[i].argv[1], L"name:", 5)) {
1250                         gtk_list_store_set(reslist, &titer, 3, icswcstombs(resp->rlines[i].argv[1] + 5, "UTF-8", NULL), -1);
1251                     } else if(!wcsncmp(resp->rlines[i].argv[1], L"lock:", 5)) {
1252                         if(!wcscmp(resp->rlines[i].argv[1] + 5, L"yes"))
1253                             gtk_list_store_set(reslist, &titer, 4, TRUE, -1);
1254                         else
1255                             gtk_list_store_set(reslist, &titer, 4, FALSE, -1);
1256                     } else if(!wcsncmp(resp->rlines[i].argv[1], L"hash:", 5)) {
1257                         gtk_list_store_set(reslist, &titer, 5, icswcstombs(resp->rlines[i].argv[1] + 5, "UTF-8", NULL), -1);
1258                     }
1259                 }
1260                 lsrestag = -1;
1261                 gtk_widget_set_sensitive(reslist_reload, TRUE);
1262             }
1263         }
1264         dc_freeresp(resp);
1265     }
1266     updatewrite();
1267 }
1268
1269 void dcfdcallback(gpointer data, gint source, GdkInputCondition condition)
1270 {
1271     int errnobak;
1272     
1273     if(((condition & GDK_INPUT_READ) && dc_handleread()) || ((condition & GDK_INPUT_WRITE) && dc_handlewrite()))
1274     {
1275         errnobak = errno;
1276         dcdisconnected();
1277         if(errnobak == 0)
1278         {
1279             msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("The server has closed the connection"));
1280         } else {
1281             msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("The connection to the server failed:\n\n%s"), strerror(errnobak));
1282         }
1283         return;
1284     }
1285     handleresps();
1286 }
1287
1288 void cb_main_dconnmenu_activate(GtkWidget *widget, gpointer data)
1289 {
1290     if(dcfd < 0)
1291         return;
1292     dc_disconnect();
1293     dcdisconnected();
1294 }
1295
1296 void cb_main_prefmenu_activate(GtkWidget *widget, gpointer data)
1297 {
1298     GtkWidget *dialog, *swnd;
1299     int resp;
1300     
1301     dialog = gtk_dialog_new_with_buttons(_("Preferences"), GTK_WINDOW(main_wnd), GTK_DIALOG_MODAL, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL);
1302     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), swnd = create_pref_wnd(), TRUE, TRUE, 0);
1303     gtk_entry_set_text(GTK_ENTRY(pref_pubhuburl), pubhubaddr);
1304     gtk_entry_set_text(GTK_ENTRY(pref_connectas), connectas);
1305     gtk_entry_set_text(GTK_ENTRY(pref_dcserver), dcserver);
1306     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(pref_autoconn), autoconn);
1307     gtk_widget_show(swnd);
1308     resp = gtk_dialog_run(GTK_DIALOG(dialog));
1309     if(resp == GTK_RESPONSE_ACCEPT)
1310     {
1311         free(pubhubaddr);
1312         pubhubaddr = sstrdup(gtk_entry_get_text(GTK_ENTRY(pref_pubhuburl)));
1313         free(connectas);
1314         connectas = sstrdup(gtk_entry_get_text(GTK_ENTRY(pref_connectas)));
1315         free(dcserver);
1316         dcserver = sstrdup(gtk_entry_get_text(GTK_ENTRY(pref_dcserver)));
1317         autoconn = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(pref_autoconn));
1318     }
1319     gtk_widget_destroy(dialog);
1320 }
1321
1322 void cb_main_lsres_activate(GtkWidget *widget, gpointer data)
1323 {
1324     gtk_list_store_clear(reslist);
1325     gtk_widget_set_sensitive(reslist_delete, FALSE);
1326     gtk_widget_set_sensitive(reslist_search, FALSE);
1327     gtk_widget_show(reslist_wnd);
1328     if(lsrestag == -1)
1329     {
1330         lsrestag = dc_queuecmd(NULL, NULL, L"filtercmd", L"lsres", NULL);
1331         gtk_widget_set_sensitive(reslist_reload, FALSE);
1332     }
1333 }
1334
1335 void dcconnect(char *host)
1336 {
1337     dcfd = dc_connect(host, -1);
1338     if(dcfd < 0)
1339     {
1340         msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect:\n\n%s"), strerror(errno));
1341         return;
1342     }
1343     gdkread = gdk_input_add(dcfd, GDK_INPUT_READ, dcfdcallback, NULL);
1344     updatewrite();
1345     gtk_widget_set_sensitive(main_connmenu, FALSE);
1346     gtk_widget_set_sensitive(main_dconnmenu, TRUE);
1347     updatesbar(_("Connecting..."));
1348 }
1349
1350 void cb_main_connmenu_activate(GtkWidget *widget, gpointer data)
1351 {
1352     char *buf;
1353     
1354     if(dcfd >= 0)
1355         return;
1356     if((buf = inputbox(_("Connect"), _("Server address:"), dcserver, 1)) == NULL)
1357         return;
1358     dcconnect(buf);
1359     free(buf);
1360 }
1361
1362 void cb_main_sdmenu_activate(GtkWidget *widget, gpointer data)
1363 {
1364     int tag;
1365     struct dc_response *resp;
1366
1367     if(dcfd < 0)
1368     {
1369         msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1370         return;
1371     }
1372     tag = dc_queuecmd(NULL, NULL, L"shutdown", NULL);
1373     if((resp = dc_gettaggedrespsync(tag)) != NULL)
1374     {
1375         if(resp->code == 502)
1376             msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1377         dc_freeresp(resp);
1378     }
1379     handleresps();
1380 }
1381
1382 void cb_main_fnaddr_activate(GtkWidget *widget, gpointer data)
1383 {
1384     int tag;
1385     struct dc_response *resp;
1386     wchar_t **toks;
1387     
1388     if(dcfd < 0)
1389     {
1390         msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1391         return;
1392     }
1393     toks = dc_lexsexpr(icsmbstowcs((char *)gtk_entry_get_text(GTK_ENTRY(main_fnaddr)), "UTF-8", NULL));
1394     if(*toks == NULL)
1395     {
1396         msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Illegal address entered"));
1397         return;
1398     }
1399     if(wcschr(toks[0], L':') == NULL)
1400     {
1401         toks[0] = srealloc(toks[0], (wcslen(toks[0]) + 5) * sizeof(wchar_t));
1402         wcscat(toks[0], L":411");
1403     }
1404     tag = dc_queuecmd(NULL, NULL, L"cnct", L"dc", L"%%a", toks, NULL);
1405     dc_freewcsarr(toks);
1406     if((resp = dc_gettaggedrespsync(tag)) != NULL)
1407     {
1408         if(resp->code == 502)
1409             msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1410         if(resp->code == 509)
1411             msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("The server could not parse that address"));
1412         if(resp->code == 515)
1413             msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("There are too many hubs connected"));
1414         dc_freeresp(resp);
1415     }
1416     gtk_entry_set_text(GTK_ENTRY(main_fnaddr), "");
1417     handleresps();
1418 }
1419
1420 void pubhubfdcallback(gpointer data, gint source, GdkInputCondition condition)
1421 {
1422     static char buf[65536];
1423     static int bufpos = 0;
1424     int ret, i;
1425     char *p, *p2;
1426     char *fields[4];
1427     wchar_t *wbuf;
1428     GtkTreeIter iter;
1429     int sorted, sortcol;
1430     GtkSortType sortorder;
1431     GtkTreeModel *sortmodel;
1432     
1433     if(!(condition & GDK_INPUT_READ))
1434         return;
1435     if(bufpos == 1024)
1436         bufpos = 0;
1437     ret = read(pubhubfd, buf + bufpos, sizeof(buf) - bufpos);
1438     if(ret <= 0)
1439     {
1440         if(ret < 0)
1441             msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not read from public hub listing process: %s"), strerror(errno));
1442         close(pubhubfd);
1443         gdk_input_remove(pubhubtag);
1444         kill(pubhubproc, SIGINT);
1445         pubhubfd = pubhubtag = -1;
1446         pubhubproc = 0;
1447         bufpos = 0;
1448         if(filterpubhub)
1449         {
1450             regfree(&pubhubfilter);
1451             filterpubhub = 0;
1452         }
1453         return;
1454     }
1455     bufpos += ret;
1456     sortmodel = gtk_tree_view_get_model(GTK_TREE_VIEW(main_phublist));
1457     sorted = gtk_tree_sortable_get_sort_column_id(GTK_TREE_SORTABLE(sortmodel), &sortcol, &sortorder);
1458     gtk_tree_view_set_model(GTK_TREE_VIEW(main_phublist), NULL);
1459     while((p = memchr(buf, '\n', bufpos)) != NULL)
1460     {
1461         *(p++) = 0;
1462         if(!filterpubhub || !regexec(&pubhubfilter, buf, 0, NULL, 0))
1463         {
1464             p2 = buf;
1465             for(i = 0; i < 4; i++)
1466             {
1467                 fields[i] = p2;
1468                 if((p2 = strchr(p2, '|')) == NULL)
1469                     break;
1470                 *(p2++) = 0;
1471             }
1472             if(i == 4)
1473             {
1474                 for(i = 0; i < 4; i++)
1475                 {
1476                     if((wbuf = icsmbstowcs(fields[i], DCCHARSET, NULL)) == NULL)
1477                     {
1478                         /*
1479                         msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not decode hublist - aborting at this point: %s"), strerror(errno));
1480                         kill(pubhubproc, SIGINT);
1481                         break;
1482                         */
1483                         fields[i] = sstrdup(_("(Invalid character)"));
1484                     } else {
1485                         if((fields[i] = icwcstombs(wbuf, "UTF-8")) == NULL)
1486                             break;
1487                     }
1488                 }
1489                 if(i == 4)
1490                 {
1491                     gtk_list_store_append(pubhubmodel, &iter);
1492                     gtk_list_store_set(pubhubmodel, &iter, 0, fields[0], 1, fields[1], 2, fields[2], 3, atoi(fields[3]), -1);
1493                 }
1494                 for(i--; i >= 0; i--)
1495                     free(fields[i]);
1496             }
1497         }
1498         memmove(buf, p, bufpos -= p - buf);
1499     }
1500     sortmodel = gtk_tree_model_sort_new_with_model(GTK_TREE_MODEL(pubhubmodel));
1501     if(sorted)
1502         gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(sortmodel), sortcol, sortorder);
1503     gtk_tree_view_set_model(GTK_TREE_VIEW(main_phublist), sortmodel);
1504 }
1505
1506 void cb_main_pubhubfilter_activate(GtkWidget *widget, gpointer data)
1507 {
1508     int pipe1[2], pipe2[2];
1509     int len, err;
1510     const char *buf;
1511     char errbuf[1024];
1512     
1513     if(pubhubtag >= 0)
1514         gdk_input_remove(pubhubtag);
1515     if(pubhubfd >= 0)
1516         close(pubhubfd);
1517     if(pubhubproc > 0)
1518         kill(pubhubproc, SIGINT);
1519     if(filterpubhub)
1520     {
1521         regfree(&pubhubfilter);
1522         filterpubhub = 0;
1523     }
1524     buf = gtk_entry_get_text(GTK_ENTRY(main_pubhubfilter));
1525     if(*buf)
1526     {
1527         if((err = regcomp(&pubhubfilter, buf, REG_EXTENDED | REG_ICASE | REG_NOSUB)) != 0)
1528         {
1529             regerror(err, &pubhubfilter, errbuf, sizeof(errbuf));
1530             msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "Could not compile regex: %s", errbuf);
1531             regfree(&pubhubfilter);
1532             filterpubhub = 0;
1533             return;
1534         }
1535         filterpubhub = 1;
1536     }
1537     gtk_list_store_clear(pubhubmodel);
1538     pipe(pipe1);
1539     if((pubhubproc = fork()) == 0)
1540     {
1541         dup2(pipe1[1], 1);
1542         close(pipe1[0]);
1543         close(pipe1[1]);
1544         execlp("wget", "wget", "-qO", "-", pubhubaddr, NULL);
1545         perror("wget");
1546         exit(127);
1547     }
1548     close(pipe1[1]);
1549     pubhubfd = pipe1[0];
1550     len = strlen(pubhubaddr);
1551     if((len > 4) && !strcmp(pubhubaddr + len - 4, ".bz2"))
1552     {
1553         pipe(pipe2);
1554         if(fork() == 0)
1555         {
1556             dup2(pipe1[0], 0);
1557             dup2(pipe2[1], 1);
1558             close(pipe1[0]);
1559             close(pipe2[0]);
1560             close(pipe2[1]);
1561             execlp("bzcat", "bzcat", NULL);
1562             perror("bzcat");
1563             exit(127);
1564         }
1565         close(pipe1[0]);
1566         close(pipe2[1]);
1567         pubhubfd = pipe2[0];
1568     }
1569     pubhubtag = gdk_input_add(pubhubfd, GDK_INPUT_READ, pubhubfdcallback, NULL);
1570 }
1571
1572 void cb_main_dcnctbtn_clicked(GtkWidget *widget, gpointer data)
1573 {
1574     GtkTreeIter iter;
1575     int tag, id;
1576     struct dc_response *resp;
1577     
1578     if(dcfd < 0)
1579     {
1580         msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1581         return;
1582     }
1583     if(!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(main_fnetnodes)), NULL, &iter))
1584     {
1585         msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("No hub selected"));
1586         return;
1587     }
1588     gtk_tree_model_get(GTK_TREE_MODEL(fnmodel), &iter, 0, &id, -1);
1589     tag = dc_queuecmd(NULL, NULL, L"dcnct", L"%%i", id, NULL);
1590     if((resp = dc_gettaggedrespsync(tag)) != NULL)
1591     {
1592         if(resp->code == 502)
1593             msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1594         dc_freeresp(resp);
1595     }
1596     handleresps();
1597 }
1598
1599 void cb_main_phublist_cchange(GtkWidget *widget, gpointer data)
1600 {
1601     GtkTreeIter iter;
1602     GtkTreeModel *model;
1603     char *addr;
1604     
1605     if(!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(main_phublist)), &model, &iter))
1606         return;
1607     gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, 1, &addr, -1);
1608     gtk_entry_set_text(GTK_ENTRY(main_fnaddr), addr);
1609     g_free(addr);
1610 }
1611
1612 void cb_main_phublist_activate(GtkWidget *widget, GtkTreePath *path, GtkTreeViewColumn *col, gpointer data)
1613 {
1614     int tag;
1615     struct dc_response *resp;
1616     GtkTreeIter iter;
1617     GtkTreeModel *model;
1618     char *buf;
1619     
1620     if(dcfd < 0)
1621     {
1622         msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1623         return;
1624     }
1625     model = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
1626     if(!gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter, path))
1627         return;
1628     gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, 1, &buf, -1);
1629     if(strchr(buf, ':') == NULL)
1630     {
1631         buf = g_realloc(buf, strlen(buf) + 5);
1632         strcat(buf, ":411");
1633     }
1634     tag = dc_queuecmd(NULL, NULL, L"cnct", L"dc", L"%%s", buf, NULL);
1635     g_free(buf);
1636     gtk_entry_set_text(GTK_ENTRY(main_fnaddr), "");
1637     if((resp = dc_gettaggedrespsync(tag)) != NULL)
1638     {
1639         if(resp->code == 502)
1640             msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1641         if(resp->code == 509)
1642             msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("The server could not parse that address"));
1643         if(resp->code == 515)
1644             msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("There are too many hubs connected"));
1645         dc_freeresp(resp);
1646     }
1647     handleresps();
1648 }
1649
1650 void cb_main_chatnodes_activate(GtkWidget *widget, GtkTreePath *path, GtkTreeViewColumn *col, gpointer uudata)
1651 {
1652     GtkTreeIter iter;
1653     int id;
1654     struct dc_fnetnode *fn;
1655     struct fndata *data;
1656     
1657     if(!gtk_tree_model_get_iter(GTK_TREE_MODEL(fnmodel), &iter, path))
1658         return;
1659     gtk_tree_model_get(GTK_TREE_MODEL(fnmodel), &iter, 0, &id, -1);
1660     if((fn = dc_findfnetnode(id)) == NULL)
1661         return;
1662     data = fn->udata;
1663     curchat = id;
1664     if(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(fnmodel), &iter))
1665     {
1666         do
1667         {
1668             gtk_tree_model_get(GTK_TREE_MODEL(fnmodel), &iter, 0, &id, -1);
1669             if(id == curchat)
1670                 gtk_list_store_set(fnmodel, &iter, 5, "gtk-apply", -1);
1671             else
1672                 gtk_list_store_set(fnmodel, &iter, 5, NULL, -1);
1673         } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(fnmodel), &iter));
1674     }
1675     gtk_text_view_set_buffer(GTK_TEXT_VIEW(main_chatview), GTK_TEXT_BUFFER(data->textbuf));
1676 }
1677
1678 void cb_main_chatstr_activate(GtkWidget *widget, gpointer data)
1679 {
1680     int tag;
1681     const char *buf;
1682     struct dc_response *resp;
1683     
1684     if(dcfd < 0)
1685     {
1686         msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1687         return;
1688     }
1689     if(curchat < 0)
1690     {
1691         msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("No hub selected"));
1692         return;
1693     }
1694     buf = gtk_entry_get_text(GTK_ENTRY(main_chatstr));
1695     tag = dc_queuecmd(NULL, NULL, L"sendchat", L"%%i", curchat, L"1", L"", L"%%s", buf, NULL);
1696     if((resp = dc_gettaggedrespsync(tag)) != NULL)
1697     {
1698         if(resp->code == 502)
1699             msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1700         else if(resp->code == 504)
1701             msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("This hub could not support all the types of characters in your chat message"));
1702         else if(resp->code == 513)
1703             msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("This hub does not support chatting"));
1704         else if(resp->code != 200)
1705             msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("An error occurred while trying to chat (%i)"), resp->code);
1706         dc_freeresp(resp);
1707     }
1708     gtk_entry_set_text(GTK_ENTRY(main_chatstr), "");
1709     handleresps();
1710 }
1711
1712 void updatesrchfld(const char *simple)
1713 {
1714     char *buf, *s;
1715     char *p, *p2;
1716     size_t bufsize, bufdata;
1717     
1718     s = sstrdup(simple);
1719     buf = NULL;
1720     bufsize = bufdata = 0;
1721     p = s;
1722     do
1723     {
1724         p2 = strchr(p, ' ');
1725         if(p2 != NULL)
1726             *(p2++) = 0;
1727         if(*p)
1728         {
1729             if(bufdata > 0)
1730                 bufcat(buf, " & ", 3);
1731             bufcat(buf, "N~", 2);
1732             for(; *p; p++)
1733             {
1734                 if(strchr("[]()$^.*?+\\|\"!", *p) != NULL)
1735                     addtobuf(buf, '\\');
1736                 addtobuf(buf, *p);
1737             }
1738         }
1739         p = p2;
1740     } while(p2 != NULL);
1741     addtobuf(buf, 0);
1742     gtk_entry_set_text(GTK_ENTRY(main_realsrch), buf);
1743     free(buf);
1744     free(s);
1745 }
1746
1747 void cb_main_simplesrch_changed(GtkWidget *widget, gpointer data)
1748 {
1749     if(srchautoupdate)
1750         return;
1751     srchautoupdate = 1;
1752     updatesrchfld(gtk_entry_get_text(GTK_ENTRY(main_simplesrch)));
1753     srchautoupdate = 0;
1754 }
1755
1756 void cb_main_realsrch_changed(GtkWidget *widget, gpointer data)
1757 {
1758     if(srchautoupdate)
1759         return;
1760     srchautoupdate = 1;
1761     gtk_entry_set_text(GTK_ENTRY(main_simplesrch), "");
1762     srchautoupdate = 0;
1763 }
1764
1765 void cb_main_srchbtn_clicked(GtkWidget *widget, gpointer data)
1766 {
1767     wchar_t **toks;
1768     int tag;
1769     struct dc_response *resp;
1770     struct dc_intresp *ires;
1771     
1772     if(dcfd < 0)
1773     {
1774         msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1775         return;
1776     }
1777     if(nextsrch != -1) /* Impossible case, but oh well... */
1778         return;
1779     toks = dc_lexsexpr(icsmbstowcs((char *)gtk_entry_get_text(GTK_ENTRY(main_realsrch)), "UTF-8", NULL));
1780     if(*toks == NULL)
1781     {
1782         msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Please enter a search expression before searching"));
1783         return;
1784     }
1785     tag = dc_queuecmd(NULL, NULL, L"search", L"all", L"%%a", toks, NULL);
1786     dc_freewcsarr(toks);
1787     if((resp = dc_gettaggedrespsync(tag)) != NULL)
1788     {
1789         if(resp->code == 501)
1790             msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("Could not find any hubs to search on"));
1791         else if(resp->code == 502)
1792             msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1793         else if(resp->code == 509)
1794             msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("The server could not parse your search expression"));
1795         else if(resp->code != 200)
1796             msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("An error occurred while trying to search (%i)"), resp->code);
1797         if(resp->code == 200)
1798         {
1799             if((ires = dc_interpret(resp)) != NULL)
1800             {
1801                 nextsrch = ires->argv[0].val.num;
1802                 srcheta = time(NULL) + ires->argv[1].val.num;
1803                 dc_freeires(ires);
1804             }
1805             gtk_widget_set_sensitive(main_realsrch, FALSE);
1806             gtk_widget_set_sensitive(main_simplesrch, FALSE);
1807             gtk_widget_set_sensitive(main_srchbtn, FALSE);
1808             gtk_widget_set_sensitive(main_srchcanbtn, TRUE);
1809             srchstatupdate();
1810         }
1811         dc_freeresp(resp);
1812     }
1813     handleresps();
1814 }
1815
1816 void cb_main_srchcanbtn_clicked(GtkWidget *widget, gpointer data)
1817 {
1818     if(nextsrch == -1)
1819         return;
1820     dc_queuecmd(NULL, NULL, L"cansrch", L"%%i", nextsrch, NULL);
1821     nextsrch = -1;
1822     gtk_widget_set_sensitive(main_realsrch, TRUE);
1823     gtk_widget_set_sensitive(main_simplesrch, TRUE);
1824     gtk_widget_set_sensitive(main_srchbtn, TRUE);
1825     gtk_widget_set_sensitive(main_srchcanbtn, FALSE);
1826     srchstatupdate();
1827 }
1828
1829 gboolean cb_main_trlist_keypress(GtkWidget *widget, GdkEventKey *event, gpointer data)
1830 {
1831     int id, tag;
1832     GtkTreeSelection *sel;
1833     GtkTreeModel *model;
1834     GtkTreeIter iter;
1835     struct dc_response *resp;
1836     
1837     if((event->type == GDK_KEY_PRESS) && (event->keyval == GDK_Delete))
1838     {
1839         sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
1840         if(gtk_tree_selection_get_selected(sel, &model, &iter))
1841         {
1842             gtk_tree_model_get(model, &iter, 0, &id, -1);
1843             tag = dc_queuecmd(NULL, NULL, L"cancel", L"%%i", id, NULL);
1844             if((resp = dc_gettaggedrespsync(tag)) != NULL)
1845             {
1846                 if(resp->code == 502)
1847                     msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1848                 else if(resp->code != 200)
1849                     msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("An error occurred while trying to cancel (%i)"), resp->code);
1850                 dc_freeresp(resp);
1851             }
1852             handleresps();
1853         }
1854         return(TRUE);
1855     }
1856     return(FALSE);
1857 }
1858
1859 void cb_main_srchres_activate(GtkWidget *widget, GtkTreePath *path, GtkTreeViewColumn *col, gpointer data)
1860 {
1861     int tag;
1862     struct dc_response *resp;
1863     GtkTreeIter iter;
1864     GtkTreeModel *model;
1865     int size, num;
1866     char *tfnet, *tpeerid, *tfilename, *thash, *arg;
1867     wchar_t *fnet, *peerid, *filename, *hash;
1868     
1869     if(dcfd < 0)
1870     {
1871         msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1872         return;
1873     }
1874     model = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
1875     if(!gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter, path))
1876         return;
1877     gtk_tree_model_get(model, &iter, 7, &num, -1);
1878     if(num > 0)
1879         return;
1880     gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, 0, &tfnet, 1, &tpeerid, 3, &tfilename, 4, &size, 9, &thash, -1);
1881     fnet = icmbstowcs(tfnet, "UTF-8");
1882     peerid = icmbstowcs(tpeerid, "UTF-8");
1883     filename = icmbstowcs(tfilename, "UTF-8");
1884     hash = (thash == NULL)?NULL:icmbstowcs(thash, "UTF-8");
1885     if((fnet == NULL) || (peerid == NULL) || (filename == NULL))
1886     {
1887         if(fnet != NULL)
1888             free(fnet);
1889         if(peerid != NULL)
1890             free(peerid);
1891         if(filename != NULL)
1892             free(filename);
1893         if(hash != NULL)
1894             free(hash);
1895         g_free(tfnet);
1896         g_free(tpeerid);
1897         g_free(tfilename);
1898         if(thash != NULL)
1899             g_free(thash);
1900         return;
1901     }
1902     g_free(tfnet);
1903     g_free(tpeerid);
1904     g_free(tfilename);
1905     arg = (char *)gtk_entry_get_text(GTK_ENTRY(main_dlarg));
1906     if(*arg)
1907         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);
1908     else
1909         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);
1910     free(fnet);
1911     free(peerid);
1912     free(filename);
1913     if(hash != NULL)
1914         free(hash);
1915     if((resp = dc_gettaggedrespsync(tag)) != NULL)
1916     {
1917         if(resp->code == 502)
1918             msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1919         if(resp->code != 200)
1920             msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("An error occurred while trying to queue the download (%i)"), resp->code);
1921         dc_freeresp(resp);
1922     }
1923     handleresps();
1924 }
1925
1926 gboolean srchfilterfunc(GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
1927 {
1928     int slots;
1929     int filteratall;
1930     
1931     filteratall = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(main_filternoslots));
1932     if(!filteratall)
1933         return(TRUE);
1934     gtk_tree_model_get(model, iter, 5, &slots, -1);
1935     if(slots < 1)
1936         return(FALSE);
1937     return(TRUE);
1938 }
1939
1940 void cb_main_filternoslots_toggled(GtkToggleButton *widget, gpointer data)
1941 {
1942     gtk_tree_model_filter_refilter(srchmodelfilter);
1943 }
1944
1945 void cb_main_srhash_activate(GtkWidget *widget, gpointer data)
1946 {
1947     GtkTreeSelection *sel;
1948     GtkTreeModel *model;
1949     GtkTreeIter iter;
1950     char *hash, *buf;
1951     
1952     if(nextsrch != -1)
1953         return;
1954     sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(main_srchres));
1955     if(gtk_tree_selection_get_selected(sel, &model, &iter))
1956     {
1957         gtk_tree_model_get(model, &iter, 9, &hash, -1);
1958         buf = sprintf2("H=%s", hash);
1959         gtk_entry_set_text(GTK_ENTRY(main_realsrch), buf);
1960         g_free(hash);
1961         free(buf);
1962         cb_main_srchbtn_clicked(widget, NULL);
1963     } else {
1964         return;
1965     }
1966 }
1967
1968 void cb_main_srcopy_activate(GtkWidget *widget, gpointer data)
1969 {
1970     GtkClipboard *cb;
1971     GtkTreeSelection *sel;
1972     GtkTreeModel *model;
1973     GtkTreeIter iter;
1974     char *hash;
1975     
1976     if(nextsrch != -1)
1977         return;
1978     sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(main_srchres));
1979     if(!gtk_tree_selection_get_selected(sel, &model, &iter))
1980         return;
1981     gtk_tree_model_get(model, &iter, 9, &hash, -1);
1982     cb = gtk_clipboard_get(gdk_atom_intern("PRIMARY", FALSE));
1983     gtk_clipboard_set_text(cb, hash, -1);
1984     g_free(hash);
1985 }
1986
1987 void cb_main_trhash_activate(GtkWidget *widget, gpointer data)
1988 {
1989     GtkTreeSelection *sel;
1990     GtkTreeModel *model;
1991     GtkTreeIter iter;
1992     char *hash, *buf;
1993     
1994     if(nextsrch != -1)
1995         return;
1996     sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(main_downloads));
1997     if(gtk_tree_selection_get_selected(sel, &model, &iter))
1998     {
1999         gtk_tree_model_get(model, &iter, 12, &hash, -1);
2000         buf = sprintf2("H=%s", hash);
2001         gtk_entry_set_text(GTK_ENTRY(main_realsrch), buf);
2002         g_free(hash);
2003         free(buf);
2004         cb_main_srchbtn_clicked(widget, NULL);
2005     } else {
2006         return;
2007     }
2008 }
2009
2010 void cb_main_trcopy_activate(GtkWidget *widget, gpointer data)
2011 {
2012     GtkClipboard *cb;
2013     GtkTreeSelection *sel;
2014     GtkTreeModel *model;
2015     GtkTreeIter iter;
2016     char *hash;
2017     
2018     sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(main_downloads));
2019     if(!gtk_tree_selection_get_selected(sel, &model, &iter))
2020         return;
2021     gtk_tree_model_get(model, &iter, 12, &hash, -1);
2022     cb = gtk_clipboard_get(gdk_atom_intern("PRIMARY", FALSE));
2023     gtk_clipboard_set_text(cb, hash, -1);
2024     g_free(hash);
2025 }
2026
2027 void cb_main_trcancel_activate(GtkWidget *widget, gpointer data)
2028 {
2029     GtkTreeSelection *sel;
2030     GtkTreeModel *model;
2031     GtkTreeIter iter;
2032     int id, tag;
2033     struct dc_response *resp;
2034     
2035     if(nextsrch != -1)
2036         return;
2037     sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(main_downloads));
2038     if(gtk_tree_selection_get_selected(sel, &model, &iter))
2039     {
2040         gtk_tree_model_get(model, &iter, 0, &id, -1);
2041         tag = dc_queuecmd(NULL, NULL, L"cancel", L"%%i", id, NULL);
2042         if((resp = dc_gettaggedrespsync(tag)) != NULL)
2043         {
2044             if(resp->code == 502)
2045                 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
2046             else if(resp->code != 200)
2047                 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("An error occurred while trying to cancel (%i)"), resp->code);
2048             dc_freeresp(resp);
2049         }
2050         handleresps();
2051     } else {
2052         return;
2053     }
2054 }
2055
2056 /* XXX: This is quite a hack, since the calling convention is
2057  * different for the popup-menu sig and the button-press-event sig. It
2058  * most certainly works, but I don't know how portable it is. */
2059 gboolean cb_main_srpopup(GtkWidget *widget, GdkEventButton *event, gpointer data)
2060 {
2061     GtkTreeSelection *sel;
2062     GtkTreeModel *model;
2063     GtkTreeIter iter;
2064     char *hash;
2065     
2066     if((event != NULL) && (event->button != 3))
2067         return(FALSE);
2068     sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
2069     if(gtk_tree_selection_get_selected(sel, &model, &iter))
2070     {
2071         gtk_tree_model_get(model, &iter, 9, &hash, -1);
2072         if((hash == NULL) || (*hash == 0))
2073         {
2074             gtk_widget_set_sensitive(main_srhash, FALSE);
2075             gtk_widget_set_sensitive(main_srcopy, FALSE);
2076         } else {
2077             if(nextsrch == -1)
2078                 gtk_widget_set_sensitive(main_srhash, TRUE);
2079             else
2080                 gtk_widget_set_sensitive(main_srhash, FALSE);
2081             gtk_widget_set_sensitive(main_srcopy, TRUE);
2082         }
2083         g_free(hash);
2084     } else {
2085         return(FALSE);
2086     }
2087     if(event == NULL)
2088         gtk_menu_popup(GTK_MENU(main_srpopup), NULL, NULL, NULL, NULL, 0, gtk_get_current_event_time());
2089     else
2090         gtk_menu_popup(GTK_MENU(main_srpopup), NULL, NULL, NULL, NULL, event->button, event->time);
2091     return(FALSE);
2092 }
2093
2094 /* The above hack note goes for this one too. */
2095 gboolean cb_main_trpopup(GtkWidget *widget, GdkEventButton *event, gpointer data)
2096 {
2097     GtkTreeSelection *sel;
2098     GtkTreeModel *model;
2099     GtkTreeIter iter;
2100     char *hash;
2101     
2102     if((event != NULL) && (event->button != 3))
2103         return(FALSE);
2104     sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
2105     if(gtk_tree_selection_get_selected(sel, &model, &iter))
2106     {
2107         gtk_tree_model_get(model, &iter, 12, &hash, -1);
2108         if((hash == NULL) || (*hash == 0))
2109         {
2110             gtk_widget_set_sensitive(main_trhash, FALSE);
2111             gtk_widget_set_sensitive(main_trcopy, FALSE);
2112         } else {
2113             if(nextsrch == -1)
2114                 gtk_widget_set_sensitive(main_trhash, TRUE);
2115             else
2116                 gtk_widget_set_sensitive(main_trhash, FALSE);
2117             gtk_widget_set_sensitive(main_trcopy, TRUE);
2118         }
2119         g_free(hash);
2120     } else {
2121         return(FALSE);
2122     }
2123     if(event == NULL)
2124         gtk_menu_popup(GTK_MENU(main_trpopup), NULL, NULL, NULL, NULL, 0, gtk_get_current_event_time());
2125     else
2126         gtk_menu_popup(GTK_MENU(main_trpopup), NULL, NULL, NULL, NULL, event->button, event->time);
2127     return(FALSE);
2128 }
2129
2130 void cb_reslist_reload_clicked(GtkWidget *widget, gpointer data)
2131 {
2132     if(lsrestag != -1)
2133         return;
2134     gtk_widget_set_sensitive(reslist_delete, FALSE);
2135     gtk_widget_set_sensitive(reslist_search, FALSE);
2136     gtk_list_store_clear(reslist);
2137     lsrestag = dc_queuecmd(NULL, NULL, L"filtercmd", L"lsres", NULL);
2138     gtk_widget_set_sensitive(reslist_reload, FALSE);
2139 }
2140
2141 int rmres(char *id)
2142 {
2143     int tag, ret;
2144     struct dc_response *resp;
2145     
2146     ret = -1;
2147     tag = dc_queuecmd(NULL, NULL, L"filtercmd", L"rmres", L"%%s", id, NULL);
2148     if((resp = dc_gettaggedrespsync(tag)) != NULL)
2149     {
2150         if(resp->numlines > 0)
2151         {
2152             if(!wcscmp(resp->rlines[0].argv[1], L"ok"))
2153                 ret = 0;
2154             else if(!wcsncmp(resp->rlines[0].argv[1], L"err:", 4))
2155                 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("An error occurred (%ls)"), resp->rlines[0].argv[1] + 4);
2156         }
2157         dc_freeresp(resp);
2158     }
2159     handleresps();
2160     return(ret);
2161 }
2162
2163 void cb_reslist_delete_clicked(GtkWidget *widget, gpointer data)
2164 {
2165     GtkTreeIter iter;
2166     GtkTreeModel *model;
2167     char *id;
2168     gboolean locked;
2169     
2170     if(nextsrch != -1)
2171         return;
2172     if(!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(reslist_list)), &model, &iter))
2173         return;
2174     gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, 0, &id, 4, &locked, -1);
2175     if(locked)
2176     {
2177         g_free(id);
2178         return;
2179     }
2180     if(!rmres(id))
2181         gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
2182     g_free(id);
2183 }
2184
2185 void cb_reslist_search_clicked(GtkWidget *widget, gpointer data)
2186 {
2187     GtkTreeIter iter;
2188     GtkTreeModel *model;
2189     char *hash, *buf;
2190     
2191     if(nextsrch != -1)
2192         return;
2193     if(!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(reslist_list)), &model, &iter))
2194         return;
2195     gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, 5, &hash, -1);
2196     buf = sprintf2("H=%s", hash);
2197     gtk_entry_set_text(GTK_ENTRY(main_realsrch), buf);
2198     free(buf);
2199     g_free(hash);
2200     cb_main_srchbtn_clicked(widget, NULL);
2201 }
2202
2203 void cb_reslist_list_cchange(GtkWidget *widget, gpointer data)
2204 {
2205     GtkTreeIter iter;
2206     GtkTreeModel *model;
2207     gboolean locked;
2208     char *hash;
2209     
2210     if(!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(reslist_list)), &model, &iter))
2211     {
2212         gtk_widget_set_sensitive(reslist_delete, FALSE);
2213         gtk_widget_set_sensitive(reslist_search, FALSE);
2214         return;
2215     }
2216     gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, 4, &locked, 5, &hash, -1);
2217     gtk_widget_set_sensitive(reslist_delete, !locked);
2218     gtk_widget_set_sensitive(reslist_search, hash && *hash);
2219     g_free(hash);
2220 }
2221
2222 void cb_reslist_list_activate(GtkWidget *widget, GtkTreePath *path, GtkTreeViewColumn *col, gpointer data)
2223 {
2224     GtkTreeIter iter;
2225     GtkTreeModel *model;
2226     char *hash, *buf;
2227
2228     model = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
2229     if(!gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter, path))
2230         return;
2231     if(nextsrch != -1)
2232         return;
2233     gtk_tree_model_get(model, &iter, 5, &hash, -1);
2234     buf = sprintf2("H=%s", hash);
2235     gtk_entry_set_text(GTK_ENTRY(main_realsrch), buf);
2236     free(buf);
2237     g_free(hash);
2238     cb_main_srchbtn_clicked(widget, NULL);
2239 }
2240
2241 gboolean cb_reslist_list_keypress(GtkWidget *widget, GdkEventKey *event, gpointer data)
2242 {
2243     GtkTreeSelection *sel;
2244     GtkTreeModel *model;
2245     GtkTreeIter iter;
2246     char *id;
2247     gboolean locked;
2248     
2249     if((event->type == GDK_KEY_PRESS) && (event->keyval == GDK_Delete))
2250     {
2251         sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
2252         if(gtk_tree_selection_get_selected(sel, &model, &iter))
2253         {
2254             gtk_tree_model_get(model, &iter, 0, &id, 4, &locked, -1);
2255             if(!locked)
2256             {
2257                 if(!rmres(id))
2258                     gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
2259             }
2260             g_free(id);
2261         }
2262         return(TRUE);
2263     }
2264     return(FALSE);
2265 }
2266
2267 void srchstatupdate(void)
2268 {
2269     char buf[1024];
2270     
2271     if(nextsrch == -1)
2272     {
2273         snprintf(buf, 1024, _("Ready to search"));
2274     } else {
2275         snprintf(buf, 1024, _("Search scheduled and will be submitted in %i seconds"), (int)(srcheta - time(NULL)));
2276     }
2277     if(strcmp(gtk_label_get_text(GTK_LABEL(main_srchstatus)), buf))
2278         gtk_label_set_text(GTK_LABEL(main_srchstatus), buf);
2279 }
2280
2281 gint srchstatupdatecb(gpointer data)
2282 {
2283     srchstatupdate();
2284     return(TRUE);
2285 }
2286
2287 void initchattags(void)
2288 {
2289     GtkTextTag *tag;
2290     
2291     chattags = gtk_text_tag_table_new();
2292     tag = gtk_text_tag_new("sender");
2293     g_object_set(tag, "foreground", "blue", NULL);
2294     gtk_text_tag_table_add(chattags, tag);
2295 }
2296
2297 int main(int argc, char **argv)
2298 {
2299     GtkWidget *wnd;
2300     PangoFontDescription *monospacefont;
2301     GtkTreeModel *sortmodel;
2302     struct passwd *pwent;
2303     
2304     setlocale(LC_ALL, "");
2305     bindtextdomain(PACKAGE, LOCALEDIR);
2306     textdomain(PACKAGE);
2307     gtk_init(&argc, &argv);
2308     dc_init();
2309     signal(SIGCHLD, SIG_IGN);
2310     pubhubaddr = sstrdup("http://www.hublist.org/PublicHubList.config.bz2");
2311     dcserver = sstrdup("localhost");
2312     if((pwent = getpwuid(getuid())) == NULL)
2313     {
2314         fprintf(stderr, "could not get your passwd data");
2315         exit(1);
2316     }
2317     connectas = sstrdup(pwent->pw_name);
2318     wnd = create_main_wnd();
2319     create_reslist_wnd();
2320     gtk_window_resize(GTK_WINDOW(reslist_wnd), 600, 400);
2321     initchattags();
2322
2323     fnmodel = gtk_list_store_new(6, G_TYPE_INT, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING, G_TYPE_STRING);
2324     gtk_tree_view_set_model(GTK_TREE_VIEW(main_fnetnodes), GTK_TREE_MODEL(fnmodel));
2325     gtk_tree_view_set_model(GTK_TREE_VIEW(main_chatnodes), GTK_TREE_MODEL(fnmodel));
2326     
2327     reslist = gtk_list_store_new(6, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_STRING);
2328     gtk_tree_view_set_model(GTK_TREE_VIEW(reslist_list), GTK_TREE_MODEL(reslist));
2329     
2330     pubhubmodel = gtk_list_store_new(4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT);
2331     sortmodel = gtk_tree_model_sort_new_with_model(GTK_TREE_MODEL(pubhubmodel));
2332     gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(sortmodel), 3, GTK_SORT_DESCENDING);
2333     gtk_tree_view_set_model(GTK_TREE_VIEW(main_phublist), GTK_TREE_MODEL(sortmodel));
2334     g_object_unref(sortmodel);
2335
2336     dlmodel = gtk_list_store_new(13, G_TYPE_INT, /* id */
2337                                  G_TYPE_INT,     /* dir */
2338                                  G_TYPE_INT,     /* state */
2339                                  G_TYPE_STRING,  /* peerid */
2340                                  G_TYPE_STRING,  /* peernick */
2341                                  G_TYPE_STRING,  /* path */
2342                                  G_TYPE_INT,     /* size */
2343                                  G_TYPE_INT,     /* curpos */
2344                                  G_TYPE_STRING,  /* stock */
2345                                  G_TYPE_FLOAT,   /* percentage */
2346                                  G_TYPE_INT,     /* error */
2347                                  G_TYPE_INT,     /* errortime */
2348                                  G_TYPE_STRING); /* hash */
2349     gtk_tree_view_set_model(GTK_TREE_VIEW(main_downloads), GTK_TREE_MODEL(dlmodel));
2350
2351     ulmodel = gtk_list_store_new(13, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING, G_TYPE_FLOAT, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING);
2352     gtk_tree_view_set_model(GTK_TREE_VIEW(main_uploads), GTK_TREE_MODEL(ulmodel));
2353
2354     srchmodel = gtk_tree_store_new(10, G_TYPE_STRING, /* fnetname */
2355                                    G_TYPE_STRING,     /* peerid */
2356                                    G_TYPE_STRING,     /* peername */
2357                                    G_TYPE_STRING,     /* filename */
2358                                    G_TYPE_INT,        /* size */
2359                                    G_TYPE_INT,        /* slots */
2360                                    G_TYPE_DOUBLE,     /* resptime */
2361                                    G_TYPE_INT,        /* sizenum */
2362                                    G_TYPE_INT,        /* speed */
2363                                    G_TYPE_STRING);    /* hash */
2364     srchmodelfilter = GTK_TREE_MODEL_FILTER(gtk_tree_model_filter_new(GTK_TREE_MODEL(srchmodel), NULL));
2365     gtk_tree_model_filter_set_visible_func(srchmodelfilter, srchfilterfunc, NULL, NULL);
2366     sortmodel = gtk_tree_model_sort_new_with_model(GTK_TREE_MODEL(srchmodelfilter));
2367     gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(sortmodel), 4, GTK_SORT_DESCENDING);
2368     gtk_tree_view_set_model(GTK_TREE_VIEW(main_srchres), GTK_TREE_MODEL(sortmodel));
2369     g_object_unref(sortmodel);
2370
2371     monospacefont = pango_font_description_from_string("Monospace 10");
2372     gtk_widget_modify_font(main_chatview, monospacefont);
2373     pango_font_description_free(monospacefont);
2374     readconfigfile();
2375     updatesbar(_("Disconnected"));
2376     gtk_widget_show(wnd);
2377     if(autoconn)
2378         dcconnect(dcserver);
2379     g_timeout_add(500, srchstatupdatecb, NULL);
2380     g_timeout_add(5000, ksupdatecb, NULL);
2381     g_timeout_add(1000, updatetransfers, NULL);
2382     gtk_main();
2383     return(0);
2384 }