| 1 | # $Id: Preferences.pm 2343 2007-08-09 21:36:08Z 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::Preferences; | ||
| 12 | use Locale::TextDomain qw (video.dvdrip); | ||
| 13 | |||
| 14 | use base qw(Video::DVDRip::GUI::Base); | ||
| 15 | |||
| 16 | use strict; | ||
| 17 | use Carp; | ||
| 18 | |||
| 19 | my $config_ff; | ||
| 20 | |||
| 21 | sub page2params { shift->{page2params} } | ||
| 22 | sub set_page2params { shift->{page2params} = $_[1] } | ||
| 23 | |||
| 24 | sub gtk_notebook { shift->{gtk_notebook} } | ||
| 25 | sub set_gtk_notebook { shift->{gtk_notebook} = $_[1] } | ||
| 26 | |||
| 27 | sub gtk_text_buffer { shift->{gtk_text_buffer} } | ||
| 28 | sub set_gtk_text_buffer { shift->{gtk_text_buffer} = $_[1] } | ||
| 29 | |||
| 30 | sub open_window { | ||
| 31 | my $self = shift; | ||
| 32 | |||
| 33 | return if $config_ff; | ||
| 34 | |||
| 35 | my $config = $self->config_object; | ||
| 36 | my $clone = $config->clone; | ||
| 37 | |||
| 38 | $self->get_context->set_object( config => $clone ); | ||
| 39 | |||
| 40 | $self->build; | ||
| 41 | |||
| 42 | 1; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub build { | ||
| 46 | my $self = shift; | ||
| 47 | |||
| 48 | my $context = $self->get_context; | ||
| 49 | |||
| 50 | $config_ff = Gtk2::Ex::FormFactory->new( | ||
| 51 | context => $context, | ||
| 52 | parent_ff => $self->get_form_factory, | ||
| 53 | sync => 1, | ||
| 54 | content => [ | ||
| 55 | Gtk2::Ex::FormFactory::Window->new( | ||
| 56 | title => __ "dvd::rip - Preferences", | ||
| 57 | customize_hook => sub { | ||
| 58 | my ($gtk_window) = @_; | ||
| 59 | $_[0]->parent->set( | ||
| 60 | default_width => 640, | ||
| 61 | default_height => 460, | ||
| 62 | ); | ||
| 63 | 1; | ||
| 64 | }, | ||
| 65 | properties => { modal => 1, }, | ||
| 66 | closed_hook => sub { | ||
| 67 | $config_ff->close if $config_ff; | ||
| 68 | $config_ff = undef; | ||
| 69 | $self->get_context->set_object( config => undef ); | ||
| 70 | 1; | ||
| 71 | }, | ||
| 72 | content => [ | ||
| 73 | Gtk2::Ex::FormFactory::Table->new( | ||
| 74 | title => __ "Global preferences", | ||
| 75 | expand => 1, | ||
| 76 | layout => " | ||
| 77 | +>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+ | ||
| 78 | | Notebook | | ||
| 79 | | | | ||
| 80 | +---------------------------------+ | ||
| 81 | ^ Check results | | ||
| 82 | +--------------------------------]+ | ||
| 83 | | Buttons | | ||
| 84 | +---------------------------------+ | ||
| 85 | ", | ||
| 86 | content => [ | ||
| 87 | $self->build_config_notebook, | ||
| 88 | $self->build_config_check_results, | ||
| 89 | $self->build_config_buttons, | ||
| 90 | ], | ||
| 91 | ), | ||
| 92 | ], | ||
| 93 | ), | ||
| 94 | |||
| 95 | ], | ||
| 96 | ); | ||
| 97 | |||
| 98 | $config_ff->open; | ||
| 99 | $config_ff->update; | ||
| 100 | |||
| 101 | $self->check_params; | ||
| 102 | |||
| 103 | 1; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub build_config_notebook { | ||
| 107 | my $self = shift; | ||
| 108 | |||
| 109 | my @notebook_pages; | ||
| 110 | |||
| 111 | my $config_object = $self->config_object; | ||
| 112 | my $context = $self->get_context; | ||
| 113 | |||
| 114 | my ( $label, $order ); | ||
| 115 | my $page_no = 0; | ||
| 116 | my %page2params; | ||
| 117 | |||
| 118 | $self->set_page2params( \%page2params ); | ||
| 119 | |||
| 120 | my $changed_hook = sub { $self->check_params }; | ||
| 121 | |||
| 122 | for ( my $i = 0; $i < @{ $config_object->order }; ) { | ||
| 123 | $label = $config_object->order->[$i]; | ||
| 124 | $order = $config_object->order->[ $i + 1 ]; | ||
| 125 | $i += 2; | ||
| 126 | |||
| 127 | my @items; | ||
| 128 | foreach my $item_name ( @{$order} ) { | ||
| 129 | my $item_def = $config_object->config->{$item_name}; | ||
| 130 | my ( $ff_class, %ff_params ); | ||
| 131 | |||
| 132 | push @{ $page2params{$page_no} }, $item_name; | ||
| 133 | |||
| 134 | $ff_params{label} = $item_def->{label}; | ||
| 135 | $ff_params{label_group} = "pref_labels"; | ||
| 136 | $ff_params{rules} = $item_def->{rules}; | ||
| 137 | $ff_params{attr} = "config.$item_name"; | ||
| 138 | $ff_params{changed_hook_after} = $changed_hook; | ||
| 139 | $ff_params{tip} = $item_def->{tooltip}; | ||
| 140 | |||
| 141 | if ( $item_def->{avail_method} ) { | ||
| 142 | my $method = $item_def->{avail_method}; | ||
| 143 | if ( ! Video::DVDRip::Config->$method() ) { | ||
| 144 | $ff_params{active_cond} = sub { 0 }; | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | if ( $item_def->{type} eq 'string' ) { | ||
| 149 | if ( $item_def->{presets} ) { | ||
| 150 | $ff_class = "Gtk2::Ex::FormFactory::Combo"; | ||
| 151 | $ff_params{presets} = $item_def->{presets}; | ||
| 152 | } | ||
| 153 | else { | ||
| 154 | $ff_class = "Gtk2::Ex::FormFactory::Entry"; | ||
| 155 | } | ||
| 156 | } | ||
| 157 | elsif ( $item_def->{type} eq 'number' ) { | ||
| 158 | $ff_class = "Gtk2::Ex::FormFactory::Entry"; | ||
| 159 | } | ||
| 160 | elsif ( $item_def->{type} eq 'file' ) { | ||
| 161 | $ff_class = "Gtk2::Ex::FormFactory::Entry"; | ||
| 162 | } | ||
| 163 | elsif ( $item_def->{type} eq 'dir' ) { | ||
| 164 | my $attr = $ff_params{attr}; | ||
| 165 | push @items, Gtk2::Ex::FormFactory::HBox->new ( | ||
| 166 | label => delete $ff_params{label}, | ||
| 167 | content => [ | ||
| 168 | Gtk2::Ex::FormFactory::Entry->new(%ff_params, expand => 1), | ||
| 169 | Gtk2::Ex::FormFactory::Button->new ( | ||
| 170 | stock => "gtk-add", | ||
| 171 | label => "", | ||
| 172 | tip => __"Create directory", | ||
| 173 | active_cond => sub { ! -d $context->get_object_attr($attr) }, | ||
| 174 | active_depends => $attr, | ||
| 175 | clicked_hook => sub { | ||
| 176 | my $dir = $context->get_object_attr($attr); | ||
| 177 | mkdir $dir, 0755 or croak "msg:".__x( | ||
| 178 | "Can't create directory:\n\n{dir}", | ||
| 179 | dir => $dir ); | ||
| 180 | $context->update_object_widgets("config"); | ||
| 181 | $self->check_params; | ||
| 182 | 1; | ||
| 183 | }, | ||
| 184 | ), | ||
| 185 | ], | ||
| 186 | ); | ||
| 187 | $ff_class = ""; | ||
| 188 | |||
| 189 | } | ||
| 190 | elsif ( $item_def->{type} eq 'switch' ) { | ||
| 191 | $ff_class = "Gtk2::Ex::FormFactory::YesNo"; | ||
| 192 | $ff_params{true_label} = __"Yes"; | ||
| 193 | $ff_params{false_label} = __"No"; | ||
| 194 | } | ||
| 195 | elsif ( $item_def->{type} eq 'popup' ) { | ||
| 196 | $ff_class = "Gtk2::Ex::FormFactory::Popup"; | ||
| 197 | $ff_params{items} = $item_def->{presets}; | ||
| 198 | } | ||
| 199 | else { | ||
| 200 | warn | ||
| 201 | "Unknown config type '$item_def->{type}' for '$item_name'"; | ||
| 202 | } | ||
| 203 | |||
| 204 | if ( $item_def->{dvd_button} && $self->has("hal") ) { | ||
| 205 | my $popup = Gtk2::Ex::FormFactory::Popup->new ( | ||
| 206 | attr => "config.selected_dvd_device", | ||
| 207 | expand_h => 0, | ||
| 208 | changed_hook_after => sub { | ||
| 209 | $self->get_context->set_object_attr ( | ||
| 210 | "config", "dvd_device", | ||
| 211 | $self->get_context->get_object_attr ( | ||
| 212 | "config.selected_dvd_device", | ||
| 213 | ), | ||
| 214 | ); | ||
| 215 | $self->check_params; | ||
| 216 | }, | ||
| 217 | tip => __"This is a list of connected DVD drives " | ||
| 218 | ."found in your system" | ||
| 219 | ); | ||
| 220 | |||
| 221 | $ff_params{expand} = 1; | ||
| 222 | |||
| 223 | push @items, Gtk2::Ex::FormFactory::HBox->new ( | ||
| 224 | label => delete $ff_params{label}, | ||
| 225 | content => [ | ||
| 226 | $ff_class->new(%ff_params), | ||
| 227 | $popup, | ||
| 228 | ], | ||
| 229 | ); | ||
| 230 | } | ||
| 231 | else { | ||
| 232 | push @items, $ff_class->new(%ff_params) | ||
| 233 | if $ff_class; | ||
| 234 | } | ||
| 235 | } | ||
| 236 | |||
| 237 | push @notebook_pages, Gtk2::Ex::FormFactory::Form->new( | ||
| 238 | title => $label, | ||
| 239 | content => \@items, | ||
| 240 | ); | ||
| 241 | |||
| 242 | ++$page_no; | ||
| 243 | } | ||
| 244 | |||
| 245 | return Gtk2::Ex::FormFactory::Notebook->new( | ||
| 246 | content => \@notebook_pages, | ||
| 247 | expand => 1, | ||
| 248 | customize_hook => sub { | ||
| 249 | $self->set_gtk_notebook( $_[0] ); | ||
| 250 | 1; | ||
| 251 | }, | ||
| 252 | changed_hook => sub { | ||
| 253 | $self->check_params; | ||
| 254 | }, | ||
| 255 | ); | ||
| 256 | } | ||
| 257 | |||
| 258 | sub build_config_buttons { | ||
| 259 | my $self = shift; | ||
| 260 | |||
| 261 | return Gtk2::Ex::FormFactory::HBox->new( | ||
| 262 | properties => { homogeneous => 1, }, | ||
| 263 | content => [ | ||
| 264 | Gtk2::Ex::FormFactory::Button->new( | ||
| 265 | label => __ "Check all settings", | ||
| 266 | stock => "gtk-spell-check", | ||
| 267 | clicked_hook => sub { | ||
| 268 | $self->check_params( all_pages => 1 ); | ||
| 269 | }, | ||
| 270 | ), | ||
| 271 | Gtk2::Ex::FormFactory::Button->new( | ||
| 272 | label => __ "Cancel", | ||
| 273 | stock => "gtk-cancel", | ||
| 274 | clicked_hook => sub { | ||
| 275 | $config_ff->close; | ||
| 276 | $config_ff = undef; | ||
| 277 | $self->get_context->set_object( config => undef ); | ||
| 278 | 1; | ||
| 279 | }, | ||
| 280 | ), | ||
| 281 | Gtk2::Ex::FormFactory::Button->new( | ||
| 282 | label => __ "Ok", | ||
| 283 | stock => "gtk-ok", | ||
| 284 | clicked_hook => sub { | ||
| 285 | my $config_object = $self->config_object; | ||
| 286 | $config_object->copy_values_from( | ||
| 287 | $self->get_context_object("config") ); | ||
| 288 | $config_object->save; | ||
| 289 | $config_ff->close; | ||
| 290 | $config_ff = undef; | ||
| 291 | $self->get_context->set_object( config => undef ); | ||
| 292 | my $project = $self->get_context->get_object("project"); | ||
| 293 | return if not $project; | ||
| 294 | $project->set_dvd_device( | ||
| 295 | $config_object->get_value('dvd_device') ); | ||
| 296 | 1; | ||
| 297 | }, | ||
| 298 | ), | ||
| 299 | ], | ||
| 300 | ); | ||
| 301 | } | ||
| 302 | |||
| 303 | sub build_config_check_results { | ||
| 304 | my $self = shift; | ||
| 305 | |||
| 306 | return Gtk2::Ex::FormFactory::VBox->new( | ||
| 307 | title => __ "Check results", | ||
| 308 | content => [ | ||
| 309 | Gtk2::Ex::FormFactory::TextView->new( | ||
| 310 | expand => 1, | ||
| 311 | scrollbars => [ "never", "always" ], | ||
| 312 | properties => { | ||
| 313 | editable => 0, | ||
| 314 | cursor_visible => 0, | ||
| 315 | wrap_mode => "word", | ||
| 316 | }, | ||
| 317 | customize_hook => sub { | ||
| 318 | my ($gtk_text_view) = @_; | ||
| 319 | my $tag_table = Gtk2::TextTagTable->new; | ||
| 320 | $tag_table->add( | ||
| 321 | $self->create_text_tag( | ||
| 322 | "good_value", | ||
| 323 | foreground => "#3f7c3d", | ||
| 324 | weight => 600, | ||
| 325 | ) | ||
| 326 | ); | ||
| 327 | $tag_table->add( | ||
| 328 | $self->create_text_tag( | ||
| 329 | "bad_value", | ||
| 330 | foreground => "#ff0000", | ||
| 331 | weight => 600, | ||
| 332 | ) | ||
| 333 | ); | ||
| 334 | my $buffer = Gtk2::TextBuffer->new($tag_table); | ||
| 335 | $gtk_text_view->set_buffer($buffer); | ||
| 336 | $self->set_gtk_text_buffer($buffer); | ||
| 337 | 1; | ||
| 338 | }, | ||
| 339 | ), | ||
| 340 | ], | ||
| 341 | ); | ||
| 342 | } | ||
| 343 | |||
| 344 | sub check_params { | ||
| 345 | my $self = shift; | ||
| 346 | my %par = @_; | ||
| 347 | my ( $page, $all_pages ) = @par{ 'page', 'all_pages' }; | ||
| 348 | |||
| 349 | my @pages; | ||
| 350 | if ( not $all_pages ) { | ||
| 351 | $page = $self->gtk_notebook->get_current_page | ||
| 352 | if not defined $page; | ||
| 353 | push @pages, $page; | ||
| 354 | } | ||
| 355 | else { | ||
| 356 | @pages = sort { $a <=> $b } keys %{ $self->page2params }; | ||
| 357 | } | ||
| 358 | |||
| 359 | my $buffer = $self->gtk_text_buffer; | ||
| 360 | my $config_object = $self->get_context_object("config"); | ||
| 361 | |||
| 362 | $buffer->set_text(""); | ||
| 363 | |||
| 364 | my $iter = $buffer->get_start_iter; | ||
| 365 | |||
| 366 | my ( $options, $method ); | ||
| 367 | foreach $page (@pages) { | ||
| 368 | $options = $self->page2params->{$page}; | ||
| 369 | foreach my $option ( @{$options} ) { | ||
| 370 | $buffer->insert( $iter, | ||
| 371 | $config_object->config->{$option}->{label} . ": " ); | ||
| 372 | my ($result_text, $result_value); | ||
| 373 | $method = "test_$option"; | ||
| 374 | if ( $config_object->can($method) ) { | ||
| 375 | ($result_text, $result_value) = $config_object->$method($option); | ||
| 376 | } | ||
| 377 | else { | ||
| 378 | $result_text = __ "not tested : Ok"; | ||
| 379 | $result_value = 1; | ||
| 380 | } | ||
| 381 | $buffer->insert_with_tags_by_name( | ||
| 382 | $iter, | ||
| 383 | $result_text . "\n", | ||
| 384 | $result_value ? "good_value" : "bad_value" | ||
| 385 | ); | ||
| 386 | } | ||
| 387 | $buffer->insert( $iter, ( "-" x 120 ) . "\n" ) | ||
| 388 | unless $page == $pages[-1]; | ||
| 389 | } | ||
| 390 | |||
| 391 | 1; | ||
| 392 | } | ||
| 393 | |||
| 394 | 1; |