summaryrefslogtreecommitdiffstats
path: root/perl-install/standalone/drakhosts
blob: 2cf717ccaa4c587bc245f29bf766f93f0cd93000 (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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/perl
#
# Copyright (C) 2005 by Mandriva aginies _ateuh_ mandriva.com
#
# 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.

my $version = "0.1";

# i18n: IMPORTANT: to get correct namespace (drakhosts instead of libDrakX)
BEGIN { unshift @::textdomains, 'drakhosts' }

use lib qw(/usr/lib/libDrakX);
use standalone;
use strict;
use common;
use network::network;

use interactive;
use ugtk2 qw(:ask :wrappers :create :dialogs);

use constant FALSE => 0;
use constant TRUE => 1;

my $HOSTS = "/etc/hosts";
my @listhosts;

use constant COLUMN_IP => 0;
use constant COLUMN_HOSTNAME => 1;
use constant COLUMN_ALIAS => 2;
use constant NUM_COLUMNS => 3;

my $in = 'interactive'->vnew('su');

my %size_groups = map { $_ => Gtk2::SizeGroup->new('horizontal') } qw(label widget);
my $label_and_widgets = sub {
  my ($label, $widget) = @_;
  gtkpack_(Gtk2::HBox->new(0,5),
           0, gtkadd_widget($size_groups{label}, $label),
           1, gtkadd_widget($size_groups{widget}, $widget),
          );
};


sub get_host_data {
# 127.0.0.1        localhost.localdomain localhost
# 10.0.1.253        guibpiv.guibland.com
  foreach (cat_($HOSTS)) {
    my ($ip, $name, $alias) = $_ =~ /^(\d\S*)\s+(\S*)\s+(.*)$/;
    $ip and push @listhosts, {
			      ip => $ip,
			      hostname => $name,
			      alias => $alias,
			     };
  }
}

sub write_conf_hosts {
  output($HOSTS, "# generated by drakhosts\n");
  foreach my $a (@listhosts) {
    append_to_file($HOSTS, "$a->{ip} $a->{hostname} $a->{alias}\n");
  }
}

sub add_modify_entry {
  my ($widget, $treeview, $wanted) = @_;
  my $model = $treeview->get_model;
  my $selection = $treeview->get_selection;
  my $iter;
  my ($i, $ip, $hostname, $alias, $oldip);
  undef $i;
  undef $iter;

  map {  $_ = Gtk2::Entry->new } $ip, $hostname, $alias;

# test if modify or add a host

  my $dialog = new Gtk2::Dialog();
  $dialog->set_title("Drakhosts $wanted entry");
  $dialog->set_modal(1);
  $dialog->set_resizable(1);

  if ($wanted =~ /modify/) {
    $iter = $selection->get_selected;
    my $path = $model->get_path($iter);
    $i = ($path->get_indices)[0];
    $ip->set_text($listhosts[$i]{ip});
    $hostname->set_text($listhosts[$i]{hostname});
    $alias->set_text($listhosts[$i]{alias});
    $oldip = $listhosts[$i]{ip};
  }

  gtkpack_($dialog->vbox,
	   0, gtkadd(Gtk2::Frame->new(N("Please $wanted information")),
		     gtkpack_(gtkset_border_width(Gtk2::VBox->new, 5),
			      0, $label_and_widgets->(N("IP address:"), $ip),
			      0, $label_and_widgets->(N("Host name:"), $hostname),
			      0, $label_and_widgets->(N("Host Aliases:"), $alias),
			     ),
		    ),
	   0, create_okcancel({
			       cancel_clicked => sub { $dialog->destroy },
			       ok_clicked => sub {
				 is_ip($ip->get_text()) or err_dialog(N("Error!"), N("Please enter a valid IP address.")) and return;
				 my $testip = chomp_($ip->get_text());
				 my $testname = chomp_($hostname->get_text());
				 my $toldip = chomp_($oldip);
				 if ($testip !~ /$toldip/ or $wanted =~ /add/) {
				   foreach my $a (@listhosts) {
				     if ($a->{ip} =~ /$testip/) {
				       err_dialog(N("Error!"), N("Same IP is already in %s file.", $HOSTS)) and return;
				     }
				   }
				 }
				 if ($wanted =~ /add/) {
				   $iter = $model->append;
				   $i = "-1";
				   push @listhosts, {
						     ip => $ip->get_text(),
						     hostname => $hostname->get_text(),
						     alias => $alias->get_text(),
						    };
				 }
				 $listhosts[$i]{hostname} = $hostname->get_text();
				 $listhosts[$i]{alias} = $alias->get_text();
				 $listhosts[$i]{ip} = $ip->get_text();
				 $model->set($iter,
					     COLUMN_IP, $listhosts[$i]{ip},
					     COLUMN_HOSTNAME, $listhosts[$i]{hostname},
					     COLUMN_ALIAS, $listhosts[$i]{alias},
					    );
				 $dialog->destroy;
				 write_conf_hosts();
			       },
			      },
			     ),
	  );
  $dialog->show_all;
}

sub remove_entry {
  my ($widget, $treeview) = @_;
  my $model = $treeview->get_model;
  my $selection = $treeview->get_selection;
  my $iter = $selection->get_selected;
    if ($iter) {
      my $path = $model->get_path($iter);
      my $i = ($path->get_indices)[0];
      ask_okcancel("Remove entry ?", "Remove $listhosts[$i]{hostname}") or return;
      $model->remove($iter);
      splice @listhosts, $i, 1;
    }
  write_conf_hosts();
}

sub create_model() {
  get_host_data();
  my $model = Gtk2::ListStore->new("Glib::String", "Glib::String",  "Glib::String");
  foreach my $a (@listhosts) {
    my $iter = $model->append;
    $model->set($iter,
		 COLUMN_IP, $a->{ip},
		 COLUMN_HOSTNAME, $a->{hostname},
		 COLUMN_ALIAS, $a->{alias},
	       ),
	     }
  return $model;
}

# add colum to model
sub add_columns {
  my $treeview = shift;
  my $model = $treeview->get_model;
  each_index {
    my $renderer = Gtk2::CellRendererText->new;
    $renderer->set(editable => 0);
    $renderer->signal_connect(edited => \&cell_edited, $model);
    $renderer->set_data(column => $::i);
    $treeview->insert_column_with_attributes(-1, $_, $renderer, 'text' => $::i);
  } N("IP address"), N("Host name"), N("Host Aliases");
}


###############
# Main Program
###############
# create model
my $model = create_model();

my $window = ugtk2->new("Drakhosts $version");
$window->{rwindow}->set_size_request(400, 300) unless $::isEmbedded;
my $W = $window->{window};
$W->signal_connect(delete_event => sub { ugtk2->exit });

my $treeview = Gtk2::TreeView->new_with_model($model);
$treeview->set_rules_hint(TRUE);
$treeview->get_selection->set_mode('single');
add_columns($treeview);

my $okcancel = create_okcancel({
				cancel_clicked => sub { ugtk2->exit },
				ok_clicked => \&write_conf_hosts,
			       },
			      );

# main interface
$W->add(gtkpack_(Gtk2::VBox->new(0,0),
		 1, create_scrolled_window($treeview),
		 0, gtkpack_(gtkset_border_width(Gtk2::HBox->new, 3),
                             0, gtksignal_connect(Gtk2::Button->new(N("Add a host")), clicked => sub {
						    eval { add_modify_entry($model, $treeview, "add") };
						    my $err = $@;
						    if ($err) {
                                                      err_dialog(N("Error"), N("Failed to add host.") . "\n\n" . $err);
                                                    }
                                                  }),
                             0, gtksignal_connect(Gtk2::Button->new(N("Modify a host")), clicked => sub {
						    eval { add_modify_entry($model, $treeview, "modify") };
						    my $err = $@;
						    if ($err) {
                                                      err_dialog(N("Error"), N("Failed to Modify host.") . "\n\n" . $err);
                                                    }
                                                  }),

                             0, gtksignal_connect(Gtk2::Button->new(N("Remove a host")), clicked => sub {
						    eval { remove_entry($model, $treeview) };
						    my $err = $@;
						    if ($err) {
                                                      err_dialog(N("Error"), N("Failed to remove host.") . "\n\n" . $err);
                                                    }
                                                  }),
			     0, Gtk2::VSeparator->new,
			    ),
		 if_($::isEmbedded, 0, $okcancel),
		),
       );

$W->show_all;
Gtk2->main;