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(:class :coercion generate_uuid);
11 use Hash
::Util
::FieldHash
;
12 use List
::Util
qw(sum0);
13 use Ref
::Util
qw(is_coderef is_ref);
14 use Scalar
::Util
qw(blessed);
19 extends
'File::KDBX::Object';
21 our $VERSION = '999.999'; # VERSION
23 sub _parent_container
{ 'groups' }
25 # has uuid => sub { generate_uuid(printable => 1) };
26 has name
=> '', coerce
=> \
&to_string
;
27 has notes
=> '', coerce
=> \
&to_string
;
28 has tags
=> '', coerce
=> \
&to_string
;
29 has icon_id
=> ICON_FOLDER
, coerce
=> \
&to_icon_constant
;
30 has custom_icon_uuid
=> undef, coerce
=> \
&to_uuid
;
31 has is_expanded
=> false
, coerce
=> \
&to_bool
;
32 has default_auto_type_sequence
=> '', coerce
=> \
&to_string
;
33 has enable_auto_type
=> undef, coerce
=> \
&to_tristate
;
34 has enable_searching
=> undef, coerce
=> \
&to_tristate
;
35 has last_top_visible_entry
=> undef, coerce
=> \
&to_uuid
;
36 # has custom_data => {};
37 has previous_parent_group
=> undef, coerce
=> \
&to_uuid
;
42 has last_modification_time
=> sub { gmtime }, store
=> 'times', coerce
=> \
&to_time
;
43 has creation_time
=> sub { gmtime }, store
=> 'times', coerce
=> \
&to_time
;
44 has last_access_time
=> sub { gmtime }, store
=> 'times', coerce
=> \
&to_time
;
45 has expiry_time
=> sub { gmtime }, store
=> 'times', coerce
=> \
&to_time
;
46 has expires
=> false
, store
=> 'times', coerce
=> \
&to_bool
;
47 has usage_count
=> 0, store
=> 'times', coerce
=> \
&to_number
;
48 has location_changed
=> sub { gmtime }, store
=> 'times', coerce
=> \
&to_time
;
50 my @ATTRS = qw(uuid custom_data entries groups);
51 sub _set_nonlazy_attributes
{
53 $self->$_ for @ATTRS, list_attributes
(ref $self);
58 if (@_ || !defined $self->{uuid
}) {
59 my %args = @_ % 2 == 1 ? (uuid
=> shift, @_) : @_;
60 my $old_uuid = $self->{uuid
};
61 my $uuid = $self->{uuid
} = delete $args{uuid
} // generate_uuid
;
62 $self->_signal('uuid.changed', $uuid, $old_uuid) if defined $old_uuid;
67 ##############################################################################
71 my $entries = $self->{entries
} //= [];
72 # FIXME - Looping through entries on each access is too expensive.
73 @$entries = map { $self->_wrap_entry($_, $self->kdbx) } @$entries;
79 # FIXME - shouldn't have to delegate to the database to get this
80 return $self->kdbx->all_entries(base
=> $self);
85 $entry = $group->add_entry($entry);
86 $entry = $group->add_entry(%entry_attributes);
88 Add an entry to a group
. If C
<$entry> already
has a parent group
, it will be removed from that group before
89 being added to C
<$group>.
95 my $entry = @_ % 2 == 1 ? shift : undef;
98 my $kdbx = delete $args{kdbx
} // eval { $self->kdbx };
100 $entry = $self->_wrap_entry($entry // [%args]);
102 $entry->kdbx($kdbx) if $kdbx;
104 push @{$self->{entries
} ||= []}, $entry->remove;
105 return $entry->_set_group($self);
110 my $uuid = is_ref
($_[0]) ? $self->_wrap_entry(shift)->uuid : shift;
111 my $objects = $self->{entries
};
112 for (my $i = 0; $i < @$objects; ++$i) {
113 my $o = $objects->[$i];
114 next if $uuid ne $o->uuid;
115 return splice @$objects, $i, 1;
116 $o->_set_group(undef);
117 return @$objects, $i, 1;
121 ##############################################################################
125 my $groups = $self->{groups
} //= [];
126 # FIXME - Looping through groups on each access is too expensive.
127 @$groups = map { $self->_wrap_group($_, $self->kdbx) } @$groups;
133 # FIXME - shouldn't have to delegate to the database to get this
134 return $self->kdbx->all_groups(base
=> $self, include_base
=> false
);
137 sub _kpx_groups
{ shift-
>groups(@_) }
141 $new_group = $group->add_group($new_group);
142 $new_group = $group->add_group(%group_attributes);
144 Add a group to a group
. If C
<$new_group> already
has a parent group
, it will be removed from that group before
145 being added to C
<$group>.
151 my $group = @_ % 2 == 1 ? shift : undef;
154 my $kdbx = delete $args{kdbx
} // eval { $self->kdbx };
156 $group = $self->_wrap_group($group // [%args]);
158 $group->kdbx($kdbx) if $kdbx;
160 push @{$self->{groups
} ||= []}, $group->remove;
161 return $group->_set_group($self);
166 my $uuid = is_ref
($_[0]) ? $self->_wrap_group(shift)->uuid : shift;
167 my $objects = $self->{groups
};
168 for (my $i = 0; $i < @$objects; ++$i) {
169 my $o = $objects->[$i];
170 next if $uuid ne $o->uuid;
171 $o->_set_group(undef);
172 return splice @$objects, $i, 1;
176 ##############################################################################
180 $new_entry = $group->add_object($new_entry);
181 $new_group = $group->add_object($new_group);
183 Add an object
(either a L
<File
::KDBX
::Entry
> or a L
<File
::KDBX
::Group
>) to a group
. This
is the generic
184 equivalent of the object forms of L
</add_entry> and L</add_group
>.
191 if ($obj->isa('File::KDBX::Entry')) {
192 $self->add_entry($obj);
194 elsif ($obj->isa('File::KDBX::Group')) {
195 $self->add_group($obj);
199 =method remove_object
201 $group->remove_object($entry);
202 $group->remove_object($group);
204 Remove an object
(either a L
<File
::KDBX
::Entry
> or a L
<File
::KDBX
::Group
>) from a group
. This
is the generic
205 equivalent of the object forms of L
</remove_entry> and L</remove_group
>.
212 my $blessed = blessed
($object);
213 return $self->remove_group($object, @_) if $blessed && $object->isa('File::KDBX::Group');
214 return $self->remove_entry($object, @_) if $blessed && $object->isa('File::KDBX::Entry');
215 return $self->remove_group($object, @_) || $self->remove_entry($object, @_);
218 ##############################################################################
222 $bool = $group->is_root;
224 Determine
if a group
is the root group of its associated database
.
230 my $kdbx = eval { $self->kdbx } or return;
231 return Hash
::Util
::FieldHash
::id
($kdbx->root) == Hash
::Util
::FieldHash
::id
($self);
236 $string = $group->path;
238 Get a string representation of a group
's lineage. This is used as the substitution value for the
239 C<{GROUP_PATH}> placeholder. See L<File::KDBX::Entry/Placeholders>.
241 For a root group, the path is simply the name of the group. For deeper groups, the path is a period-separated
242 sequence of group names between the root group and C<$group>, including C<$group> but I<not> the root group.
243 In other words, paths of deeper groups leave the root group name out.
246 -> Root # path is "Root"
247 -> Foo # path is "Foo"
248 -> Bar # path is "Foo.Bar"
250 Yeah, it doesn't make much sense to me
, either
, but this matches the behavior of KeePass
.
256 return $self->name if $self->is_root;
257 my $lineage = $self->lineage or return;
258 my @parts = (@$lineage, $self);
260 return join('.', map { $_->name } @parts);
265 $size = $group->size;
267 Get the size
(in bytes
) of a group
, including the size of all subroups
and entries
, if any
.
273 return sum0
map { $_->size } @{$self->groups}, @{$self->entries};
278 $depth = $group->depth;
280 Get the depth of a group within a database
. The root group
is at depth
0, its direct children are at depth
1,
281 etc
. A group
not in a database tree structure returns a depth of
-1.
285 sub depth
{ $_[0]->is_root ? 0 : (scalar @{$_[0]->lineage || []} || -1) }
287 sub label
{ shift-
>name(@_) }
292 return $self->SUPER::_signal
("group.$type", @_);
298 $self->last_modification_time($time);
299 $self->last_access_time($time);
317 =attr custom_icon_uuid
321 =attr default_auto_type_sequence
323 =attr enable_auto_type
325 =attr enable_searching
327 =attr last_top_visible_entry
331 =attr previous_parent_group
337 =attr last_modification_time
341 =attr last_access_time
349 =attr location_changed
351 Get or set various group fields.