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