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