2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2007 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
31 #include <gdk/gdkkeysyms.h>
33 #include <doldaconnect/uilib.h>
34 #include <doldaconnect/uimisc.h>
35 #include <doldaconnect/utils.h>
42 #include <libnotify/notify.h>
45 #define _(text) gettext(text)
49 int opos, spos, speed;
55 void updatewrite(void);
59 pid_t dpid = 0, dcpid = 0;
64 size_t derrbufsize = 0, derrbufdata = 0;
65 int dcfd, dcfdrtag, dcfdwtag = -1;
68 NotifyNotification *trnote = NULL;
71 #include "dsh-start.gtkh"
72 #include "dsh-menu.gtkh"
80 if((pfs = fopen(pf, "r")) == NULL) {
84 fgets(buf, sizeof(buf), pfs);
86 if((pid = atoi(buf)) == 0)
88 return(!kill(pid, 0));
91 void derrcb(gpointer data, gint source, GdkInputCondition cond)
95 sizebuf2(derrbuf, derrbufdata + 1024, 1);
96 ret = read(derrfd, derrbuf + derrbufdata, derrbufsize - derrbufdata);
99 bprintf(derrbuf, "\ncould not read from daemon: %s\n", strerror(errno));
100 gdk_input_remove(derrtag);
108 int msgbox(int type, int buttons, char *format, ...)
115 va_start(args, format);
116 buf = vsprintf2(format, 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);
125 void destroytr(struct dc_transfer *tr)
133 void inittr(struct dc_transfer *tr)
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;
142 tri->lastprog = time(NULL);
143 tri->sprog = ntime();
147 void notify(NotifyNotification **n, char *cat, char *title, char *body, ...)
152 va_start(args, body);
153 bbuf = vsprintf2(body, args);
156 *n = notify_notification_new_with_status_icon(title, bbuf, NULL, tray);
157 notify_notification_set_icon_from_pixbuf(*n, dcicon);
159 notify_notification_update(*n, title, bbuf, NULL);
161 notify_notification_show(*n, NULL);
165 /* XXX: Achtung! Too DC-specific! */
166 wchar_t *getfilename(wchar_t *path)
170 if((p = wcsrchr(path, L'\\')) == NULL)
176 char *bytes2si(long long bytes)
184 for(i = 0; (b >= 1024) && (i < 4); i++)
196 snprintf(ret, 64, "%.1f %s", b, sd);
200 void updatetooltip(void)
202 struct dc_transfer *tr;
204 int t, i, a, st, bc, bt;
206 size_t bufsize, bufdata;
210 for(tr = dc_transfers; tr != NULL; tr = tr->next) {
211 if(tr->dir != DC_TRNSD_DOWN)
215 if(tr->state == DC_TRNS_WAITING)
217 else if((tr->state == DC_TRNS_HS) || (tr->state == DC_TRNS_MAIN))
219 if((tr->state == DC_TRNS_MAIN)) {
224 if(tri->speed != -1) {
232 bufsize = bufdata = 0;
233 bprintf(buf, "%s: %i", _("Transfers"), t);
235 bprintf(buf, " (%i/%i)", i, a);
237 bprintf(buf, ", %.1f%%", ((double)bc / (double)bt) * 100.0);
239 bprintf(buf, ", %s/s", bytes2si(st));
241 gtk_status_icon_set_tooltip(tray, buf);
245 void trstatechange(struct dc_transfer *tr, int ostate)
250 if((ostate == DC_TRNS_MAIN) && (tr->dir == DC_TRNSD_DOWN)) {
251 if(tr->state == DC_TRNS_DONE) {
254 notify(&trnote, "transfer.complete", _("Transfer complete"), _("Finished downloading %ls from %ls"), getfilename(tr->path), tr->peernick);
259 notify(&trnote, "transfer.error", _("Transfer interrupted"), _("The transfer of %ls from %ls was interrupted from the other side"), getfilename(tr->path), tr->peernick);
263 if(tr->state == DC_TRNS_MAIN) {
265 tri->spos = tr->curpos;
266 tri->sprog = ntime();
270 void updatetrinfo(void)
272 struct dc_transfer *tr;
279 for(tr = dc_transfers; tr != NULL; tr = tr->next) {
280 if(tr->udata == NULL) {
284 if(tri->ostate != tr->state) {
285 trstatechange(tr, tri->ostate);
286 tri->ostate = tr->state;
288 if(tri->opos != tr->curpos) {
289 tri->opos = tr->curpos;
294 if((tr->state = DC_TRNS_MAIN) && (now - tri->lastprog > 600) && !tri->warned) {
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);
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;
311 void trlscb(int resp, void *data)
316 gint trupdatecb(gpointer data)
322 void logincb(int err, wchar_t *reason, void *data)
324 if(err != DC_LOGIN_ERR_SUCCESS) {
325 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server"));
328 dc_queuecmd(NULL, NULL, L"notify", L"trans:act", L"on", L"trans:prog", L"on", NULL);
329 dc_gettrlistasync(trlscb, NULL);
334 void dcfdcb(gpointer data, gint source, GdkInputCondition cond)
336 struct dc_response *resp;
338 if(((cond & GDK_INPUT_READ) && dc_handleread()) || ((cond & GDK_INPUT_WRITE) && dc_handlewrite())) {
342 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server: %s"), strerror(errno));
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"));
352 } else if(dc_checkprotocol(resp, DC_LATEST)) {
353 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Server protocol revision mismatch"));
356 gtk_status_icon_set_tooltip(tray, _("Authenticating..."));
357 dc_loginasync(NULL, 1, dc_convnone, logincb, NULL);
359 } else if(!wcscmp(resp->cmdname, L".notify")) {
360 dc_uimisc_handlenotify(resp);
368 void updatewrite(void)
374 dcfdwtag = gdk_input_add(dcfd, GDK_INPUT_WRITE, dcfdcb, NULL);
377 gdk_input_remove(dcfdwtag);
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));
389 dcfdrtag = gdk_input_add(dcfd, GDK_INPUT_READ, dcfdcb, NULL);
391 gtk_status_icon_set_tooltip(tray, _("Connecting to server..."));
394 void startdaemon(void)
399 if(getenv("HOME") != NULL)
400 snprintf(pf, sizeof(pf), "%s/.doldacond.pid", getenv("HOME"));
402 snprintf(pf, sizeof(pf), "%s/.doldacond.pid", getpwuid(getuid())->pw_dir);
403 if(access(pf, F_OK) || !running(pf)) {
405 if((dpid = fork()) == 0) {
407 for(i = 3; i < FD_SETSIZE; i++)
409 execlp("doldacond", "doldacond", "-p", pf, NULL);
417 derrtag = gdk_input_add(derrfd, GDK_INPUT_READ, derrcb, NULL);
419 gtk_widget_show(start_wnd);
420 gtk_status_icon_set_tooltip(tray, _("Starting..."));
426 gboolean daemonized(gpointer uu)
428 gtk_widget_hide(start_wnd);
431 gdk_input_remove(derrtag);
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);
444 void sighandler(int sig)
450 while((p = waitpid(-1, &status, WNOHANG)) > 0) {
453 gtk_timeout_add(1, daemonized, NULL);
454 } else if(p == dcpid) {
465 if((dcpid = fork()) == 0) {
466 for(i = 3; i < FD_SETSIZE; i++)
469 execlp("dolcon", "dolcon", NULL);
471 execlp("dolcon", "dolcon", "-l", NULL);
477 void cb_shm_dolconf_activate(GtkWidget *uu1, gpointer uu2)
481 if((dcpid = fork()) == 0) {
482 for(i = 3; i < FD_SETSIZE; i++)
484 execlp("dolconf", "dolconf", NULL);
490 void cb_shm_dolcon_activate(GtkWidget *uu1, gpointer uu2)
495 void cb_shm_quit_activate(GtkWidget *uu1, gpointer uu2)
497 dc_queuecmd(NULL, NULL, L"shutdown", NULL);
501 void tray_activate(GtkStatusIcon *uu1, gpointer uu2)
504 gtk_widget_show(start_wnd);
505 } else if(connected) {
510 void tray_popup(GtkStatusIcon *uu1, guint button, guint time, gpointer uu2)
512 gtk_menu_popup(GTK_MENU(shm_menu), NULL, NULL, NULL, NULL, button, time);
515 void cb_start_hide_clicked(GtkWidget *uu1, gpointer uu2)
517 gtk_widget_hide(start_wnd);
520 void cb_start_abort_clicked(GtkWidget *uu1, gpointer uu2)
526 void cb_start_errok_clicked(GtkWidget *uu1, gpointer uu2)
531 #include "../dolda-icon.xpm"
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);
541 int main(int argc, char **argv)
545 setlocale(LC_ALL, "");
546 bindtextdomain(PACKAGE, LOCALEDIR);
548 signal(SIGCHLD, sighandler);
550 gtk_init(&argc, &argv);
552 notify_init("Dolda Connect");
554 while((c = getopt(argc, argv, "rh")) != -1) {
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");
565 fprintf(stderr, "usage: doldacond-shell [-hr]\n");
571 dcicon = gdk_pixbuf_new_from_xpm_data((const char **)dolda_icon_xpm);
572 gtk_window_set_default_icon(dcicon);
579 g_timeout_add(10000, trupdatecb, NULL);
585 #include "dsh-start.gtk"
586 #include "dsh-menu.gtk"