Add dolconf to dsh menu.
[doldaconnect.git] / clients / gui-shell / dsh.c
CommitLineData
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
47struct trinfo {
48 int ostate;
fa9a8ac1
FT
49 int opos;
50 time_t lastprog;
34d45a15
FT
51};
52
53void updatewrite(void);
54
71aeadfc 55int remote = 0;
34d45a15
FT
56GtkStatusIcon *tray;
57pid_t dpid = 0, dcpid = 0;
58int connected = 0;
59int dstat;
60int derrfd, derrtag;
61char *derrbuf = NULL;
62size_t derrbufsize = 0, derrbufdata = 0;
63int dcfd, dcfdrtag, dcfdwtag = -1;
64GdkPixbuf *dcicon;
65#ifdef HAVE_NOTIFY
66NotifyNotification *trnote = NULL;
67#endif
68
69#include "dsh-start.gtkh"
70#include "dsh-menu.gtkh"
71
72int 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
89void 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
106int 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
123void destroytr(struct dc_transfer *tr)
124{
125 struct trinfo *tri;
126
127 tri = tr->udata;
128 free(tri);
129}
130
131void 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
143void 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! */
162wchar_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
172void 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
189void 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
219void trlscb(int resp, void *data)
220{
221 updatetrinfo();
222}
223
224void 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
237void 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
270void 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
285void 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
296void 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
328gboolean 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
346void 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
363void 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
31a01fdd
FT
379void cb_shm_dolconf_activate(GtkWidget *uu1, gpointer uu2)
380{
381 int i;
382
383 if((dcpid = fork()) == 0) {
384 for(i = 3; i < FD_SETSIZE; i++)
385 close(i);
386 execlp("dolconf", "dolconf", NULL);
387 perror("dolconf");
388 exit(127);
389 }
390}
391
34d45a15
FT
392void cb_shm_dolcon_activate(GtkWidget *uu1, gpointer uu2)
393{
394 dolcon();
395}
396
397void cb_shm_quit_activate(GtkWidget *uu1, gpointer uu2)
398{
399 dc_queuecmd(NULL, NULL, L"shutdown", NULL);
400 updatewrite();
401}
402
403void tray_activate(GtkStatusIcon *uu1, gpointer uu2)
404{
405 if(dpid != 0) {
406 gtk_widget_show(start_wnd);
407 } else if(connected) {
408 dolcon();
409 }
410}
411
412void tray_popup(GtkStatusIcon *uu1, guint button, guint time, gpointer uu2)
413{
414 gtk_menu_popup(GTK_MENU(shm_menu), NULL, NULL, NULL, NULL, button, time);
415}
416
417void cb_start_hide_clicked(GtkWidget *uu1, gpointer uu2)
418{
419 gtk_widget_hide(start_wnd);
420}
421
422void cb_start_abort_clicked(GtkWidget *uu1, gpointer uu2)
423{
424 kill(dpid, SIGINT);
425 exit(0);
426}
427
428void cb_start_errok_clicked(GtkWidget *uu1, gpointer uu2)
429{
430 gtk_main_quit();
431}
432
433#include "../dolda-icon.xpm"
434
435void inittray(void)
436{
437 tray = gtk_status_icon_new_from_pixbuf(gdk_pixbuf_scale_simple(dcicon, 24, 24, GDK_INTERP_BILINEAR));
438 gtk_status_icon_set_tooltip(tray, "");
439 g_signal_connect(G_OBJECT(tray), "activate", G_CALLBACK(tray_activate), (gpointer)NULL);
440 g_signal_connect(G_OBJECT(tray), "popup-menu", G_CALLBACK(tray_popup), (gpointer)NULL);
441}
442
443int main(int argc, char **argv)
444{
71aeadfc
FT
445 int c;
446
34d45a15
FT
447 setlocale(LC_ALL, "");
448 bindtextdomain(PACKAGE, LOCALEDIR);
449 textdomain(PACKAGE);
450 signal(SIGCHLD, sighandler);
451 dc_init();
452 gtk_init(&argc, &argv);
453#ifdef HAVE_NOTIFY
454 notify_init("Dolda Connect");
455#endif
71aeadfc
FT
456 while((c = getopt(argc, argv, "rh")) != -1) {
457 switch(c) {
458 case 'r':
459 remote = 1;
460 break;
461 case 'h':
462 printf("usage: doldacond-shell [-hr]\n");
463 printf("\t-h\tDisplay this help message\n");
464 printf("\t-r\tConnect to a remote host\n");
465 exit(0);
466 default:
467 fprintf(stderr, "usage: doldacond-shell [-hr]\n");
468 exit(1);
469 }
470 }
34d45a15
FT
471
472 create_shm_wnd();
473 dcicon = gdk_pixbuf_new_from_xpm_data((const char **)dolda_icon_xpm);
474 gtk_window_set_default_icon(dcicon);
475 inittray();
71aeadfc
FT
476 if(remote)
477 connectdc();
478 else
479 startdaemon();
34d45a15
FT
480
481 gtk_main();
482
483 return(0);
484}
485
486#include "dsh-start.gtk"
487#include "dsh-menu.gtk"