Commit | Line | Data |
---|---|---|
34d45a15 FT |
1 | /* |
2 | * Dolda Connect - Modular multiuser Direct Connect-style client | |
3 | * Copyright (C) 2007 Fredrik Tolf <fredrik@dolda2000.com> | |
4 | * | |
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. | |
9 | * | |
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. | |
14 | * | |
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 | |
18 | */ | |
19 | ||
20 | #include <stdlib.h> | |
21 | #include <stdio.h> | |
22 | #include <unistd.h> | |
23 | #include <string.h> | |
24 | #include <errno.h> | |
25 | #include <pwd.h> | |
26 | #include <locale.h> | |
27 | #include <libintl.h> | |
28 | #include <signal.h> | |
29 | #include <sys/wait.h> | |
30 | #include <gtk/gtk.h> | |
31 | #include <gdk/gdkkeysyms.h> | |
32 | #include <stdarg.h> | |
33 | #include <doldaconnect/uilib.h> | |
34 | #include <doldaconnect/uimisc.h> | |
35 | #include <doldaconnect/utils.h> | |
36 | ||
37 | #ifdef HAVE_CONFIG_H | |
38 | #include <config.h> | |
39 | #endif | |
40 | ||
41 | #ifdef HAVE_NOTIFY | |
42 | #include <libnotify/notify.h> | |
43 | #endif | |
44 | ||
45 | #define _(text) gettext(text) | |
46 | ||
47 | struct trinfo { | |
48 | int ostate; | |
fa9a8ac1 FT |
49 | int opos; |
50 | time_t lastprog; | |
34d45a15 FT |
51 | }; |
52 | ||
53 | void updatewrite(void); | |
54 | ||
71aeadfc | 55 | int remote = 0; |
34d45a15 FT |
56 | GtkStatusIcon *tray; |
57 | pid_t dpid = 0, dcpid = 0; | |
58 | int connected = 0; | |
59 | int dstat; | |
60 | int derrfd, derrtag; | |
61 | char *derrbuf = NULL; | |
62 | size_t derrbufsize = 0, derrbufdata = 0; | |
63 | int dcfd, dcfdrtag, dcfdwtag = -1; | |
64 | GdkPixbuf *dcicon; | |
65 | #ifdef HAVE_NOTIFY | |
66 | NotifyNotification *trnote = NULL; | |
67 | #endif | |
68 | ||
69 | #include "dsh-start.gtkh" | |
70 | #include "dsh-menu.gtkh" | |
71 | ||
72 | int running(char *pf) | |
73 | { | |
74 | FILE *pfs; | |
75 | char buf[1024]; | |
76 | int pid; | |
77 | ||
78 | if((pfs = fopen(pf, "r")) == NULL) { | |
79 | perror(pf); | |
80 | return(0); | |
81 | } | |
82 | fgets(buf, sizeof(buf), pfs); | |
83 | fclose(pfs); | |
84 | if((pid = atoi(buf)) == 0) | |
85 | return(0); | |
86 | return(!kill(pid, 0)); | |
87 | } | |
88 | ||
89 | void derrcb(gpointer data, gint source, GdkInputCondition cond) | |
90 | { | |
91 | int ret = 0; | |
92 | ||
93 | sizebuf2(derrbuf, derrbufdata + 1024, 1); | |
94 | ret = read(derrfd, derrbuf + derrbufdata, derrbufsize - derrbufdata); | |
95 | if(ret <= 0) { | |
96 | if(ret < 0) | |
97 | bprintf(derrbuf, "\ncould not read from daemon: %s\n", strerror(errno)); | |
98 | gdk_input_remove(derrtag); | |
99 | close(derrfd); | |
100 | derrfd = -1; | |
101 | } else { | |
102 | derrbufdata += ret; | |
103 | } | |
104 | } | |
105 | ||
106 | int msgbox(int type, int buttons, char *format, ...) | |
107 | { | |
108 | GtkWidget *swnd; | |
109 | va_list args; | |
110 | char *buf; | |
111 | int resp; | |
112 | ||
113 | va_start(args, format); | |
114 | buf = vsprintf2(format, args); | |
115 | va_end(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); | |
119 | free(buf); | |
120 | return(resp); | |
121 | } | |
122 | ||
123 | void destroytr(struct dc_transfer *tr) | |
124 | { | |
125 | struct trinfo *tri; | |
126 | ||
127 | tri = tr->udata; | |
128 | free(tri); | |
129 | } | |
130 | ||
131 | void inittr(struct dc_transfer *tr) | |
132 | { | |
133 | struct trinfo *tri; | |
134 | ||
135 | tr->udata = tri = memset(smalloc(sizeof(*tri)), 0, sizeof(*tri)); | |
136 | tr->destroycb = destroytr; | |
137 | tri->ostate = tr->state; | |
fa9a8ac1 FT |
138 | tri->opos = tr->curpos; |
139 | tri->lastprog = time(NULL); | |
34d45a15 FT |
140 | } |
141 | ||
142 | #ifdef HAVE_NOTIFY | |
143 | void notify(NotifyNotification **n, char *cat, char *title, char *body, ...) | |
144 | { | |
145 | va_list args; | |
146 | char *bbuf; | |
147 | ||
148 | va_start(args, body); | |
149 | bbuf = vsprintf2(body, args); | |
150 | va_end(args); | |
151 | if(*n == NULL) { | |
152 | *n = notify_notification_new_with_status_icon(title, bbuf, NULL, tray); | |
153 | notify_notification_set_icon_from_pixbuf(*n, dcicon); | |
154 | } else { | |
155 | notify_notification_update(*n, title, bbuf, NULL); | |
156 | } | |
157 | notify_notification_show(*n, NULL); | |
158 | } | |
159 | #endif | |
160 | ||
fa9a8ac1 FT |
161 | /* XXX: Achtung! Too DC-specific! */ |
162 | wchar_t *getfilename(wchar_t *path) | |
163 | { | |
164 | wchar_t *p; | |
165 | ||
166 | if((p = wcsrchr(path, L'\\')) == NULL) | |
167 | return(path); | |
168 | else | |
169 | return(p + 1); | |
170 | } | |
171 | ||
34d45a15 FT |
172 | void trstatechange(struct dc_transfer *tr, int ostate) |
173 | { | |
fa9a8ac1 | 174 | if((ostate == DC_TRNS_MAIN) && (tr->dir == DC_TRNSD_DOWN)) { |
34d45a15 FT |
175 | if(tr->state == DC_TRNS_DONE) { |
176 | #ifdef HAVE_NOTIFY | |
43edea91 FT |
177 | if(dcpid == 0) |
178 | notify(&trnote, "transfer.complete", _("Transfer complete"), _("Finished downloading %ls from %ls"), getfilename(tr->path), tr->peernick); | |
34d45a15 FT |
179 | #endif |
180 | } else { | |
181 | #ifdef HAVE_NOTIFY | |
43edea91 FT |
182 | if(dcpid == 0) |
183 | notify(&trnote, "transfer.error", _("Transfer interrupted"), _("The transfer of %ls from %ls was interrupted from the other side"), getfilename(tr->path), tr->peernick); | |
34d45a15 FT |
184 | #endif |
185 | } | |
186 | } | |
187 | } | |
188 | ||
189 | void updatetrinfo(void) | |
190 | { | |
191 | struct dc_transfer *tr; | |
192 | struct trinfo *tri; | |
fa9a8ac1 | 193 | time_t now; |
34d45a15 | 194 | |
fa9a8ac1 | 195 | now = time(NULL); |
34d45a15 FT |
196 | for(tr = dc_transfers; tr != NULL; tr = tr->next) { |
197 | if(tr->udata == NULL) { | |
198 | inittr(tr); | |
199 | } else { | |
200 | tri = tr->udata; | |
201 | if(tri->ostate != tr->state) { | |
202 | trstatechange(tr, tri->ostate); | |
203 | tri->ostate = tr->state; | |
204 | } | |
fa9a8ac1 FT |
205 | if(tri->opos != tr->curpos) { |
206 | tri->opos = tr->curpos; | |
207 | tri->lastprog = now; | |
208 | } | |
209 | #ifdef NOTIFY | |
210 | if((tr->state = DC_TRNS_MAIN) && (now - tri->lastprog > 600)) { | |
43edea91 FT |
211 | if(dcpid == 0) |
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); | |
fa9a8ac1 FT |
213 | } |
214 | #endif | |
34d45a15 FT |
215 | } |
216 | } | |
217 | } | |
218 | ||
219 | void trlscb(int resp, void *data) | |
220 | { | |
221 | updatetrinfo(); | |
222 | } | |
223 | ||
224 | void logincb(int err, wchar_t *reason, void *data) | |
225 | { | |
226 | if(err != DC_LOGIN_ERR_SUCCESS) { | |
227 | msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server")); | |
228 | exit(1); | |
229 | } | |
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); | |
233 | connected = 1; | |
234 | updatewrite(); | |
235 | } | |
236 | ||
237 | void dcfdcb(gpointer data, gint source, GdkInputCondition cond) | |
238 | { | |
239 | struct dc_response *resp; | |
240 | ||
241 | if(((cond & GDK_INPUT_READ) && dc_handleread()) || ((cond & GDK_INPUT_WRITE) && dc_handlewrite())) { | |
242 | if(errno == 0) { | |
243 | gtk_main_quit(); | |
244 | } else { | |
245 | msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server: %s"), strerror(errno)); | |
246 | exit(1); | |
247 | } | |
248 | return; | |
249 | } | |
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")); | |
254 | exit(1); | |
255 | } else if(dc_checkprotocol(resp, DC_LATEST)) { | |
256 | msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Server protocol revision mismatch")); | |
257 | exit(1); | |
258 | } else { | |
259 | gtk_status_icon_set_tooltip(tray, _("Authenticating...")); | |
260 | dc_loginasync(NULL, 1, dc_convnone, logincb, NULL); | |
261 | } | |
262 | } else if(!wcscmp(resp->cmdname, L".notify")) { | |
263 | dc_uimisc_handlenotify(resp); | |
264 | updatetrinfo(); | |
265 | } | |
266 | } | |
267 | updatewrite(); | |
268 | } | |
269 | ||
270 | void updatewrite(void) | |
271 | { | |
272 | if(dcfd < 0) | |
273 | return; | |
274 | if(dc_wantwrite()) { | |
275 | if(dcfdwtag == -1) | |
276 | dcfdwtag = gdk_input_add(dcfd, GDK_INPUT_WRITE, dcfdcb, NULL); | |
277 | } else { | |
278 | if(dcfdwtag != -1) { | |
279 | gdk_input_remove(dcfdwtag); | |
280 | dcfdwtag = -1; | |
281 | } | |
282 | } | |
283 | } | |
284 | ||
285 | void connectdc(void) | |
286 | { | |
71aeadfc | 287 | if((dcfd = dc_connect(remote?NULL:dc_srv_local)) < 0) { |
34d45a15 FT |
288 | msgbox(GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Could not connect to server: %s"), strerror(errno)); |
289 | exit(1); | |
290 | } | |
291 | dcfdrtag = gdk_input_add(dcfd, GDK_INPUT_READ, dcfdcb, NULL); | |
292 | updatewrite(); | |
293 | gtk_status_icon_set_tooltip(tray, _("Connecting to server...")); | |
294 | } | |
295 | ||
296 | void startdaemon(void) | |
297 | { | |
298 | char pf[1024]; | |
299 | int pfd[2], i; | |
300 | ||
301 | if(getenv("HOME") != NULL) | |
302 | snprintf(pf, sizeof(pf), "%s/.doldacond.pid", getenv("HOME")); | |
303 | else | |
304 | snprintf(pf, sizeof(pf), "%s/.doldacond.pid", getpwuid(getuid())->pw_dir); | |
305 | if(access(pf, F_OK) || !running(pf)) { | |
306 | pipe(pfd); | |
307 | if((dpid = fork()) == 0) { | |
308 | dup2(pfd[1], 2); | |
309 | for(i = 3; i < FD_SETSIZE; i++) | |
310 | close(i); | |
311 | execlp("doldacond", "doldacond", "-p", pf, NULL); | |
312 | perror("doldacond"); | |
313 | exit(127); | |
314 | } | |
315 | if(dpid == -1) | |
316 | abort(); | |
317 | close(pfd[1]); | |
318 | derrfd = pfd[0]; | |
319 | derrtag = gdk_input_add(derrfd, GDK_INPUT_READ, derrcb, NULL); | |
320 | create_start_wnd(); | |
321 | gtk_widget_show(start_wnd); | |
322 | gtk_status_icon_set_tooltip(tray, _("Starting...")); | |
323 | } else { | |
324 | connectdc(); | |
325 | } | |
326 | } | |
327 | ||
328 | gboolean daemonized(gpointer uu) | |
329 | { | |
330 | gtk_widget_hide(start_wnd); | |
331 | dpid = 0; | |
332 | if(derrfd != -1) { | |
333 | gdk_input_remove(derrtag); | |
334 | close(derrfd); | |
335 | } | |
336 | if(dstat != 0) { | |
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); | |
340 | } else { | |
341 | connectdc(); | |
342 | } | |
343 | return(FALSE); | |
344 | } | |
345 | ||
346 | void sighandler(int sig) | |
347 | { | |
348 | pid_t p; | |
349 | int status; | |
350 | ||
351 | if(sig == SIGCHLD) { | |
352 | while((p = waitpid(-1, &status, WNOHANG)) > 0) { | |
353 | if(p == dpid) { | |
354 | dstat = status; | |
355 | gtk_timeout_add(1, daemonized, NULL); | |
356 | } else if(p == dcpid) { | |
357 | dcpid = 0; | |
358 | } | |
359 | } | |
360 | } | |
361 | } | |
362 | ||
363 | void dolcon(void) | |
364 | { | |
365 | int i; | |
366 | ||
367 | if((dcpid = fork()) == 0) { | |
368 | for(i = 3; i < FD_SETSIZE; i++) | |
369 | close(i); | |
71aeadfc FT |
370 | if(remote) |
371 | execlp("dolcon", "dolcon", NULL); | |
372 | else | |
373 | execlp("dolcon", "dolcon", "-l", NULL); | |
34d45a15 FT |
374 | perror("dolcon"); |
375 | exit(127); | |
376 | } | |
377 | } | |
378 | ||
379 | void cb_shm_dolcon_activate(GtkWidget *uu1, gpointer uu2) | |
380 | { | |
381 | dolcon(); | |
382 | } | |
383 | ||
384 | void cb_shm_quit_activate(GtkWidget *uu1, gpointer uu2) | |
385 | { | |
386 | dc_queuecmd(NULL, NULL, L"shutdown", NULL); | |
387 | updatewrite(); | |
388 | } | |
389 | ||
390 | void tray_activate(GtkStatusIcon *uu1, gpointer uu2) | |
391 | { | |
392 | if(dpid != 0) { | |
393 | gtk_widget_show(start_wnd); | |
394 | } else if(connected) { | |
395 | dolcon(); | |
396 | } | |
397 | } | |
398 | ||
399 | void tray_popup(GtkStatusIcon *uu1, guint button, guint time, gpointer uu2) | |
400 | { | |
401 | gtk_menu_popup(GTK_MENU(shm_menu), NULL, NULL, NULL, NULL, button, time); | |
402 | } | |
403 | ||
404 | void cb_start_hide_clicked(GtkWidget *uu1, gpointer uu2) | |
405 | { | |
406 | gtk_widget_hide(start_wnd); | |
407 | } | |
408 | ||
409 | void cb_start_abort_clicked(GtkWidget *uu1, gpointer uu2) | |
410 | { | |
411 | kill(dpid, SIGINT); | |
412 | exit(0); | |
413 | } | |
414 | ||
415 | void cb_start_errok_clicked(GtkWidget *uu1, gpointer uu2) | |
416 | { | |
417 | gtk_main_quit(); | |
418 | } | |
419 | ||
420 | #include "../dolda-icon.xpm" | |
421 | ||
422 | void inittray(void) | |
423 | { | |
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); | |
428 | } | |
429 | ||
430 | int main(int argc, char **argv) | |
431 | { | |
71aeadfc FT |
432 | int c; |
433 | ||
34d45a15 FT |
434 | setlocale(LC_ALL, ""); |
435 | bindtextdomain(PACKAGE, LOCALEDIR); | |
436 | textdomain(PACKAGE); | |
437 | signal(SIGCHLD, sighandler); | |
438 | dc_init(); | |
439 | gtk_init(&argc, &argv); | |
440 | #ifdef HAVE_NOTIFY | |
441 | notify_init("Dolda Connect"); | |
442 | #endif | |
71aeadfc FT |
443 | while((c = getopt(argc, argv, "rh")) != -1) { |
444 | switch(c) { | |
445 | case 'r': | |
446 | remote = 1; | |
447 | break; | |
448 | case 'h': | |
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"); | |
452 | exit(0); | |
453 | default: | |
454 | fprintf(stderr, "usage: doldacond-shell [-hr]\n"); | |
455 | exit(1); | |
456 | } | |
457 | } | |
34d45a15 FT |
458 | |
459 | create_shm_wnd(); | |
460 | dcicon = gdk_pixbuf_new_from_xpm_data((const char **)dolda_icon_xpm); | |
461 | gtk_window_set_default_icon(dcicon); | |
462 | inittray(); | |
71aeadfc FT |
463 | if(remote) |
464 | connectdc(); | |
465 | else | |
466 | startdaemon(); | |
34d45a15 FT |
467 | |
468 | gtk_main(); | |
469 | ||
470 | return(0); | |
471 | } | |
472 | ||
473 | #include "dsh-start.gtk" | |
474 | #include "dsh-menu.gtk" |