#!/usr/bin/env perl
use strict;
use warnings;
use threads;
use LWP::Simple;
use Glib qw(TRUE FALSE);
use Gtk2 qw/-init -threads-init/;
my $hebra_down;
my $deb = ( 'firefox_8.0+build1-0ubuntu0.11.10.3_amd64.deb' );
my $firefox = ( "http://security.ubuntu.com/ubuntu/pool/main/f/firefox/$deb" );
die "Glib::Object thread safetly failed"
unless Glib::Object->set_threadsafe (TRUE);
my $win = Gtk2::Window->new;
$win->signal_connect (destroy => sub { Gtk2->main_quit; });
$win->set_title ($0);
$win->set_border_width (10);
$win->set_default_size (400, 200);
$win->set_position('center');
my $hbox = Gtk2::HBox->new (FALSE, 6);
$win->add ($hbox);
my $vbox = Gtk2::VBox->new (FALSE, 6);
$hbox->pack_start ($vbox, FALSE, FALSE, 0);
my $btn_iniciar = Gtk2::Button->new ('Iniciar');
$vbox->pack_start ($btn_iniciar, FALSE, FALSE, 0);
my $btn_parar = Gtk2::Button->new ('Parar');
$vbox->pack_start ($btn_parar, FALSE, FALSE, 0);
$btn_parar->set_sensitive(FALSE);
my $pending = Gtk2::Label->new ('Pulsar para iniciar la descarga');
$vbox->pack_start ($pending, FALSE, FALSE, 0);
$btn_iniciar->signal_connect (clicked => sub {
local $SIG{KILL} = sub { threads->exit };
$btn_iniciar->set_sensitive (FALSE);
$btn_parar->set_sensitive(TRUE);
$pending->set_label('Descargando');
$hebra_down = threads->create(\&procesa_descarga);
});
$btn_parar->signal_connect (clicked => sub {
$hebra_down->kill('KILL')->detach;
$btn_iniciar->set_sensitive (TRUE);
$btn_parar->set_sensitive(FALSE);
$pending->set_label('Descarga cancelada');
});
$win->show_all;
Gtk2->main;
sub procesa_descarga
{
my $ua = LWP::UserAgent->new();
$ua->timeout(10);
my $response = $ua->get( $firefox );
if ($response->is_success)
{
open (FF, ">$deb") or return "Error volcando a disco: $!\n";
print FF $response->decoded_content();
close FF;
$btn_iniciar->set_sensitive(TRUE);
$btn_parar->set_sensitive(FALSE);
if ($response->status_line eq '200 OK')
{
$pending->set_label("Descarga completa\n" . $response->status_line);
return $response->status_line;
}
else
{
$pending->set_label("Se ha producido un error\n" . $response->status_line);
return $response->status_line;
}
}
else {
$pending->set_label("Se ha producido un error\n" . $response->status_line);
return $response->status_line;
}
}