#!/usr/bin/perl

# Monitor

# Copyright (C) 1999-2002 MandrakeSoft (damien@mandrakesoft.com)
#
# 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, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

use lib qw(/usr/lib/libDrakX);

use strict;
use standalone;     #- warning, standalone must be loaded very first, for 'explanations'

use interactive;
use ugtk2 qw(:helpers :wrappers :create);
use common;
use network::netconnect;
use network::tools;
use MDK::Common::Globals "network", qw($in $prefix $connect_file $disconnect_file $connect_prog);

if ("@ARGV" =~ /--status/) { print connected(); exit(0) }
my $force = "@ARGV" =~ /--force/;
my $quiet = "@ARGV" =~ /--quiet/;
my $connect = "@ARGV" =~ /--connect/;
my $disconnect = "@ARGV" =~ /--disconnect/;
my ($default_intf) = "@ARGV" =~ /--defaultintf (\w+)/;

if ($force) {
    $connect and system("/etc/sysconfig/network-scripts/net_cnx_up");
    $disconnect and system("/etc/sysconfig/network-scripts/net_cnx_down");
    $connect = $disconnect = 0;
}
$quiet and exit(0);
my $in = 'interactive'->vnew('su', 'default');


my $window1 = ugtk2->new(N("Network Monitoring"));
$window1->{rwindow}->signal_connect(delete_event => sub { ugtk2->exit(0) });

unless ($::isEmbedded) {
    $window1->{rwindow}->set_position('center');
    $window1->{rwindow}->set_title(N("Network Monitoring"));
    $window1->{rwindow}->set_border_width(5);
}
#$::isEmbedded or $window1->{rwindow}->set_size_request(580, 320);

my $colorr = gtkcolor(50400, 655, 20000);
my $colort = gtkcolor(55400, 55400, 655);
my $colora = gtkcolor(655, 50400, 655);
my $isconnected = -1;
my @interfaces;
my $monitor = {};
my $netcnx = {};
my $netc = {};
my $intf = {};
my $c_time = 0;
my $ct_tag;
my $style = Gtk2::Style->new;
#$style->font(Gtk2::Gdk::Font->fontset_load("-adobe-times-medium-r-normal-*-12-*-75-75-p-*-iso8859-*,*-r-*"));

my ($pixmap, $darea);
my ($width, $height) = (300, 150);

network::netconnect::load_conf($netcnx, $netc, $intf);
network::netconnect::read_net_conf('', $netcnx, $netc);
MDK::Common::Globals::init(
			   in => $in,
			   prefix => '',
			   connect_file => "/etc/sysconfig/network-scripts/net_cnx_up",
			   disconnect_file => "/etc/sysconfig/network-scripts/net_cnx_down",
			   connect_prog => "/etc/sysconfig/network-scripts/net_cnx_pg");

gtkadd($window1->{window},
       gtkpack_(Gtk2::VBox->new(0,5),
                1, gtkpack_(Gtk2::HBox->new(0,5),
                            1, my $notebook = Gtk2::Notebook->new,
                            0, gtkpack_(Gtk2::VBox->new(0,5),
                                        0, gtkadd(gtkset_shadow_type(Gtk2::Frame->new(N("Settings")), 'etched_out'),
                                                  gtkpack__(Gtk2::HBox->new(0,0),
                                                            N("Connection type: "), 
                                                            my $label_cnx_type = Gtk2::Label->new("")),
                                                  
                                                 ),
                                        1, gtkadd(gtkset_shadow_type(Gtk2::Frame->new(N("Global statistics")), 'etched_out'),
                                                  gtkpack__(Gtk2::VBox->new(0,0),
                                                            create_packtable({ col_spacings => 1, row_spacings => 1 },
                                                                             [ "", N("instantaneous") , N("average") ],
                                                                             [ Gtk2::WrappedLabel->new(N("Sending\nspeed:")),   my $label_st = Gtk2::Label->new(""), my $label_sta = Gtk2::Label->new("na") ],
                                                                             [ Gtk2::WrappedLabel->new(N("Receiving\nspeed:")), my $label_sr = Gtk2::Label->new(""), my $label_sra = Gtk2::Label->new("na") ],
                                                                            ),
                                                            gtkpack__(Gtk2::HBox->new(0,0),
                                                                      N("Connection\ntime: "), 
                                                                      my $label_ct = Gtk2::Label->new(""),
                                                                     ),
                                                           )
                                                 ),
                                        0, gtkpack(Gtk2::HBox->new(0,5),
                                                   gtksignal_connect(my $button_connect = gtkset_sensitive(Gtk2::Button->new(N("Wait please")), 0), clicked => \&connection),
                                                   gtksignal_connect(my $button_close = Gtk2::Button->new(N("Close")), clicked => sub { ugtk2->exit(0) }),
                                                   )
                                       )
                           ),
                0, my $statusbar = Gtk2::Statusbar->new
               ),
      );
$window1->{rwindow}->show_all;
$window1->{rwindow}->realize;
my $gct = Gtk2::Gdk::GC->new($window1->{rwindow}->window);
$gct->set_foreground($colort);
my $gcr = Gtk2::Gdk::GC->new($window1->{rwindow}->window);
$gcr->set_foreground($colorr);
my $gca = Gtk2::Gdk::GC->new($window1->{rwindow}->window);
$gca->set_foreground($colora);
$statusbar->push(1, N("Wait please, testing your connection..."));
$window1->{rwindow}->show_all;

my $time_tag = Glib::Timeout->add(1000, \&rescan);
my $time_tag2 = Glib::Timeout->add(1000, \&update);

update();
rescan();

gtkflush() while $isconnected == -2 || $isconnected == -1;

Glib::Source->remove($time_tag2);
$time_tag2 = Glib::Timeout->add(20000, \&update);

connection() if $connect && !$isconnected || $disconnect && $isconnected;
$window1->main;
ugtk2->exit(0);

my $during_connection;
my $first;

sub connection() {
    $during_connection = 1;
    my $wasconnected = $isconnected;

    $button_connect->set_sensitive(0);
    $button_close->set_sensitive(0);
    $statusbar->pop(1);
    $statusbar->push(1, $wasconnected ? N("Disconnecting from Internet ") : N("Connecting to Internet "));
    if ($wasconnected == 1) {
	$c_time = time();
	$ct_tag = Glib::Timeout->add(1000, sub {
				       my ($sec, $min, $hour) = gmtime(time() - $c_time);
				       my $e = sprintf("%02d:%02d:%02d", $hour, $min, $sec);
				       $label_ct->set_label($e); 1 })
    } else { Glib::Source->remove($ct_tag) }
    my $nb_point = 1;
    $first = 1;

    my $_tag = Glib::Timeout->add(1000, sub {
				   $statusbar->pop(1);
				   $statusbar->push(1, ($wasconnected  == 1 ? N("Disconnecting from Internet ") : N("Connecting to Internet "))
						    . join('', map { "." } (1..$nb_point)));
				   $nb_point++;
				   if ($nb_point  < 4) { return 1 }
				   my $ret = 1;

				   my $isconnect = test_connected(0);

				   if ($nb_point < 20) {
                                       if ($first == 1)  { # first time
				           if ($isconnect == -2) { # wait for last test to finish
    				               test_connected(2); # not yet terminated, try to cancel it
				               return 1;
					   }
				           test_connected(1);  # initiates new connection test
				           $first = 0;
				           return 1;
				       }
				       if ($isconnect == -2) { return 1 } # no result yet, wait.
				       if ($isconnect == $wasconnected) {
				           # we got a test result; but the connection state did not change; retry.
                                           test_connected(1);
				           return 1;
				       }
				   }
                                   # either we got a result, or we timed out.
				   if ($isconnect != -2 || $nb_point > 20) {
				            $isconnected = $isconnect;
					    $ret = 0;
					    $statusbar->pop(1);
					    $statusbar->push(1, $wasconnected ? ($isconnected ?
									  N("Disconnection from Internet failed.") :
									  N("Disconnection from Internet complete.")) :
						     ($isconnected ?
						       N("Connection complete.") :
						       N("Connection failed.\nVerify your configuration in the Mandrake Control Center."))
						    );
				            my $delay = 1000;
					    # keep the message displayed longer if there is a problem.
				    	    if ($isconnected == $wasconnected) { $delay = 5000 }
				            my $_tag3 = Glib::Timeout->add($delay, sub {

						  $button_connect->set_sensitive(1);
				    		  $button_close->set_sensitive(1);
				    		  undef $during_connection;
						  update();
						  return 0;
                            });
                            }
                       return $ret;
                   });

    gtkflush();

    if ($wasconnected == 1) {
        system("/etc/sysconfig/network-scripts/net_cnx_down &");
    }  else  {
        system("/etc/sysconfig/network-scripts/net_cnx_up &");
    }
}

sub rescan() {
    get_val();
    foreach (@interfaces) {
	my $intf = $_;
	my $recv = $monitor->{$intf}{val}[0];
	my $transmit = $monitor->{$intf}{val}[8];
	my $refr = $monitor->{$intf}{referencer};
	my $reft = $monitor->{$intf}{referencet};
	$monitor->{sr} += $recv - $refr;
	$monitor->{st} += $transmit - $reft;

	$monitor->{$intf}{recva} += $recv - $refr;
	$monitor->{$intf}{recvan}++;
	if ($monitor->{$intf}{recvan} > 9) {
	    push(@{$monitor->{$intf}{stack_ra}}, $monitor->{$intf}{recva}/10);
	    $monitor->{$intf}{recva} = $monitor->{$intf}{recvan} = 0;
	} else { push(@{$monitor->{$intf}{stack_ra}}, -1) }
	shift @{$monitor->{$intf}{stack_ra}} if @{$monitor->{$intf}{stack_ra}} > 250;

	push(@{$monitor->{$intf}{stack_r}}, $recv - $refr);
	shift @{$monitor->{$intf}{stack_r}} if @{$monitor->{$intf}{stack_r}} > 250;
	$monitor->{$intf}{labelr}->set_label(formatXiB($recv - $monitor->{$intf}{initialr}));
	$monitor->{$intf}{referencer} = $recv;

	$monitor->{$intf}{transmita} += $transmit - $reft;
	$monitor->{$intf}{transmitan}++;
	if ($monitor->{$intf}{transmitan} > 9) {
	    push(@{$monitor->{$intf}{stack_ta}}, $monitor->{$intf}{transmita}/10);
	    $monitor->{$intf}{transmita} = $monitor->{$intf}{transmitan} = 0;
	} else { push(@{$monitor->{$intf}{stack_ta}}, -1) }
	shift @{$monitor->{$intf}{stack_ta}} if @{$monitor->{$intf}{stack_ta}} > 250;

	push(@{$monitor->{$intf}{stack_t}}, $transmit - $reft);
	shift @{$monitor->{$intf}{stack_t}} if @{$monitor->{$intf}{stack_t}} > 250;
	$monitor->{$intf}{labelt}->set_label(formatXiB($transmit - $monitor->{$intf}{initialt}));
	$monitor->{$intf}{referencet} = $transmit;

	draw_monitor($monitor->{$intf}, $intf);
    }
    $label_sr->set_label(formatXiB($monitor->{sr}) . "/s");
    $label_st->set_label(formatXiB($monitor->{st}) . "/s");
    $monitor->{sra} += $monitor->{sr};
    $monitor->{sta} += $monitor->{st};
    $monitor->{nba}++;
    if ($monitor->{nba} > 9) {
	$label_sra->set_label(formatXiB($monitor->{sra}/10) . "/s");
	$label_sta->set_label(formatXiB($monitor->{sta}/10) . "/s");
	$monitor->{sra} = 0;
	$monitor->{sta} = 0;
	$monitor->{nba} = 0;
    }
    $label_cnx_type->set_label($netcnx->{type});
    $monitor->{$_} = 0 foreach 'sr', 'st';
    1;
}

sub get_val() {
    my $a = cat_("/proc/net/dev");
    $a =~ s/^.*?\n.*?\n//;
    $a =~ s/^\s*lo:.*?\n//;
    my @line = split(/\n/, $a);
    map {
	s/\s*(\w*)://;
	my $intf = $1;
	$monitor->{$intf}{val} = [split()];
	$monitor->{$intf}{intf} = $intf;
	$intf;
    } @line;
}

sub change_color {
    my ($color) = @_;
    my $dialog = _create_dialog(N("Color configuration"));
    my $doit;
    $dialog->vbox->add(my $colorsel = Gtk2::ColorSelection->new);
    gtkpack($dialog->action_area,
            gtksignal_connect(Gtk2::Button->new(N("Cancel")), clicked => sub {
                                  $doit = 1;
                                  $dialog->destroy;
                              }),
            gtksignal_connect(Gtk2::Button->new(N("Ok")), clicked => sub { 
                                  $dialog->destroy;
                              }),
           );
    $colorsel->set_current_color($color);
    $dialog->show_all;
    $dialog->run;
    $doit ? $colorsel->get_current_color : $color;
}

my $scale;
sub update() {
     if (!$during_connection) {
        my $isconnect = test_connected(0);
        if ($isconnect != -2) {
	    $isconnected = $isconnect; # save current state
	    $isconnect = test_connected(1); # start new test
       }
    };

    my @intfs = get_val(); # get values from /proc file system
    foreach (@intfs) {
	my $intf = $_;
	if (!member($intf,@interfaces)) {
	    $default_intf = $intf;
	    $monitor->{$intf}{initialr} = $monitor->{$intf}{val}[0];
	    $monitor->{$intf}{initialt} = $monitor->{$intf}{val}[8];
	    $darea->{$intf} = Gtk2::DrawingArea->new;
	    $darea->{$intf}->set_events(["pointer_motion_mask"]);
	    $notebook->append_page(gtkshow(my $page = gtkpack_(Gtk2::VBox->new(0,0),
					       0, gtkpack__(gtkset_border_width(Gtk2::HBox->new(0,0), 5),
							    gtksize($darea->{$intf}, $width, $height)),
					       0, gtkpack_(Gtk2::HBox->new(0,0),
							    1, gtkpack__(Gtk2::VBox->new(0,0),
								      gtkpack__(gtkset_border_width(Gtk2::HBox->new(0,5), 5),
										   gtksignal_connect(my $button_t = gtkset_relief(Gtk2::Button->new, 'none'), clicked => sub {
													 $colort = change_color($colort);
													 $gct->set_foreground($colort);
													 $_[0]->queue_draw;
												     }),
										   N("sent: "), $monitor->{$intf}{labelt} = Gtk2::Label->new("0")),
								      gtkpack__(gtkset_border_width(Gtk2::HBox->new(0,5), 5),
										   gtksignal_connect(my $button_r = gtkset_relief(Gtk2::Button->new, 'none'), clicked => sub {
													 $colorr = change_color($colorr);
													 $gcr->set_foreground($colorr);
													 $_[0]->queue_draw;
												     }),
										   N("received: "), $monitor->{$intf}{labelr} = Gtk2::Label->new("0")),
								      gtkpack__(gtkset_border_width(Gtk2::HBox->new(0,5), 5),
										   gtksignal_connect(my $button_a = gtkset_relief(Gtk2::Button->new, 'none'), clicked => sub {
													 $colora = change_color($colora);
													 $gca->set_foreground($colora);
													 $_[0]->queue_draw;
												     }),
										   N("average"))
								     ),
							    0, gtkpack__(gtkset_border_width(Gtk2::VBox->new(0,0), 5),
							      gtkadd(gtkset_shadow_type(Gtk2::Frame->new(N("Local measure")), 'etched_out'),
								     gtkpack__(gtkset_border_width(Gtk2::VBox->new(0,0), 5),
									       gtkpack__(Gtk2::HBox->new(0,0),
											 N("sent: "),
											 my $measure_t = Gtk2::Label->new("0")
											),
									       gtkpack__(Gtk2::HBox->new(0,0),
											 N("received: "),
											 my $measure_r = Gtk2::Label->new("0")
											)
									      )
									)
									)
							  )
					       )),
				   Gtk2::Label->new($intf));
	    foreach my $i ([$button_t, $gct], [$button_r, $gcr], [$button_a, $gca]) {
		$i->[0]->add(gtksignal_connect(gtkshow(gtksize(gtkset_size_request(Gtk2::DrawingArea->new, 10, 10), 10, 10)), expose_event => sub { $_[0]->window->draw_rectangle($i->[1], 1, 0, 0, 10, 10) }));
	    }
	    $notebook->set_current_page($notebook->page_num($page));
	    $monitor->{$intf}{page} = $notebook->page_num($page);
         $darea->{$intf}->realize;
         $pixmap = Gtk2::Gdk::Pixmap->new($darea->{$intf}->window, $width, $height, $darea->{$intf}->window->get_depth);
	    $monitor->{$intf}{referencer} = $monitor->{$intf}{val}[0];
	    $monitor->{$intf}{referencet} = $monitor->{$intf}{val}[8];
	    $pixmap->draw_rectangle($darea->{$intf}->style->black_gc, 1, 0, 0, $width, $height);
	    $darea->{$intf}->signal_connect(motion_notify_event => sub { 
                                    my (undef, $e) = @_;
                                    my $x = $e->x - 50;
                                    my $y = $e->y;
                                    my $received    = $x >= 0 ? $monitor->{$intf}{stack_r}[$x] : 0;
                                    my $transmitted = $x >= 0 ? $monitor->{$intf}{stack_t}[$x] : 0;
                                    my $type;
                                    $y * $scale / 150 < $transmitted and $type = N("transmitted");
                                    (150 - $y) * $scale / 150 < $received and $type = N("received");
                                    $measure_r->set_label(formatXiB($received));
                                    $measure_t->set_label(formatXiB($transmitted));
                                });
	    $darea->{$intf}->signal_connect(expose_event => sub {
                         $darea->{$intf}->window->draw_drawable($darea->{$intf}->style->bg_gc('normal'), $pixmap, 0, 0, 0, 0, $width, $height);
                     });
	}
    }
    foreach (@interfaces) {
	my $intf = $_;
     $notebook->remove_page($monitor->{$intf}{page}) unless member($intf,@intfs);
    }
    @interfaces = @intfs;
	if ($isconnected != -2 && $isconnected != -1 && !$during_connection) {
	    if ($isconnected == 1 && !in_ifconfig($netcnx->{NET_INTERFACE})) {
		$isconnected = 0;
		$statusbar->pop(1);
		$statusbar->push(1, N("Warning, another internet connection has been detected, maybe using your network"));
	    } else {
		#- translators : $netcnx->{type} is the type of network connection (modem, adsl...)
		$statusbar->pop(1);
		$statusbar->push(1, $isconnected == 1 ? N("Connected") : N("Not connected"));
	    }
         $button_connect->set("label", $isconnected == 1 ? N("Disconnect %s", $netcnx->{type}) : N("Connect %s", $netcnx->{type}));
	    $button_connect->set_sensitive(1);
	}
    if (!(-e $connect_file && -e $disconnect_file)) {
	$button_connect->set_sensitive(0);
	$button_connect->set("label", N("No internet connection configured"));
    }
    1;
}

sub in_ifconfig {
    my ($intf) = @_;
    -e '/sbin/ifconfig' or return 1;
    $intf eq '' and return 1;
    `/sbin/ifconfig` =~ /$intf/;
}

sub draw_monitor {
    my ($o, $intf) = @_;
    defined $darea->{$intf} or return;
    $pixmap->draw_rectangle($darea->{$intf}->style->black_gc, 1, 0, 0, $width, $height);
    my $maxr = 0;
    foreach (@{$o->{stack_r}}) { $maxr = $_ if $_ > $maxr }
    my $maxt = 0;
    foreach (@{$o->{stack_t}}) { $maxt = $_ if $_ > $maxt }
    my $ech = $maxr + $maxt;
    $ech == 0 and $ech = 1;
    $scale = $ech;
    my $step = 49;
    foreach (@{$o->{stack_t}}) {
	$pixmap->draw_rectangle($gct, 1, $step, 0, 1, $_*150/$ech);
	$step++;
    }
    $step = 49;
    my ($av1, $av2, $last_a);
    foreach (@{$o->{stack_ta}}) {
	if ($_ != -1) {
	    if (!defined $av1) { $av1 = $_ } else { defined $av2 or $av2 = $_ }
	    if ($av1 && $av2) {
		$pixmap->draw_line($gca, $step-15, $av1*150/$ech, $step-5, $av2*150/$ech);
		$av1 = $av2;
		undef $av2;
		$last_a = $step-50;
	    }
	}
	$step++;
    }
    $step = 49;
    foreach (@{$o->{stack_r}}) {
	$pixmap->draw_rectangle($gcr, 1, $step, 151-$_*150/$ech, 1, $_*150/$ech);
	$step++;
    }
    $step = 49;
    ($av1, $av2) = undef;
    foreach (@{$o->{stack_ra}}) {
	if ($_ != -1) {
	    if (!defined $av1) { $av1 = $_ } else { defined $av2 or $av2 = $_ }
	    if (defined $av1 && defined $av2) {
		$pixmap->draw_line($gca, $step-15, 151-$av1*150/$ech, $step-5, 151-$av2*150/$ech);
		$av1 = $av2;
		undef $av2;
	    }
	}
	$step++;
    }

    my $switch = 1;
    my $gcl = Gtk2::Gdk::GC->new($darea->{$intf}->window);
    $gcl->set_foreground($darea->{$intf}->style->white);
    $gcl->set_line_attributes(1, 'on-off-dash', 'not-last', 'round');
    for (my $i = 30; $i <= 120; $i += 30) {
	$pixmap->draw_line($gcl, 50, $i, 300, $i);
	my ($gc2, $text);
	my ($dif1, $dif2);
	if ($last_a) {
	    $dif1 = abs(150-(@{$o->{stack_ra}}[$last_a])*150/$ech - $i);
	    $dif2 = abs((@{$o->{stack_ta}}[$last_a])*150/$ech - $i);
	} else {
	    $dif1 = abs(150-(@{$o->{stack_r}}[@{$o->{stack_r}}-1])*150/$ech - $i);
	    $dif2 = abs((@{$o->{stack_t}}[@{$o->{stack_t}}-1])*150/$ech - $i);
	}
	if ($dif1 < $dif2) {
	    $text = formatXiB((150-$i)*$ech/150);
	    $gc2 = $gcr;
	    my $x_l = 5;
	    if ($i > 30 && $switch) {
		$pixmap->draw_line($gct, $x_l, 0, $x_l, $i-30);
		$pixmap->draw_line($gct, $x_l-1, 0, $x_l-1, $i-30);
		$pixmap->draw_line($gct, $x_l+1, 0, $x_l+1, $i-30);
		$pixmap->draw_polygon($gct, 1, $x_l-4, $i-30, $x_l+5, $i-30, $x_l, $i-25);
	    }
	    if ($switch) {
		$pixmap->draw_line($gcr, $x_l, 150, $x_l, $i);
		$pixmap->draw_line($gcr, $x_l-1, 150, $x_l-1, $i);
		$pixmap->draw_line($gcr, $x_l+1, 150, $x_l+1, $i);
		$pixmap->draw_polygon($gcr, 1, $x_l-5, $i, $x_l+5, $i, $x_l, $i-6);
	    }
	    undef $switch;
	} else {
	    $text = formatXiB($i*$ech/150);
	    $gc2 = $gct;
	}
     $pixmap->draw_layout($gc2, 45-string_width($darea->{$intf}, $text), $i-5, $darea->{$intf}->create_pango_layout($text));
    }
    $darea->{$intf}->queue_draw;
}


sub test_connected {
    my ($arg) = @_;
    $::testing || network::tools::test_connected($arg);
}