aboutsummaryrefslogtreecommitdiffstats
path: root/edit-urpm-sources.pl
blob: 0a4c09d680096070249e1812576e4e8ce4e45b50 (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
#!/usr/bin/perl
#*****************************************************************************
# 
#  Copyright (c) 2002 Guillaume Cottenceau (gc at mandrakesoft dot com)
# 
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License version 2, as
#  published by the Free Software Foundation.
# 
#  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.
# 
#*****************************************************************************
#
# $Id$

$> and (exec {'consolehelper'} $0, @ARGV or die "consolehelper missing");

use lib qw(/usr/lib/libDrakX);
use strict;

use rpmdrake;

$::isStandalone = 1;
Gtk->init;


sub add_medium_and_check {
    my ($urpm, $msg, $options) = splice @_, 0, 3;
    standalone::explanations("Adding medium @_");
    my $wait = wait_msg($msg);
    $urpm->add_medium(@_);
    $urpm->update_media(%$options, noclean => 1);
    remove_wait_msg($wait);
    my ($medium) = grep { $_->{name} eq $_[0] } @{$urpm->{media}};
    $medium or interactive_msg('rpmdrake', _("Unable to create medium."));
    $medium->{modified} and interactive_msg('rpmdrake', _("Unable to update medium; it will be automatically disabled."));
    $urpm->write_config;
}

my $urpm = new urpm; 
$urpm->read_config; 

my ($remove, $edit, $clist);

sub add_callback {
    my ($mode, $rebuild_ui, $name_entry, $url_entry, $hdlist_entry, $count_nbs);
    my $w = my_gtk->new(_("Edit a source"));
    my %radios_infos = (local => { name => _("Local files"), url => _("Path:"), dirsel => 1 },
			ftp => { name => _("FTP server"), url => _("URL:"), loginpass => 1 },
			http => { name => _("HTTP server"), url => _("URL:") },
			removable => { name => _("Removable device"), url => _("Path or mount point:"), dirsel => 1 },
			security => { name => _("Security updates"), url => _("URL:"), securitysel => 1 },
		       );
    my @radios_names_ordered = qw(local ftp http removable security);
    my @modes_buttons = gtkradio($radios_infos{local}{name}, map { $radios_infos{$_}{name} } @radios_names_ordered);
    my $notebook = new Gtk::Notebook;
    $notebook->set_show_tabs(0); $notebook->set_show_border(0);
    mapn {
	my $info = $radios_infos{$_[0]};
	my $url_entry = sub {
	    gtkpack_(new Gtk::HBox(0, 0),
		     1, $info->{url_entry} = gtkentry,
		     if_($info->{dirsel}, 0, gtksignal_connect(new Gtk::Button(but(_("Browse..."))),
							       clicked => sub { $info->{url_entry}->set_text(ask_dir) })),
		     if_($info->{securitysel}, 0, gtksignal_connect(new Gtk::Button(but(_("Choose a mirror..."))),
								    clicked => sub { my $m = choose_mirror;
										     if ($m) {
											 my ($r) = cat_('/etc/mandrake-release') =~ /release\s(\S+)/;
											 $info->{url_entry}->set_text("$m/$r/RPMS/");
											 $info->{hdlist_entry}->set_text('../base/hdlist.cz');
										     }
										 })));
	};
	my $loginpass_entries = sub {
	    map { my $entry_name = $_->[0];
		  [ gtkpack_(new Gtk::HBox(0, 0),
			     1, new Gtk::Label,
			     0, gtksignal_connect($info->{$_->[0].'_check'} = new Gtk::CheckButton($_->[1]),
						  clicked => sub { $info->{$entry_name.'_entry'}->set_sensitive($_[0]->get_active);
								   $info->{pass_check}->set_active($_[0]->get_active);
								   $info->{login_check}->set_active($_[0]->get_active);
							       }),
			     1, new Gtk::Label),
		    gtkset_sensitive($info->{$_->[0].'_entry'} = gtkentry, 0) ] }
	      ([ 'login', _("Login:") ], [ 'pass', _("Password:") ])
	};
	my $nb = $count_nbs++;
	gtksignal_connect($_[1], 'clicked' => sub { $_[0]->get_active and $notebook->set_page($nb) });
	$notebook->append_page(my $book = create_packtable({},
		      [ _("Name:"), $info->{name_entry} = gtkentry($_[0] eq 'security' and 'update_source') ],
		      [ $info->{url}, $url_entry->() ],
		      [ _("Relative path to synthesis/hdlist:"), $info->{hdlist_entry} = gtkentry ],
		      if_($info->{loginpass}, $loginpass_entries->())));
	$book->show;
    } \@radios_names_ordered, \@modes_buttons;

    my $checkok = sub {
	my $info = $radios_infos{$radios_names_ordered[$notebook->get_current_page]};
	my ($name, $url, $hdlist) = map { $info->{$_.'_entry'}->get_text } qw(name url hdlist);
	$name eq '' || $url eq '' and interactive_msg('rpmdrake', _("You need to fill up at least the two first entries.")), return 0;
	if (member($name, map { $_->{name} } @{$urpm->{media}})) {
	    $info->{name_entry}->select_region(0, -1);
	    interactive_msg('rpmdrake', 
_("There is already a medium by that name, do you
really want to replace it?"), yesno => 1) or return 0;
	}
	1;
    };

    gtkadd($w->{window},
	   gtkpack(new Gtk::VBox(0,5),
		   new Gtk::Label(_("Adding a source:")),
		   gtkpack__(new Gtk::HBox(0, 0), new Gtk::Label(but(_("Type of source:"))), @modes_buttons),
		   $notebook,
		   new Gtk::HSeparator,
		   gtkpack(create_hbox(),
			   gtksignal_connect(new Gtk::Button(_("Ok")), clicked => sub {
						 $checkok->() and $w->{retval} = { nb => $notebook->get_current_page }, Gtk->main_quit;
					     }),
			   gtksignal_connect(new Gtk::Button(_("Cancel")), clicked => sub { $w->{retval} = 0; Gtk->main_quit }))));
    $w->{rwindow}->set_position('center');
    if ($w->main) {
	my $type = $radios_names_ordered[$w->{retval}{nb}];
	my $info = $radios_infos{$type};
	my %i = (name => $info->{name_entry}->get_text, url => $info->{url_entry}->get_text, hdlist => $info->{hdlist_entry}->get_text);
	my %make_url = (local => "file:/$i{url}", http => $i{url}, security => $i{url}, removable => "removable:/$i{url}");
	$i{url} =~ s|^ftp://||;
	$make_url{ftp} = sprintf "ftp://%s%s", $info->{login_check}->get_active
	                                           ? ($info->{login_entry}->get_text.':'.$info->{pass_entry}->get_text.'@')
						   : '',
					       $i{url};
	if (member($i{name}, map { $_->{name} } @{$urpm->{media}})) {
	    standalone::explanations("Removing medium $i{name}");
	    $urpm->select_media($i{name});
	    $urpm->remove_selected_media;
	}
	add_medium_and_check($urpm, _("Please wait, adding medium..."),
			     { probe_with_hdlist => member($type, qw(removable local)) && $i{hdlist} eq '' },
			     $i{name}, $make_url{$type}, $i{hdlist}, update => $type eq 'security');
	return 1;
    }
    return 0;
}

sub remove_callback {
    my $wait = wait_msg(_("Please wait, removing medium..."));
    my $name = $urpm->{media}[$clist->selection]{name};
    standalone::explanations("Removing medium $name");
    $urpm->select_media($name);
    $urpm->remove_selected_media;
    $urpm->update_media(noclean => 1);
    remove_wait_msg($wait);
}

sub edit_callback {
    my $medium = $urpm->{media}[$clist->selection];
    my $w = my_gtk->new(_("Edit a source"));
    my ($url_entry, $hdlist_entry);
    gtkadd($w->{window},
	   gtkpack_(new Gtk::VBox(0,5),
		    0, new Gtk::Label(_("Editing source \"%s\":", $medium->{name})),
		    0, create_packtable({},
					[ _("URL:"), $url_entry = gtkentry($medium->{url}) ],
					[ _("Relative path to synthesis/hdlist:"), $hdlist_entry = gtkentry($medium->{with_hdlist}) ]),
		    0, new Gtk::HSeparator,
		    0, gtkpack(create_hbox(),
			       gtksignal_connect(new Gtk::Button(_("Save changes")), clicked => sub { $w->{retval} = 1; Gtk->main_quit }),
			       gtksignal_connect(new Gtk::Button(_("Cancel")), clicked => sub { $w->{retval} = 0; Gtk->main_quit }))));
    $w->{rwindow}->set_position('center');
    $w->{rwindow}->set_usize(600, 0);
    if ($w->main) {
	my ($name, $update, $ignore) = map { $medium->{$_} } qw(name update ignore);
	my ($url, $with_hdlist) = ($url_entry->get_text, $hdlist_entry->get_text);
	$url =~ m|^removable://| and (interactive_msg(_("You need to insert the medium to continue"),
						      _("In order to save the changes, you need to insert the medium in the drive."),
						      yesno => 1, text => { yes => _("Ok"), no => _("Cancel") }) or return 0);
	standalone::explanations("Removing medium $name");
	$urpm->select_media($name);
	$urpm->remove_selected_media;
	add_medium_and_check($urpm, _("Please wait, updating medium..."), {}, $name, $url, $with_hdlist, update => $update);
	return 1;
    }
    return 0;
}

sub update_callback {
    my $w = my_gtk->new(_("Update source(s)"));
    gtkadd($w->{window},
	   gtkpack__(new Gtk::VBox(0,5),
		     new Gtk::Label(_("Select the source(s) you wish to update:")),
		     (my @buttons = map { new Gtk::CheckButton($_->{name}) } @{$urpm->{media}}),
		     new Gtk::HSeparator,
		     gtkpack(create_hbox(),
			     gtksignal_connect(new Gtk::Button(_("Update")), clicked => sub { $w->{retval} = 1; Gtk->main_quit }),
			     gtksignal_connect(new Gtk::Button(_("Cancel")), clicked => sub { $w->{retval} = 0; Gtk->main_quit }))));
    $w->{rwindow}->set_position('center');
    if ($w->main) {
	each_index { $_->get_active and $urpm->select_media($urpm->{media}[$::i]{name}) } @buttons;
	foreach (@{$urpm->{media}}) {  #- force ignored media to be returned alive (forked from urpmi.updatemedia...)
	    $_->{modified} and delete $_->{ignore};
	}
	slow_func(_("Please wait, updating media..."),
		  sub { $urpm->update_media(noclean => 1) });
    }
}

sub proxy_callback {
    my $w = my_gtk->new(_("Configure proxies"));
    my ($proxy, $proxy_user) = curl_download::readproxy;
    my ($user, $pass) = $proxy_user =~ /^(.+):(.+)$/;
    gtkadd($w->{window},
	   gtkpack__(new Gtk::VBox(0, 5),
		     new Gtk::Label(_("If you need a proxy, enter the hostname and an optional port (syntax: <proxyhost[:port]>):")),
		     gtkpack_(new Gtk::HBox(0, 10),
			      0, gtkset_active(my $proxybutton = new Gtk::CheckButton(_("Proxy hostname:")), to_bool($proxy)),
			      1, gtkset_sensitive(my $proxyentry = gtkentry($proxy), to_bool($proxy))),
		     new Gtk::Label(_("You may specify a user/password for the proxy authentication:")),
		     gtkpack_(new Gtk::HBox(0, 10),
			      0, gtkset_active(my $proxyuserbutton = new Gtk::CheckButton(_("User:")), to_bool($proxy_user)),
			      1, gtkset_sensitive(my $proxyuserentry = gtkentry($user), to_bool($proxy_user)),
			      0, new Gtk::Label(_("Password:")),
			      1, gtkset_sensitive(my $proxypasswordentry = gtkentry($pass), to_bool($proxy_user))),
		     new Gtk::HSeparator,
		     gtkpack(create_hbox(),
			     gtksignal_connect(new Gtk::Button(_("Ok")), clicked => sub { $w->{retval} = 1; Gtk->main_quit }),
			     gtksignal_connect(new Gtk::Button(_("Cancel")), clicked => sub { $w->{retval} = 0; Gtk->main_quit }))));
    $proxybutton->signal_connect(clicked => sub { $proxyentry->set_sensitive($_[0]->get_active);
						  $_[0]->get_active and return;
						  $proxyuserbutton->set_active(0);
						  $proxyuserentry->set_sensitive(0);
						  $proxypasswordentry->set_sensitive(0); });
    $proxyuserbutton->signal_connect(clicked => sub { $proxyuserentry->set_sensitive($_[0]->get_active);
						      $proxypasswordentry->set_sensitive($_[0]->get_active) });
    $w->{rwindow}->set_position('center');
    if ($w->main) {
	curl_download::writeproxy($proxybutton->get_active ? $proxyentry->get_text : '',
				  $proxyuserbutton->get_active ? ($proxyuserentry->get_text.':'.$proxypasswordentry->get_text) : '');
    }
}

sub mainwindow {
    my %pixmaps = (selected => [ gtkcreate_png('selected') ], unselected => [ gtkcreate_png('unselected') ]);
    my $mainw = my_gtk->new(_("Configure sources"));
    $clist = new_with_titles Gtk::CList(_("Enabled?"), _("Source"));
    $clist->set_column_auto_resize($_, 1) foreach qw(0 1);
    $clist->set_column_justification(0, 'center');
    $clist->signal_connect(button_press_event => sub { my ($row, $col) = $clist->get_selection_info($_[1]->{x}, $_[1]->{'y'});
						       if ($col == 0 && $row =~ /^\d+$/) {
							   invbool(\$urpm->{media}[$row]{ignore});
							   my $pix = $pixmaps{$urpm->{media}[$row]{ignore} ? 'unselected' : 'selected'};
							   $clist->set_pixmap($row, 0, $pix->[0], $pix->[1]);
						       }
						   });
    $clist->signal_connect(select_row => sub { $$_->set_sensitive(1) foreach (\$remove, \$edit) });
    $clist->signal_connect(unselect_row => sub { $$_->set_sensitive(0) foreach (\$remove, \$edit) });

    my $reread_media = sub {
	$clist->clear;
	foreach (@{$urpm->{media}}) {
	    $clist->append('', $_->{name});
	    my $pix = $pixmaps{$_->{ignore} ? 'unselected' : 'selected'};
	    $clist->set_pixmap($clist->rows-1, 0, $pix->[0], $pix->[1]);
	}
    };
    $reread_media->();

    gtkadd($mainw->{window},
	   gtkpack_(new Gtk::VBox(0,5),
		    1, gtkpack_(new Gtk::HBox(0, 10),
				1, $clist,
				0, gtkpack__(new Gtk::VBox(0, 5),
					     gtkset_sensitive(gtksignal_connect($remove = new Gtk::Button(but(_("Remove"))),
										clicked => sub { remove_callback; $reread_media->(); }), 0),
					     gtkset_sensitive(gtksignal_connect($edit = new Gtk::Button(but(_("Edit"))),
										clicked => sub { edit_callback and $reread_media->() }), 0),
					     gtksignal_connect(new Gtk::Button(but(_("Add..."))), 
							       clicked => sub { add_callback and $reread_media->(); }),
					     gtksignal_connect(new Gtk::Button(but(_("Update..."))), clicked => \&update_callback),
					     gtksignal_connect(new Gtk::Button(but(_("Proxy..."))), clicked => \&proxy_callback))),
		    0, new Gtk::HSeparator,
		    0, gtkpack(create_hbox(),
			       gtksignal_connect(new Gtk::Button(_("Save and quit")), clicked => sub { $mainw->{retval} = 1; Gtk->main_quit }),
			       gtksignal_connect(new Gtk::Button(_("Quit")), clicked => sub { $mainw->{retval} = 0; Gtk->main_quit }))));
    $mainw->{rwindow}->set_position('center');
    $mainw->main;
}


readconf;

if (!member(basename($0), @$already_splashed)) {
    interactive_msg('rpmdrake',
_("%s

Is it ok to continue?",
_("Welcome to the packages source editor!

This tool will help you configure the packages sources you wish to use on
your computer. They will then be available to install new software package
or to perform updates.")), yesno => 1) or myexit -1;
    push @$already_splashed, basename($0);
}

if (mainwindow()) {
    $urpm->write_config;
}

writeconf;

myexit 0;