diff options
author | Olivier Blin <oblin@mandriva.com> | 2008-03-27 00:23:58 +0000 |
---|---|---|
committer | Olivier Blin <oblin@mandriva.com> | 2008-03-27 00:23:58 +0000 |
commit | 11a0d2833d337bec7cbd67eb68f6cf1b24c65838 (patch) | |
tree | d1a334dd10b0fe952b6f97829fc43feb38a79d0e | |
parent | a846fbabbe18c6fc3334bc7476aa00bca040e405 (diff) | |
download | drakguard-11a0d2833d337bec7cbd67eb68f6cf1b24c65838.tar drakguard-11a0d2833d337bec7cbd67eb68f6cf1b24c65838.tar.gz drakguard-11a0d2833d337bec7cbd67eb68f6cf1b24c65838.tar.bz2 drakguard-11a0d2833d337bec7cbd67eb68f6cf1b24c65838.tar.xz drakguard-11a0d2833d337bec7cbd67eb68f6cf1b24c65838.zip |
factorize whitelist/blacklist code
-rwxr-xr-x | bin/drakguard | 111 |
1 files changed, 49 insertions, 62 deletions
diff --git a/bin/drakguard b/bin/drakguard index 984fffb..026e5be 100755 --- a/bin/drakguard +++ b/bin/drakguard @@ -46,15 +46,20 @@ $allusers_list->get_selection->set_mode('multiple'); my $users_list = Gtk2::SimpleList->new(N("Allowed users") => 'text'); $users_list->get_selection->set_mode('multiple'); -my $whitelist = Gtk2::SimpleList->new(N("Allowed addresses") => 'text'); -$whitelist->get_selection->set_mode('multiple'); -@{$whitelist->{data}} = read_url_list($whitelist_url_file); -my $whitelist_entry; - -my $blacklist = Gtk2::SimpleList->new(N("Forbidden addresses") => 'text'); -$blacklist->get_selection->set_mode('multiple'); -@{$blacklist->{data}} = read_url_list($blacklist_url_file); -my $blacklist_entry; +my @url_lists = ( + { + tab_title => N("Blacklist"), + list_title => N("Forbidden addresses"), + remove_text => N("Remove from blacklist"), + file => $blacklist_url_file, + }, + { + tab_title => N("Whitelist"), + list_title => N("Allowed addresses"), + remove_text => N("Remove from whitelist"), + file => $whitelist_url_file, + } +); sub update_time_change() { gtkval_modify(\$allow_time_change, $enable && $time_control); @@ -118,34 +123,29 @@ gtkadd($w->{window}, ]), ]), ]), - gtknew('Label', text => N("Blacklist")), - gtknew('VBox', spacing => 5, children => [ - 0, gtknew('HBox', border_width => 5, spacing => 5, children_loose => [ - $blacklist_entry = gtknew('Entry'), - gtknew('Button', text => N("Add"), clicked => sub { - blacklist($blacklist_entry->get_text); - $blacklist_entry->set_text(""); - }), - ]), - 1, gtknew('ScrolledWindow', width => 500, height => 300, child => $blacklist), - 0, gtknew('HButtonBox', border_width => 5, layout => 'edge', children_loose => [ - gtknew('Button', text => N("Remove from blacklist"), clicked => sub { unblacklist(get_selected_blacklist()) }), - ]), - ]), - gtknew('Label', text => N("Whitelist")), - gtknew('VBox', spacing => 5, children => [ - 0, gtknew('HBox', border_width => 5, spacing => 5, children_loose => [ - $whitelist_entry = gtknew('Entry'), - gtknew('Button', text => N("Add"), clicked => sub { - whitelist($whitelist_entry->get_text); - $whitelist_entry->set_text(""); - }), - ]), - 1, gtknew('ScrolledWindow', width => 500, height => 300, child => $whitelist), - 0, gtknew('HButtonBox', border_width => 5, layout => 'edge', children_loose => [ - gtknew('Button', text => N("Remove from whitelist"), clicked => sub { unwhitelist(get_selected_whitelist()) }), - ]), - ]), + (map { + my $list = Gtk2::SimpleList->new($_->{list_title} => 'text'); + $list->get_selection->set_mode('multiple'); + @{$list->{data}} = read_url_list($_->{file}); + my $entry; + + ( + gtknew('Label', text => $_->{tab_title}), + gtknew('VBox', spacing => 5, children => [ + 0, gtknew('HBox', border_width => 5, spacing => 5, children_loose => [ + $entry = gtknew('Entry'), + gtknew('Button', text => N("Add"), clicked => sub { + list_add_addr($list, $entry->get_text); + $entry->set_text(""); + }), + ]), + 1, gtknew('ScrolledWindow', width => 500, height => 300, child => $list), + 0, gtknew('HButtonBox', border_width => 5, layout => 'edge', children_loose => [ + gtknew('Button', text => $_->{remove_text}, clicked => sub { list_remove_selected($list) }), + ]), + ]), + ); + } @url_lists), ]), 0, $w->create_okcancel( undef, undef, undef, @@ -158,38 +158,25 @@ Gtk2->main; ugtk2::exit(0); -sub list_remove_addr { +sub list_add_addr { my ($list, @addr) = @_; - #- workaround buggy Gtk2::SimpleList array abstraction, it destroys references - @$list = map { member($_->[0], @addr) ? () : [ @$_ ] } @$list; -} - -sub get_selected_blacklist() { - uniq(map { $blacklist->{data}[$_][0] } $blacklist->get_selected_indices); -} - -sub blacklist { - my @addr = @_; - push @{$blacklist->{data}}, $_ foreach @addr; + push @{$list->{data}}, $_ foreach @addr; } -sub unblacklist { - my @addr = @_; - list_remove_addr($blacklist->{data}, @addr); -} - -sub get_selected_whitelist() { - uniq(map { $whitelist->{data}[$_][0] } $whitelist->get_selected_indices); +sub list_remove_addr { + my ($list, @addr) = @_; + #- workaround buggy Gtk2::SimpleList array abstraction, it destroys references + @{$list->{data}} = map { member($_->[0], @addr) ? () : [ @$_ ] } @{$list->{data}}; } -sub whitelist { - my @addr = @_; - push @{$whitelist->{data}}, $_ foreach @addr; +sub list_get_selected { + my ($list) = @_; + uniq(map { $list->{data}[$_][0] } $list->get_selected_indices); } -sub unwhitelist { - my @addr = @_; - list_remove_addr($whitelist->{data}, @addr); +sub list_remove_selected { + my ($list) = @_; + list_remove_addr($list, list_get_selected($list)); } sub quit_gui() { Gtk2->main_quit } |