summaryrefslogtreecommitdiffstats
path: root/bin/drakconnect
blob: 2d2aeda9a353654be5b19cefb89ab016e7f249e1 (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
#!/usr/bin/perl

# DrakConnect $Id$

# Copyright (C) 1999-2006 Mandriva
#                         Damien "Dam's" Krotkine
#                         Damien "poulpy" Chaumette
#                         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;     #- warning, standalone must be loaded very first, for 'explanations'

use interactive;
use common;
use run_program;
use network::netconnect;
use network::connection::ethernet;
use network::tools;
use network::modem;
use network::network;
use detect_devices;
use c;
use modules;
use network::adsl;
use network::connection::isdn;
use network::test;
use POSIX ":sys_wait_h";

$ugtk2::wm_icon = "drakconnect";
my $in = 'interactive'->vnew('su');
if ($in->isa('interactive::gtk')) {
    require mygtk2;
    require ugtk2;
    ugtk2->import(qw(:create :dialogs :helpers :wrappers));
    mygtk2->import(qw(gtknew));
}

my $net = {};
network::network::read_net_conf($net);
my $modules_conf = modules::any_conf->read;

$::Wizard_title = N("Network & Internet Configuration");
$::Wizard_pix_up = "drakconnect";

local $_ = join '', @ARGV;
/--skip-wizard/ and manage();
/--add/ and add_intf();
/--del/ and del_intf();
if (/--install/) {
    $::isInstall = 1;
    add_intf();
}
/--internet/ and configure_net();

# default is to run wizard
add_intf();

my @all_cards = network::connection::ethernet::get_eth_cards($modules_conf);
my %names = network::connection::ethernet::get_eth_cards_names(@all_cards);
my $int_state;
my $int_label = Gtk2::WrappedLabel->new($net->{type} eq 'lan' ? N("Gateway:") : N("Interface:"));
my $int_name = Gtk2::Label->new($net->{type} eq 'lan' ? $net->{network}{GATEWAY} : $net->{net_interface});
my $isconnected = -1;

sub manage() {
    my $p = {};
    my ($interface_menu, $selected, $apply_button);
    my $window = ugtk2->new('Manage Connection');
    unless ($::isEmbedded) {
        $window->{rwindow}->set_position('center');
        $window->{rwindow}->set_title(N("Manage connections")); # translation availlable in mcc domain => we need merging
    }

    my $notebook = Gtk2::Notebook->new;
    $notebook->set_property('show-tabs', 0);
    $notebook->set_property('show-border', 0);

    foreach (keys %names) {
	my $dev = detect_devices::is_lan_interface($_) ? $names{$_} : $_;
	$p->{$dev} = {
                      name => $_ ,
                      intf => $net->{ifcfg}{$_}
                     };
    }
    while (my ($device, $interface) = each %{$net->{ifcfg}}) {
        exists $names{$device} and next;
        my $type = network::tools::get_interface_type($interface);
        $p->{"$type ($device)"} = {
                                  name => $device,
                                  intf => $interface
                                 };
    }

    $window->{rwindow}->add(gtkpack_(Gtk2::VBox->new,
				     0, gtkpack__(Gtk2::HBox->new,
                                                  gtknew('Label', text => N("Device: "), alignment => [ 0, 0 ]),
                                                  $interface_menu = gtksignal_connect(Gtk2::ComboBox->new_text,
                                                                    changed => sub {
                                                                        $selected = $interface_menu->get_text;
                                                                        $notebook->set_current_page($p->{$selected}{gui}{index});
                                                                    },
                                                                                     ),
                                                 ),
				     1, $notebook,
				     0, create_okcancel(my $oc =
                                                        {
                                                         cancel_clicked => sub { $window->destroy; Gtk2->main_quit },
                                                         ok_clicked => sub {
                                                             if ($apply_button->get_property('sensitive')) {
                                                                 save($p, $apply_button);
                                                             }
                                                             $window->destroy;
                                                             Gtk2->main_quit;
                                                         },
                                                        },
                                                        undef, undef, '',
                                                        [ N("Help"), sub { run_program::raw({ detach => 1 }, 'drakhelp', '--id', 'internet-connection') } ],
                                                        [ N("Apply"), sub { save($p, $apply_button) }, 0, 1 ],
                                                       ),
                                    ),
                           );
    $apply_button = $oc->{buttons}{N("Apply")};

    each_index {
	my ($name, $interface) = ($_, $p->{$_}{name});
	$p->{$name}{gui}{index} = $::i;
	$p->{$name}{intf} ||= { DEVICE => $interface };
	build_tree($p->{$name}{intf}, $name);
	build_notebook($p->{$name}{intf}, $p->{$name}{gui}, $apply_button, $name, $interface);
	$notebook->append_page(gtkpack(Gtk2::VBox->new(0,0), $p->{$name}{gui}{notebook}));
    } (sort keys %$p);

    $interface_menu->set_popdown_strings(sort keys %$p);
    $interface_menu->set_active(0);
    $apply_button->set_sensitive(0);

    $window->{rwindow}->show_all;
    $window->main;
    ugtk2->exit(0);
}

sub build_tree {
    my ($intf, $interface) = @_;

    if ($interface eq 'adsl') {
	$intf->{pages} = { 'TCP/IP' => 1, 'DHCP' => 1, 'Account' => 1, 'Options' => 1, 'Information' => 1 };
	network::adsl::adsl_probe_info($net);
	$intf->{save} = sub {
            $net->{type} = 'adsl';
            network::adsl::adsl_conf_backend($in, $net);
          };
    }
    elsif ($interface eq 'modem') {
	$intf->{pages} = { 'TCP/IP' => 1, 'Account' => 1, 'Modem' => 1, 'Options' => 1 };
	put_in_hash($intf, network::modem::ppp_read_conf());
	$intf->{save} = sub { network::modem::ppp_configure($net, $in, $intf) };
    }
    elsif ($interface eq 'isdn') {
	$intf->{pages} = { 'TCP/IP' => 1, 'Account' => 1, 'Modem' => 1, 'Options' => 1 };
	network::connection::isdn::read_config($intf);
	$intf->{save} = sub { network::connection::isdn::apply_config($in, $intf) };
    }
    else {
	#- ethernet is default
	$intf->{pages} = { 'TCP/IP' => 1, 'DHCP' => 1, if_(network::tools::get_interface_type($intf) eq "wifi", 'Wireless' => 1), 'Options' => 1, 'Information' => 1 };
    }
}

sub build_notebook {
    my ($intf, $gui, $apply_button, $interface, $interface_name) = @_;

    my $apply = sub { $apply_button->set_sensitive(1) };
    my $is_ethernet = detect_devices::is_lan_interface($interface);

	my $size_group = Gtk2::SizeGroup->new('horizontal');

    if ($intf->{pages}{'TCP/IP'}) {
	gtkpack__($gui->{sheet}{'TCP/IP'} = gtkset_border_width(Gtk2::VBox->new(0,10), 5),
             gtknew('Title2', label => N("IP configuration")),
                                if_($is_ethernet,
                                    gtkpack(Gtk2::HBox->new(1,0),
                                                 gtknew('Label_Left', text => N("Protocol")),
                                                 $gui->{intf}{BOOTPROTO} = gtksignal_connect(Gtk2::ComboBox->new_text, changed => sub {
                                                     return if !$_[0]->realized;
                                                     my $proto = $gui->{intf}{BOOTPROTO};
                                                     my $protocol = $intf->{BOOTPROTO} = { reverse %{$proto->{protocols}} }->{$proto->get_text};

                                                     foreach ($gui->{intf}{IPADDR}, $gui->{intf}{NETMASK}, $gui->{network}{GATEWAY}) {
                                                         $_->set_sensitive(to_bool($protocol eq "static"));
                                                     }
                                                     $gui->{sheet}{DHCP}->set_sensitive($intf->{BOOTPROTO} eq 'dhcp');
                                                     $apply->();
                                                 },
                                                                                         ),
                                             ),
                                ),
                                gtkpack(Gtk2::HBox->new(1,0),
                                           gtknew('Label_Left', text => N("IP address")),
                                           gtksignal_connect($gui->{intf}{IPADDR} = Gtk2::Entry->new,
                                                                                        key_press_event => $apply),
                                       ),
                                gtkpack(Gtk2::HBox->new(1,0),
                                           gtknew('Label_Left', text => N("Netmask")),
                                           gtksignal_connect($gui->{intf}{NETMASK} = Gtk2::Entry->new,
                                                                                        key_press_event => $apply),
                                       ),
                                if_($is_ethernet,
                                    gtkpack(Gtk2::HBox->new(1,0),
                                               gtknew('Label_Left', text => N("Gateway")),
                                               gtksignal_connect($gui->{network}{GATEWAY} = Gtk2::Entry->new,
                                                                                            key_press_event => $apply),
                                           ),
                                ),
                         gtknew('Title2', label => N("DNS servers")),
               gtknew('Label_Left', text => join(', ', grep { $_ } $intf->{dns1} || $net->{resolv}{dnsServer},
                                                 $intf->{dns2} || $net->{resolv}{dnsServer2},
                                                 $intf->{dns3} || $net->{resolv}{dnsServer3}),
                  ),
                                gtkpack(Gtk2::HBox->new(1,0),
                                             gtknew('Label_Left', text => N("Search Domain")),
                                                    my $w2 = gtknew('Label_Left', text => $intf->{domain} || $net->{resolv}{DOMAINNAME} || 'none'),
                        ),
               );
     $size_group->add_widget($_) foreach $w2, $gui->{intf}{BOOTPROTO}, $gui->{intf}{IPADDR}, $gui->{intf}{NETMASK}, $gui->{network}{GATEWAY};

	if ($is_ethernet) {
            my $proto = $gui->{intf}{BOOTPROTO};
            $proto->{protocols} = { none => N("none"), static => N("static"), dhcp => N("DHCP") };
            $proto->set_popdown_strings(values %{$proto->{protocols}});
            $proto->set_text($proto->{protocols}{$intf->{BOOTPROTO} || 'none'});
            if ($intf->{BOOTPROTO} ne 'static') {
                $_->set_sensitive(0) foreach $gui->{intf}{IPADDR}, $gui->{intf}{NETMASK}, $gui->{network}{GATEWAY};
            }
	} else {
	    $_->set_sensitive(0) foreach $gui->{intf}{IPADDR}, $gui->{intf}{NETMASK}, $gui->{network}{GATEWAY};
	    delete $gui->{intf}{BOOTPROTO};
	}
	!$intf->{IPADDR} and ($intf->{IPADDR}, $gui->{active}, $intf->{NETMASK}) = get_intf_ip($interface_name);
	$gui->{network}{$_}->set_text($net->{network}{$_}) foreach keys %{$gui->{network}};
    }

    if ($intf->{pages}{DHCP}) {
	gtkpack(gtkset_border_width($gui->{sheet}{DHCP} = Gtk2::HBox->new(0,10), 5),
                gtkpack__(gtkset_border_width(Gtk2::VBox->new(0,10), 5),
                          gtkpack__(Gtk2::HBox->new(1,0),
                                  gtknew('Label_Left', text => N("DHCP client")),
                                  gtksignal_connect($gui->{intf}{DHCP_CLIENT} = Gtk2::ComboBox->new_with_strings(\@network::connection::ethernet::dhcp_clients,
                                                                                                                 $intf->{DHCP_CLIENT} || $network::connection::ethernet::dhcp_clients[0]),
                                                    changed => $apply)),
                          gtksignal_connect($gui->{intf_bool}{NEEDHOSTNAME} = Gtk2::CheckButton->new(N("Assign host name from DHCP address")), toggled => $apply),
                          gtkpack__(Gtk2::HBox->new(1,0),
                                    gtknew('Label_Left', text => N("DHCP host name")),
                                    gtksignal_connect($gui->{intf}{DHCP_HOSTNAME} = Gtk2::Entry->new,
                                                      key_press_event => $apply)),
                          gtkpack__(Gtk2::HBox->new(1,0),
                                    gtknew('Label_Left', text => N("DHCP timeout (in seconds)")),
                                    gtksignal_connect($gui->{intf}{DHCP_TIMEOUT} = Gtk2::Entry->new,
                                                      key_press_event => $apply)),
                          gtksignal_connect($gui->{intf_bool}{PEERDNS} = Gtk2::CheckButton->new(N("Get DNS servers from DHCP")), toggled => $apply),
                          gtksignal_connect($gui->{intf_bool}{PEERYP} = Gtk2::CheckButton->new(N("Get YP servers from DHCP")), toggled => $apply),
                          gtksignal_connect($gui->{intf_bool}{PEERNTPD} = Gtk2::CheckButton->new(N("Get NTPD servers from DHCP")), toggled => $apply),
                      ),
            );
	foreach (qw(NEEDHOSTNAME PEERDNS)) { #- default these settings to yes
	    defined $intf->{$_} or $intf->{$_} = "yes";
	}
	$gui->{intf}{$_}->set_text($intf->{$_}) foreach qw(DHCP_HOSTNAME DHCP_TIMEOUT);
	$gui->{intf_bool}{$_}->set_active(text2bool($intf->{$_})) foreach qw(NEEDHOSTNAME PEERDNS PEERYP PEERNTPD);
	$gui->{intf}{DHCP_CLIENT}->set_text($intf->{DHCP_CLIENT});
	$gui->{sheet}{DHCP}->set_sensitive($intf->{BOOTPROTO} eq 'dhcp');
    }
    my $size_group2 = Gtk2::SizeGroup->new('horizontal');
    $size_group2->add_widget($_) foreach $gui->{intf}{DHCP_HOSTNAME}, $gui->{intf}{DHCP_TIMEOUT}, $gui->{intf}{DHCP_CLIENT};

    if ($intf->{pages}{Wireless}) {
	gtkpack(gtkset_border_width($gui->{sheet}{Wireless} = Gtk2::HBox->new(0,10), 5),
		gtkpack_(Gtk2::VBox->new(0,0),
			 map { (0, gtkpack_(Gtk2::VBox->new(0,0),
					    1, Gtk2::Label->new($_->[0]),
					    0, gtksignal_connect($gui->{intf}{$_->[1]} = Gtk2::Entry->new,
								 key_press_event => $apply),
					   ));
			   } ([ N("Operating Mode"), "WIRELESS_MODE" ],
			      [ N("Network name (ESSID)"), "WIRELESS_ESSID" ],
			      [ N("Network ID"), "WIRELESS_NWID" ],
			      [ N("Operating frequency"), "WIRELESS_FREQ" ],
			      [ N("Sensitivity threshold"), "WIRELESS_SENS" ],
			      [ N("Bitrate (in b/s)"), "WIRELESS_RATE" ]
			     ),
			),
		Gtk2::VSeparator->new,
		gtkpack_(Gtk2::VBox->new(0,0),
			 map { (0, gtkpack_(Gtk2::VBox->new(0,0),
					    1, Gtk2::Label->new($_->[0]),
					    0, gtksignal_connect($gui->{intf}{$_->[1]} = Gtk2::Entry->new,
								 key_press_event => $apply),
					   ));
			   } ([ N("Encryption key"), 'WIRELESS_ENC_KEY' ],
			      [ N("RTS/CTS"), 'WIRELESS_RTS' ],
			      [ N("Fragmentation"), 'WIRELESS_FRAG' ],
			      [ N("iwconfig command extra arguments"),  'WIRELESS_IWCONFIG' ],
			      [ N("iwspy command extra arguments"), 'WIRELESS_IWSPY' ],
			      [ N("iwpriv command extra arguments"), 'WIRELESS_IWPRIV' ],
			     ),
			),
	       );
    }

    if ($intf->{pages}{Options}) {
	gtkpack__(gtkset_border_width($gui->{sheet}{Options} = Gtk2::VBox->new(0,10), 5),
                  $gui->{intf_bool}{ONBOOT} = gtksignal_connect(Gtk2::CheckButton->new(N("Start at boot")),
                                                                toggled => $apply),
                  if_($is_ethernet,
                      map { ($gui->{intf_bool}{$_->[0]} = gtksignal_connect(Gtk2::CheckButton->new($_->[1]),
                                                                            toggled => $apply));
                        } (
                           [ "MII_NOT_SUPPORTED", N("Network Hotplugging") ],
                          ),
                     ),
                  if_($interface eq 'isdn',
                      gtkpack(Gtk2::HBox->new(0,0),
                              gtkpack__(Gtk2::VBox->new(0,0),
                                        Gtk2::Label->new(N("Dialing mode")),
                                        my @dialing_mode_radio = gtkradio(("auto") x 2, "manual"),
                                       ),
                              Gtk2::VSeparator->new,
                              gtkpack__(Gtk2::VBox->new(0,0),
                                        Gtk2::Label->new(N("Connection speed")),
                                        my @speed_radio = gtkradio(("64 Kb/s") x 2, "128 Kb/s"),
                                       ),
                             ),
                      gtkpack__(Gtk2::HBox->new(0,5),
                               Gtk2::Label->new(N("Connection timeout (in sec)")),
                               gtksignal_connect($gui->{intf}{huptimeout} = Gtk2::Entry->new,
                                                    key_press_event => $apply),
                              ),
                     ),
                  gtkpack__(Gtk2::HBox->new(0,1),
                            gtknew('Label_Left', text => N("Metric")),
                            gtksignal_connect(gtkset_text($gui->{intf}{METRIC} = Gtk2::Entry->new, $intf->{METRIC}),
                                              key_press_event => $apply)),

                 );
        $dialing_mode_radio[0]->signal_connect(toggled => sub { $gui->{intf_radio}{dialing_mode} = 'auto'; $apply->() });
	$dialing_mode_radio[1]->signal_connect(toggled => sub { $gui->{intf_radio}{dialing_mode} = 'static'; $apply->() });
	$speed_radio[0]->signal_connect(toggled => sub { $gui->{intf_radio}{speed} = '64'; $apply->() });
	$speed_radio[1]->signal_connect(toggled => sub { $gui->{intf_radio}{speed} = '128'; $apply->() });
	$gui->{intf_bool}{ONBOOT}->set_active($intf->{ONBOOT} eq 'yes' ? 1 : 0);
	$gui->{intf_bool}{MII_NOT_SUPPORTED}->set_active($intf->{MII_NOT_SUPPORTED} eq 'no' ? 1 : 0);
    }

    if ($intf->{pages}{Account}) {
	if ($interface_name =~ /^speedtouch|sagem$/) {
	    $gui->{description} = $interface_name eq 'speedtouch' ? 'Alcatel|USB ADSL Modem (Speed Touch)' : 'Analog Devices Inc.|USB ADSL modem';
	}
	gtkpack_(gtkset_border_width($gui->{sheet}{Account} = Gtk2::VBox->new(0,10), 5),
		 if_($interface eq 'modem',
                      0, gtkpack(Gtk2::VBox->new(1,0),
				 gtkpack__(Gtk2::HBox->new, Gtk2::Label->new(N("Authentication"))),
				 gtkpack__(Gtk2::HBox->new, $gui->{intf}{auth} = gtksignal_connect(Gtk2::ComboBox->new_text,
                                                                                                   changed => $apply)),
				)),
		 map { (0, gtkpack(Gtk2::VBox->new(1,0),
                                   gtkpack__(Gtk2::HBox->new, Gtk2::Label->new($_->[0])),
                                   gtkpack__(Gtk2::HBox->new, $gui->{intf}{$_->[1]} = gtksignal_connect(Gtk2::Entry->new,
                                                                                                        key_press_event => $apply)),
                                  ),
		       );
		   } ([ N("Account Login (user name)"), 'login' ],
		      [ N("Account Password"), 'passwd' ],
		      if_($interface =~ /^(isdn|modem)$/, [ N("Provider phone number"), $1 eq 'modem' ? 'phone' : 'phone_out' ]),
		     ),
		);

	if ($interface eq 'modem') {
            my %auth_methods = map_index { $::i => $_ } N("PAP"), N("Terminal-based"), N("Script-based"), N("CHAP"), N("PAP/CHAP");
            $gui->{intf}{auth}->set_popdown_strings(sort values %auth_methods);
            $gui->{intf}{auth}->set_text($auth_methods{$intf->{Authentication}});
	}
	$gui->{intf}{passwd}->set_visibility(0);
    }

    if ($intf->{pages}{Modem}) {
	gtkpack(gtkset_border_width($gui->{sheet}{Modem} = Gtk2::HBox->new(0,10), 5),
		if_($interface eq 'modem',
                     gtkpack__(Gtk2::VBox->new(0,5),
                               (map { (gtkpack(Gtk2::VBox->new(1,0),
					       gtkpack__(Gtk2::HBox->new, Gtk2::Label->new($_->[0])),
					       gtkpack__(Gtk2::HBox->new, $gui->{intf}{$_->[1]} = gtksignal_connect(Gtk2::ComboBox->new_text,
                                                                                                                    changed => $apply)),
					      ),
                                   );
                                  } ([ N("Flow control"), 'FlowControl' ],
                                     [ N("Line termination"), 'Enter' ],
                                     [ N("Connection speed"), 'Speed' ],
                                    )),
                               # gtkpack(Gtk2::VBox->new(0,0), # no relative kppp option found :-(
                               #          Gtk2::Label->new(N("Dialing mode")),
                               # 	 gtkradio('', N("Tone dialing"), N("Pulse dialing")),
                               #        ),
                              ),
                     Gtk2::VSeparator->new,
                     gtkpack__(Gtk2::VBox->new(0,10),
                               gtkpack__(Gtk2::HBox->new(0,5),
                                         Gtk2::Label->new(N("Modem timeout")),
                                         $gui->{intf}{Timeout} = gtksignal_connect(Gtk2::SpinButton->new(Gtk2::Adjustment->new($intf->{Timeout}, 0, 120, 1, 5, 0), 0, 0),
                                                                                   value_changed => $apply),
                                        ),
                               gtksignal_connect($gui->{intf_bool}{UseLockFile} = Gtk2::CheckButton->new(N("Use lock file")),
                                                 toggled => $apply),
                               gtkpack__(Gtk2::HBox->new, gtksignal_connect($gui->{intf_bool}{WaitForDialTone} = Gtk2::CheckButton->new(N("Wait for dialup tone before dialing")),
                                                                            toggled => $apply)),
                               gtkpack__(Gtk2::HBox->new(0,5),
                                         Gtk2::Label->new(N("Busy wait")),
                                         $gui->{intf}{BusyWait} = gtksignal_connect(Gtk2::SpinButton->new(Gtk2::Adjustment->new($intf->{BusyWait}, 0, 120, 1, 5, 0), 0, 0),
                                                                                    value_changed => $apply),
                                        ),
                               gtkpack__(Gtk2::HBox->new(0,5),
                                         Gtk2::Label->new(N("Modem sound")),
                                         gtkpack__(Gtk2::VBox->new(0,5), my @volume_radio = gtkradio('', N("Enable"), N("Disable"))),
                                        ),
                              ),
                    ),
		if_($interface eq 'isdn',
                     gtkpack_(Gtk2::VBox->new(0,0),
                              map { (0, gtkpack(Gtk2::VBox->new(1,0),
						gtkpack__(Gtk2::HBox->new, Gtk2::Label->new($_->[0])),
						gtkpack__(Gtk2::HBox->new, $gui->{intf}{$_->[1]} = gtksignal_connect(Gtk2::Entry->new,
                                                                                                   key_press_event => $apply)),
					       ),
                                    );
                                } ([ N("Card IRQ"), 'irq' ],
                                   [ N("Card mem (DMA)"), 'mem' ],
                                   [ N("Card IO"), 'io' ],
                                   [ N("Card IO_0"), 'io0' ],
                                  ),
                             ),
                     Gtk2::VSeparator->new,
                     gtkpack__(Gtk2::VBox->new(0,0),
                               Gtk2::Label->new(N("Protocol")),
                               my @protocol_radio = gtkradio('', N("European protocol (EDSS1)"),
                                                             N("Protocol for the rest of the world\nNo D-Channel (leased lines)")),
                              ),
                    ),
	       );
	$protocol_radio[0]->signal_connect(toggled => sub { $gui->{intf_radio}{protocol} = 2; $apply->() });
	$protocol_radio[1]->signal_connect(toggled => sub { $gui->{intf_radio}{protocol} = 3; $apply->() });
	$volume_radio[0]->signal_connect(toggled => sub { $gui->{intf_radio}{Volume} = 1; $apply->() });
	$volume_radio[1]->signal_connect(toggled => sub { $gui->{intf_radio}{Volume} = 0; $apply->() });
	$gui->{intf}{FlowControl}->set_popdown_strings('Hardware [CRTSCTS]', 'Software [XON/XOFF]', 'None');
	$gui->{intf}{Enter}->set_popdown_strings('CR', 'CF', 'CR/LF');
	$gui->{intf}{Speed}->set_popdown_strings('2400', '9600', '19200', '38400', '57600', '115200');
    }

    if ($intf->{pages}{Information}) {
	my ($info) = $gui->{description} ?
	  find { $_->{description} eq $gui->{description} } detect_devices::probeall : network::connection::ethernet::mapIntfToDevice($interface_name);
	my @intfs = grep { $interface_name eq $_->[0] } @all_cards;
	if (is_empty_hash_ref($info) && @intfs == 1) {
	    my $driver = $intfs[0][1];
	    my @cards = grep { $_->{driver} eq $driver } detect_devices::probeall();
	    @cards == 1 and $info = $cards[0];
	}

	gtkpack(gtkset_border_width($gui->{sheet}{Information} = Gtk2::VBox->new(0,10), 5),
		gtktext_insert(Gtk2::TextView->new,
			       join('',
				    map { $_->[0] . ": \x{200e}" . $_->[1] . "\n" } (
					 [ N("Vendor"), split('\|', $info->{description}) ],
					 [ N("Description"), reverse split('\|', $info->{description}) ],
					 [ N("Media class"), $info->{media_type} || '-' ],
					 [ N("Module name"), $info->{driver} || '-' ],
					 [ N("Mac Address"), c::get_hw_address($interface_name) || '-' ],
					 [ N("Bus"), $info->{bus} || '-' ],
					 [ N("Location on the bus"), $info->{pci_bus} || '-' ],
										    )
				   )
			      ),
	       );
    }

    foreach (keys %{$gui->{intf}}) {
        next if ref($gui->{intf}{$_}) !~ /Gtk2::(ComboBox|Entry)/;
        # skip unset fields:
        next if !$intf->{$_};
        # special case b/c of translation:
        next if member($_, qw(BOOTPROTO ));
        if ($_ eq "FlowControl") {
            # kppp is writing translated strings :-( (eg: s/Software/Logiciel/):
            # (let's hope that all translations use 'CRTSCTS' and 'XON/OFF' as substring)
            $gui->{intf}{$_}->set_text('Hardware [CRTSCTS]') if $intf->{$_} =~ /CRTSCTS/;
            $gui->{intf}{$_}->set_text('Software [XON/XOFF]') if $intf->{$_} =~ m!XON/XOFF!;
        } else {
            $gui->{intf}{$_}->set_text($intf->{$_});
        }
    }

    $gui->{notebook} = Gtk2::Notebook->new;
    populate_notebook($gui->{notebook}, $gui);
}

sub populate_notebook {
    my ($notebook, $gui) = @_;
    foreach ('TCP/IP', 'DHCP', 'Account', 'Wireless', 'Modem', 'Options', 'Information') {
	!$gui->{sheet}{$_} and next;
	$notebook->append_page($gui->{sheet}{$_}, Gtk2::Label->new(translate($_)));
    }
}

sub save {
    my ($p, $apply_button) = @_;

    my $dialog = _create_dialog(N("Please wait"));
    gtkpack($dialog->vbox,
            gtkshow(Gtk2::Label->new(N("Please Wait... Applying the configuration"))));
    $dialog->show_all;
    gtkset_mousecursor_wait();

    Glib::Timeout->add(200, sub {
                           gtkflush();
                           delete $net->{network}{GATEWAY};
                           foreach (keys %$p) {
                               save_notebook($p->{$_}{intf}, $p->{$_}{gui}) or return;
                               $p->{$_}{intf}{save} and $p->{$_}{intf}{save}->();
                           }
                           apply();
                           system("/etc/rc.d/init.d/network restart");
                           $dialog->response(0);
                       });
    $dialog->run;

    $apply_button->set_sensitive(0);
    gtkset_mousecursor_normal();
    $dialog->destroy;
}

sub save_notebook {
    my ($intf, $gui) = @_;

    if ($gui->{network}{GATEWAY}->is_sensitive && $gui->{network}{GATEWAY}->get_text) {
        $net->{network}{GATEWAY} =  $gui->{network}{GATEWAY}->get_text;
    }
    $gui->{intf}{$_} and $intf->{$_} = $gui->{intf}{$_}->get_text foreach keys %{$gui->{intf}};
    $gui->{intf_radio}{$_} and $intf->{$_} = $gui->{intf_radio}{$_} foreach keys %{$gui->{intf_radio}};
    $intf->{$_} = bool2yesno($gui->{intf_bool}{$_}->get_active) foreach keys %{$gui->{intf_bool}};
    $gui->{intf_bool}{MII_NOT_SUPPORTED} and $intf->{MII_NOT_SUPPORTED} = bool2yesno(!$gui->{intf_bool}{MII_NOT_SUPPORTED}->get_active);

    if (my $proto = $gui->{intf}{BOOTPROTO}) {
        $intf->{BOOTPROTO} = { reverse %{$proto->{protocols}} }->{$proto->get_text};
    }
    if ($intf->{BOOTPROTO} eq 'static') {
        if (!is_ip($intf->{IPADDR})) {
            $in->ask_warn(N("Error"), N("IP address should be in format 1.2.3.4"));
            return 0;
        }
        if (!is_ip($intf->{NETMASK})) {
            $in->ask_warn(N("Error"), N("Netmask should be in format 255.255.224.0"));
            return 0;
        }
    }

    delete $intf->{IPADDR} if $intf->{IPADDR} eq N("No IP");
    delete $intf->{NETMASK} if $intf->{NETMASK} eq N("No Mask");

    if ($net->{network}{GATEWAY} && !is_ip($net->{network}{GATEWAY})) {
        $in->ask_warn(N("Error"), N("Gateway address should be in format 1.2.3.4"));
        return 0;
    }
    1;
}

sub add_intf() {
    $::isWizard = 1;
    network::netconnect::safe_main($net, $in, $modules_conf);
    $modules_conf->write;
    $in->exit(0);
}

sub del_intf() {
    my ($intf2delete, $failure);
    if (!keys %{$net->{ifcfg}}) {
      $in->ask_warn(N("Error"), N("No ethernet network adapter has been detected on your system. Please run the hardware configuration tool."));
      $in->exit(0);
    }
    require wizards;
    my $wiz = wizards->new(
      {
       defaultimage => "drakconnect",
       name => N("Remove a network interface"),
       pages => {
                 welcome => {
                             no_back => 1,
                             name => N("Select the network interface to remove:"),
                             data =>  [ { label => N("Net Device"), val => \$intf2delete, allow_empty_list => 1,
                                          list => [ keys %{$net->{ifcfg}} ],
                                          format => sub {
                                              my $type = network::tools::get_interface_type($net->{ifcfg}{$_[0]});
                                              $names{$_[0]} || ($type ? "$type ($_[0])" : $_[0]);
                                            }
                                        }
                                      ],
                             post => sub {
                                 !$::testing and eval {
                                     if (member($intf2delete, qw(adsl modem))) {
                                         eval { rm_rf("/etc/ppp/peers/ppp0") };
                                         eval { rm_rf("/etc/sysconfig/network-scripts/ifcfg-ppp0") };
                                     }
                                     if ($intf2delete eq 'adsl') {
                                         eval { rm_rf("/etc/sysconfig/network-scripts/ifcfg-sagem") };
                                     } elsif ($intf2delete eq 'isdn') {
                                         eval { rm_rf("/etc/sysconfig/network-scripts/ifcfg-ippp0") };
                                     } else {
                                         system("ifdown $intf2delete");
                                         eval { rm_rf("/etc/sysconfig/network-scripts/$intf2delete") };
                                         eval { rm_rf("/etc/sysconfig/network-scripts/ifcfg-$intf2delete") };
                                     }
                                 };
                                 $failure = $@;
                                 network::network::reload_net_applet();
                                 return "end";
                             },
                            },
                 end => {
                         name => sub {
                             $failure ?
                               N("An error occurred while deleting the \"%s\" network interface:\n\n%s", $intf2delete, $failure)
                             : N("Congratulations, the \"%s\" network interface has been successfully deleted", $intf2delete);
                         },
                         end => 1,
                        },
                },
      });
    $wiz->safe_process($in);
    $in->exit(0);
}

sub get_intf_ip {
    my ($interface) = @_;
    my ($ip, $state, $mask);
    if (-x "/sbin/ifconfig") {
	local $_ = `LC_ALL=C LANGUAGE=C /sbin/ifconfig $interface`;
	$ip = /inet addr:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/mso ? $1 : N("No IP");
	$mask = /Mask:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/mso ? $1 : N("No Mask");
	$state = /inet/ ? N("up") : N("down");
    } else {
	$ip = $net->{ifcfg}{$interface}{IPADDR};
	$state = "n/a";
    }
    ($ip, $state, $mask);
}

sub apply() {
    network::network::configure_network($net, $in, $modules_conf);
}

sub update_intbutt() {
    $int_state->set($isconnected ? N("Connected") : N("Not connected"));
}

my $net_test;
sub update_network_status() {
    unless ($net_test) {
        $net_test = network::test->new;
        $net_test->start;
    }
    if ($net_test->is_done) {
        $isconnected = $net_test->is_connected;
        update_intbutt();
        $net_test->start;
    }
    1;
}

sub configure_net() {
    my $dialog = ugtk2->new(N("Internet connection configuration"));
    my $exit_dialogsub = sub { Gtk2->main_quit };
    if (!$net->{type}) {
        $in->ask_warn(
                    N("Warning"),
                    N("You do not have any configured Internet connection.
Run the \"%s\" assistant from the Mandriva Linux Control Center", N("Set up a new network interface (LAN, ISDN, ADSL, ...)")));
        $in->exit;
    }
    unless ($::isEmbedded) {
        $dialog->{rwindow}->set_position('center');
        $dialog->{rwindow}->set_size_request(-1, -1);
        $dialog->{rwindow}->set_icon(gtkcreate_pixbuf("drakconnect"));
    }
    $dialog->{rwindow}->signal_connect(delete_event => $exit_dialogsub);

    my $param_vbox = Gtk2::VBox->new(0,0);
    my $i = 0;

    my @conf_data = (
                     [ N("Host name (optional)"), \$net->{network}{HOSTNAME} ],
                     [ N("First DNS Server (optional)"),  \$net->{resolv}{dnsServer} ],
                     [ N("Second DNS Server (optional)"), \$net->{resolv}{dnsServer2} ],
                     [ N("Third DNS server (optional)"),  \$net->{resolv}{dnsServer3} ],
                    );
    my @infos;
    gtkpack($param_vbox,
            create_packtable({},
                             map {
                                 my $c;
                                 if (defined $_->[2]) {
                                     $c = Gtk2::Combo->new;
                                     $c->set_popdown_strings(@{$_->[2]});
                                     $infos[2*$i+1] = $c->entry;
                                 } else {
                                     $c = $infos[2*$i+1] = Gtk2::Entry->new;
                                 }
                                 $infos[2*$i+1]->set_text(${$_->[1]});
                                 $i++;
                                 [ Gtk2::WrappedLabel->new($_->[0]), $c ];
                             } @conf_data
                            )
           );

    $dialog->{rwindow}->add(gtkpack_(Gtk2::VBox->new,
                                     0, Gtk2::Label->new(N("Internet Connection Configuration")),
                                     1, gtkadd(gtkcreate_frame(N("Internet access")),
                                               gtkset_border_width(create_packtable({ col_spacings => 5, row_spacings => 5, homogenous => 1 },
                                                                                    [ Gtk2::WrappedLabel->new(N("Connection type: ")),
                                                                                      Gtk2::WrappedLabel->new(translate($net->{type})) ],
                                                                                    [ $int_label, $int_name ],
                                                                                    [ Gtk2::WrappedLabel->new(N("Status:")),
                                                                                      $int_state = Gtk2::WrappedLabel->new(N("Testing your connection...")) ]
                                                                                   ),
                                                                   5),
                                              ),
                                     1, gtkadd(gtkcreate_frame(N("Parameters")), gtkset_border_width($param_vbox, 5)),
                                     0, gtkpack(create_hbox('edge'),
                                                gtksignal_connect(Gtk2::Button->new(N("Cancel")), clicked => $exit_dialogsub),
                                                gtksignal_connect(Gtk2::Button->new(N("Ok")), clicked => sub {
                                                                          foreach my $i (0..$#conf_data) {
                                                                              ${$conf_data[$i][1]} = $infos[2*$i+1]->get_text;
                                                                          }
                                                                          apply();
                                                                          $exit_dialogsub->();
                                                                      }),
                                                ),
                                    ),
                           );

    $dialog->{rwindow}->show_all;
    update_network_status();
    Glib::Timeout->add(2000, \&update_network_status);
    $dialog->main;
    ugtk2->exit(0);
}