2017-10-12 10:36:58 +02:00
|
|
|
|
using System;
|
2017-11-17 10:20:54 +01:00
|
|
|
|
using System.Collections.Generic;
|
2018-06-19 18:47:37 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2020-04-06 20:20:45 +02:00
|
|
|
|
using Windows.Storage.AccessCache;
|
2017-11-03 15:48:55 +01:00
|
|
|
|
using Windows.Storage.Pickers;
|
2017-10-02 10:44:04 +02:00
|
|
|
|
using Windows.System;
|
2017-09-30 09:00:32 -04:00
|
|
|
|
using Windows.UI.Xaml;
|
|
|
|
|
using Windows.UI.Xaml.Input;
|
2020-03-24 19:14:34 +01:00
|
|
|
|
using MediatR;
|
2020-04-06 20:20:45 +02:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-03-24 19:14:34 +01:00
|
|
|
|
using ModernKeePass.Application.Database.Commands.CloseDatabase;
|
|
|
|
|
using ModernKeePass.Application.Database.Commands.SaveDatabase;
|
|
|
|
|
using ModernKeePass.Application.Database.Queries.GetDatabase;
|
2018-06-19 18:47:37 +02:00
|
|
|
|
using ModernKeePass.Common;
|
2020-04-06 20:20:45 +02:00
|
|
|
|
using ModernKeePass.Domain.Dtos;
|
2017-10-10 15:00:31 +02:00
|
|
|
|
using ModernKeePass.Events;
|
2017-11-24 18:21:06 +01:00
|
|
|
|
using ModernKeePass.Extensions;
|
2017-11-07 18:45:35 +01:00
|
|
|
|
using ModernKeePass.ViewModels;
|
2017-09-30 09:00:32 -04:00
|
|
|
|
|
|
|
|
|
// Pour en savoir plus sur le modèle d'élément Contrôle utilisateur, consultez la page http://go.microsoft.com/fwlink/?LinkId=234236
|
|
|
|
|
|
2017-12-08 19:38:33 +01:00
|
|
|
|
namespace ModernKeePass.Views.UserControls
|
2017-09-30 09:00:32 -04:00
|
|
|
|
{
|
2017-11-07 18:50:36 +01:00
|
|
|
|
public sealed partial class CompositeKeyUserControl
|
2017-09-30 09:00:32 -04:00
|
|
|
|
{
|
2020-03-24 19:14:34 +01:00
|
|
|
|
private readonly IMediator _mediator;
|
2020-04-06 20:20:45 +02:00
|
|
|
|
private readonly ResourceHelper _resource;
|
2017-11-07 18:45:35 +01:00
|
|
|
|
public CompositeKeyVm Model => Grid.DataContext as CompositeKeyVm;
|
2017-11-07 11:45:02 +01:00
|
|
|
|
|
2017-10-11 18:43:27 +02:00
|
|
|
|
public bool CreateNew
|
2017-10-11 14:30:07 +02:00
|
|
|
|
{
|
2017-10-11 18:43:27 +02:00
|
|
|
|
get { return (bool)GetValue(CreateNewProperty); }
|
|
|
|
|
set { SetValue(CreateNewProperty, value); }
|
2017-10-11 14:30:07 +02:00
|
|
|
|
}
|
2017-10-11 18:43:27 +02:00
|
|
|
|
public static readonly DependencyProperty CreateNewProperty =
|
2017-10-11 14:30:07 +02:00
|
|
|
|
DependencyProperty.Register(
|
2017-10-11 18:43:27 +02:00
|
|
|
|
"CreateNew",
|
|
|
|
|
typeof(bool),
|
2017-11-07 18:50:36 +01:00
|
|
|
|
typeof(CompositeKeyUserControl),
|
2017-10-16 16:16:58 +02:00
|
|
|
|
new PropertyMetadata(false, (o, args) => { }));
|
|
|
|
|
|
2017-11-07 18:45:35 +01:00
|
|
|
|
public bool UpdateKey
|
2017-10-16 16:16:58 +02:00
|
|
|
|
{
|
2017-11-07 18:45:35 +01:00
|
|
|
|
get { return (bool)GetValue(UpdateKeyProperty); }
|
|
|
|
|
set { SetValue(UpdateKeyProperty, value); }
|
2017-10-16 16:16:58 +02:00
|
|
|
|
}
|
2017-11-07 18:45:35 +01:00
|
|
|
|
public static readonly DependencyProperty UpdateKeyProperty =
|
2017-10-16 16:16:58 +02:00
|
|
|
|
DependencyProperty.Register(
|
2017-11-07 18:45:35 +01:00
|
|
|
|
"UpdateKey",
|
|
|
|
|
typeof(bool),
|
2017-11-07 18:50:36 +01:00
|
|
|
|
typeof(CompositeKeyUserControl),
|
2017-11-07 18:45:35 +01:00
|
|
|
|
new PropertyMetadata(false, (o, args) => { }));
|
2017-11-07 11:45:02 +01:00
|
|
|
|
|
2017-11-24 18:21:06 +01:00
|
|
|
|
public string ButtonLabel
|
|
|
|
|
{
|
|
|
|
|
get { return (string)GetValue(ButtonLabelProperty); }
|
|
|
|
|
set { SetValue(ButtonLabelProperty, value); }
|
|
|
|
|
}
|
|
|
|
|
public static readonly DependencyProperty ButtonLabelProperty =
|
|
|
|
|
DependencyProperty.Register(
|
|
|
|
|
"ButtonLabel",
|
|
|
|
|
typeof(string),
|
|
|
|
|
typeof(CompositeKeyUserControl),
|
|
|
|
|
new PropertyMetadata("OK", (o, args) => { }));
|
|
|
|
|
|
2020-04-06 20:20:45 +02:00
|
|
|
|
public string DatabaseFilePath
|
2018-06-19 18:47:37 +02:00
|
|
|
|
{
|
2020-04-06 20:20:45 +02:00
|
|
|
|
get { return (string)GetValue(DatabaseFilePathProperty); }
|
|
|
|
|
set { SetValue(DatabaseFilePathProperty, value); }
|
2018-06-19 18:47:37 +02:00
|
|
|
|
}
|
2020-04-06 20:20:45 +02:00
|
|
|
|
public static readonly DependencyProperty DatabaseFilePathProperty =
|
2018-06-19 18:47:37 +02:00
|
|
|
|
DependencyProperty.Register(
|
2020-04-06 20:20:45 +02:00
|
|
|
|
"DatabaseFilePath",
|
|
|
|
|
typeof(string),
|
2018-06-19 18:47:37 +02:00
|
|
|
|
typeof(CompositeKeyUserControl),
|
|
|
|
|
new PropertyMetadata(null, (o, args) => { }));
|
2017-11-24 18:21:06 +01:00
|
|
|
|
|
2017-11-07 18:45:35 +01:00
|
|
|
|
public bool ShowComplexityIndicator => CreateNew || UpdateKey;
|
2017-11-07 11:45:02 +01:00
|
|
|
|
|
2020-04-09 19:43:06 +02:00
|
|
|
|
public CompositeKeyUserControl(): this(App.Services.GetRequiredService<IMediator>())
|
2020-03-24 19:14:34 +01:00
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
public CompositeKeyUserControl(IMediator mediator)
|
2017-09-30 09:00:32 -04:00
|
|
|
|
{
|
2020-03-24 19:14:34 +01:00
|
|
|
|
_mediator = mediator;
|
2020-04-06 20:20:45 +02:00
|
|
|
|
_resource = new ResourceHelper();
|
2017-10-01 08:48:29 -04:00
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-26 12:01:49 +02:00
|
|
|
|
public event EventHandler ValidationChecking;
|
|
|
|
|
public event EventHandler<PasswordEventArgs> ValidationChecked;
|
2017-10-01 08:48:29 -04:00
|
|
|
|
|
2017-11-24 18:21:06 +01:00
|
|
|
|
private async void OpenButton_OnClick(object sender, RoutedEventArgs e)
|
2017-10-01 08:48:29 -04:00
|
|
|
|
{
|
2017-10-12 10:36:58 +02:00
|
|
|
|
ValidationChecking?.Invoke(this, new EventArgs());
|
2017-11-07 18:45:35 +01:00
|
|
|
|
|
2017-11-27 15:26:36 +01:00
|
|
|
|
if (UpdateKey)
|
|
|
|
|
{
|
2020-03-24 17:31:34 +01:00
|
|
|
|
await Model.UpdateKey();
|
2020-04-02 19:12:16 +02:00
|
|
|
|
ValidationChecked?.Invoke(this, new PasswordEventArgs(Model.RootGroupId));
|
2017-11-27 15:26:36 +01:00
|
|
|
|
}
|
2017-11-24 18:21:06 +01:00
|
|
|
|
else
|
2017-10-24 18:43:46 +02:00
|
|
|
|
{
|
2020-03-24 19:14:34 +01:00
|
|
|
|
var database = await _mediator.Send(new GetDatabaseQuery());
|
2018-06-19 18:47:37 +02:00
|
|
|
|
if (database.IsOpen)
|
2017-11-24 18:21:06 +01:00
|
|
|
|
{
|
2020-04-06 20:20:45 +02:00
|
|
|
|
await MessageDialogHelper.ShowActionDialog(_resource.GetResourceValue("MessageDialogDBOpenTitle"),
|
|
|
|
|
string.Format(_resource.GetResourceValue("MessageDialogDBOpenDesc"), database.Name),
|
|
|
|
|
_resource.GetResourceValue("MessageDialogDBOpenButtonSave"),
|
|
|
|
|
_resource.GetResourceValue("MessageDialogDBOpenButtonDiscard"),
|
2018-06-19 18:47:37 +02:00
|
|
|
|
async command =>
|
|
|
|
|
{
|
2020-03-24 19:14:34 +01:00
|
|
|
|
await _mediator.Send(new SaveDatabaseCommand());
|
2018-06-21 11:13:40 +02:00
|
|
|
|
ToastNotificationHelper.ShowGenericToast(
|
|
|
|
|
database.Name,
|
2020-04-06 20:20:45 +02:00
|
|
|
|
_resource.GetResourceValue("ToastSavedMessage"));
|
2020-03-24 19:14:34 +01:00
|
|
|
|
await _mediator.Send(new CloseDatabaseCommand());
|
2020-04-06 20:20:45 +02:00
|
|
|
|
await OpenDatabase();
|
2018-06-19 18:47:37 +02:00
|
|
|
|
},
|
|
|
|
|
async command =>
|
|
|
|
|
{
|
2020-03-24 19:14:34 +01:00
|
|
|
|
await _mediator.Send(new CloseDatabaseCommand());
|
2020-04-06 20:20:45 +02:00
|
|
|
|
await OpenDatabase();
|
2018-06-19 18:47:37 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-04-06 20:20:45 +02:00
|
|
|
|
await OpenDatabase();
|
2017-11-24 18:21:06 +01:00
|
|
|
|
}
|
2017-10-24 18:43:46 +02:00
|
|
|
|
}
|
2017-10-01 08:48:29 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PasswordBox_KeyDown(object sender, KeyRoutedEventArgs e)
|
|
|
|
|
{
|
2018-01-08 18:52:03 +01:00
|
|
|
|
if (e.Key == VirtualKey.Enter && Model.IsValid)
|
|
|
|
|
{
|
|
|
|
|
OpenButton_OnClick(sender, e);
|
|
|
|
|
// Stop the event from triggering twice
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
}
|
2017-09-30 09:00:32 -04:00
|
|
|
|
}
|
2017-11-03 15:48:55 +01:00
|
|
|
|
|
|
|
|
|
private async void KeyFileButton_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
2020-03-20 17:05:46 +01:00
|
|
|
|
var picker = new FileOpenPicker
|
|
|
|
|
{
|
|
|
|
|
ViewMode = PickerViewMode.List,
|
|
|
|
|
SuggestedStartLocation = PickerLocationId.DocumentsLibrary
|
|
|
|
|
};
|
|
|
|
|
picker.FileTypeFilter.Add("*");
|
2017-11-03 15:48:55 +01:00
|
|
|
|
|
|
|
|
|
// Application now has read/write access to the picked file
|
2017-11-13 11:28:14 +01:00
|
|
|
|
var file = await picker.PickSingleFileAsync();
|
|
|
|
|
if (file == null) return;
|
2020-04-06 20:20:45 +02:00
|
|
|
|
|
|
|
|
|
var token = StorageApplicationPermissions.FutureAccessList.Add(file);
|
|
|
|
|
Model.KeyFilePath = token;
|
2017-11-07 11:45:02 +01:00
|
|
|
|
}
|
2017-11-17 10:20:54 +01:00
|
|
|
|
|
|
|
|
|
private async void CreateKeyFileButton_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var savePicker = new FileSavePicker
|
|
|
|
|
{
|
|
|
|
|
SuggestedStartLocation = PickerLocationId.DocumentsLibrary,
|
|
|
|
|
SuggestedFileName = "Key"
|
|
|
|
|
};
|
|
|
|
|
savePicker.FileTypeChoices.Add("Key file", new List<string> { ".key" });
|
|
|
|
|
|
|
|
|
|
var file = await savePicker.PickSaveFileAsync();
|
|
|
|
|
if (file == null) return;
|
|
|
|
|
|
2020-04-06 20:20:45 +02:00
|
|
|
|
var token = StorageApplicationPermissions.FutureAccessList.Add(file);
|
|
|
|
|
await Model.CreateKeyFile(new FileInfo
|
|
|
|
|
{
|
2020-04-07 12:48:18 +02:00
|
|
|
|
Id = token,
|
|
|
|
|
Name = file.DisplayName,
|
|
|
|
|
Path = file.Path
|
2020-04-06 20:20:45 +02:00
|
|
|
|
});
|
2017-11-17 10:20:54 +01:00
|
|
|
|
}
|
2018-06-19 18:47:37 +02:00
|
|
|
|
|
2020-04-06 20:20:45 +02:00
|
|
|
|
private async Task OpenDatabase()
|
2018-06-19 18:47:37 +02:00
|
|
|
|
{
|
|
|
|
|
var oldLabel = ButtonLabel;
|
2020-04-06 20:20:45 +02:00
|
|
|
|
ButtonLabel = _resource.GetResourceValue("CompositeKeyOpening");
|
|
|
|
|
if (await Dispatcher.RunTaskAsync(async () => await Model.OpenDatabase(DatabaseFilePath, CreateNew)))
|
2018-06-19 18:47:37 +02:00
|
|
|
|
{
|
2020-04-02 19:12:16 +02:00
|
|
|
|
ValidationChecked?.Invoke(this, new PasswordEventArgs(Model.RootGroupId));
|
2018-06-19 18:47:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ButtonLabel = oldLabel;
|
|
|
|
|
}
|
2017-09-30 09:00:32 -04:00
|
|
|
|
}
|
|
|
|
|
}
|