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
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>
44 #include "progressbar.h"
49 GtkTextBuffer *textbuf;
58 GtkTreeRowReference *ref;
69 GtkListStore *fnmodel, *ulmodel, *dlmodel, *pubhubmodel;
70 GtkTreeStore *srchmodel;
71 GtkTreeModelFilter *srchmodelfilter;
72 GtkTextTagTable *chattags;
73 int dcfd = -1, gdkread = -1, gdkwrite = -1;
74 int pubhubfd = -1, pubhubtag = -1, filterpubhub = 0;
78 char *pubhubaddr = NULL;
79 char *connectas = NULL;
80 char *dcserver = NULL;
82 int srchautoupdate = 0;
83 int cursrch = -1, nextsrch = -1;
85 struct srchsize *srchsizes = NULL;
86 struct knownspeed *knownspeeds = NULL;
87 int numsizes = 0, numspeeds = 0, ksqueryseq = -1, ksquerytag = -1;
89 gboolean initdeath(GtkWidget *, gpointer);
90 void cb_main_connmenu_activate(GtkWidget *widget, gpointer data);
91 void cb_main_dconnmenu_activate(GtkWidget *widget, gpointer data);
92 void cb_main_prefmenu_activate(GtkWidget *widget, gpointer data);
93 void cb_main_sdmenu_activate(GtkWidget *widget, gpointer data);
94 void cb_inpdialog_entry_activate(GtkWidget *widget, gpointer data);
95 void cb_main_fnaddr_activate(GtkWidget *widget, gpointer data);
96 void cb_main_pubhubfilter_activate(GtkWidget *widget, gpointer data);
97 void cb_main_dcnctbtn_clicked(GtkWidget *widget, gpointer data);
98 void cb_main_phublist_cchange(GtkWidget *widget, gpointer data);
99 void cb_main_phublist_activate(GtkWidget *widget, GtkTreePath *path, GtkTreeViewColumn *col, gpointer data);
100 void cb_main_chatnodes_activate(GtkWidget *widget, GtkTreePath *path, GtkTreeViewColumn *col, gpointer data);
101 void cb_main_srchres_activate(GtkWidget *widget, GtkTreePath *path, GtkTreeViewColumn *col, gpointer data);
102 void cb_main_chatstr_activate(GtkWidget *widget, gpointer data);
103 void cb_main_simplesrch_changed(GtkWidget *widget, gpointer data);
104 void cb_main_realsrch_changed(GtkWidget *widget, gpointer data);
105 void cb_main_srchbtn_clicked(GtkWidget *widget, gpointer data);
106 void cb_main_srchcanbtn_clicked(GtkWidget *widget, gpointer data);
107 void cb_main_trlist_keypress(GtkWidget *widget, GdkEventKey *event, gpointer data);
108 void cb_main_filternoslots_toggled(GtkToggleButton *widget, gpointer data);
109 void dcfdcallback(gpointer data, gint source, GdkInputCondition condition);
110 void srchstatupdate(void);
111 void transnicebytefunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data);
112 void transerrorinfo(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data);
113 void percentagefunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data);
114 void hidezerofunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data);
115 void speedtimefunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data);
117 #define DCCHARSET "windows-1252"
119 #define _(text) gettext(text)
121 #include "mainwnd.gtk"
122 #include "inpdialog.gtk"
125 void updatewrite(void)
132 gdkwrite = gdk_input_add(dcfd, GDK_INPUT_WRITE, dcfdcallback, NULL);
136 gdk_input_remove(gdkwrite);
142 void fndestroycb(struct dc_fnetnode *fn)
145 GtkTextBuffer *textbuf;
148 g_object_unref(data->textbuf);
150 if(curchat == fn->id)
152 textbuf = gtk_text_buffer_new(chattags);
153 gtk_text_view_set_buffer(GTK_TEXT_VIEW(main_chatview), textbuf);
154 g_object_unref(textbuf);
158 void addfndata(struct dc_fnetnode *fn)
162 if(fn->udata != NULL)
164 fn->destroycb = fndestroycb;
165 data = smalloc(sizeof(*data));
166 data->textbuf = gtk_text_buffer_new(chattags);
170 char *getfnstatestock(int state)
172 if(state == DC_FNN_STATE_SYN)
173 return("gtk-jump-to");
174 if(state == DC_FNN_STATE_HS)
175 return("gtk-execute");
176 if(state == DC_FNN_STATE_EST)
178 if(state == DC_FNN_STATE_DEAD)
179 return("gtk-cancel");
183 void updatehublist(void)
186 struct dc_fnetnode *fn;
193 for(fn = dc_fnetnodes; fn != NULL; fn = fn->next)
199 if(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(fnmodel), &iter))
203 gtk_tree_model_get(GTK_TREE_MODEL(fnmodel), &iter, 0, &id, -1);
204 if((fn = dc_findfnetnode(id)) == NULL)
206 /* I can't seem to get a sensible reply fromp
207 * gtk_list_store, so I'm just doing this
209 gtk_list_store_remove(fnmodel, &iter);
213 gtk_tree_model_get(GTK_TREE_MODEL(fnmodel), &iter, 1, &name, 2, &state, 3, &numusers, -1);
217 buf = icswcstombs(fn->name, "UTF-8", NULL);
218 if(strcmp(buf, name))
219 gtk_list_store_set(fnmodel, &iter, 1, buf, -1);
220 if(state != fn->state)
222 gtk_list_store_set(fnmodel, &iter, 2, fn->state, -1);
223 gtk_list_store_set(fnmodel, &iter, 4, getfnstatestock(fn->state), -1);
225 if(numusers != fn->numusers)
226 gtk_list_store_set(fnmodel, &iter, 3, fn->numusers, -1);
230 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(fnmodel), &iter));
233 for(fn = dc_fnetnodes; fn != NULL; fn = fn->next)
240 buf = icswcstombs(fn->name, "UTF-8", NULL);
241 gtk_list_store_append(fnmodel, &iter);
242 gtk_list_store_set(fnmodel, &iter, 0, fn->id, 1, buf, 2, fn->state, 3, fn->numusers, 4, getfnstatestock(fn->state), -1);
248 void percentagefunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
255 gtk_tree_model_get(model, iter, colnum, &val, -1);
256 snprintf(buf, 64, "%.2f%%", (double)(val * 100.0));
257 g_object_set(rend, "text", buf, NULL);
260 void transnicebytefunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
266 gtk_tree_model_get(model, iter, colnum, &val, -1);
268 snprintf(buf, 64, "%'i", val);
270 strcpy(buf, _("Unknown"));
271 g_object_set(rend, "text", buf, NULL);
274 void hidezerofunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
280 gtk_tree_model_get(model, iter, colnum, &val, -1);
282 snprintf(buf, 64, "%i", val);
285 g_object_set(rend, "text", buf, NULL);
288 void speedtimefunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
290 int speed, size, time;
293 gtk_tree_model_get(model, iter, 4, &size, 8, &speed, -1);
296 time = (size / speed) / 60;
298 snprintf(buf, 64, "%'i (<00:01)", speed);
300 snprintf(buf, 64, "%'i (%02i:%02i)", speed, time / 60, time % 60);
301 } else if(speed == 0) {
304 strcpy(buf, _("Unknown"));
306 g_object_set(rend, "text", buf, NULL);
309 void transerrorinfo(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
313 char finbuf[64], tbuf[64], *errstr;
315 gtk_tree_model_get(model, iter, 10, &error, 11, &errortime, -1);
316 if(error != DC_TRNSE_NOERROR)
318 if(error == DC_TRNSE_NOTFOUND)
319 errstr = _("Not found");
320 else if(error == DC_TRNSE_NOSLOTS)
321 errstr = _("No slots");
322 strftime(tbuf, 64, _("%H:%M:%S"), localtime(&errortime));
323 snprintf(finbuf, 64, _("%s (reported at %s)"), errstr, tbuf);
327 g_object_set(rend, "text", finbuf, NULL);
330 char *gettrstatestock(int state)
332 if(state == DC_TRNS_WAITING)
333 return("gtk-jump-to");
334 if(state == DC_TRNS_HS)
335 return("gtk-execute");
336 if(state == DC_TRNS_MAIN)
337 return("gtk-network");
338 if(state == DC_TRNS_DONE)
343 void updatetransferlists(void)
347 struct dc_transfer *transfer;
351 char *peerid, *peernick, *path;
352 int state, dir, size, curpos, error;
354 GtkListStore *stores[3];
356 for(transfer = dc_transfers; transfer != NULL; transfer = transfer->next)
358 stores[DC_TRNSD_UNKNOWN] = NULL;
359 stores[DC_TRNSD_UP] = ulmodel;
360 stores[DC_TRNSD_DOWN] = dlmodel;
361 for(i = 0; i < 3; i++)
363 if(stores[i] == NULL)
369 if(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(stores[i]), &iter))
373 gtk_tree_model_get(GTK_TREE_MODEL(stores[i]), &iter, 0, &id, 1, &dir, -1);
374 if(((transfer = dc_findtransfer(id)) == NULL) || (transfer->dir != dir))
376 gtk_list_store_remove(stores[i], &iter);
381 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, -1);
382 if(state != transfer->state)
383 gtk_list_store_set(stores[i], &iter, 2, transfer->state, 8, gettrstatestock(transfer->state), -1);
384 if(size != transfer->size)
385 gtk_list_store_set(stores[i], &iter, 6, transfer->size, -1);
386 if(curpos != transfer->curpos)
387 gtk_list_store_set(stores[i], &iter, 7, transfer->curpos, -1);
388 if(error != transfer->error)
389 gtk_list_store_set(stores[i], &iter, 10, transfer->error, -1);
390 if(errortime != transfer->errortime)
391 gtk_list_store_set(stores[i], &iter, 11, transfer->errortime, -1);
392 if((transfer->size > 0) && (transfer->curpos > 0))
393 gtk_list_store_set(stores[i], &iter, 9, (float)transfer->curpos / (float)transfer->size, -1);
394 buf = icswcstombs(transfer->peerid, "UTF-8", NULL);
395 if(strcmp(buf, peerid))
396 gtk_list_store_set(stores[i], &iter, 3, buf, -1);
397 buf = icswcstombs(((transfer->peernick == NULL) || (transfer->peernick[0] == L'\0'))?transfer->peerid:transfer->peernick, "UTF-8", NULL);
398 if(strcmp(buf, peernick))
399 gtk_list_store_set(stores[i], &iter, 4, buf, -1);
400 buf = (transfer->path == NULL)?_("Unknown"):icswcstombs(transfer->path, "UTF-8", NULL);
401 if(strcmp(buf, path))
402 gtk_list_store_set(stores[i], &iter, 5, buf, -1);
407 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(stores[i]), &iter));
411 for(transfer = dc_transfers; transfer != NULL; transfer = transfer->next)
415 if(stores[transfer->dir] != NULL)
417 peerid = icwcstombs(transfer->peerid, "UTF-8");
418 peernick = icwcstombs(((transfer->peernick == NULL) || (transfer->peernick[0] == L'\0'))?transfer->peerid:transfer->peernick, "UTF-8");
419 path = (transfer->path == NULL)?_("Unknown"):icwcstombs(transfer->path, "UTF-8");
420 gtk_list_store_append(stores[transfer->dir], &iter);
421 gtk_list_store_set(stores[transfer->dir], &iter,
430 8, gettrstatestock(transfer->state),
433 11, transfer->errortime,
437 if(transfer->path != NULL)
444 void updatesbar(char *msg)
446 gtk_statusbar_pop(GTK_STATUSBAR(main_statusbar), 0);
447 gtk_statusbar_push(GTK_STATUSBAR(main_statusbar), 0, msg);
450 void freesrchsizes(void)
454 for(i = 0; i < numsizes; i++)
456 if(srchsizes[i].ref != NULL)
457 gtk_tree_row_reference_free(srchsizes[i].ref);
459 if(srchsizes != NULL)
465 void dcdisconnected(void)
469 gdk_input_remove(gdkread);
474 updatetransferlists();
475 cursrch = nextsrch = -1;
476 gtk_tree_store_clear(srchmodel);
478 gtk_widget_set_sensitive(main_connmenu, TRUE);
479 gtk_widget_set_sensitive(main_dconnmenu, FALSE);
480 gtk_widget_set_sensitive(main_simplesrch, TRUE);
481 gtk_widget_set_sensitive(main_realsrch, TRUE);
482 gtk_widget_set_sensitive(main_srchbtn, TRUE);
483 gtk_widget_set_sensitive(main_srchcanbtn, FALSE);
484 updatesbar(_("Disconnected"));
487 char *inputbox(char *title, char *prompt, char *def, int echo)
493 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);
494 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(inpdialog)->vbox), swnd = create_inpdialog_wnd(), TRUE, TRUE, 0);
495 gtk_widget_show(swnd);
497 gtk_entry_set_visibility(GTK_ENTRY(inpdialog_entry), FALSE);
498 gtk_label_set_text(GTK_LABEL(inpdialog_prompt), prompt);
499 gtk_entry_set_text(GTK_ENTRY(inpdialog_entry), def);
500 resp = gtk_dialog_run(GTK_DIALOG(inpdialog));
502 gtk_entry_set_visibility(GTK_ENTRY(inpdialog_entry), TRUE);
503 if(resp == GTK_RESPONSE_ACCEPT)
504 buf = strdup(gtk_entry_get_text(GTK_ENTRY(inpdialog_entry)));
507 gtk_widget_destroy(inpdialog);
512 int msgbox(int type, int buttons, char *format, ...)
519 va_start(args, format);
520 buf = vsprintf2(format, args);
522 swnd = gtk_message_dialog_new(GTK_WINDOW(main_wnd), GTK_DIALOG_MODAL, type, buttons, "%s", buf);
523 resp = gtk_dialog_run(GTK_DIALOG(swnd));
524 gtk_widget_destroy(swnd);
529 void readconfigfile(void)
532 char *homedir, *buf, *p;
535 if((homedir = getenv("HOME")) == NULL)
537 fprintf(stderr, "warning: could not find home directory!\n");
540 buf = sprintf2("%s/.dolconrc", homedir);
541 if((cfgfile = fopen(buf, "r")) == NULL)
550 while(fgets(buf, 1024, cfgfile) != NULL)
554 p = buf + strlen(buf);
557 if((p = strchr(buf, ':')) == NULL)
560 while((*p == ' ') || (*p == '\t'))
562 if(!strcmp(buf, "wnd-width"))
565 } else if(!strcmp(buf, "wnd-height")) {
567 } else if(!strcmp(buf, "pane1-pos")) {
568 gtk_paned_set_position(GTK_PANED(main_pane1), atoi(p));
569 } else if(!strcmp(buf, "pane2-pos")) {
570 gtk_paned_set_position(GTK_PANED(main_pane2), atoi(p));
571 } else if(!strcmp(buf, "pane3-pos")) {
572 gtk_paned_set_position(GTK_PANED(main_pane3), atoi(p));
573 } else if(!strcmp(buf, "pubhubaddr")) {
575 pubhubaddr = sstrdup(p);
576 } else if(!strcmp(buf, "dcserver")) {
578 dcserver = sstrdup(p);
579 } else if(!strcmp(buf, "advexpanded")) {
580 gtk_expander_set_expanded(GTK_EXPANDER(main_advexp), atoi(p));
581 } else if(!strcmp(buf, "connectas")) {
583 connectas = sstrdup(p);
584 } else if(!strcmp(buf, "autoconn")) {
586 } else if(!strcmp(buf, "filternoslots")) {
587 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(main_filternoslots), atoi(p));
596 gtk_window_resize(GTK_WINDOW(main_wnd), w, h);
599 void updateconfigfile(void)
605 if((homedir = getenv("HOME")) == NULL)
607 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not get your home directory!"));
610 buf = sprintf2("%s/.dolconrc", homedir);
611 if((cfgfile = fopen(buf, "w")) == NULL)
614 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not open configuration file for writing: %s"), strerror(errno));
618 gtk_window_get_size(GTK_WINDOW(main_wnd), &w, &h);
619 fprintf(cfgfile, "wnd-width: %i\n", w);
620 fprintf(cfgfile, "wnd-height: %i\n", h);
621 fprintf(cfgfile, "pane1-pos: %i\n", gtk_paned_get_position(GTK_PANED(main_pane1)));
622 fprintf(cfgfile, "pane2-pos: %i\n", gtk_paned_get_position(GTK_PANED(main_pane2)));
623 fprintf(cfgfile, "pane3-pos: %i\n", gtk_paned_get_position(GTK_PANED(main_pane3)));
624 fprintf(cfgfile, "pubhubaddr: %s\n", pubhubaddr);
625 fprintf(cfgfile, "dcserver: %s\n", dcserver);
626 fprintf(cfgfile, "advexpanded: %i\n", gtk_expander_get_expanded(GTK_EXPANDER(main_advexp)));
627 fprintf(cfgfile, "connectas: %s\n", connectas);
628 fprintf(cfgfile, "autoconn: %i\n", autoconn);
629 fprintf(cfgfile, "filternoslots: %i\n", gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(main_filternoslots)));
633 gboolean initdeath(GtkWidget *widget, gpointer data)
640 void cb_inpdialog_entry_activate(GtkWidget *widget, gpointer data)
642 gtk_dialog_response(GTK_DIALOG(inpdialog), GTK_RESPONSE_ACCEPT);
645 int loginconv(int type, wchar_t *prompt, char **resp, void *data)
651 buf = icwcstombs(prompt, "UTF-8");
654 case DC_LOGIN_CONV_NOECHO:
655 if((*resp = inputbox(_("Login"), buf, "", 0)) == NULL)
658 case DC_LOGIN_CONV_ECHO:
659 if((*resp = inputbox(_("Login"), buf, "", 1)) == NULL)
662 case DC_LOGIN_CONV_INFO:
663 msgbox(GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "%s", buf);
665 case DC_LOGIN_CONV_ERROR:
666 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", buf);
674 void getfnlistcallback(int resp, void *data)
679 void gettrlistcallback(int resp, void *data)
681 updatetransferlists();
684 void logincallback(int err, wchar_t *reason, void *data)
688 case DC_LOGIN_ERR_SUCCESS:
689 dc_queuecmd(NULL, NULL, L"notify", L"all", L"on", NULL);
690 dc_getfnlistasync(getfnlistcallback, NULL);
691 dc_gettrlistasync(gettrlistcallback, NULL);
692 updatesbar("Authenticated");
694 case DC_LOGIN_ERR_NOLOGIN:
695 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not negotiate an acceptable authentication mechanism"));
699 case DC_LOGIN_ERR_SERVER:
700 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("The server has encountered an error"));
704 case DC_LOGIN_ERR_USER:
705 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Internal client error"));
709 case DC_LOGIN_ERR_CONV:
713 case DC_LOGIN_ERR_AUTHFAIL:
714 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Login attempt failed!"));
722 GtkTreeIter *ref2iter(GtkTreeRowReference *ref)
724 static GtkTreeIter iter;
727 assert((path = gtk_tree_row_reference_get_path(ref)) != NULL);
728 assert(gtk_tree_model_get_iter(GTK_TREE_MODEL(srchmodel), &iter, path));
729 gtk_tree_path_free(path);
733 GtkTreeRowReference *iter2ref(GtkTreeIter *iter)
736 GtkTreeRowReference *ref;
738 assert((path = gtk_tree_model_get_path(GTK_TREE_MODEL(srchmodel), iter)) != NULL);
739 assert((ref = gtk_tree_row_reference_new(GTK_TREE_MODEL(srchmodel), path)) != NULL);
740 gtk_tree_path_free(path);
744 struct srchsize *finddiscsize(void)
749 for(i = 0; i < numsizes; i++)
751 if(srchsizes[i].size == -1)
752 return(&srchsizes[i]);
754 srchsizes = srealloc(srchsizes, sizeof(*srchsizes) * ++numsizes);
755 srchsizes[i].size = -1;
756 srchsizes[i].num = 1;
757 srchsizes[i].slots = 0;
758 srchsizes[i].resptime = 0.0;
759 gtk_tree_store_append(srchmodel, &iter, NULL);
760 gtk_tree_store_set(srchmodel, &iter, 3, _("Discrete sizes"), 7, 1, -1);
761 srchsizes[i].ref = iter2ref(&iter);
762 return(&srchsizes[i]);
765 struct knownspeed *findksentbyname(char *userid)
769 for(i = 0; i < numspeeds; i++)
771 if(!strcmp(knownspeeds[i].userid, userid))
772 return(&knownspeeds[i]);
777 struct knownspeed *findksentbyseq(int seq)
781 for(i = 0; i < numspeeds; i++)
783 if(knownspeeds[i].seq == seq)
784 return(&knownspeeds[i]);
789 gboolean ksupdaterow(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
791 struct knownspeed *ks;
794 gtk_tree_model_get(GTK_TREE_MODEL(model), iter, 1, &userid, -1);
797 ks = findksentbyname(userid);
800 knownspeeds = srealloc(knownspeeds, (numspeeds + 1) * sizeof(*knownspeeds));
801 ks = &knownspeeds[numspeeds];
803 ks->userid = sstrdup(userid);
810 gtk_tree_store_set(GTK_TREE_STORE(model), iter, 8, ks->speed, -1);
814 gint ksupdatecb(gpointer data)
818 wchar_t **users, *buf;
819 size_t userssize, usersdata;
825 for(i = 0; i < numspeeds;)
827 if(now - knownspeeds[i].fetched > 60)
829 free(knownspeeds[i].userid);
830 memmove(&knownspeeds[i], &knownspeeds[i + 1], (--numspeeds - i) * sizeof(*knownspeeds));
835 if(oldnum != numspeeds)
836 knownspeeds = srealloc(knownspeeds, numspeeds * sizeof(*knownspeeds));
837 gtk_tree_model_foreach(GTK_TREE_MODEL(srchmodel), ksupdaterow, NULL);
841 userssize = usersdata = 0;
843 for(i = 0; i < numspeeds; i++)
845 if(knownspeeds[i].seq == -2)
847 assert((buf = icmbstowcs(knownspeeds[i].userid, "UTF-8")) != NULL);
848 knownspeeds[i].seq = ksqueryseq++;
849 addtobuf(users, buf);
852 addtobuf(users, NULL);
853 ksquerytag = dc_queuecmd(NULL, NULL, L"filtercmd", L"userspeeda", L"%%a", users, NULL);
854 dc_freewcsarr(users);
859 void handleresps(void)
862 struct dc_response *resp;
863 struct dc_intresp *ires;
864 struct dc_fnetnode *fn;
865 struct fndata *fndata;
867 GtkTreeIter titer, piter;
871 struct knownspeed *ks;
873 while((resp = dc_getresp()) != NULL)
875 if(!wcscmp(resp->cmdname, L".connect"))
877 if(resp->code == 200)
879 tosbuf = 0x10; /* Minimum cost */
880 setsockopt(dcfd, SOL_IP, IP_TOS, &tosbuf, sizeof(tosbuf));
881 updatesbar(_("Connected"));
882 dc_loginasync(connectas, 1, loginconv, logincallback, NULL);
884 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("The server refused the connection"));
888 } else if(!wcscmp(resp->cmdname, L".notify")) {
889 dc_uimisc_handlenotify(resp);
893 if((ires = dc_interpret(resp)) != NULL)
895 if((fn = dc_findfnetnode(ires->argv[0].val.num)) != NULL)
898 gtk_text_buffer_get_end_iter(fndata->textbuf, &iter);
899 if((buf = icwcstombs(ires->argv[3].val.str, "UTF-8")) != NULL)
901 gtk_text_buffer_insert_with_tags_by_name(fndata->textbuf, &iter, "<", -1, "sender", NULL);
902 gtk_text_buffer_insert_with_tags_by_name(fndata->textbuf, &iter, buf, -1, "sender", NULL);
903 gtk_text_buffer_insert_with_tags_by_name(fndata->textbuf, &iter, ">", -1, "sender", NULL);
904 gtk_text_buffer_insert(fndata->textbuf, &iter, " ", -1);
907 if((buf = icwcstombs(ires->argv[4].val.str, "UTF-8")) != NULL)
909 gtk_text_buffer_insert(fndata->textbuf, &iter, buf, -1);
910 gtk_text_buffer_insert(fndata->textbuf, &iter, "\n", -1);
912 if(curchat == fn->id)
913 gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(main_chatview), &iter, 0, 0, 0, 0);
934 updatetransferlists();
937 if((ires = dc_interpret(resp)) != NULL)
939 if(ires->argv[0].val.num == nextsrch)
940 srcheta = time(NULL) + ires->argv[0].val.num;
945 if((ires = dc_interpret(resp)) != NULL)
947 if(ires->argv[0].val.num == nextsrch)
950 dc_queuecmd(NULL, NULL, L"cansrch", L"%%i", cursrch, NULL);
953 gtk_widget_set_sensitive(main_realsrch, TRUE);
954 gtk_widget_set_sensitive(main_simplesrch, TRUE);
955 gtk_widget_set_sensitive(main_srchbtn, TRUE);
956 gtk_widget_set_sensitive(main_srchcanbtn, FALSE);
958 gtk_entry_set_text(GTK_ENTRY(main_realsrch), "");
959 gtk_entry_set_text(GTK_ENTRY(main_simplesrch), "");
960 gtk_tree_store_clear(srchmodel);
967 if((ires = dc_interpret(resp)) != NULL)
969 if(ires->argv[0].val.num == cursrch)
971 for(i = 0; i < numsizes; i++)
973 if(srchsizes[i].size == ires->argv[4].val.num)
978 srchsizes = srealloc(srchsizes, sizeof(*srchsizes) * ++numsizes);
979 srchsizes[i].size = ires->argv[4].val.num;
980 srchsizes[i].num = 1;
981 srchsizes[i].slots = ires->argv[5].val.num;
982 srchsizes[i].resptime = ires->argv[7].val.flnum;
984 ss->slots += ires->argv[5].val.num;
985 if((ss->resptime == 0.0) || (ss->resptime > ires->argv[7].val.flnum))
986 ss->resptime = ires->argv[7].val.flnum;
987 piter = *ref2iter(ss->ref);
988 gtk_tree_store_set(srchmodel, &piter, 5, ss->slots, 6, ss->resptime, -1);
989 gtk_tree_store_append(srchmodel, &titer, &piter);
990 srchsizes[i].ref = iter2ref(&titer);
991 } else if(srchsizes[i].num == 1) {
992 char *filename, *peername, *fnetname;
996 gtk_tree_model_get(GTK_TREE_MODEL(srchmodel), ref2iter(srchsizes[i].ref), 0, &fnetname, 1, &peername, 3, &filename, 5, &slots, 6, &resptime, 8, &speed, -1);
997 gtk_tree_store_remove(srchmodel, ref2iter(srchsizes[i].ref));
998 gtk_tree_row_reference_free(srchsizes[i].ref);
1001 gtk_tree_store_set(srchmodel, ref2iter(ss->ref), 5, ss->slots, -1);
1002 gtk_tree_store_append(srchmodel, &piter, NULL);
1003 srchsizes[i].slots = ires->argv[5].val.num + slots;
1004 srchsizes[i].resptime = (ires->argv[7].val.flnum < resptime)?ires->argv[7].val.flnum:resptime;
1005 srchsizes[i].num = 2;
1006 srchsizes[i].ref = iter2ref(&piter);
1007 gtk_tree_store_set(srchmodel, &piter, 4, srchsizes[i].size, 5, srchsizes[i].slots, 6, srchsizes[i].resptime, 7, 2, -1);
1008 if((buf = icwcstombs(ires->argv[1].val.str, "UTF-8")) != NULL)
1011 /* XXX: Too DC-specific! */
1012 if(strrchr(p, '\\') != NULL)
1013 p = strrchr(p, '\\') + 1;
1014 gtk_tree_store_set(srchmodel, &piter, 3, p, -1);
1017 gtk_tree_store_append(srchmodel, &titer, &piter);
1018 gtk_tree_store_set(srchmodel, &titer, 0, fnetname, 1, peername, 2, peername, 3, filename, 4, srchsizes[i].size, 5, slots, 6, resptime, 8, speed, -1);
1019 g_free(filename); g_free(peername); g_free(fnetname);
1020 gtk_tree_store_append(srchmodel, &titer, &piter);
1023 srchsizes[i].slots += ires->argv[5].val.num;
1024 if(ires->argv[7].val.flnum < srchsizes[i].resptime)
1025 srchsizes[i].resptime = ires->argv[7].val.flnum;
1026 piter = *ref2iter(srchsizes[i].ref);
1027 gtk_tree_store_set(srchmodel, &piter, 5, srchsizes[i].slots, 6, srchsizes[i].resptime, 7, srchsizes[i].num, -1);
1028 gtk_tree_store_append(srchmodel, &titer, &piter);
1030 if((buf = icwcstombs(ires->argv[1].val.str, "UTF-8")) != NULL)
1032 gtk_tree_store_set(srchmodel, &titer, 3, buf, -1);
1035 if((buf = icwcstombs(ires->argv[2].val.str, "UTF-8")) != NULL)
1037 gtk_tree_store_set(srchmodel, &titer, 0, buf, -1);
1040 if((buf = icwcstombs(ires->argv[3].val.str, "UTF-8")) != NULL)
1042 gtk_tree_store_set(srchmodel, &titer, 1, buf, -1);
1043 gtk_tree_store_set(srchmodel, &titer, 2, buf, -1);
1046 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);
1054 } else if(!wcscmp(resp->cmdname, L"filtercmd")) {
1055 if((ksquerytag >= 0) && (ksquerytag == resp->tag))
1057 for(i = 0; i < resp->numlines; i++)
1059 assert((ks = findksentbyseq(i)) != NULL);
1060 ks->speed = wcstol(resp->rlines[i].argv[1], NULL, 10);
1062 ks->fetched = time(NULL);
1073 void dcfdcallback(gpointer data, gint source, GdkInputCondition condition)
1077 if(((condition & GDK_INPUT_READ) && dc_handleread()) || ((condition & GDK_INPUT_WRITE) && dc_handlewrite()))
1083 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("The server has closed the connection"));
1085 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("The connection to the server failed:\n\n%s"), strerror(errnobak));
1092 void cb_main_dconnmenu_activate(GtkWidget *widget, gpointer data)
1100 void cb_main_prefmenu_activate(GtkWidget *widget, gpointer data)
1102 GtkWidget *dialog, *swnd;
1105 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);
1106 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), swnd = create_pref_wnd(), TRUE, TRUE, 0);
1107 gtk_entry_set_text(GTK_ENTRY(pref_pubhuburl), pubhubaddr);
1108 gtk_entry_set_text(GTK_ENTRY(pref_connectas), connectas);
1109 gtk_entry_set_text(GTK_ENTRY(pref_dcserver), dcserver);
1110 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(pref_autoconn), autoconn);
1111 gtk_widget_show(swnd);
1112 resp = gtk_dialog_run(GTK_DIALOG(dialog));
1113 if(resp == GTK_RESPONSE_ACCEPT)
1116 pubhubaddr = sstrdup(gtk_entry_get_text(GTK_ENTRY(pref_pubhuburl)));
1118 connectas = sstrdup(gtk_entry_get_text(GTK_ENTRY(pref_connectas)));
1120 dcserver = sstrdup(gtk_entry_get_text(GTK_ENTRY(pref_dcserver)));
1121 autoconn = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(pref_autoconn));
1123 gtk_widget_destroy(dialog);
1126 void dcconnect(char *host)
1128 dcfd = dc_connect(host, -1);
1131 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect:\n\n%s"), strerror(errno));
1134 gdkread = gdk_input_add(dcfd, GDK_INPUT_READ, dcfdcallback, NULL);
1136 gtk_widget_set_sensitive(main_connmenu, FALSE);
1137 gtk_widget_set_sensitive(main_dconnmenu, TRUE);
1138 updatesbar(_("Connecting..."));
1141 void cb_main_connmenu_activate(GtkWidget *widget, gpointer data)
1147 if((buf = inputbox(_("Connect"), _("Server address:"), dcserver, 1)) == NULL)
1153 void cb_main_sdmenu_activate(GtkWidget *widget, gpointer data)
1156 struct dc_response *resp;
1160 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1163 tag = dc_queuecmd(NULL, NULL, L"shutdown", NULL);
1164 if((resp = dc_gettaggedrespsync(tag)) != NULL)
1166 if(resp->code == 502)
1167 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1173 void cb_main_fnaddr_activate(GtkWidget *widget, gpointer data)
1177 struct dc_response *resp;
1181 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1184 buf = sstrdup(gtk_entry_get_text(GTK_ENTRY(main_fnaddr)));
1185 if(strchr(buf, ':') == NULL)
1187 buf = srealloc(buf, strlen(buf) + 5);
1188 strcat(buf, ":411");
1190 tag = dc_queuecmd(NULL, NULL, L"cnct", L"dc", L"%%s", buf, NULL);
1192 if((resp = dc_gettaggedrespsync(tag)) != NULL)
1194 if(resp->code == 502)
1195 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1196 if(resp->code == 509)
1197 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("The server could not parse that address"));
1200 gtk_entry_set_text(GTK_ENTRY(main_fnaddr), "");
1204 void pubhubfdcallback(gpointer data, gint source, GdkInputCondition condition)
1206 static char buf[65536];
1207 static int bufpos = 0;
1213 int sorted, sortcol;
1214 GtkSortType sortorder;
1215 GtkTreeModel *sortmodel;
1217 if(!(condition & GDK_INPUT_READ))
1221 ret = read(pubhubfd, buf + bufpos, sizeof(buf) - bufpos);
1225 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not read from public hub listing process: %s"), strerror(errno));
1227 gdk_input_remove(pubhubtag);
1228 kill(pubhubproc, SIGINT);
1229 pubhubfd = pubhubtag = -1;
1234 regfree(&pubhubfilter);
1240 sortmodel = gtk_tree_view_get_model(GTK_TREE_VIEW(main_phublist));
1241 sorted = gtk_tree_sortable_get_sort_column_id(GTK_TREE_SORTABLE(sortmodel), &sortcol, &sortorder);
1242 gtk_tree_view_set_model(GTK_TREE_VIEW(main_phublist), NULL);
1243 while((p = memchr(buf, '\n', bufpos)) != NULL)
1246 if(!filterpubhub || !regexec(&pubhubfilter, buf, 0, NULL, 0))
1249 for(i = 0; i < 4; i++)
1252 if((p2 = strchr(p2, '|')) == NULL)
1258 for(i = 0; i < 4; i++)
1260 if((wbuf = icsmbstowcs(fields[i], DCCHARSET, NULL)) == NULL)
1262 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not decode hublist - aborting at this point: %s"), strerror(errno));
1263 kill(pubhubproc, SIGINT);
1266 if((fields[i] = icwcstombs(wbuf, "UTF-8")) == NULL)
1271 gtk_list_store_append(pubhubmodel, &iter);
1272 gtk_list_store_set(pubhubmodel, &iter, 0, fields[0], 1, fields[1], 2, fields[2], 3, atoi(fields[3]), -1);
1274 for(i--; i >= 0; i--)
1278 memmove(buf, p, bufpos -= p - buf);
1280 sortmodel = gtk_tree_model_sort_new_with_model(GTK_TREE_MODEL(pubhubmodel));
1282 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(sortmodel), sortcol, sortorder);
1283 gtk_tree_view_set_model(GTK_TREE_VIEW(main_phublist), sortmodel);
1286 void cb_main_pubhubfilter_activate(GtkWidget *widget, gpointer data)
1288 int pipe1[2], pipe2[2];
1294 gdk_input_remove(pubhubtag);
1298 kill(pubhubproc, SIGINT);
1301 regfree(&pubhubfilter);
1304 buf = gtk_entry_get_text(GTK_ENTRY(main_pubhubfilter));
1307 if((err = regcomp(&pubhubfilter, buf, REG_EXTENDED | REG_ICASE | REG_NOSUB)) != 0)
1309 regerror(err, &pubhubfilter, errbuf, sizeof(errbuf));
1310 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "Could not compile regex: %s", errbuf);
1311 regfree(&pubhubfilter);
1317 gtk_list_store_clear(pubhubmodel);
1319 if((pubhubproc = fork()) == 0)
1324 execlp("wget", "wget", "-qO", "-", pubhubaddr, NULL);
1329 pubhubfd = pipe1[0];
1330 len = strlen(pubhubaddr);
1331 if((len > 4) && !strcmp(pubhubaddr + len - 4, ".bz2"))
1341 execlp("bzcat", "bzcat", NULL);
1347 pubhubfd = pipe2[0];
1349 pubhubtag = gdk_input_add(pubhubfd, GDK_INPUT_READ, pubhubfdcallback, NULL);
1352 void cb_main_dcnctbtn_clicked(GtkWidget *widget, gpointer data)
1356 struct dc_response *resp;
1360 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1363 if(!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(main_fnetnodes)), NULL, &iter))
1365 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("No hub selected"));
1368 gtk_tree_model_get(GTK_TREE_MODEL(fnmodel), &iter, 0, &id, -1);
1369 tag = dc_queuecmd(NULL, NULL, L"dcnct", L"%%i", id, NULL);
1370 if((resp = dc_gettaggedrespsync(tag)) != NULL)
1372 if(resp->code == 502)
1373 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1379 void cb_main_phublist_cchange(GtkWidget *widget, gpointer data)
1382 GtkTreeModel *model;
1385 if(!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(main_phublist)), &model, &iter))
1387 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, 1, &addr, -1);
1388 gtk_entry_set_text(GTK_ENTRY(main_fnaddr), addr);
1392 void cb_main_phublist_activate(GtkWidget *widget, GtkTreePath *path, GtkTreeViewColumn *col, gpointer data)
1395 struct dc_response *resp;
1397 GtkTreeModel *model;
1402 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1405 model = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
1406 if(!gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter, path))
1408 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, 1, &buf, -1);
1409 if(strchr(buf, ':') == NULL)
1411 buf = g_realloc(buf, strlen(buf) + 5);
1412 strcat(buf, ":411");
1414 tag = dc_queuecmd(NULL, NULL, L"cnct", L"dc", L"%%s", buf, NULL);
1416 gtk_entry_set_text(GTK_ENTRY(main_fnaddr), "");
1417 if((resp = dc_gettaggedrespsync(tag)) != NULL)
1419 if(resp->code == 502)
1420 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1421 if(resp->code == 509)
1422 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("The server could not parse that address"));
1428 void cb_main_chatnodes_activate(GtkWidget *widget, GtkTreePath *path, GtkTreeViewColumn *col, gpointer uudata)
1432 struct dc_fnetnode *fn;
1433 struct fndata *data;
1435 if(!gtk_tree_model_get_iter(GTK_TREE_MODEL(fnmodel), &iter, path))
1437 gtk_tree_model_get(GTK_TREE_MODEL(fnmodel), &iter, 0, &id, -1);
1438 if((fn = dc_findfnetnode(id)) == NULL)
1442 if(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(fnmodel), &iter))
1446 gtk_tree_model_get(GTK_TREE_MODEL(fnmodel), &iter, 0, &id, -1);
1448 gtk_list_store_set(fnmodel, &iter, 5, "gtk-apply", -1);
1450 gtk_list_store_set(fnmodel, &iter, 5, NULL, -1);
1451 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(fnmodel), &iter));
1453 gtk_text_view_set_buffer(GTK_TEXT_VIEW(main_chatview), GTK_TEXT_BUFFER(data->textbuf));
1456 void cb_main_chatstr_activate(GtkWidget *widget, gpointer data)
1460 struct dc_response *resp;
1464 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1469 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("No hub selected"));
1472 buf = gtk_entry_get_text(GTK_ENTRY(main_chatstr));
1473 tag = dc_queuecmd(NULL, NULL, L"sendchat", L"%%i", curchat, L"1", L"", L"%%s", buf, NULL);
1474 if((resp = dc_gettaggedrespsync(tag)) != NULL)
1476 if(resp->code == 502)
1477 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1478 else if(resp->code == 504)
1479 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("This hub could not support all the types of characters in your chat message"));
1480 else if(resp->code == 513)
1481 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("This hub does not support chatting"));
1482 else if(resp->code != 200)
1483 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("An error occurred while trying to chat (%i)"), resp->code);
1486 gtk_entry_set_text(GTK_ENTRY(main_chatstr), "");
1490 void updatesrchfld(const char *simple)
1494 size_t bufsize, bufdata;
1496 s = sstrdup(simple);
1498 bufsize = bufdata = 0;
1502 p2 = strchr(p, ' ');
1508 bufcat(buf, " & ", 3);
1509 bufcat(buf, "N~", 2);
1512 if(strchr("[]()$^.*?+\\|\"", *p) != NULL)
1513 addtobuf(buf, '\\');
1518 } while(p2 != NULL);
1520 gtk_entry_set_text(GTK_ENTRY(main_realsrch), buf);
1525 void cb_main_simplesrch_changed(GtkWidget *widget, gpointer data)
1530 updatesrchfld(gtk_entry_get_text(GTK_ENTRY(main_simplesrch)));
1534 void cb_main_realsrch_changed(GtkWidget *widget, gpointer data)
1539 gtk_entry_set_text(GTK_ENTRY(main_simplesrch), "");
1543 void cb_main_srchbtn_clicked(GtkWidget *widget, gpointer data)
1547 struct dc_response *resp;
1548 struct dc_intresp *ires;
1552 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1555 if(nextsrch != -1) /* Impossible case, but oh well... */
1557 toks = dc_lexsexpr(icsmbstowcs((char *)gtk_entry_get_text(GTK_ENTRY(main_realsrch)), "UTF-8", NULL));
1560 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Please enter a search expression before searching"));
1563 tag = dc_queuecmd(NULL, NULL, L"search", L"all", L"%%a", toks, NULL);
1564 dc_freewcsarr(toks);
1565 if((resp = dc_gettaggedrespsync(tag)) != NULL)
1567 if(resp->code == 501)
1568 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("Could not find any hubs to search on"));
1569 else if(resp->code == 502)
1570 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1571 else if(resp->code == 509)
1572 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("The server could not parse your search expression"));
1573 else if(resp->code != 200)
1574 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("An error occurred while trying to search (%i)"), resp->code);
1575 if(resp->code == 200)
1577 if((ires = dc_interpret(resp)) != NULL)
1579 nextsrch = ires->argv[0].val.num;
1580 srcheta = time(NULL) + ires->argv[1].val.num;
1583 gtk_widget_set_sensitive(main_realsrch, FALSE);
1584 gtk_widget_set_sensitive(main_simplesrch, FALSE);
1585 gtk_widget_set_sensitive(main_srchbtn, FALSE);
1586 gtk_widget_set_sensitive(main_srchcanbtn, TRUE);
1594 void cb_main_srchcanbtn_clicked(GtkWidget *widget, gpointer data)
1598 dc_queuecmd(NULL, NULL, L"cansrch", L"%%i", nextsrch, NULL);
1600 gtk_widget_set_sensitive(main_realsrch, TRUE);
1601 gtk_widget_set_sensitive(main_simplesrch, TRUE);
1602 gtk_widget_set_sensitive(main_srchbtn, TRUE);
1603 gtk_widget_set_sensitive(main_srchcanbtn, FALSE);
1607 void cb_main_trlist_keypress(GtkWidget *widget, GdkEventKey *event, gpointer data)
1610 GtkTreeSelection *sel;
1611 GtkTreeModel *model;
1613 struct dc_response *resp;
1615 if((event->type == GDK_KEY_PRESS) && (event->keyval == GDK_Delete))
1617 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
1618 if(gtk_tree_selection_get_selected(sel, &model, &iter))
1620 gtk_tree_model_get(model, &iter, 0, &id, -1);
1621 tag = dc_queuecmd(NULL, NULL, L"cancel", L"%%i", id, NULL);
1622 if((resp = dc_gettaggedrespsync(tag)) != NULL)
1624 if(resp->code == 502)
1625 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1626 else if(resp->code != 200)
1627 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("An error occurred while trying to cancel (%i)"), resp->code);
1635 void cb_main_srchres_activate(GtkWidget *widget, GtkTreePath *path, GtkTreeViewColumn *col, gpointer data)
1638 struct dc_response *resp;
1640 GtkTreeModel *model;
1642 char *tfnet, *tpeerid, *tfilename, *arg;
1643 wchar_t *fnet, *peerid, *filename;
1647 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1650 model = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
1651 if(!gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter, path))
1653 gtk_tree_model_get(model, &iter, 7, &num, -1);
1656 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, 0, &tfnet, 1, &tpeerid, 3, &tfilename, 4, &size, -1);
1657 fnet = icmbstowcs(tfnet, "UTF-8");
1658 peerid = icmbstowcs(tpeerid, "UTF-8");
1659 filename = icmbstowcs(tfilename, "UTF-8");
1660 if((fnet == NULL) || (peerid == NULL) || (filename == NULL))
1666 if(filename != NULL)
1676 arg = (char *)gtk_entry_get_text(GTK_ENTRY(main_dlarg));
1678 tag = dc_queuecmd(NULL, NULL, L"download", fnet, L"%%ls", peerid, L"%%ls", filename, L"%%i", size, L"user", L"%%s", arg, NULL);
1680 tag = dc_queuecmd(NULL, NULL, L"download", fnet, L"%%ls", peerid, L"%%ls", filename, L"%%i", size, NULL);
1684 if((resp = dc_gettaggedrespsync(tag)) != NULL)
1686 if(resp->code == 502)
1687 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1688 if(resp->code != 200)
1689 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("An error occurred while trying to queue the download (%i)"), resp->code);
1695 gboolean srchfilterfunc(GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
1700 filteratall = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(main_filternoslots));
1703 gtk_tree_model_get(model, iter, 5, &slots, -1);
1709 void cb_main_filternoslots_toggled(GtkToggleButton *widget, gpointer data)
1711 gtk_tree_model_filter_refilter(srchmodelfilter);
1714 void srchstatupdate(void)
1720 snprintf(buf, 1024, _("Ready to search"));
1722 snprintf(buf, 1024, _("Search scheduled and will be submitted in %i seconds"), (int)(srcheta - time(NULL)));
1724 if(strcmp(gtk_label_get_text(GTK_LABEL(main_srchstatus)), buf))
1725 gtk_label_set_text(GTK_LABEL(main_srchstatus), buf);
1728 gint srchstatupdatecb(gpointer data)
1734 void initchattags(void)
1738 chattags = gtk_text_tag_table_new();
1739 tag = gtk_text_tag_new("sender");
1740 g_object_set(tag, "foreground", "blue", NULL);
1741 gtk_text_tag_table_add(chattags, tag);
1744 int main(int argc, char **argv)
1747 PangoFontDescription *monospacefont;
1748 GtkTreeModel *sortmodel;
1749 struct passwd *pwent;
1751 setlocale(LC_ALL, "");
1752 bindtextdomain(PACKAGE, LOCALEDIR);
1753 textdomain(PACKAGE);
1754 gtk_init(&argc, &argv);
1756 signal(SIGCHLD, SIG_IGN);
1757 pubhubaddr = sstrdup("http://www.neo-modus.com/PublicHubList.config");
1758 dcserver = sstrdup("localhost");
1759 if((pwent = getpwuid(getuid())) == NULL)
1761 fprintf(stderr, "could not get your passwd data");
1764 connectas = sstrdup(pwent->pw_name);
1765 wnd = create_main_wnd();
1768 fnmodel = gtk_list_store_new(6, G_TYPE_INT, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING, G_TYPE_STRING);
1769 gtk_tree_view_set_model(GTK_TREE_VIEW(main_fnetnodes), GTK_TREE_MODEL(fnmodel));
1770 gtk_tree_view_set_model(GTK_TREE_VIEW(main_chatnodes), GTK_TREE_MODEL(fnmodel));
1772 pubhubmodel = gtk_list_store_new(4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT);
1773 sortmodel = gtk_tree_model_sort_new_with_model(GTK_TREE_MODEL(pubhubmodel));
1774 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(sortmodel), 3, GTK_SORT_DESCENDING);
1775 gtk_tree_view_set_model(GTK_TREE_VIEW(main_phublist), GTK_TREE_MODEL(sortmodel));
1776 g_object_unref(sortmodel);
1778 dlmodel = gtk_list_store_new(12, G_TYPE_INT, /* id */
1779 G_TYPE_INT, /* dir */
1780 G_TYPE_INT, /* state */
1781 G_TYPE_STRING, /* peerid */
1782 G_TYPE_STRING, /* peernick */
1783 G_TYPE_STRING, /* path */
1784 G_TYPE_INT, /* size */
1785 G_TYPE_INT, /* curpos */
1786 G_TYPE_STRING, /* stock */
1787 G_TYPE_FLOAT, /* percentage */
1788 G_TYPE_INT, /* error */
1789 G_TYPE_INT); /* errortime */
1790 gtk_tree_view_set_model(GTK_TREE_VIEW(main_downloads), GTK_TREE_MODEL(dlmodel));
1792 ulmodel = gtk_list_store_new(12, 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);
1793 gtk_tree_view_set_model(GTK_TREE_VIEW(main_uploads), GTK_TREE_MODEL(ulmodel));
1795 srchmodel = gtk_tree_store_new(9, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT, G_TYPE_DOUBLE, G_TYPE_INT, G_TYPE_INT);
1796 srchmodelfilter = GTK_TREE_MODEL_FILTER(gtk_tree_model_filter_new(GTK_TREE_MODEL(srchmodel), NULL));
1797 gtk_tree_model_filter_set_visible_func(srchmodelfilter, srchfilterfunc, NULL, NULL);
1798 sortmodel = gtk_tree_model_sort_new_with_model(GTK_TREE_MODEL(srchmodelfilter));
1799 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(sortmodel), 4, GTK_SORT_DESCENDING);
1800 gtk_tree_view_set_model(GTK_TREE_VIEW(main_srchres), GTK_TREE_MODEL(sortmodel));
1801 g_object_unref(sortmodel);
1803 monospacefont = pango_font_description_from_string("Monospace 10");
1804 gtk_widget_modify_font(main_chatview, monospacefont);
1805 pango_font_description_free(monospacefont);
1807 updatesbar(_("Disconnected"));
1808 gtk_widget_show(wnd);
1810 dcconnect(dcserver);
1811 g_timeout_add(500, srchstatupdatecb, NULL);
1812 g_timeout_add(5000, ksupdatecb, NULL);