2020-04-20 20:02:43 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-04-21 17:13:39 +02:00
|
|
|
|
using GalaSoft.MvvmLight.Command;
|
|
|
|
|
using GalaSoft.MvvmLight.Messaging;
|
2020-04-20 20:02:43 +02:00
|
|
|
|
using MediatR;
|
2020-04-21 17:13:39 +02:00
|
|
|
|
using Messages;
|
2020-04-20 20:02:43 +02:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using ModernKeePass.Application.Common.Interfaces;
|
|
|
|
|
using ModernKeePass.Application.Database.Queries.GetDatabase;
|
|
|
|
|
using ModernKeePass.Application.Database.Queries.OpenDatabase;
|
|
|
|
|
using ModernKeePass.Domain.AOP;
|
|
|
|
|
|
|
|
|
|
namespace ModernKeePass.ViewModels
|
|
|
|
|
{
|
|
|
|
|
public class OpenDatabaseControlVm : NotifyPropertyChangedBase
|
|
|
|
|
{
|
|
|
|
|
public enum StatusTypes
|
|
|
|
|
{
|
|
|
|
|
Normal = 0,
|
|
|
|
|
Error = 1,
|
|
|
|
|
Warning = 3,
|
|
|
|
|
Success = 5
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool HasPassword
|
|
|
|
|
{
|
|
|
|
|
get { return _hasPassword; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
SetProperty(ref _hasPassword, value);
|
|
|
|
|
OnPropertyChanged(nameof(IsValid));
|
2020-04-21 17:13:39 +02:00
|
|
|
|
OpenDatabaseCommand.RaiseCanExecuteChanged();
|
2020-04-20 20:02:43 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool HasKeyFile
|
|
|
|
|
{
|
|
|
|
|
get { return _hasKeyFile; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
SetProperty(ref _hasKeyFile, value);
|
|
|
|
|
OnPropertyChanged(nameof(IsValid));
|
2020-04-21 17:13:39 +02:00
|
|
|
|
OpenDatabaseCommand.RaiseCanExecuteChanged();
|
2020-04-20 20:02:43 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsValid => !_isOpening && (HasPassword || HasKeyFile && KeyFilePath != null);
|
|
|
|
|
|
|
|
|
|
public string Status
|
|
|
|
|
{
|
|
|
|
|
get { return _status; }
|
|
|
|
|
set { SetProperty(ref _status, value); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int StatusType
|
|
|
|
|
{
|
|
|
|
|
get { return (int)_statusType; }
|
|
|
|
|
set { SetProperty(ref _statusType, (StatusTypes)value); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Password
|
|
|
|
|
{
|
|
|
|
|
get { return _password; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_password = value;
|
|
|
|
|
StatusType = (int)StatusTypes.Normal;
|
|
|
|
|
Status = string.Empty;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string KeyFilePath
|
|
|
|
|
{
|
|
|
|
|
get { return _keyFilePath; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_keyFilePath = value;
|
|
|
|
|
OnPropertyChanged(nameof(IsValid));
|
2020-04-21 17:13:39 +02:00
|
|
|
|
OpenDatabaseCommand.RaiseCanExecuteChanged();
|
2020-04-20 20:02:43 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string KeyFileText
|
|
|
|
|
{
|
|
|
|
|
get { return _keyFileText; }
|
|
|
|
|
set { SetProperty(ref _keyFileText, value); }
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 17:13:39 +02:00
|
|
|
|
public string OpenButtonLabel
|
|
|
|
|
{
|
|
|
|
|
get { return _openButtonLabel; }
|
|
|
|
|
set { SetProperty(ref _openButtonLabel, value); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RelayCommand<string> OpenDatabaseCommand { get; }
|
2020-04-20 20:02:43 +02:00
|
|
|
|
|
|
|
|
|
protected readonly IMediator Mediator;
|
|
|
|
|
private readonly IResourceProxy _resource;
|
2020-04-21 17:13:39 +02:00
|
|
|
|
private readonly IMessenger _messenger;
|
2020-04-20 20:02:43 +02:00
|
|
|
|
private bool _hasPassword;
|
|
|
|
|
private bool _hasKeyFile;
|
|
|
|
|
private bool _isOpening;
|
|
|
|
|
private string _password = string.Empty;
|
|
|
|
|
private string _status;
|
|
|
|
|
private StatusTypes _statusType;
|
|
|
|
|
private string _keyFilePath;
|
|
|
|
|
private string _keyFileText;
|
2020-04-21 17:13:39 +02:00
|
|
|
|
private string _openButtonLabel;
|
2020-04-20 20:02:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public OpenDatabaseControlVm() : this(
|
|
|
|
|
App.Services.GetRequiredService<IMediator>(),
|
2020-04-21 17:13:39 +02:00
|
|
|
|
App.Services.GetRequiredService<IResourceProxy>(),
|
|
|
|
|
App.Services.GetRequiredService<IMessenger>())
|
2020-04-20 20:02:43 +02:00
|
|
|
|
{ }
|
|
|
|
|
|
2020-04-21 17:13:39 +02:00
|
|
|
|
public OpenDatabaseControlVm(IMediator mediator, IResourceProxy resource, IMessenger messenger)
|
2020-04-20 20:02:43 +02:00
|
|
|
|
{
|
|
|
|
|
Mediator = mediator;
|
|
|
|
|
_resource = resource;
|
2020-04-21 17:13:39 +02:00
|
|
|
|
_messenger = messenger;
|
|
|
|
|
OpenDatabaseCommand = new RelayCommand<string>(async databaseFilePath => await TryOpenDatabase(databaseFilePath), _ => IsValid);
|
2020-04-20 20:02:43 +02:00
|
|
|
|
_keyFileText = _resource.GetResourceValue("CompositeKeyDefaultKeyFile");
|
2020-04-21 17:13:39 +02:00
|
|
|
|
_openButtonLabel = _resource.GetResourceValue("CompositeKeyOpenButtonLabel");
|
2020-04-20 20:02:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 17:13:39 +02:00
|
|
|
|
public async Task TryOpenDatabase(string databaseFilePath)
|
2020-04-20 20:02:43 +02:00
|
|
|
|
{
|
2020-04-21 17:13:39 +02:00
|
|
|
|
_messenger.Send(new DatabaseOpeningMessage {Token = databaseFilePath});
|
|
|
|
|
|
|
|
|
|
var database = await Mediator.Send(new GetDatabaseQuery());
|
|
|
|
|
if (database.IsOpen)
|
|
|
|
|
{
|
|
|
|
|
_messenger.Send(new DatabaseAlreadyOpenedMessage { OpenedDatabase = database });
|
|
|
|
|
}
|
|
|
|
|
else await OpenDatabase(databaseFilePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task OpenDatabase(string databaseFilePath)
|
|
|
|
|
{
|
|
|
|
|
var oldLabel = _openButtonLabel;
|
|
|
|
|
OpenButtonLabel = _resource.GetResourceValue("CompositeKeyOpening");
|
|
|
|
|
_isOpening = true;
|
2020-04-20 20:02:43 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
OnPropertyChanged(nameof(IsValid));
|
2020-04-21 17:13:39 +02:00
|
|
|
|
OpenDatabaseCommand.RaiseCanExecuteChanged();
|
2020-04-20 20:02:43 +02:00
|
|
|
|
await Mediator.Send(new OpenDatabaseQuery
|
|
|
|
|
{
|
|
|
|
|
FilePath = databaseFilePath,
|
|
|
|
|
KeyFilePath = HasKeyFile ? KeyFilePath : null,
|
|
|
|
|
Password = HasPassword ? Password : null,
|
|
|
|
|
});
|
2020-04-21 17:13:39 +02:00
|
|
|
|
var rootGroupId = (await Mediator.Send(new GetDatabaseQuery())).RootGroupId;
|
|
|
|
|
|
|
|
|
|
_messenger.Send(new DatabaseOpenedMessage { RootGroupId = rootGroupId });
|
2020-04-20 20:02:43 +02:00
|
|
|
|
}
|
|
|
|
|
catch (ArgumentException)
|
|
|
|
|
{
|
|
|
|
|
var errorMessage = new StringBuilder($"{_resource.GetResourceValue("CompositeKeyErrorOpen")}\n");
|
|
|
|
|
if (HasPassword) errorMessage.AppendLine(_resource.GetResourceValue("CompositeKeyErrorUserPassword"));
|
|
|
|
|
if (HasKeyFile) errorMessage.AppendLine(_resource.GetResourceValue("CompositeKeyErrorUserKeyFile"));
|
|
|
|
|
UpdateStatus(errorMessage.ToString(), StatusTypes.Error);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
var error = $"{_resource.GetResourceValue("CompositeKeyErrorOpen")}{e.Message}";
|
|
|
|
|
UpdateStatus(error, StatusTypes.Error);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_isOpening = false;
|
|
|
|
|
OnPropertyChanged(nameof(IsValid));
|
2020-04-21 17:13:39 +02:00
|
|
|
|
OpenDatabaseCommand.RaiseCanExecuteChanged();
|
|
|
|
|
OpenButtonLabel = oldLabel;
|
2020-04-20 20:02:43 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateStatus(string text, StatusTypes type)
|
|
|
|
|
{
|
|
|
|
|
Status = text;
|
|
|
|
|
StatusType = (int)type;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|