]> Dogcows Code - chaz/p5-File-KDBX/blobdiff - lib/File/KDBX/Group.pm
Rename group iterator accessors
[chaz/p5-File-KDBX] / lib / File / KDBX / Group.pm
index 3b8b458ada0fccfaced3a9390f14b632cf7e4255..9ef603b891c58bc286c0b7e96126f2ae1b15b22e 100644 (file)
@@ -5,7 +5,7 @@ use warnings;
 use strict;
 
 use Devel::GlobalDestruction;
-use File::KDBX::Constants qw(:icon);
+use File::KDBX::Constants qw(:bool :icon :iteration);
 use File::KDBX::Error;
 use File::KDBX::Iterator;
 use File::KDBX::Util qw(:assert :class :coercion generate_uuid);
@@ -21,7 +21,43 @@ extends 'File::KDBX::Object';
 
 our $VERSION = '999.999'; # VERSION
 
-sub _parent_container { 'groups' }
+=attr name
+
+The human-readable name of the group.
+
+=attr notes
+
+Free form text string associated with the group.
+
+=attr is_expanded
+
+Whether or not subgroups are visible when listed for user selection.
+
+=attr default_auto_type_sequence
+
+The default auto-type keystroke sequence, inheritable by entries and subgroups.
+
+=attr enable_auto_type
+
+Whether or not the entry is eligible to be matched for auto-typing, inheritable by entries and subgroups.
+
+=attr enable_searching
+
+Whether or not entries within the group can show up in search results, inheritable by subgroups.
+
+=attr last_top_visible_entry
+
+The UUID of the entry visible at the top of the list.
+
+=attr entries
+
+Array of entries contained within the group.
+
+=attr groups
+
+Array of subgroups contained within the group.
+
+=cut
 
 # has uuid                        => sub { generate_uuid(printable => 1) };
 has name                        => '',          coerce => \&to_string;
@@ -67,6 +103,14 @@ sub uuid {
 
 ##############################################################################
 
+=method entries
+
+    \@entries = $group->entries;
+
+Get an array of direct child entries within a group.
+
+=cut
+
 sub entries {
     my $self = shift;
     my $entries = $self->{entries} //= [];
@@ -77,7 +121,21 @@ sub entries {
     return $entries;
 }
 
-sub entries_deeply {
+=method all_entries
+
+    \&iterator = $kdbx->all_entries(%options);
+
+Get an L<File::KDBX::Iterator> over I<entries> within a group. Supports the same options as L</groups>,
+plus some new ones:
+
+=for :list
+* C<auto_type> - Only include entries with auto-type enabled (default: false, include all)
+* C<searching> - Only include entries within groups with searching enabled (default: false, include all)
+* C<history> - Also include historical entries (default: false, include only current entries)
+
+=cut
+
+sub all_entries {
     my $self = shift;
     my %args = @_;
 
@@ -85,7 +143,7 @@ sub entries_deeply {
     my $auto_type   = delete $args{auto_type};
     my $history     = delete $args{history};
 
-    my $groups = $self->groups_deeply(%args);
+    my $groups = $self->all_groups(%args);
     my @entries;
 
     return File::KDBX::Iterator->new(sub {
@@ -128,20 +186,39 @@ sub add_entry {
     return $entry->_set_group($self)->_signal('added', $self);
 }
 
+=method remove_entry
+
+    $entry = $group->remove_entry($entry);
+    $entry = $group->remove_entry($entry_uuid);
+
+Remove an entry from a group's array of entries. Returns the entry removed or C<undef> if nothing removed.
+
+=cut
+
 sub remove_entry {
     my $self = shift;
     my $uuid = is_ref($_[0]) ? $self->_wrap_entry(shift)->uuid : shift;
+    my %args = @_;
     my $objects = $self->{entries};
     for (my $i = 0; $i < @$objects; ++$i) {
-        my $o = $objects->[$i];
-        next if $uuid ne $o->uuid;
-        $o->_set_group(undef)->_signal('removed');
+        my $object = $objects->[$i];
+        next if $uuid ne $object->uuid;
+        $object->_set_group(undef);
+        $object->_signal('removed') if $args{signal} // 1;
         return splice @$objects, $i, 1;
     }
 }
 
 ##############################################################################
 
+=method groups
+
+    \@groups = $group->groups;
+
+Get an array of direct subgroups within a group.
+
+=cut
+
 sub groups {
     my $self = shift;
     my $groups = $self->{groups} //= [];
@@ -152,14 +229,26 @@ sub groups {
     return $groups;
 }
 
-sub groups_deeply {
+=method all_groups
+
+    \&iterator = $group->all_groups(%options);
+
+Get an L<File::KDBX::Iterator> over I<groups> within a groups, deeply. Options:
+
+=for :list
+* C<inclusive> - Include C<$group> itself in the results (default: true)
+* C<algorithm> - Search algorithm, one of C<ids>, C<bfs> or C<dfs> (default: C<ids>)
+
+=cut
+
+sub all_groups {
     my $self = shift;
     my %args = @_;
 
     my @groups = ($args{inclusive} // 1) ? $self : @{$self->groups};
     my $algo = lc($args{algorithm} || 'ids');
 
-    if ($algo eq 'dfs') {
+    if ($algo eq ITERATION_DFS) {
         my %visited;
         return File::KDBX::Iterator->new(sub {
             my $next = shift @groups or return;
@@ -173,7 +262,7 @@ sub groups_deeply {
             $next;
         });
     }
-    elsif ($algo eq 'bfs') {
+    elsif ($algo eq ITERATION_BFS) {
         return File::KDBX::Iterator->new(sub {
             my $next = shift @groups or return;
             push @groups, @{$next->groups};
@@ -214,21 +303,42 @@ sub add_group {
     return $group->_set_group($self)->_signal('added', $self);
 }
 
+=method remove_group
+
+    $removed_group = $group->remove_group($group);
+    $removed_group = $group->remove_group($group_uuid);
+
+Remove a group from a group's array of subgroups. Returns the group removed or C<undef> if nothing removed.
+
+=cut
+
 sub remove_group {
     my $self = shift;
     my $uuid = is_ref($_[0]) ? $self->_wrap_group(shift)->uuid : shift;
+    my %args = @_;
     my $objects = $self->{groups};
     for (my $i = 0; $i < @$objects; ++$i) {
-        my $o = $objects->[$i];
-        next if $uuid ne $o->uuid;
-        $o->_set_group(undef)->_signal('removed');
+        my $object = $objects->[$i];
+        next if $uuid ne $object->uuid;
+        $object->_set_group(undef);
+        $object->_signal('removed') if $args{signal} // 1;
         return splice @$objects, $i, 1;
     }
 }
 
 ##############################################################################
 
-sub objects_deeply {
+=method all_objects
+
+    \&iterator = $groups->all_objects(%options);
+
+Get an L<File::KDBX::Iterator> over I<objects> within a group, deeply. Groups and entries are considered
+objects, so this is essentially a combination of L</groups> and L</entries>. This won't often be useful, but
+it can be convenient for maintenance tasks. This method takes the same options as L</groups> and L</entries>.
+
+=cut
+
+sub all_objects {
     my $self = shift;
     my %args = @_;
 
@@ -236,7 +346,7 @@ sub objects_deeply {
     my $auto_type   = delete $args{auto_type};
     my $history     = delete $args{history};
 
-    my $groups = $self->groups_deeply(%args);
+    my $groups = $self->all_groups(%args);
     my @entries;
 
     return File::KDBX::Iterator->new(sub {
@@ -296,20 +406,149 @@ sub remove_object {
 
 ##############################################################################
 
+=method effective_default_auto_type_sequence
+
+    $text = $group->effective_default_auto_type_sequence;
+
+Get the value of L</default_auto_type_sequence>, if set, or get the inherited effective default auto-type
+sequence of the parent.
+
+=cut
+
+sub effective_default_auto_type_sequence {
+    my $self = shift;
+    my $sequence = $self->default_auto_type_sequence;
+    return $sequence if defined $sequence;
+
+    my $parent = $self->group or return '{USERNAME}{TAB}{PASSWORD}{ENTER}';
+    return $parent->effective_default_auto_type_sequence;
+}
+
+=method effective_enable_auto_type
+
+    $text = $group->effective_enable_auto_type;
+
+Get the value of L</enable_auto_type>, if set, or get the inherited effective auto-type enabled value of the
+parent.
+
+=cut
+
+sub effective_enable_auto_type {
+    my $self = shift;
+    my $enabled = $self->enable_auto_type;
+    return $enabled if defined $enabled;
+
+    my $parent = $self->group or return true;
+    return $parent->effective_enable_auto_type;
+}
+
+=method effective_enable_searching
+
+    $text = $group->effective_enable_searching;
+
+Get the value of L</enable_searching>, if set, or get the inherited effective searching enabled value of the
+parent.
+
+=cut
+
+sub effective_enable_searching {
+    my $self = shift;
+    my $enabled = $self->enable_searching;
+    return $enabled if defined $enabled;
+
+    my $parent = $self->group or return true;
+    return $parent->effective_enable_searching;
+}
+
+##############################################################################
+
+=method is_empty
+
+    $bool = $group->is_empty;
+
+Get whether or not the group is empty (has no subgroups or entries).
+
+=cut
+
+sub is_empty {
+    my $self = shift;
+    return @{$self->groups} == 0 && @{$self->entries} == 0;
+}
+
 =method is_root
 
     $bool = $group->is_root;
 
-Determine if a group is the root group of its associated database.
+Determine if a group is the root group of its connected database.
 
 =cut
 
 sub is_root {
     my $self = shift;
-    my $kdbx = eval { $self->kdbx } or return;
+    my $kdbx = eval { $self->kdbx } or return FALSE;
     return Hash::Util::FieldHash::id($kdbx->root) == Hash::Util::FieldHash::id($self);
 }
 
+=method is_recycle_bin
+
+    $bool = $group->is_recycle_bin;
+
+Get whether or not a group is the recycle bin of its connected database.
+
+=cut
+
+sub is_recycle_bin {
+    my $self    = shift;
+    my $kdbx    = eval { $self->kdbx } or return FALSE;
+    my $group   = $kdbx->recycle_bin;
+    return $group && Hash::Util::FieldHash::id($group) == Hash::Util::FieldHash::id($self);
+}
+
+=method is_entry_templates
+
+    $bool = $group->is_entry_templates;
+
+Get whether or not a group is the group containing entry template in its connected database.
+
+=cut
+
+sub is_entry_templates {
+    my $self    = shift;
+    my $kdbx    = eval { $self->kdbx } or return FALSE;
+    my $group   = $kdbx->entry_templates;
+    return $group && Hash::Util::FieldHash::id($group) == Hash::Util::FieldHash::id($self);
+}
+
+=method is_last_selected
+
+    $bool = $group->is_last_selected;
+
+Get whether or not a group is the prior selected group of its connected database.
+
+=cut
+
+sub is_last_selected {
+    my $self    = shift;
+    my $kdbx    = eval { $self->kdbx } or return FALSE;
+    my $group   = $kdbx->last_selected;
+    return $group && Hash::Util::FieldHash::id($group) == Hash::Util::FieldHash::id($self);
+}
+
+=method is_last_top_visible
+
+    $bool = $group->is_last_top_visible;
+
+Get whether or not a group is the latest top visible group of its connected database.
+
+=cut
+
+sub is_last_top_visible {
+    my $self    = shift;
+    my $kdbx    = eval { $self->kdbx } or return FALSE;
+    my $group   = $kdbx->last_top_visible;
+    return $group && Hash::Util::FieldHash::id($group) == Hash::Util::FieldHash::id($self);
+}
+
 =method path
 
     $string = $group->path;
@@ -363,8 +602,6 @@ etc. A group not in a database tree structure returns a depth of -1.
 
 sub depth { $_[0]->is_root ? 0 : (scalar @{$_[0]->lineage || []} || -1) }
 
-sub label { shift->name(@_) }
-
 sub _signal {
     my $self = shift;
     my $type = shift;
@@ -378,82 +615,25 @@ sub _commit {
     $self->last_access_time($time);
 }
 
-sub effective_default_auto_type_sequence {
-    my $self = shift;
-    my $sequence = $self->default_auto_type_sequence;
-    return $sequence if defined $sequence;
-
-    my $parent = $self->parent or return '{USERNAME}{TAB}{PASSWORD}{ENTER}';
-    return $parent->effective_default_auto_type_sequence;
-}
-
-sub effective_enable_auto_type {
-    my $self = shift;
-    my $enabled = $self->enable_auto_type;
-    return $enabled if defined $enabled;
-
-    my $parent = $self->parent or return true;
-    return $parent->effective_enable_auto_type;
-}
-
-sub effective_enable_searching {
-    my $self = shift;
-    my $enabled = $self->enable_searching;
-    return $enabled if defined $enabled;
+sub label { shift->name(@_) }
 
-    my $parent = $self->parent or return true;
-    return $parent->effective_enable_searching;
-}
+### Name of the parent attribute expected to contain the object
+sub _parent_container { 'groups' }
 
 1;
 __END__
 
-=head1 DESCRIPTION
-
-=attr uuid
+=for Pod::Coverage times
 
-=attr name
-
-=attr notes
-
-=attr tags
-
-=attr icon_id
-
-=attr custom_icon_uuid
-
-=attr is_expanded
-
-=attr default_auto_type_sequence
-
-=attr enable_auto_type
-
-=attr enable_searching
-
-=attr last_top_visible_entry
-
-=attr custom_data
-
-=attr previous_parent_group
-
-=attr entries
-
-=attr groups
-
-=attr last_modification_time
-
-=attr creation_time
-
-=attr last_access_time
-
-=attr expiry_time
-
-=attr expires
+=head1 DESCRIPTION
 
-=attr usage_count
+A group in a KDBX database is a type of object that can contain entries and other groups.
 
-=attr location_changed
+There is also some metadata associated with a group. Each group in a database is identified uniquely by
+a UUID. An entry can also have an icon associated with it, and there are various timestamps. Take a look at
+the attributes to see what's available.
 
-Get or set various group fields.
+A B<File::KDBX::Group> is a subclass of L<File::KDBX::Object>. View its documentation to see other attributes
+and methods available on groups.
 
 =cut
This page took 0.03141 seconds and 4 git commands to generate.