summaryrefslogtreecommitdiffstats
path: root/lib/network/netcenter.pm
blob: 521ee288a50d075f6bb4c7d0fa22d86f0e38c59f (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
#!/usr/bin/perl
# Olivier Blin, 2007 <oblin@mandriva.com>
# Licensed under the GPL

package network::netcenter;

use strict;
use common;
use mygtk2;
use ugtk2 qw(:create :helpers :wrappers);
use network::connection;
use network::connection_manager;

sub filter_networks {
    my ($connection) = @_;
    $_->{configured} = $connection->network_is_configured($_) foreach values %{$connection->{networks}};
    my @networks = sort {
        $b->{configured} <=> $a->{configured} || $b->{signal_strength} <=> $a->{signal_strength} || $a->{name} cmp $b->{name};
    } values %{$connection->{networks}};
    splice @networks, 0, 3;
}

sub build_cmanager {
    my ($in, $net, $w, $pixbufs, $connection) = @_;

    my $cmanager = network::connection_manager::create($in, $net, $w, $pixbufs);
    $cmanager->{connection} = $connection;
    $cmanager->{gui}{show_networks} = $connection->can('get_networks') && !$connection->network_scan_is_slow;
    if ($cmanager->{gui}{show_networks}) {
        network::connection_manager::create_networks_list($cmanager);
        $cmanager->{filter_networks} = sub { filter_networks($connection) };
        network::connection_manager::update_networks($cmanager);
    }
    $cmanager;
}

sub main {
    my ($in, $net) = @_;

    my $title = N("Network Center");
    my $icon = '/usr/share/mcc/themes/default/drakroam-mdk.png';

    $ugtk2::wm_icon = $icon;
    my $w = ugtk2->new($title);
    #- so that transient_for is defined, for wait messages and popups to be centered
    $::main_window = $w->{real_window};

    my @connections = map { $_->get_connections(automatic_only => 1) } network::connection::get_types;
    @connections = uniq_ { $_->{device} } @connections;

    my $pixbufs = network::connection_manager::create_pixbufs();

    gtkadd($w->{window},
       gtknew('VBox', spacing => 5, children => [
           $::isEmbedded ? () : (0, Gtk2::Banner->new($icon, $title)),
           1, gtknew('ScrolledWindow', width => 500, height => 300, shadow_type => 'none',
                     child => gtknew('VBox', spacing => 5, children_tight => [
               map_index {
                   my $cmanager = build_cmanager($in, $net, $w, $pixbufs, $_);
                   my $box = gtknew('VBox', spacing => 5, children_tight => [
                           gtknew('HBox', children => [
                               0, gtknew('Image', file => $_->get_type_icon),
                               0, gtknew('Label', padding => [ 5, 0 ]),
                               1, gtknew('Label', ellipsize => 'end', alignment => [ 0, 0 ], text_markup => '<b>' . $_->get_description . '</b>'),
                               0, gtknew('Label', padding => [ 2, 0 ]),
                               0, $cmanager->{gui}{labels}{interface} = gtknew('Label', alignment => [ 0, 0 ], text_markup => '<b>' . $_->get_interface . '</b>'),
                           ]),
                           gtknew('HBox', children => [
                               0, gtknew('Label', padding => [ 5, 0 ]),
                               1, gtknew('VBox', spacing => 5, children_tight => [
                                   if_($cmanager->{gui}{show_networks},
                                       gtknew('Label', text => N("Please select your network:"), alignment => [ 0, 0 ]),
                                       gtknew('Frame', shadow_type => 'in', child => $cmanager->{gui}{networks_list})),
                                   gtknew('HBox', children => [
                                       1, gtknew('HButtonBox', spacing => 6, layout => 'start', children_loose => [
                                               ($cmanager->{gui}{show_networks} ?
                                                  $cmanager->{gui}{buttons}{refresh} =
                                                    gtknew('Button', text => N("Refresh"),
                                                           image => gtknew('Image', file => 'refresh', size => 16),
                                                           clicked => sub { network::connection_manager::update_networks($cmanager) })
                                                      : ()),
                                               $cmanager->{gui}{buttons}{monitor} =
                                                 gtknew('Button', text => N("Monitor"),
                                                        image => gtknew('Image', file => 'monitor-16'),
                                                        clicked => sub { network::connection_manager::monitor_connection($cmanager) }),
                                               $cmanager->{gui}{buttons}{configure} = 
                                                 gtknew('Button', text => N("Configure"),
                                                        image => gtknew('Image', file => 'configure-16'),
                                                        clicked => sub { network::connection_manager::configure_connection($cmanager) }),
                                           ]),
                                       0, $cmanager->{gui}{buttons}{connect_toggle} =
                                                 gtknew('Button',
                                                        image => gtknew('Image', file => 'activate-16'),
                                                        clicked => sub { network::connection_manager::start_connection($cmanager) }),
                                           ]),
                               ]),
                           ]),
                   ]);
                   network::connection_manager::update_on_status_change($cmanager);
                   ($::i > 0 ? Gtk2::HSeparator->new : ()), $box;
               } @connections,
           ])),
       ]),
    );

    my $base_color = $w->{window}->get_style->base('normal')->to_string;
    Gtk2::Rc->parse_string(<<END);
style "netcenter_bg" {
  bg[NORMAL] = "$base_color"
}
class "*Container*" style "netcenter_bg"

END

    $w->main;
}

1;