#!/usr/bin/perl
# AQ
# 20120918

BEGIN { $ENV{LC_ALL} = "ru_RU"; }
use IPC::SysV qw(IPC_STAT IPC_PRIVATE);

use utf8;
use locale ':not_characters';
use Glib qw/TRUE FALSE/;
use Gtk2 '-init';

my $seconds = 180;
my $minresolution = 1280;

Gtk2::Rc->parse_string(<<__);
style "superbig" {
    font_name ="Droid Sans 14"
}

style "normal" {
    font_name = "Droid Sans 11"
}

style "error" {
    font_name ="Droid Sans 11"
    text[NORMAL] = "#FF0000"
    fg[NORMAL] = "#FF0000"
}

widget "*" style "normal"
widget "*error*" style "error"
widget "*title*" style "superbig"

__

sub callback {
	my $widget = shift;
	my $data   = shift;
	if ($data =~ m/setup/) {
	$seconds = 10000;
	if ($width < $minresolution) {
	  $window->resize(900,300);
	} else {
	  $window->resize(900,250);
	}
	$window->set_position(GTK_WIN_POS_CENTER);
	$box1->hide;
	
	$button_exit = Gtk2::Button->new("Выход");
	$image_exit  = Gtk2::Image->new_from_file('/usr/share/icons/Faenza/actions/48/application-exit.png');
	$button_exit->set_image ($image_exit);
	$button_exit->set_image_position ('top');
	$button_exit->signal_connect("clicked" => \&on_stop);
	$table->attach_defaults($button_exit, 0, 6, 5, 7);
	$image_exit->show;
	$button_exit->show;

	if ($width < $minresolution) {
	$box2 = Gtk2::HBox->new (TRUE, 2);
	$table->attach_defaults($box2, 0, 6, 1, 5);
	$label_error = Gtk2::Label->new ("Для оптимальной работы необходимо 1280*1024. Для автоматического исправления нажмите кнопку \"Автоматическая настройка монитора\"");
	$label_error->set_name("error");
	$table->attach_defaults($label_error, 0, 6, 0, 1);	
	$label_error->show;
	} else {
	$box2 = Gtk2::HBox->new (TRUE, 2);
	$table->attach_defaults($box2, 0, 6, 0, 5);
	}

	$button_nm = Gtk2::Button->new("Настройка сети");
	$image_nm  = Gtk2::Image->new_from_file('/usr/share/icons/Faenza/apps/48/preferences-system-network.png');
	$button_nm->set_image ($image_nm);
	$button_nm->set_image_position ('top');
	$button_nm->signal_connect("clicked" => \&callback, "networkmanager");
	$box2->pack_start($button_nm, TRUE, TRUE, 0);
	$image_nm->show;
	$button_nm->show;
	
	$button_modem1 = Gtk2::Button->new("Ручная настройка \nмодема");
	$image_modem1  = Gtk2::Image->new_from_file('/usr/share/icons/Faenza/devices/48/network-wireless.png');
	$button_modem1->set_image ($image_modem1);
	$button_modem1->set_image_position ('top');
	$button_modem1->signal_connect("clicked" => \&callback, "gprsmanual");
	$box2->pack_start($button_modem1, TRUE, TRUE, 0);
	$image_modem1->show;
	$button_modem1->show;

	$button_modem2 = Gtk2::Button->new("Автоматический поиск \nмодема");
	$image_modem2  = Gtk2::Image->new_from_file('/usr/share/icons/Faenza/devices/48/network-wireless.png');
	$button_modem2->set_image ($image_modem2);
	$button_modem2->set_image_position ('top');
	$button_modem2->signal_connect("clicked" => \&callback, "gprsauto");
	$box2->pack_start($button_modem2, TRUE, TRUE, 0);
	$image_modem2->show;
	$button_modem2->show;

	$button_conf = Gtk2::Button->new("Настройка \nтерминального ПО");
	$image_conf  = Gtk2::Image->new_from_file('/usr/share/icons/Faenza/categories/48/package_system.png');
	$button_conf->set_image ($image_conf);
	$button_conf->set_image_position ('top');
	$button_conf->signal_connect("clicked" => \&callback, "configurator");
	$box2->pack_start($button_conf, TRUE, TRUE, 0);
	$image_conf->show;
	$button_conf->show;
	
	if ($width < $minresolution) {
	  $button_res = Gtk2::Button->new("Автоматическая \nнастройка монитора");
	  $image_res  = Gtk2::Image->new_from_file('/usr/share/icons/Faenza/devices/48/computer.png');
	  $button_res->set_image ($image_res);
	  $button_res->set_image_position ('top');
	  $button_res->signal_connect("clicked" => \&callback, "fixresolution");
	  $box2->pack_start($button_res, TRUE, TRUE, 0);
	  $image_res->show;
	  $button_res->show;
	}
	

	$box2->show;
	}
	
	if ($data =~ m/networkmanager/) {
		system("/bin/nm-connection-editor");
	}
	if ($data =~ m/gprsmanual/) {
		system("sudo /home/terminal/atm/bin/g-ppp");
	}
	if ($data =~ m/gprsauto/) {
		system("LC_ALL=ru_RU.UTF-8 xfce4-terminal -e \"sudo /home/terminal/atm/bin/gprsconfig_alternate\" -T \"Настройка модема\" --hide-toolbar --hide-menubar");
	}
	if ($data =~ m/configurator/) {
		system("LC_ALL=ru_RU.UTF-8 ~/atm/configure.sh");
	}
	if ($data =~ m/fixresolution/) {
		system("LC_ALL=ru_RU.UTF-8 sudo /usr/local/bin/setnomodeset");
	}

}

sub progress_timeout {
	$seconds--;
	if ($seconds eq 0) {Gtk2->main_quit;}
        return TRUE;
}

sub on_stop {
	Gtk2->main_quit;
}

Gtk2->init;

$window = Gtk2::Window->new;
$screen = $window->get_screen;
$width = $screen->get_width;
$height = $screen->get_height;
$window->signal_connect("destroy" => \&on_stop);
$window->signal_connect("delete-event" => \&on_stop);
$window->set_default_size(900,200);
$window->set_position (GTK_WIN_POS_CENTER);
$window->set_title("Мастер настройки терминала");
$window->activate_focus;
$window->set_border_width(3);

$table = Gtk2::Table->new(7, 6, TRUE);
$table->set_col_spacings (5);
$table->set_row_spacings (5);
$window->add($table);

$box1 = Gtk2::HBox->new (TRUE, 2);
$table->attach_defaults($box1, 0, 6, 0, 7);

        $seconds = 10000;
        if ($width < $minresolution) {
          $window->resize(900,300);
        } else {
          $window->resize(900,250);
        }
        $window->set_position(GTK_WIN_POS_CENTER);
        $box1->hide;

        $button_exit = Gtk2::Button->new("Выход");
        $image_exit  = Gtk2::Image->new_from_file('/usr/share/icons/Faenza/actions/48/application-exit.png');
        $button_exit->set_image ($image_exit);
        $button_exit->set_image_position ('top');
        $button_exit->signal_connect("clicked" => \&on_stop);
        $table->attach_defaults($button_exit, 0, 6, 5, 7);
        $image_exit->show;
        $button_exit->show;

        if ($width < $minresolution) {
        $box2 = Gtk2::HBox->new (TRUE, 2);
        $table->attach_defaults($box2, 0, 6, 1, 5);
        $label_error = Gtk2::Label->new ("Для оптимальной работы необходимо 1280*1024. Для автоматического исправления нажмите кнопку \"Автоматическая настройка монитора\"");
        $label_error->set_name("error");
        $table->attach_defaults($label_error, 0, 6, 0, 1);
        $label_error->show;
        } else {
        $box2 = Gtk2::HBox->new (TRUE, 2);
        $table->attach_defaults($box2, 0, 6, 0, 5);
        }

        $button_nm = Gtk2::Button->new("Настройка сети");
        $image_nm  = Gtk2::Image->new_from_file('/usr/share/icons/Faenza/apps/48/preferences-system-network.png');
        $button_nm->set_image ($image_nm);
        $button_nm->set_image_position ('top');
        $button_nm->signal_connect("clicked" => \&callback, "networkmanager");
        $box2->pack_start($button_nm, TRUE, TRUE, 0);
        $image_nm->show;
        $button_nm->show;

        $button_modem1 = Gtk2::Button->new("Ручная настройка \nмодема");
        $image_modem1  = Gtk2::Image->new_from_file('/usr/share/icons/Faenza/devices/48/network-wireless.png');
        $button_modem1->set_image ($image_modem1);
        $button_modem1->set_image_position ('top');
        $button_modem1->signal_connect("clicked" => \&callback, "gprsmanual");
        $box2->pack_start($button_modem1, TRUE, TRUE, 0);
        $image_modem1->show;
        $button_modem1->show;

        $button_modem2 = Gtk2::Button->new("Автоматический поиск \nмодема");
        $image_modem2  = Gtk2::Image->new_from_file('/usr/share/icons/Faenza/devices/48/network-wireless.png');
        $button_modem2->set_image ($image_modem2);
        $button_modem2->set_image_position ('top');
        $button_modem2->signal_connect("clicked" => \&callback, "gprsauto");
        $box2->pack_start($button_modem2, TRUE, TRUE, 0);
        $image_modem2->show;
        $button_modem2->show;

        $button_conf = Gtk2::Button->new("Настройка \nтерминального ПО");
        $image_conf  = Gtk2::Image->new_from_file('/usr/share/icons/Faenza/categories/48/package_system.png');
        $button_conf->set_image ($image_conf);
        $button_conf->set_image_position ('top');
        $button_conf->signal_connect("clicked" => \&callback, "configurator");
        $box2->pack_start($button_conf, TRUE, TRUE, 0);
        $image_conf->show;
        $button_conf->show;

        if ($width < $minresolution) {
          $button_res = Gtk2::Button->new("Автоматическая \nнастройка монитора");
          $image_res  = Gtk2::Image->new_from_file('/usr/share/icons/Faenza/devices/48/computer.png');
          $button_res->set_image ($image_res);
          $button_res->set_image_position ('top');
          $button_res->signal_connect("clicked" => \&callback, "fixresolution");
          $box2->pack_start($button_res, TRUE, TRUE, 0);
          $image_res->show;
          $button_res->show;
        }


        $box2->show;

$box1->show;
$table->show;
$window->show;


my $timer = Glib::Timeout->add(1000, \&progress_timeout);

Gtk2->main;
