#!/usr/bin/perl
################################################################################
# Mandriva Online                                                              # 
#                                                                              #
# Copyright (C) 2003-2005 Mandriva                                             #
#                                                                              #
# Daouda Lo <daouda at mandriva dot com>                                       #
#                                                                              #
# This program is free software; you can redistribute it and/or modify         #
# it under the terms of the GNU General Public License Version 2 as            #
# published by the Free Software Foundation.                                   #
#                                                                              #
# 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 strict;
use POSIX ":sys_wait_h";
use Config;
use lib qw(/usr/lib/libDrakX);
use interactive;
use common;

use Gtk2::Pango;

BEGIN { unshift @::textdomains, 'mdkonline' }

use ugtk2 qw(:all);
use lib qw(/usr/lib/libDrakX/drakfirsttime);
use mdkonline;
use Gtk2::TrayIcon;
use Gtk2::NotificationBubble;

mdkonline::is_running('mdkapplet') and die "mdkapplet already running\n";

my $in = interactive->vnew('');

ugtk2::add_icon_path("/usr/share/mdkonline/pixmaps/");
my $arch = arch();
my $online_site = "https://www.mandrivaonline.com/";
my ($menu, $timeout, $refreshtm, $networktm, $eventbox, $img, $av_pkgs, $update_label, $lastch, $mLog, $buffer, $textview, $wlog, $textvw, $state_global, $MW_vbox, $bubble_applet);
my ($need_update, $raisedwindow, $isAvailable, $debug, $conf_launched) = (0, 0, 0, 0, 0);

my @proctable;

my $conffile = '/etc/sysconfig/mdkonline';
my $localdir = "$ENV{HOME}/.MdkOnline";
my $localfile = "$localdir/mdkonline";

my $release = mdkonline::get_release();

#compatibility
mkdir_p($localdir) if !-d $localdir;
-e "$ENV{HOME}/.mdkonline" and system("mv", "$ENV{HOME}/.mdkonline", $localfile);

my %state = (
	     okay => {
		      colour => [ 'okay' ],
		      changes => [ 'busy', 'critical', 'disconnected' ],
		      menu => [ 'configureApplet', 'check', 'weblink' ],
		      tt => [ N_("Your system is up-to-date") ]
		     },
 	     critical => {
		      colour => [ 'noconf' ],
		      changes => [ 'okay', 'busy', 'critical', 'disconnected' ],
		      menu => [ 'configureApplet', 'check', 'weblink' ],
		      tt => [ N_("Service configuration problem. Please check logs and send mail to support\@mandrivaonline.com") ]
		     },
	     busy => {
		      colour => [ 'busy' ], 
		      changes => [ 'okay', 'critical', 'error', 'disconnected' ],
		      menu => [],
		      tt => [ N_("System is busy. Please wait ...") ]
		     },
	     updates => {
		      colour => [ 'error' ],
		      changes => [ 'okay' ],
		      menu => [ 'update', 'check', 'weblink' ],
		      tt => [ N_("New updates are available for your system") ]
		     },
	     bundles => {
		      colour => [ 'bundle' ],
		      changes => [ 'okay' ],
		      menu => [ 'update', 'check', 'weblink' ],
		      tt => [ N_("New bundles are available for your system") ]
		      },
	     noconfig => {
		      colour => [ 'noconf' ],
		      changes => [ 'okay' ],
		      menu => [ 'weblink', 'register' ],
		      tt => [ N_("Service is not configured. Please click on \"Configure the service\"") ]
		     },
	     disconnected => {
		      colour => [ 'disconnect' ],
		      changes => [ 'okay', 'busy', 'critical', 'error' ],
		      menu => [ 'confNetwork' ],
		      tt => [ N_("Network is down. Please configure your network") ]
		     },
	     disabled => {
		      colour => [ 'disabled' ],
		      changes => [ 'okay', 'busy', 'critical', 'error' ],
		      menu => [ 'configureApplet', 'weblink' ],
		      tt => [ N_("Service is not activated. Please click on \"Online Website\"") ]
		     },
	     notsupported => {
		      colour => [ 'disabled' ],
		      changes => [ 'okay', 'busy', 'critical', 'error' ],
		      menu => [ 'weblink' ],
		      tt => [ N_("Release not supported (too old release, or development release)") ]
		     }
	    );

my %actions = (
	       'update' => { name => N("Install updates"), launch => sub { installUpdates() } },
	       'configureApplet' => { name => N("Configure the service"), launch => sub { configure() } },
	       'check' => { name => N("Check Updates"), launch => sub { 
				my $w = $in->wait_message(N("Please wait"), N("Check updates") . '...');
				checkUpdates(); undef $w } }, 
	       'weblink' => { name => N("Online WebSite"), launch => sub { mdkonline::get_site($online_site, 'info.php') } },
	       'confNetwork' => { name => N("Configure Network"), launch => sub { configNetwork() } },
	       'register' => { name => N("Configure Now!"), launch => sub { configure() } }
	      );

gtkadd(my $icon = Gtk2::TrayIcon->new("MdkApplet"), 
       gtkadd($eventbox = Gtk2::EventBox->new, 
	      gtkpack($img = Gtk2::Image->new)
	     )
      );
#$icon->shape_combine_mask($img, 0, 0);
$eventbox->signal_connect(button_press_event => sub {
			      if (!$raisedwindow) {
				  if ($_[1]->button == 1) {
				      if (-e $conffile) { $raisedwindow = 1; showMainWindow() } else { $raisedwindow = 0; configure() }
				  }
			      }
			      $_[1]->button == 3 && $menu and $menu->popup(undef, undef, undef, undef, $_[1]->button, $_[1]->time);
			  });
my ($opt) = @ARGV;
if ($opt eq '--force' || $opt eq '-f') { setAutoStart('TRUE') }
if ($opt eq '--debug') { $debug = 1 }

shouldStart() or die "$localfile should be set to TRUE: please use --force or -f option to launch applet";

checkConfig();
checkUpdates();
cronUpdate();

$icon->show_all;

$SIG{USR1} = 'IGNORE';
$SIG{USR2} = 'IGNORE';
$SIG{CHLD} = \&harvester;

Glib::Timeout->add(200, sub { harvester('CHLD', 1); 1 });

Gtk2->main;

ugtk2::exit(0);

# Signal management 
sub harvester {
    my ($signame, $clean) = @_;
    my $childpid ;
    do {
        $childpid = waitpid(-1, &WNOHANG);
        WIFEXITED($?) and refresh_gui(1);
    } while $childpid > 0;
    return if $clean;
}
sub fork_exec {
    my ($prog, $pid_table) = @_;
    my $pid = fork();
    if (defined $pid) {
        !$pid and do { exec($prog) or POSIX::_exit() };
        push @$pid_table, $pid;
        return $pid;
    } else {
        refresh_gui(1);                                                                        
    }                                                                                       
}
sub refresh_gui {
    my ($sens) = @_;
    !$conf_launched and silentCheck(); $conf_launched = 0;
    $MW_vbox and $MW_vbox->set_sensitive($sens);
}

sub showMainWindow() {
    my $w = Gtk2::Window->new('toplevel');
    $w->set_title(N("Mandriva Linux Updates Applet"));
    $w->signal_connect(delete_event => sub { $w->destroy; $raisedwindow = 0 });
    gtkset_size_request($w, 400, 300);
    $w->set_position('center');
    $w->set_icon(Gtk2::Gdk::Pixbuf->new_from_file('/usr/share/icons/mini/mdkonline.png'));
    $textvw = Gtk2::TextView->new;
    my ($choice, $time, $update_label, $lastch);
    my $autocheck;
    gtkadd($w,
           gtkpack_($MW_vbox = Gtk2::VBox->new(0, 5),
		    0, gtkadd(gtkset_shadow_type(Gtk2::Frame->new(N("Actions")), 'etched_in'),
			      gtkpack_(Gtk2::VBox->new(0, 3),
				       1, gtksignal_connect(Gtk2::Button->new(N("Install updates")), clicked => sub { installUpdates() }),
				       1, gtksignal_connect(Gtk2::Button->new(N("Configure")), clicked => sub { configure() }),
				       1, gtksignal_connect(Gtk2::Button->new(N("Check updates")), clicked => sub {
								my $w = $in->wait_message(N("Please wait"), N("Check updates") . '...');
								checkUpdates(); undef $w }),
				       1, gtksignal_connect(Gtk2::Button->new(N("See logs")), clicked => sub { if (defined $wlog) { $wlog->{window}->show } else { $wlog = displayLogs(); $wlog->main } }),
				      )
			     ),
		    1, gtkadd(gtkset_shadow_type(Gtk2::Frame->new(N("Status")), 'etched_in'),
			    gtkpack_(Gtk2::VBox->new(0, 3),
				 1, create_scrolled_window(gtktext_insert($textvw, refresh_contents($state_global), wrap_mode => 'none')))),
		    0, gtkpack(Gtk2::HSeparator->new),
		    0, gtkpack(gtksignal_connect(Gtk2::Button->new(N("Close")), clicked => sub {
						     if (defined $wlog) { $wlog->destroy; undef $wlog }
						     $w->destroy; $raisedwindow = 0;
						 })
			      )
		   )
	  );
    $w->show_all;
    gtkflush()
}
sub setLabel {
    my ($widget, $string) = shift;
    $widget->set_label($string) if defined $widget;
    gtkflush();
}
sub refresh_status {
    my $status = shift;
    gtktext_insert($textvw, refresh_contents($status), wrap_mode => 'none');
}
sub refresh_contents {
    my $status = shift;
    my $color = {};
    my %h = getVarsFromSh($conffile); 
    foreach my $l (['first', 'red'], ['second', 'royalblue3'], ['third', 'green']) { 
	$color->{$l->[0]} = { 'foreground' => $l->[1], 'weight' => Gtk2::Pango->PANGO_WEIGHT_BOLD };
    }
    my $fixed_tag = { 'font' => 'monospace' };
    setLastTime();
    my $last = lastCheck();
    my $contents = [ [ N("Network Connection: "), $color->{second} ], [ isNetwork() ? N("Up") . "\n" : N("Down") . "\n", isNetwork() ? $color->{third} : $color->{first} ],
		     [ N("Last check: "), $color->{second} ], [ "$last\n", $fixed_tag ],
		     [ N("Machine name:"), $color->{second} ], [ $h{MACHINE} . "\n", $fixed_tag ],
		     [ N("Updates: "), $color->{second} ], [ common::sprintf_fixutf8($state{$status}{tt}[0]), $status eq 'okay' ? $color->{third} : $color->{first} ],
		   ];
    $contents;
}
sub configNetwork() { logIt(N("Launching drakconnect\n")); refresh_gui(0); fork_exec("/usr/sbin/drakconnect") }
sub installUpdates {
    my $binfile = '/usr/bin/mdkapplet';
    my $oldmd5 = $release <= 10.2 ? mdkonline::md5file($binfile) : common::md5file($binfile);
    logIt(N("Launching mdkupdate --applet\n"));
    refresh_gui(0);
    fork_exec("/usr/sbin/mdkupdate --applet");
    my $newmd5 = $release <= 10.2 ? mdkonline::md5file($binfile) : common::md5file($binfile);
    if ($newmd5 ne $oldmd5) { logIt(N("Mandriva Online seems to be reinstalled, reloading applet ....")); exec('/usr/bin/mdkapplet') }
    #my $w = $in->wait_message(N("Please wait"), N("Check updates"));
    silentCheck(); gtkflush();
    #undef $w;
    my $mdkupdate_status = cat_('/var/tmp/mdkupdate.log');
    if ($mdkupdate_status && $mdkupdate_status !~ /OK/) { logIt($mdkupdate_status); $in->ask_warn(N("Mandriva Linux Updates Applet"), $mdkupdate_status) }
}
sub silentCheck {
    my $link = $online_site . "online3_RemoteAction.php" . '?action=UpdateList';
    my %h = getVarsFromSh($conffile); 
    my ($u, $ct);
    logIt(N("Computing new updates...\n"));
    my $link = $link . '&log=' . $h{LOGIN} . '&host=' . $h{MACHINE};
    logIt(N("Connecting to") . " $link ...\n");
    gtkflush(); go2State('busy'); gtkflush();
    my $response = mdkonline::get_from_URL($link, 'MdkAppletAgent/0.1');
    if ($response->is_error) { 
	my $status_err = $response->status_line;
	go2State('critical'); 
	logIt("Unable to contact Mandriva Online server: $status_err") and return; 
    }
    $ct = $response->content;
    if ($debug) {
	print "\nCONTENTS = $ct\n" if $debug;
	logIt(N("Response from Mandriva Online server\n") . $ct);
    }
    if ($ct =~ /rpm/) { $u = 11 } elsif ($ct =~ m/(\d+)/) { $u = sprintf("%d", $1) } else { $u = 10 }
    #   99 - log or host or action or pass empty, wrong action
    #   98 - wrong pass
    #   97 - host not active
    logIt("Returned value after association (silentCheck)= $u\n") if $debug;
    my $retcode = {
		   10 => sub { okState() },
		   11 => sub {
		       my @contents = sort(split /\n/, $ct);
		       my @t = split /\n/, `rpm -qa`;
		       my $comp = 0;
		       foreach my $cand (@contents) {
			   $cand =~ s/\.(i586|ppc|ia64|noarch|x86_64|amd64|ppc64).rpm//;
			   my ($name, $ver, $rel) = $cand =~ /(.*)-(.*)-(.*)$/;
			   $comp = compareWithInstalled($name, $ver, $rel, \@t);
			   #print "COMP =  $comp\n";
			   if ($comp) { $isAvailable = 1; logIt(N("Checking... Updates are available\n") . "\n"); go2State('updates') } else { $isAvailable = 0 }
			   $comp and last;
		       }
		       !$comp and okState();
		   },
		   94 => sub { logIt(N("Development release not supported by service")); go2State('notsupported') },
		   95 => sub { logIt(N("Too old release not supported by service")); go2State('notsupported') },
		   96 => sub { logIt(N("Unknown state")); go2State('critical') },
		   97 => sub { logIt(N("Online services disabled. Contact Mandriva Online site\n")); go2State('disabled') },
		   98 => sub { logIt(N("Wrong Password.\n")); go2State('critical') },
		   99 => sub { logIt(N("Wrong Action or host or login.\n")); go2State('critical') },
                   500 => sub { logIt(N("Something is wrong with your network settings (check your route, firewall or proxy settings)\n")); go2State('critical') }
		  };
    eval { $retcode->{$u}->() }; if ($@) { logIt(N("Problem occured while connecting to the server, please contact the support team")); go2State('critical') }
}
sub okState { $isAvailable = 0; logIt(N("System is up-to-date\n")); go2State('okay') }
sub compareWithInstalled {
    my ($name, $ver, $rel, $t) = @_;
    my $isUpdate = 0;
    foreach my $p (@$t) { 
	my ($n, $v, $r) = $p =~ /(.*)-(.*)-(.*)$/;
	if ($name eq $n) {
	    my ($iu, $ir);
	    if ($debug) { $iu = mdkonline::rpm_ver_cmp($ver, $v); $ir = mdkonline::rpm_ver_cmp($rel, $r) } 
	    if (mdkonline::rpm_ver_cmp($ver, $v) > 0 || mdkonline::rpm_ver_cmp($ver, $v) == 0 && mdkonline::rpm_ver_cmp($rel, $r) > 0) {
		logIt("$name-$ver-$rel $n-$v-$r *** CMPVER=$iu ** CMPREL = $ir \n") if $debug;
		$isUpdate = 1 and last;
	    }
	}
    }
    $isUpdate;
}
sub cronUpdate {
    Glib::Timeout->add(10*1000, sub {
	checkConfig();
	1;
    });
    $timeout = Glib::Timeout->add(3*60*60*1000, sub {
				      checkUpdates();
				      1;
				  });
}
sub lastCheck {
    my %h = getVarsFromSh($localfile);
    my ($t, $l);
    $t = $h{LASTCHECK};
    $t =~ s/_/  /g;
    $t || N("No check");
}
sub getTime {
    my $d = localtime();
    $d =~ s/\s+/_/g;
    $d;
}
sub setLastTime {
    my $date = getTime();
    setVar($localfile, 'LASTCHECK', $date);
}
sub checkConfig {
    if (!-e $conffile) {
	logIt(N("Checking config file: Not present\n"));
	go2State('noconfig');
    } elsif (!isNetwork()) {
	logIt(N("Checking Network: seems disabled\n"));
	go2State('disconnected');
    } elsif (member($state_global, qw(disconnected noconfig))) {
	silentCheck(); #- state has changed, update
    }
}
sub checkUpdates {
    member($state_global, qw(disconnected noconfig)) or silentCheck();
}
sub go2State {
    my $state = shift;
    $menu->destroy if $menu;
    $menu = setState($state);
    $state_global = $state;
    defined $textvw and refresh_status($state);
}
sub isNetwork {
    my $network;
    if ($release <= 10.0) {
	$network = gethostbyname("mandrivaonline.com") ? 1 : 0;
    } elsif ($release <= 10.2) {
	require network::netconnect;
 	require network::tools;
	my ($netcnx, $netc, $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);
	$network = to_bool($gw_address);
    } else {
	require network::network;
	require network::tools;
	my $net = {};
    	network::network::read_net_conf($net);
	my ($_gw_intf, $_is_up, $gw_address, $_dns_server) = network::tools::get_internet_connection($net);
	$network = to_bool($gw_address);
    }
    $network;
}
sub configure {
    refresh_gui(0);
    fork_exec("/usr/sbin/mdkonline");
    $conf_launched = 1;
}
sub displayLogs {
    my $w = ugtk2->new(N("Logs"), center => 1);
    gtkset_size_request($w->{window}, 500, 400);
    $w->{window}->signal_connect(delete_event => sub { $w->destroy; undef $wlog });
    $textview = Gtk2::TextView->new;
    $buffer = $textview->get_buffer;
    gtkadd($w->{window},
	   gtkpack_(Gtk2::VBox->new(0, 2),
		    1, create_scrolled_window(gtktext_insert($textview, $mLog)),
		    0, Gtk2::HSeparator->new,
		    0,  gtkpack_(Gtk2::HBox->new(0, 5),
				 0, gtksignal_connect(my $close = Gtk2::Button->new(N("Close")), 
						      clicked => sub { 
							  $w->destroy;
							  undef $wlog;
						      }),
				 1, Gtk2::Label->new(""),
				 0, gtksignal_connect(my $clear = Gtk2::Button->new(N("Clear")),
						      clicked => sub {
							  $mLog = '';
							  $buffer->set_text($mLog);
						      }),
				)));
    $w;
}
sub shouldStart() {
    my %p = getVarsFromSh($localfile);
    my $ret = $p{AUTOSTART} eq 'FALSE' ? 0 : 1;
    $ret;
}
sub setState {
    my $state_type = shift;
    my $checkme;
    my $arr = $state{$state_type}{menu};
    my $tmp = gtkcreate_pixbuf($state{$state_type}{colour}[0]);
    $img->set_from_pixbuf($tmp);
    #my $tooltip = Gtk2::Tooltips->new;
    gtkset_tip(new Gtk2::Tooltips, $eventbox, formatAlaTeX(common::sprintf_fixutf8(translate($state{$state_type}{tt}[0]))));
    my $menu = Gtk2::Menu->new;
    foreach (@$arr) { 
	my $l = $actions{$_}{name};
	$menu->append(gtksignal_connect(gtkshow(Gtk2::MenuItem->new_with_label($actions{$_}{name})), activate => $actions{$_}{launch}));
    }
    $menu->append(gtkshow(Gtk2::SeparatorMenuItem->new));
    $menu->append(gtksignal_connect(gtkshow(Gtk2::MenuItem->new_with_label(N("About..."))), activate => sub { mdkonline::get_site($online_site) }));
    $menu->append(gtksignal_connect(gtkset_active($checkme = Gtk2::CheckMenuItem->new_with_label(N("Always launch on startup")), shouldStart()), toggled => sub { setAutoStart(uc(bool2text($checkme->get_active))) }));
    $checkme->show;
    $menu->append(gtksignal_connect(gtkshow(Gtk2::MenuItem->new_with_label(N("Quit"))), activate => sub { mainQuit() }));
    $menu;
}
sub logIt {
    my $log = shift;
    my ($Second, $Minute, $Hour, $Day, $Month, $Year, $WeekDay, $DayOfYear, $IsDST) = localtime();
    $mLog .= $Hour . ':' . $Minute . ':' . $Second . '  ' . $log;
    if (defined $wlog) { $buffer->insert_at_cursor($log) }
}
sub setVar {
    my ($file, $var, $st) = @_;
    my %s = getVarsFromSh($file);
    $s{$var} = $st;
    setVarsInSh($file, \%s);
}
sub setAutoStart {
    my $state = shift;
    my $date = getTime();
    if (-f $localfile) {
	setVar($localfile, 'AUTOSTART', $state);
    } else { output_p $localfile, 
	       qq(AUTOSTART=$state
LASTCHECK=$date
);
	 }
}
sub mainQuit() {
#    setAutoStart('FALSE');
    Glib::Source->remove($timeout) if $timeout;
    Gtk2->main_quit;
}