summaryrefslogtreecommitdiffstats
path: root/perl-install/interactive_newt.pm
blob: abdb09503359228a3f0f42023a386bc575396d06 (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
package interactive_newt; # $Id$

use diagnostics;
use strict;
use vars qw(@ISA);

@ISA = qw(interactive);

use interactive;
use common;
use log;
use Newt::Newt; #- !! provides Newt and not Newt::Newt

my ($width, $height) = (80, 25);
my @wait_messages;

sub new() {
    Newt::Init;
    Newt::Cls;
    Newt::SetSuspendCallback;
    ($width, $height) = Newt::GetScreenSize;
    open STDERR,">/dev/null" if $::isStandalone;
    bless {}, $_[0];
}

sub enter_console { Newt::Suspend }
sub leave_console { Newt::Resume }
sub suspend { Newt::Suspend }
sub resume { Newt::Resume }
sub end() { Newt::Finished }
sub exit() { end; exit($_[1]) }
END { end() }

sub myTextbox {
    my $allow_scroll = shift;

    my $width = $width - 9;
    my @l = map { /(.{1,$width})/g } map { split "\n" } @_;
    my $h = min($height - 13, int @l);
    my $flag = 1 << 6; 
    if ($h < @l) {
	if ($allow_scroll) {
	    $flag |= 1 << 2; #- NEWT_FLAG_SCROLL
	} else {
	    # remove the text, no other way!
	    @l = @l[0 .. $h-1];
	}
    }
    my $mess = Newt::Component::Textbox(1, 0, my $w = max(map { length } @l) + 1, $h, $flag);
    $mess->TextboxSetText(join("\n", @_));
    $mess, $w + 1, $h;
}

sub separator {
    my $blank = Newt::Component::Form(\undef, '', 0);
    $blank->FormSetWidth ($_[0]);
    $blank->FormSetHeight($_[1]);
    $blank;
}
sub checkval { $_[0] && $_[0] ne ' '  ? '*' : ' ' }

sub ask_fromW {
    my ($o, $common, $l, $l2) = @_;
    my $ignore; #-to handle recursivity
    my $old_focus = -2;

    #-the widgets
    my (@widgets, $total_size);

    my $set_all = sub {
	$ignore = 1;
	$_->{set}->(${$_->{e}{val}}) foreach @widgets;
#	$_->{w}->set_sensitive(!$_->{e}{disabled}()) foreach @widgets;
	$ignore = 0;
    };
    my $get_all = sub {
	${$_->{e}{val}} = $_->{get}->() foreach @widgets;
    };
    my $create_widget = sub {
	my ($e, $ind) = @_;

	$e->{type} = 'list' if $e->{type} =~ /(icon|tree)list/;

	#- combo doesn't exist, fallback to a sensible default
	$e->{type} = $e->{not_edit} ? 'list' : 'entry' if $e->{type} eq 'combo';

	my $changed = sub {
	    return if $ignore;
	    return $old_focus++ if $old_focus == -2; #- handle special first case
	    $get_all->();

	    #- TODO: this is very rough :(
	    $common->{callbacks}{$old_focus == $ind ? 'changed' : 'focus_out'}->($ind);

	    $set_all->();
	    $old_focus = $ind;
	};

	my ($w, $real_w, $set, $get, $expand, $size);
	if ($e->{type} eq 'bool') {
	    $w = Newt::Component::Checkbox(-1, -1, $e->{text} || '', checkval(${$e->{val}}), " *");
	    $set = sub { $w->CheckboxSetValue(checkval($_[0])) };
	    $get = sub { $w->CheckboxGetValue == ord '*' };
	} elsif ($e->{type} eq 'button') {
	    $w = Newt::Component::Button(-1, -1, may_apply($e->{format}, ${$e->{val}}));
	} elsif ($e->{type} =~ /list/) {
	    my ($h, $wi) = (@$l == 1 && $height > 30 ? 10 : 5, 20);
	    my $scroll = @{$e->{list}} > $h ? 1 << 2 : 0;
	    $size = min(int @{$e->{list}}, $h);

	    $w = Newt::Component::Listbox(-1, -1, $h, $scroll); #- NEWT_FLAG_SCROLL	    
	    foreach (@{$e->{list}}) {
		my $t = may_apply($e->{format}, $_);
		$w->ListboxAddEntry($t, $_);
		$wi = max($wi, length $t);
	    }
	    $w->ListboxSetWidth(min($wi + 3, $width - 7)); # 3 added for the scrollbar (?)
	    $get = sub { $w->ListboxGetCurrent };
	    $set = sub {
		my ($val) = @_;
		map_index {
		    $w->ListboxSetCurrent($::i) if $val eq $_;
		} @{$e->{list}};
	    };
	} else {
	    $w = Newt::Component::Entry(-1, -1, '', 20, ($e->{hidden} && 1 << 11) | 1 << 2);
	    $get = sub { $w->EntryGetValue };
	    $set = sub { $w->EntrySet($_[0], 1) };
	}
	$total_size += $size || 1;

	#- !! callbacks must be kept otherwise perl will free them !!
	#- (better handling of addCallback needed)

	{ e => $e, w => $w, real_w => $real_w || $w, expand => $expand, callback => $changed,
	  get => $get || sub { ${$e->{val}} }, set => $set || sub {} };
    };
    @widgets = map_index { $create_widget->($_, $::i) } @$l;

    $_->{w}->addCallback($_->{callback}) foreach @widgets;

    $set_all->();

    my $grid = Newt::Grid::CreateGrid(3, max(1, int @$l));
    map_index {
	$grid->GridSetField(0, $::i, 1, ${Newt::Component::Label(-1, -1, $_->{e}{label})}, 0, 0, 1, 0, 1, 0);
	$grid->GridSetField(1, $::i, 1, ${$_->{real_w}}, 0, 0, 0, 0, 1, 0);
    } @widgets;

    my $listg = do {
	my $height = 18;
	#- use a scrolled window if there is a lot of checkboxes (aka 
	#- ask_many_from_list) or a lot of widgets in general (aka
	#- options of a native PostScript printer in printerdrake)
	#- !! works badly together with list's (lists are one widget, so a
	#- big list window will not switch to scrollbar mode) :-(
	if ((((grep { $_->{type} eq 'bool' } @$l) > 6) ||
             ((@$l) > 3)) && $total_size > $height) {
	    $grid->GridPlace(1, 1); #- Uh?? otherwise the size allocated is bad

	    my $scroll = Newt::Component::VerticalScrollbar(-1, -1, $height, 9, 10);
	    my $subf = $scroll->Form('', 0);
	    $subf->FormSetHeight($height);
	    $subf->FormAddGrid($grid, 0);
	    Newt::Grid::HCloseStacked($subf, separator(1, $height), $scroll);
	} else {
	    $grid;
	}
    };
    my ($buttons, $ok, $cancel) = Newt::Grid::ButtonBar($common->{ok} || _("Ok"), if_($common->{cancel}, $common->{cancel}));

    my $form = Newt::Component::Form(\undef, '', 0);
    my $window = Newt::Grid::GridBasicWindow(first(myTextbox(@widgets == 0, @{$common->{messages}})), $listg, $buttons);
    $window->GridWrappedWindow($common->{title} || '');
    $form->FormAddGrid($window, 1);

    my $check = sub {
	my ($f) = @_;

	$get_all->();
	my ($error, $focus) = $f->();
	
	if ($error) {
	    $set_all->();
	}
	!$error;
    };

    my ($destroyed, $canceled);
    do {
	my $r = $form->RunForm;
	foreach (@widgets) {
	    if ($$r == ${$_->{w}}) {
		$destroyed = 1;
		$form->FormDestroy;
		Newt::PopWindow;
		my $v = $_->{e}{clicked_may_quit}();
		$v or return ask_fromW($o, $common, $l, $l2);
	    }
	}
	$canceled = $cancel && $$r == $$cancel;

    } until ($check->($common->{callbacks}{$canceled ? 'canceled' : 'complete'}));

    if (!$destroyed) {
	$form->FormDestroy;
	Newt::PopWindow;
    }
    !$canceled;
}


sub waitbox {
    my ($title, $messages) = @_;
    my ($t, $w, $h) = myTextbox(1, @$messages);
    my $f = Newt::Component::Form(\undef, '', 0);
    Newt::CenteredWindow($w, $h, $title);
    $f->FormAddComponent($t);
    $f->DrawForm;
    Newt::Refresh;
    $f->FormDestroy;
    push @wait_messages, $f;
    $f;
}


sub wait_messageW {
    my ($o, $title, $messages) = @_;
    { form => waitbox($title, $messages), title => $title };
}

sub wait_message_nextW {
    my ($o, $messages, $w) = @_;
    $o->wait_message_endW($w);
    $o->wait_messageW($w->{title}, $messages);
}
sub wait_message_endW {
    my ($o, $w) = @_;
    my $wait = pop @wait_messages;
#    log::l("interactive_newt does not handle none stacked wait-messages") if $w->{form} != $wait;
    Newt::PopWindow;
}


1;
e specific license terms grant you such rights, you usually cannot install the programs on more than one system, or adapt it to be used on a network. In doubt, please contact directly the distributor or editor of the component. Transfer to third parties or copying of such components including the documentation is usually forbidden. All rights to the components of the next CD media belong to their respective authors and are protected by intellectual property and copyright laws applicable to software programs. "); #-###################################################################################### #- misc imports #-###################################################################################### use common; use partition_table qw(:types); use partition_table_raw; use install_steps; use install_interactive; use install_any; use detect_devices; use run_program; use devices; use fsedit; use loopback; use mouse; use modules; use lang; use keyboard; use any; use fs; use log; #-###################################################################################### #- In/Out Steps Functions #-###################################################################################### sub errorInStep($$) { my ($o, $err) = @_; $o->ask_warn(_("Error"), [ _("An error occurred"), formatError($err) ]); } sub kill_action { my ($o) = @_; $o->kill; } sub charsetChanged {} #-###################################################################################### #- Steps Functions #-###################################################################################### #------------------------------------------------------------------------------ sub selectLanguage { my ($o) = @_; $o->{lang} = any::selectLanguage($o, $o->{lang}, $o->{langs} ||= {}) || return $o->ask_yesorno('', _("Do you really want to leave the installation?")) ? $o->exit : &selectLanguage; install_steps::selectLanguage($o); $o->charsetChanged; if ($o->isa('interactive_gtk')) { $o->ask_warn('', formatAlaTeX( "If you see this message it is because you chose a language for which DrakX does not include a translation yet; however the fact that it is listed means there is some support for it anyway. That is, once GNU/Linux will be installed, you will be able to at least read and write in that language; and possibly more (various fonts, spell checkers, various programs translated etc. that varies from language to language).")) if $o->{lang} !~ /^en/ && !lang::load_mo(); } else { #- don't use _( ) for this, as it is never translated $o->ask_warn('', "The characters of your language can't be displayed in console, so the messages will be displayed in english during installation") if $ENV{LANGUAGE} eq 'C'; } } sub acceptLicence { my ($o) = @_; return if $o->{useless_thing_accepted}; $o->set_help('license'); $o->{useless_thing_accepted} = $o->ask_from_list_(_("License agreement"), formatAlaTeX( _("Introduction The operating system and the different components available in the Mandrake Linux distribution shall be called the \"Software Products\" hereafter. The Software Products include, but are not restricted to, the set of programs, methods, rules and documentation related to the operating system and the different components of the Mandrake Linux distribution. 1. License Agreement Please read this document carefully. This document is a license agreement between you and MandrakeSoft S.A. which applies to the Software Products. By installing, duplicating or using the Software Products in any manner, you explicitly accept and fully agree to conform to the terms and conditions of this License. If you disagree with any portion of the License, you are not allowed to install, duplicate or use the Software Products. Any attempt to install, duplicate or use the Software Products in a manner which does not comply with the terms and conditions of this License is void and will terminate your rights under this License. Upon termination of the License, you must immediately destroy all copies of the Software Products. 2. Limited Warranty The Software Products and attached documentation are provided \"as is\", with no warranty, to the extent permitted by law. MandrakeSoft S.A. will, in no circumstances and to the extent permitted by law, be liable for any special, incidental, direct or indirect damages whatsoever (including without limitation damages for loss of business, interruption of business, financial loss, legal fees and penalties resulting from a court judgment, or any other consequential loss) arising out of the use or inability to use the Software Products, even if MandrakeSoft S.A. has been advised of the possibility or occurance of such damages. LIMITED LIABILITY LINKED TO POSSESSING OR USING PROHIBITED SOFTWARE IN SOME COUNTRIES To the extent permitted by law, MandrakeSoft S.A. or its distributors will, in no circumstances, be liable for any special, incidental, direct or indirect damages whatsoever (including without limitation damages for loss of business, interruption of business, financial loss, legal fees and penalties resulting from a court judgment, or any other consequential loss) arising out of the possession and use of software components or arising out of downloading software components from one of Mandrake Linux sites which are prohibited or restricted in some countries by local laws. This limited liability applies to, but is not restricted to, the strong cryptography components included in the Software Products. 3. The GPL License and Related Licenses The Software Products consist of components created by different persons or entities. Most of these components are governed under the terms and conditions of the GNU General Public Licence, hereafter called \"GPL\", or of similar licenses. Most of these licenses allow you to use, duplicate, adapt or redistribute the components which they cover. Please read carefully the terms and conditions of the license agreement for each component before using any component. Any question on a component license should be addressed to the component author and not to MandrakeSoft. The programs developed by MandrakeSoft S.A. are governed by the GPL License. Documentation written by MandrakeSoft S.A. is governed by a specific license. Please refer to the documentation for further details. 4. Intellectual Property Rights All rights to the components of the Software Products belong to their respective authors and are protected by intellectual property and copyright laws applicable to software programs. MandrakeSoft S.A. reserves its rights to modify or adapt the Software Products, as a whole or in parts, by all means and for all purposes. \"Mandrake\", \"Mandrake Linux\" and associated logos are trademarks of MandrakeSoft S.A. 5. Governing Laws If any portion of this agreement is held void, illegal or inapplicable by a court judgment, this portion is excluded from this contract. You remain bound by the other applicable sections of the agreement. The terms and conditions of this License are governed by the Laws of France. All disputes on the terms of this license will preferably be settled out of court. As a last resort, the dispute will be referred to the appropriate Courts of Law of Paris - France. For any question on this document, please contact MandrakeSoft S.A. ")), [ __("Accept"), __("Refuse") ], "Refuse") eq "Accept" and return; $o->ask_yesorno('', _("Are you sure you refuse the licence?"), 1) and $o->exit; &acceptLicence; } #------------------------------------------------------------------------------ sub selectKeyboard { my ($o, $clicked) = @_; my $l = keyboard::lang2keyboards(lang::langs($o->{langs})); #- good guess, don't ask return install_steps::selectKeyboard($o) if !$::expert && !$clicked && $l->[0][1] >= 90 && listlength(lang::langs($o->{langs})) == 1; my @best = map { $_->[0] } @$l; push @best, 'us_intl' if !member('us_intl', @best); my $format = sub { translate(keyboard::keyboard2text($_[0])) }; my $other; my $ext_keyboard = $o->{keyboard}; $o->ask_from_( { title => _("Keyboard"), messages => _("Please choose your keyboard layout."), advanced_messages => _("Here is the full list of keyboards available"), advanced_label => _("More"), callbacks => { changed => sub { $other = $_[0]==1 } }, }, [ if_(@best > 1, { val => \$o->{keyboard}, type => 'list', format => $format, sort => 1, list => [ @best ] }), { val => \$ext_keyboard, type => 'list', format => $format, list => [ difference2([ keyboard::keyboards ], \@best) ], advanced => @best > 1 } ]); delete $o->{keyboard_unsafe}; $o->{keyboard} = $ext_keyboard if $other; install_steps::selectKeyboard($o); } #------------------------------------------------------------------------------ sub selectInstallClass1 { my ($o, $verif, $l, $def, $l2, $def2) = @_; $verif->($o->ask_from_list(_("Install Class"), _("Which installation class do you want?"), $l, $def) || die 'already displayed'); $::live ? 'Update' : $o->ask_from_list_(_("Install/Update"), _("Is this an install or an update?"), $l2, $def2); } #------------------------------------------------------------------------------ sub selectInstallClass { my ($o, $clicked) = @_; my %c = my @c = ( if_(!$::corporate, _("Recommended") => "beginner", ), if_($o->{meta_class} ne 'desktop', _("Expert") => "expert", ), ); %c = @c = (_("Expert") => "expert") if $::expert && !$clicked; $o->set_help('selectInstallClassCorpo') if $::corporate; my $verifInstallClass = sub { $::expert = $c{$_[0]} eq "expert" }; my $installMode = $o->{isUpgrade} ? $o->{keepConfiguration} ? __("Upgrade packages only") : __("Upgrade") : __("Install"); $installMode = $o->selectInstallClass1($verifInstallClass, first(list2kv(@c)), ${{reverse %c}}{$::expert ? "expert" : "beginner"}, [ __("Install"), __("Upgrade"), __("Upgrade packages only") ], $installMode); $o->{isUpgrade} = $installMode =~ /Upgrade/; $o->{keepConfiguration} = $installMode =~ /packages only/; install_steps::selectInstallClass($o); } #------------------------------------------------------------------------------ sub selectMouse { my ($o, $force) = @_; $force ||= $o->{mouse}{unsafe} || $::expert; my $prev = $o->{mouse}{type} . '|' . $o->{mouse}{name}; $o->{mouse} = mouse::fullname2mouse( $o->ask_from_treelist_('', _("Please choose the type of your mouse."), '|', [ mouse::fullnames ], $prev) || return) if $force; if ($force && $o->{mouse}{type} eq 'serial') { $o->set_help('selectSerialPort'); $o->{mouse}{device} =