/* $Id$ * Gnome user interface * Copyright (C) 2004 Jan Kratochvil * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; exactly version 2 of June 1991 is required * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "config.h" #include "ui-gnome.h" /* self */ #include #include "main.h" #include #include #include #include #include #include #include #include #include #include #include #include #include /* Config: */ #define PATHNAME_PID "/var/run/udpforward.pid" #define DAEMON_CHECK_INTERVAL_MS 500 #define PORT_RANGE_BEGIN 2048 #define PORT_RANGE_END 10240 static GnomeApp *App; static GtkButton *ButtonStart; static GtkButton *ButtonStop; static GtkHBox *PortHBox; static GnomeAppBar *AppBar; static GtkEntry *PortEntry; static pid_t daemon_pid; static gboolean is_daemon_running(void) { FILE *f; char buf[LINE_MAX],*got; int pid_int; daemon_pid=(pid_t)-1; if (!(f=fopen(PATHNAME_PID,"r"))) goto err; got=fgets(buf,sizeof(buf),f); fclose(f); /* FIXME: error ignored */ if (got!=buf) { err_unlink: unlink(PATHNAME_PID); err: return FALSE; } pid_int=atoi(buf); if (pid_int<=1) goto err_unlink; if (kill((pid_t)pid_int,0)) { if (errno==ESRCH) goto err_unlink; goto err; } daemon_pid=(pid_t)pid_int; return TRUE; } static void state_start_stop(void) { gboolean daemon_running; daemon_running=is_daemon_running(); gtk_widget_set_sensitive(GTK_WIDGET(ButtonStart),!daemon_running); gtk_widget_set_sensitive(GTK_WIDGET(ButtonStop) , daemon_running); gtk_widget_set_sensitive(GTK_WIDGET(PortHBox) ,!daemon_running); if (daemon_running) gnome_appbar_set_status(AppBar, udpforward_printf_alloca(_("udpforward daemon running as PID %d."),(int)daemon_pid)); else gnome_appbar_set_status(AppBar,_("No udpforward daemon currently running.")); } static gboolean daemon_check_timeout_func(gpointer data /* unused */) { state_start_stop(); return TRUE; /* continue running */ } void on_PortButtonRandom_clicked(GtkButton *button,gpointer user_data) { g_return_if_fail(GTK_IS_BUTTON(button)); state_start_stop(); if ((pid_t)-1!=daemon_pid) return; gtk_entry_set_text(PortEntry, udpforward_printf_alloca("%d",(int)g_random_int_range(PORT_RANGE_BEGIN,PORT_RANGE_END))); } void on_AutostartCheckButton_toggled(GtkToggleButton *togglebutton,gpointer user_data) { g_return_if_fail(GTK_IS_TOGGLE_BUTTON(togglebutton)); } void on_ButtonStart_clicked(GtkButton *button,gpointer user_data) { g_return_if_fail(GTK_IS_BUTTON(button)); state_start_stop(); if ((pid_t)-1!=daemon_pid) return; } void on_ButtonStop_clicked(GtkButton *button,gpointer user_data) { int errno_save; g_return_if_fail(GTK_IS_BUTTON(button)); state_start_stop(); if ((pid_t)-1==daemon_pid) return; errno=0; kill(daemon_pid,SIGKILL); errno_save=errno; if (errno_save) g_warning(udpforward_printf_alloca(_("Unable to stop the daemon at PID %d: %s"), (int)daemon_pid,strerror(errno_save))); } void on_ButtonHide_clicked(GtkButton *button,gpointer user_data) { g_return_if_fail(GTK_IS_BUTTON(button)); gtk_main_quit(); } /* of "ui-gnome-interface.h": */ GtkWidget *create_App(void); /* of "ui-gnome-support.h": */ GtkWidget *lookup_widget(GtkWidget *widget,const gchar *widget_name); gboolean ui_gnome_init(void) { App=GNOME_APP(create_App()); ButtonStart=GTK_BUTTON(lookup_widget(GTK_WIDGET(App),"ButtonStart")); ButtonStop=GTK_BUTTON(lookup_widget(GTK_WIDGET(App),"ButtonStop")); PortHBox=GTK_HBOX(lookup_widget(GTK_WIDGET(App),"PortHBox")); AppBar=GNOME_APPBAR(lookup_widget(GTK_WIDGET(App),"AppBar")); PortEntry=GTK_ENTRY(lookup_widget(GTK_WIDGET(App),"PortEntry")); gtk_widget_show_all(GTK_WIDGET(App)); g_timeout_add( DAEMON_CHECK_INTERVAL_MS, /* interval */ daemon_check_timeout_func, /* function */ NULL); /* data; unused */ return TRUE; } void ui_gnome_interactive(void) { gtk_main(); }