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;
54 void updatewrite(void);
58 pid_t dpid = 0, dcpid = 0;
63 size_t derrbufsize = 0, derrbufdata = 0;
64 int dcfd, dcfdrtag, dcfdwtag = -1;
67 NotifyNotification *trnote = NULL;
70 #include "dsh-start.gtkh"
71 #include "dsh-menu.gtkh"
79 if((pfs = fopen(pf, "r")) == NULL) {
83 fgets(buf, sizeof(buf), pfs);
85 if((pid = atoi(buf)) == 0)
87 return(!kill(pid, 0));
90 void derrcb(gpointer data, gint source, GdkInputCondition cond)
94 sizebuf2(derrbuf, derrbufdata + 1024, 1);
95 ret = read(derrfd, derrbuf + derrbufdata, derrbufsize - derrbufdata);
98 bprintf(derrbuf, "\ncould not read from daemon: %s\n", strerror(errno));
99 gdk_input_remove(derrtag);
107 int msgbox(int type, int buttons, char *format, ...)
114 va_start(args, format);
115 buf = vsprintf2(format, args);
117 swnd = gtk_message_dialog_new(NULL, 0, type, buttons, "%s", buf);
118 resp = gtk_dialog_run(GTK_DIALOG(swnd));
119 gtk_widget_destroy(swnd);
124 void destroytr(struct dc_transfer *tr)
132 void inittr(struct dc_transfer *tr)
136 tr->udata = tri = memset(smalloc(sizeof(*tri)), 0, sizeof(*tri));
137 tr->destroycb = destroytr;
138 tri->ostate = tr->state;
139 tri->spos = tri->opos = tr->curpos;
141 tri->lastprog = time(NULL);
142 tri->sprog = ntime();
146 void notify(NotifyNotification **n, char *cat, char *title, char *body, ...)
151 va_start(args, body);
152 bbuf = vsprintf2(body, args);
155 *n = notify_notification_new_with_status_icon(title, bbuf, NULL, tray);
156 notify_notification_set_icon_from_pixbuf(*n, dcicon);
158 notify_notification_update(*n, title, bbuf, NULL);
160 notify_notification_show(*n, NULL);
164 /* XXX: Achtung! Too DC-specific! */
165 wchar_t *getfilename(wchar_t *path)
169 if((p = wcsrchr(path, L'\\')) == NULL)
175 char *bytes2si(long long bytes)
183 for(i = 0; (b >= 1024) && (i < 4); i++)
195 snprintf(ret, 64, "%.1f %s", b, sd);
199 void updatetooltip(void)
201 struct dc_transfer *tr;
205 size_t bufsize, bufdata;
209 for(tr = dc_transfers; tr != NULL; tr = tr->next) {
210 if(tr->dir != DC_TRNSD_DOWN)
214 if(tr->state == DC_TRNS_WAITING)
216 else if((tr->state == DC_TRNS_HS) || (tr->state == DC_TRNS_MAIN))
218 if((tr->state == DC_TRNS_MAIN) && (tri->speed != -1)) {
225 bufsize = bufdata = 0;
226 bprintf(buf, "Transfers: %i", t);
228 bprintf(buf, " (%i/%i)", i, a);
230 bprintf(buf, ", %s/s", bytes2si(st));
233 gtk_status_icon_set_tooltip(tray, buf);
237 void trstatechange(struct dc_transfer *tr, int ostate)
242 if((ostate == DC_TRNS_MAIN) && (tr->dir == DC_TRNSD_DOWN)) {
243 if(tr->state == DC_TRNS_DONE) {
246 notify(&trnote, "transfer.complete", _("Transfer complete"), _("Finished downloading %ls from %ls"), getfilename(tr->path), tr->peernick);
251 notify(&trnote, "transfer.error", _("Transfer interrupted"), _("The transfer of %ls from %ls was interrupted from the other side"), getfilename(tr->path), tr->peernick);
255 if(tr->state == DC_TRNS_MAIN) {
257 tri->spos = tr->curpos;
258 tri->sprog = ntime();
262 void updatetrinfo(void)
264 struct dc_transfer *tr;
271 for(tr = dc_transfers; tr != NULL; tr = tr->next) {
272 if(tr->udata == NULL) {
276 if(tri->ostate != tr->state) {
277 trstatechange(tr, tri->ostate);
278 tri->ostate = tr->state;
280 if(tri->opos != tr->curpos) {
281 tri->opos = tr->curpos;
285 if((tr->state = DC_TRNS_MAIN) && (now - tri->lastprog > 600)) {
287 notify(&trnote, "transfer.error", _("Transfer stalled"), _("The transfer of %ls from %ls has not made progress for 10 minutes"), getfilename(tr->path), tr->peernick);
290 if((tr->state == DC_TRNS_MAIN) && (dnow - tri->sprog > 10)) {
291 tri->speed = ((double)(tr->curpos - tri->spos) / (dnow - tri->sprog));
292 tri->spos = tr->curpos;
300 void trlscb(int resp, void *data)
305 gint trupdatecb(gpointer data)
311 void logincb(int err, wchar_t *reason, void *data)
313 if(err != DC_LOGIN_ERR_SUCCESS) {
314 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server"));
317 dc_queuecmd(NULL, NULL, L"notify", L"trans:act", L"on", L"trans:prog", L"on", NULL);
318 dc_gettrlistasync(trlscb, NULL);
323 void dcfdcb(gpointer data, gint source, GdkInputCondition cond)
325 struct dc_response *resp;
327 if(((cond & GDK_INPUT_READ) && dc_handleread()) || ((cond & GDK_INPUT_WRITE) && dc_handlewrite())) {
331 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server: %s"), strerror(errno));
336 while((resp = dc_getresp()) != NULL) {
337 if(!wcscmp(resp->cmdname, L".connect")) {
338 if(resp->code != 201) {
339 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("The server refused the connection"));
341 } else if(dc_checkprotocol(resp, DC_LATEST)) {
342 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Server protocol revision mismatch"));
345 gtk_status_icon_set_tooltip(tray, _("Authenticating..."));
346 dc_loginasync(NULL, 1, dc_convnone, logincb, NULL);
348 } else if(!wcscmp(resp->cmdname, L".notify")) {
349 dc_uimisc_handlenotify(resp);
357 void updatewrite(void)
363 dcfdwtag = gdk_input_add(dcfd, GDK_INPUT_WRITE, dcfdcb, NULL);
366 gdk_input_remove(dcfdwtag);
374 if((dcfd = dc_connect(remote?NULL:dc_srv_local)) < 0) {
375 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server: %s"), strerror(errno));
378 dcfdrtag = gdk_input_add(dcfd, GDK_INPUT_READ, dcfdcb, NULL);
380 gtk_status_icon_set_tooltip(tray, _("Connecting to server..."));
383 void startdaemon(void)
388 if(getenv("HOME") != NULL)
389 snprintf(pf, sizeof(pf), "%s/.doldacond.pid", getenv("HOME"));
391 snprintf(pf, sizeof(pf), "%s/.doldacond.pid", getpwuid(getuid())->pw_dir);
392 if(access(pf, F_OK) || !running(pf)) {
394 if((dpid = fork()) == 0) {
396 for(i = 3; i < FD_SETSIZE; i++)
398 execlp("doldacond", "doldacond", "-p", pf, NULL);
406 derrtag = gdk_input_add(derrfd, GDK_INPUT_READ, derrcb, NULL);
408 gtk_widget_show(start_wnd);
409 gtk_status_icon_set_tooltip(tray, _("Starting..."));
415 gboolean daemonized(gpointer uu)
417 gtk_widget_hide(start_wnd);
420 gdk_input_remove(derrtag);
424 gtk_status_icon_set_visible(tray, FALSE);
425 gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(start_log)), derrbuf, derrbufdata);
426 gtk_widget_show(start_errwnd);
433 void sighandler(int sig)
439 while((p = waitpid(-1, &status, WNOHANG)) > 0) {
442 gtk_timeout_add(1, daemonized, NULL);
443 } else if(p == dcpid) {
454 if((dcpid = fork()) == 0) {
455 for(i = 3; i < FD_SETSIZE; i++)
458 execlp("dolcon", "dolcon", NULL);
460 execlp("dolcon", "dolcon", "-l", NULL);
466 void cb_shm_dolconf_activate(GtkWidget *uu1, gpointer uu2)
470 if((dcpid = fork()) == 0) {
471 for(i = 3; i < FD_SETSIZE; i++)
473 execlp("dolconf", "dolconf", NULL);
479 void cb_shm_dolcon_activate(GtkWidget *uu1, gpointer uu2)
484 void cb_shm_quit_activate(GtkWidget *uu1, gpointer uu2)
486 dc_queuecmd(NULL, NULL, L"shutdown", NULL);
490 void tray_activate(GtkStatusIcon *uu1, gpointer uu2)
493 gtk_widget_show(start_wnd);
494 } else if(connected) {
499 void tray_popup(GtkStatusIcon *uu1, guint button, guint time, gpointer uu2)
501 gtk_menu_popup(GTK_MENU(shm_menu), NULL, NULL, NULL, NULL, button, time);
504 void cb_start_hide_clicked(GtkWidget *uu1, gpointer uu2)
506 gtk_widget_hide(start_wnd);
509 void cb_start_abort_clicked(GtkWidget *uu1, gpointer uu2)
515 void cb_start_errok_clicked(GtkWidget *uu1, gpointer uu2)
520 #include "../dolda-icon.xpm"
524 tray = gtk_status_icon_new_from_pixbuf(gdk_pixbuf_scale_simple(dcicon, 24, 24, GDK_INTERP_BILINEAR));
525 gtk_status_icon_set_tooltip(tray, "");
526 g_signal_connect(G_OBJECT(tray), "activate", G_CALLBACK(tray_activate), (gpointer)NULL);
527 g_signal_connect(G_OBJECT(tray), "popup-menu", G_CALLBACK(tray_popup), (gpointer)NULL);
530 int main(int argc, char **argv)
534 setlocale(LC_ALL, "");
535 bindtextdomain(PACKAGE, LOCALEDIR);
537 signal(SIGCHLD, sighandler);
539 gtk_init(&argc, &argv);
541 notify_init("Dolda Connect");
543 while((c = getopt(argc, argv, "rh")) != -1) {
549 printf("usage: doldacond-shell [-hr]\n");
550 printf("\t-h\tDisplay this help message\n");
551 printf("\t-r\tConnect to a remote host\n");
554 fprintf(stderr, "usage: doldacond-shell [-hr]\n");
560 dcicon = gdk_pixbuf_new_from_xpm_data((const char **)dolda_icon_xpm);
561 gtk_window_set_default_icon(dcicon);
568 g_timeout_add(10000, trupdatecb, NULL);
574 #include "dsh-start.gtk"
575 #include "dsh-menu.gtk"