1 package File
::KDBX
::Group
;
2 # ABSTRACT: A KDBX database group
7 use Devel
::GlobalDestruction
;
8 use File
::KDBX
::Constants
qw(:icon);
10 use File
::KDBX
::Util
qw(generate_uuid);
11 use Hash
::Util
::FieldHash
;
12 use List
::Util
qw(sum0);
13 use Ref
::Util
qw(is_ref);
14 use Scalar
::Util
qw(blessed);
19 use parent
'File::KDBX::Object';
21 our $VERSION = '999.999'; # VERSION
23 sub _parent_container
{ 'groups' }
25 my @ATTRS = qw(uuid custom_data entries groups);
27 # uuid => sub { generate_uuid(printable => 1) },
31 icon_id
=> ICON_FOLDER
,
32 custom_icon_uuid
=> undef,
34 default_auto_type_sequence
=> '',
35 enable_auto_type
=> undef,
36 enable_searching
=> undef,
37 last_top_visible_entry
=> undef,
38 # custom_data => sub { +{} },
39 previous_parent_group
=> undef,
40 # entries => sub { +[] },
41 # groups => sub { +[] },
44 last_modification_time
=> sub { gmtime },
45 creation_time
=> sub { gmtime },
46 last_access_time
=> sub { gmtime },
47 expiry_time
=> sub { gmtime },
50 location_changed
=> sub { gmtime },
53 while (my ($attr, $default) = each %ATTRS) {
54 no strict
'refs'; ## no critic (ProhibitNoStrict)
57 $self->{$attr} = shift if @_;
58 $self->{$attr} //= (ref $default eq 'CODE') ? $default->($self) : $default;
61 while (my ($attr, $default) = each %ATTRS_TIMES) {
62 no strict
'refs'; ## no critic (ProhibitNoStrict)
65 $self->{times}{$attr} = shift if @_;
66 $self->{times}{$attr} //= (ref $default eq 'CODE') ? $default->($self) : $default;
70 sub _set_default_attributes
{
72 $self->$_ for @ATTRS, keys %ATTRS, keys %ATTRS_TIMES;
77 if (@_ || !defined $self->{uuid
}) {
78 my %args = @_ % 2 == 1 ? (uuid
=> shift, @_) : @_;
79 my $old_uuid = $self->{uuid
};
80 my $uuid = $self->{uuid
} = delete $args{uuid
} // generate_uuid
;
81 # if (defined $old_uuid and my $kdbx = $KDBX{$self}) {
82 # $kdbx->_update_group_uuid($old_uuid, $uuid, $self);
88 ##############################################################################
92 my $entries = $self->{entries
} //= [];
93 # FIXME - Looping through entries on each access is too expensive.
94 @$entries = map { $self->_wrap_entry($_, $self->kdbx) } @$entries;
100 # FIXME - shouldn't have to delegate to the database to get this
101 return $self->kdbx->all_entries(base
=> $self);
106 $entry = $group->add_entry($entry);
107 $entry = $group->add_entry(%entry_attributes);
109 Add an entry to a group
. If C
<$entry> already
has a parent group
, it will be removed from that group before
110 being added to C
<$group>.
116 my $entry = @_ % 2 == 1 ? shift : undef;
119 my $kdbx = delete $args{kdbx
} // eval { $self->kdbx };
121 $entry = $self->_wrap_entry($entry // [%args]);
123 $entry->kdbx($kdbx) if $kdbx;
125 push @{$self->{entries
} ||= []}, $entry->remove;
126 return $entry->_set_group($self);
131 my $uuid = is_ref
($_[0]) ? $self->_wrap_entry(shift)->uuid : shift;
132 my $objects = $self->{entries
};
133 for (my $i = 0; $i < @$objects; ++$i) {
134 my $o = $objects->[$i];
135 next if $uuid ne $o->uuid;
136 return splice @$objects, $i, 1;
137 $o->_set_group(undef);
138 return @$objects, $i, 1;
142 ##############################################################################
146 my $groups = $self->{groups
} //= [];
147 # FIXME - Looping through groups on each access is too expensive.
148 @$groups = map { $self->_wrap_group($_, $self->kdbx) } @$groups;
154 # FIXME - shouldn't have to delegate to the database to get this
155 return $self->kdbx->all_groups(base
=> $self, include_base
=> false
);
158 sub _kpx_groups
{ shift-
>groups(@_) }
162 $new_group = $group->add_group($new_group);
163 $new_group = $group->add_group(%group_attributes);
165 Add a group to a group
. If C
<$new_group> already
has a parent group
, it will be removed from that group before
166 being added to C
<$group>.
172 my $group = @_ % 2 == 1 ? shift : undef;
175 my $kdbx = delete $args{kdbx
} // eval { $self->kdbx };
177 $group = $self->_wrap_group($group // [%args]);
179 $group->kdbx($kdbx) if $kdbx;
181 push @{$self->{groups
} ||= []}, $group->remove;
182 return $group->_set_group($self);
187 my $uuid = is_ref
($_[0]) ? $self->_wrap_group(shift)->uuid : shift;
188 my $objects = $self->{groups
};
189 for (my $i = 0; $i < @$objects; ++$i) {
190 my $o = $objects->[$i];
191 next if $uuid ne $o->uuid;
192 $o->_set_group(undef);
193 return splice @$objects, $i, 1;
197 ##############################################################################
201 $new_entry = $group->add_object($new_entry);
202 $new_group = $group->add_object($new_group);
204 Add an object
(either a L
<File
::KDBX
::Entry
> or a L
<File
::KDBX
::Group
>) to a group
. This
is the generic
205 equivalent of the object forms of L
</add_entry> and L</add_group
>.
212 if ($obj->isa('File::KDBX::Entry')) {
213 $self->add_entry($obj);
215 elsif ($obj->isa('File::KDBX::Group')) {
216 $self->add_group($obj);
220 =method remove_object
222 $group->remove_object($entry);
223 $group->remove_object($group);
225 Remove an object
(either a L
<File
::KDBX
::Entry
> or a L
<File
::KDBX
::Group
>) from a group
. This
is the generic
226 equivalent of the object forms of L
</remove_entry> and L</remove_group
>.
233 my $blessed = blessed
($object);
234 return $self->remove_group($object, @_) if $blessed && $object->isa('File::KDBX::Group');
235 return $self->remove_entry($object, @_) if $blessed && $object->isa('File::KDBX::Entry');
236 return $self->remove_group($object, @_) || $self->remove_entry($object, @_);
239 ##############################################################################
243 $bool = $group->is_root;
245 Determine
if a group
is the root group of its associated database
.
251 my $kdbx = eval { $self->kdbx } or return;
252 return Hash
::Util
::FieldHash
::id
($kdbx->root) == Hash
::Util
::FieldHash
::id
($self);
257 $string = $group->path;
259 Get a string representation of a group
's lineage. This is used as the substitution value for the
260 C<{GROUP_PATH}> placeholder. See L<File::KDBX::Entry/Placeholders>.
262 For a root group, the path is simply the name of the group. For deeper groups, the path is a period-separated
263 sequence of group names between the root group and C<$group>, including C<$group> but I<not> the root group.
264 In other words, paths of deeper groups leave the root group name out.
267 -> Root # path is "Root"
268 -> Foo # path is "Foo"
269 -> Bar # path is "Foo.Bar"
271 Yeah, it doesn't make much sense to me
, either
, but this matches the behavior of KeePass
.
277 return $self->name if $self->is_root;
278 my $lineage = $self->lineage or return;
279 my @parts = (@$lineage, $self);
281 return join('.', map { $_->name } @parts);
286 $size = $group->size;
288 Get the size
(in bytes
) of a group
, including the size of all subroups
and entries
, if any
.
294 return sum0
map { $_->size } @{$self->groups}, @{$self->entries};
299 $depth = $group->depth;
301 Get the depth of a group within a database
. The root group
is at depth
0, its direct children are at depth
1,
302 etc
. A group
not in a database tree structure returns a depth of
-1.
306 sub depth
{ $_[0]->is_root ? 0 : (scalar @{$_[0]->lineage || []} || -1) }
308 sub label
{ shift-
>name(@_) }
325 =attr custom_icon_uuid
329 =attr default_auto_type_sequence
331 =attr enable_auto_type
333 =attr enable_searching
335 =attr last_top_visible_entry
339 =attr previous_parent_group
345 =attr last_modification_time
349 =attr last_access_time
357 =attr location_changed
359 Get or set various group fields.