summaryrefslogtreecommitdiffstats
path: root/bin/draknetprofile
blob: 904979bc455dc1bfb293cafa811c84dd9099e7d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/perl

# Copyright (C) 2006 Mandriva
#                    Olivier Blin <blino@mandriva.com>
#                    Thierry Vignaud <tvignaud@mandriva.com>
#
# 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;