| 1 | package Wx::Perl::DirTree::Linux; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Cwd; | ||
| 6 | use Exporter; | ||
| 7 | use File::Spec; | ||
| 8 | |||
| 9 | our @ISA = qw(Exporter); | ||
| 10 | our @EXPORT = qw( | ||
| 11 | add_root | ||
| 12 | AddChildren | ||
| 13 | ); | ||
| 14 | |||
| 15 | our $VERSION = 0.02; | ||
| 16 | |||
| 17 | sub add_root { | ||
| 18 | my ($self,$args) = @_; | ||
| 19 | |||
| 20 | my $dir = [ '/', '/' ]; | ||
| 21 | |||
| 22 | if( exists $args->{dir} and -e $args->{dir} and | ||
| 23 | exists $args->{is_root} and $args->{is_root} ){ | ||
| 24 | my $path = Cwd::abs_path( $args->{dir} ); | ||
| 25 | my @dirs = File::Spec->splitdir( $path ); | ||
| 26 | $dir = [ $path, $dirs[-1] ]; | ||
| 27 | } | ||
| 28 | |||
| 29 | my $root = $self->AddRoot( $dir->[1] ); | ||
| 30 | $self->SetPlData( $root, $dir->[0] ); | ||
| 31 | $self->SetItemHasChildren( $root, 1 ); | ||
| 32 | $self->Expand( $root ); | ||
| 33 | |||
| 34 | if( exists $args->{dir} and -e $args->{dir} and | ||
| 35 | ( ( exists $args->{is_root} and not $args->{is_root} ) or | ||
| 36 | not exists $args->{is_root} )){ | ||
| 37 | |||
| 38 | my $path = Cwd::abs_path( $args->{dir} ); | ||
| 39 | my @dirs = File::Spec->splitdir( $path ); | ||
| 40 | my $tmp_item = _find_node( $self, $root, $dirs[0] ); | ||
| 41 | $self->Expand( $tmp_item ); | ||
| 42 | |||
| 43 | for my $i ( 1 .. $#dirs ){ | ||
| 44 | my $subdir = File::Spec->catdir( @dirs[0..$i] ); | ||
| 45 | |||
| 46 | my @data = _get_content( $subdir ); | ||
| 47 | _insert_items( $self, $tmp_item, @data ); | ||
| 48 | |||
| 49 | $tmp_item = _find_node( $self, $tmp_item, $subdir ); | ||
| 50 | $self->Expand( $tmp_item ); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | sub _find_node { | ||
| 56 | my ($tree,$parent,$value) = @_; | ||
| 57 | |||
| 58 | $value = File::Spec->catdir( $value ) unless $value =~ m!\\$!; | ||
| 59 | |||
| 60 | my ($id,$cookie) = $tree->GetFirstChild( $parent ); | ||
| 61 | |||
| 62 | while( $id->IsOk ){ | ||
| 63 | if( $tree->GetPlData( $id ) eq $value ){ | ||
| 64 | return $id; | ||
| 65 | } | ||
| 66 | |||
| 67 | ($id,$cookie) = $tree->GetNextChild( $parent, $cookie ); | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | sub AddChildren { | ||
| 72 | my ($self,$event) = @_; | ||
| 73 | |||
| 74 | my $tree = $event->GetEventObject; | ||
| 75 | my $item = $event->GetItem; | ||
| 76 | my $data = $tree->GetPlData( $item ); | ||
| 77 | |||
| 78 | if( $tree->GetChildrenCount( $item, 0 ) ){ | ||
| 79 | } | ||
| 80 | else{ | ||
| 81 | my @array = _get_content( $data ); | ||
| 82 | _insert_items( $tree, $item, @array ); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | sub _insert_items { | ||
| 87 | my ( $tree, $item, @data ) = @_; | ||
| 88 | |||
| 89 | for my $child ( @data){ | ||
| 90 | my ($label,$value,$is_dir) = @$child; | ||
| 91 | my $childobj = $tree->AppendItem( $item, $label ); | ||
| 92 | $tree->SetPlData( $childobj, $value ); | ||
| 93 | $tree->SetItemHasChildren( $childobj, 1 ) if $is_dir; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | sub _get_content { | ||
| 98 | my ($dir) = @_; | ||
| 99 | |||
| 100 | opendir my $dirh, $dir or die $!; | ||
| 101 | my @files = sort grep{ !/^\.\.?$/ }readdir $dirh; | ||
| 102 | closedir $dirh; | ||
| 103 | |||
| 104 | return map{ [ $_, $dir . '/' . $_, -d $dir . '/' . $_ ] }(@files); | ||
| 105 | } | ||
| 106 | |||
| 107 | 1; | ||
| 108 | |||
| 109 | # ABSTRACT: module for the directory tree on Linux | ||
| 110 | |||
| 111 | |||
| 112 | __END__ |