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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
#!/usr/bin/perl
use strict;
use lib qw(/usr/lib/libDrakX);
use standalone;
use common;
use mouse;
use detect_devices;
use ugtk2 qw(:create :dialogs :helpers :wrappers);
use interactive;
use Libconf qw(:functions);
use Libconf::Glueconf::NUT::Ups_conf;
# config files:
my %files = (devices => "/etc/ups/ups.conf",
access => "/etc/ups/upsd.conf",
users => "/etc/ups/upsd.users",
);
my ($struct, $users); # NUT configuration
my ($w, $in); # GUI
my %indexes;
sub writeconf() {
info_dialog(N("Warning"), "Write support for users is incomplete\n\nIt lacks some support for some extra fields that would be lost else");
log::explanations("Updating NUT configuration accordingly");
$struct->writeConf($files{devices});
if ($users) {
log::explanations("Updating NUT users configuration accordingly");
$users->writeConf($files{users});
}
require services;
services::restart("upsd");
}
sub read_nut_config() {
$struct = Libconf::Glueconf::NUT::Ups_conf->new({ filename => $files{devices} });
}
sub readDriversList() {
my (%ups, @ups);
local $_;
foreach (cat_(first(glob("/usr/share/doc/nut/docs/driver.list")))) {
/^#/ and next;
if (my ($vendor, $model, $extra, $driver) = /^"(.*)"\s+"(.*)"\s+"(.*)"\s+"(.*)"/) {
$ups{$vendor}{$model . $extra} = {
driver => $driver,
extra => $extra,
};
push @ups, "$vendor|$model ($extra)";
}
}
\%ups, \@ups;
}
my %models;
sub add_device_wizard {
my ($in, $config) = @_;
my ($ups_models, $model_list) = readDriversList();
my ($ups, $vendor, $model, $extra, $name, $driver, $port, @new_devices, $opts);
my %methods = (
# network => N("Connected through the network"), # need SNMP probe
# serial => N("Connected through a serial port"),
# usb => N("Connected through an usb cable"),
auto => N("Connected through a serial port or an usb cable"),
manual => N("Manual configuration"),
);
my $method = $methods{auto};
require wizards;
my $wiz = wizards->new({
#defaultimage => "logdrake.png", # FIXME
name => N("Add an UPS device"),
pages => {
welcome => {
name => N("Welcome to the UPS configuration utility.
Here, you'll add a new UPS to your system.\n"),
no_back => 1,
next => 'method'
},
method => {
name => N("We're going to add an UPS device.
Do you want to autodetect UPS devices connected to this machine or to manually select them?"),
data => [ { label => N("Autodetection"), val => \$method, type => "list",
list => [ values %methods ] } ],
post => sub { +{ reverse %methods }->{$method} },
},
auto => {
end => 1,
pre => sub {
local $::isWizard;
my $_wait = $in->wait_message(N("Please wait"), N("Detection in progress"));
# UPS autoconfig:
detect_devices::probeSerialDevices() if !$::testing;
@new_devices = ();
foreach my $ups_device (detect_devices::getUPS()) {
my $str = $ups_device->{name} || $ups_device->{DESCRIPTION};
$str =~ s/ /_/g;
$name = $str;
if (!exists $struct->{$str}) {
$port = $struct->{$str}{port} = $ups_device->{port} || $ups_device->{DEVICE};
$driver = $struct->{$str}{driver} = $ups_device->{driver};
push @new_devices, $str;
}
}
},
name => sub {
if (@new_devices) {
N("Congratulations") . "\n\n" .
N("The wizard successfully added the following UPS devices:") . join("\n\n-", @new_devices);
} else {
N("No new UPS devices was found");
}
},
},
manual => {
name => N("UPS driver configuration") . "\n\n" . N("Please select your UPS model."),
data => [ { label => N("Manufacturer / Model:"), val => \$ups, list => $model_list,
type => 'combo', sort => 1, separator => '|' }, ],
post => sub {
($vendor, $model, $extra) = ($1, $2, $3) if $ups =~ /(.*)\|(.*) \((.*)\)$/;
($name, $driver, $port) = ("myups", $ups_models->{$vendor}{$model . $extra}{driver}, "");
($driver, $opts) = split(/\s+/, $driver);
"driver";
},
},
driver => {
name => sub {
N("UPS driver configuration") . "\n\n" . N("We are configuring the \"%s\" UPS from \"%s\".
Please fill in its name, its driver and its port.", $model, $vendor);
},
data => sub {
[
{ label => N("Name:"), val => \$name, help => N("The name of your ups") },
{ label => N("Driver:"), val => \$driver, help => N("The driver that manages your ups") },
{ label => N("Port:"), val => \$port, format => \&detect_devices::serialPort2text, type => "combo",
list => [ &detect_devices::serialPorts() ], not_edit => 0,
help => N("The port on which is connected your ups") },
];
},
post => sub {
$port = '/dev/' . $port if $port !~ m!/dev/!;
return 'end';
},
},
end => {
name => sub {
N("Congratulations") . "\n\n" . N("The wizard successfully configured the new \"%s\" UPS device.",
$model . "|" . $vendor);
},
end => 1,
no_back => 1,
next => 0
},
},
});
$wiz->process($in);
$config->{$name}{driver} = $driver;
$config->{$name}{port} = $port;
$config->{$name}{$1} = $2 if $opts =~ /\b(.*)=(.*)\b/;
# refresh the GUI when needed:
$models{ups}->append_set(1 => $name, 2 => $driver, 3 => $port) if $models{ups};
log::explanations(qq(Configuring "$name" UPS));
}
my (@acls, @rules);
sub load_access_conf() {
foreach (cat_($files{access})) {
s/#.*//;
if (/^\s*ACL\s*(\S*)\s*(\S*)/) {
my ($ip, $mask) = split('/', $2);
push @acls, [ $1, $ip, $mask ];
} elsif (/^\s*ACCESS\s*(\S*)\s*(\S*)\s*(\S*)/) {
push @rules, [ $1, $2, $3 ];
}
}
}
#------------------------------------------------------------------
# misc gui data
sub edit_row {
my ($model, $o_iter) = @_;
# create new item if needed (that is when adding a new one) at end of list
my $iter = $o_iter || $model->append;
my $dialog = Gtk2::Dialog->new;
$dialog->set_transient_for($w->{real_window});
$dialog->set_modal(1);
gtkpack_($dialog->vbox,
#map {
#}
);
gtkadd($dialog->action_area,
gtksignal_connect(Gtk2::Button->new(N("Ok")), clicked => sub {
# create new item if needed (that is when adding a new one) at end of list
$iter ||= $model->append;
# $model->set($iter, 1 => $file->get_text); # FILL ME
$dialog->destroy;
# $modified++;
}),
gtksignal_connect(Gtk2::Button->new(N("Cancel")), clicked => sub { $dialog->destroy }),
);
$dialog->show_all;
}
sub add_callback {
my ($model, $_list, $_getindex) = @_;
edit_row($model);
}
sub edit_callback {
my ($model, $list) = @_;
my ($iter) = $list->get_selection->get_selected;
return unless $iter;
edit_row($model, $iter);
}
sub del_callback {
my ($model, $list) = @_;
#my (undef, $iter) = $list->get_selection->get_selected;
#my $removed_idx = $model->get($iter, 0); # 1st column is index
#@rules = grep { $_->{index} ne $removed_idx } @rules;
#$tree->remove($iter);
#sensitive_buttons(0);
#$modified++;
}
my @pages = (
{ name => N("UPS devices"),
columns => [ N("Name"), N("Driver"), N("Port") ], # N("Manufacturer"), N("Model"),
callbacks => {
add => sub {
eval { add_device_wizard($in, $struct) };
my $err = $@;
die $err if $err && $err !~ /wizcancel/;
$::WizardWindow->destroy if defined $::WizardWindow;
undef $::WizardWindow;
},
edit => sub {},
remove => sub {},
},
load => sub {
read_nut_config();
map { [ $_, @{$struct->{$_}}{qw(driver port)} ] } keys %$struct;
},
id => "ups",
},
{ name => N("UPS users"),
columns => [ N("Name") ],
callbacks => {
add => sub {
my ($name) = @_;
$users->{$name} = {};
},
edit => sub {},
remove => sub {},
},
load => sub {
$users = Libconf::Glueconf::NUT::Ups_conf->new({ filename => $files{users} });
map { [ $_ ] } keys %$users;
},
id => "users",
},
{ name => N("Access Control Lists"),
columns => [ N("Name"), N("IP address"), N("IP mask") ],
callbacks => {
add => sub {},
edit => sub {},
remove => sub {},
},
load => sub {
load_access_conf();
@acls;
},
id => "acls",
},
{ name => N("Rules"),
columns => [ N("Action"), N("Level"), N("ACL name"), N("Password") ],
callbacks => {
N("Add") => sub {},
N("Edit") => sub {},
N("Remove") => sub {},
},
load => sub { @rules }, # already loaded when we loaded acls
id => "rules",
},
);
#------------------------------------------------------------------
# initialize:
#$in = 'interactive'->vnew('su'); # require_root_capability();
$in = 'interactive'->vnew;
$ugtk2::wm_icon = "drakups";
if (!$::testing) {
$in->do_pkgs->ensure_is_installed('nut-server', '/usr/sbin/upsd') or $in->exit(1);
}
if (member('--wizard', @ARGV)) {
read_nut_config();
add_device_wizard($in, $struct);
writeconf();
$in->exit($@ ? 1 : 0);
}
$w = ugtk2->new(N("UPS Management"));
if (!$::isEmbedded) {
mygtk3::register_main_window($w->{real_window});
$w->{window}->set_size_request(500, 550);
$w->{rwindow}->set_title(N("DrakUPS"));
}
#------------------------------------------------------------------
# main window:
my $_msg = N("Welcome to the UPS configuration tools");
$w->{window}->add(gtkpack_(Gtk2::VBox->new,
if_(!$::isEmbedded, 0, Gtk2::Banner->new('drakups', N("DrakUPS"))),
1, my $nb = Gtk2::Notebook->new,
0, create_okcancel(
{
ok_clicked => sub {
#$_->{save}->() foreach @pages;
writeconf();
$w->exit;
},
cancel_clicked => sub { $w->exit },
},
),
),
);
#------------------------------------------------------------------
# build the notebook
foreach my $i (@pages) {
my $model = $models{$i->{id}} = Gtk2::ListStore->new("Glib::Int", ("Glib::String") x listlength(@{$i->{columns}}));
my (%buttons, $list);
$indexes{$i->{name}} = 0;
my $idx = \$indexes{$i->{name}};
my $getindex = sub { $$idx++ };
$nb->append_page(gtkpack_(Gtk2::VBox->new,
1, create_scrolled_window($list = Gtk2::TreeView->new_with_model($model),
[ 'automatic', 'automatic' ]),
0, gtkpack(Gtk2::HButtonBox->new,
(map {
my ($id, $label, $sub) = @$_;
gtksignal_connect($buttons{$id} = Gtk2::Button->new($label), clicked => sub {
$sub->($model, $list, $getindex);
});
} ([ 'add', N("Add"), $i->{callbacks}{add} || \&add_callback ],
[ 'edit', N("Edit"), \&edit_callback ],
[ 'remove', N("Remove"), \&del_callback ],
)
)
#(map {
# gtksignal_connect(Gtk2::Button->new($_), clicked => $i->{callbacks}{$_}),
#} keys %{$i->{callbacks}})
),
),
Gtk2::Label->new($i->{name}),
);
#$i->{list} = $list;
each_index {
$list->append_column(Gtk2::TreeViewColumn->new_with_attributes($_, Gtk2::CellRendererText->new, 'text' => $::i + 1));
} @{$i->{columns}};
my @u = $i->{load}->();
foreach my $line (@u) {
$model->append_set(0 => $getindex->(), map_index { $::i + 1 => $_ } @$line);
}
my $set_sensitive = sub {
my ($bool) = @_;
$buttons{$_}->set_sensitive($bool) foreach qw(remove edit);
};
$set_sensitive->(0);
$list->get_selection->signal_connect('changed' => sub {
my ($select) = @_;
my (undef, $iter) = $select->get_selected;
$set_sensitive->(defined $iter);
});
}
#------------------------------------------------------------------
# let's start the show:
$w->{rwindow}->show_all;
$w->main;
|