summaryrefslogtreecommitdiffstats
path: root/urpm/msg.pm
blob: d913b23337cb93f5fc29a20083a523ca7ea39d43 (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
package urpm::msg;

use strict;
use Exporter;
our @ISA = 'Exporter';
our @EXPORT = qw(N log_it to_utf8 message_input gmessage message toMb);

my $noexpr = N("Nn");
my $yesexpr = N("Yy");

#- I18N.
eval {
    require Locale::gettext;
    use POSIX qw(LC_ALL);
    POSIX::setlocale(LC_ALL, "");
    Locale::gettext::textdomain("urpmi");
};

sub N {
    my ($format, @params) = @_;
    sprintf(
	eval { Locale::gettext::gettext($format || '') } || $format,
	@params,
    );
}

sub log_it {
    #- if invoked as a simple user, nothing should be logged.
    if ($::log) {
	open my $fh, ">>$::log" or die "can't output to log file: $!\n";
	print $fh @_;
	close $fh;
    }
}

sub to_utf8 { Locale::gettext::iconv($_[0], undef, "UTF-8") }

sub gmessage {
    my ($msg, %params) = @_;
    my $ok = to_utf8($params{ok} || N("Ok"));
    my $cancel = to_utf8($params{cancel} || N("Cancel"));
    $ok =~ s/,/\\,/g; $cancel =~ s/,/\\,/g;
    my $buttons = $params{ok_only} ? "$ok:0" : "$ok:0,$cancel:2";
    foreach (@{$params{add_buttons}}) {
	s/,/\\,/g;
	$buttons .= ",$_";
    }
    $msg = to_utf8($msg);
    `gmessage -default "$ok" -buttons "$buttons" "$msg"`;
}

sub message_input {
    my ($msg, $default_input, %opts) = @_;
    my $input;
    if ($urpm::args::options{X} && !$default_input) {
	#- if a default input is given, the user doesn't have to choose (and being asked).
	gmessage($msg, ok_only => 1);
	$urpm::args::options{bug} and log_it($msg);
    } else {
	while (1) {
	    if ($urpm::args::options{bug}) {
		print STDOUT $msg;
	    } else {
		print ::SAVEOUT $msg;
	    }
	    if ($default_input) {
		$urpm::args::options{bug} and log_it($default_input);
		return $default_input;
	    }
	    $input = <STDIN>;
	    defined $input or return undef;
	    $urpm::args::options{bug} and log_it($input);
	    if ($opts{boolean}) {
		$input =~ /^[$noexpr$yesexpr]*$/ and last;
	    } elsif ($opts{range}) {
		1 <= $input && $input <= $opts{range} and last;
	    } else {
		last;
	    }
	    message(N("Sorry, bad choice, try again\n"));
	}
    }
    return $input;
}

sub message {
    my ($msg, $no_X) = @_;
    if ($urpm::args::options{X} && !$no_X && !$::auto) {
	gmessage($msg, ok_only => 1);
	$urpm::args::options{bug} and log_it($msg);
    } else {
	if ($urpm::args::options{bug}) {
	    print STDOUT "$msg\n";
	} else {
	    print ::SAVEOUT "$msg\n";
	}
    }
}

sub toMb {
    my $nb = $_[0] / 1024 / 1024;
    int $nb + 0.5;
}

sub localtime2changelog { scalar(localtime($_[0])) =~ /(.*) \S+ (\d{4})$/ && "$1 $2" };

1;

__END__

=head1 NAME

urpm::msg - routines to prompt messages from the urpm* tools

=head1 SYNOPSIS

=head1 DESCRIPTION

=cut