From 59ab43ca9c413708b1e2ab1da6261e8a18dfbcaf Mon Sep 17 00:00:00 2001 From: Geoffroy BONNEVILLE Date: Sat, 25 Apr 2020 22:34:50 +0200 Subject: [PATCH] Remove entry from mru when it does not exist anymore --- .../UWP/StorageFileClient.cs | 16 ++++++++++++++-- ModernKeePass/Strings/en-US/CodeBehind.resw | 6 ++++++ ModernKeePass/Strings/fr-FR/CodeBehind.resw | 6 ++++++ WinAppCommon/Messages/FileNotFoundMessage.cs | 7 +++++++ WinAppCommon/ViewModels/RecentVm.cs | 12 +++++++++--- .../UserControls/OpenDatabaseControlVm.cs | 12 +++++++++++- WinAppCommon/WinAppCommon.projitems | 1 + 7 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 WinAppCommon/Messages/FileNotFoundMessage.cs diff --git a/ModernKeePass.Infrastructure/UWP/StorageFileClient.cs b/ModernKeePass.Infrastructure/UWP/StorageFileClient.cs index 67bef8c..e1115df 100644 --- a/ModernKeePass.Infrastructure/UWP/StorageFileClient.cs +++ b/ModernKeePass.Infrastructure/UWP/StorageFileClient.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Runtime.InteropServices.WindowsRuntime; using System.Threading.Tasks; using Windows.Storage; @@ -38,10 +39,21 @@ namespace ModernKeePass.Infrastructure.UWP private async Task GetFile(string token) { if (StorageApplicationPermissions.MostRecentlyUsedList.ContainsItem(token)) - return await StorageApplicationPermissions.MostRecentlyUsedList.GetFileAsync(token).AsTask(); + { + try + { + return await StorageApplicationPermissions.MostRecentlyUsedList.GetFileAsync(token).AsTask(); + } + catch (Exception) + { + StorageApplicationPermissions.MostRecentlyUsedList.Remove(token); + throw new FileNotFoundException(); + } + } + if (StorageApplicationPermissions.FutureAccessList.ContainsItem(token)) return await StorageApplicationPermissions.FutureAccessList.GetFileAsync(token).AsTask(); - return null; + throw new FileNotFoundException(); } } } \ No newline at end of file diff --git a/ModernKeePass/Strings/en-US/CodeBehind.resw b/ModernKeePass/Strings/en-US/CodeBehind.resw index a6e2ccb..e8a3ef9 100644 --- a/ModernKeePass/Strings/en-US/CodeBehind.resw +++ b/ModernKeePass/Strings/en-US/CodeBehind.resw @@ -267,4 +267,10 @@ Open + + File does not exist anymore + + + File not found + \ No newline at end of file diff --git a/ModernKeePass/Strings/fr-FR/CodeBehind.resw b/ModernKeePass/Strings/fr-FR/CodeBehind.resw index c3e8b29..b3cfbe1 100644 --- a/ModernKeePass/Strings/fr-FR/CodeBehind.resw +++ b/ModernKeePass/Strings/fr-FR/CodeBehind.resw @@ -267,4 +267,10 @@ Open + + Le fichier n'existe plus. + + + Fichier manquant + \ No newline at end of file diff --git a/WinAppCommon/Messages/FileNotFoundMessage.cs b/WinAppCommon/Messages/FileNotFoundMessage.cs new file mode 100644 index 0000000..b3bf11e --- /dev/null +++ b/WinAppCommon/Messages/FileNotFoundMessage.cs @@ -0,0 +1,7 @@ +namespace Messages +{ + public class FileNotFoundMessage + { + + } +} \ No newline at end of file diff --git a/WinAppCommon/ViewModels/RecentVm.cs b/WinAppCommon/ViewModels/RecentVm.cs index c855fbc..3f0d5f0 100644 --- a/WinAppCommon/ViewModels/RecentVm.cs +++ b/WinAppCommon/ViewModels/RecentVm.cs @@ -3,13 +3,14 @@ using System.Linq; using System.Windows.Input; using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; +using Messages; using ModernKeePass.Application.Common.Interfaces; using ModernKeePass.Domain.Interfaces; using ModernKeePass.ViewModels.ListItems; namespace ModernKeePass.ViewModels { - public class RecentVm : ObservableObject, IHasSelectableObject + public class RecentVm : ViewModelBase, IHasSelectableObject { private readonly IRecentProxy _recent; private ISelectableModel _selectedItem; @@ -45,13 +46,18 @@ namespace ModernKeePass.ViewModels { _recent = recent; ClearAllCommand = new RelayCommand(ClearAll); - + PopulateRecentItems(); + MessengerInstance.Register(this, _ => PopulateRecentItems()); + } + + private void PopulateRecentItems() + { var recentItems = _recent.GetAll().Select(r => new RecentItemVm(r)); RecentItems = new ObservableCollection(recentItems); if (RecentItems.Count > 0) SelectedItem = RecentItems[0]; } - + private void ClearAll() { _recent.ClearAll(); diff --git a/WinAppCommon/ViewModels/UserControls/OpenDatabaseControlVm.cs b/WinAppCommon/ViewModels/UserControls/OpenDatabaseControlVm.cs index 231f1ac..de07859 100644 --- a/WinAppCommon/ViewModels/UserControls/OpenDatabaseControlVm.cs +++ b/WinAppCommon/ViewModels/UserControls/OpenDatabaseControlVm.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Text; using System.Threading.Tasks; using GalaSoft.MvvmLight; @@ -95,6 +96,7 @@ namespace ModernKeePass.ViewModels private readonly IMediator _mediator; private readonly IResourceProxy _resource; + private readonly INotificationService _notification; private bool _hasPassword; private bool _hasKeyFile; private bool _isOpening; @@ -105,10 +107,11 @@ namespace ModernKeePass.ViewModels private string _keyFileText; private string _openButtonLabel; - public OpenDatabaseControlVm(IMediator mediator, IResourceProxy resource) + public OpenDatabaseControlVm(IMediator mediator, IResourceProxy resource, INotificationService notification) { _mediator = mediator; _resource = resource; + _notification = notification; OpenDatabaseCommand = new RelayCommand(async databaseFilePath => await TryOpenDatabase(databaseFilePath), _ => IsValid); _keyFileText = _resource.GetResourceValue("CompositeKeyDefaultKeyFile"); _openButtonLabel = _resource.GetResourceValue("CompositeKeyOpenButtonLabel"); @@ -153,6 +156,13 @@ namespace ModernKeePass.ViewModels if (HasKeyFile) errorMessage.AppendLine(_resource.GetResourceValue("CompositeKeyErrorUserKeyFile")); UpdateStatus(errorMessage.ToString(), StatusTypes.Error); } + catch (FileNotFoundException) + { + _notification.Show( + $"{_resource.GetResourceValue("FileNotFoundTitle")}", + $"{_resource.GetResourceValue("FileNotFoundDescription")}"); + MessengerInstance.Send(new FileNotFoundMessage()); + } catch (Exception e) { var error = $"{_resource.GetResourceValue("CompositeKeyErrorOpen")}{e.Message}"; diff --git a/WinAppCommon/WinAppCommon.projitems b/WinAppCommon/WinAppCommon.projitems index 4b065f0..e75578e 100644 --- a/WinAppCommon/WinAppCommon.projitems +++ b/WinAppCommon/WinAppCommon.projitems @@ -36,6 +36,7 @@ +