package interactive; # $Id$ use diagnostics; use strict; #-###################################################################################### #- misc imports #-###################################################################################### use MDK::Common::Func; use common; #- minimal example using interactive: # #- > use lib qw(/usr/lib/libDrakX); #- > use interactive; #- > my $in = interactive->vnew; #- > $in->ask_okcancel('title', 'question'); #- > $in->exit; #- ask_from_ takes global options ($common): #- title => window title #- messages => message displayed in the upper part of the window #- advanced_messages => message displayed when "Advanced" is pressed #- ok => force the name of the "Ok"/"Next" button #- cancel => force the name of the "Cancel"/"Previous" button #- advanced_label => force the name of the "Advanced" button #- advanced_label_close => force the name of the "Basic" button #- advanced_state => if set to 1, force the "Advanced" part of the dialog to be opened initially #- focus_cancel => force focus on the "Cancel" button #- focus_first => force focus on the first entry #- callbacks => functions called when something happen: complete canceled advanced changed focus_out ok_disabled #- ask_from_ takes a list of entries with fields: #- val => reference to the value #- label => description #- icon => icon to put before the description #- help => tooltip #- advanced => wether it is shown in by default or only in advanced mode #- disabled => function returning wether it should be disabled (grayed) #- gtk => gtk preferences #- type => #- button => (with clicked or clicked_may_quit) #- (type defaults to button if clicked or clicked_may_quit is there) #- (val need not be a reference) (if clicked_may_quit return true, it's as if "Ok" was pressed) #- label => (val need not be a reference) (type defaults to label if val is not a reference) #- bool (with "text" or "image" (which overrides text) giving an image filename) #- range (with min, max) #- combo (with list, not_edit, format) #- list (with list, icon2f (aka icon), separator (aka tree), format (aka pre_format function), #- help can be a hash or a function, #- tree_expanded boolean telling wether the tree should be wide open by default #- quit_if_double_click boolean #- allow_empty_list disables the special cases for 0 and 1 element lists #- image2f is a subroutine which takes a value of the list as parameter, and returns an array (text, image_file_name)) #- entry (the default) (with hidden) # #- heritate from this class and you'll get all made interactivity for same steps. #- for this you need to provide #- - ask_from_listW(o, title, messages, arrayref, default) returns one string of arrayref #- #- where #- - o is the object #- - title is a string #- - messages is an refarray of strings #- - default is an optional string (default is in arrayref) #- - arrayref is an arrayref of strings #- - arrayref2 contains booleans telling the default state, #- #- ask_from_list and ask_from_list_ are wrappers around ask_from_biglist and ask_from_smalllist #- #- ask_from_list_ just translate arrayref before calling ask_from_list and untranslate the result #- #- ask_from_listW should handle differently small lists and big ones. #- #-###################################################################################### #- OO Stuff #-###################################################################################### sub new($) { my ($type) = @_; bless {}, ref $type || $type; } sub vnew { my ($_type, $su, $icon) = @_; $su = $su eq "su"; if ($ENV{INTERACTIVE_HTTP}) { require interactive::http; return interactive::http->new; } require c; if ($su) { $ENV{PATH} = "/sbin:/usr/sbin:$ENV{PATH}"; $su = '' if $::testing || $ENV{TESTING}; } require_root_capability() if $su; if (check_for_xserver()) { eval { require interactive::gtk }; if (!$@) { my $o = interactive::gtk->new; if ($icon && $icon ne 'default' && !$::isWizard) { $o->{icon} = $icon } else { undef $o->{icon} } return $o; } } require 'log.pm'; #- "require log" causes some pb, perl thinking that "log" is the log() function undef *log::l; *log::l = sub {}; # otherwise, it will bother us :( require interactive::newt; interactive::newt->new; } sub enter_console {} sub leave_console {} sub suspend {} sub resume {} sub end {} sub exit { exit($_[0]) } #-###################################################################################### #- Interactive functions #-###################################################################################### sub ask_warn { my ($o, $title, $message) = @_; local $::isWizard = 0; ask_from_listf_no_check($o, $title, $message, undef, [ N("Ok") ]); } sub ask_yesorno { my ($o, $title, $message, $def, $help) = @_; ask_from_list_($o, $title, $message, [ N_("Yes"), N_("No") ], $def ? "Yes" : "No", $help, 'nocancel') eq "Yes"; } sub ask_okcancel { my ($o, $title, $message, $def, $help) = @_; if ($::isWizard) { $::no_separator = 1; $o->ask_from_no_check({ title => $title, messages => $message, focus_cancel => !$def }, []); } else { ask_from_list_($o, $title, $message, [ N_("Ok"), N_("Cancel") ], $def ? "Ok" : "Cancel", $help, 'nocancel') eq "Ok"; } } sub ask_file { my ($o, $title, $dir) = @_; $o->ask_fileW($title, $dir); } sub ask_fileW { my ($o, $title, $_dir) = @_; $o->ask_from_entry($title, N("Choose a file")); } sub ask_from_list { my ($o, $title, $message, $l, $def, $help, $nocancel) = @_; ask_from_listf($o, $title, $message, undef, $l, $def, $help, $nocancel); } sub ask_from_list_ { my ($o, $title, $message, $l, $def, $help, $nocancel) = @_; ask_from_listf($o, $title, $message, sub { translate($_[0]) }, $l, $def, $help, $nocancel); } sub ask_from_listf_ { my ($o, $title, $message, $f, $l, $def, $help, $nocancel) = @_; ask_from_listf($o, $title, $message, sub { translate($f->(@_)) }, $l, $def, $help, $nocancel); } sub ask_from_listf { my ($_o, $_title, $_message, $_f, $l, $_def, $_help, $_nocancel) = @_; @$l == 0 and die "ask_from_list: empty list\n" . backtrace(); @$l == 1 and return $l->[0]; goto &ask_from_listf_no_check; } sub ask_from_listf_no_check { my ($o, $title, $message, $f, $l, $def, $help, $nocancel) = @_; if (@$l <= 2 && !$::isWizard) { my ($ok, $cancel) = map { $_ && may_apply($f, $_) } @$l; if (length "$ok$cancel" < 70) { my $ret = eval { ask_from_no_check($o, { title => $title, messages => $message, ok => $ok, if_($cancel, cancel => $cancel, focus_cancel => $def eq $l->[1]) }, [] ) ? $l->[0] : $l->[1]; }; die if $@ && $@ !~ /^wizcancel/; return $@ ? undef : $ret; } } ask_from_($o, { title => $title, messages => $message, if_($nocancel, cancel => ''), }, [ { val => \$def, type => 'list', list => $l, help => $help, format => $f } ]) && $def; } sub ask_from_treelist { my ($o, $title, $message, $separator, $l, $def) = @_; ask_from_treelistf($o, $title, $message, $separator, undef, $l, $def); } sub ask_from_treelist_ { my ($o, $title, $message, $separator, $l, $def) = @_; my $transl = sub { join '|', map { translate($_) } split(quotemeta($separator), $_[0]) }; ask_from_treelistf($o, $title, $message, $separator, $transl, $l, $def); } sub ask_from_treelistf { my ($o, $title, $message, $separator, $f, $l, $def) = @_; ask_from($o, $title, $message, [ { val => \$def, separator => $separator, list => $l, format => $f, sort => 1 } ]) or return; $def; } sub ask_many_from_list { my ($o, $title, $message, @l) = @_; @l = grep { @{$_->{list}} } @l or return ''; foreach my $h (@l) { $h->{e}{$_} = { text => may_apply($h->{label}, $_), val => $h->{val} ? $h->{val}->($_) : do { my $i = $h->{value} ? $h->{value}->($_) : $h->{values} ? member($_, @{$h->{values}}) : 0; \$i; }, type => 'bool', help => may_apply($h->{help}, $_, ''), icon => may_apply($h->{icon2f}, $_, ''), } foreach @{$h->{list}}; if ($h->{sort}) { $h->{list} = [ sort { $h->{e}{$a}{text} cmp $h->{e}{$b}{text} } @{$h->{list}} ]; } } $o->ask_from($title, $message, [ map { my $h = $_; map { $h->{e}{$_} } @{$h->{list}} } @l ]) or return; @l = map { my $h = $_; [ grep { ${$h->{e}{$_}{val}} } @{$h->{list}} ]; } @l; wantarray() ? @l : $l[0]; } sub ask_from_entry { my ($o, $title, $message, %callback) = @_; first(ask_from_entries($o, $title, $message, [''], %callback)); } sub ask_from_entries { my ($o, $title, $message, $l, %callback) = @_; my @l = map { my $i = ''; { label => $_, val => \$i } } @$l; $o->ask_from($title, $message, \@l, %callback) ? map { ${$_->{val}} } @l : undef; } sub ask_from__add_modify_remove { my ($o, $title, $message, $l, %callback) = @_; die "ask_from__add_modify_remove only handles one item" if @$l != 1; $callback{$_} or internal_error("missing callback $_") foreach qw(Add Modify Remove); if ($o->can('ask_from__add_modify_removeW')) { $o->ask_from__add_modify_removeW($title, $message, $l, %callback); } else { my $e = $l->[0]; my $chosen_element; put_in_hash($e, { allow_empty_list => 1, val => \$chosen_element, type => 'list' }); while (1) { my $continue; my @l = (@$l, map { my $s = $_; { val => translate($_), clicked_may_quit => sub { my $r = $callback{$s}->($chosen