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;
|
|
|
|
|
using Windows.Storage;
|
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;
|
|
|
|
|
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;
|
2017-10-10 15:00:31 +02:00
|
|
|
|
using ModernKeePass.Events;
|
2017-11-24 18:21:06 +01:00
|
|
|
|
using ModernKeePass.Extensions;
|
2018-06-19 18:47:37 +02:00
|
|
|
|
using ModernKeePass.Interfaces;
|
2017-12-04 12:20:05 +01:00
|
|
|
|
using ModernKeePass.Services;
|
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;
|
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) => { }));
|
|
|
|
|
|
2018-06-19 18:47:37 +02:00
|
|
|
|
public StorageFile DatabaseFile
|
|
|
|
|
{
|
|
|
|
|
get { return (StorageFile)GetValue(DatabaseFileProperty); }
|
|
|
|
|
set { SetValue(DatabaseFileProperty, value); }
|
|
|
|
|
}
|
|
|
|
|
public static readonly DependencyProperty DatabaseFileProperty =
|
|
|
|
|
DependencyProperty.Register(
|
|
|
|
|
"DatabaseFile",
|
|
|
|
|
typeof(StorageFile),
|
|
|
|
|
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-03-24 19:14:34 +01:00
|
|
|
|
public CompositeKeyUserControl(): this(App.Mediator)
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
public CompositeKeyUserControl(IMediator mediator)
|
2017-09-30 09:00:32 -04:00
|
|
|
|
{
|
2020-03-24 19:14:34 +01:00
|
|
|
|
_mediator = mediator;
|
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-03-30 19:43:04 +02:00
|
|
|
|
ValidationChecked?.Invoke(this, new PasswordEventArgs(Model.RootGroup));
|
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());
|
2017-12-04 12:20:05 +01:00
|
|
|
|
var resource = new ResourcesService();
|
2018-06-19 18:47:37 +02:00
|
|
|
|
if (database.IsOpen)
|
2017-11-24 18:21:06 +01:00
|
|
|
|
{
|
2018-06-20 17:20:15 +02:00
|
|
|
|
await MessageDialogHelper.ShowActionDialog(resource.GetResourceValue("MessageDialogDBOpenTitle"),
|
2018-06-19 18:47:37 +02:00
|
|
|
|
string.Format(resource.GetResourceValue("MessageDialogDBOpenDesc"), database.Name),
|
|
|
|
|
resource.GetResourceValue("MessageDialogDBOpenButtonSave"),
|
|
|
|
|
resource.GetResourceValue("MessageDialogDBOpenButtonDiscard"),
|
|
|
|
|
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,
|
|
|
|
|
resource.GetResourceValue("ToastSavedMessage"));
|
2020-03-24 19:14:34 +01:00
|
|
|
|
await _mediator.Send(new CloseDatabaseCommand());
|
2018-06-19 18:47:37 +02:00
|
|
|
|
await OpenDatabase(resource);
|
|
|
|
|
},
|
|
|
|
|
async command =>
|
|
|
|
|
{
|
2020-03-24 19:14:34 +01:00
|
|
|
|
await _mediator.Send(new CloseDatabaseCommand());
|
2018-06-19 18:47:37 +02:00
|
|
|
|
await OpenDatabase(resource);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await OpenDatabase(resource);
|
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;
|
|
|
|
|
Model.KeyFile = file;
|
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-03-24 17:31:34 +01:00
|
|
|
|
await Model.CreateKeyFile(file);
|
2017-11-17 10:20:54 +01:00
|
|
|
|
}
|
2018-06-19 18:47:37 +02:00
|
|
|
|
|
|
|
|
|
private async Task OpenDatabase(IResourceService resource)
|
|
|
|
|
{
|
|
|
|
|
var oldLabel = ButtonLabel;
|
|
|
|
|
ButtonLabel = resource.GetResourceValue("CompositeKeyOpening");
|
|
|
|
|
if (await Dispatcher.RunTaskAsync(async () => await Model.OpenDatabase(DatabaseFile, CreateNew)))
|
|
|
|
|
{
|
2020-03-30 19:43:04 +02:00
|
|
|
|
ValidationChecked?.Invoke(this, new PasswordEventArgs(Model.RootGroup));
|
2018-06-19 18:47:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ButtonLabel = oldLabel;
|
|
|
|
|
}
|
2017-09-30 09:00:32 -04:00
|
|
|
|
}
|
|
|
|
|
}
|