Fixed transfer stall detecion in dsh.
[doldaconnect.git] / clients / gui-shell / dsh.c
1 /*
2  *  Dolda Connect - Modular multiuser Direct Connect-style client
3  *  Copyright (C) 2007 Fredrik Tolf <fredrik@dolda2000.com>
4  *  
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *  
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *  
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <pwd.h>
26 #include <locale.h>
27 #include <libintl.h>
28 #include <signal.h>
29 #include <sys/wait.h>
30 #include <gtk/gtk.h>
31 #include <gdk/gdkkeysyms.h>
32 #include <stdarg.h>
33 #include <doldaconnect/uilib.h>
34 #include <doldaconnect/uimisc.h>
35 #include <doldaconnect/utils.h>
36
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #ifdef HAVE_NOTIFY
42 #include <libnotify/notify.h>
43 #endif
44
45 #define _(text) gettext(text)
46
47 struct trinfo {
48     int ostate;
49     int opos, spos, speed;
50     time_t lastprog;
51     int warned;
52     double sprog;
53 };
54
55 void updatewrite(void);
56
57 int remote = 0;
58 GtkStatusIcon *tray;
59 pid_t dpid = 0, dcpid = 0;
60 int connected = 0;
61 int dstat;
62 int derrfd, derrtag;
63 char *derrbuf = NULL;
64 size_t derrbufsize = 0, derrbufdata = 0;
65 int dcfd, dcfdrtag, dcfdwtag = -1;
66 GdkPixbuf *dcicon;
67 #ifdef HAVE_NOTIFY
68 NotifyNotification *trnote = NULL;
69 #endif
70
71 #include "dsh-start.gtkh"
72 #include "dsh-menu.gtkh"
73
74 int running(char *pf)
75 {
76     FILE *pfs;
77     char buf[1024];
78     int pid;
79     
80     if((pfs = fopen(pf, "r")) == NULL) {
81         perror(pf);
82         return(0);
83     }
84     fgets(buf, sizeof(buf), pfs);
85     fclose(pfs);
86     if((pid = atoi(buf)) == 0)
87         return(0);
88     return(!kill(pid, 0));
89 }
90
91 void derrcb(gpointer data, gint source, GdkInputCondition cond)
92 {
93     int ret = 0;
94     
95     sizebuf2(derrbuf, derrbufdata + 1024, 1);
96     ret = read(derrfd, derrbuf + derrbufdata, derrbufsize - derrbufdata);
97     if(ret <= 0) {
98         if(ret < 0)
99             bprintf(derrbuf, "\ncould not read from daemon: %s\n", strerror(errno));
100         gdk_input_remove(derrtag);
101         close(derrfd);
102         derrfd = -1;
103     } else {
104         derrbufdata += ret;
105     }
106 }
107
108 int msgbox(int type, int buttons, char *format, ...)
109 {
110     GtkWidget *swnd;
111     va_list args;
112     char *buf;
113     int resp;
114     
115     va_start(args, format);
116     buf = vsprintf2(format, args);
117     va_end(args);
118     swnd = gtk_message_dialog_new(NULL, 0, type, buttons, "%s", buf);
119     resp = gtk_dialog_run(GTK_DIALOG(swnd));
120     gtk_widget_destroy(swnd);
121     free(buf);
122     return(resp);
123 }
124
125 void destroytr(struct dc_transfer *tr)
126 {
127     struct trinfo *tri;
128     
129     tri = tr->udata;
130     free(tri);
131 }
132
133 void inittr(struct dc_transfer *tr)
134 {
135     struct trinfo *tri;
136     
137     tr->udata = tri = memset(smalloc(sizeof(*tri)), 0, sizeof(*tri));
138     tr->destroycb = destroytr;
139     tri->ostate = tr->state;
140     tri->spos = tri->opos = tr->curpos;
141     tri->speed = -1;
142     tri->lastprog = time(NULL);
143     tri->sprog = ntime();
144 }
145
146 #ifdef HAVE_NOTIFY
147 void notify(NotifyNotification **n, char *cat, char *title, char *body, ...)
148 {
149     va_list args;
150     char *bbuf;
151     
152     va_start(args, body);
153     bbuf = vsprintf2(body, args);
154     va_end(args);
155     if(*n == NULL) {
156         *n = notify_notification_new_with_status_icon(title, bbuf, NULL, tray);
157         notify_notification_set_icon_from_pixbuf(*n, dcicon);
158     } else {
159         notify_notification_update(*n, title, bbuf, NULL);
160     }
161     notify_notification_show(*n, NULL);
162 }
163 #endif
164
165 /* XXX: Achtung! Too DC-specific! */
166 wchar_t *getfilename(wchar_t *path)
167 {
168     wchar_t *p;
169     
170     if((p = wcsrchr(path, L'\\')) == NULL)
171         return(path);
172     else
173         return(p + 1);
174 }
175
176 char *bytes2si(long long bytes)
177 {
178     int i;
179     double b;
180     char *sd;
181     static char ret[64];
182     
183     b = bytes;
184     for(i = 0; (b >= 1024) && (i < 4); i++)
185         b /= 1024;
186     if(i == 0)
187         sd = "B";
188     else if(i == 1)
189         sd = "kiB";
190     else if(i == 2)
191         sd = "MiB";
192     else if(i == 3)
193         sd = "GiB";
194     else
195         sd = "TiB";
196     snprintf(ret, 64, "%.1f %s", b, sd);
197     return(ret);
198 }
199
200 void updatetooltip(void)
201 {
202     struct dc_transfer *tr;
203     struct trinfo *tri;
204     int t, i, a, st, bc, bt;
205     char *buf;
206     size_t bufsize, bufdata;
207     
208     t = i = a = 0;
209     st = bc = bt = -1;
210     for(tr = dc_transfers; tr != NULL; tr = tr->next) {
211         if(tr->dir != DC_TRNSD_DOWN)
212             continue;
213         tri = tr->udata;
214         t++;
215         if(tr->state == DC_TRNS_WAITING)
216             i++;
217         else if((tr->state == DC_TRNS_HS) || (tr->state == DC_TRNS_MAIN))
218             a++;
219         if((tr->state == DC_TRNS_MAIN)) {
220             if(bt == -1)
221                 bc = bt = 0;
222             bc += tr->curpos;
223             bt += tr->size;
224             if(tri->speed != -1) {
225                 if(st == -1)
226                     st = 0;
227                 st += tri->speed;
228             }
229         }
230     }
231     buf = NULL;
232     bufsize = bufdata = 0;
233     bprintf(buf, "%s: %i", _("Transfers"), t);
234     if(t > 0)
235         bprintf(buf, " (%i/%i)", i, a);
236     if(bt > 0)
237         bprintf(buf, ", %.1f%%", ((double)bc / (double)bt) * 100.0);
238     if(st != -1)
239         bprintf(buf, ", %s/s", bytes2si(st));
240     addtobuf(buf, 0);
241     gtk_status_icon_set_tooltip(tray, buf);
242     free(buf);
243 }
244
245 void trstatechange(struct dc_transfer *tr, int ostate)
246 {
247     struct trinfo *tri;
248     
249     tri = tr->udata;
250     if((ostate == DC_TRNS_MAIN) && (tr->dir == DC_TRNSD_DOWN)) {
251         if(tr->state == DC_TRNS_DONE) {
252 #ifdef HAVE_NOTIFY
253             if(dcpid == 0)
254                 notify(&trnote, "transfer.complete", _("Transfer complete"), _("Finished downloading %ls from %ls"), getfilename(tr->path), tr->peernick);
255 #endif
256         } else {
257 #ifdef HAVE_NOTIFY
258             if(dcpid == 0)
259                 notify(&trnote, "transfer.error", _("Transfer interrupted"), _("The transfer of %ls from %ls was interrupted from the other side"), getfilename(tr->path), tr->peernick);
260 #endif
261         }
262     }
263     if(tr->state == DC_TRNS_MAIN) {
264         tri->speed = -1;
265         tri->spos = tr->curpos;
266         tri->sprog = ntime();
267     }
268 }
269
270 void updatetrinfo(void)
271 {
272     struct dc_transfer *tr;
273     struct trinfo *tri;
274     time_t now;
275     double dnow;
276     
277     now = time(NULL);
278     dnow = ntime();
279     for(tr = dc_transfers; tr != NULL; tr = tr->next) {
280         if(tr->udata == NULL) {
281             inittr(tr);
282         } else {
283             tri = tr->udata;
284             if(tri->ostate != tr->state) {
285                 trstatechange(tr, tri->ostate);
286                 tri->ostate = tr->state;
287             }
288             if(tri->opos != tr->curpos) {
289                 tri->opos = tr->curpos;
290                 tri->lastprog = now;
291                 tri->warned = 0;
292             }
293 #ifdef HAVE_NOTIFY
294             if((tr->state = DC_TRNS_MAIN) && (now - tri->lastprog > 600) && !tri->warned) {
295                 if(dcpid == 0) {
296                     notify(&trnote, "transfer.error", _("Transfer stalled"), _("The transfer of %ls from %ls has not made progress for 10 minutes"), getfilename(tr->path), tr->peernick);
297                     tri->warned = 1;
298                 }
299             }
300 #endif
301             if((tr->state == DC_TRNS_MAIN) && (dnow - tri->sprog > 10)) {
302                 tri->speed = ((double)(tr->curpos - tri->spos) / (dnow - tri->sprog));
303                 tri->spos = tr->curpos;
304                 tri->sprog = dnow;
305             }
306         }
307     }
308     updatetooltip();
309 }
310
311 void trlscb(int resp, void *data)
312 {
313     updatetrinfo();
314 }
315
316 gint trupdatecb(gpointer data)
317 {
318     updatetrinfo();
319     return(TRUE);
320 }
321
322 void logincb(int err, wchar_t *reason, void *data)
323 {
324     if(err != DC_LOGIN_ERR_SUCCESS) {
325         msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server"));
326         exit(1);
327     }
328     dc_queuecmd(NULL, NULL, L"notify", L"trans:act", L"on", L"trans:prog", L"on", NULL);
329     dc_gettrlistasync(trlscb, NULL);
330     connected = 1;
331     updatewrite();
332 }
333
334 void dcfdcb(gpointer data, gint source, GdkInputCondition cond)
335 {
336     struct dc_response *resp;
337     
338     if(((cond & GDK_INPUT_READ) && dc_handleread()) || ((cond & GDK_INPUT_WRITE) && dc_handlewrite())) {
339         if(errno == 0) {
340             gtk_main_quit();
341         } else {
342             msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server: %s"), strerror(errno));
343             exit(1);
344         }
345         return;
346     }
347     while((resp = dc_getresp()) != NULL) {
348         if(!wcscmp(resp->cmdname, L".connect")) {
349             if(resp->code != 201) {
350                 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("The server refused the connection"));
351                 exit(1);
352             } else if(dc_checkprotocol(resp, DC_LATEST)) {
353                 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Server protocol revision mismatch"));
354                 exit(1);
355             } else {
356                 gtk_status_icon_set_tooltip(tray, _("Authenticating..."));
357                 dc_loginasync(NULL, 1, dc_convnone, logincb, NULL);
358             }
359         } else if(!wcscmp(resp->cmdname, L".notify")) {
360             dc_uimisc_handlenotify(resp);
361             updatetrinfo();
362         }
363         dc_freeresp(resp);
364     }
365     updatewrite();
366 }
367
368 void updatewrite(void)
369 {
370     if(dcfd < 0)
371         return;
372     if(dc_wantwrite()) {
373         if(dcfdwtag == -1)
374             dcfdwtag = gdk_input_add(dcfd, GDK_INPUT_WRITE, dcfdcb, NULL);
375     } else {
376         if(dcfdwtag != -1) {
377             gdk_input_remove(dcfdwtag);
378             dcfdwtag = -1;
379         }
380     }
381 }
382
383 void connectdc(void)
384 {
385     if((dcfd = dc_connect(remote?NULL:dc_srv_local)) < 0) {
386         msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server: %s"), strerror(errno));
387         exit(1);
388     }
389     dcfdrtag = gdk_input_add(dcfd, GDK_INPUT_READ, dcfdcb, NULL);
390     updatewrite();
391     gtk_status_icon_set_tooltip(tray, _("Connecting to server..."));
392 }
393
394 void startdaemon(void)
395 {
396     char pf[1024];
397     int pfd[2], i;
398     
399     if(getenv("HOME") != NULL)
400         snprintf(pf, sizeof(pf), "%s/.doldacond.pid", getenv("HOME"));
401     else
402         snprintf(pf, sizeof(pf), "%s/.doldacond.pid", getpwuid(getuid())->pw_dir);
403     if(access(pf, F_OK) || !running(pf)) {
404         pipe(pfd);
405         if((dpid = fork()) == 0) {
406             dup2(pfd[1], 2);
407             for(i = 3; i < FD_SETSIZE; i++)
408                 close(i);
409             execlp("doldacond", "doldacond", "-p", pf, NULL);
410             perror("doldacond");
411             exit(127);
412         }
413         if(dpid == -1)
414             abort();
415         close(pfd[1]);
416         derrfd = pfd[0];
417         derrtag = gdk_input_add(derrfd, GDK_INPUT_READ, derrcb, NULL);
418         create_start_wnd();
419         gtk_widget_show(start_wnd);
420         gtk_status_icon_set_tooltip(tray, _("Starting..."));
421     } else {
422         connectdc();
423     }
424 }
425
426 gboolean daemonized(gpointer uu)
427 {
428     gtk_widget_hide(start_wnd);
429     dpid = 0;
430     if(derrfd != -1) {
431         gdk_input_remove(derrtag);
432         close(derrfd);
433     }
434     if(dstat != 0) {
435         gtk_status_icon_set_visible(tray, FALSE);
436         gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(start_log)), derrbuf, derrbufdata);
437         gtk_widget_show(start_errwnd);
438     } else {
439         connectdc();
440     }
441     return(FALSE);
442 }
443
444 void sighandler(int sig)
445 {
446     pid_t p;
447     int status;
448     
449     if(sig == SIGCHLD) {
450         while((p = waitpid(-1, &status, WNOHANG)) > 0) {
451             if(p == dpid) {
452                 dstat = status;
453                 gtk_timeout_add(1, daemonized, NULL);
454             } else if(p == dcpid) {
455                 dcpid = 0;
456             }
457         }
458     }
459 }
460
461 void dolcon(void)
462 {
463     int i;
464     
465     if((dcpid = fork()) == 0) {
466         for(i = 3; i < FD_SETSIZE; i++)
467             close(i);
468         if(remote)
469             execlp("dolcon", "dolcon", NULL);
470         else
471             execlp("dolcon", "dolcon", "-l", NULL);
472         perror("dolcon");
473         exit(127);
474     }
475 }
476
477 void cb_shm_dolconf_activate(GtkWidget *uu1, gpointer uu2)
478 {
479     int i;
480     
481     if((dcpid = fork()) == 0) {
482         for(i = 3; i < FD_SETSIZE; i++)
483             close(i);
484         execlp("dolconf", "dolconf", NULL);
485         perror("dolconf");
486         exit(127);
487     }
488 }
489
490 void cb_shm_dolcon_activate(GtkWidget *uu1, gpointer uu2)
491 {
492     dolcon();
493 }
494
495 void cb_shm_quit_activate(GtkWidget *uu1, gpointer uu2)
496 {
497     dc_queuecmd(NULL, NULL, L"shutdown", NULL);
498     updatewrite();
499 }
500
501 void tray_activate(GtkStatusIcon *uu1, gpointer uu2)
502 {
503     if(dpid != 0) {
504         gtk_widget_show(start_wnd);
505     } else if(connected) {
506         dolcon();
507     }
508 }
509
510 void tray_popup(GtkStatusIcon *uu1, guint button, guint time, gpointer uu2)
511 {
512     gtk_menu_popup(GTK_MENU(shm_menu), NULL, NULL, NULL, NULL, button, time);
513 }
514
515 void cb_start_hide_clicked(GtkWidget *uu1, gpointer uu2)
516 {
517     gtk_widget_hide(start_wnd);
518 }
519
520 void cb_start_abort_clicked(GtkWidget *uu1, gpointer uu2)
521 {
522     kill(dpid, SIGINT);
523     exit(0);
524 }
525
526 void cb_start_errok_clicked(GtkWidget *uu1, gpointer uu2)
527 {
528     gtk_main_quit();
529 }
530
531 #include "../dolda-icon.xpm"
532
533 void inittray(void)
534 {
535     tray = gtk_status_icon_new_from_pixbuf(gdk_pixbuf_scale_simple(dcicon, 24, 24, GDK_INTERP_BILINEAR));
536     gtk_status_icon_set_tooltip(tray, "");
537     g_signal_connect(G_OBJECT(tray), "activate", G_CALLBACK(tray_activate), (gpointer)NULL);
538     g_signal_connect(G_OBJECT(tray), "popup-menu", G_CALLBACK(tray_popup), (gpointer)NULL);
539 }
540
541 int main(int argc, char **argv)
542 {
543     int c;
544     
545     setlocale(LC_ALL, "");
546     bindtextdomain(PACKAGE, LOCALEDIR);
547     textdomain(PACKAGE);
548     signal(SIGCHLD, sighandler);
549     dc_init();
550     gtk_init(&argc, &argv);
551 #ifdef HAVE_NOTIFY
552     notify_init("Dolda Connect");
553 #endif
554     while((c = getopt(argc, argv, "rh")) != -1) {
555         switch(c) {
556         case 'r':
557             remote = 1;
558             break;
559         case 'h':
560             printf("usage: doldacond-shell [-hr]\n");
561             printf("\t-h\tDisplay this help message\n");
562             printf("\t-r\tConnect to a remote host\n");
563             exit(0);
564         default:
565             fprintf(stderr, "usage: doldacond-shell [-hr]\n");
566             exit(1);
567         }
568     }
569
570     create_shm_wnd();
571     dcicon = gdk_pixbuf_new_from_xpm_data((const char **)dolda_icon_xpm);
572     gtk_window_set_default_icon(dcicon);
573     inittray();
574     if(remote)
575         connectdc();
576     else
577         startdaemon();
578     
579     g_timeout_add(10000, trupdatecb, NULL);
580     gtk_main();
581
582     return(0);
583 }
584
585 #include "dsh-start.gtk"
586 #include "dsh-menu.gtk"