Do not require specific installation path.
[udpgate.git] / src / ui-gnome.c
1 /* $Id$
2  * Gnome user interface
3  * Copyright (C) 2004 Jan Kratochvil <project-udpgate@jankratochvil.net>
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; exactly version 2 of June 1991 is required
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #include "config.h"
21
22 #include "ui-gnome.h"   /* self */
23 #include <glib/gmessages.h>
24 #include "main.h"
25 #include <libgnomeui/gnome-app.h>
26 #include <sys/types.h>
27 #include <signal.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <libgnomeui/gnome-appbar.h>
31 #include <gtk/gtkbutton.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <gtk/gtkentry.h>
35 #include <gtk/gtktogglebutton.h>
36 #include <string.h>
37 #include <gtk/gtkmain.h>
38 #include <libgnomeui/gnome-app-util.h>
39 #include <gtk/gtklabel.h>
40 #include <gtk/gtkcheckbutton.h>
41
42 #include "network.h"
43 #include "packet.h"
44 #include "startup.h"
45
46
47 /* Config: */
48 #define DAEMON_CHECK_INTERVAL_MS 100
49 #define EXTERNAL_STARTUP_CHECK_INTERVAL_MS 1000
50 #define PORT_RANGE_BEGIN 2048
51 #define PORT_RANGE_END 10240
52
53
54 static GnomeApp *App;
55 static GtkButton *ButtonStart;
56 static GtkButton *ButtonStop;
57 static GtkHBox *PortHBox;
58 static GnomeAppBar *AppBar;
59 static GtkEntry *PortEntry;
60 static GtkEntry *HostIPEntry;
61 static GtkLabel *AutostartLabel;
62 static GtkCheckButton *AutostartCheckButton;
63
64
65 static void state_start_stop(void)
66 {
67 pid_t daemon_pid;
68 gboolean daemon_running;
69 static gboolean last_daemon_running,last_daemon_running_set=FALSE;
70
71         daemon_pid=is_daemon_running();
72         daemon_running=((pid_t)-1!=daemon_pid);
73
74         /* Cache the result; maybe not needed. */
75         if (last_daemon_running_set && last_daemon_running==daemon_running)
76                 return;
77         last_daemon_running=daemon_running;
78         last_daemon_running_set=TRUE;
79
80         gtk_widget_set_sensitive(GTK_WIDGET(ButtonStart),!daemon_running);
81         gtk_widget_set_sensitive(GTK_WIDGET(ButtonStop) , daemon_running);
82         gtk_widget_set_sensitive(GTK_WIDGET(PortHBox)   ,!daemon_running);
83         if (daemon_running)
84                 gnome_appbar_set_status(AppBar,
85                                 udpgate_printf_alloca(_("udpgate daemon running as PID %d."),(int)daemon_pid));
86         else
87                 gnome_appbar_set_status(AppBar,_("No udpgate daemon currently running."));
88 }
89
90 static gboolean daemon_check_timeout_func(gpointer data /* unused */)
91 {
92         if (!App)       /* Quitting? */
93                 return FALSE;   /* stop running */
94
95         state_start_stop();
96         return TRUE;    /* continue running */
97 }
98
99 static gboolean external_startup_check_timeout_func(gpointer data /* unused */)
100 {
101 gboolean state_startup_is_on;
102
103         if (!App)       /* Quitting? */
104                 return FALSE;   /* stop running */
105
106         if (startup_query(&state_startup_is_on))
107                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(AutostartCheckButton),state_startup_is_on);
108         return TRUE;    /* continue running */
109 }
110
111 void on_PortButtonRandom_clicked(GtkButton *button,gpointer user_data)
112 {
113         g_return_if_fail(GTK_IS_BUTTON(button));
114
115         state_start_stop();
116         if ((pid_t)-1!=is_daemon_running())
117                 return;
118         gtk_entry_set_text(PortEntry,
119                         udpgate_printf_alloca("%d",(int)g_random_int_range(PORT_RANGE_BEGIN,PORT_RANGE_END)));
120 }
121
122 void on_AutostartCheckButton_toggled(GtkToggleButton *togglebutton,gpointer user_data)
123 {
124 static gint inside=0;
125
126         g_return_if_fail(GTK_IS_TOGGLE_BUTTON(togglebutton));
127
128         /* Avoid reentrancy to prevent segfault during failed registration.
129          * FIXME: Who knows why? Some forbidden GTK recursion occurs.
130          */
131         g_assert(inside>=0);
132         if (inside)
133                 return;
134         inside++;
135
136         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(AutostartCheckButton)))
137                 startup_on();
138         else
139                 startup_off();
140         external_startup_check_timeout_func(NULL);      /* data; unused */
141
142         g_assert(inside==1);
143         inside--;
144 }
145
146 void on_ButtonStart_clicked(GtkButton *button,gpointer user_data)
147 {
148         g_return_if_fail(GTK_IS_BUTTON(button));
149
150         if (!optarg_port_set_string(gtk_entry_get_text(PortEntry)))
151                 return;
152         network_start(optarg_port);
153 }
154
155 void on_ButtonStop_clicked(GtkButton *button,gpointer user_data)
156 {
157         g_return_if_fail(GTK_IS_BUTTON(button));
158
159         network_stop();
160 }
161
162 void on_ButtonHide_clicked(GtkButton *button,gpointer user_data)
163 {
164         g_return_if_fail(GTK_IS_BUTTON(button));
165
166         /* update config file */
167         optarg_port_set_string(gtk_entry_get_text(PortEntry));
168
169         /* Do not: gtk_main_quit();
170          * as 'App' widget will quit our gtk_main() automatically.
171          */
172         gtk_widget_destroy(GTK_WIDGET(App));
173         App=NULL;
174 }
175
176 static void ui_gnome_network_notify_hostip(guint32 hostip_guint32)
177 {
178         if (!App)       /* Quitting? */
179                 return;
180
181         if (!hostip_guint32) {
182 pid_t daemon_pid;
183
184                 daemon_pid=is_daemon_running();
185                 if ((pid_t)-1==daemon_pid)
186                         gtk_entry_set_text(HostIPEntry,_("(unknown; Start the daemon)"));
187                 else if (getpid()==daemon_pid)
188                         gtk_entry_set_text(HostIPEntry,_("(unknown; detecting...)"));
189                 else
190                         gtk_entry_set_text(HostIPEntry,_("(unknown; Kill the daemon and start your own)"));
191                 }
192         else {
193                 gtk_entry_set_text(HostIPEntry,HOSTIP_GUINT32_TO_STRING(hostip_guint32));
194                 }
195 }
196
197 static guint ui_gnome_g_log_handler_handler_id;
198 static void ui_gnome_g_log_handler(const gchar *log_domain,GLogLevelFlags log_level,const gchar *message,gpointer user_data)
199 {
200 GtkWidget *dialog;
201
202         if (!App)       /* Quitting? */
203                 return;
204
205         /**/ if (log_level & G_LOG_LEVEL_ERROR)
206                 dialog=gnome_app_error(App,message);
207         else if (log_level & (G_LOG_LEVEL_CRITICAL|G_LOG_LEVEL_WARNING))
208                 dialog=gnome_app_warning(App,message);
209         else
210                 dialog=gnome_app_message(App,message);
211
212         gtk_window_set_modal(GTK_WINDOW(dialog),TRUE);
213         g_signal_connect((gpointer)dialog,"close",G_CALLBACK(gtk_main_quit),NULL);
214         gtk_main();
215         /* 'dialog' gets destroyed automatically */
216 }
217
218 static void ui_gnome_interactive(void)
219 {
220         gtk_main();
221         network_notify_hostip=NULL;
222         g_log_remove_handler(
223                         G_LOG_DOMAIN,   /* log_domain; "Captive" */
224                         ui_gnome_g_log_handler_handler_id);     /* handler_id */
225         network_detach();
226 }
227
228 /* of "ui-gnome-interface.h": */
229 GtkWidget *create_App(void);
230 /* of "ui-gnome-support.h": */
231 GtkWidget *lookup_widget(GtkWidget *widget,const gchar *widget_name);
232
233 gboolean ui_gnome_init(void)
234 {
235         App=GNOME_APP(create_App());
236
237         ButtonStart=GTK_BUTTON(lookup_widget(GTK_WIDGET(App),"ButtonStart"));
238         ButtonStop=GTK_BUTTON(lookup_widget(GTK_WIDGET(App),"ButtonStop"));
239         PortHBox=GTK_HBOX(lookup_widget(GTK_WIDGET(App),"PortHBox"));
240         AppBar=GNOME_APPBAR(lookup_widget(GTK_WIDGET(App),"AppBar"));
241         PortEntry=GTK_ENTRY(lookup_widget(GTK_WIDGET(App),"PortEntry"));
242         HostIPEntry=GTK_ENTRY(lookup_widget(GTK_WIDGET(App),"HostIPEntry"));
243         AutostartLabel=GTK_LABEL(lookup_widget(GTK_WIDGET(App),"AutostartLabel"));
244         AutostartCheckButton=GTK_CHECK_BUTTON(lookup_widget(GTK_WIDGET(App),"AutostartCheckButton"));
245
246         /* ui_gnome_g_log_handler() needs 'App'. */
247         ui_gnome_g_log_handler_handler_id=g_log_set_handler(
248                         G_LOG_DOMAIN,   /* log_domain; "Captive" */
249                         (G_LOG_LEVEL_MASK|G_LOG_FLAG_FATAL)&~(0
250                                         |G_LOG_LEVEL_MESSAGE
251                                         |G_LOG_LEVEL_INFO
252                                         |G_LOG_LEVEL_DEBUG),    /* log_levels */
253                         ui_gnome_g_log_handler, /* log_func */
254                         NULL);  /* user_data */
255
256         ui_gnome_network_notify_hostip(0);
257         gtk_entry_set_text(PortEntry,udpgate_printf_alloca("%d",(int)optarg_port));
258         if (!startup_init()) {
259                 gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(AutostartCheckButton),TRUE);
260                 gtk_widget_set_sensitive(GTK_WIDGET(AutostartLabel),FALSE);
261                 gtk_widget_set_sensitive(GTK_WIDGET(AutostartCheckButton),FALSE);
262                 }
263         daemon_check_timeout_func(NULL);        /* data; unused */
264         external_startup_check_timeout_func(NULL);      /* data; unused */
265
266         gtk_widget_show_all(GTK_WIDGET(App));
267         g_timeout_add(
268                         DAEMON_CHECK_INTERVAL_MS,       /* interval */
269                         daemon_check_timeout_func,      /* function */
270                         NULL);  /* data; unused */
271         g_timeout_add(
272                         EXTERNAL_STARTUP_CHECK_INTERVAL_MS,     /* interval */
273                         external_startup_check_timeout_func,    /* function */
274                         NULL);  /* data; unused */
275
276         network_notify_hostip=ui_gnome_network_notify_hostip;
277
278         ui_interactive=ui_gnome_interactive;
279
280         return TRUE;
281 }