| 1 | package BBS::Perm::Plugin::Feed; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use Carp; | ||
| 6 | use Gtk2; | ||
| 7 | use Glib qw/TRUE FALSE/; | ||
| 8 | use File::Slurp; | ||
| 9 | use Encode; | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | my ( $class, %args ) = @_; | ||
| 13 | my $self = {}; | ||
| 14 | bless $self, ref $class || $class; | ||
| 15 | |||
| 16 | my $entry = Gtk2::Entry->new; | ||
| 17 | my $label = Gtk2::Label->new_with_mnemonic( $args{label} || '_Feed: ' ); | ||
| 18 | my $widget = $args{widget} || Gtk2::HBox->new; | ||
| 19 | $widget->pack_start( $label, FALSE, FALSE, 0 ); | ||
| 20 | $widget->pack_start( $entry, TRUE, TRUE, 0 ); | ||
| 21 | $entry->signal_connect( changed => sub { $self->_update_store } ); | ||
| 22 | my $entry_c = Gtk2::EntryCompletion->new; | ||
| 23 | $entry->set_completion($entry_c); | ||
| 24 | my $store = Gtk2::ListStore->new('Glib::String'); | ||
| 25 | |||
| 26 | $entry_c->set_model($store); | ||
| 27 | $entry_c->set_text_column(0); | ||
| 28 | $entry_c->set_popup_completion(TRUE); | ||
| 29 | $entry_c->set_inline_completion(TRUE); | ||
| 30 | $self->{entry} = $entry; | ||
| 31 | $self->{label} = $label; | ||
| 32 | $self->{_entry_c} = $entry_c; | ||
| 33 | $self->{_store} = $store; | ||
| 34 | $self->{widget} = $widget; | ||
| 35 | return $self; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub _update_store { | ||
| 39 | my $self = shift; | ||
| 40 | my $store = $self->{_store}; | ||
| 41 | my $text = $self->{entry}->get_text; | ||
| 42 | $store->clear; | ||
| 43 | if ( $text =~ m{^([^:]*.*/)} ) { | ||
| 44 | my $dir = $1; | ||
| 45 | my $dh; | ||
| 46 | opendir $dh, $1; | ||
| 47 | |||
| 48 | my @names = map { $dir . $_ } | ||
| 49 | grep { ( $_ !~ /^\./ ) && ( -d "$dir/$_" || -T "$dir/$_" ) } | ||
| 50 | readdir $dh; | ||
| 51 | for (@names) { | ||
| 52 | my $iter = $store->append; | ||
| 53 | $store->set( $iter, 0, $_ ); | ||
| 54 | } | ||
| 55 | closedir $dh; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | sub text { | ||
| 60 | my $self = shift; | ||
| 61 | my $input = $self->entry->get_text; | ||
| 62 | my $text; | ||
| 63 | |||
| 64 | my $encoding = 'utf8'; # default is utf8 | ||
| 65 | if ( $ENV{LLC_ALL} && $ENV{LLC_ALL} =~ /\.(.*)$/ ) { | ||
| 66 | $encoding = lc $1; | ||
| 67 | } | ||
| 68 | elsif ( $ENV{LANG} && $ENV{LANG} =~ /\.(.*)$/ ) { | ||
| 69 | $encoding = lc $1; | ||
| 70 | } | ||
| 71 | |||
| 72 | if ( $input =~ /^\s*:\s*(.*)/ ) { | ||
| 73 | $text = decode $encoding, `$1`; | ||
| 74 | } | ||
| 75 | elsif ( -f $input ) { | ||
| 76 | $text = decode $encoding, read_file($input); | ||
| 77 | } | ||
| 78 | else { | ||
| 79 | carp 'bad input'; | ||
| 80 | } | ||
| 81 | return $text; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub widget { | ||
| 85 | return shift->{widget}; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub AUTOLOAD { | ||
| 89 | our $AUTOLOAD; | ||
| 90 | no strict 'refs'; | ||
| 91 | if ( $AUTOLOAD =~ /.*::(.*)/ ) { | ||
| 92 | my $element = $1; | ||
| 93 | *$AUTOLOAD = sub { return shift->{$element} }; | ||
| 94 | goto &$AUTOLOAD; | ||
| 95 | } | ||
| 96 | |||
| 97 | } | ||
| 98 | |||
| 99 | sub DESTROY { } | ||
| 100 | |||
| 101 | 1; | ||
| 102 | |||
| 103 | __END__ |