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