summaryrefslogtreecommitdiffstats
path: root/lib/network/netcenter.pm
blob: 14df14f1243fa260fc4a90c3b51dbd5fa0fbf628 (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
#!/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 gtkset_image {
    my ($w, $file, $o_size) = @_;
    my $image = $o_size
    ?  Gtk2::Image->new_from_pixbuf(gtkcreate_pixbuf($file)->scale_simple($o_size, $o_size, 'hyper'))
    :  gtknew('Image', file => $file);
    $w->set_image($image);
    $w;
}

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 = reverse(uniq_ { $_->{device} } reverse(@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, child => gtknew('VBox', spacing => 20, children_tight => [
               map {
                   my $cmanager = build_cmanager($in, $net, $w, $pixbufs, $_);
                   gtknew('HBox', children_tight => [
                       gtknew('Image', file => $_->get_type_icon),
                       gtknew('VBox', spacing => 10, children_tight => [
                           gtknew('Title2', label => $_->get_description),
                           gtknew('HBox', children_tight => [
                               gtknew('Label', padding => [ 5, 0 ]),
                               gtknew('VBox', children_tight => [
                                   if_($cmanager->{gui}{show_networks}, $cmanager->{gui}{networks_list}),
                                   gtknew('HBox', children_tight => [
                                       gtknew('VBox', children_tight => [
                                           gtknew('HButtonBox', children_tight => [
                                               gtkset_image(gtknew('Button'), 'connected'),
                                               gtkset_image(gtknew('Button'), 'disconnected'),
                                               gtkset_image(gtknew('Button'), 'monitor-24'),
                                               gtkset_image(gtknew('Button'), 'configure-24'),
                                               ($cmanager->{gui}{show_networks} ? (0, gtkset_image(gtknew('Button'), 'refresh')) : ()),
                                           ]),
                                       ]),
                                   ]),
                               ]),
                           ]),
                       ]),
                   ]);
               } @connections,
           ])),
       ]),
    );

    $w->main;
}

1;