#!/usr/bin/perl

# Netconfig

# Copyright (C) 2003 Damien Krotkine (dams@tuxfamily.org)
#
# 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.

# DrakX includes
use lib qw(/usr/lib/libDrakX);
use interactive;
use common;
use ugtk2 qw(:helpers :wrappers :create);

# we include the Network Systemconf module
use Libconf::System::Network;
use Libconf::GUI::Gtk2 qw(:generators);

# for debugging purpose
use Data::Dumper;

# we ask systemconf to give us the structure representing the global network config
my $sys_network = new Libconf::System::Network();

# we dump the content for debugging purpose
print Data::Dumper->Dump([$sys_network], ['sys_network']) . "\n";

# we build the GUI

# a window
my $window = Gtk2::Window->new('toplevel');
$window->signal_connect(delete_event => sub { ugtk2->exit(0) });
$window->set_position('center');
$window->set_title(N("netconfig - Network configuration"));

# in the window, we add the widget generated by Libconf::GUI::Gtk2::generateWidget(),
# an OK button that will save the modification in the network config files,
# a Cancel button that will leave without saving.
gtkadd($window,
       gtkpack_(Gtk2::VBox->new(0,10),
                1, gtkshow(generateWidget($sys_network)),
                0, gtkadd(gtkset_layout(Gtk2::HButtonBox->new, 'end'),
                          gtksignal_connect(new Gtk2::Button(N("OK")),
                                            clicked => sub { save_exit() }),
                          gtksignal_connect(new Gtk2::Button(N("Cancel")),
                                            clicked => sub { cancel_exit() })
                         )
               ),
      );

# we make everything visible in the window
$window->show_all;

#and we start the Gtk2 main loop.
Gtk2->main;

# After the user has clicked on one button, we dump the content of the sys_network structure for debugging purpose
print Data::Dumper->Dump([$sys_network], ['sys_network']) . "\n";

#and we exit the program.
ugtk2->exit(0);

# the function called when the user clicks on the OK button
sub save_exit() {

    # we ask the $sys_network object to write itself back to the real config file
    # he knows everything about himself, so no need to give him any arg.
    # the file is rewritten, without loosing any information or comments.
    $sys_network->writeConf;

    # we exit the gtk2 main loop
    Gtk2->main_quit;
}

# the function called when the user clicks on the Cancel button
sub cancel_exit() {

    # we exit the gtk2 main loop
    Gtk2->main_quit;
}
