/[soft]/drakx-net/trunk/bin/drakhosts
ViewVC logotype

Contents of /drakx-net/trunk/bin/drakhosts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 7150 - (show annotations) (download)
Sun Jan 20 12:16:37 2013 UTC (11 years, 2 months ago) by tv
File size: 7952 byte(s)
fix translation domain (Yuri Chornoivan, mga#8629)
1 #!/usr/bin/perl
2 #
3 # Copyright (C) 2005-2006 by Mandriva aginies _ateuh_ mandriva.com
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2, or (at your option)
8 # any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18 # 02111-1307, USA.
19
20 # i18n: IMPORTANT: to get correct namespace (drakhosts instead of libDrakX)
21 BEGIN { unshift @::textdomains, 'drakx-net' }
22
23 use lib qw(/usr/lib/libDrakX);
24 use standalone;
25 use strict;
26 use common;
27 use network::network;
28
29 $ugtk2::wm_icon = 'IC-Dhost-48';
30 use mygtk2 qw(gtknew);
31 use ugtk2 qw(:ask :wrappers :create :dialogs);
32
33 use constant FALSE => 0;
34
35
36 my $HOSTS = "/etc/hosts";
37 my @listhosts;
38
39 use constant COLUMN_IP => 0;
40 use constant COLUMN_HOSTNAME => 1;
41 use constant COLUMN_ALIAS => 2;
42 use constant NUM_COLUMNS => 3;
43
44 require_root_capability();
45
46 my %size_groups = map { $_ => Gtk2::SizeGroup->new('horizontal') } qw(label widget);
47 my $label_and_widgets = sub {
48 my ($label, $widget) = @_;
49 gtkpack_(Gtk2::HBox->new(0,5),
50 0, gtkadd_widget($size_groups{label}, gtknew('Label_Left', text => $label)),
51 1, gtkadd_widget($size_groups{widget}, $widget),
52 );
53 };
54
55
56 sub get_host_data() {
57 # 127.0.0.1 localhost.localdomain localhost
58 # 10.0.1.253 guibpiv.guibland.com
59 foreach (cat_($HOSTS)) {
60 my ($ip, $name, $alias) = /^(\d\S*)\s+(\S*)\s+(.*)$/;
61 $ip and push @listhosts, {
62 ip => $ip,
63 hostname => $name,
64 alias => $alias,
65 };
66 }
67 }
68
69 sub write_conf_hosts() {
70 output($HOSTS, "# generated by drakhosts\n");
71 foreach my $a (@listhosts) {
72 append_to_file($HOSTS, "$a->{ip} $a->{hostname} $a->{alias}\n");
73 }
74 }
75
76 sub add_modify_entry {
77 my ($treeview, $wanted, $title) = @_;
78 my $model = $treeview->get_model;
79 my $selection = $treeview->get_selection;
80 my $iter;
81 my ($i, $ip, $hostname, $alias);
82 undef $i;
83 undef $iter;
84
85 $_ = Gtk2::Entry->new foreach $ip, $hostname, $alias;
86
87 # test if modify or add a host
88
89 my $dialog = _create_dialog();
90 $dialog->set_transient_for($::main_window);
91 $dialog->set_title($title);
92 $dialog->set_modal(1);
93 $dialog->set_resizable(1);
94 $dialog->set_size_request(300, -1);
95
96 if ($wanted =~ /modify/) {
97 $iter = $selection->get_selected;
98 $iter or info_dialog(N("Error"), N("Please add an host to be able to modify it.")) and return;
99 my $path = $model->get_path($iter);
100 $i = ($path->get_indices)[0];
101 $ip->set_text($listhosts[$i]{ip});
102 $hostname->set_text($listhosts[$i]{hostname});
103 $alias->set_text($listhosts[$i]{alias});
104 }
105
106 my $text;
107 $text = N("Please modify information") if $wanted =~ /modify/;
108 $text = N("Please delete information") if $wanted =~ /delete/;
109 $text = N("Please add information") if $wanted =~ /add/;
110
111 gtkpack__($dialog->vbox,
112 gtknew('Title2', label => $text),
113 $label_and_widgets->(N("IP address:"), $ip),
114 $label_and_widgets->(N("Host name:"), $hostname),
115 $label_and_widgets->(N("Host Aliases:"), $alias),
116 create_okcancel({
117 cancel_clicked => sub { $dialog->destroy },
118 ok_clicked => sub {
119 is_ip($ip->get_text) or err_dialog(N("Error!"), N("Please enter a valid IP address.")) and return;
120 if ($wanted =~ /add/) {
121 $iter = $model->append;
122 $i = "-1";
123 push @listhosts, {
124 ip => $ip->get_text,
125 hostname => $hostname->get_text,
126 alias => $alias->get_text,
127 };
128 }
129 $listhosts[$i]{hostname} = $hostname->get_text;
130 $listhosts[$i]{alias} = $alias->get_text;
131 $listhosts[$i]{ip} = $ip->get_text;
132 $model->set($iter,
133 COLUMN_IP, $listhosts[$i]{ip},
134 COLUMN_HOSTNAME, $listhosts[$i]{hostname},
135 COLUMN_ALIAS, $listhosts[$i]{alias},
136 );
137 $dialog->destroy;
138 # write_conf_hosts();
139 },
140 },
141 ),
142 );
143 $dialog->show_all;
144 }
145
146 sub remove_entry {
147 my ($treeview) = @_;
148 my $model = $treeview->get_model;
149 my $selection = $treeview->get_selection;
150 my $iter = $selection->get_selected;
151 if ($iter) {
152 my $path = $model->get_path($iter);
153 my $i = ($path->get_indices)[0];
154 ask_okcancel("Remove entry ?", "Remove $listhosts[$i]{hostname}") or return;
155 $model->remove($iter);
156 splice @listhosts, $i, 1;
157 }
158 # write_conf_hosts();
159 }
160
161 sub create_model() {
162 get_host_data();
163 my $model = Gtk2::ListStore->new("Glib::String", "Glib::String", "Glib::String");
164 foreach my $a (@listhosts) {
165 my $iter = $model->append;
166 $model->set($iter,
167 COLUMN_IP, $a->{ip},
168 COLUMN_HOSTNAME, $a->{hostname},
169 COLUMN_ALIAS, $a->{alias},
170 );
171 }
172 return $model;
173 }
174
175 # add colum to model
176 sub add_columns {
177 my $treeview = shift;
178 each_index {
179 my $renderer = Gtk2::CellRendererText->new;
180 $renderer->set(editable => 0);
181 $renderer->set_data(column => $::i);
182 $treeview->insert_column_with_attributes(-1, $_, $renderer, 'text' => $::i);
183 } N("IP address"), N("Host name"), N("Host Aliases");
184 }
185
186
187 ###############
188 # Main Program
189 ###############
190 # create model
191 my $model = create_model();
192
193 my $window = ugtk2->new(N("Manage hosts definitions"));
194 $::main_window = $window->{real_window};
195 $window->{rwindow}->set_size_request(500, 400) unless $::isEmbedded;
196 my $W = $window->{window};
197 $W->signal_connect(delete_event => sub { ugtk2->exit });
198
199 my $treeview = Gtk2::TreeView->new_with_model($model);
200 $treeview->set_rules_hint(1);
201 $treeview->get_selection->set_mode('single');
202 add_columns($treeview);
203
204 $treeview->signal_connect(button_press_event => sub {
205 my (undef, $event) = @_;
206 my $selection = $treeview->get_selection;
207 my $iter = $selection->get_selected;
208 if ($iter) {
209 add_modify_entry($treeview, 'modify', N("Modify entry")) if $event->type eq '2button-press';
210 }
211 });
212
213 my $okcancel = create_okcancel({
214 cancel_clicked => sub { ugtk2->exit },
215 ok_clicked => sub { write_conf_hosts(); ugtk2->exit },
216 },
217 );
218
219
220
221 # main interface
222 $W->add(gtkpack_(Gtk2::VBox->new(0,0),
223 if_(!$::isEmbedded, 0, Gtk2::Banner->new('IC-Dhost-48', N("Manage hosts definitions"))),
224 #if_($::isEmbedded, 0, Gtk2::Label->new("Here you can add, remove and alter hosts definition.")),
225 1, gtkpack_(gtkset_border_width(Gtk2::HBox->new, 0),
226 1, create_scrolled_window($treeview),
227 0, gtkpack_(gtkset_border_width(create_vbox('start', 3)),
228 0, gtksignal_connect(Gtk2::Button->new(N("Add")), clicked => sub {
229 eval { add_modify_entry($treeview, 'add', N("Add entry")) };
230 my $err = $@;
231 if ($err) {
232 err_dialog(N("Error"), N("Failed to add host.") . "\n\n" . $err);
233 }
234 }),
235 0, gtksignal_connect(Gtk2::Button->new(N("Modify")), clicked => sub {
236 eval { add_modify_entry($treeview, 'modify', N("Modify entry")) };
237 my $err = $@;
238 if ($err) {
239 err_dialog(N("Error"), N("Failed to Modify host.") . "\n\n" . $err);
240 }
241 }),
242 0, gtksignal_connect(Gtk2::Button->new(N("Remove")), clicked => sub {
243 eval { remove_entry($treeview) };
244 my $err = $@;
245 if ($err) {
246 err_dialog(N("Error"), N("Failed to remove host.") . "\n\n" . $err);
247 }
248 }),
249 if_($::isEmbedded, 0, gtksignal_connect(Gtk2::Button->new(N("Quit")), clicked => sub { ugtk2->exit })),
250 ),
251 ),
252 0, $okcancel,
253 ),
254 );
255
256 $W->show_all;
257 Gtk2->main;

  ViewVC Help
Powered by ViewVC 1.1.30