2017-10-12 10:36:58 +02:00
|
|
|
|
using System;
|
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;
|
2017-10-10 15:00:31 +02:00
|
|
|
|
using ModernKeePass.Common;
|
|
|
|
|
using ModernKeePass.Events;
|
2017-11-04 12:11:30 -04:00
|
|
|
|
using ModernKeePassLib.Cryptography;
|
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
|
|
|
|
|
|
|
|
|
|
namespace ModernKeePass.Controls
|
|
|
|
|
{
|
2017-10-12 17:45:37 +02:00
|
|
|
|
public sealed partial class OpenDatabaseUserControl
|
2017-09-30 09:00:32 -04: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-10-11 14:30:07 +02:00
|
|
|
|
typeof(OpenDatabaseUserControl),
|
2017-10-16 16:16:58 +02:00
|
|
|
|
new PropertyMetadata(false, (o, args) => { }));
|
|
|
|
|
|
|
|
|
|
public string Password
|
|
|
|
|
{
|
|
|
|
|
get { return (string)GetValue(PasswordProperty); }
|
|
|
|
|
set { SetValue(PasswordProperty, value); }
|
|
|
|
|
}
|
|
|
|
|
public static readonly DependencyProperty PasswordProperty =
|
|
|
|
|
DependencyProperty.Register(
|
|
|
|
|
"Password",
|
|
|
|
|
typeof(string),
|
|
|
|
|
typeof(OpenDatabaseUserControl),
|
|
|
|
|
new PropertyMetadata(string.Empty, (o, args) => { }));
|
2017-11-04 12:11:30 -04:00
|
|
|
|
|
2017-10-02 10:44:04 +02:00
|
|
|
|
public OpenDatabaseUserControl()
|
2017-09-30 09:00:32 -04:00
|
|
|
|
{
|
2017-10-01 08:48:29 -04:00
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-12 10:36:58 +02:00
|
|
|
|
public event PasswordCheckingEventHandler ValidationChecking;
|
|
|
|
|
public delegate void PasswordCheckingEventHandler(object sender, EventArgs e);
|
2017-10-02 10:44:04 +02:00
|
|
|
|
public event PasswordCheckedEventHandler ValidationChecked;
|
2017-10-10 15:00:31 +02:00
|
|
|
|
public delegate void PasswordCheckedEventHandler(object sender, PasswordEventArgs e);
|
2017-10-01 08:48:29 -04:00
|
|
|
|
|
|
|
|
|
private void OpenButton_OnClick(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
2017-10-12 10:36:58 +02:00
|
|
|
|
ValidationChecking?.Invoke(this, new EventArgs());
|
2017-10-01 08:48:29 -04:00
|
|
|
|
var app = (App)Application.Current;
|
2017-11-03 15:48:55 +01:00
|
|
|
|
StatusTextBlock.Text = app.Database.Open(PasswordCheckBox.IsChecked.HasValue && PasswordCheckBox.IsChecked.Value ? PasswordBox.Password : null, CreateNew);
|
2017-10-10 15:00:31 +02:00
|
|
|
|
if (app.Database.Status == DatabaseHelper.DatabaseStatus.Opened)
|
2017-10-20 20:02:52 +02:00
|
|
|
|
{
|
2017-10-10 15:00:31 +02:00
|
|
|
|
ValidationChecked?.Invoke(this, new PasswordEventArgs(app.Database.RootGroup));
|
2017-10-20 20:02:52 +02:00
|
|
|
|
}
|
2017-10-24 18:43:46 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
VisualStateManager.GoToState(PasswordBox, "Error", true);
|
|
|
|
|
}
|
2017-10-01 08:48:29 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PasswordBox_KeyDown(object sender, KeyRoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.Key == VirtualKey.Enter) OpenButton_OnClick(null, null);
|
2017-10-24 18:43:46 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
VisualStateManager.GoToState(PasswordBox, "Normal", true);
|
|
|
|
|
StatusTextBlock.Text = string.Empty;
|
|
|
|
|
}
|
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)
|
|
|
|
|
{
|
|
|
|
|
var picker =
|
|
|
|
|
new FileOpenPicker
|
|
|
|
|
{
|
|
|
|
|
ViewMode = PickerViewMode.List,
|
|
|
|
|
SuggestedStartLocation = PickerLocationId.DocumentsLibrary
|
|
|
|
|
};
|
|
|
|
|
picker.FileTypeFilter.Add(".key");
|
|
|
|
|
|
|
|
|
|
// Application now has read/write access to the picked file
|
|
|
|
|
var file = await picker.PickSingleFileAsync();
|
|
|
|
|
if (file == null) return;
|
|
|
|
|
var app = (App)Application.Current;
|
|
|
|
|
app.Database.KeyFile = file;
|
|
|
|
|
StatusTextBlock.Text = $"Key file: {file.Name}";
|
|
|
|
|
}
|
2017-09-30 09:00:32 -04:00
|
|
|
|
}
|
|
|
|
|
}
|