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);
54 pid_t dpid = 0, dcpid = 0;
59 size_t derrbufsize = 0, derrbufdata = 0;
60 int dcfd, dcfdrtag, dcfdwtag = -1;
63 NotifyNotification *trnote = NULL;
66 #include "dsh-start.gtkh"
67 #include "dsh-menu.gtkh"
75 if((pfs = fopen(pf, "r")) == NULL) {
79 fgets(buf, sizeof(buf), pfs);
81 if((pid = atoi(buf)) == 0)
83 return(!kill(pid, 0));
86 void derrcb(gpointer data, gint source, GdkInputCondition cond)
90 sizebuf2(derrbuf, derrbufdata + 1024, 1);
91 ret = read(derrfd, derrbuf + derrbufdata, derrbufsize - derrbufdata);
94 bprintf(derrbuf, "\ncould not read from daemon: %s\n", strerror(errno));
95 gdk_input_remove(derrtag);
103 int msgbox(int type, int buttons, char *format, ...)
110 va_start(args, format);
111 buf = vsprintf2(format, args);
113 swnd = gtk_message_dialog_new(NULL, 0, type, buttons, "%s", buf);
114 resp = gtk_dialog_run(GTK_DIALOG(swnd));
115 gtk_widget_destroy(swnd);
120 void destroytr(struct dc_transfer *tr)
128 void inittr(struct dc_transfer *tr)
132 tr->udata = tri = memset(smalloc(sizeof(*tri)), 0, sizeof(*tri));
133 tr->destroycb = destroytr;
134 tri->ostate = tr->state;
138 void notify(NotifyNotification **n, char *cat, char *title, char *body, ...)
143 va_start(args, body);
144 bbuf = vsprintf2(body, args);
147 *n = notify_notification_new_with_status_icon(title, bbuf, NULL, tray);
148 notify_notification_set_icon_from_pixbuf(*n, dcicon);
150 notify_notification_update(*n, title, bbuf, NULL);
152 notify_notification_show(*n, NULL);
156 void trstatechange(struct dc_transfer *tr, int ostate)
158 if(ostate == DC_TRNS_MAIN) {
159 if(tr->state == DC_TRNS_DONE) {
161 notify(&trnote, "transfer.complete", _("Transfer complete"), _("Finished downloading %ls from %ls"), tr->path, tr->peernick);
165 notify(&trnote, "transfer.error", _("Transfer interrupted"), _("The transfer of %ls from %ls was interrupted from the other side"), tr->path, tr->peernick);
171 void updatetrinfo(void)
173 struct dc_transfer *tr;
176 for(tr = dc_transfers; tr != NULL; tr = tr->next) {
177 if(tr->udata == NULL) {
181 if(tri->ostate != tr->state) {
182 trstatechange(tr, tri->ostate);
183 tri->ostate = tr->state;
189 void trlscb(int resp, void *data)
194 void logincb(int err, wchar_t *reason, void *data)
196 if(err != DC_LOGIN_ERR_SUCCESS) {
197 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server"));
200 gtk_status_icon_set_tooltip(tray, "Dolda Connect");
201 dc_queuecmd(NULL, NULL, L"notify", L"trans:act", L"on", L"trans:prog", L"on", NULL);
202 dc_gettrlistasync(trlscb, NULL);
207 void dcfdcb(gpointer data, gint source, GdkInputCondition cond)
209 struct dc_response *resp;
211 if(((cond & GDK_INPUT_READ) && dc_handleread()) || ((cond & GDK_INPUT_WRITE) && dc_handlewrite())) {
215 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server: %s"), strerror(errno));
220 while((resp = dc_getresp()) != NULL) {
221 if(!wcscmp(resp->cmdname, L".connect")) {
222 if(resp->code != 201) {
223 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("The server refused the connection"));
225 } else if(dc_checkprotocol(resp, DC_LATEST)) {
226 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Server protocol revision mismatch"));
229 gtk_status_icon_set_tooltip(tray, _("Authenticating..."));
230 dc_loginasync(NULL, 1, dc_convnone, logincb, NULL);
232 } else if(!wcscmp(resp->cmdname, L".notify")) {
233 dc_uimisc_handlenotify(resp);
240 void updatewrite(void)
246 dcfdwtag = gdk_input_add(dcfd, GDK_INPUT_WRITE, dcfdcb, NULL);
249 gdk_input_remove(dcfdwtag);
257 if((dcfd = dc_connect(dc_srv_local)) < 0) {
258 msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server: %s"), strerror(errno));
261 dcfdrtag = gdk_input_add(dcfd, GDK_INPUT_READ, dcfdcb, NULL);
263 gtk_status_icon_set_tooltip(tray, _("Connecting to server..."));
266 void startdaemon(void)
271 if(getenv("HOME") != NULL)
272 snprintf(pf, sizeof(pf), "%s/.doldacond.pid", getenv("HOME"));
274 snprintf(pf, sizeof(pf), "%s/.doldacond.pid", getpwuid(getuid())->pw_dir);
275 if(access(pf, F_OK) || !running(pf)) {
277 if((dpid = fork()) == 0) {
279 for(i = 3; i < FD_SETSIZE; i++)
281 execlp("doldacond", "doldacond", "-p", pf, NULL);
289 derrtag = gdk_input_add(derrfd, GDK_INPUT_READ, derrcb, NULL);
291 gtk_widget_show(start_wnd);
292 gtk_status_icon_set_tooltip(tray, _("Starting..."));
298 gboolean daemonized(gpointer uu)
300 gtk_widget_hide(start_wnd);
303 gdk_input_remove(derrtag);
307 gtk_status_icon_set_visible(tray, FALSE);
308 gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(start_log)), derrbuf, derrbufdata);
309 gtk_widget_show(start_errwnd);
316 void sighandler(int sig)
322 while((p = waitpid(-1, &status, WNOHANG)) > 0) {
325 gtk_timeout_add(1, daemonized, NULL);
326 } else if(p == dcpid) {
337 if((dcpid = fork()) == 0) {
338 for(i = 3; i < FD_SETSIZE; i++)
340 execlp("dolcon", "dolcon", NULL);
346 void cb_shm_dolcon_activate(GtkWidget *uu1, gpointer uu2)
351 void cb_shm_quit_activate(GtkWidget *uu1, gpointer uu2)
353 dc_queuecmd(NULL, NULL, L"shutdown", NULL);
357 void tray_activate(GtkStatusIcon *uu1, gpointer uu2)
360 gtk_widget_show(start_wnd);
361 } else if(connected) {
366 void tray_popup(GtkStatusIcon *uu1, guint button, guint time, gpointer uu2)
368 gtk_menu_popup(GTK_MENU(shm_menu), NULL, NULL, NULL, NULL, button, time);
371 void cb_start_hide_clicked(GtkWidget *uu1, gpointer uu2)
373 gtk_widget_hide(start_wnd);
376 void cb_start_abort_clicked(GtkWidget *uu1, gpointer uu2)
382 void cb_start_errok_clicked(GtkWidget *uu1, gpointer uu2)
387 #include "../dolda-icon.xpm"
391 tray = gtk_status_icon_new_from_pixbuf(gdk_pixbuf_scale_simple(dcicon, 24, 24, GDK_INTERP_BILINEAR));
392 gtk_status_icon_set_tooltip(tray, "");
393 g_signal_connect(G_OBJECT(tray), "activate", G_CALLBACK(tray_activate), (gpointer)NULL);
394 g_signal_connect(G_OBJECT(tray), "popup-menu", G_CALLBACK(tray_popup), (gpointer)NULL);
397 int main(int argc, char **argv)
399 setlocale(LC_ALL, "");
400 bindtextdomain(PACKAGE, LOCALEDIR);
402 signal(SIGCHLD, sighandler);
404 gtk_init(&argc, &argv);
406 notify_init("Dolda Connect");
410 dcicon = gdk_pixbuf_new_from_xpm_data((const char **)dolda_icon_xpm);
411 gtk_window_set_default_icon(dcicon);
420 #include "dsh-start.gtk"
421 #include "dsh-menu.gtk"