#!/usr/bin/perl # Copyright (C) 2006 Mandriva # Olivier Blin # Thierry Vignaud # # 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 strict; use lib qw(/usr/lib/libDrakX); # i18n: IMPORTANT: to get correct namespace (drakx-net instead of libDrakX) BEGIN { unshift @::textdomains, 'drakx-net' } use standalone; use common; use network::network; use mygtk3; use Gtk3::SimpleList; use ugtk3 qw(:create :helpers :wrappers :dialogs :ask); use POSIX (); require_root_capability(); $ugtk3::wm_icon = 'draknetprofile-16'; my $title = N("Network profiles"); my $w = ugtk3->new($title); $::main_window = $w->{real_window}; #- so that transient_for is defined for wait messages and dialogs my $net = {}; my @profiles; my $default_profile = "default"; my %buttons; package Gtk3::CellRendererRadio; sub new { my $renderer = Gtk3::CellRendererToggle->new; $renderer->set_radio(1); $renderer; } 1; package main; Gtk3::SimpleList->add_column_type( 'radio', type => 'Glib::Boolean', renderer => 'Gtk3::CellRendererRadio', attr => 'active', ); my $modules_list = Gtk3::SimpleList->new( N("Module") => 'hidden', N("Enabled") => 'bool', N("Description") => 'text', ); my @r = $modules_list->get_column(0)->get_cells; $r[0]->signal_connect('toggled' => sub { my ($_renderer, $row, $_col) = @_; if ($modules_list->{data}[$row][1]) { network::network::netprofile_module_enable($modules_list->{data}[$row][0]); } else { network::network::netprofile_module_disable($modules_list->{data}[$row][0]); } }); my $profiles_list = Gtk3::SimpleList->new( "" => 'hidden', N("Profile") => 'text', ); $profiles_list->get_selection->signal_connect('changed' => sub { if ($profiles_list->get_sensitive) { my ($index) = $profiles_list->get_selected_indices; $_->set_sensitive(defined $index) foreach values %buttons; } }); sub load_netprofile_modules() { my @modules = network::network::netprofile_modules(); my %descriptions = map { $_ => translate($_) } N_("Save and restore the active services"), N_("Network connection settings"), N_("Firewall settings"), N_("Firewall settings (IPv6)"), N_("Proxy settings"), N_("Urpmi settings"), N_("Networkmanager connection settings"); foreach (@modules) { push @{$modules_list->{data}}, [ $_->{module}, $_->{enabled}, $descriptions{$_->{description}} || $_->{description}, ]; } } sub get_selected_profile() { my ($index) = $profiles_list->get_selected_indices; if (!$index) { my $pc = @{$profiles_list->{data}}; # FIXME: the following does nothing my $index = 0; for (my $i = 0; $i < $pc; $i++) { if ($profiles_list->{data}[$i][0] == 1) { $index = $i; } } } defined $index && $profiles_list->{data}[$index][1]; } sub update_profiles() { network::network::netprofile_read($net); @profiles = network::network::netprofile_list(); @{$profiles_list->{data}} = map { [ $_ eq $net->{PROFILE}, $_ ] } @profiles; my $index = eval { find_index { $_ eq $net->{PROFILE} } @profiles }; $profiles_list->select($index) if defined $index; } sub set_selected_profile() { set_profile(get_selected_profile()); } sub set_profile { my ($profile) = @_; gtkset_mousecursor_wait($w->{window}->get_window); $profiles_list->set_sensitive(0); $_->set_sensitive(0) foreach values %buttons; gtkflush(); unless (fork()) { network::network::netprofile_set($net, $profile); POSIX::_exit(0); } $SIG{CHLD} = sub { $SIG{CHLD} = 'IGNORE'; gtkset_mousecursor_normal($w->{window}->get_window); update_profiles(); $_->set_sensitive(1) foreach values %buttons; $profiles_list->set_sensitive(1); }; } sub clone_profile() { #my $source_profile = get_selected_profile(); my $dialog = _create_dialog(N("New profile...")); my $entry_dialog = Gtk3::Entry->new; gtkpack($dialog->get_child, Gtk3::WrappedLabel->new(N("Please specify the name of the new network profile to be created (e.g., work, home, roaming, ..). This new profile will be created based on current settings, and you'll be able to configure your system configuration as usual afterwards.")), $entry_dialog, ); gtkpack($dialog->get_action_area, gtksignal_connect(Gtk3::Button->new(N("Cancel")), clicked => sub { $dialog->destroy }), gtksignal_connect(my $bok = Gtk3::Button->new(N("Ok")), clicked => sub { my $dest_profile = $entry_dialog->get_text; # netprofile does not like spaces in profile names... $dest_profile =~ s/ /_/g; if (member($dest_profile, @profiles)) { err_dialog(N("Error"), N("The \"%s\" profile already exists!", $dest_profile), { transient => $dialog }); return 1; } $dialog->destroy; network::network::netprofile_set($net, $dest_profile); update_profiles(); info_dialog(N("New profile created"), N("You are now using network profile %s. You can configure your system as usual, and all your network settings from now on will be saved into this profile.", $dest_profile)); }), ); $bok->set_can_default(1); $bok->grab_default; $dialog->show_all; } sub delete_selected_profile() { my $profile = get_selected_profile(); if ($profile eq $default_profile) { my $ret = warn_dialog(N("Warning"), N("Are you sure you want to delete the default profile?")); return if !$ret; } elsif ($profile eq $net->{PROFILE}) { err_dialog(N("Error"), N("You can not delete the current profile. Please switch to a different profile first.")); return; } network::network::netprofile_delete($profile); update_profiles(); } # create advanced view to configure modules my $expander = Gtk3::Expander->new(N("Advanced")); $expander->add(gtkpack_(Gtk3::VBox->new, 0, gtkpack_(gtkset_border_width(Gtk3::HBox->new, 1), 1, gtkpack_(gtkset_border_width(Gtk3::VBox->new, 0), 0, gtknew('WrappedLabel', text => N("Select the netprofile modules:")), 0, gtknew('ScrolledWindow', width => 300, height => 150, child => $modules_list), ), ), ), ); $expander->show_all; #$expander->signal_connect(activate => sub { $w->shrink_topwindow; }); gtkadd($w->{window}, gtknew('VBox', spacing => 5, children => [ $::isEmbedded ? () : (0, Gtk3::Banner->new('draknetprofile', $title)), 0, gtknew('WrappedLabel', text => N("This tool allows you to control network profiles.")), 0, gtknew('WrappedLabel', text => N("Select a network profile:")), 1, gtknew('ScrolledWindow', width => 300, height => 150, child => $profiles_list), 0, $expander, 0, gtknew('HButtonBox', children_loose => [ $buttons{activate} = gtknew('Button', text => N("Activate"), clicked => \&set_selected_profile, sensitive => 0), $buttons{clone} = gtknew('Button', text => N("New"), clicked => \&clone_profile, sensitive => 0), $buttons{delete} = gtknew('Button', text => N("Delete"), clicked => \&delete_selected_profile, sensitive => 0), gtknew('Button', text => N("Quit"), clicked => sub { Gtk3->main_quit }), ]), ]), ); load_netprofile_modules(); update_profiles(); $w->main;