#!/usr/bin/perl

# Copyright (C) 2004 Robert Vojta <robert.vojta@mandrake.cz> 
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# 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 lib qw(/usr/share/drakxtools-ng/lib);
use preload;
use comm;
use dialogs;

use Gtk2 -init;
use Gtk2::GladeXML;
use Libconf::System::Network::Proxy;

use Locale::gettext;
use POSIX;

setuplocales();

comm::require_root;

#
# Glade XML file informations
#
my $xmlfile = "$::gtkguidir/drakproxy.glade";

my $gladexml = Gtk2::GladeXML->new($xmlfile, 'drakproxyw');
my $mainw = $gladexml->get_widget('drakproxyw');

#
# Main widgets
#
my $http_proxy_entry = $gladexml->get_widget('http_proxy_entry');
my $ftp_proxy_entry = $gladexml->get_widget('ftp_proxy_entry');

#
# Libconf stuff
#
my $sys_proxy = new Libconf::System::Network::Proxy();
$ftp_proxy_entry->set_text($sys_proxy->{ftp});
$http_proxy_entry->set_text($sys_proxy->{http});

#
# Connect signals and run
#
$gladexml->signal_autoconnect_from_package('main');
Gtk2->main;
exit;

sub gtk_main_quit {
    Gtk2->main_quit;
}

sub on_cancel_button_clicked {
    Gtk2->main_quit;
}

sub check_user_entries {
    my $proxy_warn = "";

    my $http_proxy = $http_proxy_entry->get_text;
    if (length($http_proxy) > 0 && $http_proxy !~ /^http:\/\//) {
	$proxy_warn = T("HTTP proxy should start with http://");
    }

    my $ftp_proxy = $ftp_proxy_entry->get_text;
    if (length($ftp_proxy) > 0 && $ftp_proxy !~ /^(ftp|http):\/\//) {
	$proxy_warn .= "\n" if length($proxy_warn) > 0;
	$proxy_warn .= T("FTP proxy should start with http:// or ftp://");
    }

    if (length($proxy_warn) > 0) {
	dialogs::warning_dialog($mainw, undef, T("Incorrect proxy settings"), $proxy_warn);
	return 0;
    }

    1;
}

sub apply_changes {
    $sys_proxy->{http} = $http_proxy_entry->get_text;
    $sys_proxy->{ftp} = $ftp_proxy_entry->get_text;
    $sys_proxy->writeConf;

    1;
}

sub on_ok_button_clicked {
    return if !check_user_entries;
    return if !apply_changes;

    Gtk2->main_quit;
}
