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 gboolean cb_main_trlist_keypress(GtkWidget *widget, GdkEventKey *event, gpointer data);
108 void cb_main_filternoslots_toggled(GtkToggleButton *widget, gpointer data);
109 void cb_main_srhash_activate(GtkWidget *widget, gpointer data);
110 void cb_main_trhash_activate(GtkWidget *widget, gpointer data);
111 void cb_main_trcancel_activate(GtkWidget *widget, gpointer data);
112 gboolean cb_main_srpopup(GtkWidget *widget, GdkEventButton *event, gpointer data);
113 gboolean cb_main_trpopup(GtkWidget *widget, GdkEventButton *event, gpointer data);
114 void dcfdcallback(gpointer data, gint source, GdkInputCondition condition);
115 void srchstatupdate(void);
116 void transnicebytefunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data);
117 void transerrorinfo(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data);
118 void percentagefunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data);
119 void hidezerofunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data);
120 void speedtimefunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data);
122 #define DCCHARSET "windows-1252"
124 #define _(text) gettext(text)
126 #include "mainwnd.gtk"
127 #include "inpdialog.gtk"
130 void updatewrite(void)
137 gdkwrite = gdk_input_add(dcfd, GDK_INPUT_WRITE, dcfdcallback, NULL);
141 gdk_input_remove(gdkwrite);
147 void fndestroycb(struct dc_fnetnode *fn)
150 GtkTextBuffer *textbuf;
153 g_object_unref(data->textbuf);
155 if(curchat == fn->id)
157 textbuf = gtk_text_buffer_new(chattags);
158 gtk_text_view_set_buffer(GTK_TEXT_VIEW(main_chatview), textbuf);
159 g_object_unref(textbuf);
163 void addfndata(struct dc_fnetnode *fn)
167 if(fn->udata != NULL)
169 fn->destroycb = fndestroycb;
170 data = smalloc(sizeof(*data));
171 data->textbuf = gtk_text_buffer_new(chattags);
175 char *getfnstatestock(int state)
177 if(state == DC_FNN_STATE_SYN)
178 return("gtk-jump-to");
179 if(state == DC_FNN_STATE_HS)
180 return("gtk-execute");
181 if(state == DC_FNN_STATE_EST)
183 if(state == DC_FNN_STATE_DEAD)
184 return("gtk-cancel");
188 void updatehublist(void)
191 struct dc_fnetnode *fn;
198 for(fn = dc_fnetnodes; fn != NULL; fn = fn->next)
204 if(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(fnmodel), &iter))
208 gtk_tree_model_get(GTK_TREE_MODEL(fnmodel), &iter, 0, &id, -1);
209 if((fn = dc_findfnetnode(id)) == NULL)
211 /* I can't seem to get a sensible reply fromp
212 * gtk_list_store, so I'm just doing this
214 gtk_list_store_remove(fnmodel, &iter);
218 gtk_tree_model_get(GTK_TREE_MODEL(fnmodel), &iter, 1, &name, 2, &state, 3, &numusers, -1);
222 buf = icswcstombs(fn->name, "UTF-8", NULL);
223 if(strcmp(buf, name))
224 gtk_list_store_set(fnmodel, &iter, 1, buf, -1);
225 if(state != fn->state)
227 gtk_list_store_set(fnmodel, &iter, 2, fn->state, -1);
228 gtk_list_store_set(fnmodel, &iter, 4, getfnstatestock(fn->state), -1);
230 if(numusers != fn->numusers)
231 gtk_list_store_set(fnmodel, &iter, 3, fn->numusers, -1);
235 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(fnmodel), &iter));
238 for(fn = dc_fnetnodes; fn != NULL; fn = fn->next)
245 buf = icswcstombs(fn->name, "UTF-8", NULL);
246 gtk_list_store_append(fnmodel, &iter);
247 gtk_list_store_set(fnmodel, &iter, 0, fn->id, 1, buf, 2, fn->state, 3, fn->numusers, 4, getfnstatestock(fn->state), -1);
253 void percentagefunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
260 gtk_tree_model_get(model, iter, colnum, &val, -1);
261 snprintf(buf, 64, "%.2f%%", (double)(val * 100.0));
262 g_object_set(rend, "text", buf, NULL);
265 void transnicebytefunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
271 gtk_tree_model_get(model, iter, colnum, &val, -1);
273 snprintf(buf, 64, "%'i", val);
275 strcpy(buf, _("Unknown"));
276 g_object_set(rend, "text", buf, NULL);
279 void hidezerofunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
285 gtk_tree_model_get(model, iter, colnum, &val, -1);
287 snprintf(buf, 64, "%i", val);
290 g_object_set(rend, "text", buf, NULL);
293 void speedtimefunc(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
295 int speed, size, time;
298 gtk_tree_model_get(model, iter, 4, &size, 8, &speed, -1);
301 time = (size / speed) / 60;
303 snprintf(buf, 64, "%'i (<00:01)", speed);
305 snprintf(buf, 64, "%'i (%02i:%02i)", speed, time / 60, time % 60);
306 } else if(speed == 0) {
309 strcpy(buf, _("Unknown"));
311 g_object_set(rend, "text", buf, NULL);
314 void transerrorinfo(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
318 char finbuf[64], tbuf[64], *errstr;
320 gtk_tree_model_get(model, iter, 10, &error, 11, &errortime, -1);
321 if(error != DC_TRNSE_NOERROR)
323 if(error == DC_TRNSE_NOTFOUND)
324 errstr = _("Not found");
325 else if(error == DC_TRNSE_NOSLOTS)
326 errstr = _("No slots");
327 strftime(tbuf, 64, _("%H:%M:%S"), localtime(&errortime));
328 snprintf(finbuf, 64, _("%s (reported at %s)"), errstr, tbuf);
332 g_object_set(rend, "text", finbuf, NULL);
335 char *gettrstatestock(int state)
337 if(state == DC_TRNS_WAITING)
338 return("gtk-jump-to");
339 if(state == DC_TRNS_HS)
340 return("gtk-execute");
341 if(state == DC_TRNS_MAIN)
342 return("gtk-network");
343 if(state == DC_TRNS_DONE)
348 void updatetransferlists(void)
352 struct dc_transfer *transfer;
356 char *peerid, *peernick, *path, *hash;
357 int state, dir, size, curpos, error;
359 GtkListStore *stores[3];
361 for(transfer = dc_transfers; transfer != NULL; transfer = transfer->next)
363 stores[DC_TRNSD_UNKNOWN] = NULL;
364 stores[DC_TRNSD_UP] = ulmodel;
365 stores[DC_TRNSD_DOWN] = dlmodel;
366 for(i = 0; i < 3; i++)
368 if(stores[i] == NULL)
374 if(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(stores[i]), &iter))
378 gtk_tree_model_get(GTK_TREE_MODEL(stores[i]), &iter, 0, &id, 1, &dir, -1);
379 if(((transfer = dc_findtransfer(id)) == NULL) || (transfer->dir != dir))
381 gtk_list_store_remove(stores[i], &iter);
386 gtk_tree_model_get(GTK_TREE_MODEL(stores[i]), &iter, 2, &state, 3, &peerid, 4, &peernick, 5, &path, 6, &size, 7, &curpos, 10, &error, 11, &errortime, 12, &hash, -1);
387 if(state != transfer->state)
388 gtk_list_store_set(stores[i], &iter, 2, transfer->state, 8, gettrstatestock(transfer->state), -1);
389 if(size != transfer->size)
390 gtk_list_store_set(stores[i], &iter, 6, transfer->size, -1);
391 if(curpos != transfer->curpos)
392 gtk_list_store_set(stores[i], &iter, 7, transfer->curpos, -1);
393 if(error != transfer->error)
394 gtk_list_store_set(stores[i], &iter, 10, transfer->error, -1);
395 if(errortime != transfer->errortime)
396 gtk_list_store_set(stores[i], &iter, 11, transfer->errortime, -1);
397 if((transfer->size > 0) && (transfer->curpos > 0))
398 gtk_list_store_set(stores[i], &iter, 9, (float)transfer->curpos / (float)transfer->size, -1);
399 buf = icswcstombs(transfer->peerid, "UTF-8", NULL);
400 if(strcmp(buf, peerid))
401 gtk_list_store_set(stores[i], &iter, 3, buf, -1);
402 buf = icswcstombs(((transfer->peernick == NULL) || (transfer->peernick[0] == L'\0'))?transfer->peerid:transfer->peernick, "UTF-8", NULL);
403 if(strcmp(buf, peernick))
404 gtk_list_store_set(stores[i], &iter, 4, buf, -1);
405 buf = (transfer->path == NULL)?_("Unknown"):icswcstombs(transfer->path, "UTF-8", NULL);
406 if(strcmp(buf, path))
407 gtk_list_store_set(stores[i], &iter, 5, buf, -1);
408 buf = (transfer->hash == NULL)?"":icswcstombs(transfer->hash, "UTF-8", NULL);
409 if(strcmp(buf, path))
410 gtk_list_store_set(stores[i], &iter, 12, buf, -1);
416 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(stores[i]), &iter));
420 for(transfer = dc_transfers; transfer != NULL; transfer = transfer->next)
424 if(stores[transfer->dir] != NULL)
426 peerid = icwcstombs(transfer->peerid, "UTF-8");
427 peernick = icwcstombs(((transfer->peernick == NULL) || (transfer->peernick[0] == L'\0'))?transfer->peerid:transfer->peernick, "UTF-8");
428 path = (transfer->path == NULL)?_("Unknown"):icwcstombs(transfer->path, "UTF-8");
429 hash = (transfer->hash == NULL)?"":icwcstombs(transfer->hash, "UTF-8");
430 gtk_list_store_append(stores[transfer->dir], &iter);
431 gtk_list_store_set(stores[transfer->dir], &iter,
440 8, gettrstatestock(transfer->state),
443 11, transfer->errortime,
448 if(transfer->path != NULL)
450 if(transfer->hash != NULL)
457 void updatesbar(char *msg)
459 gtk_statusbar_pop(GTK_STATUSBAR(main_statusbar), 0);
460 gtk_statusbar_push(GTK_STATUSBAR(main_statusbar), 0, msg);
463 void freesrchsizes(void)
467 for(i = 0; i < numsizes; i++)
469 if(srchsizes[i].ref != NULL)
470 gtk_tree_row_reference_free(srchsizes[i].ref);
472 if(srchsizes != NULL)
478 void dcdisconnected(void)
482 gdk_input_remove(gdkread);
487 updatetransferlists();
488 cursrch = nextsrch = -1;
489 gtk_tree_store_clear(srchmodel);
491 gtk_widget_set_sensitive(main_connmenu, TRUE);
492 gtk_widget_set_sensitive(main_dconnmenu, FALSE);
493 gtk_widget_set_sensitive(main_simplesrch, TRUE);
494 gtk_widget_set_sensitive(main_realsrch, TRUE);
495 gtk_widget_set_sensitive(main_srchbtn, TRUE);
496 gtk_widget_set_sensitive(main_srchcanbtn, FALSE);
497 updatesbar(_("Disconnected"));
500 char *inputbox(char *title, char *prompt, char *def, int echo)
506 inpdialog = gtk_dialog_new_with_buttons(title, GTK_WINDOW(main_wnd), GTK_DIALOG_MODAL, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL);
507 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(inpdialog)->vbox), swnd = create_inpdialog_wnd(), TRUE, TRUE, 0);
508 gtk_widget_show(swnd);
510 gtk_entry_set_visibility(GTK_ENTRY(inpdialog_entry), FALSE);
511 gtk_label_set_text(GTK_LABEL(inpdialog_prompt), prompt);
512 gtk_entry_set_text(GTK_ENTRY(inpdialog_entry), def);
513 resp = gtk_dialog_run(GTK_DIALOG(inpdialog));
515 gtk_entry_set_visibility(GTK_ENTRY(inpdialog_entry), TRUE);
516 if(resp == GTK_RESPONSE_ACCEPT)
517 buf = strdup(gtk_entry_get_text(GTK_ENTRY(inpdialog_entry)));
520 gtk_widget_destroy(inpdialog);
525 int msgbox(int type, int buttons, char *format, ...)
532 va_start(args, format);
533 buf = vsprintf2(format, args);
535 swnd = gtk_message_dialog_new(GTK_WINDOW(main_wnd), GTK_DIALOG_MODAL, type, buttons, "%s", buf);
536 resp = gtk_dialog_run(GTK_DIALOG(swnd));
537 gtk_widget_destroy(swnd);
542 void readconfigfile(void)
545 char *homedir, *buf, *p;
548 if((homedir = getenv("HOME")) == NULL)
550 fprintf(stderr, "warning: could not find home directory!\n");
553 buf = sprintf2("%s/.dolconrc", homedir);
554 if((cfgfile = fopen(buf, "r")) == NULL)
563 while(fgets(buf, 1024, cfgfile) != NULL)
567 p = buf + strlen(buf);
570 if((p = strchr(buf, ':')) == NULL)
573 while((*p == ' ') || (*p == '\t'))
575 if(!strcmp(buf, "wnd-width"))
578 } else if(!strcmp(buf, "wnd-height")) {
580 } else if(!strcmp(buf, "pane1-pos")) {
581 gtk_paned_set_position(GTK_PANED(main_pane1), atoi(p));
582 } else if(!strcmp(buf, "pane2-pos")) {
583 gtk_paned_set_position(GTK_PANED(main_pane2), atoi(p));
584 } else if(!strcmp(buf, "pane3-pos")) {
585 gtk_paned_set_position(GTK_PANED(main_pane3), atoi(p));
586 } else if(!strcmp(buf, "pubhubaddr")) {
588 pubhubaddr = sstrdup(p);
589 } else if(!strcmp(buf, "dcserver")) {
591 dcserver = sstrdup(p);
592 } else if(!strcmp(buf, "advexpanded")) {
593 gtk_expander_set_expanded(GTK_EXPANDER(main_advexp), atoi(p));
594 } else if(!strcmp(buf, "connectas")) {
596 connectas = sstrdup(p);
597 } else if(!strcmp(buf, "autoconn")) {
599 } else if(!strcmp(buf, "filternoslots")) {
600 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(main_filternoslots), atoi(p));
609 gtk_window_resize(GTK_WINDOW(main_wnd), w, h);
612 void updateconfigfile(void)
618 if((homedir = getenv("HOME")) == NULL)
620 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not get your home directory!"));
623 buf = sprintf2("%s/.dolconrc", homedir);
624 if((cfgfile = fopen(buf, "w")) == NULL)
627 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not open configuration file for writing: %s"), strerror(errno));
631 gtk_window_get_size(GTK_WINDOW(main_wnd), &w, &h);
632 fprintf(cfgfile, "wnd-width: %i\n", w);
633 fprintf(cfgfile, "wnd-height: %i\n", h);
634 fprintf(cfgfile, "pane1-pos: %i\n", gtk_paned_get_position(GTK_PANED(main_pane1)));
635 fprintf(cfgfile, "pane2-pos: %i\n", gtk_paned_get_position(GTK_PANED(main_pane2)));
636 fprintf(cfgfile, "pane3-pos: %i\n", gtk_paned_get_position(GTK_PANED(main_pane3)));
637 fprintf(cfgfile, "pubhubaddr: %s\n", pubhubaddr);
638 fprintf(cfgfile, "dcserver: %s\n", dcserver);
639 fprintf(cfgfile, "advexpanded: %i\n", gtk_expander_get_expanded(GTK_EXPANDER(main_advexp)));
640 fprintf(cfgfile, "connectas: %s\n", connectas);
641 fprintf(cfgfile, "autoconn: %i\n", autoconn);
642 fprintf(cfgfile, "filternoslots: %i\n", gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(main_filternoslots)));
646 gboolean initdeath(GtkWidget *widget, gpointer data)
653 void cb_inpdialog_entry_activate(GtkWidget *widget, gpointer data)
655 gtk_dialog_response(GTK_DIALOG(inpdialog), GTK_RESPONSE_ACCEPT);
658 int loginconv(int type, wchar_t *prompt, char **resp, void *data)
664 buf = icwcstombs(prompt, "UTF-8");
667 case DC_LOGIN_CONV_NOECHO:
668 if((*resp = inputbox(_("Login"), buf, "", 0)) == NULL)
671 case DC_LOGIN_CONV_ECHO:
672 if((*resp = inputbox(_("Login"), buf, "", 1)) == NULL)
675 case DC_LOGIN_CONV_INFO:
676 msgbox(GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "%s", buf);
678 case DC_LOGIN_CONV_ERROR:
679 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", buf);
687 void getfnlistcallback(int resp, void *data)
692 void gettrlistcallback(int resp, void *data)
694 updatetransferlists();
697 void logincallback(int err, wchar_t *reason, void *data)
701 case DC_LOGIN_ERR_SUCCESS:
702 dc_queuecmd(NULL, NULL, L"notify", L"all", L"on", NULL);
703 dc_getfnlistasync(getfnlistcallback, NULL);
704 dc_gettrlistasync(gettrlistcallback, NULL);
705 updatesbar("Authenticated");
707 case DC_LOGIN_ERR_NOLOGIN:
708 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not negotiate an acceptable authentication mechanism"));
712 case DC_LOGIN_ERR_SERVER:
713 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("The server has encountered an error"));
717 case DC_LOGIN_ERR_USER:
718 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Internal client error"));
722 case DC_LOGIN_ERR_CONV:
726 case DC_LOGIN_ERR_AUTHFAIL:
727 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Login attempt failed!"));
735 GtkTreeIter *ref2iter(GtkTreeRowReference *ref)
737 static GtkTreeIter iter;
740 assert((path = gtk_tree_row_reference_get_path(ref)) != NULL);
741 assert(gtk_tree_model_get_iter(GTK_TREE_MODEL(srchmodel), &iter, path));
742 gtk_tree_path_free(path);
746 GtkTreeRowReference *iter2ref(GtkTreeIter *iter)
749 GtkTreeRowReference *ref;
751 assert((path = gtk_tree_model_get_path(GTK_TREE_MODEL(srchmodel), iter)) != NULL);
752 assert((ref = gtk_tree_row_reference_new(GTK_TREE_MODEL(srchmodel), path)) != NULL);
753 gtk_tree_path_free(path);
757 struct srchsize *finddiscsize(void)
762 for(i = 0; i < numsizes; i++)
764 if(srchsizes[i].size == -1)
765 return(&srchsizes[i]);
767 srchsizes = srealloc(srchsizes, sizeof(*srchsizes) * ++numsizes);
768 srchsizes[i].size = -1;
769 srchsizes[i].num = 1;
770 srchsizes[i].slots = 0;
771 srchsizes[i].resptime = 0.0;
772 gtk_tree_store_append(srchmodel, &iter, NULL);
773 gtk_tree_store_set(srchmodel, &iter, 3, _("Discrete sizes"), 7, 1, -1);
774 srchsizes[i].ref = iter2ref(&iter);
775 return(&srchsizes[i]);
778 struct knownspeed *findksentbyname(char *userid)
782 for(i = 0; i < numspeeds; i++)
784 if(!strcmp(knownspeeds[i].userid, userid))
785 return(&knownspeeds[i]);
790 struct knownspeed *findksentbyseq(int seq)
794 for(i = 0; i < numspeeds; i++)
796 if(knownspeeds[i].seq == seq)
797 return(&knownspeeds[i]);
802 gboolean ksupdaterow(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
804 struct knownspeed *ks;
807 gtk_tree_model_get(GTK_TREE_MODEL(model), iter, 1, &userid, -1);
810 ks = findksentbyname(userid);
813 knownspeeds = srealloc(knownspeeds, (numspeeds + 1) * sizeof(*knownspeeds));
814 ks = &knownspeeds[numspeeds];
816 ks->userid = sstrdup(userid);
823 gtk_tree_store_set(GTK_TREE_STORE(model), iter, 8, ks->speed, -1);
827 gint ksupdatecb(gpointer data)
831 wchar_t **users, *buf;
832 size_t userssize, usersdata;
838 for(i = 0; i < numspeeds;)
840 if(now - knownspeeds[i].fetched > 60)
842 free(knownspeeds[i].userid);
843 memmove(&knownspeeds[i], &knownspeeds[i + 1], (--numspeeds - i) * sizeof(*knownspeeds));
848 if(oldnum != numspeeds)
855 knownspeeds = srealloc(knownspeeds, numspeeds * sizeof(*knownspeeds));
858 gtk_tree_model_foreach(GTK_TREE_MODEL(srchmodel), ksupdaterow, NULL);
862 userssize = usersdata = 0;
864 for(i = 0; i < numspeeds; i++)
866 if(knownspeeds[i].seq == -2)
868 assert((buf = icmbstowcs(knownspeeds[i].userid, "UTF-8")) != NULL);
869 knownspeeds[i].seq = ksqueryseq++;
870 addtobuf(users, buf);
873 addtobuf(users, NULL);
874 ksquerytag = dc_queuecmd(NULL, NULL, L"filtercmd", L"userspeeda", L"%%a", users, NULL);
875 dc_freewcsarr(users);
880 void handleresps(void)
883 struct dc_response *resp;
884 struct dc_intresp *ires;
885 struct dc_fnetnode *fn;
886 struct fndata *fndata;
888 GtkTreeIter titer, piter;
892 struct knownspeed *ks;
894 while((resp = dc_getresp()) != NULL)
896 if(!wcscmp(resp->cmdname, L".connect"))
898 if(resp->code == 200)
900 tosbuf = 0x10; /* Minimum cost */
901 setsockopt(dcfd, SOL_IP, IP_TOS, &tosbuf, sizeof(tosbuf));
902 updatesbar(_("Connected"));
903 dc_loginasync(connectas, 1, loginconv, logincallback, NULL);
905 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("The server refused the connection"));
909 } else if(!wcscmp(resp->cmdname, L".notify")) {
910 dc_uimisc_handlenotify(resp);
914 if((ires = dc_interpret(resp)) != NULL)
916 if((fn = dc_findfnetnode(ires->argv[0].val.num)) != NULL)
919 gtk_text_buffer_get_end_iter(fndata->textbuf, &iter);
920 if((buf = icwcstombs(ires->argv[3].val.str, "UTF-8")) != NULL)
922 gtk_text_buffer_insert_with_tags_by_name(fndata->textbuf, &iter, "<", -1, "sender", NULL);
923 gtk_text_buffer_insert_with_tags_by_name(fndata->textbuf, &iter, buf, -1, "sender", NULL);
924 gtk_text_buffer_insert_with_tags_by_name(fndata->textbuf, &iter, ">", -1, "sender", NULL);
925 gtk_text_buffer_insert(fndata->textbuf, &iter, " ", -1);
928 if((buf = icwcstombs(ires->argv[4].val.str, "UTF-8")) != NULL)
930 gtk_text_buffer_insert(fndata->textbuf, &iter, buf, -1);
931 gtk_text_buffer_insert(fndata->textbuf, &iter, "\n", -1);
933 if(curchat == fn->id)
934 gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(main_chatview), &iter, 0, 0, 0, 0);
956 updatetransferlists();
959 if((ires = dc_interpret(resp)) != NULL)
961 if(ires->argv[0].val.num == nextsrch)
962 srcheta = time(NULL) + ires->argv[0].val.num;
967 if((ires = dc_interpret(resp)) != NULL)
969 if(ires->argv[0].val.num == nextsrch)
972 dc_queuecmd(NULL, NULL, L"cansrch", L"%%i", cursrch, NULL);
975 gtk_widget_set_sensitive(main_realsrch, TRUE);
976 gtk_widget_set_sensitive(main_simplesrch, TRUE);
977 gtk_widget_set_sensitive(main_srchbtn, TRUE);
978 gtk_widget_set_sensitive(main_srchcanbtn, FALSE);
980 gtk_entry_set_text(GTK_ENTRY(main_realsrch), "");
981 gtk_entry_set_text(GTK_ENTRY(main_simplesrch), "");
982 gtk_tree_store_clear(srchmodel);
989 if((ires = dc_interpret(resp)) != NULL)
991 if(ires->argv[0].val.num == cursrch)
993 for(i = 0; i < numsizes; i++)
995 if(srchsizes[i].size == ires->argv[4].val.num)
1000 srchsizes = srealloc(srchsizes, sizeof(*srchsizes) * ++numsizes);
1001 srchsizes[i].size = ires->argv[4].val.num;
1002 srchsizes[i].num = 1;
1003 srchsizes[i].slots = ires->argv[5].val.num;
1004 srchsizes[i].resptime = ires->argv[7].val.flnum;
1005 ss = finddiscsize();
1006 ss->slots += ires->argv[5].val.num;
1007 if((ss->resptime == 0.0) || (ss->resptime > ires->argv[7].val.flnum))
1008 ss->resptime = ires->argv[7].val.flnum;
1009 piter = *ref2iter(ss->ref);
1010 gtk_tree_store_set(srchmodel, &piter, 5, ss->slots, 6, ss->resptime, -1);
1011 gtk_tree_store_append(srchmodel, &titer, &piter);
1012 srchsizes[i].ref = iter2ref(&titer);
1013 } else if(srchsizes[i].num == 1) {
1014 char *filename, *peername, *fnetname, *hash;
1018 gtk_tree_model_get(GTK_TREE_MODEL(srchmodel), ref2iter(srchsizes[i].ref), 0, &fnetname, 1, &peername, 3, &filename, 5, &slots, 6, &resptime, 8, &speed, 9, &hash, -1);
1019 gtk_tree_store_remove(srchmodel, ref2iter(srchsizes[i].ref));
1020 gtk_tree_row_reference_free(srchsizes[i].ref);
1021 ss = finddiscsize();
1023 gtk_tree_store_set(srchmodel, ref2iter(ss->ref), 5, ss->slots, -1);
1024 gtk_tree_store_append(srchmodel, &piter, NULL);
1025 srchsizes[i].slots = ires->argv[5].val.num + slots;
1026 srchsizes[i].resptime = (ires->argv[7].val.flnum < resptime)?ires->argv[7].val.flnum:resptime;
1027 srchsizes[i].num = 2;
1028 srchsizes[i].ref = iter2ref(&piter);
1029 gtk_tree_store_set(srchmodel, &piter, 4, srchsizes[i].size, 5, srchsizes[i].slots, 6, srchsizes[i].resptime, 7, 2, -1);
1030 if((buf = icwcstombs(ires->argv[1].val.str, "UTF-8")) != NULL)
1033 /* XXX: Too NMDC-specific! */
1034 if(strrchr(p, '\\') != NULL)
1035 p = strrchr(p, '\\') + 1;
1036 gtk_tree_store_set(srchmodel, &piter, 3, p, -1);
1039 gtk_tree_store_append(srchmodel, &titer, &piter);
1040 gtk_tree_store_set(srchmodel, &titer, 0, fnetname, 1, peername, 2, peername, 3, filename, 4, srchsizes[i].size, 5, slots, 6, resptime, 8, speed, 9, hash, -1);
1041 g_free(filename); g_free(peername); g_free(fnetname); g_free(hash);
1042 gtk_tree_store_append(srchmodel, &titer, &piter);
1045 srchsizes[i].slots += ires->argv[5].val.num;
1046 if(ires->argv[7].val.flnum < srchsizes[i].resptime)
1047 srchsizes[i].resptime = ires->argv[7].val.flnum;
1048 piter = *ref2iter(srchsizes[i].ref);
1049 gtk_tree_store_set(srchmodel, &piter, 5, srchsizes[i].slots, 6, srchsizes[i].resptime, 7, srchsizes[i].num, -1);
1050 gtk_tree_store_append(srchmodel, &titer, &piter);
1052 if((buf = icwcstombs(ires->argv[1].val.str, "UTF-8")) != NULL)
1054 gtk_tree_store_set(srchmodel, &titer, 3, buf, -1);
1057 if((buf = icwcstombs(ires->argv[2].val.str, "UTF-8")) != NULL)
1059 gtk_tree_store_set(srchmodel, &titer, 0, buf, -1);
1062 if((buf = icwcstombs(ires->argv[3].val.str, "UTF-8")) != NULL)
1064 gtk_tree_store_set(srchmodel, &titer, 1, buf, -1);
1065 gtk_tree_store_set(srchmodel, &titer, 2, buf, -1);
1068 if((buf = icwcstombs(ires->argv[8].val.str, "UTF-8")) != NULL)
1070 gtk_tree_store_set(srchmodel, &titer, 9, buf, -1);
1073 gtk_tree_store_set(srchmodel, &titer, 4, ires->argv[4].val.num, 5, ires->argv[5].val.num, 6, ires->argv[7].val.flnum, 8, -1, -1);
1081 } else if(!wcscmp(resp->cmdname, L"filtercmd")) {
1082 if((ksquerytag >= 0) && (ksquerytag == resp->tag))
1084 for(i = 0; i < resp->numlines; i++)
1086 assert((ks = findksentbyseq(i)) != NULL);
1087 ks->speed = wcstol(resp->rlines[i].argv[1], NULL, 10);
1089 ks->fetched = time(NULL);
1100 void dcfdcallback(gpointer data, gint source, GdkInputCondition condition)
1104 if(((condition & GDK_INPUT_READ) && dc_handleread()) || ((condition & GDK_INPUT_WRITE) && dc_handlewrite()))
1110 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("The server has closed the connection"));
1112 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("The connection to the server failed:\n\n%s"), strerror(errnobak));
1119 void cb_main_dconnmenu_activate(GtkWidget *widget, gpointer data)
1127 void cb_main_prefmenu_activate(GtkWidget *widget, gpointer data)
1129 GtkWidget *dialog, *swnd;
1132 dialog = gtk_dialog_new_with_buttons(_("Preferences"), GTK_WINDOW(main_wnd), GTK_DIALOG_MODAL, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL);
1133 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), swnd = create_pref_wnd(), TRUE, TRUE, 0);
1134 gtk_entry_set_text(GTK_ENTRY(pref_pubhuburl), pubhubaddr);
1135 gtk_entry_set_text(GTK_ENTRY(pref_connectas), connectas);
1136 gtk_entry_set_text(GTK_ENTRY(pref_dcserver), dcserver);
1137 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(pref_autoconn), autoconn);
1138 gtk_widget_show(swnd);
1139 resp = gtk_dialog_run(GTK_DIALOG(dialog));
1140 if(resp == GTK_RESPONSE_ACCEPT)
1143 pubhubaddr = sstrdup(gtk_entry_get_text(GTK_ENTRY(pref_pubhuburl)));
1145 connectas = sstrdup(gtk_entry_get_text(GTK_ENTRY(pref_connectas)));
1147 dcserver = sstrdup(gtk_entry_get_text(GTK_ENTRY(pref_dcserver)));
1148 autoconn = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(pref_autoconn));
1150 gtk_widget_destroy(dialog);
1153 void dcconnect(char *host)
1155 dcfd = dc_connect(host, -1);
1158 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect:\n\n%s"), strerror(errno));
1161 gdkread = gdk_input_add(dcfd, GDK_INPUT_READ, dcfdcallback, NULL);
1163 gtk_widget_set_sensitive(main_connmenu, FALSE);
1164 gtk_widget_set_sensitive(main_dconnmenu, TRUE);
1165 updatesbar(_("Connecting..."));
1168 void cb_main_connmenu_activate(GtkWidget *widget, gpointer data)
1174 if((buf = inputbox(_("Connect"), _("Server address:"), dcserver, 1)) == NULL)
1180 void cb_main_sdmenu_activate(GtkWidget *widget, gpointer data)
1183 struct dc_response *resp;
1187 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1190 tag = dc_queuecmd(NULL, NULL, L"shutdown", NULL);
1191 if((resp = dc_gettaggedrespsync(tag)) != NULL)
1193 if(resp->code == 502)
1194 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1200 void cb_main_fnaddr_activate(GtkWidget *widget, gpointer data)
1203 struct dc_response *resp;
1208 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1211 toks = dc_lexsexpr(icsmbstowcs((char *)gtk_entry_get_text(GTK_ENTRY(main_fnaddr)), "UTF-8", NULL));
1214 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Illegal address entered"));
1217 if(wcschr(toks[0], L':') == NULL)
1219 toks[0] = srealloc(toks[0], (wcslen(toks[0]) + 5) * sizeof(wchar_t));
1220 wcscat(toks[0], L":411");
1222 tag = dc_queuecmd(NULL, NULL, L"cnct", L"dc", L"%%a", toks, NULL);
1223 dc_freewcsarr(toks);
1224 if((resp = dc_gettaggedrespsync(tag)) != NULL)
1226 if(resp->code == 502)
1227 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1228 if(resp->code == 509)
1229 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("The server could not parse that address"));
1232 gtk_entry_set_text(GTK_ENTRY(main_fnaddr), "");
1236 void pubhubfdcallback(gpointer data, gint source, GdkInputCondition condition)
1238 static char buf[65536];
1239 static int bufpos = 0;
1245 int sorted, sortcol;
1246 GtkSortType sortorder;
1247 GtkTreeModel *sortmodel;
1249 if(!(condition & GDK_INPUT_READ))
1253 ret = read(pubhubfd, buf + bufpos, sizeof(buf) - bufpos);
1257 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not read from public hub listing process: %s"), strerror(errno));
1259 gdk_input_remove(pubhubtag);
1260 kill(pubhubproc, SIGINT);
1261 pubhubfd = pubhubtag = -1;
1266 regfree(&pubhubfilter);
1272 sortmodel = gtk_tree_view_get_model(GTK_TREE_VIEW(main_phublist));
1273 sorted = gtk_tree_sortable_get_sort_column_id(GTK_TREE_SORTABLE(sortmodel), &sortcol, &sortorder);
1274 gtk_tree_view_set_model(GTK_TREE_VIEW(main_phublist), NULL);
1275 while((p = memchr(buf, '\n', bufpos)) != NULL)
1278 if(!filterpubhub || !regexec(&pubhubfilter, buf, 0, NULL, 0))
1281 for(i = 0; i < 4; i++)
1284 if((p2 = strchr(p2, '|')) == NULL)
1290 for(i = 0; i < 4; i++)
1292 if((wbuf = icsmbstowcs(fields[i], DCCHARSET, NULL)) == NULL)
1294 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not decode hublist - aborting at this point: %s"), strerror(errno));
1295 kill(pubhubproc, SIGINT);
1298 if((fields[i] = icwcstombs(wbuf, "UTF-8")) == NULL)
1303 gtk_list_store_append(pubhubmodel, &iter);
1304 gtk_list_store_set(pubhubmodel, &iter, 0, fields[0], 1, fields[1], 2, fields[2], 3, atoi(fields[3]), -1);
1306 for(i--; i >= 0; i--)
1310 memmove(buf, p, bufpos -= p - buf);
1312 sortmodel = gtk_tree_model_sort_new_with_model(GTK_TREE_MODEL(pubhubmodel));
1314 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(sortmodel), sortcol, sortorder);
1315 gtk_tree_view_set_model(GTK_TREE_VIEW(main_phublist), sortmodel);
1318 void cb_main_pubhubfilter_activate(GtkWidget *widget, gpointer data)
1320 int pipe1[2], pipe2[2];
1326 gdk_input_remove(pubhubtag);
1330 kill(pubhubproc, SIGINT);
1333 regfree(&pubhubfilter);
1336 buf = gtk_entry_get_text(GTK_ENTRY(main_pubhubfilter));
1339 if((err = regcomp(&pubhubfilter, buf, REG_EXTENDED | REG_ICASE | REG_NOSUB)) != 0)
1341 regerror(err, &pubhubfilter, errbuf, sizeof(errbuf));
1342 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "Could not compile regex: %s", errbuf);
1343 regfree(&pubhubfilter);
1349 gtk_list_store_clear(pubhubmodel);
1351 if((pubhubproc = fork()) == 0)
1356 execlp("wget", "wget", "-qO", "-", pubhubaddr, NULL);
1361 pubhubfd = pipe1[0];
1362 len = strlen(pubhubaddr);
1363 if((len > 4) && !strcmp(pubhubaddr + len - 4, ".bz2"))
1373 execlp("bzcat", "bzcat", NULL);
1379 pubhubfd = pipe2[0];
1381 pubhubtag = gdk_input_add(pubhubfd, GDK_INPUT_READ, pubhubfdcallback, NULL);
1384 void cb_main_dcnctbtn_clicked(GtkWidget *widget, gpointer data)
1388 struct dc_response *resp;
1392 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1395 if(!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(main_fnetnodes)), NULL, &iter))
1397 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("No hub selected"));
1400 gtk_tree_model_get(GTK_TREE_MODEL(fnmodel), &iter, 0, &id, -1);
1401 tag = dc_queuecmd(NULL, NULL, L"dcnct", L"%%i", id, NULL);
1402 if((resp = dc_gettaggedrespsync(tag)) != NULL)
1404 if(resp->code == 502)
1405 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1411 void cb_main_phublist_cchange(GtkWidget *widget, gpointer data)
1414 GtkTreeModel *model;
1417 if(!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(main_phublist)), &model, &iter))
1419 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, 1, &addr, -1);
1420 gtk_entry_set_text(GTK_ENTRY(main_fnaddr), addr);
1424 void cb_main_phublist_activate(GtkWidget *widget, GtkTreePath *path, GtkTreeViewColumn *col, gpointer data)
1427 struct dc_response *resp;
1429 GtkTreeModel *model;
1434 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1437 model = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
1438 if(!gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter, path))
1440 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, 1, &buf, -1);
1441 if(strchr(buf, ':') == NULL)
1443 buf = g_realloc(buf, strlen(buf) + 5);
1444 strcat(buf, ":411");
1446 tag = dc_queuecmd(NULL, NULL, L"cnct", L"dc", L"%%s", buf, NULL);
1448 gtk_entry_set_text(GTK_ENTRY(main_fnaddr), "");
1449 if((resp = dc_gettaggedrespsync(tag)) != NULL)
1451 if(resp->code == 502)
1452 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1453 if(resp->code == 509)
1454 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("The server could not parse that address"));
1460 void cb_main_chatnodes_activate(GtkWidget *widget, GtkTreePath *path, GtkTreeViewColumn *col, gpointer uudata)
1464 struct dc_fnetnode *fn;
1465 struct fndata *data;
1467 if(!gtk_tree_model_get_iter(GTK_TREE_MODEL(fnmodel), &iter, path))
1469 gtk_tree_model_get(GTK_TREE_MODEL(fnmodel), &iter, 0, &id, -1);
1470 if((fn = dc_findfnetnode(id)) == NULL)
1474 if(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(fnmodel), &iter))
1478 gtk_tree_model_get(GTK_TREE_MODEL(fnmodel), &iter, 0, &id, -1);
1480 gtk_list_store_set(fnmodel, &iter, 5, "gtk-apply", -1);
1482 gtk_list_store_set(fnmodel, &iter, 5, NULL, -1);
1483 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(fnmodel), &iter));
1485 gtk_text_view_set_buffer(GTK_TEXT_VIEW(main_chatview), GTK_TEXT_BUFFER(data->textbuf));
1488 void cb_main_chatstr_activate(GtkWidget *widget, gpointer data)
1492 struct dc_response *resp;
1496 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1501 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("No hub selected"));
1504 buf = gtk_entry_get_text(GTK_ENTRY(main_chatstr));
1505 tag = dc_queuecmd(NULL, NULL, L"sendchat", L"%%i", curchat, L"1", L"", L"%%s", buf, NULL);
1506 if((resp = dc_gettaggedrespsync(tag)) != NULL)
1508 if(resp->code == 502)
1509 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1510 else if(resp->code == 504)
1511 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("This hub could not support all the types of characters in your chat message"));
1512 else if(resp->code == 513)
1513 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("This hub does not support chatting"));
1514 else if(resp->code != 200)
1515 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("An error occurred while trying to chat (%i)"), resp->code);
1518 gtk_entry_set_text(GTK_ENTRY(main_chatstr), "");
1522 void updatesrchfld(const char *simple)
1526 size_t bufsize, bufdata;
1528 s = sstrdup(simple);
1530 bufsize = bufdata = 0;
1534 p2 = strchr(p, ' ');
1540 bufcat(buf, " & ", 3);
1541 bufcat(buf, "N~", 2);
1544 if(strchr("[]()$^.*?+\\|\"!", *p) != NULL)
1545 addtobuf(buf, '\\');
1550 } while(p2 != NULL);
1552 gtk_entry_set_text(GTK_ENTRY(main_realsrch), buf);
1557 void cb_main_simplesrch_changed(GtkWidget *widget, gpointer data)
1562 updatesrchfld(gtk_entry_get_text(GTK_ENTRY(main_simplesrch)));
1566 void cb_main_realsrch_changed(GtkWidget *widget, gpointer data)
1571 gtk_entry_set_text(GTK_ENTRY(main_simplesrch), "");
1575 void cb_main_srchbtn_clicked(GtkWidget *widget, gpointer data)
1579 struct dc_response *resp;
1580 struct dc_intresp *ires;
1584 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1587 if(nextsrch != -1) /* Impossible case, but oh well... */
1589 toks = dc_lexsexpr(icsmbstowcs((char *)gtk_entry_get_text(GTK_ENTRY(main_realsrch)), "UTF-8", NULL));
1592 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Please enter a search expression before searching"));
1595 tag = dc_queuecmd(NULL, NULL, L"search", L"all", L"%%a", toks, NULL);
1596 dc_freewcsarr(toks);
1597 if((resp = dc_gettaggedrespsync(tag)) != NULL)
1599 if(resp->code == 501)
1600 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("Could not find any hubs to search on"));
1601 else if(resp->code == 502)
1602 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1603 else if(resp->code == 509)
1604 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("The server could not parse your search expression"));
1605 else if(resp->code != 200)
1606 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("An error occurred while trying to search (%i)"), resp->code);
1607 if(resp->code == 200)
1609 if((ires = dc_interpret(resp)) != NULL)
1611 nextsrch = ires->argv[0].val.num;
1612 srcheta = time(NULL) + ires->argv[1].val.num;
1615 gtk_widget_set_sensitive(main_realsrch, FALSE);
1616 gtk_widget_set_sensitive(main_simplesrch, FALSE);
1617 gtk_widget_set_sensitive(main_srchbtn, FALSE);
1618 gtk_widget_set_sensitive(main_srchcanbtn, TRUE);
1626 void cb_main_srchcanbtn_clicked(GtkWidget *widget, gpointer data)
1630 dc_queuecmd(NULL, NULL, L"cansrch", L"%%i", nextsrch, NULL);
1632 gtk_widget_set_sensitive(main_realsrch, TRUE);
1633 gtk_widget_set_sensitive(main_simplesrch, TRUE);
1634 gtk_widget_set_sensitive(main_srchbtn, TRUE);
1635 gtk_widget_set_sensitive(main_srchcanbtn, FALSE);
1639 gboolean cb_main_trlist_keypress(GtkWidget *widget, GdkEventKey *event, gpointer data)
1642 GtkTreeSelection *sel;
1643 GtkTreeModel *model;
1645 struct dc_response *resp;
1647 if((event->type == GDK_KEY_PRESS) && (event->keyval == GDK_Delete))
1649 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
1650 if(gtk_tree_selection_get_selected(sel, &model, &iter))
1652 gtk_tree_model_get(model, &iter, 0, &id, -1);
1653 tag = dc_queuecmd(NULL, NULL, L"cancel", L"%%i", id, NULL);
1654 if((resp = dc_gettaggedrespsync(tag)) != NULL)
1656 if(resp->code == 502)
1657 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1658 else if(resp->code != 200)
1659 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("An error occurred while trying to cancel (%i)"), resp->code);
1669 void cb_main_srchres_activate(GtkWidget *widget, GtkTreePath *path, GtkTreeViewColumn *col, gpointer data)
1672 struct dc_response *resp;
1674 GtkTreeModel *model;
1676 char *tfnet, *tpeerid, *tfilename, *thash, *arg;
1677 wchar_t *fnet, *peerid, *filename, *hash;
1681 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1684 model = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
1685 if(!gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter, path))
1687 gtk_tree_model_get(model, &iter, 7, &num, -1);
1690 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, 0, &tfnet, 1, &tpeerid, 3, &tfilename, 4, &size, 9, &thash, -1);
1691 fnet = icmbstowcs(tfnet, "UTF-8");
1692 peerid = icmbstowcs(tpeerid, "UTF-8");
1693 filename = icmbstowcs(tfilename, "UTF-8");
1694 hash = (thash == NULL)?NULL:icmbstowcs(thash, "UTF-8");
1695 if((fnet == NULL) || (peerid == NULL) || (filename == NULL))
1701 if(filename != NULL)
1715 arg = (char *)gtk_entry_get_text(GTK_ENTRY(main_dlarg));
1717 tag = dc_queuecmd(NULL, NULL, L"download", fnet, L"%%ls", peerid, L"%%ls", filename, L"%%i", size, L"hash", L"%%ls", (hash == NULL)?L"":hash, L"user", L"%%s", arg, NULL);
1719 tag = dc_queuecmd(NULL, NULL, L"download", fnet, L"%%ls", peerid, L"%%ls", filename, L"%%i", size, L"hash", L"%%ls", (hash == NULL)?L"":hash, NULL);
1725 if((resp = dc_gettaggedrespsync(tag)) != NULL)
1727 if(resp->code == 502)
1728 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1729 if(resp->code != 200)
1730 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("An error occurred while trying to queue the download (%i)"), resp->code);
1736 gboolean srchfilterfunc(GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
1741 filteratall = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(main_filternoslots));
1744 gtk_tree_model_get(model, iter, 5, &slots, -1);
1750 void cb_main_filternoslots_toggled(GtkToggleButton *widget, gpointer data)
1752 gtk_tree_model_filter_refilter(srchmodelfilter);
1755 void cb_main_srhash_activate(GtkWidget *widget, gpointer data)
1757 GtkTreeSelection *sel;
1758 GtkTreeModel *model;
1764 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(main_srchres));
1765 if(gtk_tree_selection_get_selected(sel, &model, &iter))
1767 gtk_tree_model_get(model, &iter, 9, &hash, -1);
1768 buf = sprintf2("H=%s", hash);
1769 gtk_entry_set_text(GTK_ENTRY(main_realsrch), buf);
1772 cb_main_srchbtn_clicked(widget, NULL);
1778 void cb_main_trhash_activate(GtkWidget *widget, gpointer data)
1780 GtkTreeSelection *sel;
1781 GtkTreeModel *model;
1787 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(main_downloads));
1788 if(gtk_tree_selection_get_selected(sel, &model, &iter))
1790 gtk_tree_model_get(model, &iter, 12, &hash, -1);
1791 buf = sprintf2("H=%s", hash);
1792 gtk_entry_set_text(GTK_ENTRY(main_realsrch), buf);
1795 cb_main_srchbtn_clicked(widget, NULL);
1801 void cb_main_trcancel_activate(GtkWidget *widget, gpointer data)
1803 GtkTreeSelection *sel;
1804 GtkTreeModel *model;
1807 struct dc_response *resp;
1811 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(main_downloads));
1812 if(gtk_tree_selection_get_selected(sel, &model, &iter))
1814 gtk_tree_model_get(model, &iter, 0, &id, -1);
1815 tag = dc_queuecmd(NULL, NULL, L"cancel", L"%%i", id, NULL);
1816 if((resp = dc_gettaggedrespsync(tag)) != NULL)
1818 if(resp->code == 502)
1819 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1820 else if(resp->code != 200)
1821 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("An error occurred while trying to cancel (%i)"), resp->code);
1830 /* XXX: This is quite a hack, since the calling convention is
1831 * different for the popup-menu sig and the button-press-event sig. It
1832 * most certainly works, but I don't know how portable it is. */
1833 gboolean cb_main_srpopup(GtkWidget *widget, GdkEventButton *event, gpointer data)
1835 GtkTreeSelection *sel;
1836 GtkTreeModel *model;
1840 if((event != NULL) && (event->button != 3))
1842 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
1843 if(gtk_tree_selection_get_selected(sel, &model, &iter))
1845 gtk_tree_model_get(model, &iter, 9, &hash, -1);
1846 if((nextsrch != -1) || (hash == NULL) || (*hash == 0))
1847 gtk_widget_set_sensitive(main_srhash, FALSE);
1849 gtk_widget_set_sensitive(main_srhash, TRUE);
1855 gtk_menu_popup(GTK_MENU(main_srpopup), NULL, NULL, NULL, NULL, 0, gtk_get_current_event_time());
1857 gtk_menu_popup(GTK_MENU(main_srpopup), NULL, NULL, NULL, NULL, event->button, event->time);
1861 /* The above hack note goes for this one too. */
1862 gboolean cb_main_trpopup(GtkWidget *widget, GdkEventButton *event, gpointer data)
1864 GtkTreeSelection *sel;
1865 GtkTreeModel *model;
1869 if((event != NULL) && (event->button != 3))
1871 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
1872 if(gtk_tree_selection_get_selected(sel, &model, &iter))
1874 gtk_tree_model_get(model, &iter, 12, &hash, -1);
1875 if((nextsrch != -1) || (hash == NULL) || (*hash == 0))
1876 gtk_widget_set_sensitive(main_trhash, FALSE);
1878 gtk_widget_set_sensitive(main_trhash, TRUE);
1884 gtk_menu_popup(GTK_MENU(main_trpopup), NULL, NULL, NULL, NULL, 0, gtk_get_current_event_time());
1886 gtk_menu_popup(GTK_MENU(main_trpopup), NULL, NULL, NULL, NULL, event->button, event->time);
1890 void srchstatupdate(void)
1896 snprintf(buf, 1024, _("Ready to search"));
1898 snprintf(buf, 1024, _("Search scheduled and will be submitted in %i seconds"), (int)(srcheta - time(NULL)));
1900 if(strcmp(gtk_label_get_text(GTK_LABEL(main_srchstatus)), buf))
1901 gtk_label_set_text(GTK_LABEL(main_srchstatus), buf);
1904 gint srchstatupdatecb(gpointer data)
1910 void initchattags(void)
1914 chattags = gtk_text_tag_table_new();
1915 tag = gtk_text_tag_new("sender");
1916 g_object_set(tag, "foreground", "blue", NULL);
1917 gtk_text_tag_table_add(chattags, tag);
1920 int main(int argc, char **argv)
1923 PangoFontDescription *monospacefont;
1924 GtkTreeModel *sortmodel;
1925 struct passwd *pwent;
1927 setlocale(LC_ALL, "");
1928 bindtextdomain(PACKAGE, LOCALEDIR);
1929 textdomain(PACKAGE);
1930 gtk_init(&argc, &argv);
1932 signal(SIGCHLD, SIG_IGN);
1933 pubhubaddr = sstrdup("http://www.neo-modus.com/PublicHubList.config");
1934 dcserver = sstrdup("localhost");
1935 if((pwent = getpwuid(getuid())) == NULL)
1937 fprintf(stderr, "could not get your passwd data");
1940 connectas = sstrdup(pwent->pw_name);
1941 wnd = create_main_wnd();
1944 fnmodel = gtk_list_store_new(6, G_TYPE_INT, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING, G_TYPE_STRING);
1945 gtk_tree_view_set_model(GTK_TREE_VIEW(main_fnetnodes), GTK_TREE_MODEL(fnmodel));
1946 gtk_tree_view_set_model(GTK_TREE_VIEW(main_chatnodes), GTK_TREE_MODEL(fnmodel));
1948 pubhubmodel = gtk_list_store_new(4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT);
1949 sortmodel = gtk_tree_model_sort_new_with_model(GTK_TREE_MODEL(pubhubmodel));
1950 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(sortmodel), 3, GTK_SORT_DESCENDING);
1951 gtk_tree_view_set_model(GTK_TREE_VIEW(main_phublist), GTK_TREE_MODEL(sortmodel));
1952 g_object_unref(sortmodel);
1954 dlmodel = gtk_list_store_new(13, G_TYPE_INT, /* id */
1955 G_TYPE_INT, /* dir */
1956 G_TYPE_INT, /* state */
1957 G_TYPE_STRING, /* peerid */
1958 G_TYPE_STRING, /* peernick */
1959 G_TYPE_STRING, /* path */
1960 G_TYPE_INT, /* size */
1961 G_TYPE_INT, /* curpos */
1962 G_TYPE_STRING, /* stock */
1963 G_TYPE_FLOAT, /* percentage */
1964 G_TYPE_INT, /* error */
1965 G_TYPE_INT, /* errortime */
1966 G_TYPE_STRING); /* hash */
1967 gtk_tree_view_set_model(GTK_TREE_VIEW(main_downloads), GTK_TREE_MODEL(dlmodel));
1969 ulmodel = gtk_list_store_new(13, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING, G_TYPE_FLOAT, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING);
1970 gtk_tree_view_set_model(GTK_TREE_VIEW(main_uploads), GTK_TREE_MODEL(ulmodel));
1972 srchmodel = gtk_tree_store_new(10, G_TYPE_STRING, /* fnetname */
1973 G_TYPE_STRING, /* peerid */
1974 G_TYPE_STRING, /* peername */
1975 G_TYPE_STRING, /* filename */
1976 G_TYPE_INT, /* size */
1977 G_TYPE_INT, /* slots */
1978 G_TYPE_DOUBLE, /* resptime */
1979 G_TYPE_INT, /* sizenum */
1980 G_TYPE_INT, /* speed */
1981 G_TYPE_STRING); /* hash */
1982 srchmodelfilter = GTK_TREE_MODEL_FILTER(gtk_tree_model_filter_new(GTK_TREE_MODEL(srchmodel), NULL));
1983 gtk_tree_model_filter_set_visible_func(srchmodelfilter, srchfilterfunc, NULL, NULL);
1984 sortmodel = gtk_tree_model_sort_new_with_model(GTK_TREE_MODEL(srchmodelfilter));
1985 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(sortmodel), 4, GTK_SORT_DESCENDING);
1986 gtk_tree_view_set_model(GTK_TREE_VIEW(main_srchres), GTK_TREE_MODEL(sortmodel));
1987 g_object_unref(sortmodel);
1989 monospacefont = pango_font_description_from_string("Monospace 10");
1990 gtk_widget_modify_font(main_chatview, monospacefont);
1991 pango_font_description_free(monospacefont);
1993 updatesbar(_("Disconnected"));
1994 gtk_widget_show(wnd);
1996 dcconnect(dcserver);
1997 g_timeout_add(500, srchstatupdatecb, NULL);
1998 g_timeout_add(5000, ksupdatecb, NULL);