summaryrefslogtreecommitdiffstats
path: root/lib/network/drakvpn.pm
blob: c68127d4300b375ac665e03adde99316113a2f28 (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
package network::drakvpn;

=head1 NAME

network::drakvpn - Interactive VPN configuration

=head1 SYNOPSIS

 use interactive;
 use network::drakvpn;

 my $in = 'interactive'->vnew('su');
 network::drakvpn::create_connection($in);

=cut

use strict;
use common;

use network::vpn;

sub create_connection {
    my ($in) = @_;
    my $vpn_type;
    my $vpn_connection;
    my $new_name;
    require wizards;
    my $wiz = wizards->new({
        defaultimage => "drakvpn",
        name => N("VPN configuration"),
        pages => {
            welcome => {
                if_(!$::isInstall, no_back => 1),
                name => N("Choose the VPN type"),
                data => [ {
                    val => \$vpn_type, type => 'list',
                    list => [ sort { $a->get_description cmp $b->get_description } network::vpn::list_types ],
                    format => sub { $_[0] && $_[0]->get_description },
                    allow_empty_list => 1,
                } ],
                complete => sub {
                    $vpn_type or return 1;
                    my @packages = $vpn_type->get_packages;
                    if (@packages && !$in->do_pkgs->install(@packages)) {
                        $in->ask_warn(N("Error"), N("Could not install the packages (%s)!", join(', ', @packages)));
                        return 1;
                    }
                    if ($vpn_type->can('prepare')) {
                        my $wait = $in->wait_message(N("Please wait"), N("Initializing tools and detecting devices for %s...", $vpn_type->get_type));
                        if (!$vpn_type->prepare) {
                          undef $wait;
                          $in->ask_warn(N("Error"), N("Unable to initialize %s connection type!", $vpn_type->get_type));
                          return 1;
                        }
                    }
                },
                next => "vpn_name",
            },
            vpn_name => {
                name => N("Please select an existing VPN connection or enter a new name."),
                data => sub { [
                    { label => N("VPN connection"), val => \$vpn_connection, type => 'list',
                      list => [ $vpn_type->get_configured_connections, '' ],
                      format => sub { $_[0] ? $_[0]{name} : N("Configure a new connection...") },
                      gtk => { use_boxradio => 1 } },
                    { label => N("New name"), val => \$new_name, disabled => sub { $vpn_connection } },
                ] },
                complete => sub {
                    if (!$vpn_connection && !$new_name) {
                        $in->ask_warn(N("Warning"), N("You must select an existing connection or enter a new name."));
                        1;
                    }
                },
                post => sub {
                    $vpn_connection ||= $vpn_type->new($new_name);
                    $vpn_connection->read_config;
                    $vpn_connection->can('get_key_settings') ? "vpn_key_settings" : "vpn_settings";
                },
            },
            vpn_key_settings => {
                name => N("Please enter the required key(s)"),
                data => sub { $vpn_connection->get_key_settings },
                next => "vpn_settings",
            },
            vpn_settings => {
                name => N("Please enter the settings of your VPN connection"),
                data => sub { $vpn_connection->get_settings },
                post => sub {
                    $vpn_connection->write_config;
                    "connect";
                },
            },
            connect => {
                name => N("Do you want to start the connection now?"),
                type => "yesorno",
                post => sub {
                    my ($connect) = @_;
                    if ($connect) {
                        $vpn_connection->is_started and $vpn_connection->stop;
                        $vpn_connection->start($in) or $in->ask_warn(N("Warning"), N("Connection failed."));
                    }
                    require network::network;
                    network::network::reload_net_applet();
                    "end";
                },
            },
            end => {
                name => N("The VPN connection is now configured.

This VPN connection can be automatically started together with a network connection.
It can be done by reconfiguring the network connection and selecting this VPN connection.
"),
                end => 1,
            },
        },
    });
    $wiz->process($in);
}

1;