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)
849 knownspeeds = srealloc(knownspeeds, numspeeds * sizeof(*knownspeeds));
850 gtk_tree_model_foreach(GTK_TREE_MODEL(srchmodel), ksupdaterow, NULL);
854 userssize = usersdata = 0;
856 for(i = 0; i < numspeeds; i++)
858 if(knownspeeds[i].seq == -2)
860 assert((buf = icmbstowcs(knownspeeds[i].userid, "UTF-8")) != NULL);
861 knownspeeds[i].seq = ksqueryseq++;
862 addtobuf(users, buf);
865 addtobuf(users, NULL);
866 ksquerytag = dc_queuecmd(NULL, NULL, L"filtercmd", L"userspeeda", L"%%a", users, NULL);
867 dc_freewcsarr(users);
872 void handleresps(void)
875 struct dc_response *resp;
876 struct dc_intresp *ires;
877 struct dc_fnetnode *fn;
878 struct fndata *fndata;
880 GtkTreeIter titer, piter;
884 struct knownspeed *ks;
886 while((resp = dc_getresp()) != NULL)
888 if(!wcscmp(resp->cmdname, L".connect"))
890 if(resp->code == 200)
892 tosbuf = 0x10; /* Minimum cost */
893 setsockopt(dcfd, SOL_IP, IP_TOS, &tosbuf, sizeof(tosbuf));
894 updatesbar(_("Connected"));
895 dc_loginasync(connectas, 1, loginconv, logincallback, NULL);
897 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("The server refused the connection"));
901 } else if(!wcscmp(resp->cmdname, L".notify")) {
902 dc_uimisc_handlenotify(resp);
906 if((ires = dc_interpret(resp)) != NULL)
908 if((fn = dc_findfnetnode(ires->argv[0].val.num)) != NULL)
911 gtk_text_buffer_get_end_iter(fndata->textbuf, &iter);
912 if((buf = icwcstombs(ires->argv[3].val.str, "UTF-8")) != NULL)
914 gtk_text_buffer_insert_with_tags_by_name(fndata->textbuf, &iter, "<", -1, "sender", NULL);
915 gtk_text_buffer_insert_with_tags_by_name(fndata->textbuf, &iter, buf, -1, "sender", NULL);
916 gtk_text_buffer_insert_with_tags_by_name(fndata->textbuf, &iter, ">", -1, "sender", NULL);
917 gtk_text_buffer_insert(fndata->textbuf, &iter, " ", -1);
920 if((buf = icwcstombs(ires->argv[4].val.str, "UTF-8")) != NULL)
922 gtk_text_buffer_insert(fndata->textbuf, &iter, buf, -1);
923 gtk_text_buffer_insert(fndata->textbuf, &iter, "\n", -1);
925 if(curchat == fn->id)
926 gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(main_chatview), &iter, 0, 0, 0, 0);
948 updatetransferlists();
951 if((ires = dc_interpret(resp)) != NULL)
953 if(ires->argv[0].val.num == nextsrch)
954 srcheta = time(NULL) + ires->argv[0].val.num;
959 if((ires = dc_interpret(resp)) != NULL)
961 if(ires->argv[0].val.num == nextsrch)
964 dc_queuecmd(NULL, NULL, L"cansrch", L"%%i", cursrch, NULL);
967 gtk_widget_set_sensitive(main_realsrch, TRUE);
968 gtk_widget_set_sensitive(main_simplesrch, TRUE);
969 gtk_widget_set_sensitive(main_srchbtn, TRUE);
970 gtk_widget_set_sensitive(main_srchcanbtn, FALSE);
972 gtk_entry_set_text(GTK_ENTRY(main_realsrch), "");
973 gtk_entry_set_text(GTK_ENTRY(main_simplesrch), "");
974 gtk_tree_store_clear(srchmodel);
981 if((ires = dc_interpret(resp)) != NULL)
983 if(ires->argv[0].val.num == cursrch)
985 for(i = 0; i < numsizes; i++)
987 if(srchsizes[i].size == ires->argv[4].val.num)
992 srchsizes = srealloc(srchsizes, sizeof(*srchsizes) * ++numsizes);
993 srchsizes[i].size = ires->argv[4].val.num;
994 srchsizes[i].num = 1;
995 srchsizes[i].slots = ires->argv[5].val.num;
996 srchsizes[i].resptime = ires->argv[7].val.flnum;
998 ss->slots += ires->argv[5].val.num;
999 if((ss->resptime == 0.0) || (ss->resptime > ires->argv[7].val.flnum))
1000 ss->resptime = ires->argv[7].val.flnum;
1001 piter = *ref2iter(ss->ref);
1002 gtk_tree_store_set(srchmodel, &piter, 5, ss->slots, 6, ss->resptime, -1);
1003 gtk_tree_store_append(srchmodel, &titer, &piter);
1004 srchsizes[i].ref = iter2ref(&titer);
1005 } else if(srchsizes[i].num == 1) {
1006 char *filename, *peername, *fnetname, *hash;
1010 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);
1011 gtk_tree_store_remove(srchmodel, ref2iter(srchsizes[i].ref));
1012 gtk_tree_row_reference_free(srchsizes[i].ref);
1013 ss = finddiscsize();
1015 gtk_tree_store_set(srchmodel, ref2iter(ss->ref), 5, ss->slots, -1);
1016 gtk_tree_store_append(srchmodel, &piter, NULL);
1017 srchsizes[i].slots = ires->argv[5].val.num + slots;
1018 srchsizes[i].resptime = (ires->argv[7].val.flnum < resptime)?ires->argv[7].val.flnum:resptime;
1019 srchsizes[i].num = 2;
1020 srchsizes[i].ref = iter2ref(&piter);
1021 gtk_tree_store_set(srchmodel, &piter, 4, srchsizes[i].size, 5, srchsizes[i].slots, 6, srchsizes[i].resptime, 7, 2, -1);
1022 if((buf = icwcstombs(ires->argv[1].val.str, "UTF-8")) != NULL)
1025 /* XXX: Too NMDC-specific! */
1026 if(strrchr(p, '\\') != NULL)
1027 p = strrchr(p, '\\') + 1;
1028 gtk_tree_store_set(srchmodel, &piter, 3, p, -1);
1031 gtk_tree_store_append(srchmodel, &titer, &piter);
1032 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);
1033 g_free(filename); g_free(peername); g_free(fnetname); g_free(hash);
1034 gtk_tree_store_append(srchmodel, &titer, &piter);
1037 srchsizes[i].slots += ires->argv[5].val.num;
1038 if(ires->argv[7].val.flnum < srchsizes[i].resptime)
1039 srchsizes[i].resptime = ires->argv[7].val.flnum;
1040 piter = *ref2iter(srchsizes[i].ref);
1041 gtk_tree_store_set(srchmodel, &piter, 5, srchsizes[i].slots, 6, srchsizes[i].resptime, 7, srchsizes[i].num, -1);
1042 gtk_tree_store_append(srchmodel, &titer, &piter);
1044 if((buf = icwcstombs(ires->argv[1].val.str, "UTF-8")) != NULL)
1046 gtk_tree_store_set(srchmodel, &titer, 3, buf, -1);
1049 if((buf = icwcstombs(ires->argv[2].val.str, "UTF-8")) != NULL)
1051 gtk_tree_store_set(srchmodel, &titer, 0, buf, -1);
1054 if((buf = icwcstombs(ires->argv[3].val.str, "UTF-8")) != NULL)
1056 gtk_tree_store_set(srchmodel, &titer, 1, buf, -1);
1057 gtk_tree_store_set(srchmodel, &titer, 2, buf, -1);
1060 if((buf = icwcstombs(ires->argv[8].val.str, "UTF-8")) != NULL)
1062 gtk_tree_store_set(srchmodel, &titer, 9, buf, -1);
1065 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);
1073 } else if(!wcscmp(resp->cmdname, L"filtercmd")) {
1074 if((ksquerytag >= 0) && (ksquerytag == resp->tag))
1076 for(i = 0; i < resp->numlines; i++)
1078 assert((ks = findksentbyseq(i)) != NULL);
1079 ks->speed = wcstol(resp->rlines[i].argv[1], NULL, 10);
1081 ks->fetched = time(NULL);
1092 void dcfdcallback(gpointer data, gint source, GdkInputCondition condition)
1096 if(((condition & GDK_INPUT_READ) && dc_handleread()) || ((condition & GDK_INPUT_WRITE) && dc_handlewrite()))
1102 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("The server has closed the connection"));
1104 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("The connection to the server failed:\n\n%s"), strerror(errnobak));
1111 void cb_main_dconnmenu_activate(GtkWidget *widget, gpointer data)
1119 void cb_main_prefmenu_activate(GtkWidget *widget, gpointer data)
1121 GtkWidget *dialog, *swnd;
1124 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);
1125 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), swnd = create_pref_wnd(), TRUE, TRUE, 0);
1126 gtk_entry_set_text(GTK_ENTRY(pref_pubhuburl), pubhubaddr);
1127 gtk_entry_set_text(GTK_ENTRY(pref_connectas), connectas);
1128 gtk_entry_set_text(GTK_ENTRY(pref_dcserver), dcserver);
1129 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(pref_autoconn), autoconn);
1130 gtk_widget_show(swnd);
1131 resp = gtk_dialog_run(GTK_DIALOG(dialog));
1132 if(resp == GTK_RESPONSE_ACCEPT)
1135 pubhubaddr = sstrdup(gtk_entry_get_text(GTK_ENTRY(pref_pubhuburl)));
1137 connectas = sstrdup(gtk_entry_get_text(GTK_ENTRY(pref_connectas)));
1139 dcserver = sstrdup(gtk_entry_get_text(GTK_ENTRY(pref_dcserver)));
1140 autoconn = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(pref_autoconn));
1142 gtk_widget_destroy(dialog);
1145 void dcconnect(char *host)
1147 dcfd = dc_connect(host, -1);
1150 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect:\n\n%s"), strerror(errno));
1153 gdkread = gdk_input_add(dcfd, GDK_INPUT_READ, dcfdcallback, NULL);
1155 gtk_widget_set_sensitive(main_connmenu, FALSE);
1156 gtk_widget_set_sensitive(main_dconnmenu, TRUE);
1157 updatesbar(_("Connecting..."));
1160 void cb_main_connmenu_activate(GtkWidget *widget, gpointer data)
1166 if((buf = inputbox(_("Connect"), _("Server address:"), dcserver, 1)) == NULL)
1172 void cb_main_sdmenu_activate(GtkWidget *widget, gpointer data)
1175 struct dc_response *resp;
1179 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1182 tag = dc_queuecmd(NULL, NULL, L"shutdown", NULL);
1183 if((resp = dc_gettaggedrespsync(tag)) != NULL)
1185 if(resp->code == 502)
1186 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1192 void cb_main_fnaddr_activate(GtkWidget *widget, gpointer data)
1195 struct dc_response *resp;
1200 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1203 toks = dc_lexsexpr(icsmbstowcs((char *)gtk_entry_get_text(GTK_ENTRY(main_fnaddr)), "UTF-8", NULL));
1206 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Illegal address entered"));
1209 if(wcschr(toks[0], L':') == NULL)
1211 toks[0] = srealloc(toks[0], (wcslen(toks[0]) + 5) * sizeof(wchar_t));
1212 wcscat(toks[0], L":411");
1214 tag = dc_queuecmd(NULL, NULL, L"cnct", L"dc", L"%%a", toks, NULL);
1215 dc_freewcsarr(toks);
1216 if((resp = dc_gettaggedrespsync(tag)) != NULL)
1218 if(resp->code == 502)
1219 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1220 if(resp->code == 509)
1221 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("The server could not parse that address"));
1224 gtk_entry_set_text(GTK_ENTRY(main_fnaddr), "");
1228 void pubhubfdcallback(gpointer data, gint source, GdkInputCondition condition)
1230 static char buf[65536];
1231 static int bufpos = 0;
1237 int sorted, sortcol;
1238 GtkSortType sortorder;
1239 GtkTreeModel *sortmodel;
1241 if(!(condition & GDK_INPUT_READ))
1245 ret = read(pubhubfd, buf + bufpos, sizeof(buf) - bufpos);
1249 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not read from public hub listing process: %s"), strerror(errno));
1251 gdk_input_remove(pubhubtag);
1252 kill(pubhubproc, SIGINT);
1253 pubhubfd = pubhubtag = -1;
1258 regfree(&pubhubfilter);
1264 sortmodel = gtk_tree_view_get_model(GTK_TREE_VIEW(main_phublist));
1265 sorted = gtk_tree_sortable_get_sort_column_id(GTK_TREE_SORTABLE(sortmodel), &sortcol, &sortorder);
1266 gtk_tree_view_set_model(GTK_TREE_VIEW(main_phublist), NULL);
1267 while((p = memchr(buf, '\n', bufpos)) != NULL)
1270 if(!filterpubhub || !regexec(&pubhubfilter, buf, 0, NULL, 0))
1273 for(i = 0; i < 4; i++)
1276 if((p2 = strchr(p2, '|')) == NULL)
1282 for(i = 0; i < 4; i++)
1284 if((wbuf = icsmbstowcs(fields[i], DCCHARSET, NULL)) == NULL)
1286 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not decode hublist - aborting at this point: %s"), strerror(errno));
1287 kill(pubhubproc, SIGINT);
1290 if((fields[i] = icwcstombs(wbuf, "UTF-8")) == NULL)
1295 gtk_list_store_append(pubhubmodel, &iter);
1296 gtk_list_store_set(pubhubmodel, &iter, 0, fields[0], 1, fields[1], 2, fields[2], 3, atoi(fields[3]), -1);
1298 for(i--; i >= 0; i--)
1302 memmove(buf, p, bufpos -= p - buf);
1304 sortmodel = gtk_tree_model_sort_new_with_model(GTK_TREE_MODEL(pubhubmodel));
1306 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(sortmodel), sortcol, sortorder);
1307 gtk_tree_view_set_model(GTK_TREE_VIEW(main_phublist), sortmodel);
1310 void cb_main_pubhubfilter_activate(GtkWidget *widget, gpointer data)
1312 int pipe1[2], pipe2[2];
1318 gdk_input_remove(pubhubtag);
1322 kill(pubhubproc, SIGINT);
1325 regfree(&pubhubfilter);
1328 buf = gtk_entry_get_text(GTK_ENTRY(main_pubhubfilter));
1331 if((err = regcomp(&pubhubfilter, buf, REG_EXTENDED | REG_ICASE | REG_NOSUB)) != 0)
1333 regerror(err, &pubhubfilter, errbuf, sizeof(errbuf));
1334 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "Could not compile regex: %s", errbuf);
1335 regfree(&pubhubfilter);
1341 gtk_list_store_clear(pubhubmodel);
1343 if((pubhubproc = fork()) == 0)
1348 execlp("wget", "wget", "-qO", "-", pubhubaddr, NULL);
1353 pubhubfd = pipe1[0];
1354 len = strlen(pubhubaddr);
1355 if((len > 4) && !strcmp(pubhubaddr + len - 4, ".bz2"))
1365 execlp("bzcat", "bzcat", NULL);
1371 pubhubfd = pipe2[0];
1373 pubhubtag = gdk_input_add(pubhubfd, GDK_INPUT_READ, pubhubfdcallback, NULL);
1376 void cb_main_dcnctbtn_clicked(GtkWidget *widget, gpointer data)
1380 struct dc_response *resp;
1384 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1387 if(!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(main_fnetnodes)), NULL, &iter))
1389 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("No hub selected"));
1392 gtk_tree_model_get(GTK_TREE_MODEL(fnmodel), &iter, 0, &id, -1);
1393 tag = dc_queuecmd(NULL, NULL, L"dcnct", L"%%i", id, NULL);
1394 if((resp = dc_gettaggedrespsync(tag)) != NULL)
1396 if(resp->code == 502)
1397 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1403 void cb_main_phublist_cchange(GtkWidget *widget, gpointer data)
1406 GtkTreeModel *model;
1409 if(!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(main_phublist)), &model, &iter))
1411 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, 1, &addr, -1);
1412 gtk_entry_set_text(GTK_ENTRY(main_fnaddr), addr);
1416 void cb_main_phublist_activate(GtkWidget *widget, GtkTreePath *path, GtkTreeViewColumn *col, gpointer data)
1419 struct dc_response *resp;
1421 GtkTreeModel *model;
1426 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1429 model = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
1430 if(!gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter, path))
1432 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, 1, &buf, -1);
1433 if(strchr(buf, ':') == NULL)
1435 buf = g_realloc(buf, strlen(buf) + 5);
1436 strcat(buf, ":411");
1438 tag = dc_queuecmd(NULL, NULL, L"cnct", L"dc", L"%%s", buf, NULL);
1440 gtk_entry_set_text(GTK_ENTRY(main_fnaddr), "");
1441 if((resp = dc_gettaggedrespsync(tag)) != NULL)
1443 if(resp->code == 502)
1444 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1445 if(resp->code == 509)
1446 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("The server could not parse that address"));
1452 void cb_main_chatnodes_activate(GtkWidget *widget, GtkTreePath *path, GtkTreeViewColumn *col, gpointer uudata)
1456 struct dc_fnetnode *fn;
1457 struct fndata *data;
1459 if(!gtk_tree_model_get_iter(GTK_TREE_MODEL(fnmodel), &iter, path))
1461 gtk_tree_model_get(GTK_TREE_MODEL(fnmodel), &iter, 0, &id, -1);
1462 if((fn = dc_findfnetnode(id)) == NULL)
1466 if(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(fnmodel), &iter))
1470 gtk_tree_model_get(GTK_TREE_MODEL(fnmodel), &iter, 0, &id, -1);
1472 gtk_list_store_set(fnmodel, &iter, 5, "gtk-apply", -1);
1474 gtk_list_store_set(fnmodel, &iter, 5, NULL, -1);
1475 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(fnmodel), &iter));
1477 gtk_text_view_set_buffer(GTK_TEXT_VIEW(main_chatview), GTK_TEXT_BUFFER(data->textbuf));
1480 void cb_main_chatstr_activate(GtkWidget *widget, gpointer data)
1484 struct dc_response *resp;
1488 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1493 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("No hub selected"));
1496 buf = gtk_entry_get_text(GTK_ENTRY(main_chatstr));
1497 tag = dc_queuecmd(NULL, NULL, L"sendchat", L"%%i", curchat, L"1", L"", L"%%s", buf, NULL);
1498 if((resp = dc_gettaggedrespsync(tag)) != NULL)
1500 if(resp->code == 502)
1501 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1502 else if(resp->code == 504)
1503 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("This hub could not support all the types of characters in your chat message"));
1504 else if(resp->code == 513)
1505 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("This hub does not support chatting"));
1506 else if(resp->code != 200)
1507 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("An error occurred while trying to chat (%i)"), resp->code);
1510 gtk_entry_set_text(GTK_ENTRY(main_chatstr), "");
1514 void updatesrchfld(const char *simple)
1518 size_t bufsize, bufdata;
1520 s = sstrdup(simple);
1522 bufsize = bufdata = 0;
1526 p2 = strchr(p, ' ');
1532 bufcat(buf, " & ", 3);
1533 bufcat(buf, "N~", 2);
1536 if(strchr("[]()$^.*?+\\|\"!", *p) != NULL)
1537 addtobuf(buf, '\\');
1542 } while(p2 != NULL);
1544 gtk_entry_set_text(GTK_ENTRY(main_realsrch), buf);
1549 void cb_main_simplesrch_changed(GtkWidget *widget, gpointer data)
1554 updatesrchfld(gtk_entry_get_text(GTK_ENTRY(main_simplesrch)));
1558 void cb_main_realsrch_changed(GtkWidget *widget, gpointer data)
1563 gtk_entry_set_text(GTK_ENTRY(main_simplesrch), "");
1567 void cb_main_srchbtn_clicked(GtkWidget *widget, gpointer data)
1571 struct dc_response *resp;
1572 struct dc_intresp *ires;
1576 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1579 if(nextsrch != -1) /* Impossible case, but oh well... */
1581 toks = dc_lexsexpr(icsmbstowcs((char *)gtk_entry_get_text(GTK_ENTRY(main_realsrch)), "UTF-8", NULL));
1584 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Please enter a search expression before searching"));
1587 tag = dc_queuecmd(NULL, NULL, L"search", L"all", L"%%a", toks, NULL);
1588 dc_freewcsarr(toks);
1589 if((resp = dc_gettaggedrespsync(tag)) != NULL)
1591 if(resp->code == 501)
1592 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("Could not find any hubs to search on"));
1593 else if(resp->code == 502)
1594 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1595 else if(resp->code == 509)
1596 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("The server could not parse your search expression"));
1597 else if(resp->code != 200)
1598 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("An error occurred while trying to search (%i)"), resp->code);
1599 if(resp->code == 200)
1601 if((ires = dc_interpret(resp)) != NULL)
1603 nextsrch = ires->argv[0].val.num;
1604 srcheta = time(NULL) + ires->argv[1].val.num;
1607 gtk_widget_set_sensitive(main_realsrch, FALSE);
1608 gtk_widget_set_sensitive(main_simplesrch, FALSE);
1609 gtk_widget_set_sensitive(main_srchbtn, FALSE);
1610 gtk_widget_set_sensitive(main_srchcanbtn, TRUE);
1618 void cb_main_srchcanbtn_clicked(GtkWidget *widget, gpointer data)
1622 dc_queuecmd(NULL, NULL, L"cansrch", L"%%i", nextsrch, NULL);
1624 gtk_widget_set_sensitive(main_realsrch, TRUE);
1625 gtk_widget_set_sensitive(main_simplesrch, TRUE);
1626 gtk_widget_set_sensitive(main_srchbtn, TRUE);
1627 gtk_widget_set_sensitive(main_srchcanbtn, FALSE);
1631 gboolean cb_main_trlist_keypress(GtkWidget *widget, GdkEventKey *event, gpointer data)
1634 GtkTreeSelection *sel;
1635 GtkTreeModel *model;
1637 struct dc_response *resp;
1639 if((event->type == GDK_KEY_PRESS) && (event->keyval == GDK_Delete))
1641 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
1642 if(gtk_tree_selection_get_selected(sel, &model, &iter))
1644 gtk_tree_model_get(model, &iter, 0, &id, -1);
1645 tag = dc_queuecmd(NULL, NULL, L"cancel", L"%%i", id, NULL);
1646 if((resp = dc_gettaggedrespsync(tag)) != NULL)
1648 if(resp->code == 502)
1649 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1650 else if(resp->code != 200)
1651 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("An error occurred while trying to cancel (%i)"), resp->code);
1661 void cb_main_srchres_activate(GtkWidget *widget, GtkTreePath *path, GtkTreeViewColumn *col, gpointer data)
1664 struct dc_response *resp;
1666 GtkTreeModel *model;
1668 char *tfnet, *tpeerid, *tfilename, *thash, *arg;
1669 wchar_t *fnet, *peerid, *filename, *hash;
1673 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Not connected to DC server"));
1676 model = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
1677 if(!gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter, path))
1679 gtk_tree_model_get(model, &iter, 7, &num, -1);
1682 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, 0, &tfnet, 1, &tpeerid, 3, &tfilename, 4, &size, 9, &thash, -1);
1683 fnet = icmbstowcs(tfnet, "UTF-8");
1684 peerid = icmbstowcs(tpeerid, "UTF-8");
1685 filename = icmbstowcs(tfilename, "UTF-8");
1686 hash = (thash == NULL)?NULL:icmbstowcs(thash, "UTF-8");
1687 if((fnet == NULL) || (peerid == NULL) || (filename == NULL))
1693 if(filename != NULL)
1707 arg = (char *)gtk_entry_get_text(GTK_ENTRY(main_dlarg));
1709 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);
1711 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);
1717 if((resp = dc_gettaggedrespsync(tag)) != NULL)
1719 if(resp->code == 502)
1720 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1721 if(resp->code != 200)
1722 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("An error occurred while trying to queue the download (%i)"), resp->code);
1728 gboolean srchfilterfunc(GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
1733 filteratall = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(main_filternoslots));
1736 gtk_tree_model_get(model, iter, 5, &slots, -1);
1742 void cb_main_filternoslots_toggled(GtkToggleButton *widget, gpointer data)
1744 gtk_tree_model_filter_refilter(srchmodelfilter);
1747 void cb_main_srhash_activate(GtkWidget *widget, gpointer data)
1749 GtkTreeSelection *sel;
1750 GtkTreeModel *model;
1756 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(main_srchres));
1757 if(gtk_tree_selection_get_selected(sel, &model, &iter))
1759 gtk_tree_model_get(model, &iter, 9, &hash, -1);
1760 buf = sprintf2("H=%s", hash);
1761 gtk_entry_set_text(GTK_ENTRY(main_realsrch), buf);
1764 cb_main_srchbtn_clicked(widget, NULL);
1770 void cb_main_trhash_activate(GtkWidget *widget, gpointer data)
1772 GtkTreeSelection *sel;
1773 GtkTreeModel *model;
1779 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(main_downloads));
1780 if(gtk_tree_selection_get_selected(sel, &model, &iter))
1782 gtk_tree_model_get(model, &iter, 12, &hash, -1);
1783 buf = sprintf2("H=%s", hash);
1784 gtk_entry_set_text(GTK_ENTRY(main_realsrch), buf);
1787 cb_main_srchbtn_clicked(widget, NULL);
1793 void cb_main_trcancel_activate(GtkWidget *widget, gpointer data)
1795 GtkTreeSelection *sel;
1796 GtkTreeModel *model;
1799 struct dc_response *resp;
1803 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(main_downloads));
1804 if(gtk_tree_selection_get_selected(sel, &model, &iter))
1806 gtk_tree_model_get(model, &iter, 0, &id, -1);
1807 tag = dc_queuecmd(NULL, NULL, L"cancel", L"%%i", id, NULL);
1808 if((resp = dc_gettaggedrespsync(tag)) != NULL)
1810 if(resp->code == 502)
1811 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("You do not have permission to do that"));
1812 else if(resp->code != 200)
1813 msgbox(GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, _("An error occurred while trying to cancel (%i)"), resp->code);
1822 /* XXX: This is quite a hack, since the calling convention is
1823 * different for the popup-menu sig and the button-press-event sig. It
1824 * most certainly works, but I don't know how portable it is. */
1825 gboolean cb_main_srpopup(GtkWidget *widget, GdkEventButton *event, gpointer data)
1827 GtkTreeSelection *sel;
1828 GtkTreeModel *model;
1832 if((event != NULL) && (event->button != 3))
1834 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
1835 if(gtk_tree_selection_get_selected(sel, &model, &iter))
1837 gtk_tree_model_get(model, &iter, 9, &hash, -1);
1838 if((nextsrch != -1) || (hash == NULL) || (*hash == 0))
1839 gtk_widget_set_sensitive(main_srhash, FALSE);
1841 gtk_widget_set_sensitive(main_srhash, TRUE);
1847 gtk_menu_popup(GTK_MENU(main_srpopup), NULL, NULL, NULL, NULL, 0, gtk_get_current_event_time());
1849 gtk_menu_popup(GTK_MENU(main_srpopup), NULL, NULL, NULL, NULL, event->button, event->time);
1853 /* The above hack note goes for this one too. */
1854 gboolean cb_main_trpopup(GtkWidget *widget, GdkEventButton *event, gpointer data)
1856 GtkTreeSelection *sel;
1857 GtkTreeModel *model;
1861 if((event != NULL) && (event->button != 3))
1863 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
1864 if(gtk_tree_selection_get_selected(sel, &model, &iter))
1866 gtk_tree_model_get(model, &iter, 12, &hash, -1);
1867 if((nextsrch != -1) || (hash == NULL) || (*hash == 0))
1868 gtk_widget_set_sensitive(main_trhash, FALSE);
1870 gtk_widget_set_sensitive(main_trhash, TRUE);
1876 gtk_menu_popup(GTK_MENU(main_trpopup), NULL, NULL, NULL, NULL, 0, gtk_get_current_event_time());
1878 gtk_menu_popup(GTK_MENU(main_trpopup), NULL, NULL, NULL, NULL, event->button, event->time);
1882 void srchstatupdate(void)
1888 snprintf(buf, 1024, _("Ready to search"));
1890 snprintf(buf, 1024, _("Search scheduled and will be submitted in %i seconds"), (int)(srcheta - time(NULL)));
1892 if(strcmp(gtk_label_get_text(GTK_LABEL(main_srchstatus)), buf))
1893 gtk_label_set_text(GTK_LABEL(main_srchstatus), buf);
1896 gint srchstatupdatecb(gpointer data)
1902 void initchattags(void)
1906 chattags = gtk_text_tag_table_new();
1907 tag = gtk_text_tag_new("sender");
1908 g_object_set(tag, "foreground", "blue", NULL);
1909 gtk_text_tag_table_add(chattags, tag);
1912 int main(int argc, char **argv)
1915 PangoFontDescription *monospacefont;
1916 GtkTreeModel *sortmodel;
1917 struct passwd *pwent;
1919 setlocale(LC_ALL, "");
1920 bindtextdomain(PACKAGE, LOCALEDIR);
1921 textdomain(PACKAGE);
1922 gtk_init(&argc, &argv);
1924 signal(SIGCHLD, SIG_IGN);
1925 pubhubaddr = sstrdup("http://www.neo-modus.com/PublicHubList.config");
1926 dcserver = sstrdup("localhost");
1927 if((pwent = getpwuid(getuid())) == NULL)
1929 fprintf(stderr, "could not get your passwd data");
1932 connectas = sstrdup(pwent->pw_name);
1933 wnd = create_main_wnd();
1936 fnmodel = gtk_list_store_new(6, G_TYPE_INT, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING, G_TYPE_STRING);
1937 gtk_tree_view_set_model(GTK_TREE_VIEW(main_fnetnodes), GTK_TREE_MODEL(fnmodel));
1938 gtk_tree_view_set_model(GTK_TREE_VIEW(main_chatnodes), GTK_TREE_MODEL(fnmodel));
1940 pubhubmodel = gtk_list_store_new(4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT);
1941 sortmodel = gtk_tree_model_sort_new_with_model(GTK_TREE_MODEL(pubhubmodel));
1942 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(sortmodel), 3, GTK_SORT_DESCENDING);
1943 gtk_tree_view_set_model(GTK_TREE_VIEW(main_phublist), GTK_TREE_MODEL(sortmodel));
1944 g_object_unref(sortmodel);
1946 dlmodel = gtk_list_store_new(13, G_TYPE_INT, /* id */
1947 G_TYPE_INT, /* dir */
1948 G_TYPE_INT, /* state */
1949 G_TYPE_STRING, /* peerid */
1950 G_TYPE_STRING, /* peernick */
1951 G_TYPE_STRING, /* path */
1952 G_TYPE_INT, /* size */
1953 G_TYPE_INT, /* curpos */
1954 G_TYPE_STRING, /* stock */
1955 G_TYPE_FLOAT, /* percentage */
1956 G_TYPE_INT, /* error */
1957 G_TYPE_INT, /* errortime */
1958 G_TYPE_STRING); /* hash */
1959 gtk_tree_view_set_model(GTK_TREE_VIEW(main_downloads), GTK_TREE_MODEL(dlmodel));
1961 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);
1962 gtk_tree_view_set_model(GTK_TREE_VIEW(main_uploads), GTK_TREE_MODEL(ulmodel));
1964 srchmodel = gtk_tree_store_new(10, G_TYPE_STRING, /* fnetname */
1965 G_TYPE_STRING, /* peerid */
1966 G_TYPE_STRING, /* peername */
1967 G_TYPE_STRING, /* filename */
1968 G_TYPE_INT, /* size */
1969 G_TYPE_INT, /* slots */
1970 G_TYPE_DOUBLE, /* resptime */
1971 G_TYPE_INT, /* sizenum */
1972 G_TYPE_INT, /* speed */
1973 G_TYPE_STRING); /* hash */
1974 srchmodelfilter = GTK_TREE_MODEL_FILTER(gtk_tree_model_filter_new(GTK_TREE_MODEL(srchmodel), NULL));
1975 gtk_tree_model_filter_set_visible_func(srchmodelfilter, srchfilterfunc, NULL, NULL);
1976 sortmodel = gtk_tree_model_sort_new_with_model(GTK_TREE_MODEL(srchmodelfilter));
1977 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(sortmodel), 4, GTK_SORT_DESCENDING);
1978 gtk_tree_view_set_model(GTK_TREE_VIEW(main_srchres), GTK_TREE_MODEL(sortmodel));
1979 g_object_unref(sortmodel);
1981 monospacefont = pango_font_description_from_string("Monospace 10");
1982 gtk_widget_modify_font(main_chatview, monospacefont);
1983 pango_font_description_free(monospacefont);
1985 updatesbar(_("Disconnected"));
1986 gtk_widget_show(wnd);
1988 dcconnect(dcserver);
1989 g_timeout_add(500, srchstatupdatecb, NULL);
1990 g_timeout_add(5000, ksupdatecb, NULL);