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
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;
65 static void state_start_stop(void)
68 gboolean daemon_running;
69 static gboolean last_daemon_running,last_daemon_running_set=FALSE;
71 daemon_pid=is_daemon_running();
72 daemon_running=((pid_t)-1!=daemon_pid);
74 /* Cache the result; maybe not needed. */
75 if (last_daemon_running_set && last_daemon_running==daemon_running)
77 last_daemon_running=daemon_running;
78 last_daemon_running_set=TRUE;
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);
84 gnome_appbar_set_status(AppBar,
85 udpgate_printf_alloca(_("udpgate daemon running as PID %d."),(int)daemon_pid));
87 gnome_appbar_set_status(AppBar,_("No udpgate daemon currently running."));
90 static gboolean daemon_check_timeout_func(gpointer data /* unused */)
92 if (!App) /* Quitting? */
93 return FALSE; /* stop running */
96 return TRUE; /* continue running */
99 static gboolean external_startup_check_timeout_func(gpointer data /* unused */)
101 gboolean state_startup_is_on;
103 if (!App) /* Quitting? */
104 return FALSE; /* stop running */
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 */
111 void on_PortButtonRandom_clicked(GtkButton *button,gpointer user_data)
113 g_return_if_fail(GTK_IS_BUTTON(button));
116 if ((pid_t)-1!=is_daemon_running())
118 gtk_entry_set_text(PortEntry,
119 udpgate_printf_alloca("%d",(int)g_random_int_range(PORT_RANGE_BEGIN,PORT_RANGE_END)));
122 void on_AutostartCheckButton_toggled(GtkToggleButton *togglebutton,gpointer user_data)
124 static gint inside=0;
126 g_return_if_fail(GTK_IS_TOGGLE_BUTTON(togglebutton));
128 /* Avoid reentrancy to prevent segfault during failed registration.
129 * FIXME: Who knows why? Some forbidden GTK recursion occurs.
136 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(AutostartCheckButton)))
140 external_startup_check_timeout_func(NULL); /* data; unused */
146 void on_ButtonStart_clicked(GtkButton *button,gpointer user_data)
148 g_return_if_fail(GTK_IS_BUTTON(button));
150 if (!optarg_port_set_string(gtk_entry_get_text(PortEntry)))
152 network_start(optarg_port);
155 void on_ButtonStop_clicked(GtkButton *button,gpointer user_data)
157 g_return_if_fail(GTK_IS_BUTTON(button));
162 void on_ButtonHide_clicked(GtkButton *button,gpointer user_data)
164 g_return_if_fail(GTK_IS_BUTTON(button));
166 /* update config file */
167 optarg_port_set_string(gtk_entry_get_text(PortEntry));
169 /* Do not: gtk_main_quit();
170 * as 'App' widget will quit our gtk_main() automatically.
172 gtk_widget_destroy(GTK_WIDGET(App));
176 static void ui_gnome_network_notify_hostip(guint32 hostip_guint32)
178 if (!App) /* Quitting? */
181 if (!hostip_guint32) {
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...)"));
190 gtk_entry_set_text(HostIPEntry,_("(unknown; Kill the daemon and start your own)"));
193 gtk_entry_set_text(HostIPEntry,HOSTIP_GUINT32_TO_STRING(hostip_guint32));
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)
202 if (!App) /* Quitting? */
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);
210 dialog=gnome_app_message(App,message);
212 gtk_window_set_modal(GTK_WINDOW(dialog),TRUE);
213 g_signal_connect((gpointer)dialog,"close",G_CALLBACK(gtk_main_quit),NULL);
215 /* 'dialog' gets destroyed automatically */
218 static void ui_gnome_interactive(void)
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 */
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);
233 gboolean ui_gnome_init(void)
235 App=GNOME_APP(create_App());
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"));
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
252 |G_LOG_LEVEL_DEBUG), /* log_levels */
253 ui_gnome_g_log_handler, /* log_func */
254 NULL); /* user_data */
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);
263 daemon_check_timeout_func(NULL); /* data; unused */
264 external_startup_check_timeout_func(NULL); /* data; unused */
266 gtk_widget_show_all(GTK_WIDGET(App));
268 DAEMON_CHECK_INTERVAL_MS, /* interval */
269 daemon_check_timeout_func, /* function */
270 NULL); /* data; unused */
272 EXTERNAL_STARTUP_CHECK_INTERVAL_MS, /* interval */
273 external_startup_check_timeout_func, /* function */
274 NULL); /* data; unused */
276 network_notify_hostip=ui_gnome_network_notify_hostip;
278 ui_interactive=ui_gnome_interactive;