| 1 | # $Id: Progress.pm 2187 2006-08-16 19:34:38Z joern $ | ||
| 2 | |||
| 3 | #----------------------------------------------------------------------- | ||
| 4 | # Copyright (C) 2001-2006 Jörn Reder <joern AT zyn.de>. | ||
| 5 | # All Rights Reserved. See file COPYRIGHT for details. | ||
| 6 | # | ||
| 7 | # This module is part of Video::DVDRip, which is free software; you can | ||
| 8 | # redistribute it and/or modify it under the same terms as Perl itself. | ||
| 9 | #----------------------------------------------------------------------- | ||
| 10 | |||
| 11 | package Video::DVDRip::GUI::Progress; | ||
| 12 | |||
| 13 | use base qw( Video::DVDRip::GUI::Base ); | ||
| 14 | |||
| 15 | use Locale::TextDomain qw (video.dvdrip); | ||
| 16 | |||
| 17 | use strict; | ||
| 18 | use Carp; | ||
| 19 | use Data::Dumper; | ||
| 20 | use Cwd; | ||
| 21 | |||
| 22 | use POSIX qw(:errno_h); | ||
| 23 | |||
| 24 | sub cb_cancel { shift->{cb_cancel} } | ||
| 25 | sub is_active { shift->{is_active} } | ||
| 26 | sub progress_state { shift->{progress_state} } | ||
| 27 | sub gtk_progress { shift->{gtk_progress} } | ||
| 28 | sub max_value { shift->{max_value} } | ||
| 29 | sub details_ff { shift->{details_ff} } | ||
| 30 | sub paused { shift->{paused} } | ||
| 31 | sub show_details { shift->{show_details} } | ||
| 32 | |||
| 33 | sub set_cb_cancel { shift->{cb_cancel} = $_[1] } | ||
| 34 | sub set_is_active { shift->{is_active} = $_[1] } | ||
| 35 | sub set_progress_state { shift->{progress_state} = $_[1] } | ||
| 36 | sub set_gtk_progress { shift->{gtk_progress} = $_[1] } | ||
| 37 | sub set_max_value { shift->{max_value} = $_[1] } | ||
| 38 | sub set_details_ff { shift->{details_ff} = $_[1] } | ||
| 39 | sub set_paused { shift->{paused} = $_[1] } | ||
| 40 | sub set_show_details { shift->{show_details} = $_[1] } | ||
| 41 | |||
| 42 | sub build_factory { | ||
| 43 | my $self = shift; | ||
| 44 | |||
| 45 | $self->get_context->set_object( "progress" => $self ); | ||
| 46 | |||
| 47 | my $progress = Gtk2::Ex::FormFactory::Form->new( | ||
| 48 | title => __ "Status", | ||
| 49 | object => "project", | ||
| 50 | content => [ | ||
| 51 | Gtk2::Ex::FormFactory::HBox->new( | ||
| 52 | active_cond => sub { $self->is_active }, | ||
| 53 | active_depends => "progress.is_active", | ||
| 54 | content => [ | ||
| 55 | Gtk2::Ex::FormFactory::ProgressBar->new( | ||
| 56 | name => "progress", | ||
| 57 | attr => "progress.progress_state", | ||
| 58 | expand => 1, | ||
| 59 | ), | ||
| 60 | Gtk2::Ex::FormFactory::ToggleButton->new( | ||
| 61 | attr => "progress.show_details", | ||
| 62 | tip => __ "Show job plan and progress details", | ||
| 63 | active => 0, | ||
| 64 | true_label => "", | ||
| 65 | false_label => "", | ||
| 66 | stock => "gtk-zoom-in", | ||
| 67 | changed_hook_after => sub { | ||
| 68 | $self->toggle_details_window; | ||
| 69 | 1; | ||
| 70 | }, | ||
| 71 | ), | ||
| 72 | Gtk2::Ex::FormFactory::ToggleButton->new( | ||
| 73 | attr => "progress.paused", | ||
| 74 | name => "progress_pause", | ||
| 75 | stock => "gtk-media-pause", | ||
| 76 | tip => __ "Pause and resume processing", | ||
| 77 | label => "", | ||
| 78 | false_label => "", | ||
| 79 | true_label => "", | ||
| 80 | changed_hook => sub { | ||
| 81 | my $job = $self->get_context->get_object_attr( | ||
| 82 | "exec_flow_gui.job"); | ||
| 83 | $job->pause; | ||
| 84 | 1; | ||
| 85 | }, | ||
| 86 | ), | ||
| 87 | Gtk2::Ex::FormFactory::Button->new( | ||
| 88 | name => "progress_cancel", | ||
| 89 | stock => "gtk-cancel", | ||
| 90 | tip => __ "Cancel processing", | ||
| 91 | label => "", | ||
| 92 | clicked_hook => sub { | ||
| 93 | my $cb_cancel = $self->cb_cancel; | ||
| 94 | &$cb_cancel() if $cb_cancel; | ||
| 95 | 1; | ||
| 96 | }, | ||
| 97 | ), | ||
| 98 | ], | ||
| 99 | ), | ||
| 100 | ], | ||
| 101 | ); | ||
| 102 | |||
| 103 | return $progress; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub open { | ||
| 107 | my $self = shift; | ||
| 108 | my %par = @_; | ||
| 109 | my ( $max_value, $label, $cb_cancel ) | ||
| 110 | = @par{ 'max_value', 'label', 'cb_cancel' }; | ||
| 111 | |||
| 112 | $self->set_gtk_progress( | ||
| 113 | $self->get_form_factory->get_widget("progress")->get_gtk_widget, | ||
| 114 | ); | ||
| 115 | |||
| 116 | $self->set_is_active(1); | ||
| 117 | $self->set_max_value($max_value); | ||
| 118 | $self->set_cb_cancel($cb_cancel); | ||
| 119 | $self->set_paused(0); | ||
| 120 | |||
| 121 | $self->get_context->update_object_attr_widgets("progress.is_active"); | ||
| 122 | $self->get_context->update_object_attr_widgets("progress.paused"); | ||
| 123 | |||
| 124 | $self->details_ff->update if $self->details_ff; | ||
| 125 | |||
| 126 | 1; | ||
| 127 | } | ||
| 128 | |||
| 129 | sub update { | ||
| 130 | my $self = shift; | ||
| 131 | my %par = @_; | ||
| 132 | my ( $value, $label ) = @par{ 'value', 'label' }; | ||
| 133 | |||
| 134 | $value = 0 if $value < 0; | ||
| 135 | $value = 1 if $value > 1; | ||
| 136 | |||
| 137 | $self->gtk_progress->set_text($label); | ||
| 138 | $self->gtk_progress->set_fraction($value); | ||
| 139 | |||
| 140 | 1; | ||
| 141 | } | ||
| 142 | |||
| 143 | sub close { | ||
| 144 | my $self = shift; | ||
| 145 | |||
| 146 | $self->gtk_progress->set_fraction(0); | ||
| 147 | |||
| 148 | $self->set_is_active(0); | ||
| 149 | $self->set_idle_label; | ||
| 150 | |||
| 151 | $self->get_context->update_object_attr_widgets("progress.is_active"); | ||
| 152 | |||
| 153 | if ( $self->details_ff ) { | ||
| 154 | $self->details_ff->get_widget("progress_detail_buttons")->update_all; | ||
| 155 | } | ||
| 156 | |||
| 157 | 1; | ||
| 158 | } | ||
| 159 | |||
| 160 | sub cancel { | ||
| 161 | my $self = shift; | ||
| 162 | |||
| 163 | my $cb_cancel = $self->cb_cancel; | ||
| 164 | |||
| 165 | &$cb_cancel() if $cb_cancel; | ||
| 166 | |||
| 167 | $self->close; | ||
| 168 | |||
| 169 | 1; | ||
| 170 | } | ||
| 171 | |||
| 172 | sub set_idle_label { | ||
| 173 | my $self = shift; | ||
| 174 | |||
| 175 | my $project = eval { $self->project }; | ||
| 176 | |||
| 177 | my $label; | ||
| 178 | if ($project) { | ||
| 179 | my $free = $project->get_free_diskspace; | ||
| 180 | $label = __x( "Free diskspace: {free} MB", free => $free ); | ||
| 181 | } | ||
| 182 | else { | ||
| 183 | $label = ""; | ||
| 184 | } | ||
| 185 | |||
| 186 | $self->gtk_progress->set_text($label); | ||
| 187 | |||
| 188 | 1; | ||
| 189 | } | ||
| 190 | |||
| 191 | sub toggle_details_window { | ||
| 192 | my $self = shift; | ||
| 193 | |||
| 194 | $self->build_details_ff if $self->show_details && !$self->details_ff; | ||
| 195 | |||
| 196 | 1; | ||
| 197 | } | ||
| 198 | |||
| 199 | sub build_details_ff { | ||
| 200 | my $self = shift; | ||
| 201 | |||
| 202 | my $gtk_window = $self->get_form_factory->get_form_factory_gtk_window; | ||
| 203 | |||
| 204 | my $ff = Gtk2::Ex::FormFactory->new( | ||
| 205 | context => $self->get_context, | ||
| 206 | content => [ | ||
| 207 | Gtk2::Ex::FormFactory::Window->new( | ||
| 208 | title => __"dvd::rip - Job plan and progress details", | ||
| 209 | inactive => "invisible", | ||
| 210 | active_cond => sub { $self->show_details }, | ||
| 211 | active_depends => "progress.show_details", | ||
| 212 | content => [ | ||
| 213 | Gtk2::Ex::FormFactory::ExecFlow->new( | ||
| 214 | name => "exec_flow", | ||
| 215 | attr => "exec_flow_gui.job", | ||
| 216 | scrollbars => [ 'automatic', 'automatic' ], | ||
| 217 | width => 640, | ||
| 218 | height => 300, | ||
| 219 | expand => 1, | ||
| 220 | # add_columns => [ "diskspace_consumed", "diskspace_freed" ], | ||
| 221 | ), | ||
| 222 | Gtk2::Ex::FormFactory::HBox->new( | ||
| 223 | name => "progress_detail_buttons", | ||
| 224 | properties => { homogeneous => 1 }, | ||
| 225 | content => [ | ||
| 226 | Gtk2::Ex::FormFactory::Label->new( | ||
| 227 | label => " ", | ||
| 228 | expand => 1, | ||
| 229 | ), | ||
| 230 | Gtk2::Ex::FormFactory::Button->new( | ||
| 231 | stock => "gtk-zoom-out", | ||
| 232 | label => __ "Hide window", | ||
| 233 | tip => __ "Hide details window", | ||
| 234 | clicked_hook => sub { | ||
| 235 | $self->get_context->set_object_attr( | ||
| 236 | "progress.show_details", 0 ); | ||
| 237 | }, | ||
| 238 | ), | ||
| 239 | Gtk2::Ex::FormFactory::ToggleButton->new( | ||
| 240 | attr => "progress.paused", | ||
| 241 | name => "details_progress_pause", | ||
| 242 | stock => "gtk-media-pause", | ||
| 243 | false_label => __ "Pause jobs", | ||
| 244 | true_label => __ "Resume", | ||
| 245 | tip => __ "Pause and resume processing", | ||
| 246 | changed_hook => sub { | ||
| 247 | my $job | ||
| 248 | = $self->get_context->get_object_attr( | ||
| 249 | "exec_flow_gui.job"); | ||
| 250 | $job->pause; | ||
| 251 | 1; | ||
| 252 | }, | ||
| 253 | active_cond => sub { $self->is_active }, | ||
| 254 | active_depends => "progress.is_active", | ||
| 255 | ), | ||
| 256 | Gtk2::Ex::FormFactory::Button->new( | ||
| 257 | name => "details_progress_cancel", | ||
| 258 | stock => "gtk-cancel", | ||
| 259 | label => __ "Cancel jobs", | ||
| 260 | tip => __ "Cancel all running jobs", | ||
| 261 | clicked_hook => sub { | ||
| 262 | my $cb_cancel = $self->cb_cancel; | ||
| 263 | &$cb_cancel() if $cb_cancel; | ||
| 264 | 1; | ||
| 265 | }, | ||
| 266 | active_cond => sub { $self->is_active }, | ||
| 267 | active_depends => "progress.is_active", | ||
| 268 | ), | ||
| 269 | ], | ||
| 270 | ), | ||
| 271 | ], | ||
| 272 | closed_hook => sub { | ||
| 273 | $self->get_context->set_object_attr( | ||
| 274 | "progress.show_details", 0 ); | ||
| 275 | $self->set_details_ff(undef); | ||
| 276 | }, | ||
| 277 | ), | ||
| 278 | ], | ||
| 279 | ); | ||
| 280 | |||
| 281 | $ff->build; | ||
| 282 | $ff->update; | ||
| 283 | $ff->show; | ||
| 284 | |||
| 285 | $self->set_details_ff($ff); | ||
| 286 | |||
| 287 | 1; | ||
| 288 | } | ||
| 289 | |||
| 290 | 1; |