From 2f1355104e5f15367ba26134fdb32769e4844ee4 Mon Sep 17 00:00:00 2001 From: Geoffroy Bonneville Date: Wed, 11 Oct 2017 18:43:27 +0200 Subject: [PATCH] WIP New database (and open and recent...) --- ModernKeePass/App.xaml.cs | 5 +- ModernKeePass/Common/DatabaseHelper.cs | 27 +++++++--- .../Controls/OpenDatabaseUserControl.xaml.cs | 14 ++--- ModernKeePass/ModernKeePass.csproj | 8 +++ ModernKeePass/Package.appxmanifest | 7 +++ ModernKeePass/Pages/NewDatabasePage.xaml | 23 ++++++++ ModernKeePass/Pages/NewDatabasePage.xaml.cs | 52 +++++++++++++++++++ ModernKeePass/Pages/OpenDatabasePage.xaml | 2 +- ModernKeePass/Pages/RecentDatabasesPage.xaml | 2 +- .../ViewModels/Items/RecentItemVm.cs | 1 - ModernKeePass/ViewModels/MainVm.cs | 5 +- ModernKeePass/ViewModels/NewVm.cs | 43 +++++++++++++++ ModernKeePass/ViewModels/OpenVm.cs | 11 ++-- ModernKeePass/ViewModels/RecentVm.cs | 10 +++- 14 files changed, 185 insertions(+), 25 deletions(-) create mode 100644 ModernKeePass/Pages/NewDatabasePage.xaml create mode 100644 ModernKeePass/Pages/NewDatabasePage.xaml.cs create mode 100644 ModernKeePass/ViewModels/NewVm.cs diff --git a/ModernKeePass/App.xaml.cs b/ModernKeePass/App.xaml.cs index 970952c..01b53e8 100644 --- a/ModernKeePass/App.xaml.cs +++ b/ModernKeePass/App.xaml.cs @@ -20,6 +20,7 @@ namespace ModernKeePass sealed partial class App : Application { public DatabaseHelper Database { get; set; } = new DatabaseHelper(); + /// /// Initializes the singleton application object. This is the first line of authored code /// executed, and as such is the logical equivalent of main() or WinMain(). @@ -106,11 +107,11 @@ namespace ModernKeePass protected override void OnFileActivated(FileActivatedEventArgs args) { base.OnFileActivated(args); - /*Database.DatabaseFile = args.Files[0] as StorageFile; var rootFrame = new Frame(); + Database.DatabaseFile = args.Files[0] as StorageFile; rootFrame.Navigate(typeof(MainPage), args); Window.Current.Content = rootFrame; - Window.Current.Activate();*/ + Window.Current.Activate(); } } } diff --git a/ModernKeePass/Common/DatabaseHelper.cs b/ModernKeePass/Common/DatabaseHelper.cs index 247a317..d6e598a 100644 --- a/ModernKeePass/Common/DatabaseHelper.cs +++ b/ModernKeePass/Common/DatabaseHelper.cs @@ -16,23 +16,38 @@ namespace ModernKeePass.Common Opening = 1, Opened = 2 } - private readonly PwDatabase _pwDatabase = new PwDatabase(); + private PwDatabase _pwDatabase = new PwDatabase(); + private StorageFile _databaseFile; public GroupVm RootGroup { get; set; } public DatabaseStatus Status { get; private set; } = DatabaseStatus.Closed; + public string Name => _pwDatabase?.Name; - public string Name { get; private set; } - public string Open(StorageFile databaseFile, string password) + public StorageFile DatabaseFile + { + get { return _databaseFile; } + set + { + _databaseFile = value; + Status = DatabaseStatus.Opening; + } + } + + public string Open(string password, bool createNew = false) { var key = new CompositeKey(); try { key.AddUserKey(new KcpPassword(password)); - _pwDatabase.Open(IOConnectionInfo.FromFile(databaseFile), key, new NullStatusLogger()); + var ioConnection = IOConnectionInfo.FromFile(DatabaseFile); + if (createNew) + { + _pwDatabase.New(ioConnection, key); + } + _pwDatabase.Open(ioConnection, key, new NullStatusLogger()); if (_pwDatabase.IsOpen) { - Name = databaseFile.Name; Status = DatabaseStatus.Opened; RootGroup = new GroupVm(_pwDatabase.RootGroup, null); } @@ -51,7 +66,7 @@ namespace ModernKeePass.Common } return string.Empty; } - + public void Save() { if (_pwDatabase != null && _pwDatabase.IsOpen) diff --git a/ModernKeePass/Controls/OpenDatabaseUserControl.xaml.cs b/ModernKeePass/Controls/OpenDatabaseUserControl.xaml.cs index 502a8cb..5128a00 100644 --- a/ModernKeePass/Controls/OpenDatabaseUserControl.xaml.cs +++ b/ModernKeePass/Controls/OpenDatabaseUserControl.xaml.cs @@ -14,15 +14,15 @@ namespace ModernKeePass.Controls { public sealed partial class OpenDatabaseUserControl : UserControl { - public StorageFile DatabaseFile + public bool CreateNew { - get { return (StorageFile)GetValue(DatabaseFileProperty); } - set { SetValue(DatabaseFileProperty, value); } + get { return (bool)GetValue(CreateNewProperty); } + set { SetValue(CreateNewProperty, value); } } - public static readonly DependencyProperty DatabaseFileProperty = + public static readonly DependencyProperty CreateNewProperty = DependencyProperty.Register( - "DatabaseFile", - typeof(StorageFile), + "CreateNew", + typeof(bool), typeof(OpenDatabaseUserControl), new PropertyMetadata(null, (o, args) => { })); @@ -37,7 +37,7 @@ namespace ModernKeePass.Controls private void OpenButton_OnClick(object sender, RoutedEventArgs e) { var app = (App)Application.Current; - StatusTextBlock.Text = app.Database.Open(DatabaseFile, PasswordBox.Password); + StatusTextBlock.Text = app.Database.Open(PasswordBox.Password, CreateNew); if (app.Database.Status == DatabaseHelper.DatabaseStatus.Opened) ValidationChecked?.Invoke(this, new PasswordEventArgs(app.Database.RootGroup)); } diff --git a/ModernKeePass/ModernKeePass.csproj b/ModernKeePass/ModernKeePass.csproj index e8866e7..41283d7 100644 --- a/ModernKeePass/ModernKeePass.csproj +++ b/ModernKeePass/ModernKeePass.csproj @@ -132,6 +132,9 @@ MainPage.xaml + + NewDatabasePage.xaml + @@ -153,6 +156,7 @@ + @@ -191,6 +195,10 @@ Designer MSBuild:Compile + + Designer + MSBuild:Compile + Designer MSBuild:Compile diff --git a/ModernKeePass/Package.appxmanifest b/ModernKeePass/Package.appxmanifest index 779e8cf..331b972 100644 --- a/ModernKeePass/Package.appxmanifest +++ b/ModernKeePass/Package.appxmanifest @@ -37,6 +37,13 @@ + + + + .kdbx + + + diff --git a/ModernKeePass/Pages/NewDatabasePage.xaml b/ModernKeePass/Pages/NewDatabasePage.xaml new file mode 100644 index 0000000..0ce15df --- /dev/null +++ b/ModernKeePass/Pages/NewDatabasePage.xaml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + diff --git a/ModernKeePass/Pages/NewDatabasePage.xaml.cs b/ModernKeePass/Pages/NewDatabasePage.xaml.cs new file mode 100644 index 0000000..6805a8f --- /dev/null +++ b/ModernKeePass/Pages/NewDatabasePage.xaml.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using Windows.Storage.Pickers; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; +using Windows.UI.Xaml.Navigation; +using ModernKeePass.Events; +using ModernKeePass.ViewModels; + +// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238 + +namespace ModernKeePass.Pages +{ + /// + /// An empty page that can be used on its own or navigated to within a Frame. + /// + public sealed partial class NewDatabasePage : Page + { + private Frame _mainFrame; + + public NewDatabasePage() + { + InitializeComponent(); + } + + protected override void OnNavigatedTo(NavigationEventArgs e) + { + base.OnNavigatedTo(e); + _mainFrame = e.Parameter as Frame; + } + + private async void ButtonBase_OnClick(object sender, RoutedEventArgs e) + { + var savePicker = new FileSavePicker + { + SuggestedStartLocation = PickerLocationId.DocumentsLibrary, + SuggestedFileName = "New Database" + }; + savePicker.FileTypeChoices.Add("KeePass 2.x database", new List { ".kdbx" }); + + var file = await savePicker.PickSaveFileAsync(); + if (file == null) return; + var viewModel = DataContext as OpenVm; + viewModel.OpenFile(file); + } + + private void PasswordUserControl_PasswordChecked(object sender, PasswordEventArgs e) + { + _mainFrame.Navigate(typeof(GroupDetailPage), e.RootGroup); + } + } +} diff --git a/ModernKeePass/Pages/OpenDatabasePage.xaml b/ModernKeePass/Pages/OpenDatabasePage.xaml index 254d5c6..65e32ef 100644 --- a/ModernKeePass/Pages/OpenDatabasePage.xaml +++ b/ModernKeePass/Pages/OpenDatabasePage.xaml @@ -19,6 +19,6 @@ - + \ No newline at end of file diff --git a/ModernKeePass/Pages/RecentDatabasesPage.xaml b/ModernKeePass/Pages/RecentDatabasesPage.xaml index 23a53cf..27bda3a 100644 --- a/ModernKeePass/Pages/RecentDatabasesPage.xaml +++ b/ModernKeePass/Pages/RecentDatabasesPage.xaml @@ -23,7 +23,7 @@ - + diff --git a/ModernKeePass/ViewModels/Items/RecentItemVm.cs b/ModernKeePass/ViewModels/Items/RecentItemVm.cs index 1c46c89..b09a268 100644 --- a/ModernKeePass/ViewModels/Items/RecentItemVm.cs +++ b/ModernKeePass/ViewModels/Items/RecentItemVm.cs @@ -8,7 +8,6 @@ namespace ModernKeePass.ViewModels private bool _isSelected; public string Token { get; set; } public string Name { get; set; } - public StorageFile File { get; set; } public bool IsSelected { diff --git a/ModernKeePass/ViewModels/MainVm.cs b/ModernKeePass/ViewModels/MainVm.cs index 5311e70..086951c 100644 --- a/ModernKeePass/ViewModels/MainVm.cs +++ b/ModernKeePass/ViewModels/MainVm.cs @@ -53,7 +53,10 @@ namespace ModernKeePass.ViewModels Title = "Open", PageType = typeof(OpenDatabasePage), Destination = destinationFrame, Parameter = referenceFrame, SymbolIcon = Symbol.Page2, IsSelected = app.Database.Status == DatabaseHelper.DatabaseStatus.Opening }, - new MainMenuItemVm {Title = "New" /*, PageType = typeof(NewDatabasePage)*/, Destination = destinationFrame, SymbolIcon = Symbol.Add}, + new MainMenuItemVm + { + Title = "New" , PageType = typeof(NewDatabasePage), Destination = destinationFrame, Parameter = referenceFrame, SymbolIcon = Symbol.Add + }, new MainMenuItemVm { Title = "Save" , PageType = typeof(SaveDatabasePage), Destination = destinationFrame, Parameter = referenceFrame, SymbolIcon = Symbol.Save, diff --git a/ModernKeePass/ViewModels/NewVm.cs b/ModernKeePass/ViewModels/NewVm.cs new file mode 100644 index 0000000..fdbacee --- /dev/null +++ b/ModernKeePass/ViewModels/NewVm.cs @@ -0,0 +1,43 @@ +using System.ComponentModel; +using Windows.Storage; +using Windows.Storage.AccessCache; +using Windows.UI.Xaml; +using ModernKeePass.Common; + +namespace ModernKeePass.ViewModels +{ + public class NewVm : INotifyPropertyChanged + { + public bool ShowPasswordBox + { + get { return ((App)Application.Current).Database.Status == DatabaseHelper.DatabaseStatus.Opening; } + } + + public string Name + { + get { return ((App)Application.Current).Database.Name; } + } + + public event PropertyChangedEventHandler PropertyChanged; + + private void NotifyPropertyChanged(string propertyName) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + + public void OpenFile(StorageFile file) + { + var database = ((App)Application.Current).Database; + database.DatabaseFile = file; + NotifyPropertyChanged("Name"); + NotifyPropertyChanged("ShowPasswordBox"); + AddToRecentList(file); + } + + private void AddToRecentList(StorageFile file) + { + var mru = StorageApplicationPermissions.MostRecentlyUsedList; + mru.Add(file, file.DisplayName); + } + } +} \ No newline at end of file diff --git a/ModernKeePass/ViewModels/OpenVm.cs b/ModernKeePass/ViewModels/OpenVm.cs index 3e6cfc2..f8c6e90 100644 --- a/ModernKeePass/ViewModels/OpenVm.cs +++ b/ModernKeePass/ViewModels/OpenVm.cs @@ -1,25 +1,25 @@ using System.ComponentModel; using Windows.Storage; using Windows.Storage.AccessCache; +using Windows.UI.Xaml; +using ModernKeePass.Common; namespace ModernKeePass.ViewModels { public class OpenVm: INotifyPropertyChanged { - public StorageFile File { get; set; } public bool ShowPasswordBox { - get { return File != null; } + get { return ((App)Application.Current).Database.Status == DatabaseHelper.DatabaseStatus.Opening; } } public string Name { - get { return File?.Name; } + get { return ((App)Application.Current).Database.Name; } } public event PropertyChangedEventHandler PropertyChanged; - private void NotifyPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); @@ -27,7 +27,8 @@ namespace ModernKeePass.ViewModels public void OpenFile(StorageFile file) { - File = file; + var database = ((App)Application.Current).Database; + database.DatabaseFile = file; NotifyPropertyChanged("Name"); NotifyPropertyChanged("ShowPasswordBox"); AddToRecentList(file); diff --git a/ModernKeePass/ViewModels/RecentVm.cs b/ModernKeePass/ViewModels/RecentVm.cs index 3208a0e..6e09966 100644 --- a/ModernKeePass/ViewModels/RecentVm.cs +++ b/ModernKeePass/ViewModels/RecentVm.cs @@ -2,6 +2,7 @@ using System.Collections.ObjectModel; using System.Linq; using Windows.Storage.AccessCache; +using Windows.UI.Xaml; using ModernKeePass.Common; namespace ModernKeePass.ViewModels @@ -36,7 +37,14 @@ namespace ModernKeePass.ViewModels } var mru = StorageApplicationPermissions.MostRecentlyUsedList; - _selectedItem.File = mru.GetFileAsync(SelectedItem.Token).GetAwaiter().GetResult(); + var database = ((App)Application.Current).Database; + try + { + database.DatabaseFile = mru.GetFileAsync(SelectedItem.Token).GetAwaiter().GetResult(); + } + catch (Exception e) + { + } } }