File: Common.pm

package info (click to toggle)
systeminstaller 1.04-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 748 kB
  • ctags: 261
  • sloc: perl: 5,769; makefile: 70
file content (216 lines) | stat: -rw-r--r-- 6,880 bytes parent folder | download | duplicates (2)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package SystemInstaller::Tk::Common;

#   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 of the License, 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

use base qw(Exporter);
use vars qw(@EXPORT %Labels);
use AppConfig;
use File::Basename;
use strict;

@EXPORT = qw(selector2entry
             quit_button
             label_entry_line 
             label_option_line 
             label_listbox_line 
             resethash
             imageexists
             init_si_config
             done_window
             error_window
            );

%Labels = ();

#
#  selector2entry sets up the callback needed for a fileselector to be tied to 
#  an entry box
#  

sub selector2entry {
    my ($var, $selector) = @_;

    # now we attempt to do some reasonable directory setting
    my $dir = $$var;
    if(-d $dir) {
        $selector->configure(-directory => $dir);
    } else {
        my $dir2 = dirname($dir);
        if(-d $dir2) {
            $selector->configure(-directory => $dir2);
        }
    }
    $$var = $selector->Show();
}

sub reset_window {
    my ($window, $curvars, $defvars, $optiondefaults) = @_;
    resethash($curvars, $defvars);
    foreach my $key (keys %$optiondefaults) {
        if($$optiondefaults{$key}) {
            $$optiondefaults{$key}->setOption($$curvars{$key});
        }
    }
}

sub close_after {
    my ($window, $onclose, @args) = @_;
    if(ref($onclose) eq "CODE") {
        &$onclose(@args);
    }
    $window->destroy;
}

sub done_window {
    my ($window, $message, $onclose, @args) = @_;
    my $done = $window->Toplevel();
    $done->title("Done!");
    my $label = $done->Message(-text => $message, 
                               -foreground => "blue",
                              );
    $label->grid();
    my $button = $done->Button(
                               -text=>"Close",
                               -command=> [\&close_after, $done, $onclose, @args],
                               -pady => 8,
                               -padx => 8,
                              );
    $button->grid();
}

sub error_window {
    my ($window, $message, $onclose, @args) = @_;
    my $done = $window->Toplevel();
    $done->title("ERROR!");
    my $label = $done->Message(-text => $message, 
                               -foreground => "red",
                              );
    $label->grid();
    my $button = $done->Button(
                               -text=>"Close",
                               -command=> [\&close_after, $done, $onclose, @args],
                               -pady => 8,
                               -padx => 8,
                              );
    $button->grid();
}

sub init_si_config {
    my $config = new AppConfig(
                               DEFAULT_IMAGE_DIR => { ARGCOUNT => 1},
                               AUTOINSTALL_SCRIPT_DIR => { ARGCOUNT => 1},
                               AUTOINSTALL_BOOT_DIR => { ARGCOUNT => 1},
                               RSYNCD_CONF => { ARGCOUNT => 1},
                               RSYNC_STUB_DIR => { ARGCOUNT => 1},
                               CONFIG_DIR => { ARGCOUNT => 1},
                               TFTP_DIR => { ARGCOUNT => 1},
                               NET_BOOT_DEFAULT => { ARGCOUNT => 1},
                               # now for tksis configuration parameters
                               ICON_DIR => { ARGCOUNT => 1, DEFAULT => "/usr/share/systeminstaller/images"},
                               XTERM_CMD => { ARGCOUNT => 1, 
                                              DEFAULT => "xterm -bg black -fg magenta",
                                            },
                              );
    $config->file("/etc/systemimager/systemimager.conf", "/etc/systeminstaller/tksis.conf");
    return $config;
}

#
#  resethash sets one hash to another hash.
#

sub resethash {
    my ($hash1, $hash2) = @_;
    foreach my $key (keys %$hash2) {
        $$hash1{$key} = $$hash2{$key};
    }
}

#
#  The following creates a set of widgets with do 'Label: [Entry]' 
#

sub label_entry_line {
    my ($window, $labeltext, $variable, $validate, @morewidgets) = @_;
    my @options;
    if($validate) {
        @options = (
                    -validatecommand => $validate,
                    -validate => "focusout",
                   );
    }
    my $label = $window->Label(-text => "$labeltext: ",
                               -anchor => "w");
    my $entry = $window->Entry(-textvariable => $variable, @options);
    $label->grid($entry,@morewidgets);
}

# This creates a small list box with 1 item ($selection) selected.
sub label_listbox_line {
    my ($window, $labeltext, $selection, $listitems , @morewidgets) = @_;
    my $label = $window->Label(-text => "$labeltext: ",
                               -anchor => "w");
    my $listbox = $window->Scrolled("Listbox",-scrollbars => 'e', -height => 2, 
                 -width => 17, -selectmode => "single", -exportselection=>0);
    $listbox->insert(0,$selection);
    $listbox->insert('end',@$listitems);
    $listbox->selectionSet(0);

    $label->grid($listbox,@morewidgets);
    return $listbox;
}

sub label_entry_file_line {
    
}

sub quit_button {
    my $window = shift;
    my $quit_button = $window->Button(
                                      -text=>"Close",
                                      -command=> [sub { shift->destroy }, $window],
                                      -pady => 8,
                                      -padx => 8,
                                     );
    return $quit_button;
}

sub label_option_line {
    my ($window, $labeltext, $variable, $options, @morewidgets) = @_;
    my $label = $window->Label(-text => "$labeltext: ",
                               -anchor => "w");

    my $default = $$variable;
    my $optionmenu = $window->Optionmenu(-options => $options,
                                      -variable => $variable);
    $optionmenu->setOption($default) if $default;

    $label->grid($optionmenu, @morewidgets, -sticky => "nesw");
    return $optionmenu;
}

sub imageexists {
    my ($rsyncconf, $imagename) = @_;
    open(IN,"<$rsyncconf") or return undef;
    if(grep(/\[$imagename\]/, <IN>)) {
        close(IN);
        return 1;
    }
    close(IN);
    return undef;
}


1;