2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com)
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.
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.
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
20 /* This file is a complete and total mess, mostly because of my
21 * inability to structure GUI programs properly. Looking at it too
22 * closely may cause ocular hemorrhaging. */
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <gdk/gdkkeysyms.h>
33 #include <doldaconnect/uilib.h>
34 #include <doldaconnect/uimisc.h>
35 #include <doldaconnect/utils.h>
46 /* "Programming with libxml2 is like the thrilling embrace of an
49 #include <libxml/parser.h>
50 #include <libxml/tree.h>
55 #include "progressbar.h"
65 size_t poshist[TRHISTSIZE];
66 double timehist[TRHISTSIZE];
72 GtkTextBuffer *textbuf;
81 GtkTreeRowReference *ref;
92 GtkListStore *fnmodel, *ulmodel, *dlmodel, *reslist;
93 int (*pubhubhandler)(int, char *, size_t *) = NULL;
94 GtkTreeStore *srchmodel;
95 GtkTreeModelFilter *srchmodelfilter;
96 GtkTextTagTable *chattags;
97 int dcfd = -1, gdkread = -1, gdkwrite = -1;
98 int pubhubfd = -1, pubhubtag = -1, filterpubhub = 0;
100 regex_t pubhubfilter;
101 pid_t pubhubproc = 0;
102 char *pubhubaddr = NULL;
103 char *connectas = NULL;
104 char *dcserver = NULL;
106 int srchautoupdate = 0;
107 int cursrch = -1, nextsrch = -1;
109 struct srchsize *srchsizes = NULL;
110 struct knownspeed *knownspeeds = NULL;
111 int numsizes = 0, numspeeds = 0, ksqueryseq = -1, ksquerytag = -1, lsrestag = -1;
113 gboolean initdeath(GtkWidget *, gpointer);
114 void cb_main_connmenu_activate(GtkWidget *widget, gpointer data);
115 void cb_main_dconnmenu_activate(GtkWidget *widget, gpointer data);
116 void cb_main_prefmenu_activate(GtkWidget *widget, gpointer data);
117 void cb_main_lsres_activate(GtkWidget *widget, gpointer data);
118 void cb_main_sdmenu_activate(GtkWidget *widget, gpointer data);
119 void cb_inpdialog_entry_activate(GtkWidget *widget, gpointer data);
120 void cb_main_fnaddr_activate(GtkWidget *widget, gpointer data);
121 void cb_main_pubhubfilter_activate(GtkWidget *widget, gpointer data);
122 void cb_main_dcnctbtn_clicked(GtkWidget *widget, gpointer data);
123 void cb_main_phublist_cchange(GtkWidget *widget, gpointer data);
124 void cb_main_phublist_activate(GtkWidget *widget, GtkTreePath *path, GtkTreeViewColumn *col, gpointer data);
125 void cb_main_chatnodes_activate(GtkWidget *widget, GtkTreePath *path, GtkTreeViewColumn *col, gpointer data);
126 void cb_main_srchres_activate(GtkWidget *widget, GtkTreePath *path, GtkTreeViewColumn *col, gpointer data);
127 void cb_main_chatstr_activate(GtkWidget *widget, gpointer data);
128 void cb_main_simplesrch_changed(GtkWidget *widget, gpointer data);
129 void cb_main_realsrch_changed(GtkWidget *widget, gpointer data);
130 void cb_main_srchbtn_clicked(GtkWidget *widget, gpointer data);
131 void cb_main_srchcanbtn_clicked(GtkWidget *widget, gpointer data);
132 gboolean cb_main_trlist_keypress(GtkWidget *widget, GdkEventKey *event, gpointer data);
133 void cb_main_filternoslots_toggled(GtkToggleButton *widget, gpointer data);
134 void cb_main_srhash_activate(GtkWidget *widget, gpointer data);
135 void cb_main_srcopy_activate(GtkWidget *widget, gpointer data);
136 void cb_main_trhash_activate(GtkWidget *widget, gpointer data);
137 void cb_main_trcopy_activate(GtkWidget *widget, gpointer data);
138 void cb_main_trreset_activate(GtkWidget *widget, gpointer data);
139 void cb_main_trcancel_activate(GtkWidget *widget, gpointer data);
140 gboolean cb_main_srpopup(GtkWidget *widget, GdkEventButton *event, gpointer data);
141 gboolean cb_main_trpopup(GtkWidget *widget, GdkEventButton *event, gpointer data);
142 void cb_reslist_reload_clicked(GtkWidget *widget, gpointer data);
143 void cb_reslist_delete_clicked(GtkWidget *widget, gpointer data);
144 void cb_reslist_search_clicked(GtkWidget *widget, gpointer data);
145 void cb_reslist_list_cchange(GtkWidget *widget, gpointer data);
146 void cb_reslist_list_activate(GtkWidget *widget, GtkTreePath *path, GtkTreeViewColumn *col, gpointer data);
147 gboolean cb_reslist_list_keypress(GtkWidget *widget, GdkEventKey *event, gpointer data);
148 void dcfdcallback(gpointer data, gint source, GdkInputCondition condition);
149 void srchstatupdate(void);
150 void transnicebytefunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data);
151 void transnicebytefunc2(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data);
152 void transspeedinfo(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data);
153 void transerrorinfo(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data);
154 void percentagefunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data);
155 void hidezerofunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data);
156 void speedtimefunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data);
158 #define DCCHARSET "windows-1252"
160 #define _(text) gettext(text)
162 #include "mainwnd.gtk"
163 #include "inpdialog.gtk"
165 #include "reslist.gtk"
167 void updatewrite(void)
174 gdkwrite = gdk_input_add(dcfd, GDK_INPUT_WRITE, dcfdcallback, NULL);
178 gdk_input_remove(gdkwrite);
188 gettimeofday(&tv, NULL);
189 return((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
192 void fndestroycb(struct dc_fnetnode *fn)
195 GtkTextBuffer *textbuf;
198 g_object_unref(data->textbuf);
200 if(curchat == fn->id)
202 textbuf = gtk_text_buffer_new(chattags);
203 gtk_text_view_set_buffer(GTK_TEXT_VIEW(main_chatview), textbuf);
204 g_object_unref(textbuf);
208 void addfndata(struct dc_fnetnode *fn)
212 if(fn->udata != NULL)
214 fn->destroycb = fndestroycb;
215 data = smalloc(sizeof(*data));
216 data->textbuf = gtk_text_buffer_new(chattags);
220 void trdestroycb(struct dc_transfer *tr)
225 void addtrdata(struct dc_transfer *tr)
229 if(tr->udata != NULL)
231 tr->destroycb = trdestroycb;
232 data = smalloc(sizeof(*data));
233 memset(data, 0, sizeof(*data));
237 void updatetrdata(struct dc_transfer *tr)
243 if(data->hc < TRHISTSIZE)
245 data->poshist[data->hc] = tr->curpos;
246 data->timehist[data->hc] = ntime();
249 for(i = 0; i < TRHISTSIZE - 1; i++)
251 data->poshist[i] = data->poshist[i + 1];
252 data->timehist[i] = data->timehist[i + 1];
254 data->poshist[i] = tr->curpos;
255 data->timehist[i] = ntime();
259 char *getfnstatestock(int state)
261 if(state == DC_FNN_STATE_SYN)
262 return("gtk-jump-to");
263 if(state == DC_FNN_STATE_HS)
264 return("gtk-execute");
265 if(state == DC_FNN_STATE_EST)
267 if(state == DC_FNN_STATE_DEAD)
268 return("gtk-cancel");
272 void updatehublist(void)
275 struct dc_fnetnode *fn;
282 for(fn = dc_fnetnodes; fn != NULL; fn = fn->next)
288 if(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(fnmodel), &iter))
292 gtk_tree_model_get(GTK_TREE_MODEL(fnmodel), &iter, 0, &id, -1);
293 if((fn = dc_findfnetnode(id)) == NULL)
295 /* I can't seem to get a sensible reply fromp
296 * gtk_list_store, so I'm just doing this
298 gtk_list_store_remove(fnmodel, &iter);
302 gtk_tree_model_get(GTK_TREE_MODEL(fnmodel), &iter, 1, &name, 2, &state, 3, &numusers, -1);
306 buf = icswcstombs(fn->name, "UTF-8", NULL);
307 if(strcmp(buf, name))
308 gtk_list_store_set(fnmodel, &iter, 1, buf, -1);
309 if(state != fn->state)
311 gtk_list_store_set(fnmodel, &iter, 2, fn->state, -1);
312 gtk_list_store_set(fnmodel, &iter, 4, getfnstatestock(fn->state), -1);
314 if(numusers != fn->numusers)
315 gtk_list_store_set(fnmodel, &iter, 3, fn->numusers, -1);
319 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(fnmodel), &iter));
322 for(fn = dc_fnetnodes; fn != NULL; fn = fn->next)
329 buf = icswcstombs(fn->name, "UTF-8", NULL);
330 gtk_list_store_append(fnmodel, &iter);
331 gtk_list_store_set(fnmodel, &iter, 0, fn->id, 1, buf, 2, fn->state, 3, fn->numusers, 4, getfnstatestock(fn->state), -1);
337 char *bytes2si(long long bytes)
345 for(i = 0; (b >= 1024) && (i < 4); i++)
357 snprintf(ret, 64, "%.1f %s", b, sd);
361 void percentagefunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
367 colnum = GPOINTER_TO_INT(data);
368 gtk_tree_model_get(model, iter, colnum, &val, -1);
369 snprintf(buf, 64, "%.2f%%", (double)(val * 100.0));
370 g_object_set(rend, "text", buf, NULL);
373 void transnicebytefunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
378 colnum = GPOINTER_TO_INT(data);
379 gtk_tree_model_get(model, iter, colnum, &val, -1);
381 snprintf(buf, 64, "%'i", val);
383 strcpy(buf, _("Unknown"));
384 g_object_set(rend, "text", buf, NULL);
387 void transnicebytefunc2(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
393 colnum = GPOINTER_TO_INT(data);
394 gtk_tree_model_get(model, iter, colnum, &val, -1);
396 strcpy(buf, bytes2si(val));
398 strcpy(buf, _("Unknown"));
399 g_object_set(rend, "text", buf, NULL);
402 void hidezerofunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
407 colnum = GPOINTER_TO_INT(data);
408 gtk_tree_model_get(model, iter, colnum, &val, -1);
410 snprintf(buf, 64, "%i", val);
413 g_object_set(rend, "text", buf, NULL);
416 void speedtimefunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
418 int speed, size, time;
421 gtk_tree_model_get(model, iter, 4, &size, 8, &speed, -1);
424 time = (size / speed) / 60;
426 snprintf(buf, 64, "%'i (<00:01)", speed);
428 snprintf(buf, 64, "%'i (%02i:%02i)", speed, time / 60, time % 60);
429 } else if(speed == 0) {
432 strcpy(buf, _("Unknown"));
434 g_object_set(rend, "text", buf, NULL);
437 void transspeedinfo(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
440 struct dc_transfer *tr;
445 gtk_tree_model_get(model, iter, 0, &id, -1);
446 if((tr = dc_findtransfer(id)) != NULL)
449 if((tr->state != DC_TRNS_MAIN) || (d == NULL))
452 } else if(d->hc < 2) {
455 speed = (((double)(d->poshist[d->hc - 1] - d->poshist[0])) / (d->timehist[d->hc - 1] - d->timehist[0]));
456 snprintf(buf, 64, "%s/s", bytes2si(speed));
461 g_object_set(rend, "text", buf, NULL);
464 void transerrorinfo(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
468 char finbuf[64], tbuf[64], *errstr;
470 gtk_tree_model_get(model, iter, 10, &error, 11, &errortime, -1);
471 if(error != DC_TRNSE_NOERROR)
473 if(error == DC_TRNSE_NOTFOUND)
474 errstr = _("Not found");
475 else if(error == DC_TRNSE_NOSLOTS)
476 errstr = _("No slots");
477 strftime(tbuf, 64, _("%H:%M:%S"), localtime(&errortime));
478 snprintf(finbuf, 64, _("%s (reported at %s)"), errstr, tbuf);
482 g_object_set(rend, "text", finbuf, NULL);
485 char *gettrstatestock(int state)
487 if(state == DC_TRNS_WAITING)
488 return("gtk-jump-to");
489 if(state == DC_TRNS_HS)
490 return("gtk-execute");
491 if(state == DC_TRNS_MAIN)
492 return("gtk-network");
493 if(state == DC_TRNS_DONE)
498 gint updatetransfers(gpointer data)
500 struct dc_transfer *tr;
505 for(tr = dc_transfers; tr != NULL; tr = tr->next)
507 if((d = tr->udata) != NULL)
509 if((d->hc > 0) && ((now - d->timehist[d->hc - 1]) > 2))
516 void updatetransferlists(void)
520 struct dc_transfer *transfer;
524 char *peerid, *peernick, *path, *hash;
525 int state, dir, size, curpos, error;
527 GtkListStore *stores[3];
529 for(transfer = dc_transfers; transfer != NULL; transfer = transfer->next)
531 stores[DC_TRNSD_UNKNOWN] = NULL;
532 stores[DC_TRNSD_UP] = ulmodel;
533 stores[DC_TRNSD_DOWN] = dlmodel;
534 for(i = 0; i < 3; i++)
536 if(stores[i] == NULL)
542 if(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(stores[i]), &iter))
546 gtk_tree_model_get(GTK_TREE_MODEL(stores[i]), &iter, 0, &id, 1, &dir, -1);
547 if(((transfer = dc_findtransfer(id)) == NULL) || (transfer->dir != dir))
549 gtk_list_store_remove(stores[i], &iter);
554 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);
555 if(state != transfer->state)
556 gtk_list_store_set(stores[i], &iter, 2, transfer->state, 8, gettrstatestock(transfer->state), -1);
557 if(size != transfer->size)
558 gtk_list_store_set(stores[i], &iter, 6, transfer->size, -1);
559 if(curpos != transfer->curpos)
561 gtk_list_store_set(stores[i], &iter, 7, transfer->curpos, -1);
562 if(transfer->udata != NULL)
563 updatetrdata(transfer);
565 if(error != transfer->error)
566 gtk_list_store_set(stores[i], &iter, 10, transfer->error, -1);
567 if(errortime != transfer->errortime)
568 gtk_list_store_set(stores[i], &iter, 11, transfer->errortime, -1);
569 if((transfer->size > 0) && (transfer->curpos > 0))
570 gtk_list_store_set(stores[i], &iter, 9, (float)transfer->curpos / (float)transfer->size, -1);
571 buf = icswcstombs(transfer->peerid, "UTF-8", NULL);
572 if(strcmp(buf, peerid))
573 gtk_list_store_set(stores[i], &iter, 3, buf, -1);
574 buf = icswcstombs(((transfer->peernick == NULL) || (transfer->peernick[0] == L'\0'))?transfer->peerid:transfer->peernick, "UTF-8", NULL);
575 if(strcmp(buf, peernick))
576 gtk_list_store_set(stores[i], &iter, 4, buf, -1);
577 buf = (transfer->path == NULL)?_("Unknown"):icswcstombs(transfer->path, "UTF-8", NULL);
578 if(strcmp(buf, path))
579 gtk_list_store_set(stores[i], &iter, 5, buf, -1);
580 buf = (transfer->hash == NULL)?"":icswcstombs(transfer->hash, "UTF-8", NULL);
581 if(strcmp(buf, path))
582 gtk_list_store_set(stores[i], &iter, 12, buf, -1);
588 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(stores[i]), &iter));
592 for(transfer = dc_transfers; transfer != NULL; transfer = transfer->next)
596 if(stores[transfer->dir] != NULL)
598 peerid = icwcstombs(transfer->peerid, "UTF-8");
599 peernick = icwcstombs(((transfer->peernick == NULL) || (transfer->peernick[0] == L'\0'))?transfer->peerid:transfer->peernick, "UTF-8");
600 path = (transfer->path == NULL)?_("Unknown"):icwcstombs(transfer->path, "UTF-8");
601 hash = (transfer->hash == NULL)?"":icwcstombs(transfer->hash, "UTF-8");
602 gtk_list_store_append(stores[transfer->dir], &iter);
603 gtk_list_store_set(stores[transfer->dir], &iter,
612 8, gettrstatestock(transfer->state),
615 11, transfer->errortime,
620 if(transfer->path != NULL)
622 if(transfer->hash != NULL)
630 void updatesbar(char *msg)
632 gtk_statusbar_pop(GTK_STATUSBAR(main_statusbar), 0);
633 gtk_statusbar_push(GTK_STATUSBAR(main_statusbar), 0, msg);
636 void freesrchsizes(void)
640 for(i = 0; i < numsizes; i++)
642 if(srchsizes[i].ref != NULL)
643 gtk_tree_row_reference_free(srchsizes[i].ref);
645 if(srchsizes != NULL)
651 void dcdisconnected(void)
655 gdk_input_remove(gdkread);
660 updatetransferlists();
661 cursrch = nextsrch = -1;
662 gtk_tree_store_clear(srchmodel);
664 gtk_widget_set_sensitive(main_connmenu, TRUE);
665 gtk_widget_set_sensitive(main_dconnmenu, FALSE);
666 gtk_widget_set_sensitive(main_simplesrch, TRUE);
667 gtk_widget_set_sensitive(main_realsrch, TRUE);
668 gtk_widget_set_sensitive(main_srchbtn, TRUE);
669 gtk_widget_set_sensitive(main_srchcanbtn, FALSE);
670 updatesbar(_("Disconnected"));
673 char *inputbox(char *title, char *prompt, char *def, int echo)
679 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);
680 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(inpdialog)->vbox), swnd = create_inpdialog_wnd(), TRUE, TRUE, 0);
681 gtk_widget_show(swnd);
683 gtk_entry_set_visibility(GTK_ENTRY(inpdialog_entry), FALSE);
684 gtk_label_set_text(GTK_LABEL(inpdialog_prompt), prompt);
685 gtk_entry_set_text(GTK_ENTRY(inpdialog_entry), def);
686 resp = gtk_dialog_run(GTK_DIALOG(inpdialog));
688 gtk_entry_set_visibility(GTK_ENTRY(inpdialog_entry), TRUE);
689 if(resp == GTK_RESPONSE_ACCEPT)
690 buf = strdup(gtk_entry_get_text(GTK_ENTRY(inpdialog_entry)));
693 gtk_widget_destroy(inpdialog);
698 int msgbox(int type, int buttons, char *format, ...)
699 #if defined(__GNUC__)
700 __attribute__ ((format (printf, 3, 4)))
704 int msgbox(int type, int buttons, char *format, ...)
711 va_start(args, format);
712 buf = vsprintf2(format, args);
714 swnd = gtk_message_dialog_new(GTK_WINDOW(main_wnd), GTK_DIALOG_MODAL, type, buttons, "%s", buf);
715 resp = gtk_dialog_run(GTK_DIALOG(swnd));
716 gtk_widget_destroy(swnd);
721 void readconfigfile(void)
724 char *homedir, *buf, *p;
727 if((homedir = getenv("HOME")) == NULL)
729 fprintf(stderr, "warning: could not find home directory!\n");
732 buf = sprintf2("%s/.dolconrc", homedir);
733 if((cfgfile = fopen(buf, "r")) == NULL)
742 while(fgets(buf, 1024, cfgfile) != NULL)
746 p = buf + strlen(buf);
749 if((p = strchr(buf, ':')) == NULL)
752 while((*p == ' ') || (*p == '\t'))
754 if(!strcmp(buf, "wnd-width"))
757 } else if(!strcmp(buf, "wnd-height")) {
759 } else if(!strcmp(buf, "pane1-pos")) {
760 gtk_paned_set_position(GTK_PANED(main_pane1), atoi(p));
761 } else if(!strcmp(buf, "pane2-pos")) {
762 gtk_paned_set_position(GTK_PANED(main_pane2), atoi(p));
763 } else if(!strcmp(buf, "pane3-pos")) {
764 gtk_paned_set_position(GTK_PANED(main_pane3), atoi(p));
765 } else if(!strcmp(buf, "pubhubaddr")) {
767 pubhubaddr = sstrdup(p);
768 } else if(!strcmp(buf, "dcserver")) {
770 dcserver = sstrdup(p);
771 } else if(!strcmp(buf, "advexpanded")) {
772 gtk_expander_set_expanded(GTK_EXPANDER(main_advexp), atoi(p));
773 } else if(!strcmp(buf, "connectas")) {
775 connectas = sstrdup(p);
776 } else if(!strcmp(buf, "autoconn")) {
778 } else if(!strcmp(buf, "filternoslots")) {
779 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(main_filternoslots), atoi(p));
788 gtk_window_resize(GTK_WINDOW(main_wnd), w, h);
791 void updateconfigfile(void)
797 if((homedir = getenv("HOME")) == NULL)
799 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not get your home directory!"));
802 buf = sprintf2("%s/.dolconrc", homedir);
803 if((cfgfile = fopen(buf, "w")) == NULL)
806 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not open configuration file for writing: %s"), strerror(errno));
810 gtk_window_get_size(GTK_WINDOW(main_wnd), &w, &h);
811 fprintf(cfgfile, "wnd-width: %i\n", w);
812 fprintf(cfgfile, "wnd-height: %i\n", h);
813 fprintf(cfgfile, "pane1-pos: %i\n", gtk_paned_get_position(GTK_PANED(main_pane1)));
814 fprintf(cfgfile, "pane2-pos: %i\n", gtk_paned_get_position(GTK_PANED(main_pane2)));
815 fprintf(cfgfile, "pane3-pos: %i\n", gtk_paned_get_position(GTK_PANED(main_pane3)));
816 fprintf(cfgfile, "pubhubaddr: %s\n", pubhubaddr);
817 fprintf(cfgfile, "dcserver: %s\n", dcserver);
818 fprintf(cfgfile, "advexpanded: %i\n", gtk_expander_get_expanded(GTK_EXPANDER(main_advexp)));
819 fprintf(cfgfile, "connectas: %s\n", connectas);
820 fprintf(cfgfile, "autoconn: %i\n", autoconn);
821 fprintf(cfgfile, "filternoslots: %i\n", gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(main_filternoslots)));
825 gboolean initdeath(GtkWidget *widget, gpointer data)
832 void cb_inpdialog_entry_activate(GtkWidget *widget, gpointer data)
834 gtk_dialog_response(GTK_DIALOG(inpdialog), GTK_RESPONSE_ACCEPT);
837 int loginconv(int type, wchar_t *prompt, char **resp, void *data)
843 buf = icwcstombs(prompt, "UTF-8");
846 case DC_LOGIN_CONV_NOECHO:
847 if((*resp = inputbox(_("Login"), buf, "", 0)) == NULL)
850 case DC_LOGIN_CONV_ECHO:
851 if((*resp = inputbox(_("Login"), buf, "", 1)) == NULL)
854 case DC_LOGIN_CONV_INFO:
855 msgbox(GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "%s", buf);
857 case DC_LOGIN_CONV_ERROR:
858 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", buf);
866 void getfnlistcallback(int resp, void *data)
871 void gettrlistcallback(int resp, void *data)
873 updatetransferlists();
876 void logincallback(int err, wchar_t *reason, void *data)
880 case DC_LOGIN_ERR_SUCCESS:
881 dc_queuecmd(NULL, NULL, L"notify", L"all", L"on", NULL);
882 dc_getfnlistasync(getfnlistcallback, NULL);
883 dc_gettrlistasync(gettrlistcallback, NULL);
884 updatesbar("Authenticated");
886 case DC_LOGIN_ERR_NOLOGIN:
887 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not negotiate an acceptable authentication mechanism"));
891 case DC_LOGIN_ERR_SERVER:
892 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("The server has encountered an error"));
896 case DC_LOGIN_ERR_USER:
897 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Internal client error"));
901 case DC_LOGIN_ERR_CONV:
905 case DC_LOGIN_ERR_AUTHFAIL:
906 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Login attempt failed!"));
914 GtkTreeIter *ref2iter(GtkTreeRowReference *ref)
916 static GtkTreeIter iter;
919 assert((path = gtk_tree_row_reference_get_path(ref)) != NULL);
920 assert(gtk_tree_model_get_iter(GTK_TREE_MODEL(srchmodel), &iter, path));
921 gtk_tree_path_free(path);
925 GtkTreeRowReference *iter2ref(GtkTreeIter *iter)
928 GtkTreeRowReference *ref;
930 assert((path = gtk_tree_model_get_path(GTK_TREE_MODEL(srchmodel), iter)) != NULL);
931 assert((ref = gtk_tree_row_reference_new(GTK_TREE_MODEL(srchmodel), path)) != NULL);
932 gtk_tree_path_free(path);
936 struct srchsize *finddiscsize(void)
941 for(i = 0; i < numsizes; i++)
943 if(srchsizes[i].size == -1)
944 return(&srchsizes[i]);
946 srchsizes = srealloc(srchsizes, sizeof(*srchsizes) * ++numsizes);
947 srchsizes[i].size = -1;
948 srchsizes[i].num = 1;
949 srchsizes[i].slots = 0;
950 srchsizes[i].resptime = 0.0;
951 gtk_tree_store_append(srchmodel, &iter, NULL);
952 gtk_tree_store_set(srchmodel, &iter, 3, _("Discrete sizes"), 7, 1, -1);
953 srchsizes[i].ref = iter2ref(&iter);
954 return(&srchsizes[i]);
957 struct knownspeed *findksentbyname(char *userid)
961 for(i = 0; i < numspeeds; i++)
963 if(!strcmp(knownspeeds[i].userid, userid))
964 return(&knownspeeds[i]);
969 struct knownspeed *findksentbyseq(int seq)
973 for(i = 0; i < numspeeds; i++)
975 if(knownspeeds[i].seq == seq)
976 return(&knownspeeds[i]);
981 gboolean ksupdaterow(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
983 struct knownspeed *ks;
986 gtk_tree_model_get(GTK_TREE_MODEL(model), iter, 1, &userid, -1);
989 ks = findksentbyname(userid);
992 knownspeeds = srealloc(knownspeeds, (numspeeds + 1) * sizeof(*knownspeeds));
993 ks = &knownspeeds[numspeeds];
995 ks->userid = sstrdup(userid);
1002 gtk_tree_store_set(GTK_TREE_STORE(model), iter, 8, ks->speed, -1);
1006 gint ksupdatecb(gpointer data)
1010 wchar_t **users, *buf;
1011 size_t userssize, usersdata;
1013 if(ksquerytag != -1)
1017 for(i = 0; i < numspeeds;)
1019 if(now - knownspeeds[i].fetched > 60)
1021 free(knownspeeds[i].userid);
1022 memmove(&knownspeeds[i], &knownspeeds[i + 1], (--numspeeds - i) * sizeof(*knownspeeds));
1027 if(oldnum != numspeeds)
1034 knownspeeds = srealloc(knownspeeds, numspeeds * sizeof(*knownspeeds));
1037 gtk_tree_model_foreach(GTK_TREE_MODEL(srchmodel), ksupdaterow, NULL);
1038 if(ksqueryseq == -2)
1041 userssize = usersdata = 0;
1043 for(i = 0; i < numspeeds; i++)
1045 if(knownspeeds[i].seq == -2)
1047 assert((buf = icmbstowcs(knownspeeds[i].userid, "UTF-8")) != NULL);
1048 knownspeeds[i].seq = ksqueryseq++;
1049 addtobuf(users, buf);
1052 addtobuf(users, NULL);
1053 ksquerytag = dc_queuecmd(NULL, NULL, L"filtercmd", L"userspeeda", L"%a", users, NULL);
1054 dc_freewcsarr(users);
1059 void handleresps(void)
1062 struct dc_response *resp;
1063 struct dc_intresp *ires;
1064 struct dc_fnetnode *fn;
1065 struct fndata *fndata;
1067 GtkTreeIter titer, piter;
1070 struct srchsize *ss;
1071 struct knownspeed *ks;
1073 while((resp = dc_getresp()) != NULL)
1075 if(!wcscmp(resp->cmdname, L".connect"))
1077 if(resp->code != 201)
1079 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("The server refused the connection"));
1082 } else if(dc_checkprotocol(resp, DC_LATEST)) {
1083 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Server protocol revision mismatch"));
1087 tosbuf = 0x10; /* Minimum cost */
1088 setsockopt(dcfd, SOL_IP, IP_TOS, &tosbuf, sizeof(tosbuf));
1089 updatesbar(_("Connected"));
1090 dc_loginasync(connectas, 1, loginconv, logincallback, NULL);
1092 } else if(!wcscmp(resp->cmdname, L".notify")) {
1093 dc_uimisc_handlenotify(resp);
1097 if((ires = dc_interpret(resp)) != NULL)
1099 if((fn = dc_findfnetnode(ires->argv[0].val.num)) != NULL)
1102 gtk_text_buffer_get_end_iter(fndata->textbuf, &iter);
1103 if((buf = icwcstombs(ires->argv[3].val.str, "UTF-8")) != NULL)
1105 gtk_text_buffer_insert_with_tags_by_name(fndata->textbuf, &iter, "<", -1, "sender", NULL);
1106 gtk_text_buffer_insert_with_tags_by_name(fndata->textbuf, &iter, buf, -1, "sender", NULL);
1107 gtk_text_buffer_insert_with_tags_by_name(fndata->textbuf, &iter, ">", -1, "sender", NULL);
1108 gtk_text_buffer_insert(fndata->textbuf, &iter, " ", -1);
1111 if((buf = icwcstombs(ires->argv[4].val.str, "UTF-8")) != NULL)
1113 gtk_text_buffer_insert(fndata->textbuf, &iter, buf, -1);
1114 gtk_text_buffer_insert(fndata->textbuf, &iter, "\n", -1);
1116 if(curchat == fn->id)
1117 gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(main_chatview), &iter, 0, 0, 0, 0);
1139 updatetransferlists();
1142 if((ires = dc_interpret(resp)) != NULL)
1144 if(ires->argv[0].val.num == nextsrch)
1145 srcheta = time(NULL) + ires->argv[0].val.num;
1150 if((ires = dc_interpret(resp)) != NULL)
1152 if(ires->argv[0].val.num == nextsrch)
1155 dc_queuecmd(NULL, NULL, L"cansrch", L"%i", cursrch, NULL);
1158 gtk_widget_set_sensitive(main_realsrch, TRUE);
1159 gtk_widget_set_sensitive(main_simplesrch, TRUE);
1160 gtk_widget_set_sensitive(main_srchbtn, TRUE);
1161 gtk_widget_set_sensitive(main_srchcanbtn, FALSE);
1163 gtk_entry_set_text(GTK_ENTRY(main_realsrch), "");
1164 gtk_entry_set_text(GTK_ENTRY(main_simplesrch), "");
1165 gtk_tree_store_clear(srchmodel);
1172 if((ires = dc_interpret(resp)) != NULL)
1174 if(ires->argv[0].val.num == cursrch)
1176 for(i = 0; i < numsizes; i++)
1178 if(srchsizes[i].size == ires->argv[4].val.num)
1183 srchsizes = srealloc(srchsizes, sizeof(*srchsizes) * ++numsizes);
1184 srchsizes[i].size = ires->argv[4].val.num;
1185 srchsizes[i].num = 1;
1186 srchsizes[i].slots = ires->argv[5].val.num;
1187 srchsizes[i].resptime = ires->argv[7].val.flnum;
1188 ss = finddiscsize();
1189 ss->slots += ires->argv[5].val.num;
1190 if((ss->resptime == 0.0) || (ss->resptime > ires->argv[7].val.flnum))
1191 ss->resptime = ires->argv[7].val.flnum;
1192 piter = *ref2iter(ss->ref);
1193 gtk_tree_store_set(srchmodel, &piter, 5, ss->slots, 6, ss->resptime, -1);
1194 gtk_tree_store_append(srchmodel, &titer, &piter);
1195 srchsizes[i].ref = iter2ref(&titer);
1196 } else if(srchsizes[i].num == 1) {
1197 char *filename, *peername, *fnetname, *hash;
1201 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);
1202 gtk_tree_store_remove(srchmodel, ref2iter(srchsizes[i].ref));
1203 gtk_tree_row_reference_free(srchsizes[i].ref);
1204 ss = finddiscsize();
1206 gtk_tree_store_set(srchmodel, ref2iter(ss->ref), 5, ss->slots, -1);
1207 gtk_tree_store_append(srchmodel, &piter, NULL);
1208 srchsizes[i].slots = ires->argv[5].val.num + slots;
1209 srchsizes[i].resptime = (ires->argv[7].val.flnum < resptime)?ires->argv[7].val.flnum:resptime;
1210 srchsizes[i].num = 2;
1211 srchsizes[i].ref = iter2ref(&piter);
1212 gtk_tree_store_set(srchmodel, &piter, 4, srchsizes[i].size, 5, srchsizes[i].slots, 6, srchsizes[i].resptime, 7, 2, -1);
1213 if((buf = icwcstombs(ires->argv[1].val.str, "UTF-8")) != NULL)
1216 /* XXX: Too NMDC-specific! */
1217 if(strrchr(p, '\\') != NULL)
1218 p = strrchr(p, '\\') + 1;
1219 gtk_tree_store_set(srchmodel, &piter, 3, p, -1);
1222 gtk_tree_store_append(srchmodel, &titer, &piter);
1223 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);
1224 g_free(filename); g_free(peername); g_free(fnetname); g_free(hash);
1225 gtk_tree_store_append(srchmodel, &titer, &piter);
1228 srchsizes[i].slots += ires->argv[5].val.num;
1229 if(ires->argv[7].val.flnum < srchsizes[i].resptime)
1230 srchsizes[i].resptime = ires->argv[7].val.flnum;
1231 piter = *ref2iter(srchsizes[i].ref);
1232 gtk_tree_store_set(srchmodel, &piter, 5, srchsizes[i].slots, 6, srchsizes[i].resptime, 7, srchsizes[i].num, -1);
1233 gtk_tree_store_append(srchmodel, &titer, &piter);
1235 if((buf = icwcstombs(ires->argv[1].val.str, "UTF-8")) != NULL)
1237 gtk_tree_store_set(srchmodel, &titer, 3, buf, -1);
1240 if((buf = icwcstombs(ires->argv[2].val.str, "UTF-8")) != NULL)
1242 gtk_tree_store_set(srchmodel, &titer, 0, buf, -1);
1245 if((buf = icwcstombs(ires->argv[3].val.str, "UTF-8")) != NULL)
1247 gtk_tree_store_set(srchmodel, &titer, 1, buf, -1);
1248 gtk_tree_store_set(srchmodel, &titer, 2, buf, -1);
1251 if((buf = icwcstombs(ires->argv[8].val.str, "UTF-8")) != NULL)
1253 gtk_tree_store_set(srchmodel, &titer, 9, buf, -1);
1256 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);
1264 } else if(!wcscmp(resp->cmdname, L"filtercmd")) {
1265 if((ksquerytag >= 0) && (ksquerytag == resp->tag))
1267 for(i = 0; i < resp->numlines; i++)
1269 assert((ks = findksentbyseq(i)) != NULL);
1270 ks->speed = wcstol(resp->rlines[i].argv[1], NULL, 10);
1272 ks->fetched = time(NULL);
1276 } else if((lsrestag >= 0) && (lsrestag == resp->tag)) {
1277 for(i = 0; i < resp->numlines; i++)
1279 if(!wcsncmp(resp->rlines[i].argv[1], L"id:", 3))
1281 gtk_list_store_append(reslist, &titer);
1282 gtk_list_store_set(reslist, &titer, 0, icswcstombs(resp->rlines[i].argv[1] + 3, "UTF-8", NULL), -1);
1283 } else if(!wcsncmp(resp->rlines[i].argv[1], L"size:", 5)) {
1284 gtk_list_store_set(reslist, &titer, 1, wcstol(resp->rlines[i].argv[1] + 5, NULL, 10), -1);
1285 } else if(!wcsncmp(resp->rlines[i].argv[1], L"prog:", 5)) {
1286 gtk_list_store_set(reslist, &titer, 2, wcstol(resp->rlines[i].argv[1] + 5, NULL, 10), -1);
1287 } else if(!wcsncmp(resp->rlines[i].argv[1], L"name:", 5)) {
1288 gtk_list_store_set(reslist, &titer, 3, icswcstombs(resp->rlines[i].argv[1] + 5, "UTF-8", NULL), -1);
1289 } else if(!wcsncmp(resp->rlines[i].argv[1], L"lock:", 5)) {
1290 if(!wcscmp(resp->rlines[i].argv[1] + 5, L"yes"))
1291 gtk_list_store_set(reslist, &titer, 4, TRUE, -1);
1293 gtk_list_store_set(reslist, &titer, 4, FALSE, -1);
1294 } else if(!wcsncmp(resp->rlines[i].argv[1], L"hash:", 5)) {
1295 gtk_list_store_set(reslist, &titer, 5, icswcstombs(resp->rlines[i].argv[1] + 5, "UTF-8", NULL), -1);
1299 gtk_widget_set_sensitive(reslist_reload, TRUE);
1307 void dcfdcallback(gpointer data, gint source, GdkInputCondition condition)
1311 if(((condition & GDK_INPUT_READ) && dc_handleread()) || ((condition & GDK_INPUT_WRITE) && dc_handlewrite()))
1317 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("The server has closed the connection"));
1319 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("The connection to the server failed:\n\n%s"), strerror(errnobak));
1326 void cb_main_dconnmenu_activate(GtkWidget *widget, gpointer data)
1334 void cb_main_prefmenu_activate(GtkWidget *widget, gpointer data)
1336 GtkWidget *dialog, *swnd;
1339 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);
1340 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), swnd = create_pref_wnd(), TRUE, TRUE, 0);
1341 gtk_entry_set_text(GTK_ENTRY(pref_pubhuburl), pubhubaddr);
1342 gtk_entry_set_text(GTK_ENTRY(pref_connectas), connectas);
1343 gtk_entry_set_text(GTK_ENTRY(pref_dcserver), dcserver);
1344 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(pref_autoconn), autoconn);
1345 gtk_widget_show(swnd);
1346 resp = gtk_dialog_run(GTK_DIALOG(dialog));
1347 if(resp == GTK_RESPONSE_ACCEPT)
1350 pubhubaddr = sstrdup(gtk_entry_get_text(GTK_ENTRY(pref_pubhuburl)));
1352 connectas = sstrdup(gtk_entry_get_text(GTK_ENTRY(pref_connectas)));
1354 dcserver = sstrdup(gtk_entry_get_text(GTK_ENTRY(pref_dcserver)));
1355 autoconn = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(pref_autoconn));
1357 gtk_widget_destroy(dialog);
1360 void cb_main_lsres_activate(GtkWidget *widget, gpointer data)
1362 gtk_list_store_clear(reslist);
1363 gtk_widget_set_sensitive(reslist_delete, FALSE);
1364 gtk_widget_set_sensitive(reslist_search, FALSE);
1365 gtk_widget_show(reslist_wnd);
1368 lsrestag = dc_queuecmd(NULL, NULL, L"filtercmd", L"lsres", NULL);
1369 gtk_widget_set_sensitive(reslist_reload, FALSE);
1373 void dcconnect(char *host)
1375 dcfd = dc_connect(host);
1378 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect:\n\n%s"), strerror(errno));
1381 gdkread = gdk_input_add(dcfd, GDK_INPUT_READ, dcfdcallback, NULL);
1383 gtk_widget_set_sensitive(main_connmenu, FALSE);
1384 gtk_widget_set_sensitive(main_dconnmenu, TRUE);
1385 updatesbar(_("Connecting..."));
1388 void cb_main_connmenu_activate(GtkWidget *widget, gpointer data)
1394 if((buf = inputbox(_("Connect"), _("Server address:"), dcserver, 1)) == NULL)
1400 void cb_main_sdmenu_activate(GtkWidget *widget, gpointer data)
1403 struct dc_response *resp;
1407 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1410 tag = dc_queuecmd(NULL, NULL, L"shutdown", NULL);
1411 if((resp = dc_gettaggedrespsync(tag)) != NULL)
1413 if(resp->code == 502)
1414 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1420 void cb_main_fnaddr_activate(GtkWidget *widget, gpointer data)
1423 struct dc_response *resp;
1428 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1431 toks = dc_lexsexpr(icsmbstowcs((char *)gtk_entry_get_text(GTK_ENTRY(main_fnaddr)), "UTF-8", NULL));
1434 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Illegal address entered"));
1437 if(wcschr(toks[0], L':') == NULL)
1439 toks[0] = srealloc(toks[0], (wcslen(toks[0]) + 5) * sizeof(wchar_t));
1440 wcscat(toks[0], L":411");
1442 tag = dc_queuecmd(NULL, NULL, L"cnct", L"dc", L"%a", toks, NULL);
1443 dc_freewcsarr(toks);
1444 if((resp = dc_gettaggedrespsync(tag)) != NULL)
1446 if(resp->code == 502)
1447 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1448 if(resp->code == 509)
1449 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("The server could not parse that address"));
1450 if(resp->code == 515)
1451 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("There are too many hubs connected"));
1454 gtk_entry_set_text(GTK_ENTRY(main_fnaddr), "");
1458 void setpubhubmodel(GtkTreeModel *model, int sortcol, int numcols, int *cols, char **names)
1460 GtkTreeViewColumn *col;
1461 GtkCellRenderer *rnd;
1462 GtkTreeModel *sortmodel;
1465 while((col = gtk_tree_view_get_column(GTK_TREE_VIEW(main_phublist), 0)) != NULL)
1466 gtk_tree_view_remove_column(GTK_TREE_VIEW(main_phublist), col);
1467 for(i = 0; i < numcols; i++) {
1468 if(gtk_tree_model_get_column_type(model, cols[i]) == G_TYPE_INT64)
1470 col = gtk_tree_view_column_new();
1471 gtk_tree_view_column_set_title(col, names[i]);
1472 rnd = gtk_cell_renderer_text_new();
1473 gtk_tree_view_column_pack_start(col, rnd, TRUE);
1474 gtk_tree_view_column_set_cell_data_func(col, rnd, transnicebytefunc2, GINT_TO_POINTER(cols[i]), NULL);
1476 col = gtk_tree_view_column_new_with_attributes(names[i], gtk_cell_renderer_text_new(), "text", cols[i], NULL);
1478 gtk_tree_view_column_set_sort_column_id(col, cols[i]);
1479 gtk_tree_view_column_set_resizable(col, TRUE);
1480 gtk_tree_view_append_column(GTK_TREE_VIEW(main_phublist), col);
1482 sortmodel = gtk_tree_model_sort_new_with_model(model);
1483 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(sortmodel), sortcol, GTK_SORT_DESCENDING);
1484 gtk_tree_view_set_model(GTK_TREE_VIEW(main_phublist), sortmodel);
1485 g_object_unref(sortmodel);
1488 xmlNodePtr findnode(xmlNodePtr node, char *name)
1490 for(; node != NULL; node = node->next)
1492 if(!strcmp((char *)node->name, name))
1498 int pubhubxmlhandler(int op, char *buf, size_t *len)
1500 static xmlParserCtxtPtr ctxt = NULL;
1502 xmlNodePtr dr, r, cr, c, n;
1503 int numcols, *cols, sortcol;
1505 char **names, *name, *stype, *attr;
1506 GtkListStore *model;
1518 ctxt = xmlCreatePushParserCtxt(NULL, NULL, buf, *len, pubhubaddr);
1523 xmlParseChunk(ctxt, buf, *len, 0);
1530 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("A hub list could not be read from %s"), pubhubaddr);
1533 xmlParseChunk(ctxt, NULL, 0, 1);
1534 if(!ctxt->wellFormed)
1536 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("The hub list at %s is not valid"), pubhubaddr);
1540 dr = xmlDocGetRootElement(ctxt->myDoc);
1542 r = findnode(dr->children, "Hubs");
1544 cr = findnode(r->children, "Columns");
1547 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("The hub list at %s cannot be understood"), pubhubaddr);
1550 for(c = findnode(cr->children, "Column"); c != NULL; c = findnode(c->next, "Column"))
1552 name = (char *)xmlGetProp(c, (xmlChar *)"Name");
1553 stype = (char *)xmlGetProp(c, (xmlChar *)"Type");
1554 type = G_TYPE_INVALID;
1557 if(!strcmp(stype, "string"))
1558 type = G_TYPE_STRING;
1559 else if(!strcmp(stype, "int"))
1561 else if(!strcmp(stype, "bytes"))
1562 type = G_TYPE_INT64;
1564 if((name != NULL) && (type != G_TYPE_INVALID))
1566 names = srealloc(names, (numcols + 1) * sizeof(*names));
1567 types = srealloc(types, (numcols + 1) * sizeof(*names));
1568 names[numcols] = sstrdup(name);
1569 types[numcols] = type;
1579 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("The hub list at %s did not contain any columns"), pubhubaddr);
1582 for(i = 0; i < numcols; i++)
1584 if(!strcmp(names[i], "Address"))
1587 names[0] = names[i];
1590 types[0] = types[i];
1597 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("The hub list at %s did not contain the address to any hubs"), pubhubaddr);
1600 model = gtk_list_store_newv(numcols, types);
1601 for(n = findnode(r->children, "Hub"); n != NULL; n = findnode(n->next, "Hub"))
1603 if(!xmlHasProp(n, (xmlChar *)"Address") || !xmlHasProp(n, (xmlChar *)"Name"))
1608 attr = (char *)xmlGetProp(n, (xmlChar *)"Name");
1609 if(!regexec(&pubhubfilter, attr, 0, NULL, 0))
1612 if((attr = (char *)xmlGetProp(n, (xmlChar *)"Description")) != NULL)
1614 if(!regexec(&pubhubfilter, attr, 0, NULL, 0))
1621 gtk_list_store_append(model, &iter);
1622 for(i = 0; i < numcols; i++)
1624 attr = (char *)xmlGetProp(n, (xmlChar *)names[i]);
1627 if(types[i] == G_TYPE_STRING)
1628 gtk_list_store_set(model, &iter, i, attr, -1);
1629 else if(types[i] == G_TYPE_INT)
1630 gtk_list_store_set(model, &iter, i, atoi(attr), -1);
1631 else if(types[i] == G_TYPE_INT64)
1632 gtk_list_store_set(model, &iter, i, strtoll(attr, NULL, 0), -1);
1637 cols = smalloc((numcols - 1) * sizeof(*cols));
1638 for(i = 1; i < numcols; i++)
1641 for(i = 0; i < numcols; i++)
1643 if(!strcmp(names[i], "Users"))
1646 setpubhubmodel(GTK_TREE_MODEL(model), sortcol, numcols - 1, cols, names + 1);
1648 g_object_unref(model);
1653 if(ctxt->myDoc != NULL)
1654 xmlFreeDoc(ctxt->myDoc);
1655 xmlFreeParserCtxt(ctxt);
1662 for(i = 0; i < numcols; i++)
1670 int pubhuboldhandler(int op, char *buf, size_t *len)
1672 static GtkListStore *model = NULL;
1676 char *fields[4], *names[3];
1683 model = gtk_list_store_new(4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT);
1686 while((p = memchr(buf, '\n', *len)) != NULL)
1689 for(i = 0, p2 = buf; i < 4; i++) {
1691 if((p2 = strchr(p2, '|')) == NULL)
1696 for(i = 0; i < 4; i++) {
1697 if((wbuf = icsmbstowcs(fields[i], DCCHARSET, NULL)) == NULL) {
1698 fields[i] = sstrdup(_("(Invalid character)"));
1700 if((fields[i] = icwcstombs(wbuf, "UTF-8")) == NULL)
1705 if(!filterpubhub || !regexec(&pubhubfilter, fields[0], 0, NULL, 0) || !regexec(&pubhubfilter, fields[2], 0, NULL, 0)) {
1706 gtk_list_store_append(model, &iter);
1707 gtk_list_store_set(model, &iter, 0, fields[1], 1, fields[0], 2, fields[2], 3, atoi(fields[3]), -1);
1710 for(i--; i >= 0; i--)
1713 memmove(buf, p, *len -= p - buf);
1717 cols[0] = 3; names[0] = _("# users");
1718 cols[1] = 1; names[1] = _("Name");
1719 cols[2] = 2; names[2] = _("Description");
1720 setpubhubmodel(GTK_TREE_MODEL(model), 3, 3, cols, names);
1724 g_object_unref(model);
1731 void pubhubfdcallback(gpointer data, gint source, GdkInputCondition condition)
1733 static char buf[65536];
1734 static size_t bufpos = 0;
1737 if(!(condition & GDK_INPUT_READ))
1739 if(bufpos == sizeof(buf))
1741 ret = read(pubhubfd, buf + bufpos, sizeof(buf) - bufpos);
1746 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not read from public hub listing process: %s"), strerror(errno));
1748 pubhubhandler(PHO_EOF, NULL, NULL);
1752 if(pubhubhandler(PHO_DATA, buf, &bufpos))
1757 pubhubhandler(PHO_FINI, NULL, NULL);
1758 pubhubhandler = NULL;
1759 gdk_input_remove(pubhubtag);
1761 kill(pubhubproc, SIGINT);
1762 pubhubfd = pubhubtag = -1;
1767 regfree(&pubhubfilter);
1773 void cb_main_pubhubfilter_activate(GtkWidget *widget, gpointer data)
1775 int pipe1[2], pipe2[2];
1777 const char *buf, *p;
1781 gdk_input_remove(pubhubtag);
1785 kill(pubhubproc, SIGINT);
1786 if(pubhubhandler != NULL)
1788 pubhubhandler(PHO_FINI, NULL, NULL);
1789 pubhubhandler = NULL;
1793 regfree(&pubhubfilter);
1796 buf = gtk_entry_get_text(GTK_ENTRY(main_pubhubfilter));
1799 if((err = regcomp(&pubhubfilter, buf, REG_EXTENDED | REG_ICASE | REG_NOSUB)) != 0)
1801 regerror(err, &pubhubfilter, errbuf, sizeof(errbuf));
1802 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "Could not compile regex: %s", errbuf);
1803 regfree(&pubhubfilter);
1810 if((pubhubproc = fork()) == 0)
1815 execlp("wget", "wget", "-qO", "-", pubhubaddr, NULL);
1820 pubhubfd = pipe1[0];
1821 len = strlen(pubhubaddr);
1822 p = pubhubaddr + len;
1823 if((len > 4) && !strncmp(p - 4, ".bz2", 4))
1835 execlp("bzcat", "bzcat", NULL);
1841 pubhubfd = pipe2[0];
1843 if((len > 4) && !strncmp(p - 4, ".xml", 4))
1847 pubhubhandler = pubhubxmlhandler;
1849 pubhubhandler = pubhuboldhandler;
1851 pubhubhandler(PHO_INIT, NULL, NULL);
1852 pubhubtag = gdk_input_add(pubhubfd, GDK_INPUT_READ, pubhubfdcallback, NULL);
1855 void cb_main_dcnctbtn_clicked(GtkWidget *widget, gpointer data)
1859 struct dc_response *resp;
1863 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1866 if(!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(main_fnetnodes)), NULL, &iter))
1868 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("No hub selected"));
1871 gtk_tree_model_get(GTK_TREE_MODEL(fnmodel), &iter, 0, &id, -1);
1872 tag = dc_queuecmd(NULL, NULL, L"dcnct", L"%i", id, NULL);
1873 if((resp = dc_gettaggedrespsync(tag)) != NULL)
1875 if(resp->code == 502)
1876 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1882 void cb_main_phublist_cchange(GtkWidget *widget, gpointer data)
1885 GtkTreeModel *model;
1888 if(!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(main_phublist)), &model, &iter))
1890 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, 0, &addr, -1);
1891 gtk_entry_set_text(GTK_ENTRY(main_fnaddr), addr);
1895 void cb_main_phublist_activate(GtkWidget *widget, GtkTreePath *path, GtkTreeViewColumn *col, gpointer data)
1898 struct dc_response *resp;
1900 GtkTreeModel *model;
1905 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1908 model = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
1909 if(!gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter, path))
1911 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, 0, &buf, -1);
1912 if(strchr(buf, ':') == NULL)
1914 buf = g_realloc(buf, strlen(buf) + 5);
1915 strcat(buf, ":411");
1917 tag = dc_queuecmd(NULL, NULL, L"cnct", L"dc", L"%s", buf, NULL);
1919 gtk_entry_set_text(GTK_ENTRY(main_fnaddr), "");
1920 if((resp = dc_gettaggedrespsync(tag)) != NULL)
1922 if(resp->code == 502)
1923 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1924 if(resp->code == 509)
1925 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("The server could not parse that address"));
1926 if(resp->code == 515)
1927 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("There are too many hubs connected"));
1933 void cb_main_chatnodes_activate(GtkWidget *widget, GtkTreePath *path, GtkTreeViewColumn *col, gpointer uudata)
1937 struct dc_fnetnode *fn;
1938 struct fndata *data;
1940 if(!gtk_tree_model_get_iter(GTK_TREE_MODEL(fnmodel), &iter, path))
1942 gtk_tree_model_get(GTK_TREE_MODEL(fnmodel), &iter, 0, &id, -1);
1943 if((fn = dc_findfnetnode(id)) == NULL)
1947 if(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(fnmodel), &iter))
1951 gtk_tree_model_get(GTK_TREE_MODEL(fnmodel), &iter, 0, &id, -1);
1953 gtk_list_store_set(fnmodel, &iter, 5, "gtk-apply", -1);
1955 gtk_list_store_set(fnmodel, &iter, 5, NULL, -1);
1956 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(fnmodel), &iter));
1958 gtk_text_view_set_buffer(GTK_TEXT_VIEW(main_chatview), GTK_TEXT_BUFFER(data->textbuf));
1961 void cb_main_chatstr_activate(GtkWidget *widget, gpointer data)
1965 struct dc_response *resp;
1969 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1974 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("No hub selected"));
1977 buf = gtk_entry_get_text(GTK_ENTRY(main_chatstr));
1978 tag = dc_queuecmd(NULL, NULL, L"sendchat", L"%i", curchat, L"1", L"", L"%s", buf, NULL);
1979 if((resp = dc_gettaggedrespsync(tag)) != NULL)
1981 if(resp->code == 502)
1982 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1983 else if(resp->code == 504)
1984 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("This hub could not support all the types of characters in your chat message"));
1985 else if(resp->code == 513)
1986 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("This hub does not support chatting"));
1987 else if(resp->code != 200)
1988 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("An error occurred while trying to chat (%i)"), resp->code);
1991 gtk_entry_set_text(GTK_ENTRY(main_chatstr), "");
1995 void updatesrchfld(const char *simple)
1999 size_t bufsize, bufdata;
2001 s = sstrdup(simple);
2003 bufsize = bufdata = 0;
2007 p2 = strchr(p, ' ');
2013 bufcat(buf, " & ", 3);
2014 bufcat(buf, "N~", 2);
2017 if(strchr("[]()$^.*?+\\|\"!", *p) != NULL)
2018 addtobuf(buf, '\\');
2023 } while(p2 != NULL);
2025 gtk_entry_set_text(GTK_ENTRY(main_realsrch), buf);
2030 void cb_main_simplesrch_changed(GtkWidget *widget, gpointer data)
2035 updatesrchfld(gtk_entry_get_text(GTK_ENTRY(main_simplesrch)));
2039 void cb_main_realsrch_changed(GtkWidget *widget, gpointer data)
2044 gtk_entry_set_text(GTK_ENTRY(main_simplesrch), "");
2048 void cb_main_srchbtn_clicked(GtkWidget *widget, gpointer data)
2052 struct dc_response *resp;
2053 struct dc_intresp *ires;
2057 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
2060 if(nextsrch != -1) /* Impossible case, but oh well... */
2062 toks = dc_lexsexpr(icsmbstowcs((char *)gtk_entry_get_text(GTK_ENTRY(main_realsrch)), "UTF-8", NULL));
2065 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Please enter a search expression before searching"));
2068 tag = dc_queuecmd(NULL, NULL, L"search", L"all", L"%a", toks, NULL);
2069 dc_freewcsarr(toks);
2070 if((resp = dc_gettaggedrespsync(tag)) != NULL)
2072 if(resp->code == 501)
2073 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("Could not find any hubs to search on"));
2074 else if(resp->code == 502)
2075 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
2076 else if(resp->code == 509)
2077 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("The server could not parse your search expression"));
2078 else if(resp->code != 200)
2079 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("An error occurred while trying to search (%i)"), resp->code);
2080 if(resp->code == 200)
2082 if((ires = dc_interpret(resp)) != NULL)
2084 nextsrch = ires->argv[0].val.num;
2085 srcheta = time(NULL) + ires->argv[1].val.num;
2088 gtk_widget_set_sensitive(main_realsrch, FALSE);
2089 gtk_widget_set_sensitive(main_simplesrch, FALSE);
2090 gtk_widget_set_sensitive(main_srchbtn, FALSE);
2091 gtk_widget_set_sensitive(main_srchcanbtn, TRUE);
2099 void cb_main_srchcanbtn_clicked(GtkWidget *widget, gpointer data)
2103 dc_queuecmd(NULL, NULL, L"cansrch", L"%i", nextsrch, NULL);
2105 gtk_widget_set_sensitive(main_realsrch, TRUE);
2106 gtk_widget_set_sensitive(main_simplesrch, TRUE);
2107 gtk_widget_set_sensitive(main_srchbtn, TRUE);
2108 gtk_widget_set_sensitive(main_srchcanbtn, FALSE);
2112 gboolean cb_main_trlist_keypress(GtkWidget *widget, GdkEventKey *event, gpointer data)
2115 GtkTreeSelection *sel;
2116 GtkTreeModel *model;
2118 struct dc_response *resp;
2120 if((event->type == GDK_KEY_PRESS) && (event->keyval == GDK_Delete))
2122 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
2123 if(gtk_tree_selection_get_selected(sel, &model, &iter))
2125 gtk_tree_model_get(model, &iter, 0, &id, -1);
2126 tag = dc_queuecmd(NULL, NULL, L"cancel", L"%i", id, NULL);
2127 if((resp = dc_gettaggedrespsync(tag)) != NULL)
2129 if(resp->code == 502)
2130 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
2131 else if(resp->code != 200)
2132 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("An error occurred while trying to cancel (%i)"), resp->code);
2142 void cb_main_srchres_activate(GtkWidget *widget, GtkTreePath *path, GtkTreeViewColumn *col, gpointer data)
2145 struct dc_response *resp;
2147 GtkTreeModel *model;
2149 char *tfnet, *tpeerid, *tfilename, *thash, *arg;
2150 wchar_t *fnet, *peerid, *filename, *hash;
2154 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
2157 model = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
2158 if(!gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter, path))
2160 gtk_tree_model_get(model, &iter, 7, &num, -1);
2163 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, 0, &tfnet, 1, &tpeerid, 3, &tfilename, 4, &size, 9, &thash, -1);
2164 fnet = icmbstowcs(tfnet, "UTF-8");
2165 peerid = icmbstowcs(tpeerid, "UTF-8");
2166 filename = icmbstowcs(tfilename, "UTF-8");
2167 hash = (thash == NULL)?NULL:icmbstowcs(thash, "UTF-8");
2168 if((fnet == NULL) || (peerid == NULL) || (filename == NULL))
2174 if(filename != NULL)
2188 arg = (char *)gtk_entry_get_text(GTK_ENTRY(main_dlarg));
2190 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);
2192 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);
2198 if((resp = dc_gettaggedrespsync(tag)) != NULL)
2200 if(resp->code == 502)
2201 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
2202 if(resp->code != 200)
2203 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("An error occurred while trying to queue the download (%i)"), resp->code);
2209 gboolean srchfilterfunc(GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
2214 filteratall = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(main_filternoslots));
2217 gtk_tree_model_get(model, iter, 5, &slots, -1);
2223 void cb_main_filternoslots_toggled(GtkToggleButton *widget, gpointer data)
2225 gtk_tree_model_filter_refilter(srchmodelfilter);
2228 void cb_main_srhash_activate(GtkWidget *widget, gpointer data)
2230 GtkTreeSelection *sel;
2231 GtkTreeModel *model;
2237 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(main_srchres));
2238 if(gtk_tree_selection_get_selected(sel, &model, &iter))
2240 gtk_tree_model_get(model, &iter, 9, &hash, -1);
2241 buf = sprintf2("H=%s", hash);
2242 gtk_entry_set_text(GTK_ENTRY(main_realsrch), buf);
2245 cb_main_srchbtn_clicked(widget, NULL);
2251 void cb_main_srcopy_activate(GtkWidget *widget, gpointer data)
2254 GtkTreeSelection *sel;
2255 GtkTreeModel *model;
2261 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(main_srchres));
2262 if(!gtk_tree_selection_get_selected(sel, &model, &iter))
2264 gtk_tree_model_get(model, &iter, 9, &hash, -1);
2265 cb = gtk_clipboard_get(gdk_atom_intern("PRIMARY", FALSE));
2266 gtk_clipboard_set_text(cb, hash, -1);
2270 void cb_main_trhash_activate(GtkWidget *widget, gpointer data)
2272 GtkTreeSelection *sel;
2273 GtkTreeModel *model;
2279 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(main_downloads));
2280 if(gtk_tree_selection_get_selected(sel, &model, &iter))
2282 gtk_tree_model_get(model, &iter, 12, &hash, -1);
2283 buf = sprintf2("H=%s", hash);
2284 gtk_entry_set_text(GTK_ENTRY(main_realsrch), buf);
2287 cb_main_srchbtn_clicked(widget, NULL);
2293 void cb_main_trcopy_activate(GtkWidget *widget, gpointer data)
2296 GtkTreeSelection *sel;
2297 GtkTreeModel *model;
2301 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(main_downloads));
2302 if(!gtk_tree_selection_get_selected(sel, &model, &iter))
2304 gtk_tree_model_get(model, &iter, 12, &hash, -1);
2305 cb = gtk_clipboard_get(gdk_atom_intern("PRIMARY", FALSE));
2306 gtk_clipboard_set_text(cb, hash, -1);
2310 void cb_main_trreset_activate(GtkWidget *widget, gpointer data)
2312 GtkTreeSelection *sel;
2313 GtkTreeModel *model;
2316 struct dc_response *resp;
2318 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(main_downloads));
2319 if(gtk_tree_selection_get_selected(sel, &model, &iter))
2321 gtk_tree_model_get(model, &iter, 0, &id, -1);
2322 tag = dc_queuecmd(NULL, NULL, L"reset", L"%i", id, NULL);
2323 if((resp = dc_gettaggedrespsync(tag)) != NULL)
2325 if(resp->code == 502)
2326 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
2327 else if(resp->code != 200)
2328 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("An error occurred while trying to reset (%i)"), resp->code);
2337 void cb_main_trcancel_activate(GtkWidget *widget, gpointer data)
2339 GtkTreeSelection *sel;
2340 GtkTreeModel *model;
2343 struct dc_response *resp;
2345 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(main_downloads));
2346 if(gtk_tree_selection_get_selected(sel, &model, &iter))
2348 gtk_tree_model_get(model, &iter, 0, &id, -1);
2349 tag = dc_queuecmd(NULL, NULL, L"cancel", L"%i", id, NULL);
2350 if((resp = dc_gettaggedrespsync(tag)) != NULL)
2352 if(resp->code == 502)
2353 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
2354 else if(resp->code != 200)
2355 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("An error occurred while trying to cancel (%i)"), resp->code);
2364 /* XXX: This is quite a hack, since the calling convention is
2365 * different for the popup-menu sig and the button-press-event sig. It
2366 * most certainly works, but I don't know how portable it is. */
2367 gboolean cb_main_srpopup(GtkWidget *widget, GdkEventButton *event, gpointer data)
2369 GtkTreeSelection *sel;
2370 GtkTreeModel *model;
2374 if((event != NULL) && (event->button != 3))
2376 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
2377 if(gtk_tree_selection_get_selected(sel, &model, &iter))
2379 gtk_tree_model_get(model, &iter, 9, &hash, -1);
2380 if((hash == NULL) || (*hash == 0))
2382 gtk_widget_set_sensitive(main_srhash, FALSE);
2383 gtk_widget_set_sensitive(main_srcopy, FALSE);
2386 gtk_widget_set_sensitive(main_srhash, TRUE);
2388 gtk_widget_set_sensitive(main_srhash, FALSE);
2389 gtk_widget_set_sensitive(main_srcopy, TRUE);
2396 gtk_menu_popup(GTK_MENU(main_srpopup), NULL, NULL, NULL, NULL, 0, gtk_get_current_event_time());
2398 gtk_menu_popup(GTK_MENU(main_srpopup), NULL, NULL, NULL, NULL, event->button, event->time);
2402 /* The above hack note goes for this one too. */
2403 gboolean cb_main_trpopup(GtkWidget *widget, GdkEventButton *event, gpointer data)
2405 GtkTreeSelection *sel;
2406 GtkTreeModel *model;
2410 if((event != NULL) && (event->button != 3))
2412 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
2413 if(gtk_tree_selection_get_selected(sel, &model, &iter))
2415 gtk_tree_model_get(model, &iter, 12, &hash, -1);
2416 if((hash == NULL) || (*hash == 0))
2418 gtk_widget_set_sensitive(main_trhash, FALSE);
2419 gtk_widget_set_sensitive(main_trcopy, FALSE);
2422 gtk_widget_set_sensitive(main_trhash, TRUE);
2424 gtk_widget_set_sensitive(main_trhash, FALSE);
2425 gtk_widget_set_sensitive(main_trcopy, TRUE);
2432 gtk_menu_popup(GTK_MENU(main_trpopup), NULL, NULL, NULL, NULL, 0, gtk_get_current_event_time());
2434 gtk_menu_popup(GTK_MENU(main_trpopup), NULL, NULL, NULL, NULL, event->button, event->time);
2438 void cb_reslist_reload_clicked(GtkWidget *widget, gpointer data)
2442 gtk_widget_set_sensitive(reslist_delete, FALSE);
2443 gtk_widget_set_sensitive(reslist_search, FALSE);
2444 gtk_list_store_clear(reslist);
2445 lsrestag = dc_queuecmd(NULL, NULL, L"filtercmd", L"lsres", NULL);
2446 gtk_widget_set_sensitive(reslist_reload, FALSE);
2452 struct dc_response *resp;
2455 tag = dc_queuecmd(NULL, NULL, L"filtercmd", L"rmres", L"%s", id, NULL);
2456 if((resp = dc_gettaggedrespsync(tag)) != NULL)
2458 if(resp->numlines > 0)
2460 if(!wcscmp(resp->rlines[0].argv[1], L"ok"))
2462 else if(!wcsncmp(resp->rlines[0].argv[1], L"err:", 4))
2463 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("An error occurred (%ls)"), resp->rlines[0].argv[1] + 4);
2471 void cb_reslist_delete_clicked(GtkWidget *widget, gpointer data)
2474 GtkTreeModel *model;
2480 if(!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(reslist_list)), &model, &iter))
2482 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, 0, &id, 4, &locked, -1);
2489 gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
2493 void cb_reslist_search_clicked(GtkWidget *widget, gpointer data)
2496 GtkTreeModel *model;
2501 if(!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(reslist_list)), &model, &iter))
2503 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, 5, &hash, -1);
2504 buf = sprintf2("H=%s", hash);
2505 gtk_entry_set_text(GTK_ENTRY(main_realsrch), buf);
2508 cb_main_srchbtn_clicked(widget, NULL);
2511 void cb_reslist_list_cchange(GtkWidget *widget, gpointer data)
2514 GtkTreeModel *model;
2518 if(!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(reslist_list)), &model, &iter))
2520 gtk_widget_set_sensitive(reslist_delete, FALSE);
2521 gtk_widget_set_sensitive(reslist_search, FALSE);
2524 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, 4, &locked, 5, &hash, -1);
2525 gtk_widget_set_sensitive(reslist_delete, !locked);
2526 gtk_widget_set_sensitive(reslist_search, hash && *hash);
2530 void cb_reslist_list_activate(GtkWidget *widget, GtkTreePath *path, GtkTreeViewColumn *col, gpointer data)
2533 GtkTreeModel *model;
2536 model = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
2537 if(!gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter, path))
2541 gtk_tree_model_get(model, &iter, 5, &hash, -1);
2542 buf = sprintf2("H=%s", hash);
2543 gtk_entry_set_text(GTK_ENTRY(main_realsrch), buf);
2546 cb_main_srchbtn_clicked(widget, NULL);
2549 gboolean cb_reslist_list_keypress(GtkWidget *widget, GdkEventKey *event, gpointer data)
2551 GtkTreeSelection *sel;
2552 GtkTreeModel *model;
2557 if((event->type == GDK_KEY_PRESS) && (event->keyval == GDK_Delete))
2559 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
2560 if(gtk_tree_selection_get_selected(sel, &model, &iter))
2562 gtk_tree_model_get(model, &iter, 0, &id, 4, &locked, -1);
2566 gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
2575 void srchstatupdate(void)
2581 snprintf(buf, 1024, _("Ready to search"));
2583 snprintf(buf, 1024, _("Search scheduled and will be submitted in %i seconds"), (int)(srcheta - time(NULL)));
2585 if(strcmp(gtk_label_get_text(GTK_LABEL(main_srchstatus)), buf))
2586 gtk_label_set_text(GTK_LABEL(main_srchstatus), buf);
2589 gint srchstatupdatecb(gpointer data)
2595 void initchattags(void)
2599 chattags = gtk_text_tag_table_new();
2600 tag = gtk_text_tag_new("sender");
2601 g_object_set(tag, "foreground", "blue", NULL);
2602 gtk_text_tag_table_add(chattags, tag);
2605 int main(int argc, char **argv)
2608 PangoFontDescription *monospacefont;
2609 GtkTreeModel *sortmodel;
2610 struct passwd *pwent;
2612 setlocale(LC_ALL, "");
2613 bindtextdomain(PACKAGE, LOCALEDIR);
2614 textdomain(PACKAGE);
2615 gtk_init(&argc, &argv);
2617 signal(SIGCHLD, SIG_IGN);
2618 pubhubaddr = sstrdup("http://www.hublist.org/PublicHubList.xml.bz2");
2619 dcserver = sstrdup("");
2620 if((pwent = getpwuid(getuid())) == NULL)
2622 fprintf(stderr, "could not get your passwd data");
2625 connectas = sstrdup(pwent->pw_name);
2626 wnd = create_main_wnd();
2627 create_reslist_wnd();
2628 gtk_window_resize(GTK_WINDOW(reslist_wnd), 600, 400);
2631 fnmodel = gtk_list_store_new(6, G_TYPE_INT, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING, G_TYPE_STRING);
2632 gtk_tree_view_set_model(GTK_TREE_VIEW(main_fnetnodes), GTK_TREE_MODEL(fnmodel));
2633 gtk_tree_view_set_model(GTK_TREE_VIEW(main_chatnodes), GTK_TREE_MODEL(fnmodel));
2635 reslist = gtk_list_store_new(6, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_STRING);
2636 gtk_tree_view_set_model(GTK_TREE_VIEW(reslist_list), GTK_TREE_MODEL(reslist));
2638 dlmodel = gtk_list_store_new(13, G_TYPE_INT, /* id */
2639 G_TYPE_INT, /* dir */
2640 G_TYPE_INT, /* state */
2641 G_TYPE_STRING, /* peerid */
2642 G_TYPE_STRING, /* peernick */
2643 G_TYPE_STRING, /* path */
2644 G_TYPE_INT, /* size */
2645 G_TYPE_INT, /* curpos */
2646 G_TYPE_STRING, /* stock */
2647 G_TYPE_FLOAT, /* percentage */
2648 G_TYPE_INT, /* error */
2649 G_TYPE_INT, /* errortime */
2650 G_TYPE_STRING); /* hash */
2651 gtk_tree_view_set_model(GTK_TREE_VIEW(main_downloads), GTK_TREE_MODEL(dlmodel));
2653 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);
2654 gtk_tree_view_set_model(GTK_TREE_VIEW(main_uploads), GTK_TREE_MODEL(ulmodel));
2656 srchmodel = gtk_tree_store_new(10, G_TYPE_STRING, /* fnetname */
2657 G_TYPE_STRING, /* peerid */
2658 G_TYPE_STRING, /* peername */
2659 G_TYPE_STRING, /* filename */
2660 G_TYPE_INT, /* size */
2661 G_TYPE_INT, /* slots */
2662 G_TYPE_DOUBLE, /* resptime */
2663 G_TYPE_INT, /* sizenum */
2664 G_TYPE_INT, /* speed */
2665 G_TYPE_STRING); /* hash */
2666 srchmodelfilter = GTK_TREE_MODEL_FILTER(gtk_tree_model_filter_new(GTK_TREE_MODEL(srchmodel), NULL));
2667 gtk_tree_model_filter_set_visible_func(srchmodelfilter, srchfilterfunc, NULL, NULL);
2668 sortmodel = gtk_tree_model_sort_new_with_model(GTK_TREE_MODEL(srchmodelfilter));
2669 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(sortmodel), 4, GTK_SORT_DESCENDING);
2670 gtk_tree_view_set_model(GTK_TREE_VIEW(main_srchres), GTK_TREE_MODEL(sortmodel));
2671 g_object_unref(sortmodel);
2673 monospacefont = pango_font_description_from_string("Monospace 10");
2674 gtk_widget_modify_font(main_chatview, monospacefont);
2675 pango_font_description_free(monospacefont);
2677 updatesbar(_("Disconnected"));
2678 gtk_widget_show(wnd);
2680 dcconnect(dcserver);
2681 g_timeout_add(500, srchstatupdatecb, NULL);
2682 g_timeout_add(5000, ksupdatecb, NULL);
2683 g_timeout_add(1000, updatetransfers, NULL);