diff options
Diffstat (limited to 'perl-install/standalone/net_applet')
| -rw-r--r-- | perl-install/standalone/net_applet | 106 |
1 files changed, 59 insertions, 47 deletions
diff --git a/perl-install/standalone/net_applet b/perl-install/standalone/net_applet index d71dd102d..c005862cc 100644 --- a/perl-install/standalone/net_applet +++ b/perl-install/standalone/net_applet @@ -1,30 +1,24 @@ #!/usr/bin/perl -#use strict; +use strict; use lib qw(/usr/lib/libDrakX); use c; use common; +use standalone; use Digest::MD5; +use network::netconnect; +use network::tools; use Gtk2::TrayIcon; -eval { require ugtk2; ugtk2->import(qw(:all)); require Gtk2::Pango }; -if ($@) { - print "This program cannot be run in console mode.\n"; - c::_exit(0); #- skip ugtk2::END -} +use ugtk2 qw(:create :helpers :wrappers); my ($eventbox, $img); -my ($menu, $_timeout); -my $raisedwindow = 0; +my ($current_state, $menu, $timeout); my $onstartupfile = "$ENV{HOME}/.net_applet"; add_icon_path("/usr/share/libDrakX/pixmaps/"); -#don't launch it twice :) -#my @pids = fuzzy_pidofs(qr/\bnet_applet\b/); -#foreach (@pids) { -# print "pid = $_ \n"; -#} -#@pids > 1 and die "net_applet already running\n"; +# Allow multiple instances, but only one per user: +is_running('net_applet') and die "net_applet already running\n"; my $prog_name = "/usr/bin/net_applet"; my $current_md5 = md5file($prog_name); @@ -32,20 +26,34 @@ my %appletstate = ( connected => { colour => [ 'connected' ], changes => [ 'disconnected', 'error', 'busy' ], - menu => [ 'confNetwork', 'monitorNetwork', 'refresh', 'help' ], - tt => [ N("Network is up on interface ") ] + menu => [ 'downNetwork', 'confNetwork', 'monitorNetwork', 'refresh', 'help' ], + tt => [ N_("Network is up on interface %s") ] }, disconnected => { colour => [ 'disconnected' ], changes => [ 'connected', 'error', 'busy' ], - menu => [ 'confNetwork', 'refresh', 'help' ], - tt => [ N("Network is down. Click on \"configure Network\"") ] - } + menu => [ 'upNetwork', 'confNetwork', 'refresh', 'help' ], + tt => [ + #-PO: keep the "Configure Network" substring synced with the "Configure Network" message below + N_("Network is down on interface %s. Click on \"Configure Network\"") + ] + }, + notconfigured => { + colour => [ 'disconnected' ], + changes => [ 'connected' ], + menu => [ 'confNetwork', 'refresh', 'help' ], + tt => [ + N_("You don't have any configured Internet connection. +Run the \"Add Connection\" assistant from the Mandrakelinux Control Center") + ] + } ); my %actions = ( + 'upNetwork' => { name => sub { N("Connect %s", $_[0]) }, launch => \&network::tools::start_interface }, + 'downNetwork' => { name => sub { N("Disconnect %s", $_[0]) }, launch => \&network::tools::stop_interface }, + 'monitorNetwork' => { name => N("Monitor Network"), launch => sub { system("/usr/sbin/net_monitor --defaultintf $_[0] &") } }, 'confNetwork' => { name => N("Configure Network"), launch => sub { system("/usr/sbin/drakconnect --skip-wizard &") } }, - 'monitorNetwork' => { name => N("Monitor Network"), launch => sub { system("/usr/sbin/net_monitor &") } }, 'refresh' => { name => N("Refresh"), launch => sub { checkNetwork() } }, 'help' => { name => N("Get Online Help"), launch => sub { system("drakhelp --id internet-connection &") } } ); @@ -56,11 +64,9 @@ gtkadd(my $icon = Gtk2::TrayIcon->new("Net_Applet"), ) ); $eventbox->signal_connect(button_press_event => sub { - if (!$raisedwindow) { - if ($_[1]->button == 1) { - $raisedwindow = 1; netMonitor() - } - } + if ($_[1]->button == 1) { + is_running('net_monitor') or netMonitor() + } $_[1]->button == 3 && $menu and $menu->popup(undef, undef, undef, undef, $_[1]->button, $_[1]->time); }); my ($opt) = @ARGV; @@ -76,6 +82,14 @@ Gtk2->main; ugtk2::exit(0); +sub is_running { + my ($name) = @_; + any { + my ($ppid, $pid, $n) = /^\s*(\d+)\s+(\d+)\s+(.*)/; + #- to run ps, perl may create some process with $name as name and 1 as ppid + $ppid != 1 && $pid != $$ && $n eq $name; + } `ps -o '%P %p %c' -u $ENV{USER}`; +} sub shouldStart() { my %p = getVarsFromSh($onstartupfile); my $ret = $p{AUTOSTART} eq 'FALSE' ? 0 : 1; @@ -92,27 +106,18 @@ sub md5file { return wantarray() ? @md5 : $md5[0]; } sub netMonitor() { - system("/usr/sbin/net_monitor --testing"); + system("/usr/sbin/net_monitor&"); checkNetwork() } sub checkNetwork() { - my $gatewayconf = '/etc/sysconfig/network'; - my %h = getVarsFromSh($gatewayconf); - my $new_md5 = md5file($prog_name); - # print "NEW($newmd5) = OLD($oldmd5)\n" - my ($gd, $addrgd); - if ($h{GATEWAYDEV}) { - $addrgd = getIP($gd = $h{GATEWAYDEV}); - } else { - ($addrgd, $gd) = `/sbin/ip route show` =~ /^default\s+via\s+(\S+).*\s+dev\s+(\S+)/m; - } - - $gd or die "No Gatewaydev defined"; - - if ($addrgd) { - go2State('connected', $gd) - } else { go2State('disconnected', '') } + my $netcnx = {}; + my $netc = {}; + my $intf = {}; + network::netconnect::read_net_conf($netcnx, $netc, $intf); + my ($gw_intf, $is_up, $gw_address, $dns_server) = network::tools::get_internet_connection($netc, $intf); + go2State($gw_address ? 'connected' : $gw_intf ? 'disconnected' : 'notconfigured', $gw_intf); + my $new_md5 = md5file($prog_name); if ($new_md5 ne $current_md5) { exec($prog_name) }; } sub getIP { @@ -123,14 +128,18 @@ sub getIP { return wantarray() ? @ip : $ip[0]; } sub cronNetwork() { - $_timeout = Glib::Timeout->add(60*1000, sub { + $timeout = Glib::Timeout->add(5*1000, sub { checkNetwork(); 1; }); } sub go2State { - $menu and $menu->destroy; - $menu = setState(@_) + my ($state_type, $interface) = @_; + if ($current_state ne $state_type) { + $current_state = $state_type; + $menu and $menu->destroy; + $menu = setState($state_type, $interface); + } } sub setState { my ($state_type, $interface) = @_; @@ -138,10 +147,12 @@ sub setState { my $arr = $appletstate{$state_type}{menu}; my $tmp = gtkcreate_pixbuf($appletstate{$state_type}{colour}[0]); $img->set_from_pixbuf($tmp); - gtkset_tip(new Gtk2::Tooltips, $eventbox, formatAlaTeX($appletstate{$state_type}{tt}[0] . $interface)); + gtkset_tip(Gtk2::Tooltips->new, $eventbox, formatAlaTeX(common::sprintf_fixutf8(translate($appletstate{$state_type}{tt}[0]), $interface))); my $menu = Gtk2::Menu->new; foreach (@$arr) { - $menu->append(gtksignal_connect(gtkshow(Gtk2::MenuItem->new_with_label($actions{$_}{name})), activate => $actions{$_}{launch})); + my $name = ref($actions{$_}{name}) eq 'CODE' ? $actions{$_}{name}->($interface) : $actions{$_}{name}; + my $launch = $actions{$_}{launch}; + $menu->append(gtksignal_connect(gtkshow(Gtk2::MenuItem->new_with_label($name)), activate => sub { $launch->($interface) })); } $menu->append(gtkshow(Gtk2::SeparatorMenuItem->new)); $menu->append(gtksignal_connect(gtkset_active($checkmi = Gtk2::CheckMenuItem->new_with_label(N("Always launch on startup")), shouldStart()), toggled => sub { setAutoStart(uc(bool2text($checkmi->get_active))) })); @@ -150,6 +161,7 @@ sub setState { $menu } sub mainQuit() { + Glib::Source->remove($timeout) if $timeout; Gtk2->main_quit } sub setAutoStart { |
