#!/usr/bin/perl
# pptp_traffic_applet
# Copyright (C) 2002 Mihail Vasiliev
#
#  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; either version 2 of the License, or
#  (at your option) any later version.
# 
#  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., 675 Mass Ave, Cambridge, MA 02139, USA.



#TITLE: Gnome applet, measuring traffic over PPP/PPTP connection
#REQUIRES: Gtk Gnome


use Gnome;
use Gtk;

init Gnome::Panel::AppletWidget 'pptp_traffic_applet';

my $UP_SCRIPT="sudo /etc/rc.d/init.d/pptp start";
my $DOWN_SCRIPT="sudo /etc/rc.d/init.d/pptp stop";
my $PPPSTATS_SCRIPT="/usr/sbin/pppstats";
my $DIGITS_AFTER_DOT="2";
my $MAX_TRAFFIC=0; # in bytes
my $REDRAW_PERIOD=3000; # in milliseconds

my $messageifdown="Interface\nis down";
my $message=$messageifdown;

if ( -f $ENV{HOME}."/.pptp_traffic_appletrc" )

{
	open(FILE,"<".$ENV{HOME}."/.pptp_traffic_appletrc");
	while(<FILE>)
		{
		chomp;
		if($_=~/\s*UP_SCRIPT\s*=\s*"(.*)".*/) {$UP_SCRIPT=$1;}
		if(/\s*DOWN_SCRIPT\s*=\s*"(.*)".*/) {$DOWN_SCRIPT=$1;}
		if(/\s*PPPSTATS_SCRIPT\s*=\s*"(.*)".*/) {$PPPSTATS_SCRIPT=$1;}
		if(/\s*DIGITS_AFTER_DOT\s*=\s*"(.*)".*/) {$DIGITS_AFTER_DOT=$1;}
		if(/\s*MAX_TRAFFIC\s*=\s*"(.*)".*/) {$MAX_TRAFFIC=$1;}
		if(/\s*REDRAW_PERIOD\s*=\s*"(.*)".*/) {$REDRAW_PERIOD=$1;}
		};
	
	close FILE;
};

my $i=0;

$applet = new Gnome::Panel::AppletWidget 'pptp_traffic_applet';
realize $applet;

$vbox = new Gtk::VBox->new(0,1);

$label = new Gtk::Label->new("");
$label->set_usize(70,30);
$label->set_justify(left);
show $label;
$vbox->add($label);


$hbox = new Gtk::HBox->new(0,1);
show $hbox;

$up_btn = new Gtk::Button "UP";
$up_btn->set_usize(-1,15);
show $up_btn;
$hbox->add($up_btn);


$up_btn->signal_connect("clicked", sub {$message=$messageifdown;system $UP_SCRIPT;});

$down_btn = new Gtk::Button "DN";
$down_btn->set_usize(-1,15);
show $down_btn;
$down_btn->signal_connect("clicked", sub {$message=$messageifdown;system $DOWN_SCRIPT;});
$hbox->add($down_btn);


$cfg_btn = new Gtk::Button "CF";
$cfg_btn->set_usize(-1,15);
show $cfg_btn;
$hbox->add($cfg_btn);

$cfg_btn->signal_connect("clicked", sub 
	{
	$setup_win = new Gtk::Widget "GtkWindow",
			GtkWindow::type	=> 	   -toplevel,
			GtkWindow::title =>	   "Configure pptp_traffic_applet",
			GtkWindow::allow_grow =>   0,
			GtkWindow::allow_shrink => 0;
	$setup_vbox = new Gtk::VBox->new(0,1);
	
	$setup_hb1 = new Gtk::HBox->new(0,1);
	$setup_lb1 = new Gtk::Label->new("Script, which turns the interface up:");
	show $setup_lb1;
	$setup_hb1->add($setup_lb1);
	$setup_ed1 = new Gtk::Entry;
	$setup_ed1->set_text($UP_SCRIPT);
	show $setup_ed1;
	$setup_hb1->add($setup_ed1);
	show $setup_hb1;
	$setup_vbox->add($setup_hb1);
		
	$setup_hb2 = new Gtk::HBox->new(0,1);
	$setup_lb2 = new Gtk::Label->new("Script, which turns the interface down:");
	show $setup_lb2;
	$setup_hb2->add($setup_lb2);
	$setup_ed2 = new Gtk::Entry;
 	$setup_ed2->set_text($DOWN_SCRIPT);
	show $setup_ed2;
	$setup_hb2->add($setup_ed2);
	show $setup_hb2;
	$setup_vbox->add($setup_hb2);

	$setup_hb3 = new Gtk::HBox->new(0,1);
	$setup_lb3 = new Gtk::Label->new("Script, which reads statistics:");
	show $setup_lb3;
	$setup_hb3->add($setup_lb3);
	$setup_ed3 = new Gtk::Entry;
	$setup_ed3->set_text( $PPPSTATS_SCRIPT);
	show $setup_ed3;
	$setup_hb3->add($setup_ed3);
	show $setup_hb3;
	$setup_vbox->add($setup_hb3);
	
	$setup_hb4 = new Gtk::HBox->new(0,1);
	$setup_lb4 = new Gtk::Label->new("Number of digits after decimal dot:");
	show $setup_lb4;
	$setup_hb4->add($setup_lb4);
	$setup_ed4 = new Gtk::Entry;
	$setup_ed4->set_text($DIGITS_AFTER_DOT);
	show $setup_ed4;
	$setup_hb4->add($setup_ed4);
	show $setup_hb4;
	$setup_vbox->add($setup_hb4);
	
	$setup_hb5 = new Gtk::HBox->new(0,1);
	$setup_lb5 = new Gtk::Label->new("Maximum allowed traffic in bytes, 0 seems infinty:");
	show $setup_lb5;
	$setup_hb5->add($setup_lb5);
	$setup_ed5 = new Gtk::Entry;
 	$setup_ed5->set_text($MAX_TRAFFIC);
	show $setup_ed5;
	$setup_hb5->add($setup_ed5);
	show $setup_hb5;
	$setup_vbox->add($setup_hb5);
	
	$setup_hb6 = new Gtk::HBox->new(0,1);
	$setup_lb6 = new Gtk::Label->new("Redraw period in miliseconds:");
	show $setup_lb6;
	$setup_hb6->add($setup_lb6);
	$setup_ed6 = new Gtk::Entry; 
	$setup_ed6->set_text($REDRAW_PERIOD);
	show $setup_ed6;
	$setup_hb6->add($setup_ed6);
	show $setup_hb6;
	$setup_vbox->add($setup_hb6);

	
	$setup_btnhbox = new Gtk::HBox->new(0,1),
			GtkContainer::border_width	=>	10;
	$setup_okbtn = new Gtk::Button "OK";
	$setup_okbtn->signal_connect("clicked",sub { 
		$UP_SCRIPT=		$setup_ed1->get_text;
		$DOWN_SCRIPT=		$setup_ed2->get_text;
		$PPPSTATS_SCRIPT=	$setup_ed3->get_text;
		$DIGITS_AFTER_DOT=	$setup_ed4->get_text;
		$MAX_TRAFFIC=		$setup_ed5->get_text;
		$REDRAW_PERIOD=		$setup_ed6->get_text;

		unless(open(FILE,">".$ENV{HOME}."/.pptp_traffic_appletrc")){
			$message = "Can't save settings!";
			return ;
		};
		print FILE 'UP_SCRIPT="'.$UP_SCRIPT.'"'."\n";
		print FILE 'DOWN_SCRIPT="'.$DOWN_SCRIPT.'"'."\n";
		print FILE 'PPPSTATS_SCRIPT="'.$PPPSTATS_SCRIPT.'"'."\n";
		print FILE 'DIGITS_AFTER_DOT="'.$DIGITS_AFTER_DOT.'"'."\n";
		print FILE 'MAX_TRAFFIC="'.$MAX_TRAFFIC.'"'."\n";
		print FILE 'REDRAW_PERIOD="'.$REDRAW_PERIOD.'"'."\n";
		close(FILE);
		
		destroy $setup_lb1;destroy $setup_ed1;destroy $setup_hb1;
		destroy $setup_lb2;destroy $setup_ed2;destroy $setup_hb2;
		destroy $setup_lb3;destroy $setup_ed3;destroy $setup_hb3;
		destroy $setup_lb4;destroy $setup_ed4;destroy $setup_hb4;
		destroy $setup_lb5;destroy $setup_ed5;destroy $setup_hb5;
		destroy $setup_okbtn;destroy $setup_cancelbtn;destroy $setup_btnhbox;
		destroy $setup_vbox;destroy $setup_win;
		});
	show $setup_okbtn;
	$setup_btnhbox->add($setup_okbtn);
	
	$setup_cancelbtn = new Gtk::Button "Cancel";
	$setup_cancelbtn->signal_connect("clicked",sub {
		destroy $setup_lb1;destroy $setup_ed1;destroy $setup_hb1;
		destroy $setup_lb2;destroy $setup_ed2;destroy $setup_hb2;
		destroy $setup_lb3;destroy $setup_ed3;destroy $setup_hb3;
		destroy $setup_lb4;destroy $setup_ed4;destroy $setup_hb4;
		destroy $setup_lb5;destroy $setup_ed5;destroy $setup_hb5;
		destroy $setup_okbtn;destroy $setup_cancelbtn;destroy $setup_btnhbox;
		destroy $setup_vbox;destroy $setup_win;
		});
	show $setup_cancelbtn;
	$setup_btnhbox->add($setup_cancelbtn);
	show $setup_btnhbox;

	$setup_vbox->add($setup_btnhbox);
	show $setup_vbox;	
	$setup_win->add($setup_vbox);
	show $setup_win;
	});


$vbox->add($hbox);
$vbox->show;
$applet->add($vbox);
show $applet;

sub textalize {
$s=$_[0];
if ($s/1000000000>1) {$s=sprintf("%.".$DIGITS_AFTER_DOT."fG",$s/1000000000);return $s;};
if ($s/1000000>1) {$s=sprintf("%.".$DIGITS_AFTER_DOT."fM",$s/1000000);return $s;};
if ($s/1000>1) {$s=sprintf("%.".$DIGITS_AFTER_DOT."fk",$s/1000);return $s;};
return $s;
};

sub release_event {

    open(PIPE,"$PPPSTATS_SCRIPT|") || {} ;
    $_=<PIPE>;
	if ($_ eq "") {
		$label->set($message);
		return 1;
	};
    $_=<PIPE>;
    $_=~s/^\s+//;
    @list=split(/\s+/,$_);
    
    if ($list[0]/1000>1) {}
    $i="IN:".textalize($list[0])."\n"."OUT:".textalize($list[6]);

    if(($MAX_TRAFFIC != 0)&&($list[0]>$MAX_TRAFFIC)) {
		system $DOWN_SCRIPT;
		$message="Traffic\n limited";
	};

    $label->set($i);
   
    return 1;
};


Gtk->timeout_add($REDRAW_PERIOD, \&release_event);

gtk_main Gnome::Panel::AppletWidget;
