DirTree/Win32.pm untested

filter: Statementstmt 0.0% 0 / 90 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 167 CC 9 SCAR 19.9
CC
9
Cov
0%
add_root
12.0
_find_node
12.0
AddChildren
12.0
CRAP 7.3
1 package Wx::Perl::DirTree::Win32;
2  
3 use strict;
4 use warnings;
5 use Cwd;
6 use Exporter;
7 use File::Spec;
8 use Win32::API;
9  
10 our @ISA = qw(Exporter);
11 our @EXPORT = qw(
12 add_root
13 AddChildren
14 );
15  
16 our $VERSION = 0.02;
17  
18 sub add_root {
19 my ($self, $args) = @_;
20
21 my $dir = [ '/', $ENV{COMPUTERNAME} || 'My Computer' ];
22
23 if( exists $args->{dir} and -e $args->{dir} and
24 exists $args->{is_root} and $args->{is_root} ){
25 my $path = Cwd::abs_path( $args->{dir} );
26 my @dirs = File::Spec->splitdir( $path );
27 $dir = [ $path, $dirs[-1] ];
28 }
29
30 my $root = $self->AddRoot( $dir->[1] );
31 $self->SetPlData( $root, $dir->[0] );
32 $self->SetItemHasChildren( $root, 1 );
33 $self->Expand( $root );
34
35 if( exists $args->{dir} and -e $args->{dir} and
36 ( ( exists $args->{is_root} and not $args->{is_root} ) or
37 not exists $args->{is_root} )){
38
39 my $path = Cwd::abs_path( $args->{dir} );
40 my @dirs = File::Spec->splitdir( $path );
41 my $tmp_item = _find_node( $self, $root, $dirs[0] );
42 $self->Expand( $tmp_item );
43
44 for my $i ( 1 .. $#dirs ){
45 my $subdir = File::Spec->catdir( @dirs[0..$i] );
46
47 my @data = _get_content( $subdir );
48 _insert_items( $self, $tmp_item, @data );
49
50 $tmp_item = _find_node( $self, $tmp_item, $subdir );
51 $self->Expand( $tmp_item );
52 }
53 }
54 }
55  
56 sub _find_node {
57 my ($tree,$parent,$value) = @_;
58
59 $value = File::Spec->catdir( $value ) unless $value =~ m!\\$!;
60
61 my ($id,$cookie) = $tree->GetFirstChild( $parent );
62
63 while( $id->IsOk ){
64 if( $tree->GetPlData( $id ) eq $value ){
65 return $id;
66 }
67
68 ($id,$cookie) = $tree->GetNextChild( $parent, $cookie );
69 }
70 }
71  
72 sub AddChildren {
73 my ($self,$event) = @_;
74
75 my $tree = $event->GetEventObject;
76 my $item = $event->GetItem;
77 my $data = $tree->GetPlData( $item );
78
79 if( $tree->GetChildrenCount( $item, 0 ) ){
80 #warn $data;
81
82 }
83 else{
84 my @array;
85
86 if( $data and $data eq '/' ){
87 @array = _get_drives();
88 }
89 else{
90 @array = _get_content( $data );
91 }
92  
93 _insert_items( $tree, $item, @array );
94 }
95 }
96  
97 sub _insert_items {
98 my ( $tree, $item, @data ) = @_;
99  
100 for my $child ( @data){
101 my ($label,$value,$is_dir) = @$child;
102 my $childobj = $tree->AppendItem( $item, $label );
103 $tree->SetPlData( $childobj, $value );
104 $tree->SetItemHasChildren( $childobj, 1 ) if $is_dir;
105 }
106 }
107  
108 sub _get_content {
109 my ($dir) = @_;
110
111 opendir my $dirh, $dir or die $!;
112 my @files = sort grep{ !/^\.\.?$/ }readdir $dirh;
113 closedir $dirh;
114
115 my @list;
116
117 for my $file ( @files ){
118 my $is_dir = -d $dir . '/' . $file;
119 my $value = $is_dir ?
120 File::Spec->catdir( $dir, $file ) :
121 File::Spec->catfile( $dir, $file ) ;
122 push @list, [ $file, $value, $is_dir ];
123 }
124 return @list;
125 }
126  
127 sub _get_drives {
128 my $function = Win32::API->new( 'kernel32', 'GetLogicalDriveStringsA', 'NP', 'N' );
129
130 my $drivestr = ' 'x1024;
131 my $ret = $function->Call( 1024, $drivestr );
132
133 my $drives = substr( $drivestr, 0, $ret );
134 my @list = split /\0/, $drives;
135
136 @list = map{ [ $_, $_, 1 ] }@list;
137
138 return @list;
139 }
140  
141 1;
142  
143 # ABSTRACT: module for the directory tree on Win32
144  
145  
146 __END__