Attempt to monitor stalled transfers.
[doldaconnect.git] / clients / gui-shell / dsh.c
CommitLineData
34d45a15
FT
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
47struct trinfo {
48 int ostate;
fa9a8ac1
FT
49 int opos;
50 time_t lastprog;
34d45a15
FT
51};
52
53void updatewrite(void);
54
71aeadfc 55int remote = 0;
34d45a15
FT
56GtkStatusIcon *tray;
57pid_t dpid = 0, dcpid = 0;
58int connected = 0;
59int dstat;
60int derrfd, derrtag;
61char *derrbuf = NULL;
62size_t derrbufsize = 0, derrbufdata = 0;
63int dcfd, dcfdrtag, dcfdwtag = -1;
64GdkPixbuf *dcicon;
65#ifdef HAVE_NOTIFY
66NotifyNotification *trnote = NULL;
67#endif
68
69#include "dsh-start.gtkh"
70#include "dsh-menu.gtkh"
71
72int running(char *pf)
73{
74 FILE *pfs;
75 char buf[1024];
76 int pid;
77
78 if((pfs = fopen(pf, "r")) == NULL) {
79 perror(pf);
80 return(0);
81 }
82 fgets(buf, sizeof(buf), pfs);
83 fclose(pfs);
84 if((pid = atoi(buf)) == 0)
85 return(0);
86 return(!kill(pid, 0));
87}
88
89void derrcb(gpointer data, gint source, GdkInputCondition cond)
90{
91 int ret = 0;
92
93 sizebuf2(derrbuf, derrbufdata + 1024, 1);
94 ret = read(derrfd, derrbuf + derrbufdata, derrbufsize - derrbufdata);
95 if(ret <= 0) {
96 if(ret < 0)
97 bprintf(derrbuf, "\ncould not read from daemon: %s\n", strerror(errno));
98 gdk_input_remove(derrtag);
99 close(derrfd);
100 derrfd = -1;
101 } else {
102 derrbufdata += ret;
103 }
104}
105
106int msgbox(int type, int buttons, char *format, ...)
107{
108 GtkWidget *swnd;
109 va_list args;
110 char *buf;
111 int resp;
112
113 va_start(args, format);
114 buf = vsprintf2(format, args);
115 va_end(args);
116 swnd = gtk_message_dialog_new(NULL, 0, type, buttons, "%s", buf);
117 resp = gtk_dialog_run(GTK_DIALOG(swnd));
118 gtk_widget_destroy(swnd);
119 free(buf);
120 return(resp);
121}
122
123void destroytr(struct dc_transfer *tr)
124{
125 struct trinfo *tri;
126
127 tri = tr->udata;
128 free(tri);
129}
130
131void inittr(struct dc_transfer *tr)
132{
133 struct trinfo *tri;
134
135 tr->udata = tri = memset(smalloc(sizeof(*tri)), 0, sizeof(*tri));
136 tr->destroycb = destroytr;
137 tri->ostate = tr->state;
fa9a8ac1
FT
138 tri->opos = tr->curpos;
139 tri->lastprog = time(NULL);
34d45a15
FT
140}
141
142#ifdef HAVE_NOTIFY
143void notify(NotifyNotification **n, char *cat, char *title, char *body, ...)
144{
145 va_list args;
146 char *bbuf;
147
148 va_start(args, body);
149 bbuf = vsprintf2(body, args);
150 va_end(args);
151 if(*n == NULL) {
152 *n = notify_notification_new_with_status_icon(title, bbuf, NULL, tray);
153 notify_notification_set_icon_from_pixbuf(*n, dcicon);
154 } else {
155 notify_notification_update(*n, title, bbuf, NULL);
156 }
157 notify_notification_show(*n, NULL);
158}
159#endif
160
fa9a8ac1
FT
161/* XXX: Achtung! Too DC-specific! */
162wchar_t *getfilename(wchar_t *path)
163{
164 wchar_t *p;
165
166 if((p = wcsrchr(path, L'\\')) == NULL)
167 return(path);
168 else
169 return(p + 1);
170}
171
34d45a15
FT
172void trstatechange(struct dc_transfer *tr, int ostate)
173{
fa9a8ac1 174 if((ostate == DC_TRNS_MAIN) && (tr->dir == DC_TRNSD_DOWN)) {
34d45a15
FT
175 if(tr->state == DC_TRNS_DONE) {
176#ifdef HAVE_NOTIFY
fa9a8ac1 177 notify(&trnote, "transfer.complete", _("Transfer complete"), _("Finished downloading %ls from %ls"), getfilename(tr->path), tr->peernick);
34d45a15
FT
178#endif
179 } else {
180#ifdef HAVE_NOTIFY
fa9a8ac1 181 notify(&trnote, "transfer.error", _("Transfer interrupted"), _("The transfer of %ls from %ls was interrupted from the other side"), getfilename(tr->path), tr->peernick);
34d45a15
FT
182#endif
183 }
184 }
185}
186
187void updatetrinfo(void)
188{
189 struct dc_transfer *tr;
190 struct trinfo *tri;
fa9a8ac1 191 time_t now;
34d45a15 192
fa9a8ac1 193 now = time(NULL);
34d45a15
FT
194 for(tr = dc_transfers; tr != NULL; tr = tr->next) {
195 if(tr->udata == NULL) {
196 inittr(tr);
197 } else {
198 tri = tr->udata;
199 if(tri->ostate != tr->state) {
200 trstatechange(tr, tri->ostate);
201 tri->ostate = tr->state;
202 }
fa9a8ac1
FT
203 if(tri->opos != tr->curpos) {
204 tri->opos = tr->curpos;
205 tri->lastprog = now;
206 }
207#ifdef NOTIFY
208 if((tr->state = DC_TRNS_MAIN) && (now - tri->lastprog > 600)) {
209 notify(&trnote, "transfer.error", _("Transfer stalled"), _("The transfer of %ls from %ls has not made progress for 10 minutes"), getfilename(tr->path), tr->peernick);
210 }
211#endif
34d45a15
FT
212 }
213 }
214}
215
216void trlscb(int resp, void *data)
217{
218 updatetrinfo();
219}
220
221void logincb(int err, wchar_t *reason, void *data)
222{
223 if(err != DC_LOGIN_ERR_SUCCESS) {
224 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server"));
225 exit(1);
226 }
227 gtk_status_icon_set_tooltip(tray, "Dolda Connect");
228 dc_queuecmd(NULL, NULL, L"notify", L"trans:act", L"on", L"trans:prog", L"on", NULL);
229 dc_gettrlistasync(trlscb, NULL);
230 connected = 1;
231 updatewrite();
232}
233
234void dcfdcb(gpointer data, gint source, GdkInputCondition cond)
235{
236 struct dc_response *resp;
237
238 if(((cond & GDK_INPUT_READ) && dc_handleread()) || ((cond & GDK_INPUT_WRITE) && dc_handlewrite())) {
239 if(errno == 0) {
240 gtk_main_quit();
241 } else {
242 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server: %s"), strerror(errno));
243 exit(1);
244 }
245 return;
246 }
247 while((resp = dc_getresp()) != NULL) {
248 if(!wcscmp(resp->cmdname, L".connect")) {
249 if(resp->code != 201) {
250 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("The server refused the connection"));
251 exit(1);
252 } else if(dc_checkprotocol(resp, DC_LATEST)) {
253 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Server protocol revision mismatch"));
254 exit(1);
255 } else {
256 gtk_status_icon_set_tooltip(tray, _("Authenticating..."));
257 dc_loginasync(NULL, 1, dc_convnone, logincb, NULL);
258 }
259 } else if(!wcscmp(resp->cmdname, L".notify")) {
260 dc_uimisc_handlenotify(resp);
261 updatetrinfo();
262 }
263 }
264 updatewrite();
265}
266
267void updatewrite(void)
268{
269 if(dcfd < 0)
270 return;
271 if(dc_wantwrite()) {
272 if(dcfdwtag == -1)
273 dcfdwtag = gdk_input_add(dcfd, GDK_INPUT_WRITE, dcfdcb, NULL);
274 } else {
275 if(dcfdwtag != -1) {
276 gdk_input_remove(dcfdwtag);
277 dcfdwtag = -1;
278 }
279 }
280}
281
282void connectdc(void)
283{
71aeadfc 284 if((dcfd = dc_connect(remote?NULL:dc_srv_local)) < 0) {
34d45a15
FT
285 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server: %s"), strerror(errno));
286 exit(1);
287 }
288 dcfdrtag = gdk_input_add(dcfd, GDK_INPUT_READ, dcfdcb, NULL);
289 updatewrite();
290 gtk_status_icon_set_tooltip(tray, _("Connecting to server..."));
291}
292
293void startdaemon(void)
294{
295 char pf[1024];
296 int pfd[2], i;
297
298 if(getenv("HOME") != NULL)
299 snprintf(pf, sizeof(pf), "%s/.doldacond.pid", getenv("HOME"));
300 else
301 snprintf(pf, sizeof(pf), "%s/.doldacond.pid", getpwuid(getuid())->pw_dir);
302 if(access(pf, F_OK) || !running(pf)) {
303 pipe(pfd);
304 if((dpid = fork()) == 0) {
305 dup2(pfd[1], 2);
306 for(i = 3; i < FD_SETSIZE; i++)
307 close(i);
308 execlp("doldacond", "doldacond", "-p", pf, NULL);
309 perror("doldacond");
310 exit(127);
311 }
312 if(dpid == -1)
313 abort();
314 close(pfd[1]);
315 derrfd = pfd[0];
316 derrtag = gdk_input_add(derrfd, GDK_INPUT_READ, derrcb, NULL);
317 create_start_wnd();
318 gtk_widget_show(start_wnd);
319 gtk_status_icon_set_tooltip(tray, _("Starting..."));
320 } else {
321 connectdc();
322 }
323}
324
325gboolean daemonized(gpointer uu)
326{
327 gtk_widget_hide(start_wnd);
328 dpid = 0;
329 if(derrfd != -1) {
330 gdk_input_remove(derrtag);
331 close(derrfd);
332 }
333 if(dstat != 0) {
334 gtk_status_icon_set_visible(tray, FALSE);
335 gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(start_log)), derrbuf, derrbufdata);
336 gtk_widget_show(start_errwnd);
337 } else {
338 connectdc();
339 }
340 return(FALSE);
341}
342
343void sighandler(int sig)
344{
345 pid_t p;
346 int status;
347
348 if(sig == SIGCHLD) {
349 while((p = waitpid(-1, &status, WNOHANG)) > 0) {
350 if(p == dpid) {
351 dstat = status;
352 gtk_timeout_add(1, daemonized, NULL);
353 } else if(p == dcpid) {
354 dcpid = 0;
355 }
356 }
357 }
358}
359
360void dolcon(void)
361{
362 int i;
363
364 if((dcpid = fork()) == 0) {
365 for(i = 3; i < FD_SETSIZE; i++)
366 close(i);
71aeadfc
FT
367 if(remote)
368 execlp("dolcon", "dolcon", NULL);
369 else
370 execlp("dolcon", "dolcon", "-l", NULL);
34d45a15
FT
371 perror("dolcon");
372 exit(127);
373 }
374}
375
376void cb_shm_dolcon_activate(GtkWidget *uu1, gpointer uu2)
377{
378 dolcon();
379}
380
381void cb_shm_quit_activate(GtkWidget *uu1, gpointer uu2)
382{
383 dc_queuecmd(NULL, NULL, L"shutdown", NULL);
384 updatewrite();
385}
386
387void tray_activate(GtkStatusIcon *uu1, gpointer uu2)
388{
389 if(dpid != 0) {
390 gtk_widget_show(start_wnd);
391 } else if(connected) {
392 dolcon();
393 }
394}
395
396void tray_popup(GtkStatusIcon *uu1, guint button, guint time, gpointer uu2)
397{
398 gtk_menu_popup(GTK_MENU(shm_menu), NULL, NULL, NULL, NULL, button, time);
399}
400
401void cb_start_hide_clicked(GtkWidget *uu1, gpointer uu2)
402{
403 gtk_widget_hide(start_wnd);
404}
405
406void cb_start_abort_clicked(GtkWidget *uu1, gpointer uu2)
407{
408 kill(dpid, SIGINT);
409 exit(0);
410}
411
412void cb_start_errok_clicked(GtkWidget *uu1, gpointer uu2)
413{
414 gtk_main_quit();
415}
416
417#include "../dolda-icon.xpm"
418
419void inittray(void)
420{
421 tray = gtk_status_icon_new_from_pixbuf(gdk_pixbuf_scale_simple(dcicon, 24, 24, GDK_INTERP_BILINEAR));
422 gtk_status_icon_set_tooltip(tray, "");
423 g_signal_connect(G_OBJECT(tray), "activate", G_CALLBACK(tray_activate), (gpointer)NULL);
424 g_signal_connect(G_OBJECT(tray), "popup-menu", G_CALLBACK(tray_popup), (gpointer)NULL);
425}
426
427int main(int argc, char **argv)
428{
71aeadfc
FT
429 int c;
430
34d45a15
FT
431 setlocale(LC_ALL, "");
432 bindtextdomain(PACKAGE, LOCALEDIR);
433 textdomain(PACKAGE);
434 signal(SIGCHLD, sighandler);
435 dc_init();
436 gtk_init(&argc, &argv);
437#ifdef HAVE_NOTIFY
438 notify_init("Dolda Connect");
439#endif
71aeadfc
FT
440 while((c = getopt(argc, argv, "rh")) != -1) {
441 switch(c) {
442 case 'r':
443 remote = 1;
444 break;
445 case 'h':
446 printf("usage: doldacond-shell [-hr]\n");
447 printf("\t-h\tDisplay this help message\n");
448 printf("\t-r\tConnect to a remote host\n");
449 exit(0);
450 default:
451 fprintf(stderr, "usage: doldacond-shell [-hr]\n");
452 exit(1);
453 }
454 }
34d45a15
FT
455
456 create_shm_wnd();
457 dcicon = gdk_pixbuf_new_from_xpm_data((const char **)dolda_icon_xpm);
458 gtk_window_set_default_icon(dcicon);
459 inittray();
71aeadfc
FT
460 if(remote)
461 connectdc();
462 else
463 startdaemon();
34d45a15
FT
464
465 gtk_main();
466
467 return(0);
468}
469
470#include "dsh-start.gtk"
471#include "dsh-menu.gtk"