| 1 | package gurpm::RPMProgressDialog; | ||
| 2 | |||
| 3 | #- Copyright (C) 2005 MandrakeSoft SA | ||
| 4 | #- Copyright (C) 2005-2010 Mandriva SA | ||
| 5 | #- Copyright (C) 2011-2020 Mageia | ||
| 6 | |||
| 7 | # Sharing code from gurpmi2 && Rpmdrake::gurpm | ||
| 8 | # Gtk3 only (no ugtk3/mygtk3) as it's used by gurpmi too... | ||
| 9 | # | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | gurpm::RPMProgressDialog is a widget for gurpmi, rpmdrake (drakx already has its own) | ||
| 14 | that presents a global progress bar. | ||
| 15 | |||
| 16 | =head1 USAGE | ||
| 17 | |||
| 18 | How to use: | ||
| 19 | |||
| 20 | my $w = gurpm::RPMProgressDialog->new: | ||
| 21 | #$w->change_widget(Gtk3::Box->new); | ||
| 22 | label(N("Preparing packages installation...")); | ||
| 23 | ... # compute packages to install/remove... | ||
| 24 | $w->init_progressbar; | ||
| 25 | urpm::main_loop::run($urpm, $state, $nb, \@ask_unselect, { | ||
| 26 | trans_log => \&gurpm::RPMProgressDialog::callback_download, | ||
| 27 | pre_check_sig => \&gurpm::RPMProgressDialog::pre_check_sig, | ||
| 28 | inst => \&gurpm::RPMProgressDialog::callback_inst, | ||
| 29 | trans => \&gurpm::RPMProgressDialog::callback_inst, | ||
| 30 | uninst => \&gurpm::RPMProgressDialog::callback_inst, | ||
| 31 | .... | ||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | use strict; | ||
| 38 | use Gtk3; | ||
| 39 | use urpm::download; | ||
| 40 | use urpm::msg 'N'; | ||
| 41 | use urpm::util qw(max member); | ||
| 42 | use Scalar::Util qw(weaken); | ||
| 43 | |||
| 44 | our @ISA = qw(Gtk3::Window); | ||
| 45 | |||
| 46 | sub title { | ||
| 47 | $::auto_select ? N("Distribution Upgrade") : N("Packages installation"); | ||
| 48 | } | ||
| 49 | |||
| 50 | # package variable needed in order to simplify callbacks | ||
| 51 | my ($mainw, $urpm, $old_main_window); | ||
| 52 | |||
| 53 | my $progressbar_size = 450; | ||
| 54 | my ($progress_nb, $download_nb, $index); | ||
| 55 | |||
| 56 | |||
| 57 | =head2 Creators | ||
| 58 | |||
| 59 | =over 4 | ||
| 60 | |||
| 61 | =item gurpm::RPMProgressDialog->new($urpm, $o_quit) | ||
| 62 | |||
| 63 | Creates a new Progress Dialog. | ||
| 64 | |||
| 65 | Arguments are an urpm object and a quit routine reference. | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | |||
| 70 | sub new { | ||
| 71 | my ($self, $global_urpm, $o_quit, $o_no_modal) = @_; | ||
| 72 | # my $w = ugtk3->new($title, %options, default_width => 600, width => 600); | ||
| 73 | my $w = $mainw = bless(Gtk3::Window->new('toplevel'), $self); | ||
| 74 | |||
| 75 | $old_main_window = $::main_window; | ||
| 76 | $::main_window = $w; | ||
| 77 | $w->set_border_width(12); | ||
| 78 | $w->set_title($w->title); | ||
| 79 | $w->signal_connect(destroy => $o_quit) if $o_quit; | ||
| 80 | $w->set_position('center_always'); | ||
| 81 | $w->set_default_size($progressbar_size, 60); | ||
| 82 | # for matchbox window manager during install: | ||
| 83 | if (!$o_no_modal) { | ||
| 84 | $w->set_type_hint('dialog'); | ||
| 85 | $w->set_modal(1); | ||
| 86 | } | ||
| 87 | $w->{mainbox} = Gtk3::VBox->new(0, 5); | ||
| 88 | $w->add($w->{mainbox}); | ||
| 89 | $urpm = $global_urpm; | ||
| 90 | |||
| 91 | # Prevent cycle: | ||
| 92 | weaken($urpm); # fixes GC but not ideal | ||
| 93 | weaken($mainw); | ||
| 94 | weaken($::main_window); | ||
| 95 | |||
| 96 | bless($w, $self); | ||
| 97 | } | ||
| 98 | |||
| 99 | =back | ||
| 100 | |||
| 101 | =head2 Methods | ||
| 102 | |||
| 103 | =over 4 | ||
| 104 | |||
| 105 | =item change_widget($w, $box_widget) | ||
| 106 | |||
| 107 | Replaces the contents of the main window with the specified box | ||
| 108 | (avoids popup multiplication) | ||
| 109 | |||
| 110 | =cut | ||
| 111 | |||
| 112 | sub change_widget { | ||
| 113 | my ($w, $mainbox) = @_; | ||
| 114 | $w->remove($w->{mainbox}); | ||
| 115 | $w->add($w->{mainbox} = $mainbox); | ||
| 116 | $w->show_all; | ||
| 117 | } | ||
| 118 | |||
| 119 | =item label($w, $o_text) | ||
| 120 | |||
| 121 | sets the window to a please-wait message | ||
| 122 | |||
| 123 | =cut | ||
| 124 | |||
| 125 | sub label { | ||
| 126 | my ($w, $o_text) = @_; | ||
| 127 | my $wait_vbox = Gtk3::VBox->new(0, 5); | ||
| 128 | my $label = Gtk3::Label->new($o_text || N("Please wait...")); | ||
| 129 | $label->set_alignment(0.5, 0.5); | ||
| 130 | $wait_vbox->pack_start($label, 1, 1, 0); | ||
| 131 | $w->change_widget($wait_vbox); | ||
| 132 | $w->sync; | ||
| 133 | } | ||
| 134 | |||
| 135 | |||
| 136 | # From ugtk3.pm/mygtk3.pm: | ||
| 137 | sub gtk_new_Label_Left { | ||
| 138 | my ($text) = @_; | ||
| 139 | my $w = Gtk3::Label->new($text); | ||
| 140 | $w->set_alignment(0, 0); | ||
| 141 | $w; | ||
| 142 | } | ||
| 143 | |||
| 144 | =item init_progressbar($w) | ||
| 145 | |||
| 146 | Put a progress bar in the dialog. | ||
| 147 | |||
| 148 | =cut | ||
| 149 | |||
| 150 | sub init_progressbar { | ||
| 151 | my ($w) = @_; | ||
| 152 | my $vbox = Gtk3::VBox->new(0, 5); | ||
| 153 | |||
| 154 | my $global_label = gtk_new_Label_Left('<b>' . $w->title . '</b>'); | ||
| 155 | $global_label->set_use_markup(1); | ||
| 156 | $vbox->pack_start($global_label, 0, 0, 0); | ||
| 157 | |||
| 158 | my $global_progressbar = $w->{global_progressbar} = Gtk3::ProgressBar->new; | ||
| 159 | $vbox->pack_start($global_progressbar, 0, 0, 0); | ||
| 160 | |||
| 161 | $vbox->pack_start($w->{progresslabel} = gtk_new_Label_Left('-'), 1, 1, 0); | ||
| 162 | |||
| 163 | my $progressbar = Gtk3::ProgressBar->new; | ||
| 164 | $progressbar->set_size_request($progressbar_size, -1); | ||
| 165 | $vbox->pack_start($progressbar, 0, 0, 0); | ||
| 166 | $w->{progressbar} = $progressbar; | ||
| 167 | $progress_nb = $download_nb = $index = 0; | ||
| 168 | |||
| 169 | $w->change_widget($vbox); | ||
| 170 | } | ||
| 171 | |||
| 172 | =item set_progresslabel($w, $text) | ||
| 173 | |||
| 174 | Update the progress label | ||
| 175 | |||
| 176 | =cut | ||
| 177 | |||
| 178 | sub set_progresslabel { | ||
| 179 | my ($w, $text) = @_; | ||
| 180 | $w->{progresslabel}->set_label($text); | ||
| 181 | } | ||
| 182 | |||
| 183 | =item set_progressbar($w, $local_ratio) | ||
| 184 | |||
| 185 | Update the progress bar | ||
| 186 | |||
| 187 | =cut | ||
| 188 | |||
| 189 | sub set_progressbar { | ||
| 190 | my ($w, $local_ratio) = @_; | ||
| 191 | if ($progress_nb || $download_nb) { # this happens when computing transaction | ||
| 192 | $w->{global_progressbar}->set_fraction(($download_nb + $progress_nb - 1 + $local_ratio) / 2 / $urpm->{nb_install}); | ||
| 193 | } | ||
| 194 | $w->{progressbar}->set_fraction($local_ratio); | ||
| 195 | } | ||
| 196 | |||
| 197 | =item validate_cancel($self, $cancel_msg, $cancel_cb) | ||
| 198 | |||
| 199 | Add a "Cancel" button, with $cancel_msg as label, calling | ||
| 200 | $cancel_cb when clicked. | ||
| 201 | |||
| 202 | =cut | ||
| 203 | |||
| 204 | sub validate_cancel { | ||
| 205 | my ($self, $cancel_msg, $cancel_cb) = @_; | ||
| 206 | if (!$self->{cancel}) { | ||
| 207 | $self->{hbox_cancel} = Gtk3::HButtonBox->new; | ||
| 208 | $self->{hbox_cancel}->add($self->{cancel} = Gtk3::Button->new($cancel_msg)); | ||
| 209 | $self->{mainbox}->add($self->{hbox_cancel}); | ||
| 210 | $self->{cancel}->signal_connect('clicked' => \&$cancel_cb); | ||
| 211 | $self->{hbox_cancel}->show_all; | ||
| 212 | } | ||
| 213 | $self->{cancel}->set_sensitive(1); | ||
| 214 | $self->{cancel}->show; | ||
| 215 | } | ||
| 216 | |||
| 217 | =item invalidate_cancel($self) | ||
| 218 | |||
| 219 | Disable the "Cancel" button if any. | ||
| 220 | |||
| 221 | =cut | ||
| 222 | |||
| 223 | sub invalidate_cancel { | ||
| 224 | my ($self) = @_; | ||
| 225 | $self->{cancel} and $self->{cancel}->set_sensitive(0); | ||
| 226 | } | ||
| 227 | |||
| 228 | =item invalidate_cancel($self) | ||
| 229 | |||
| 230 | Disable the "Cancel" button if any and make it insensitive so that it'll never | ||
| 231 | be usable again. | ||
| 232 | |||
| 233 | =cut | ||
| 234 | |||
| 235 | |||
| 236 | sub invalidate_cancel_forever { | ||
| 237 | my ($self) = @_; | ||
| 238 | $self->{hbox_cancel} or return; | ||
| 239 | $self->{hbox_cancel}->destroy; | ||
| 240 | } | ||
| 241 | |||
| 242 | |||
| 243 | =item sync($w) | ||
| 244 | |||
| 245 | tell Gtk+ to refresh the dialog content if needed. | ||
| 246 | |||
| 247 | =cut | ||
| 248 | |||
| 249 | sub sync { | ||
| 250 | my ($w) = @_; | ||
| 251 | $w->show; | ||
| 252 | Gtk3::main_iteration while Gtk3::events_pending; | ||
| 253 | } | ||
| 254 | |||
| 255 | =item gurpm::RPMProgressDialog::get_something_done() | ||
| 256 | |||
| 257 | Whether some package has been installed or removed | ||
| 258 | |||
| 259 | =cut | ||
| 260 | |||
| 261 | sub get_something_done() { | ||
| 262 | $progress_nb; | ||
| 263 | } | ||
| 264 | |||
| 265 | =item canceled($w) | ||
| 266 | |||
| 267 | Whether downloading has been canceled or not. | ||
| 268 | |||
| 269 | =cut | ||
| 270 | |||
| 271 | sub canceled { | ||
| 272 | my ($w) = @_; | ||
| 273 | $w->{canceled}; | ||
| 274 | } | ||
| 275 | |||
| 276 | =back | ||
| 277 | |||
| 278 | =head2 Callbacks | ||
| 279 | |||
| 280 | =over 4 | ||
| 281 | |||
| 282 | =item callback_pre_check_sig() | ||
| 283 | |||
| 284 | This callback is called when checking packages before installing them. | ||
| 285 | |||
| 286 | Its purpose is to display installation progress in the dialog. | ||
| 287 | |||
| 288 | =cut | ||
| 289 | |||
| 290 | sub callback_pre_check_sig { | ||
| 291 | $mainw->set_progresslabel(N("Verifying package signatures...")); | ||
| 292 | $mainw->sync; | ||
| 293 | } | ||
| 294 | |||
| 295 | =item callback_inst($urpm, $type, $id, $subtype, $amount, $total) | ||
| 296 | |||
| 297 | This callback is called when a new RPM DB transaction is created and | ||
| 298 | when packages are installed. | ||
| 299 | |||
| 300 | Its purpose is to display installation progress in the dialog. | ||
| 301 | |||
| 302 | =cut | ||
| 303 | |||
| 304 | sub callback_inst { | ||
| 305 | my ($urpm, $type, $id, $subtype, $amount, $total) = @_; | ||
| 306 | my $pkg = defined $id ? $urpm->{depslist}[$id] : undef; | ||
| 307 | if ($subtype eq 'start') { | ||
| 308 | if ($type eq 'trans') { | ||
| 309 | $index = 0; | ||
| 310 | $mainw->set_progresslabel(N("Preparing...")); | ||
| 311 | } else { | ||
| 312 | my $msg; | ||
| 313 | if ($type eq 'uninst') { | ||
| 314 | $msg = N("Removing package `%s' ...", $urpm->{trans}->Element_fullname($index)); | ||
| 315 | } else { | ||
| 316 | $progress_nb++; | ||
| 317 | $msg = N("Installing package `%s' (%s/%s)...", $pkg->name, $progress_nb, $urpm->{nb_install}); | ||
| 318 | } | ||
| 319 | $index++; | ||
| 320 | $download_nb = max($download_nb, $progress_nb); | ||
| 321 | $mainw->set_progressbar(0); | ||
| 322 | $mainw->set_progresslabel($msg); | ||
| 323 | } | ||
| 324 | } elsif ($subtype eq 'progress') { | ||
| 325 | $mainw->set_progressbar($amount / $total); | ||
| 326 | } | ||
| 327 | $mainw->sync; | ||
| 328 | } | ||
| 329 | |||
| 330 | =item callback_download($mode, $file, $percent, $total, $eta, $speed) | ||
| 331 | |||
| 332 | This callback is called when packages are downloaded prior being installed | ||
| 333 | in a RPM transaction. | ||
| 334 | |||
| 335 | Its purpose is to display download progress in the dialog. | ||
| 336 | |||
| 337 | =cut | ||
| 338 | |||
| 339 | sub callback_download { | ||
| 340 | my ($mode, $file, $percent, $total, $eta, $speed) = @_; | ||
| 341 | |||
| 342 | urpm::download::sync_logger($mode, $file, $percent, $total, $eta, $speed); | ||
| 343 | |||
| 344 | if (member($mode, 'start', 'progress')) { | ||
| 345 | $file =~ s|/*\s*$||; $file =~ s|.*/||; | ||
| 346 | $mainw->set_progresslabel(N("Downloading package `%s'...", $file) . "\n" . | ||
| 347 | &urpm::download::progress_text($mode, $percent, $total, $eta, $speed)); | ||
| 348 | } | ||
| 349 | if ($mode eq 'start') { | ||
| 350 | $mainw->validate_cancel(N("Cancel"), \&callback_cancel); | ||
| 351 | $download_nb++; | ||
| 352 | $mainw->set_progressbar(0); | ||
| 353 | select(undef, undef, undef, 0.1); #- hackish | ||
| 354 | } elsif ($mode eq 'progress') { | ||
| 355 | $mainw->set_progressbar($percent / 100); | ||
| 356 | } elsif ($mode eq 'end') { | ||
| 357 | $mainw->set_progressbar(1); | ||
| 358 | } elsif ($mode eq 'error') { | ||
| 359 | #- error is 3rd argument, saved in $percent | ||
| 360 | push @{$urpm->{download_errors}}, N("...retrieving failed: %s", $percent); | ||
| 361 | } | ||
| 362 | $mainw->sync; | ||
| 363 | } | ||
| 364 | |||
| 365 | =item callback_cancel() | ||
| 366 | |||
| 367 | This callback is to be called when canceling packages download. | ||
| 368 | |||
| 369 | =cut | ||
| 370 | |||
| 371 | sub callback_cancel { | ||
| 372 | $mainw->{canceled} = 1; | ||
| 373 | } | ||
| 374 | |||
| 375 | |||
| 376 | sub DESTROY { | ||
| 377 | my ($self) = @_; | ||
| 378 | undef $mainw; | ||
| 379 | undef $urpm; | ||
| 380 | $::main_window = $old_main_window; | ||
| 381 | |||
| 382 | $self = undef; | ||
| 383 | } | ||
| 384 | |||
| 385 | |||
| 386 | =back | ||
| 387 | |||
| 388 | =head1 Copyright | ||
| 389 | |||
| 390 | Copyright (C) 2005 MandrakeSoft SA | ||
| 391 | |||
| 392 | Copyright (C) 2005-2010 Mandriva SA | ||
| 393 | |||
| 394 | Copyright (C) 2011-2020 Mageia | ||
| 395 | |||
| 396 | =cut | ||
| 397 | |||
| 398 | 1; |