summaryrefslogtreecommitdiffstats
path: root/common/Wizcommon.pm
blob: f9ae4c7b2efc88e49e1045895f324426c5a43cc6 (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
#!/usr/bin/perl

# 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
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# 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.

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  run_command_and_log test_host_domain wizard_progress_bar);

my $net;

sub check_dhcp {
    $net->is_dhcp and return 'dhcp_warning';
}

sub new {
    my ($class) = @_;
    $net = new MDK::Wizard::IFCFG;
    bless {
	    net => $net,
       }, $class;
}

sub check_started {
  my ($service) = @_;
  my ($isrunning) = chomp_(`pidof $service`) =~ /(\d+)/m;
  if (!$isrunning) {
    return 'error_end';
  }
}

sub test_host_domain {
  my ($SHORTHOSTNAME, $DOMAINNAME) = @_;
  if ($SHORTHOSTNAME =~ /localhost/) {
    return 0, N("You need to readjust your hostname. 'localhost' is not a correct hostname for a DNS server. Hostname must be a FQDN: Fully Qualified Domain Name");
  }
  if (member($DOMAINNAME, qw(localdomain (none))) && -z $DOMAINNAME) {
    return 0, N("You need to readjust your domainname. For a DNS server you need a correct domainname, not equal to localdomain or none. Hostname must be a FQDN: Fully Qualified Domain Name. Launch drakconnect to adjust it.");
  }
  return 1;
}


sub wizard_progress_bar {
    use lib qw(/usr/lib/libDrakX);
    use ugtk2 qw(:wrappers);
    my ($command, $descr) = @_;
    my ($value, $timer);
    my $my_win = ugtk2->new("");
    my $window1 = $my_win->{window};
    gtkadd($window1,
	   gtkpack(Gtk2::VBox->new(0,0),
		   gtkpack_(Gtk2::VBox->new(0, 3),
			    0, my $text = Gtk2::Label->new($descr),
			    0, my $text2 = Gtk2::Label->new,
			    0, Gtk2::HSeparator->new,
			    0, my $pbar = Gtk2::ProgressBar->new,
			   ),
		  ),
	  );
    $window1->realize;
    $pbar->set_pulse_step(0.1);

    local *TMP;
    open(TMP, "$command 2>&1 |");
    while ($value = <TMP>) {
      $timer = Glib::Timeout->add(10, sub {});
      $pbar->pulse;
      $text2->set_text($value);
      gtkflush();
      next;
      $my_win->main;
      $window1->show_all;
      undef $value;
    }
    close TMP;
    $my_win->destroy;
}


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,\nyou 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);
    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