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>
34 #include <doldaconnect/uilib.h>
35 #include <doldaconnect/uimisc.h>
36 #include <doldaconnect/utils.h>
43 #include <libnotify/notify.h>
46 #define _(text) gettext(text)
57 void updatewrite(void);
62 pid_t dpid = 0, dcpid = 0;
67 size_t derrbufsize = 0, derrbufdata = 0;
68 int dcfd, dcfdrtag, dcfdwtag = -1;
71 NotifyNotification *trnote = NULL;
74 #include "dsh-start.gtkh"
75 #include "dsh-menu.gtkh"
83 if((pfs = fopen(pf, "r")) == NULL) {
87 fgets(buf, sizeof(buf), pfs);
89 if((pid = atoi(buf)) == 0)
91 return(!kill(pid, 0));
94 void derrcb(gpointer data, gint source, GdkInputCondition cond)
98 sizebuf2(derrbuf, derrbufdata + 1024, 1);
99 ret = read(derrfd, derrbuf + derrbufdata, derrbufsize - derrbufdata);
102 bprintf(derrbuf, "\ncould not read from daemon: %s\n", strerror(errno));
103 gdk_input_remove(derrtag);
111 int msgbox(int type, int buttons, char *format, ...)
118 va_start(args, format);
119 buf = vsprintf2(format, args);
121 swnd = gtk_message_dialog_new(NULL, 0, type, buttons, "%s", buf);
122 resp = gtk_dialog_run(GTK_DIALOG(swnd));
123 gtk_widget_destroy(swnd);
128 void destroytr(struct dc_transfer *tr)
136 void inittr(struct dc_transfer *tr)
140 tr->udata = tri = memset(smalloc(sizeof(*tri)), 0, sizeof(*tri));
141 tr->destroycb = destroytr;
142 tri->ostate = tr->state;
143 tri->spos = tri->opos = tr->curpos;
145 tri->lastprog = time(NULL);
146 tri->sprog = ntime();
150 void notify(NotifyNotification **n, char *cat, char *title, char *body, ...)
155 va_start(args, body);
156 bbuf = vsprintf2(body, args);
159 *n = notify_notification_new_with_status_icon(title, bbuf, NULL, tray);
160 notify_notification_set_icon_from_pixbuf(*n, dcicon);
162 notify_notification_update(*n, title, bbuf, NULL);
164 notify_notification_show(*n, NULL);
168 /* XXX: Achtung! Too DC-specific! */
169 wchar_t *getfilename(wchar_t *path)
173 if((p = wcsrchr(path, L'\\')) == NULL)
179 char *bytes2si(long long bytes)
187 for(i = 0; (b >= 1024) && (i < 4); i++)
199 snprintf(ret, 64, "%.1f %s", b, sd);
203 void updatetooltip(void)
205 struct dc_transfer *tr;
210 size_t bufsize, bufdata;
214 for(tr = dc_transfers; tr != NULL; tr = tr->next) {
215 if(tr->dir != DC_TRNSD_DOWN)
219 if(tr->state == DC_TRNS_WAITING)
221 else if((tr->state == DC_TRNS_HS) || (tr->state == DC_TRNS_MAIN))
223 if((tr->state == DC_TRNS_MAIN)) {
228 if(tri->speed != -1) {
236 bufsize = bufdata = 0;
237 bprintf(buf, "%s: %i", _("Transfers"), t);
239 bprintf(buf, " (%i/%i)", i, a);
241 bprintf(buf, ", %.1f%%", ((double)bc / (double)bt) * 100.0);
243 bprintf(buf, ", %s/s", bytes2si(st));
245 gtk_status_icon_set_tooltip(tray, buf);
249 void trstatechange(struct dc_transfer *tr, int ostate)
254 if((ostate == DC_TRNS_MAIN) && (tr->dir == DC_TRNSD_DOWN)) {
255 if(tr->state == DC_TRNS_DONE) {
258 notify(&trnote, "transfer.complete", _("Transfer complete"), _("Finished downloading %ls from %ls"), getfilename(tr->path), tr->peernick);
263 notify(&trnote, "transfer.error", _("Transfer interrupted"), _("The transfer of %ls from %ls was interrupted from the other side"), getfilename(tr->path), tr->peernick);
267 if(tr->state == DC_TRNS_MAIN) {
269 tri->spos = tr->curpos;
270 tri->sprog = ntime();
274 void updatetrinfo(void)
276 struct dc_transfer *tr;
283 for(tr = dc_transfers; tr != NULL; tr = tr->next) {
284 if(tr->udata == NULL) {
288 if(tri->ostate != tr->state) {
289 trstatechange(tr, tri->ostate);
290 tri->ostate = tr->state;
292 if(tri->opos != tr->curpos) {
293 tri->opos = tr->curpos;
298 if((tr->state = DC_TRNS_MAIN) && (now - tri->lastprog > 600) && !tri->warned) {
300 notify(&trnote, "transfer.error", _("Transfer stalled"), _("The transfer of %ls from %ls has not made progress for 10 minutes"), getfilename(tr->path), tr->peernick);
305 if((tr->state == DC_TRNS_MAIN) && (dnow - tri->sprog > 10)) {
306 tri->speed = ((double)(tr->curpos - tri->spos) / (dnow - tri->sprog));
307 tri->spos = tr->curpos;
315 void trlscb(int resp, void *data)
320 gint trupdatecb(gpointer data)
326 void logincb(int err, wchar_t *reason, void *data)
328 if(err != DC_LOGIN_ERR_SUCCESS) {
329 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server"));
332 dc_queuecmd(NULL, NULL, L"notify", L"trans:act", L"on", L"trans:prog", L"on", NULL);
333 dc_gettrlistasync(trlscb, NULL);
338 void dcfdcb(gpointer data, gint source, GdkInputCondition cond)
340 struct dc_response *resp;
342 if(((cond & GDK_INPUT_READ) && dc_handleread()) || ((cond & GDK_INPUT_WRITE) && dc_handlewrite())) {
346 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server: %s"), strerror(errno));
351 while((resp = dc_getresp()) != NULL) {
352 if(!wcscmp(resp->cmdname, L".connect")) {
353 if(resp->code != 201) {
354 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("The server refused the connection"));
356 } else if(dc_checkprotocol(resp, DC_LATEST)) {
357 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Server protocol revision mismatch"));
360 gtk_status_icon_set_tooltip(tray, _("Authenticating..."));
361 dc_loginasync(NULL, 1, dc_convnone, logincb, NULL);
363 } else if(!wcscmp(resp->cmdname, L".notify")) {
364 dc_uimisc_handlenotify(resp);
372 void updatewrite(void)
378 dcfdwtag = gdk_input_add(dcfd, GDK_INPUT_WRITE, dcfdcb, NULL);
381 gdk_input_remove(dcfdwtag);
389 if((dcfd = dc_connect(server)) < 0) {
390 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server: %s"), strerror(errno));
393 dcfdrtag = gdk_input_add(dcfd, GDK_INPUT_READ, dcfdcb, NULL);
395 gtk_status_icon_set_tooltip(tray, _("Connecting to server..."));
398 void startdaemon(void)
404 if(getenv("HOME") != NULL)
405 snprintf(pf, sizeof(pf), "%s/.doldacond.pid", getenv("HOME"));
407 snprintf(pf, sizeof(pf), "%s/.doldacond.pid", getpwuid(getuid())->pw_dir);
408 if(access(pf, F_OK) || !running(pf)) {
411 sigaddset(&ss, SIGCHLD);
412 sigprocmask(SIG_BLOCK, &ss, NULL);
413 if((dpid = fork()) == 0) {
414 sigprocmask(SIG_UNBLOCK, &ss, NULL);
416 for(i = 3; i < FD_SETSIZE; i++)
418 execlp("doldacond", "doldacond", "-p", pf, NULL);
426 derrtag = gdk_input_add(derrfd, GDK_INPUT_READ, derrcb, NULL);
428 gtk_widget_show(start_wnd);
429 gtk_status_icon_set_tooltip(tray, _("Starting..."));
430 sigprocmask(SIG_UNBLOCK, &ss, NULL);
436 gboolean daemonized(gpointer uu)
438 gtk_widget_hide(start_wnd);
441 gdk_input_remove(derrtag);
445 gtk_status_icon_set_visible(tray, FALSE);
446 gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(start_log)), derrbuf, derrbufdata);
447 gtk_widget_show(start_errwnd);
454 void sighandler(int sig)
460 while((p = waitpid(-1, &status, WNOHANG)) > 0) {
463 gtk_timeout_add(1, daemonized, NULL);
464 } else if(p == dcpid) {
475 if((dcpid = fork()) == 0) {
476 for(i = 3; i < FD_SETSIZE; i++)
479 execlp("dolcon", "dolcon", NULL);
481 execlp("dolcon", "dolcon", "-l", NULL);
487 void cb_shm_dolconf_activate(GtkWidget *uu1, gpointer uu2)
491 if((dcpid = fork()) == 0) {
492 for(i = 3; i < FD_SETSIZE; i++)
494 execlp("dolconf", "dolconf", NULL);
500 void cb_shm_dolcon_activate(GtkWidget *uu1, gpointer uu2)
505 void cb_shm_quit_activate(GtkWidget *uu1, gpointer uu2)
507 dc_queuecmd(NULL, NULL, L"shutdown", NULL);
511 void tray_activate(GtkStatusIcon *uu1, gpointer uu2)
514 gtk_widget_show(start_wnd);
515 } else if(connected) {
520 void tray_popup(GtkStatusIcon *uu1, guint button, guint time, gpointer uu2)
522 gtk_menu_popup(GTK_MENU(shm_menu), NULL, NULL, NULL, NULL, button, time);
525 void cb_start_hide_clicked(GtkWidget *uu1, gpointer uu2)
527 gtk_widget_hide(start_wnd);
530 void cb_start_abort_clicked(GtkWidget *uu1, gpointer uu2)
536 void cb_start_errok_clicked(GtkWidget *uu1, gpointer uu2)
541 #include "../dolda-icon.xpm"
545 tray = gtk_status_icon_new_from_pixbuf(gdk_pixbuf_scale_simple(dcicon, 24, 24, GDK_INTERP_BILINEAR));
546 gtk_status_icon_set_tooltip(tray, "");
547 g_signal_connect(G_OBJECT(tray), "activate", G_CALLBACK(tray_activate), (gpointer)NULL);
548 g_signal_connect(G_OBJECT(tray), "popup-menu", G_CALLBACK(tray_popup), (gpointer)NULL);
551 int main(int argc, char **argv)
555 setlocale(LC_ALL, "");
556 bindtextdomain(PACKAGE, LOCALEDIR);
558 signal(SIGCHLD, sighandler);
560 server = dc_srv_local;
561 gtk_init(&argc, &argv);
563 notify_init("Dolda Connect");
565 while((c = getopt(argc, argv, "Vrhs:")) != -1) {
576 printf("usage: doldacond-shell [-hr]\n");
577 printf("\t-h\tDisplay this help message\n");
578 printf("\t-r\tConnect to a remote host\n");
579 printf("\t-V\tDisplay version info and exit\n");
582 printf("%s", RELEASEINFO);
585 fprintf(stderr, "usage: doldacond-shell [-hr]\n");
591 dcicon = gdk_pixbuf_new_from_xpm_data((const char **)dolda_icon_xpm);
592 gtk_window_set_default_icon(dcicon);
599 g_timeout_add(10000, trupdatecb, NULL);
605 #include "dsh-start.gtk"
606 #include "dsh-menu.gtk"