From a580803a8d525ab1152e6c32d4714d63415f424c Mon Sep 17 00:00:00 2001 From: Herbert Poul Date: Tue, 19 May 2020 09:19:46 +0200 Subject: [PATCH] ignore missing recycle bin. --- lib/src/kdbx_dao.dart | 14 ------------ lib/src/kdbx_file.dart | 51 +++++++++++++++++++++++------------------- 2 files changed, 28 insertions(+), 37 deletions(-) diff --git a/lib/src/kdbx_dao.dart b/lib/src/kdbx_dao.dart index 6eaabe7..69b8e4b 100644 --- a/lib/src/kdbx_dao.dart +++ b/lib/src/kdbx_dao.dart @@ -20,20 +20,6 @@ extension KdbxDao on KdbxFile { orElse: () => throw StateError('Unable to find group with uuid $uuid')); - KdbxGroup _createRecycleBin() { - body.meta.recycleBinEnabled.set(true); - final group = createGroup(parent: body.rootGroup, name: 'Trash'); - group.icon.set(KdbxIcon.TrashBin); - group.enableAutoType.set(false); - group.enableSearching.set(false); - body.meta.recycleBinUUID.set(group.uuid); - return group; - } - - KdbxGroup getRecycleBinOrCreate() { - return recycleBin ?? _createRecycleBin(); - } - void deleteGroup(KdbxGroup group) { move(group, getRecycleBinOrCreate()); } diff --git a/lib/src/kdbx_file.dart b/lib/src/kdbx_file.dart index 20a7ac0..69fbb30 100644 --- a/lib/src/kdbx_file.dart +++ b/lib/src/kdbx_file.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'dart:typed_data'; import 'package:kdbx/src/crypto/protected_value.dart'; +import 'package:kdbx/src/kdbx_consts.dart'; import 'package:kdbx/src/kdbx_dao.dart'; import 'package:kdbx/src/kdbx_format.dart'; import 'package:kdbx/src/kdbx_group.dart'; @@ -67,18 +68,18 @@ class KdbxFile { _dirtyObjectsChanged.close(); } - KdbxGroup _recycleBin; + CachedValue _recycleBin; /// Returns the recycle bin, if it exists, null otherwise. - KdbxGroup get recycleBin => _recycleBin ??= _findRecycleBin(); + KdbxGroup get recycleBin => (_recycleBin ??= _findRecycleBin()).value; - KdbxGroup _findRecycleBin() { + CachedValue _findRecycleBin() { final uuid = body.meta.recycleBinUUID.get(); if (uuid?.isNil != false) { - return null; + return CachedValue.withNull(); } try { - return findGroupByUuid(uuid); + return CachedValue.withValue(findGroupByUuid(uuid)); } catch (e, stackTrace) { _logger.warning(() { final groupDebug = body.rootGroup @@ -89,25 +90,29 @@ class KdbxFile { }); _logger.severe('Inconsistency error, uuid $uuid not found in groups.', e, stackTrace); - rethrow; + return CachedValue.withNull(); } } -// void _subscribeToChildren() { -// final allObjects = _allObjects; -// for (final obj in allObjects) { -// _subscriptions.handle(obj.changes.listen((event) { -// if (event.isDirty) { -// isDirty = true; -// if (event.object is KdbxGroup) { -// Future(() { -// // resubscribe, just in case some child groups/entries have changed. -// _subscriptions.cancelSubscriptions(); -// _subscribeToChildren(); -// }); -// } -// } -// })); -// } -// } + KdbxGroup _createRecycleBin() { + body.meta.recycleBinEnabled.set(true); + final group = createGroup(parent: body.rootGroup, name: 'Trash'); + group.icon.set(KdbxIcon.TrashBin); + group.enableAutoType.set(false); + group.enableSearching.set(false); + body.meta.recycleBinUUID.set(group.uuid); + _recycleBin = CachedValue.withValue(group); + return group; + } + + KdbxGroup getRecycleBinOrCreate() { + return recycleBin ?? _createRecycleBin(); + } +} + +class CachedValue { + CachedValue.withNull() : value = null; + CachedValue.withValue(this.value) : assert(value != null); + + final T value; }