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)
53 void updatewrite(void);
57 pid_t dpid = 0, dcpid = 0;
62 size_t derrbufsize = 0, derrbufdata = 0;
63 int dcfd, dcfdrtag, dcfdwtag = -1;
66 NotifyNotification *trnote = NULL;
69 #include "dsh-start.gtkh"
70 #include "dsh-menu.gtkh"
78 if((pfs = fopen(pf, "r")) == NULL) {
82 fgets(buf, sizeof(buf), pfs);
84 if((pid = atoi(buf)) == 0)
86 return(!kill(pid, 0));
89 void derrcb(gpointer data, gint source, GdkInputCondition cond)
93 sizebuf2(derrbuf, derrbufdata + 1024, 1);
94 ret = read(derrfd, derrbuf + derrbufdata, derrbufsize - derrbufdata);
97 bprintf(derrbuf, "\ncould not read from daemon: %s\n", strerror(errno));
98 gdk_input_remove(derrtag);
106 int msgbox(int type, int buttons, char *format, ...)
113 va_start(args, format);
114 buf = vsprintf2(format, 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);
123 void destroytr(struct dc_transfer *tr)
131 void inittr(struct dc_transfer *tr)
135 tr->udata = tri = memset(smalloc(sizeof(*tri)), 0, sizeof(*tri));
136 tr->destroycb = destroytr;
137 tri->ostate = tr->state;
138 tri->opos = tr->curpos;
139 tri->lastprog = time(NULL);
143 void notify(NotifyNotification **n, char *cat, char *title, char *body, ...)
148 va_start(args, body);
149 bbuf = vsprintf2(body, args);
152 *n = notify_notification_new_with_status_icon(title, bbuf, NULL, tray);
153 notify_notification_set_icon_from_pixbuf(*n, dcicon);
155 notify_notification_update(*n, title, bbuf, NULL);
157 notify_notification_show(*n, NULL);
161 /* XXX: Achtung! Too DC-specific! */
162 wchar_t *getfilename(wchar_t *path)
166 if((p = wcsrchr(path, L'\\')) == NULL)
172 void trstatechange(struct dc_transfer *tr, int ostate)
174 if((ostate == DC_TRNS_MAIN) && (tr->dir == DC_TRNSD_DOWN)) {
175 if(tr->state == DC_TRNS_DONE) {
178 notify(&trnote, "transfer.complete", _("Transfer complete"), _("Finished downloading %ls from %ls"), getfilename(tr->path), tr->peernick);
183 notify(&trnote, "transfer.error", _("Transfer interrupted"), _("The transfer of %ls from %ls was interrupted from the other side"), getfilename(tr->path), tr->peernick);
189 void updatetrinfo(void)
191 struct dc_transfer *tr;
196 for(tr = dc_transfers; tr != NULL; tr = tr->next) {
197 if(tr->udata == NULL) {
201 if(tri->ostate != tr->state) {
202 trstatechange(tr, tri->ostate);
203 tri->ostate = tr->state;
205 if(tri->opos != tr->curpos) {
206 tri->opos = tr->curpos;
210 if((tr->state = DC_TRNS_MAIN) && (now - tri->lastprog > 600)) {
212 notify(&trnote, "transfer.error", _("Transfer stalled"), _("The transfer of %ls from %ls has not made progress for 10 minutes"), getfilename(tr->path), tr->peernick);
219 void trlscb(int resp, void *data)
224 void logincb(int err, wchar_t *reason, void *data)
226 if(err != DC_LOGIN_ERR_SUCCESS) {
227 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server"));
230 gtk_status_icon_set_tooltip(tray, "Dolda Connect");
231 dc_queuecmd(NULL, NULL, L"notify", L"trans:act", L"on", L"trans:prog", L"on", NULL);
232 dc_gettrlistasync(trlscb, NULL);
237 void dcfdcb(gpointer data, gint source, GdkInputCondition cond)
239 struct dc_response *resp;
241 if(((cond & GDK_INPUT_READ) && dc_handleread()) || ((cond & GDK_INPUT_WRITE) && dc_handlewrite())) {
245 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server: %s"), strerror(errno));
250 while((resp = dc_getresp()) != NULL) {
251 if(!wcscmp(resp->cmdname, L".connect")) {
252 if(resp->code != 201) {
253 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("The server refused the connection"));
255 } else if(dc_checkprotocol(resp, DC_LATEST)) {
256 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Server protocol revision mismatch"));
259 gtk_status_icon_set_tooltip(tray, _("Authenticating..."));
260 dc_loginasync(NULL, 1, dc_convnone, logincb, NULL);
262 } else if(!wcscmp(resp->cmdname, L".notify")) {
263 dc_uimisc_handlenotify(resp);
270 void updatewrite(void)
276 dcfdwtag = gdk_input_add(dcfd, GDK_INPUT_WRITE, dcfdcb, NULL);
279 gdk_input_remove(dcfdwtag);
287 if((dcfd = dc_connect(remote?NULL:dc_srv_local)) < 0) {
288 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server: %s"), strerror(errno));
291 dcfdrtag = gdk_input_add(dcfd, GDK_INPUT_READ, dcfdcb, NULL);
293 gtk_status_icon_set_tooltip(tray, _("Connecting to server..."));
296 void startdaemon(void)
301 if(getenv("HOME") != NULL)
302 snprintf(pf, sizeof(pf), "%s/.doldacond.pid", getenv("HOME"));
304 snprintf(pf, sizeof(pf), "%s/.doldacond.pid", getpwuid(getuid())->pw_dir);
305 if(access(pf, F_OK) || !running(pf)) {
307 if((dpid = fork()) == 0) {
309 for(i = 3; i < FD_SETSIZE; i++)
311 execlp("doldacond", "doldacond", "-p", pf, NULL);
319 derrtag = gdk_input_add(derrfd, GDK_INPUT_READ, derrcb, NULL);
321 gtk_widget_show(start_wnd);
322 gtk_status_icon_set_tooltip(tray, _("Starting..."));
328 gboolean daemonized(gpointer uu)
330 gtk_widget_hide(start_wnd);
333 gdk_input_remove(derrtag);
337 gtk_status_icon_set_visible(tray, FALSE);
338 gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(start_log)), derrbuf, derrbufdata);
339 gtk_widget_show(start_errwnd);
346 void sighandler(int sig)
352 while((p = waitpid(-1, &status, WNOHANG)) > 0) {
355 gtk_timeout_add(1, daemonized, NULL);
356 } else if(p == dcpid) {
367 if((dcpid = fork()) == 0) {
368 for(i = 3; i < FD_SETSIZE; i++)
371 execlp("dolcon", "dolcon", NULL);
373 execlp("dolcon", "dolcon", "-l", NULL);
379 void cb_shm_dolcon_activate(GtkWidget *uu1, gpointer uu2)
384 void cb_shm_quit_activate(GtkWidget *uu1, gpointer uu2)
386 dc_queuecmd(NULL, NULL, L"shutdown", NULL);
390 void tray_activate(GtkStatusIcon *uu1, gpointer uu2)
393 gtk_widget_show(start_wnd);
394 } else if(connected) {
399 void tray_popup(GtkStatusIcon *uu1, guint button, guint time, gpointer uu2)
401 gtk_menu_popup(GTK_MENU(shm_menu), NULL, NULL, NULL, NULL, button, time);
404 void cb_start_hide_clicked(GtkWidget *uu1, gpointer uu2)
406 gtk_widget_hide(start_wnd);
409 void cb_start_abort_clicked(GtkWidget *uu1, gpointer uu2)
415 void cb_start_errok_clicked(GtkWidget *uu1, gpointer uu2)
420 #include "../dolda-icon.xpm"
424 tray = gtk_status_icon_new_from_pixbuf(gdk_pixbuf_scale_simple(dcicon, 24, 24, GDK_INTERP_BILINEAR));
425 gtk_status_icon_set_tooltip(tray, "");
426 g_signal_connect(G_OBJECT(tray), "activate", G_CALLBACK(tray_activate), (gpointer)NULL);
427 g_signal_connect(G_OBJECT(tray), "popup-menu", G_CALLBACK(tray_popup), (gpointer)NULL);
430 int main(int argc, char **argv)
434 setlocale(LC_ALL, "");
435 bindtextdomain(PACKAGE, LOCALEDIR);
437 signal(SIGCHLD, sighandler);
439 gtk_init(&argc, &argv);
441 notify_init("Dolda Connect");
443 while((c = getopt(argc, argv, "rh")) != -1) {
449 printf("usage: doldacond-shell [-hr]\n");
450 printf("\t-h\tDisplay this help message\n");
451 printf("\t-r\tConnect to a remote host\n");
454 fprintf(stderr, "usage: doldacond-shell [-hr]\n");
460 dcicon = gdk_pixbuf_new_from_xpm_data((const char **)dolda_icon_xpm);
461 gtk_window_set_default_icon(dcicon);
473 #include "dsh-start.gtk"
474 #include "dsh-menu.gtk"