Try probing the server indefinitely from commandline.
[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 #define UI_GNOME_PROBE_TIMEOUT_SEC 10
53
54
55 static GnomeApp *App;
56 static GtkButton *ButtonStart;
57 static GtkButton *ButtonStop;
58 static GtkHBox *PortHBox;
59 static GnomeAppBar *AppBar;
60 static GtkEntry *PortEntry;
61 static GtkEntry *HostIPEntry;
62 static GtkLabel *AutostartLabel;
63 static GtkCheckButton *AutostartCheckButton;
64
65
66 static void state_start_stop(void)
67 {
68 pid_t daemon_pid;
69 gboolean daemon_running;
70 static gboolean last_daemon_running,last_daemon_running_set=FALSE;
71
72         daemon_pid=is_daemon_running();
73         daemon_running=((pid_t)-1!=daemon_pid);
74
75         /* Cache the result; maybe not needed. */
76         if (last_daemon_running_set && last_daemon_running==daemon_running)
77                 return;
78         last_daemon_running=daemon_running;
79         last_daemon_running_set=TRUE;
80
81         gtk_widget_set_sensitive(GTK_WIDGET(ButtonStart),!daemon_running);
82         gtk_widget_set_sensitive(GTK_WIDGET(ButtonStop) , daemon_running);
83         gtk_widget_set_sensitive(GTK_WIDGET(PortHBox)   ,!daemon_running);
84         if (daemon_running)
85                 gnome_appbar_set_status(AppBar,
86                                 udpgate_printf_alloca(_("udpgate daemon running as PID %d."),(int)daemon_pid));
87         else
88                 gnome_appbar_set_status(AppBar,_("No udpgate daemon currently running."));
89 }
90
91 static gboolean daemon_check_timeout_func(gpointer data /* unused */)
92 {
93         if (!App)       /* Quitting? */
94                 return FALSE;   /* stop running */
95
96         state_start_stop();
97         return TRUE;    /* continue running */
98 }
99
100 static gboolean external_startup_check_timeout_func(gpointer data /* unused */)
101 {
102 gboolean state_startup_is_on;
103
104         if (!App)       /* Quitting? */
105                 return FALSE;   /* stop running */
106
107         if (startup_query(&state_startup_is_on))
108                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(AutostartCheckButton),state_startup_is_on);
109         return TRUE;    /* continue running */
110 }
111
112 void on_PortButtonRandom_clicked(GtkButton *button,gpointer user_data)
113 {
114         g_return_if_fail(GTK_IS_BUTTON(button));
115
116         state_start_stop();
117         if ((pid_t)-1!=is_daemon_running())
118                 return;
119         gtk_entry_set_text(PortEntry,
120                         udpgate_printf_alloca("%d",(int)g_random_int_range(PORT_RANGE_BEGIN,PORT_RANGE_END)));
121 }
122
123 void on_AutostartCheckButton_toggled(GtkToggleButton *togglebutton,gpointer user_data)
124 {
125 static gint inside=0;
126
127         g_return_if_fail(GTK_IS_TOGGLE_BUTTON(togglebutton));
128
129         /* Avoid reentrancy to prevent segfault during failed registration.
130          * FIXME: Who knows why? Some forbidden GTK recursion occurs.
131          */
132         g_assert(inside>=0);
133         if (inside)
134                 return;
135         inside++;
136
137         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(AutostartCheckButton)))
138                 startup_on();
139         else
140                 startup_off();
141         external_startup_check_timeout_func(NULL);      /* data; unused */
142
143         g_assert(inside==1);
144         inside--;
145 }
146
147 void on_ButtonStart_clicked(GtkButton *button,gpointer user_data)
148 {
149         g_return_if_fail(GTK_IS_BUTTON(button));
150
151         if (!optarg_port_set_string(gtk_entry_get_text(PortEntry)))
152                 return;
153         network_start(optarg_port);
154 }
155
156 void on_ButtonStop_clicked(GtkButton *button,gpointer user_data)
157 {
158         g_return_if_fail(GTK_IS_BUTTON(button));
159
160         network_stop();
161 }
162
163 void on_ButtonHide_clicked(GtkButton *button,gpointer user_data)
164 {
165         g_return_if_fail(GTK_IS_BUTTON(button));
166
167         /* update config file */
168         optarg_port_set_string(gtk_entry_get_text(PortEntry));
169
170         /* Do not: gtk_main_quit();
171          * as 'App' widget will quit our gtk_main() automatically.
172          */
173         gtk_widget_destroy(GTK_WIDGET(App));
174         App=NULL;
175 }
176
177 static void ui_gnome_network_notify_hostip(guint32 hostip_guint32)
178 {
179         if (!App)       /* Quitting? */
180                 return;
181
182         if (!hostip_guint32) {
183 pid_t daemon_pid;
184
185                 daemon_pid=is_daemon_running();
186                 if ((pid_t)-1==daemon_pid)
187                         gtk_entry_set_text(HostIPEntry,_("(unknown; Start the daemon)"));
188                 else if (getpid()==daemon_pid)
189                         gtk_entry_set_text(HostIPEntry,_("(unknown; detecting...)"));
190                 else
191                         gtk_entry_set_text(HostIPEntry,_("(unknown; Kill the daemon and start your own)"));
192                 }
193         else {
194                 gtk_entry_set_text(HostIPEntry,HOSTIP_GUINT32_TO_STRING(hostip_guint32));
195                 }
196 }
197
198 static guint ui_gnome_g_log_handler_handler_id;
199 static void ui_gnome_g_log_handler(const gchar *log_domain,GLogLevelFlags log_level,const gchar *message,gpointer user_data)
200 {
201 GtkWidget *dialog;
202
203         if (!App)       /* Quitting? */
204                 return;
205
206         /**/ if (log_level & G_LOG_LEVEL_ERROR)
207                 dialog=gnome_app_error(App,message);
208         else if (log_level & (G_LOG_LEVEL_CRITICAL|G_LOG_LEVEL_WARNING))
209                 dialog=gnome_app_warning(App,message);
210         else
211                 dialog=gnome_app_message(App,message);
212
213         gtk_window_set_modal(GTK_WINDOW(dialog),TRUE);
214         g_signal_connect((gpointer)dialog,"close",G_CALLBACK(gtk_main_quit),NULL);
215         gtk_main();
216         /* 'dialog' gets destroyed automatically */
217 }
218
219 static void ui_gnome_interactive(void)
220 {
221         probe_timeout_sec_max=UI_GNOME_PROBE_TIMEOUT_SEC;
222         gtk_main();
223         network_notify_hostip=NULL;
224         g_log_remove_handler(
225                         G_LOG_DOMAIN,   /* log_domain; "Captive" */
226                         ui_gnome_g_log_handler_handler_id);     /* handler_id */
227         network_detach();
228 }
229
230 /* of "ui-gnome-interface.h": */
231 GtkWidget *create_App(void);
232 /* of "ui-gnome-support.h": */
233 GtkWidget *lookup_widget(GtkWidget *widget,const gchar *widget_name);
234
235 gboolean ui_gnome_init(void)
236 {
237         App=GNOME_APP(create_App());
238
239         ButtonStart=GTK_BUTTON(lookup_widget(GTK_WIDGET(App),"ButtonStart"));
240         ButtonStop=GTK_BUTTON(lookup_widget(GTK_WIDGET(App),"ButtonStop"));
241         PortHBox=GTK_HBOX(lookup_widget(GTK_WIDGET(App),"PortHBox"));
242         AppBar=GNOME_APPBAR(lookup_widget(GTK_WIDGET(App),"AppBar"));
243         PortEntry=GTK_ENTRY(lookup_widget(GTK_WIDGET(App),"PortEntry"));
244         HostIPEntry=GTK_ENTRY(lookup_widget(GTK_WIDGET(App),"HostIPEntry"));
245         AutostartLabel=GTK_LABEL(lookup_widget(GTK_WIDGET(App),"AutostartLabel"));
246         AutostartCheckButton=GTK_CHECK_BUTTON(lookup_widget(GTK_WIDGET(App),"AutostartCheckButton"));
247
248         /* ui_gnome_g_log_handler() needs 'App'. */
249         ui_gnome_g_log_handler_handler_id=g_log_set_handler(
250                         G_LOG_DOMAIN,   /* log_domain; "Captive" */
251                         (G_LOG_LEVEL_MASK|G_LOG_FLAG_FATAL)&~(0
252                                         |G_LOG_LEVEL_MESSAGE
253                                         |G_LOG_LEVEL_INFO
254                                         |G_LOG_LEVEL_DEBUG),    /* log_levels */
255                         ui_gnome_g_log_handler, /* log_func */
256                         NULL);  /* user_data */
257
258         ui_gnome_network_notify_hostip(0);
259         gtk_entry_set_text(PortEntry,udpgate_printf_alloca("%d",(int)optarg_port));
260         if (!startup_init()) {
261                 gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(AutostartCheckButton),TRUE);
262                 gtk_widget_set_sensitive(GTK_WIDGET(AutostartLabel),FALSE);
263                 gtk_widget_set_sensitive(GTK_WIDGET(AutostartCheckButton),FALSE);
264                 }
265         daemon_check_timeout_func(NULL);        /* data; unused */
266         external_startup_check_timeout_func(NULL);      /* data; unused */
267
268         gtk_widget_show_all(GTK_WIDGET(App));
269         g_timeout_add(
270                         DAEMON_CHECK_INTERVAL_MS,       /* interval */
271                         daemon_check_timeout_func,      /* function */
272                         NULL);  /* data; unused */
273         g_timeout_add(
274                         EXTERNAL_STARTUP_CHECK_INTERVAL_MS,     /* interval */
275                         external_startup_check_timeout_func,    /* function */
276                         NULL);  /* data; unused */
277
278         network_notify_hostip=ui_gnome_network_notify_hostip;
279
280         ui_interactive=ui_gnome_interactive;
281
282         return TRUE;
283 }