#!/usr/bin/perl

use strict;
use warnings;
use IO::File;
use Gtk2 '-init';
use Gtk2::TrayIcon;

# Timeout, weil Panel manchmal noch nicht geladen ist;
# es ist dann eventuell keine Anzeige zu sehen!
sleep 10;

my $title = 'machpause';
my $version = '0.6';

my $info = '/var/cache/machpause/shutdown';
my $config_dir = '/etc/machpause';

my $cur_user = '';
if ($ENV{'USER'}){
    $cur_user = $ENV{'USER'};
}

my $window_central = Gtk2::Window->new();
$window_central->set_default_size(350,30);
$window_central->set_title ($title . ' ' . $version);
$window_central->set_position('center_always');
$window_central->signal_connect('delete_event' => sub { ende(); });

my $iconfile = '/usr/share/pixmaps/machpause.xpm';
if (-f $iconfile){
    $window_central->set_default_icon_from_file($iconfile);
}

my $vbox = Gtk2::VBox->new(0, 10);
$vbox->set_border_width(20);
$window_central->add($vbox);

my $hbox = Gtk2::HBox->new(0, 10);
$hbox->set_border_width(20);
$vbox->pack_start($hbox, 0, 0, 0);

my $start_label= Gtk2::Label->new();
$hbox->pack_start($start_label, 0, 0, 0);

my $b_close = Gtk2::Button->new_from_stock('gtk-close');
$b_close->signal_connect (clicked => sub { $window_central->hide(); });
$vbox->pack_end($b_close, 0, 0, 0);

$window_central->show_all();
$window_central->hide();

my $fontsize = 12000;

my $timer     = -1;
my $click     = 0;
my $icon      = Gtk2::TrayIcon->new('timer');
my $menu      = Gtk2::Menu->new();

my $buffer =  $title . " Version: " . $version;
my $item = Gtk2::ImageMenuItem->new_with_mnemonic($buffer);
$item->set_image(Gtk2::Image->new_from_icon_name('gtk-dialog-info', 'menu'));
$menu->append($item);
$item->signal_connect('activate', sub { about_dialog(); });
$item->show();
$item = Gtk2::SeparatorMenuItem->new();
$menu->append($item);
$item->show();
$item = Gtk2::ImageMenuItem->new_with_mnemonic('Wochenplan');
$item->set_image(Gtk2::Image->new_from_icon_name('gtk-home', 'menu'));
$menu->append($item);
$item->signal_connect('activate', sub { load_status_window(); });
$item->show();
$item = Gtk2::ImageMenuItem->new_with_mnemonic('Status');
$item->set_image(Gtk2::Image->new_from_icon_name('gtk-refresh', 'menu'));
$menu->append($item);
$item->signal_connect('activate', sub { $window_central->show(); });
$item->show();
$item = Gtk2::SeparatorMenuItem->new();
$menu->append($item);
$item->show();
$item = Gtk2::ImageMenuItem->new_with_mnemonic('Exit');
$item->set_image(Gtk2::Image->new_from_icon_name('gtk-quit', 'menu'));
$menu->append($item);
$item->signal_connect('activate', sub { ende(); });
$item->show();

my $label     = Gtk2::Label->new();
my $tooltip   = Gtk2::Tooltips->new();
my $event_box = Gtk2::EventBox->new();

$event_box->signal_connect('button-release-event', 
    sub {
	my ($widget_mouse, $event_mouse) = @_;
	my $mouse_button_nr = $event_mouse->button();
	if ($mouse_button_nr == 1){
	    show_central_window();
	}
	else {
    	    $menu->popup(undef, undef, undef, undef, 0, 0); return 1;
	}
    });

$event_box->add($label);
$icon->add($event_box);
$icon->show_all();

check_status();
running();
Gtk2->main();

sub running {
    if ($timer > 9) {
	$fontsize = 14000;
	if ($timer > 99) {
	    $fontsize = 8000;
	}
        $label->set_markup('<span foreground="#080"
                    size="'.$fontsize.'">'.$timer.'</span>');
	$start_label->set_markup('<span foreground="#080" 
		    size="30000">Shutdown in '.$timer.' Minuten.</span>');
    }
    elsif ($timer < 0) {
	$fontsize = 15500;
	$label->set_markup('<span foreground="#000"
		    size="'.$fontsize.'">X</span>');
	$start_label->set_markup('<span foreground="#000" 
		    size="30000">Shutdown ist nicht aktiv.</span>');
    }
    else {
	$fontsize = 15000;
        $label->set_markup('<span foreground="#f00"
                    size="'.$fontsize.'">'.$timer.'</span>');
	$start_label->set_markup('<span foreground="#f00" 
		    size="30000">Shutdown in '.$timer.' Minuten.</span>');
    }
    $tooltip->set_tip($icon, "Shutdown in $timer Minuten.");
    if ($timer == 30 || $timer == 10) {
	show_central_window();
    }
    if ($timer <= 5 && $timer > 0) {
	show_central_window();
    }
    if ($timer == 0) {
        $start_label->set_markup("<span foreground='#f00'
        size='30000'>Shutdown-Warnung:\nder Computer wird\nin wenigen Sekunden\nheruntergefahren!</span>");	
        show_central_window();
    }    
    check_status();
    Glib::Timeout->add( 60000, sub { running(); 0; } );
}

sub check_status {
    if (-f "$info") {
        my $file = IO::File->new("<$info");
        if ($file) {
            CACHE:
            while (<$file>) {
		if ( / \A \d+ \z /xms ) {
                    $timer = $_;
                    last CACHE;
                }
    	    }
    	    $file->close();
        }
    }
    if ($cur_user) {
	my $today = `LANG=C date +%A`;
	chomp $today;
        if (! -f "/var/cache/machpause/$today/$cur_user") {
            $timer = -1;
        }
        if (! -f "$config_dir/$today/$cur_user") {
            $timer = -1;
        }		
    }
}

sub show_central_window {
    $window_central->set_keep_above('1');
    $window_central->grab_focus();
    $window_central->set_modal('1');
    $window_central->set_focus($b_close);
    $window_central->show();
}

sub load_status_window {
    my $win = Gtk2::Window->new();
    $win->signal_connect('delete_event' => sub {$win->destroy();});
    $win->set_default_size(400,200);
    $win->set_default_icon_from_file($iconfile);
    $win->set_title("Wochenplan -- User: " . $cur_user);
    $win->set_position('center');
    $win->set_border_width(3);
    $win->set_modal(0);
    my $vbox = Gtk2::VBox->new(1, 0);
    $vbox->set_border_width(20);
    $win->add($vbox);
    my @weekdays = qw {Monday Tuesday Wednesday Thursday Friday Saturday Sunday};
    foreach my $day (@weekdays) {
	my $pvbox = Gtk2::HBox->new(1, 0);
        my $label = Gtk2::Label->new($day);
	my $conf_val = 'undefiniert';
	if (-f "$config_dir/$day/$cur_user") {
    	    my $cfile = IO::File->new("<$config_dir/$day/$cur_user");
    	    if ($cfile) {
        	NUMBER:
        	while (<$cfile>) {
		    if ( / \A \d+ \z /xms ) {
                	$conf_val = $_;
                	chomp($conf_val);
                	last NUMBER;
            	    }
        	}
        	$cfile->close();
    	    }
	}
	my $day_val = Gtk2::Label->new($conf_val);
	$pvbox->pack_start($label, 1, 1, 0);
	$pvbox->pack_end($day_val, 1, 1, 0);
	$vbox->pack_start($pvbox, 1, 1, 0);
    }
    my $hbox = Gtk2::HBox->new(0, 0);
    $hbox->set_border_width(2);
    $vbox->pack_start($hbox, 0, 0, 0);
    my $okbutton = Gtk2::Button->new_from_stock('gtk-ok');
    $hbox->pack_end($okbutton, 0, 0, 0);
    $okbutton->signal_connect (clicked => sub { $win->destroy(); });
    $win->show_all;
}

sub about_dialog {
    my $ich = 'Uwe Kerstan';
    my $pname = 'Machpause';
    my $comment = 'Shutdown User Controll';
    my $homepage = 'http://www.linuxer.onlinehome.de/';
    my $copy = '2008';
    my $lizenz = "This package is free software; you can redistribute it and/or modify\n";
    $lizenz .= "it under the terms of the GNU General Public License as published by\n";
    $lizenz .= "the Free Software Foundation; version 2 dated June, 1991.\n\n";
    $lizenz .= "This package is distributed in the hope that it will be useful,\n";
    $lizenz .= "but WITHOUT ANY WARRANTY; without even the implied warranty of,\n";
    $lizenz .= "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n";
    $lizenz .= "GNU General Public License for more details.\n\n";
    $lizenz .= "You should have received a copy of the GNU General Public License\n";
    $lizenz .= "along with this package; if not, write to the Free Software\n";
    $lizenz .= "Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA\n";
    $lizenz .= "02111-1307, USA.\n\n";
    $lizenz .= "On Debian GNU/Linux systems, the complete text of the GNU General\n";
    $lizenz .= "Public License can be found in /usr/share/common-licenses/GPL.\n";

    if ($Gtk2::VERSION > 1.144){
        Gtk2->show_about_dialog(undef,
        'program-name' => $pname,
        'authors' => $ich,
        'artists' => $ich,
        'comments' => $comment,
        'copyright' => $copy,
        'license' => $lizenz,
        'version' => $version,
        'website' => $homepage);
    }else{
        my $about = Gtk2::AboutDialog->new();
        $about->set_authors($ich);
        $about->set_artists($ich);
        $about->set_comments($comment);
        $about->set_copyright($copy);
        $about->set_license($lizenz);
        $about->set_name($pname);;
        $about->set_version($version);
        $about->set_website($homepage);
        $about->run();
    }
}

sub ende {
    Gtk2->main_quit();
    1;
}

### eof
