| 1 | package Tree::Navigator::Node::Win32::Registry; | ||
| 2 | use utf8; | ||
| 3 | use Moose; | ||
| 4 | use namespace::autoclean; | ||
| 5 | extends 'Tree::Navigator::Node'; | ||
| 6 | |||
| 7 | use Win32API::Registry qw/:HKEY_ :Func :KEY_/; | ||
| 8 | use Params::Validate qw/validate SCALAR SCALARREF/; | ||
| 9 | |||
| 10 | my %root_key = ( | ||
| 11 | 'HKEY_CLASSES_ROOT' => HKEY_CLASSES_ROOT, | ||
| 12 | 'HKEY_CURRENT_CONFIG' => HKEY_CURRENT_CONFIG, | ||
| 13 | 'HKEY_CURRENT_USER' => HKEY_CURRENT_USER, | ||
| 14 | 'HKEY_DYN_DATA' => HKEY_DYN_DATA, | ||
| 15 | 'HKEY_LOCAL_MACHINE' => HKEY_LOCAL_MACHINE, | ||
| 16 | 'HKEY_PERFORMANCE_DATA' => HKEY_PERFORMANCE_DATA, | ||
| 17 | 'HKEY_USERS' => HKEY_USERS, | ||
| 18 | ); | ||
| 19 | |||
| 20 | $root_key{'HKLM'} = $root_key{'HKEY_LOCAL_MACHINE'}; | ||
| 21 | $root_key{'HKCU'} = $root_key{'HKEY_CURRENT_USER'}; | ||
| 22 | |||
| 23 | |||
| 24 | sub MOUNT { | ||
| 25 | my ($class, $mount_args) = @_; | ||
| 26 | my @mount_point = %{$mount_args->{mount_point} || {}}; | ||
| 27 | $mount_args->{mount_point} = validate(@mount_point , { | ||
| 28 | key => {type => SCALAR}, | ||
| 29 | exclude => {type => SCALARREF, isa => 'Regexp', optional => 1}, | ||
| 30 | }); | ||
| 31 | |||
| 32 | my $key_name = $mount_args->{mount_point}{key}; | ||
| 33 | my $reg_key = $root_key{$key_name} | ||
| 34 | or die "no such root key: $key_name"; | ||
| 35 | $mount_args->{mount_point}{root_key} = $reg_key; | ||
| 36 | } | ||
| 37 | |||
| 38 | |||
| 39 | |||
| 40 | sub _root_key { | ||
| 41 | my $self = shift; | ||
| 42 | return $self->mount_point->{_key}; | ||
| 43 | } | ||
| 44 | |||
| 45 | |||
| 46 | sub _key { | ||
| 47 | my $self = shift; | ||
| 48 | my $root_key = $self->mount_point->{root_key}; | ||
| 49 | |||
| 50 | my $path = $self->path || ''; | ||
| 51 | $path =~ tr[/][\\]; | ||
| 52 | my $key; | ||
| 53 | RegOpenKeyEx($root_key, $path, 0, KEY_READ, $key) | ||
| 54 | or die $^E; | ||
| 55 | return $key; | ||
| 56 | } | ||
| 57 | |||
| 58 | |||
| 59 | sub _children { | ||
| 60 | my $self = shift; | ||
| 61 | |||
| 62 | my $key = $self->_key; | ||
| 63 | my $i = 0; | ||
| 64 | my @children; | ||
| 65 | while (1) { | ||
| 66 | my $child_name; | ||
| 67 | RegEnumKeyEx($key, $i++, $child_name, [], [], [], [], []) | ||
| 68 | or last; | ||
| 69 | push @children, $child_name; | ||
| 70 | } | ||
| 71 | return \@children; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub _child { | ||
| 75 | my ($self, $child_path) = @_; | ||
| 76 | my $class = ref $self; | ||
| 77 | |||
| 78 | # TODO? should check for existence of subkey ? | ||
| 79 | |||
| 80 | return $class->new( | ||
| 81 | mount_point => $self->mount_point, | ||
| 82 | path => $self->_join_path($self->path, $child_path), | ||
| 83 | ); | ||
| 84 | } | ||
| 85 | |||
| 86 | |||
| 87 | sub _attributes { | ||
| 88 | my $self = shift; | ||
| 89 | |||
| 90 | my $key = $self->_key; | ||
| 91 | my $i = 0; | ||
| 92 | my %attrs; | ||
| 93 | while (1) { | ||
| 94 | my $attr_name; | ||
| 95 | my $attr_value; | ||
| 96 | RegEnumValue($key, $i++, $attr_name, [], [], [], $attr_value, []) | ||
| 97 | or last; | ||
| 98 | $attrs{$attr_name} = $attr_value; | ||
| 99 | } | ||
| 100 | return \%attrs; | ||
| 101 | } | ||
| 102 | |||
| 103 | __PACKAGE__->meta->make_immutable; | ||
| 104 | |||
| 105 | |||
| 106 | 1; # End of Tree::Navigator::Node::Win32::Registry; | ||
| 107 | |||
| 108 | __END__ |