X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=lib%2FFile%2FKDBX%2FGroup.pm;h=f89d933db0d6d544c88f022fca5f3d47c53ed0af;hb=1d0a10e989a4d0487aa13cf4f56e533d3795469d;hp=0870dd1ffb8ab585016c5a684d6b05993e3bd019;hpb=37b09e0f2832514b33de4499a83f22d5ffe7c0a3;p=chaz%2Fp5-File-KDBX diff --git a/lib/File/KDBX/Group.pm b/lib/File/KDBX/Group.pm index 0870dd1..f89d933 100644 --- a/lib/File/KDBX/Group.pm +++ b/lib/File/KDBX/Group.pm @@ -22,48 +22,35 @@ our $VERSION = '999.999'; # VERSION sub _parent_container { 'groups' } -my @ATTRS = qw(uuid custom_data entries groups icon_id); -my %ATTRS = ( - # uuid => sub { generate_uuid(printable => 1) }, - name => ['', coerce => \&to_string], - notes => ['', coerce => \&to_string], - tags => ['', coerce => \&to_string], - # icon_id => sub { defined $_[1] ? icon($_[1]) : ICON_FOLDER }, - custom_icon_uuid => [undef, coerce => \&to_uuid], - is_expanded => [false, coerce => \&to_bool], - default_auto_type_sequence => ['', coerce => \&to_string], - enable_auto_type => [undef, coerce => \&to_tristate], - enable_searching => [undef, coerce => \&to_tristate], - last_top_visible_entry => [undef, coerce => \&to_uuid], - # custom_data => {}, - previous_parent_group => [undef, coerce => \&to_uuid], - # entries => [], - # groups => [], - times => [{}], -); - -my %ATTRS_TIMES = ( - last_modification_time => [sub { gmtime }, coerce => \&to_time], - creation_time => [sub { gmtime }, coerce => \&to_time], - last_access_time => [sub { gmtime }, coerce => \&to_time], - expiry_time => [sub { gmtime }, coerce => \&to_time], - expires => [false, coerce => \&to_bool], - usage_count => [0, coerce => \&to_number], - location_changed => [sub { gmtime }, coerce => \&to_time], -); - -has icon_id => ICON_FOLDER, coerce => sub { icon($_[0]) }; - -while (my ($attr, $default) = each %ATTRS) { - has $attr => @$default; -} -while (my ($attr, $default) = each %ATTRS_TIMES) { - has $attr => @$default, store => 'times'; -} - -sub _set_default_attributes { +# has uuid => sub { generate_uuid(printable => 1) }; +has name => '', coerce => \&to_string; +has notes => '', coerce => \&to_string; +has tags => '', coerce => \&to_string; +has icon_id => ICON_FOLDER, coerce => \&to_icon_constant; +has custom_icon_uuid => undef, coerce => \&to_uuid; +has is_expanded => false, coerce => \&to_bool; +has default_auto_type_sequence => '', coerce => \&to_string; +has enable_auto_type => undef, coerce => \&to_tristate; +has enable_searching => undef, coerce => \&to_tristate; +has last_top_visible_entry => undef, coerce => \&to_uuid; +# has custom_data => {}; +has previous_parent_group => undef, coerce => \&to_uuid; +# has entries => []; +# has groups => []; +has times => {}; + +has last_modification_time => sub { gmtime }, store => 'times', coerce => \&to_time; +has creation_time => sub { gmtime }, store => 'times', coerce => \&to_time; +has last_access_time => sub { gmtime }, store => 'times', coerce => \&to_time; +has expiry_time => sub { gmtime }, store => 'times', coerce => \&to_time; +has expires => false, store => 'times', coerce => \&to_bool; +has usage_count => 0, store => 'times', coerce => \&to_number; +has location_changed => sub { gmtime }, store => 'times', coerce => \&to_time; + +my @ATTRS = qw(uuid custom_data entries groups); +sub _set_nonlazy_attributes { my $self = shift; - $self->$_ for @ATTRS, keys %ATTRS, keys %ATTRS_TIMES; + $self->$_ for @ATTRS, list_attributes(ref $self); } sub uuid { @@ -115,7 +102,7 @@ sub add_entry { $entry->kdbx($kdbx) if $kdbx; push @{$self->{entries} ||= []}, $entry->remove; - return $entry->_set_group($self); + return $entry->_set_group($self)->_signal('added', $self); } sub remove_entry { @@ -125,9 +112,8 @@ sub remove_entry { for (my $i = 0; $i < @$objects; ++$i) { my $o = $objects->[$i]; next if $uuid ne $o->uuid; + $o->_set_group(undef)->_signal('removed'); return splice @$objects, $i, 1; - $o->_set_group(undef); - return @$objects, $i, 1; } } @@ -141,10 +127,44 @@ sub groups { return $groups; } +=method all_groups + + \@groups = $group->all_groups(%options); + +Get all groups within a group, deeply, in a flat array. Supported options: + +=cut + sub all_groups { my $self = shift; - # FIXME - shouldn't have to delegate to the database to get this - return $self->kdbx->all_groups(base => $self, include_base => false); + + my @groups; + for my $subgroup (@{$self->groups}) { + push @groups, @{$subgroup->all_groups}; + } + + return \@groups; +} + +=method find_groups + + @groups = $kdbx->find_groups($query, %options); + +Find all groups deeply that match to a query. Options are the same as for L. + +See L for a description of what C<$query> can be. + +=cut + +sub find_groups { + my $self = shift; + my $query = shift or throw 'Must provide a query'; + my %args = @_; + my %all_groups = ( # FIXME + base => $args{base}, + include_base => $args{include_base}, + ); + return @{search($self->all_groups(%all_groups), is_arrayref($query) ? @$query : $query)}; } sub _kpx_groups { shift->groups(@_) } @@ -171,7 +191,7 @@ sub add_group { $group->kdbx($kdbx) if $kdbx; push @{$self->{groups} ||= []}, $group->remove; - return $group->_set_group($self); + return $group->_set_group($self)->_signal('added', $self); } sub remove_group { @@ -181,7 +201,7 @@ sub remove_group { for (my $i = 0; $i < @$objects; ++$i) { my $o = $objects->[$i]; next if $uuid ne $o->uuid; - $o->_set_group(undef); + $o->_set_group(undef)->_signal('removed'); return splice @$objects, $i, 1; } }