diff options
Diffstat (limited to 'common/Wizcommon.pm')
-rw-r--r-- | common/Wizcommon.pm | 64 |
1 files changed, 63 insertions, 1 deletions
diff --git a/common/Wizcommon.pm b/common/Wizcommon.pm index aa53865c..9e8722b1 100644 --- a/common/Wizcommon.pm +++ b/common/Wizcommon.pm @@ -3,6 +3,7 @@ # Copyright (C) 2003 Mandrakesoft # # Author: Florent Villard <warly@mandrakesoft.com> +# A Ginies # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -20,11 +21,13 @@ package MDK::Wizard::Wizcommon; use strict; +use standalone; +use common; use MDK::Common; use MDK::Wizard::IFCFG; our @ISA = qw(Exporter); -our @EXPORT = qw(check_started); +our @EXPORT = qw(check_started run_command_and_log); my $net; @@ -48,4 +51,63 @@ sub check_started { } } + +my $timeout; +sub gtktext_get_log { + my ($command, $log_w, $log_scroll) = @_; + my $buffer = $log_w->get_buffer; + $buffer->delete($buffer->get_start_iter, $buffer->get_end_iter); + + my ($prev_scroll, $want_scroll_down) = (0, 1); + my $pid = open(my $F, "$command 2>&1 |") or return sub { 1 }; + fcntl($F, c::F_SETFL(), c::O_NONBLOCK()) or die "can't fcntl F_SETFL: $!"; + Glib::Source->remove($timeout) if $timeout; $timeout = ''; + $timeout = Glib::Timeout->add(100, sub { + if ($buffer) { + my $end = $buffer->get_end_iter; + while (defined (my $s = <$F>)) { + $buffer->insert_interactive($end, $s, -1); + } + my $new_scroll = $log_scroll->child->get_vadjustment->get_value; + $want_scroll_down &&= $new_scroll >= $prev_scroll; + $prev_scroll = $new_scroll; + $log_w->scroll_to_iter($end, 0, 0, 0, 0) if $want_scroll_down; + } + if (waitpid($pid, c::WNOHANG()) > 0) { + $buffer->insert_interactive($buffer->get_end_iter, "\n" . "#" x 20 . "\n" . " This script has terminated\ +, you can close this window", -1) if $buffer; + $pid = $timeout = ''; + 0; + } else { + 1; + } + }); + sub { + if ($pid) { + $::in->ask_yesorno('', N("The command is still running. Do you want to kill it and quit the Wizard ?")) or return; + kill 9, $pid+1; + 1; + } + $buffer = ''; + 1; + }; +} + +sub run_command_and_log { + use lib qw(/usr/lib/libDrakX); + use ugtk2 qw(:ask :helpers :wrappers :create); + my ($command, $descr, $when_command_is_over) = @_; + my $w = ugtk2->new(''); + gtkadd(gtkset_size_request($w->{window}, 800, 400), + gtkpack_(gtkset_modal(Gtk2::VBox->new, 1), + 0, $descr, + 1, my $log_scroll = create_scrolled_window(my $log_w = gtkset_editable(Gtk2::TextView->new, 0)), + 0, my $close_w = Gtk2::Button->new(N("Close"))), + ); + my $stop_running = gtktext_get_log($command, $log_w, $log_scroll); + $close_w->signal_connect(clicked => sub { $stop_running->() and $w->destroy and return 0}); + $close_w->grab_focus; + $w->show; +} + 1 |