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 my @ATTRS = qw(uuid custom_data entries groups icon_id);
27 # uuid => sub { generate_uuid(printable => 1) },
28 name
=> ['', coerce
=> \
&to_string
],
29 notes
=> ['', coerce
=> \
&to_string
],
30 tags
=> ['', coerce
=> \
&to_string
],
31 # icon_id => sub { defined $_[1] ? icon($_[1]) : ICON_FOLDER },
32 custom_icon_uuid
=> [undef, coerce
=> \
&to_uuid
],
33 is_expanded
=> [false
, coerce
=> \
&to_bool
],
34 default_auto_type_sequence
=> ['', coerce
=> \
&to_string
],
35 enable_auto_type
=> [undef, coerce
=> \
&to_tristate
],
36 enable_searching
=> [undef, coerce
=> \
&to_tristate
],
37 last_top_visible_entry
=> [undef, coerce
=> \
&to_uuid
],
39 previous_parent_group
=> [undef, coerce
=> \
&to_uuid
],
46 last_modification_time
=> [sub { gmtime }, coerce
=> \
&to_time
],
47 creation_time
=> [sub { gmtime }, coerce
=> \
&to_time
],
48 last_access_time
=> [sub { gmtime }, coerce
=> \
&to_time
],
49 expiry_time
=> [sub { gmtime }, coerce
=> \
&to_time
],
50 expires
=> [false
, coerce
=> \
&to_bool
],
51 usage_count
=> [0, coerce
=> \
&to_number
],
52 location_changed
=> [sub { gmtime }, coerce
=> \
&to_time
],
55 has icon_id
=> ICON_FOLDER
, coerce
=> sub { icon
($_[0]) };
57 while (my ($attr, $default) = each %ATTRS) {
58 has $attr => @$default;
60 while (my ($attr, $default) = each %ATTRS_TIMES) {
61 has $attr => @$default, store
=> 'times';
64 sub _set_default_attributes
{
66 $self->$_ for @ATTRS, keys %ATTRS, keys %ATTRS_TIMES;
71 if (@_ || !defined $self->{uuid
}) {
72 my %args = @_ % 2 == 1 ? (uuid
=> shift, @_) : @_;
73 my $old_uuid = $self->{uuid
};
74 my $uuid = $self->{uuid
} = delete $args{uuid
} // generate_uuid
;
75 $self->_signal('uuid.changed', $uuid, $old_uuid) if defined $old_uuid;
80 ##############################################################################
84 my $entries = $self->{entries
} //= [];
85 # FIXME - Looping through entries on each access is too expensive.
86 @$entries = map { $self->_wrap_entry($_, $self->kdbx) } @$entries;
92 # FIXME - shouldn't have to delegate to the database to get this
93 return $self->kdbx->all_entries(base
=> $self);
98 $entry = $group->add_entry($entry);
99 $entry = $group->add_entry(%entry_attributes);
101 Add an entry to a group
. If C
<$entry> already
has a parent group
, it will be removed from that group before
102 being added to C
<$group>.
108 my $entry = @_ % 2 == 1 ? shift : undef;
111 my $kdbx = delete $args{kdbx
} // eval { $self->kdbx };
113 $entry = $self->_wrap_entry($entry // [%args]);
115 $entry->kdbx($kdbx) if $kdbx;
117 push @{$self->{entries
} ||= []}, $entry->remove;
118 return $entry->_set_group($self);
123 my $uuid = is_ref
($_[0]) ? $self->_wrap_entry(shift)->uuid : shift;
124 my $objects = $self->{entries
};
125 for (my $i = 0; $i < @$objects; ++$i) {
126 my $o = $objects->[$i];
127 next if $uuid ne $o->uuid;
128 return splice @$objects, $i, 1;
129 $o->_set_group(undef);
130 return @$objects, $i, 1;
134 ##############################################################################
138 my $groups = $self->{groups
} //= [];
139 # FIXME - Looping through groups on each access is too expensive.
140 @$groups = map { $self->_wrap_group($_, $self->kdbx) } @$groups;
146 # FIXME - shouldn't have to delegate to the database to get this
147 return $self->kdbx->all_groups(base
=> $self, include_base
=> false
);
150 sub _kpx_groups
{ shift-
>groups(@_) }
154 $new_group = $group->add_group($new_group);
155 $new_group = $group->add_group(%group_attributes);
157 Add a group to a group
. If C
<$new_group> already
has a parent group
, it will be removed from that group before
158 being added to C
<$group>.
164 my $group = @_ % 2 == 1 ? shift : undef;
167 my $kdbx = delete $args{kdbx
} // eval { $self->kdbx };
169 $group = $self->_wrap_group($group // [%args]);
171 $group->kdbx($kdbx) if $kdbx;
173 push @{$self->{groups
} ||= []}, $group->remove;
174 return $group->_set_group($self);
179 my $uuid = is_ref
($_[0]) ? $self->_wrap_group(shift)->uuid : shift;
180 my $objects = $self->{groups
};
181 for (my $i = 0; $i < @$objects; ++$i) {
182 my $o = $objects->[$i];
183 next if $uuid ne $o->uuid;
184 $o->_set_group(undef);
185 return splice @$objects, $i, 1;
189 ##############################################################################
193 $new_entry = $group->add_object($new_entry);
194 $new_group = $group->add_object($new_group);
196 Add an object
(either a L
<File
::KDBX
::Entry
> or a L
<File
::KDBX
::Group
>) to a group
. This
is the generic
197 equivalent of the object forms of L
</add_entry> and L</add_group
>.
204 if ($obj->isa('File::KDBX::Entry')) {
205 $self->add_entry($obj);
207 elsif ($obj->isa('File::KDBX::Group')) {
208 $self->add_group($obj);
212 =method remove_object
214 $group->remove_object($entry);
215 $group->remove_object($group);
217 Remove an object
(either a L
<File
::KDBX
::Entry
> or a L
<File
::KDBX
::Group
>) from a group
. This
is the generic
218 equivalent of the object forms of L
</remove_entry> and L</remove_group
>.
225 my $blessed = blessed
($object);
226 return $self->remove_group($object, @_) if $blessed && $object->isa('File::KDBX::Group');
227 return $self->remove_entry($object, @_) if $blessed && $object->isa('File::KDBX::Entry');
228 return $self->remove_group($object, @_) || $self->remove_entry($object, @_);
231 ##############################################################################
235 $bool = $group->is_root;
237 Determine
if a group
is the root group of its associated database
.
243 my $kdbx = eval { $self->kdbx } or return;
244 return Hash
::Util
::FieldHash
::id
($kdbx->root) == Hash
::Util
::FieldHash
::id
($self);
249 $string = $group->path;
251 Get a string representation of a group
's lineage. This is used as the substitution value for the
252 C<{GROUP_PATH}> placeholder. See L<File::KDBX::Entry/Placeholders>.
254 For a root group, the path is simply the name of the group. For deeper groups, the path is a period-separated
255 sequence of group names between the root group and C<$group>, including C<$group> but I<not> the root group.
256 In other words, paths of deeper groups leave the root group name out.
259 -> Root # path is "Root"
260 -> Foo # path is "Foo"
261 -> Bar # path is "Foo.Bar"
263 Yeah, it doesn't make much sense to me
, either
, but this matches the behavior of KeePass
.
269 return $self->name if $self->is_root;
270 my $lineage = $self->lineage or return;
271 my @parts = (@$lineage, $self);
273 return join('.', map { $_->name } @parts);
278 $size = $group->size;
280 Get the size
(in bytes
) of a group
, including the size of all subroups
and entries
, if any
.
286 return sum0
map { $_->size } @{$self->groups}, @{$self->entries};
291 $depth = $group->depth;
293 Get the depth of a group within a database
. The root group
is at depth
0, its direct children are at depth
1,
294 etc
. A group
not in a database tree structure returns a depth of
-1.
298 sub depth
{ $_[0]->is_root ? 0 : (scalar @{$_[0]->lineage || []} || -1) }
300 sub label
{ shift-
>name(@_) }
305 return $self->SUPER::_signal
("group.$type", @_);
311 $self->last_modification_time($time);
312 $self->last_access_time($time);
330 =attr custom_icon_uuid
334 =attr default_auto_type_sequence
336 =attr enable_auto_type
338 =attr enable_searching
340 =attr last_top_visible_entry
344 =attr previous_parent_group
350 =attr last_modification_time
354 =attr last_access_time
362 =attr location_changed
364 Get or set various group fields.