From 1046110dd277847573b63d24076c57007c82ede7 Mon Sep 17 00:00:00 2001 From: Geoffroy BONNEVILLE Date: Wed, 8 Apr 2020 16:23:15 +0200 Subject: [PATCH] Code cleanup (bis) --- .../Common/Interfaces/IImportFormat.cs | 3 +- .../File/CsvImportFormat.cs | 3 +- .../KeePass/EntryMappingProfile.cs | 42 +------------------ .../KeePass/GroupMappingProfile.cs | 10 ----- .../KeePass/KeePassDatabaseClient.cs | 19 ++++++++- 5 files changed, 21 insertions(+), 56 deletions(-) diff --git a/ModernKeePass.Application/Common/Interfaces/IImportFormat.cs b/ModernKeePass.Application/Common/Interfaces/IImportFormat.cs index 302adba..8044f55 100644 --- a/ModernKeePass.Application/Common/Interfaces/IImportFormat.cs +++ b/ModernKeePass.Application/Common/Interfaces/IImportFormat.cs @@ -1,10 +1,9 @@ using System.Collections.Generic; -using System.Threading.Tasks; namespace ModernKeePass.Application.Common.Interfaces { public interface IImportFormat { - Task>> Import(IList fileContents); + List> Import(IList fileContents); } } \ No newline at end of file diff --git a/ModernKeePass.Infrastructure/File/CsvImportFormat.cs b/ModernKeePass.Infrastructure/File/CsvImportFormat.cs index 92db98a..94195de 100644 --- a/ModernKeePass.Infrastructure/File/CsvImportFormat.cs +++ b/ModernKeePass.Infrastructure/File/CsvImportFormat.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.Threading.Tasks; using ModernKeePass.Application.Common.Interfaces; namespace ModernKeePass.Infrastructure.File @@ -10,7 +9,7 @@ namespace ModernKeePass.Infrastructure.File private const char Delimiter = ';'; private const char LineDelimiter = '\n'; - public async Task>> Import(IList fileContents) + public List> Import(IList fileContents) { var parsedResult = new List>(); foreach (var line in fileContents) diff --git a/ModernKeePass.Infrastructure/KeePass/EntryMappingProfile.cs b/ModernKeePass.Infrastructure/KeePass/EntryMappingProfile.cs index c79015d..bb8e421 100644 --- a/ModernKeePass.Infrastructure/KeePass/EntryMappingProfile.cs +++ b/ModernKeePass.Infrastructure/KeePass/EntryMappingProfile.cs @@ -3,19 +3,12 @@ using System.Linq; using AutoMapper; using ModernKeePass.Domain.Entities; using ModernKeePassLib; -using ModernKeePassLib.Security; namespace ModernKeePass.Infrastructure.KeePass { public class EntryMappingProfile: Profile { public EntryMappingProfile() - { - FromModelToDto(); - FromDtoToModel(); - } - - private void FromDtoToModel() { Uri url; CreateMap() @@ -42,40 +35,7 @@ namespace ModernKeePass.Infrastructure.KeePass .ToDictionary(s => s.Key, s => GetEntryValue(src, s.Key)))) .ForMember(dest => dest.LastModificationDate, opt => opt.MapFrom(src => new DateTimeOffset(src.LastModificationTime))); } - - private void FromModelToDto() - { - CreateMap().ConvertUsing(); - } - + private string GetEntryValue(PwEntry entry, string key) => entry.Strings.GetSafe(key).ReadString(); } - - public class EntryToPwEntryDictionaryConverter : ITypeConverter - { - public PwEntry Convert(EntryEntity source, PwEntry destination, ResolutionContext context) - { - //destination.Uuid = new PwUuid(System.Convert.FromBase64String(source.Id)); - destination.ExpiryTime = source.ExpirationDate.DateTime; - destination.Expires = source.HasExpirationDate; - destination.LastModificationTime = source.LastModificationDate.DateTime; - destination.BackgroundColor = source.BackgroundColor; - destination.ForegroundColor = source.ForegroundColor; - destination.IconId = IconMapper.MapIconToPwIcon(source.Icon); - SetEntryValue(destination, PwDefs.TitleField, source.Name); - SetEntryValue(destination, PwDefs.UserNameField, source.UserName); - SetEntryValue(destination, PwDefs.PasswordField, source.Password); - SetEntryValue(destination, PwDefs.UrlField, source.Url?.ToString()); - SetEntryValue(destination, PwDefs.NotesField, source.Notes); - foreach (var additionalField in source.AdditionalFields) - { - SetEntryValue(destination, additionalField.Key, additionalField.Value); - } - return destination; - } - private void SetEntryValue(PwEntry entry, string key, string newValue) - { - if (newValue != null) entry.Strings.Set(key, new ProtectedString(true, newValue)); - } - } } \ No newline at end of file diff --git a/ModernKeePass.Infrastructure/KeePass/GroupMappingProfile.cs b/ModernKeePass.Infrastructure/KeePass/GroupMappingProfile.cs index c572985..973f4f0 100644 --- a/ModernKeePass.Infrastructure/KeePass/GroupMappingProfile.cs +++ b/ModernKeePass.Infrastructure/KeePass/GroupMappingProfile.cs @@ -7,12 +7,6 @@ namespace ModernKeePass.Infrastructure.KeePass public class GroupMappingProfile : Profile { public GroupMappingProfile() - { - FromModelToDto(); - FromDtoToModel(); - } - - private void FromDtoToModel() { CreateMap() .ForMember(d => d.ParentId, opts => opts.MapFrom(s => s.ParentGroup.Uuid.ToHexString())) @@ -25,9 +19,5 @@ namespace ModernKeePass.Infrastructure.KeePass .ForMember(d => d.SubGroups, opts => opts.MapFrom(s => s.Groups)) .MaxDepth(2); } - - private void FromModelToDto() - { - } } } \ No newline at end of file diff --git a/ModernKeePass.Infrastructure/KeePass/KeePassDatabaseClient.cs b/ModernKeePass.Infrastructure/KeePass/KeePassDatabaseClient.cs index 971e59d..6ef5fb8 100644 --- a/ModernKeePass.Infrastructure/KeePass/KeePassDatabaseClient.cs +++ b/ModernKeePass.Infrastructure/KeePass/KeePassDatabaseClient.cs @@ -23,6 +23,8 @@ namespace ModernKeePass.Infrastructure.KeePass private readonly IDateTime _dateTime; private readonly PwDatabase _pwDatabase = new PwDatabase(); private Credentials _credentials; + // Flag: Has Dispose already been called? + private bool _disposed; public string ZeroId => PwUuid.Zero.ToHexString(); @@ -317,7 +319,22 @@ namespace ModernKeePass.Infrastructure.KeePass public void Dispose() { - if (IsOpen) CloseDatabase(); + Dispose(true); + GC.SuppressFinalize(this); + } + + // Protected implementation of Dispose pattern. + protected virtual void Dispose(bool disposing) + { + if (_disposed) + return; + + if (disposing) + { + _pwDatabase.Close(); + } + + _disposed = true; } } } \ No newline at end of file