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)
51 void updatewrite(void);
55 pid_t dpid = 0, dcpid = 0;
60 size_t derrbufsize = 0, derrbufdata = 0;
61 int dcfd, dcfdrtag, dcfdwtag = -1;
64 NotifyNotification *trnote = NULL;
67 #include "dsh-start.gtkh"
68 #include "dsh-menu.gtkh"
76 if((pfs = fopen(pf, "r")) == NULL) {
80 fgets(buf, sizeof(buf), pfs);
82 if((pid = atoi(buf)) == 0)
84 return(!kill(pid, 0));
87 void derrcb(gpointer data, gint source, GdkInputCondition cond)
91 sizebuf2(derrbuf, derrbufdata + 1024, 1);
92 ret = read(derrfd, derrbuf + derrbufdata, derrbufsize - derrbufdata);
95 bprintf(derrbuf, "\ncould not read from daemon: %s\n", strerror(errno));
96 gdk_input_remove(derrtag);
104 int msgbox(int type, int buttons, char *format, ...)
111 va_start(args, format);
112 buf = vsprintf2(format, args);
114 swnd = gtk_message_dialog_new(NULL, 0, type, buttons, "%s", buf);
115 resp = gtk_dialog_run(GTK_DIALOG(swnd));
116 gtk_widget_destroy(swnd);
121 void destroytr(struct dc_transfer *tr)
129 void inittr(struct dc_transfer *tr)
133 tr->udata = tri = memset(smalloc(sizeof(*tri)), 0, sizeof(*tri));
134 tr->destroycb = destroytr;
135 tri->ostate = tr->state;
139 void notify(NotifyNotification **n, char *cat, char *title, char *body, ...)
144 va_start(args, body);
145 bbuf = vsprintf2(body, args);
148 *n = notify_notification_new_with_status_icon(title, bbuf, NULL, tray);
149 notify_notification_set_icon_from_pixbuf(*n, dcicon);
151 notify_notification_update(*n, title, bbuf, NULL);
153 notify_notification_show(*n, NULL);
157 void trstatechange(struct dc_transfer *tr, int ostate)
159 if(ostate == DC_TRNS_MAIN) {
160 if(tr->state == DC_TRNS_DONE) {
162 notify(&trnote, "transfer.complete", _("Transfer complete"), _("Finished downloading %ls from %ls"), tr->path, tr->peernick);
166 notify(&trnote, "transfer.error", _("Transfer interrupted"), _("The transfer of %ls from %ls was interrupted from the other side"), tr->path, tr->peernick);
172 void updatetrinfo(void)
174 struct dc_transfer *tr;
177 for(tr = dc_transfers; tr != NULL; tr = tr->next) {
178 if(tr->udata == NULL) {
182 if(tri->ostate != tr->state) {
183 trstatechange(tr, tri->ostate);
184 tri->ostate = tr->state;
190 void trlscb(int resp, void *data)
195 void logincb(int err, wchar_t *reason, void *data)
197 if(err != DC_LOGIN_ERR_SUCCESS) {
198 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server"));
201 gtk_status_icon_set_tooltip(tray, "Dolda Connect");
202 dc_queuecmd(NULL, NULL, L"notify", L"trans:act", L"on", L"trans:prog", L"on", NULL);
203 dc_gettrlistasync(trlscb, NULL);
208 void dcfdcb(gpointer data, gint source, GdkInputCondition cond)
210 struct dc_response *resp;
212 if(((cond & GDK_INPUT_READ) && dc_handleread()) || ((cond & GDK_INPUT_WRITE) && dc_handlewrite())) {
216 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server: %s"), strerror(errno));
221 while((resp = dc_getresp()) != NULL) {
222 if(!wcscmp(resp->cmdname, L".connect")) {
223 if(resp->code != 201) {
224 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("The server refused the connection"));
226 } else if(dc_checkprotocol(resp, DC_LATEST)) {
227 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Server protocol revision mismatch"));
230 gtk_status_icon_set_tooltip(tray, _("Authenticating..."));
231 dc_loginasync(NULL, 1, dc_convnone, logincb, NULL);
233 } else if(!wcscmp(resp->cmdname, L".notify")) {
234 dc_uimisc_handlenotify(resp);
241 void updatewrite(void)
247 dcfdwtag = gdk_input_add(dcfd, GDK_INPUT_WRITE, dcfdcb, NULL);
250 gdk_input_remove(dcfdwtag);
258 if((dcfd = dc_connect(remote?NULL:dc_srv_local)) < 0) {
259 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server: %s"), strerror(errno));
262 dcfdrtag = gdk_input_add(dcfd, GDK_INPUT_READ, dcfdcb, NULL);
264 gtk_status_icon_set_tooltip(tray, _("Connecting to server..."));
267 void startdaemon(void)
272 if(getenv("HOME") != NULL)
273 snprintf(pf, sizeof(pf), "%s/.doldacond.pid", getenv("HOME"));
275 snprintf(pf, sizeof(pf), "%s/.doldacond.pid", getpwuid(getuid())->pw_dir);
276 if(access(pf, F_OK) || !running(pf)) {
278 if((dpid = fork()) == 0) {
280 for(i = 3; i < FD_SETSIZE; i++)
282 execlp("doldacond", "doldacond", "-p", pf, NULL);
290 derrtag = gdk_input_add(derrfd, GDK_INPUT_READ, derrcb, NULL);
292 gtk_widget_show(start_wnd);
293 gtk_status_icon_set_tooltip(tray, _("Starting..."));
299 gboolean daemonized(gpointer uu)
301 gtk_widget_hide(start_wnd);
304 gdk_input_remove(derrtag);
308 gtk_status_icon_set_visible(tray, FALSE);
309 gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(start_log)), derrbuf, derrbufdata);
310 gtk_widget_show(start_errwnd);
317 void sighandler(int sig)
323 while((p = waitpid(-1, &status, WNOHANG)) > 0) {
326 gtk_timeout_add(1, daemonized, NULL);
327 } else if(p == dcpid) {
338 if((dcpid = fork()) == 0) {
339 for(i = 3; i < FD_SETSIZE; i++)
342 execlp("dolcon", "dolcon", NULL);
344 execlp("dolcon", "dolcon", "-l", NULL);
350 void cb_shm_dolcon_activate(GtkWidget *uu1, gpointer uu2)
355 void cb_shm_quit_activate(GtkWidget *uu1, gpointer uu2)
357 dc_queuecmd(NULL, NULL, L"shutdown", NULL);
361 void tray_activate(GtkStatusIcon *uu1, gpointer uu2)
364 gtk_widget_show(start_wnd);
365 } else if(connected) {
370 void tray_popup(GtkStatusIcon *uu1, guint button, guint time, gpointer uu2)
372 gtk_menu_popup(GTK_MENU(shm_menu), NULL, NULL, NULL, NULL, button, time);
375 void cb_start_hide_clicked(GtkWidget *uu1, gpointer uu2)
377 gtk_widget_hide(start_wnd);
380 void cb_start_abort_clicked(GtkWidget *uu1, gpointer uu2)
386 void cb_start_errok_clicked(GtkWidget *uu1, gpointer uu2)
391 #include "../dolda-icon.xpm"
395 tray = gtk_status_icon_new_from_pixbuf(gdk_pixbuf_scale_simple(dcicon, 24, 24, GDK_INTERP_BILINEAR));
396 gtk_status_icon_set_tooltip(tray, "");
397 g_signal_connect(G_OBJECT(tray), "activate", G_CALLBACK(tray_activate), (gpointer)NULL);
398 g_signal_connect(G_OBJECT(tray), "popup-menu", G_CALLBACK(tray_popup), (gpointer)NULL);
401 int main(int argc, char **argv)
405 setlocale(LC_ALL, "");
406 bindtextdomain(PACKAGE, LOCALEDIR);
408 signal(SIGCHLD, sighandler);
410 gtk_init(&argc, &argv);
412 notify_init("Dolda Connect");
414 while((c = getopt(argc, argv, "rh")) != -1) {
420 printf("usage: doldacond-shell [-hr]\n");
421 printf("\t-h\tDisplay this help message\n");
422 printf("\t-r\tConnect to a remote host\n");
425 fprintf(stderr, "usage: doldacond-shell [-hr]\n");
431 dcicon = gdk_pixbuf_new_from_xpm_data((const char **)dolda_icon_xpm);
432 gtk_window_set_default_icon(dcicon);
444 #include "dsh-start.gtk"
445 #include "dsh-menu.gtk"