summaryrefslogtreecommitdiffstats
path: root/bin/draknetprofile
blob: 49ca28711fe8aba84d45f99c8a8a335fb517c11e (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
#!/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 mygtk2;
use Gtk2::SimpleList;
use ugtk2 qw(:create :helpers :wrappers :dialogs);

$ugtk2::wm_icon = 'draknetprofile-16';
my $title = N("Network profiles");
my $w = ugtk2->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 Gtk2::CellRendererRadio;

sub new {
    my $renderer = Gtk2::CellRendererToggle->new;
    $renderer->set_radio(1);
    $renderer->set_property('mode', 'inert');
    $renderer;
}

1;

package main;

Gtk2::SimpleList->add_column_type(
    'radio',
    type     => 'Glib::Boolean',
    renderer => 'Gtk2::CellRendererRadio',
    attr     => 'active',
);

my $profiles_list = Gtk2::SimpleList->new(
    "" => 'radio',
    N("Profile") => 'text',
);
$profiles_list->get_selection->signal_connect('changed' => sub {
                                                  my ($index) = $profiles_list->get_selected_indices;
                                                  $_->set_sensitive(defined $index) foreach values %buttons;
                                              });

sub get_selected_profile() {
    my ($index) = $profiles_list->get_selected_indices;
    defined $index && $profiles_list->{data}[$index][1];
}

sub update_profiles() {
    my $previous_profile = get_selected_profile();
    network::network::netprofile_read($net);
    @profiles = network::network::netprofile_list();
    @{$profiles_list->{data}} = map { [ $_ eq $net->{PROFILE}, $_  ] } @profiles;
    my $index = eval { find_index { $_ eq $previous_profile } @profiles };
    $profiles_list->select($index) if defined $index;
}

sub set_selected_profile() {
    my $profile = get_selected_profile();
    gtkset_mousecursor_wait($w->{window}->window);
    gtkflush();
    network::network::netprofile_set($net, $profile);
    gtkset_mousecursor_normal($w->{window}->window);
    update_profiles();
}

sub clone_profile() {
    my $source_profile = get_selected_profile();
    my $dialog = _create_dialog(N("New profile..."));
    my $entry_dialog = Gtk2::Entry->new;
    gtkpack($dialog->vbox,
            Gtk2::WrappedLabel->new(N("Name of the profile to create (the new profile is created as a copy of the current one):")),
            $entry_dialog,
        );
    gtkpack($dialog->action_area,
            gtksignal_connect(Gtk2::Button->new(N("Cancel")),
                              clicked => sub { $dialog->destroy }),
            gtksignal_connect(my $bok = Gtk2::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_clone($source_profile, $dest_profile);
                                  update_profiles();
                              }),
        );
    $bok->can_default(1);
    $bok->grab_default;
    $dialog->show_all;
}

sub delete_selected_profile() {
    my $profile = get_selected_profile();
    if ($profile eq $default_profile) {
        warn_dialog(N("Warning"), N("You can not delete the default profile"));
    } elsif ($profile eq $net->{PROFILE}) {
        warn_dialog(N("Warning"), N("You can not delete the current profile"));
    } else {
        network::network::netprofile_delete($profile);
        update_profiles();
    }
}

gtkadd($w->{window},
       gtknew('VBox', spacing => 5, children => [
           $::isEmbedded ? () : (0, Gtk2::Banner->new('draknetprofile', $title)),
           0, gtknew('WrappedLabel', text => N("This tool allows to activate an existing network profile, and to manage (clone, delete) profiles.") . "\n\n" . N("To modify a profile, you have to activate it first.")),
           1, gtknew('ScrolledWindow', width => 300, height => 150, child => $profiles_list),
           0, gtknew('HButtonBox', children_loose => [
               $buttons{activate} = gtknew('Button', text => N("Activate"), clicked => \&set_selected_profile),
               $buttons{clone} = gtknew('Button', text => N("Clone"), clicked => \&clone_profile),
               $buttons{delete} = gtknew('Button', text => N("Delete"), clicked => \&delete_selected_profile),
               gtknew('Button', text => N("Quit"), clicked => sub { Gtk2->main_quit }),
           ]),
       ]),
   );

update_profiles();
$w->main;