New 'HostIP' message: "(unknown; Kill the daemon and start your own)"
[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         state_start_stop();
93         return TRUE;    /* continue running */
94 }
95
96 static gboolean external_startup_check_timeout_func(gpointer data /* unused */)
97 {
98 gboolean state_startup_is_on;
99
100         if (startup_query(&state_startup_is_on))
101                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(AutostartCheckButton),state_startup_is_on);
102         return TRUE;    /* continue running */
103 }
104
105 void on_PortButtonRandom_clicked(GtkButton *button,gpointer user_data)
106 {
107         g_return_if_fail(GTK_IS_BUTTON(button));
108
109         state_start_stop();
110         if ((pid_t)-1!=is_daemon_running())
111                 return;
112         gtk_entry_set_text(PortEntry,
113                         udpgate_printf_alloca("%d",(int)g_random_int_range(PORT_RANGE_BEGIN,PORT_RANGE_END)));
114 }
115
116 void on_AutostartCheckButton_toggled(GtkToggleButton *togglebutton,gpointer user_data)
117 {
118         g_return_if_fail(GTK_IS_TOGGLE_BUTTON(togglebutton));
119
120         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(AutostartCheckButton)))
121                 startup_on();
122         else
123                 startup_off();
124         external_startup_check_timeout_func(NULL);      /* data; unused */
125 }
126
127 void on_ButtonStart_clicked(GtkButton *button,gpointer user_data)
128 {
129         g_return_if_fail(GTK_IS_BUTTON(button));
130
131         if (!optarg_port_set_string(gtk_entry_get_text(PortEntry)))
132                 return;
133         network_start(optarg_port);
134 }
135
136 void on_ButtonStop_clicked(GtkButton *button,gpointer user_data)
137 {
138         g_return_if_fail(GTK_IS_BUTTON(button));
139
140         network_stop();
141 }
142
143 void on_ButtonHide_clicked(GtkButton *button,gpointer user_data)
144 {
145         g_return_if_fail(GTK_IS_BUTTON(button));
146
147         /* update config file */
148         optarg_port_set_string(gtk_entry_get_text(PortEntry));
149
150         /* Do not: gtk_main_quit();
151          * as 'App' widget will quit our gtk_main() automatically.
152          */
153         gtk_widget_destroy(GTK_WIDGET(App));
154 }
155
156 static void ui_gnome_network_notify_hostip(guint32 hostip_guint32)
157 {
158         if (!hostip_guint32) {
159 pid_t daemon_pid;
160
161                 daemon_pid=is_daemon_running();
162                 if ((pid_t)-1==daemon_pid)
163                         gtk_entry_set_text(HostIPEntry,_("(unknown; Start the daemon)"));
164                 else if (getpid()==daemon_pid)
165                         gtk_entry_set_text(HostIPEntry,_("(unknown; detecting...)"));
166                 else
167                         gtk_entry_set_text(HostIPEntry,_("(unknown; Kill the daemon and start your own)"));
168                 }
169         else {
170                 gtk_entry_set_text(HostIPEntry,HOSTIP_GUINT32_TO_STRING(hostip_guint32));
171                 }
172 }
173
174 static guint ui_gnome_g_log_handler_handler_id;
175 static void ui_gnome_g_log_handler(const gchar *log_domain,GLogLevelFlags log_level,const gchar *message,gpointer user_data)
176 {
177 GtkWidget *dialog;
178
179         /**/ if (log_level & G_LOG_LEVEL_ERROR)
180                 dialog=gnome_app_error(App,message);
181         else if (log_level & (G_LOG_LEVEL_CRITICAL|G_LOG_LEVEL_WARNING))
182                 dialog=gnome_app_warning(App,message);
183         else
184                 dialog=gnome_app_message(App,message);
185
186         gtk_window_set_modal(GTK_WINDOW(dialog),TRUE);
187         g_signal_connect((gpointer)dialog,"close",G_CALLBACK(gtk_main_quit),NULL);
188         gtk_main();
189         /* 'dialog' gets destroyed automatically */
190 }
191
192 static void ui_gnome_interactive(void)
193 {
194         gtk_main();
195         network_notify_hostip=NULL;
196         g_log_remove_handler(
197                         G_LOG_DOMAIN,   /* log_domain; "Captive" */
198                         ui_gnome_g_log_handler_handler_id);     /* handler_id */
199         network_detach();
200 }
201
202 /* of "ui-gnome-interface.h": */
203 GtkWidget *create_App(void);
204 /* of "ui-gnome-support.h": */
205 GtkWidget *lookup_widget(GtkWidget *widget,const gchar *widget_name);
206
207 gboolean ui_gnome_init(void)
208 {
209         App=GNOME_APP(create_App());
210
211         ButtonStart=GTK_BUTTON(lookup_widget(GTK_WIDGET(App),"ButtonStart"));
212         ButtonStop=GTK_BUTTON(lookup_widget(GTK_WIDGET(App),"ButtonStop"));
213         PortHBox=GTK_HBOX(lookup_widget(GTK_WIDGET(App),"PortHBox"));
214         AppBar=GNOME_APPBAR(lookup_widget(GTK_WIDGET(App),"AppBar"));
215         PortEntry=GTK_ENTRY(lookup_widget(GTK_WIDGET(App),"PortEntry"));
216         HostIPEntry=GTK_ENTRY(lookup_widget(GTK_WIDGET(App),"HostIPEntry"));
217         AutostartLabel=GTK_LABEL(lookup_widget(GTK_WIDGET(App),"AutostartLabel"));
218         AutostartCheckButton=GTK_CHECK_BUTTON(lookup_widget(GTK_WIDGET(App),"AutostartCheckButton"));
219
220         /* ui_gnome_g_log_handler() needs 'App'. */
221         ui_gnome_g_log_handler_handler_id=g_log_set_handler(
222                         G_LOG_DOMAIN,   /* log_domain; "Captive" */
223                         (G_LOG_LEVEL_MASK|G_LOG_FLAG_FATAL)&~(0
224                                         |G_LOG_LEVEL_MESSAGE
225                                         |G_LOG_LEVEL_INFO
226                                         |G_LOG_LEVEL_DEBUG),    /* log_levels */
227                         ui_gnome_g_log_handler, /* log_func */
228                         NULL);  /* user_data */
229
230         ui_gnome_network_notify_hostip(0);
231         gtk_entry_set_text(PortEntry,udpgate_printf_alloca("%d",(int)optarg_port));
232         if (!startup_init()) {
233                 gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(AutostartCheckButton),TRUE);
234                 gtk_widget_set_sensitive(GTK_WIDGET(AutostartLabel),FALSE);
235                 gtk_widget_set_sensitive(GTK_WIDGET(AutostartCheckButton),FALSE);
236                 }
237         daemon_check_timeout_func(NULL);        /* data; unused */
238         external_startup_check_timeout_func(NULL);      /* data; unused */
239
240         gtk_widget_show_all(GTK_WIDGET(App));
241         g_timeout_add(
242                         DAEMON_CHECK_INTERVAL_MS,       /* interval */
243                         daemon_check_timeout_func,      /* function */
244                         NULL);  /* data; unused */
245         g_timeout_add(
246                         EXTERNAL_STARTUP_CHECK_INTERVAL_MS,     /* interval */
247                         external_startup_check_timeout_func,    /* function */
248                         NULL);  /* data; unused */
249
250         network_notify_hostip=ui_gnome_network_notify_hostip;
251
252         ui_interactive=ui_gnome_interactive;
253
254         return TRUE;
255 }