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_coderef 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
=> sub { defined $_[1] ? icon
($_[1]) : 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 { scalar gmtime },
45 creation_time
=> sub { scalar gmtime },
46 last_access_time
=> sub { scalar gmtime },
47 expiry_time
=> sub { scalar gmtime },
50 location_changed
=> sub { scalar gmtime },
53 while (my ($attr, $setter) = each %ATTRS) {
54 no strict
'refs'; ## no critic (ProhibitNoStrict)
55 *{$attr} = is_coderef
$setter ? sub {
57 $self->{$attr} = $setter->($self, shift) if @_;
58 $self->{$attr} //= $setter->($self);
61 $self->{$attr} = shift if @_;
62 $self->{$attr} //= $setter;
65 while (my ($attr, $default) = each %ATTRS_TIMES) {
66 no strict
'refs'; ## no critic (ProhibitNoStrict)
69 $self->{times}{$attr} = shift if @_;
70 $self->{times}{$attr} //= (ref $default eq 'CODE') ? $default->($self) : $default;
74 sub _set_default_attributes
{
76 $self->$_ for @ATTRS, keys %ATTRS, keys %ATTRS_TIMES;
81 if (@_ || !defined $self->{uuid
}) {
82 my %args = @_ % 2 == 1 ? (uuid
=> shift, @_) : @_;
83 my $old_uuid = $self->{uuid
};
84 my $uuid = $self->{uuid
} = delete $args{uuid
} // generate_uuid
;
85 $self->_signal('uuid.changed', $uuid, $old_uuid) if defined $old_uuid;
90 ##############################################################################
94 my $entries = $self->{entries
} //= [];
95 # FIXME - Looping through entries on each access is too expensive.
96 @$entries = map { $self->_wrap_entry($_, $self->kdbx) } @$entries;
102 # FIXME - shouldn't have to delegate to the database to get this
103 return $self->kdbx->all_entries(base
=> $self);
108 $entry = $group->add_entry($entry);
109 $entry = $group->add_entry(%entry_attributes);
111 Add an entry to a group
. If C
<$entry> already
has a parent group
, it will be removed from that group before
112 being added to C
<$group>.
118 my $entry = @_ % 2 == 1 ? shift : undef;
121 my $kdbx = delete $args{kdbx
} // eval { $self->kdbx };
123 $entry = $self->_wrap_entry($entry // [%args]);
125 $entry->kdbx($kdbx) if $kdbx;
127 push @{$self->{entries
} ||= []}, $entry->remove;
128 return $entry->_set_group($self);
133 my $uuid = is_ref
($_[0]) ? $self->_wrap_entry(shift)->uuid : shift;
134 my $objects = $self->{entries
};
135 for (my $i = 0; $i < @$objects; ++$i) {
136 my $o = $objects->[$i];
137 next if $uuid ne $o->uuid;
138 return splice @$objects, $i, 1;
139 $o->_set_group(undef);
140 return @$objects, $i, 1;
144 ##############################################################################
148 my $groups = $self->{groups
} //= [];
149 # FIXME - Looping through groups on each access is too expensive.
150 @$groups = map { $self->_wrap_group($_, $self->kdbx) } @$groups;
156 # FIXME - shouldn't have to delegate to the database to get this
157 return $self->kdbx->all_groups(base
=> $self, include_base
=> false
);
160 sub _kpx_groups
{ shift-
>groups(@_) }
164 $new_group = $group->add_group($new_group);
165 $new_group = $group->add_group(%group_attributes);
167 Add a group to a group
. If C
<$new_group> already
has a parent group
, it will be removed from that group before
168 being added to C
<$group>.
174 my $group = @_ % 2 == 1 ? shift : undef;
177 my $kdbx = delete $args{kdbx
} // eval { $self->kdbx };
179 $group = $self->_wrap_group($group // [%args]);
181 $group->kdbx($kdbx) if $kdbx;
183 push @{$self->{groups
} ||= []}, $group->remove;
184 return $group->_set_group($self);
189 my $uuid = is_ref
($_[0]) ? $self->_wrap_group(shift)->uuid : shift;
190 my $objects = $self->{groups
};
191 for (my $i = 0; $i < @$objects; ++$i) {
192 my $o = $objects->[$i];
193 next if $uuid ne $o->uuid;
194 $o->_set_group(undef);
195 return splice @$objects, $i, 1;
199 ##############################################################################
203 $new_entry = $group->add_object($new_entry);
204 $new_group = $group->add_object($new_group);
206 Add an object
(either a L
<File
::KDBX
::Entry
> or a L
<File
::KDBX
::Group
>) to a group
. This
is the generic
207 equivalent of the object forms of L
</add_entry> and L</add_group
>.
214 if ($obj->isa('File::KDBX::Entry')) {
215 $self->add_entry($obj);
217 elsif ($obj->isa('File::KDBX::Group')) {
218 $self->add_group($obj);
222 =method remove_object
224 $group->remove_object($entry);
225 $group->remove_object($group);
227 Remove an object
(either a L
<File
::KDBX
::Entry
> or a L
<File
::KDBX
::Group
>) from a group
. This
is the generic
228 equivalent of the object forms of L
</remove_entry> and L</remove_group
>.
235 my $blessed = blessed
($object);
236 return $self->remove_group($object, @_) if $blessed && $object->isa('File::KDBX::Group');
237 return $self->remove_entry($object, @_) if $blessed && $object->isa('File::KDBX::Entry');
238 return $self->remove_group($object, @_) || $self->remove_entry($object, @_);
241 ##############################################################################
245 $bool = $group->is_root;
247 Determine
if a group
is the root group of its associated database
.
253 my $kdbx = eval { $self->kdbx } or return;
254 return Hash
::Util
::FieldHash
::id
($kdbx->root) == Hash
::Util
::FieldHash
::id
($self);
259 $string = $group->path;
261 Get a string representation of a group
's lineage. This is used as the substitution value for the
262 C<{GROUP_PATH}> placeholder. See L<File::KDBX::Entry/Placeholders>.
264 For a root group, the path is simply the name of the group. For deeper groups, the path is a period-separated
265 sequence of group names between the root group and C<$group>, including C<$group> but I<not> the root group.
266 In other words, paths of deeper groups leave the root group name out.
269 -> Root # path is "Root"
270 -> Foo # path is "Foo"
271 -> Bar # path is "Foo.Bar"
273 Yeah, it doesn't make much sense to me
, either
, but this matches the behavior of KeePass
.
279 return $self->name if $self->is_root;
280 my $lineage = $self->lineage or return;
281 my @parts = (@$lineage, $self);
283 return join('.', map { $_->name } @parts);
288 $size = $group->size;
290 Get the size
(in bytes
) of a group
, including the size of all subroups
and entries
, if any
.
296 return sum0
map { $_->size } @{$self->groups}, @{$self->entries};
301 $depth = $group->depth;
303 Get the depth of a group within a database
. The root group
is at depth
0, its direct children are at depth
1,
304 etc
. A group
not in a database tree structure returns a depth of
-1.
308 sub depth
{ $_[0]->is_root ? 0 : (scalar @{$_[0]->lineage || []} || -1) }
310 sub label
{ shift-
>name(@_) }
315 return $self->SUPER::_signal
("group.$type", @_);
321 $self->last_modification_time($time);
322 $self->last_access_time($time);
340 =attr custom_icon_uuid
344 =attr default_auto_type_sequence
346 =attr enable_auto_type
348 =attr enable_searching
350 =attr last_top_visible_entry
354 =attr previous_parent_group
360 =attr last_modification_time
364 =attr last_access_time
372 =attr location_changed
374 Get or set various group fields.