summaryrefslogtreecommitdiffstats
path: root/gurpmi
blob: 97dcc166d82fcd089942c585b976094552f062db (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
#!/usr/bin/perl

#- Copyright (C) 2005 Mandrakesoft

use strict;
use warnings;

BEGIN { #- set up a safe path and environment
    $ENV{PATH} = "/bin:/usr/bin:/usr/X11R6/bin";
    delete @ENV{qw(ENV BASH_ENV IFS CDPATH)};
}

#- This is needed because text printed by Gtk2 will always be encoded
#- in UTF-8; we first check if LC_ALL is defined, because if it is,
#- changing only LC_COLLATE will have no effect.
use POSIX qw(setlocale LC_ALL LC_COLLATE);
use locale;
BEGIN {
    my $collation_locale = $ENV{LC_ALL};
    if ($collation_locale) {
	$collation_locale =~ /UTF-8/ or setlocale(LC_ALL, "$collation_locale.UTF-8");
    } else {
	$collation_locale = setlocale(LC_COLLATE);
	$collation_locale =~ /UTF-8/ or setlocale(LC_COLLATE, "$collation_locale.UTF-8");
    }
}

use urpm;
use urpm::msg qw(N);
use Gtk2;
use MDK::Common qw(partition);

sub usage () {
    print STDERR <<USAGE;
gurpmi version $urpm::VERSION
Usage :
    gurpmi <rpm> [ <rpm>... ]
USAGE
    exit 0;
}

#- fatal gurpmi initialisation error (*not* fatal urpmi errors)
sub fatal { print STDERR "$_[0]\n"; exit 1 }

sub quit () { Gtk2->main_quit }

sub but ($) { "    $_[0]    " }

#- globals
my (@all_rpms, $srpms, $rpms);
my ($mainw, $mainbox);

#- Gtk2 helper functions

sub add_button_box {
    my ($vbox, @buttons) = @_;
    my $hbox = Gtk2::HButtonBox->new;
    $vbox->pack_start($hbox, 0, 0, 0);
    $hbox->set_layout('edge');
    $_->set_alignment(0.5, 0.5), $hbox->add($_) foreach @buttons;
}

sub new_label {
    my ($msg) = @_;
    my $label = Gtk2::Label->new($msg);
    $label->set_line_wrap(1);
    $label->set_alignment(0.5, 0.5);
    if (($msg =~ tr/\n/\n/) > 5) {
	my $sw = Gtk2::ScrolledWindow->new;
	$sw->set_policy('never', 'automatic');
	$sw->add_with_viewport($label);
	$sw->set_size_request(-1,200);
	return $sw;
    } else {
	return $label;
    }
}

#- Parse command line
foreach (@ARGV) {
    if (/^-/) {
	/^--?[hv?]/ and usage;
	fatal N("Unknown option %s", $_);
    }
    push @all_rpms, $_;
}
@all_rpms or fatal N("No packages specified");

#- Now, the graphical stuff.

Gtk2->init;

#- Create main window

$mainw = Gtk2::Window->new('toplevel');
$mainw->set_border_width(3);
$mainw->set_title(N("RPM installation"));
$mainw->signal_connect(destroy => \&quit);
$mainw->set_position('center');
$mainw->set_modal(0);
$mainbox = Gtk2::VBox->new(0, 5);
$mainw->add($mainbox);

#- Ask question: save or install ?
#- change depending on the number of rpms, and on the presence of srpms
($srpms, $rpms) = partition { /\.src\.rpm$/ } @all_rpms;
{
    my $msg = (
	@$srpms > 0
	? N("You have selected a source package:

%s

You probably didn't want to install it on your computer (installing it would allow you to make modifications to its sourcecode then compile it).

What would you like to do?", $srpms->[0])
	: @all_rpms == 1
	? N("You are about to install the following software package on your computer:

%s

You may prefer to just save it. What is your choice?", $rpms->[0])
	: N("You are about to install the following software packages on your computer:

%s

Proceed?", join "\n", @all_rpms)
    );
    $mainbox->pack_start(new_label($msg), 1, 1, 0);
}

{   #- buttons
    my $inst_button = Gtk2::Button->new(but N("_Install"));
    my $save_button = @all_rpms == 1 ? Gtk2::Button->new(but N("_Save")) : undef;
    my $ccel_button = Gtk2::Button->new(but N("_Cancel"));

    $inst_button->signal_connect(clicked => \&do_install);
    $save_button and $save_button->signal_connect(clicked => sub {
	my $file_dialog = Gtk2::FileSelection->new(N("Choose location to save file"));
	$file_dialog->set_modal(1);
	$file_dialog->set_position('center');
	$file_dialog->set_filename($rpms->[0]); #- TODO must work for srpms too
	$file_dialog->hide_fileop_buttons;
	$file_dialog->ok_button->signal_connect(clicked => sub {
		my $location = $file_dialog->get_filename;
		quit;
		$location and exec '/bin/mv', '-f', $rpms->[0], $location;
	    });
	$file_dialog->cancel_button->signal_connect(clicked => \&quit);
	$file_dialog->show;
    });
    $ccel_button->signal_connect(clicked => \&quit);
    add_button_box($mainbox, grep { defined $_ } $inst_button, $save_button, $ccel_button);
}

$mainw->show_all;
Gtk2->main;

#- Performs installation.
sub do_install {
    quit;
    #- we need to switch to root if we're not already (via consolehelper)
    #- yes. hardcoded paths. safe.
    exec $> ? '/usr/bin/gurpmi2' : '/usr/sbin/gurpmi2', @ARGV;
}