3aa468103c36b9751324e86b621ab5db088482d1
[udpgate.git] / src / ui-gnome.c
1 /* $Id$
2  * Gnome user interface
3  * Copyright (C) 2004 Jan Kratochvil <project-udpforward@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
40 #include "network.h"
41
42
43 /* Config: */
44 #define DAEMON_CHECK_INTERVAL_MS 500
45 #define PORT_RANGE_BEGIN 2048
46 #define PORT_RANGE_END 10240
47
48
49 static GnomeApp *App;
50 static GtkButton *ButtonStart;
51 static GtkButton *ButtonStop;
52 static GtkHBox *PortHBox;
53 static GnomeAppBar *AppBar;
54 static GtkEntry *PortEntry;
55
56
57 static void state_start_stop(void)
58 {
59 pid_t daemon_pid;
60
61         daemon_pid=is_daemon_running();
62         gboolean daemon_running=((pid_t)-1!=daemon_pid);
63         gtk_widget_set_sensitive(GTK_WIDGET(ButtonStart),!daemon_running);
64         gtk_widget_set_sensitive(GTK_WIDGET(ButtonStop) , daemon_running);
65         gtk_widget_set_sensitive(GTK_WIDGET(PortHBox)   ,!daemon_running);
66         if (daemon_running)
67                 gnome_appbar_set_status(AppBar,
68                                 udpforward_printf_alloca(_("udpforward daemon running as PID %d."),(int)daemon_pid));
69         else
70                 gnome_appbar_set_status(AppBar,_("No udpforward daemon currently running."));
71 }
72
73 static gboolean daemon_check_timeout_func(gpointer data /* unused */)
74 {
75         state_start_stop();
76         return TRUE;    /* continue running */
77 }
78
79 void on_PortButtonRandom_clicked(GtkButton *button,gpointer user_data)
80 {
81         g_return_if_fail(GTK_IS_BUTTON(button));
82
83         state_start_stop();
84         if ((pid_t)-1!=is_daemon_running())
85                 return;
86         gtk_entry_set_text(PortEntry,
87                         udpforward_printf_alloca("%d",(int)g_random_int_range(PORT_RANGE_BEGIN,PORT_RANGE_END)));
88 }
89
90 void on_AutostartCheckButton_toggled(GtkToggleButton *togglebutton,gpointer user_data)
91 {
92         g_return_if_fail(GTK_IS_TOGGLE_BUTTON(togglebutton));
93 }
94
95 void on_ButtonStart_clicked(GtkButton *button,gpointer user_data)
96 {
97 const gchar *port_string;
98 char *endp;
99 long port_long;
100
101         g_return_if_fail(GTK_IS_BUTTON(button));
102
103         port_string=gtk_entry_get_text(PortEntry);
104         port_long=strtol(port_string,&endp,0);
105         if (endp && *endp) {
106                 g_warning(_("Invalid port specification, offending string: %s"),endp);
107                 return;
108                 }
109         if (port_long<1 || port_long>=G_MAXINT || (endp && *endp)) {
110                 g_warning(_("Invalid port integer number specification (%ld)"),port_long);
111                 return;
112                 }
113         network_start(port_long);
114 }
115
116 void on_ButtonStop_clicked(GtkButton *button,gpointer user_data)
117 {
118         g_return_if_fail(GTK_IS_BUTTON(button));
119
120         network_stop();
121 }
122
123 void on_ButtonHide_clicked(GtkButton *button,gpointer user_data)
124 {
125         g_return_if_fail(GTK_IS_BUTTON(button));
126
127         /* Do not: gtk_main_quit();
128          * as 'App' widget will quit our gtk_main() automatically.
129          */
130         gtk_widget_destroy(GTK_WIDGET(App));
131 }
132
133 static void ui_gnome_g_log_handler(const gchar *log_domain,GLogLevelFlags log_level,const gchar *message,gpointer user_data)
134 {
135 GtkWidget *dialog;
136
137         /**/ if (log_level & G_LOG_LEVEL_ERROR)
138                 dialog=gnome_app_error(App,message);
139         else if (log_level & (G_LOG_LEVEL_CRITICAL|G_LOG_LEVEL_WARNING))
140                 dialog=gnome_app_warning(App,message);
141         else
142                 dialog=gnome_app_message(App,message);
143
144         gtk_window_set_modal(GTK_WINDOW(dialog),TRUE);
145         g_signal_connect((gpointer)dialog,"close",G_CALLBACK(gtk_main_quit),NULL);
146         gtk_main();
147         /* 'dialog' gets destroyed automatically */
148 }
149
150 /* of "ui-gnome-interface.h": */
151 GtkWidget *create_App(void);
152 /* of "ui-gnome-support.h": */
153 GtkWidget *lookup_widget(GtkWidget *widget,const gchar *widget_name);
154
155 gboolean ui_gnome_init(void)
156 {
157         App=GNOME_APP(create_App());
158
159         ButtonStart=GTK_BUTTON(lookup_widget(GTK_WIDGET(App),"ButtonStart"));
160         ButtonStop=GTK_BUTTON(lookup_widget(GTK_WIDGET(App),"ButtonStop"));
161         PortHBox=GTK_HBOX(lookup_widget(GTK_WIDGET(App),"PortHBox"));
162         AppBar=GNOME_APPBAR(lookup_widget(GTK_WIDGET(App),"AppBar"));
163         PortEntry=GTK_ENTRY(lookup_widget(GTK_WIDGET(App),"PortEntry"));
164
165         /* ui_gnome_g_log_handler() needs 'App'. */
166         g_log_set_handler(
167                         G_LOG_DOMAIN,   /* log_domain; "Captive" */
168                         G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL,    /* log_levels */
169                         ui_gnome_g_log_handler, /* log_func */
170                         NULL);  /* user_data */
171
172         gtk_widget_show_all(GTK_WIDGET(App));
173         g_timeout_add(
174                         DAEMON_CHECK_INTERVAL_MS,       /* interval */
175                         daemon_check_timeout_func,      /* function */
176                         NULL);  /* data; unused */
177         return TRUE;
178 }
179
180 void ui_gnome_interactive(void)
181 {
182         gtk_main();
183 }