3 * Copyright (C) 2004 Jan Kratochvil <project-udpgate@jankratochvil.net>
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
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.
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
22 #include "ui-gnome.h" /* self */
23 #include <glib/gmessages.h>
25 #include <libgnomeui/gnome-app.h>
26 #include <sys/types.h>
30 #include <libgnomeui/gnome-appbar.h>
31 #include <gtk/gtkbutton.h>
34 #include <gtk/gtkentry.h>
35 #include <gtk/gtktogglebutton.h>
37 #include <gtk/gtkmain.h>
38 #include <libgnomeui/gnome-app-util.h>
39 #include <gtk/gtklabel.h>
40 #include <gtk/gtkcheckbutton.h>
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
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;
66 static void state_start_stop(void)
69 gboolean daemon_running;
70 static gboolean last_daemon_running,last_daemon_running_set=FALSE;
72 daemon_pid=is_daemon_running();
73 daemon_running=((pid_t)-1!=daemon_pid);
75 /* Cache the result; maybe not needed. */
76 if (last_daemon_running_set && last_daemon_running==daemon_running)
78 last_daemon_running=daemon_running;
79 last_daemon_running_set=TRUE;
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);
85 gnome_appbar_set_status(AppBar,
86 udpgate_printf_alloca(_("udpgate daemon running as PID %d."),(int)daemon_pid));
88 gnome_appbar_set_status(AppBar,_("No udpgate daemon currently running."));
91 static gboolean daemon_check_timeout_func(gpointer data /* unused */)
93 if (!App) /* Quitting? */
94 return FALSE; /* stop running */
97 return TRUE; /* continue running */
100 static gboolean external_startup_check_timeout_func(gpointer data /* unused */)
102 gboolean state_startup_is_on;
104 if (!App) /* Quitting? */
105 return FALSE; /* stop running */
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 */
112 void on_PortButtonRandom_clicked(GtkButton *button,gpointer user_data)
114 g_return_if_fail(GTK_IS_BUTTON(button));
117 if ((pid_t)-1!=is_daemon_running())
119 gtk_entry_set_text(PortEntry,
120 udpgate_printf_alloca("%d",(int)g_random_int_range(PORT_RANGE_BEGIN,PORT_RANGE_END)));
123 void on_AutostartCheckButton_toggled(GtkToggleButton *togglebutton,gpointer user_data)
125 static gint inside=0;
127 g_return_if_fail(GTK_IS_TOGGLE_BUTTON(togglebutton));
129 /* Avoid reentrancy to prevent segfault during failed registration.
130 * FIXME: Who knows why? Some forbidden GTK recursion occurs.
137 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(AutostartCheckButton)))
141 external_startup_check_timeout_func(NULL); /* data; unused */
147 void on_ButtonStart_clicked(GtkButton *button,gpointer user_data)
149 g_return_if_fail(GTK_IS_BUTTON(button));
151 if (!optarg_port_set_string(gtk_entry_get_text(PortEntry)))
153 network_start(optarg_port);
156 void on_ButtonStop_clicked(GtkButton *button,gpointer user_data)
158 g_return_if_fail(GTK_IS_BUTTON(button));
163 void on_ButtonHide_clicked(GtkButton *button,gpointer user_data)
165 g_return_if_fail(GTK_IS_BUTTON(button));
167 /* update config file */
168 optarg_port_set_string(gtk_entry_get_text(PortEntry));
170 /* Do not: gtk_main_quit();
171 * as 'App' widget will quit our gtk_main() automatically.
173 gtk_widget_destroy(GTK_WIDGET(App));
177 static void ui_gnome_network_notify_hostip(guint32 hostip_guint32)
179 if (!App) /* Quitting? */
182 if (!hostip_guint32) {
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...)"));
191 gtk_entry_set_text(HostIPEntry,_("(unknown; Kill the daemon and start your own)"));
194 gtk_entry_set_text(HostIPEntry,HOSTIP_GUINT32_TO_STRING(hostip_guint32));
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)
203 if (!App) /* Quitting? */
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);
211 dialog=gnome_app_message(App,message);
213 gtk_window_set_modal(GTK_WINDOW(dialog),TRUE);
214 g_signal_connect((gpointer)dialog,"close",G_CALLBACK(gtk_main_quit),NULL);
216 /* 'dialog' gets destroyed automatically */
219 static void ui_gnome_interactive(void)
221 probe_timeout_sec_max=UI_GNOME_PROBE_TIMEOUT_SEC;
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 */
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);
235 gboolean ui_gnome_init(void)
237 App=GNOME_APP(create_App());
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"));
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
254 |G_LOG_LEVEL_DEBUG), /* log_levels */
255 ui_gnome_g_log_handler, /* log_func */
256 NULL); /* user_data */
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);
265 daemon_check_timeout_func(NULL); /* data; unused */
266 external_startup_check_timeout_func(NULL); /* data; unused */
268 gtk_widget_show_all(GTK_WIDGET(App));
270 DAEMON_CHECK_INTERVAL_MS, /* interval */
271 daemon_check_timeout_func, /* function */
272 NULL); /* data; unused */
274 EXTERNAL_STARTUP_CHECK_INTERVAL_MS, /* interval */
275 external_startup_check_timeout_func, /* function */
276 NULL); /* data; unused */
278 network_notify_hostip=ui_gnome_network_notify_hostip;
280 ui_interactive=ui_gnome_interactive;