WIP New database (and open and recent...)

This commit is contained in:
2017-10-11 18:43:27 +02:00
committed by BONNEVILLE Geoffroy
parent 97b1475100
commit 2f1355104e
14 changed files with 185 additions and 25 deletions

View File

@@ -20,6 +20,7 @@ namespace ModernKeePass
sealed partial class App : Application sealed partial class App : Application
{ {
public DatabaseHelper Database { get; set; } = new DatabaseHelper(); public DatabaseHelper Database { get; set; } = new DatabaseHelper();
/// <summary> /// <summary>
/// Initializes the singleton application object. This is the first line of authored code /// 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(). /// executed, and as such is the logical equivalent of main() or WinMain().
@@ -106,11 +107,11 @@ namespace ModernKeePass
protected override void OnFileActivated(FileActivatedEventArgs args) protected override void OnFileActivated(FileActivatedEventArgs args)
{ {
base.OnFileActivated(args); base.OnFileActivated(args);
/*Database.DatabaseFile = args.Files[0] as StorageFile;
var rootFrame = new Frame(); var rootFrame = new Frame();
Database.DatabaseFile = args.Files[0] as StorageFile;
rootFrame.Navigate(typeof(MainPage), args); rootFrame.Navigate(typeof(MainPage), args);
Window.Current.Content = rootFrame; Window.Current.Content = rootFrame;
Window.Current.Activate();*/ Window.Current.Activate();
} }
} }
} }

View File

@@ -16,23 +16,38 @@ namespace ModernKeePass.Common
Opening = 1, Opening = 1,
Opened = 2 Opened = 2
} }
private readonly PwDatabase _pwDatabase = new PwDatabase(); private PwDatabase _pwDatabase = new PwDatabase();
private StorageFile _databaseFile;
public GroupVm RootGroup { get; set; } public GroupVm RootGroup { get; set; }
public DatabaseStatus Status { get; private set; } = DatabaseStatus.Closed; public DatabaseStatus Status { get; private set; } = DatabaseStatus.Closed;
public string Name => _pwDatabase?.Name;
public string Name { get; private set; } public StorageFile DatabaseFile
public string Open(StorageFile databaseFile, string password) {
get { return _databaseFile; }
set
{
_databaseFile = value;
Status = DatabaseStatus.Opening;
}
}
public string Open(string password, bool createNew = false)
{ {
var key = new CompositeKey(); var key = new CompositeKey();
try try
{ {
key.AddUserKey(new KcpPassword(password)); 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) if (_pwDatabase.IsOpen)
{ {
Name = databaseFile.Name;
Status = DatabaseStatus.Opened; Status = DatabaseStatus.Opened;
RootGroup = new GroupVm(_pwDatabase.RootGroup, null); RootGroup = new GroupVm(_pwDatabase.RootGroup, null);
} }
@@ -51,7 +66,7 @@ namespace ModernKeePass.Common
} }
return string.Empty; return string.Empty;
} }
public void Save() public void Save()
{ {
if (_pwDatabase != null && _pwDatabase.IsOpen) if (_pwDatabase != null && _pwDatabase.IsOpen)

View File

@@ -14,15 +14,15 @@ namespace ModernKeePass.Controls
{ {
public sealed partial class OpenDatabaseUserControl : UserControl public sealed partial class OpenDatabaseUserControl : UserControl
{ {
public StorageFile DatabaseFile public bool CreateNew
{ {
get { return (StorageFile)GetValue(DatabaseFileProperty); } get { return (bool)GetValue(CreateNewProperty); }
set { SetValue(DatabaseFileProperty, value); } set { SetValue(CreateNewProperty, value); }
} }
public static readonly DependencyProperty DatabaseFileProperty = public static readonly DependencyProperty CreateNewProperty =
DependencyProperty.Register( DependencyProperty.Register(
"DatabaseFile", "CreateNew",
typeof(StorageFile), typeof(bool),
typeof(OpenDatabaseUserControl), typeof(OpenDatabaseUserControl),
new PropertyMetadata(null, (o, args) => { })); new PropertyMetadata(null, (o, args) => { }));
@@ -37,7 +37,7 @@ namespace ModernKeePass.Controls
private void OpenButton_OnClick(object sender, RoutedEventArgs e) private void OpenButton_OnClick(object sender, RoutedEventArgs e)
{ {
var app = (App)Application.Current; 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) if (app.Database.Status == DatabaseHelper.DatabaseStatus.Opened)
ValidationChecked?.Invoke(this, new PasswordEventArgs(app.Database.RootGroup)); ValidationChecked?.Invoke(this, new PasswordEventArgs(app.Database.RootGroup));
} }

View File

@@ -132,6 +132,9 @@
<DependentUpon>MainPage.xaml</DependentUpon> <DependentUpon>MainPage.xaml</DependentUpon>
</Compile> </Compile>
<Compile Include="Mappings\PwIconToSegoeMapping.cs" /> <Compile Include="Mappings\PwIconToSegoeMapping.cs" />
<Compile Include="Pages\NewDatabasePage.xaml.cs">
<DependentUpon>NewDatabasePage.xaml</DependentUpon>
</Compile>
<Compile Include="ViewModels\Items\MainMenuItemVm.cs" /> <Compile Include="ViewModels\Items\MainMenuItemVm.cs" />
<Compile Include="ViewModels\Items\RecentItemVm.cs" /> <Compile Include="ViewModels\Items\RecentItemVm.cs" />
<Compile Include="Pages\EntryDetailPage.xaml.cs"> <Compile Include="Pages\EntryDetailPage.xaml.cs">
@@ -153,6 +156,7 @@
<Compile Include="ViewModels\EntryVm.cs" /> <Compile Include="ViewModels\EntryVm.cs" />
<Compile Include="ViewModels\GroupVm.cs" /> <Compile Include="ViewModels\GroupVm.cs" />
<Compile Include="ViewModels\MainVm.cs" /> <Compile Include="ViewModels\MainVm.cs" />
<Compile Include="ViewModels\NewVm.cs" />
<Compile Include="ViewModels\OpenVm.cs" /> <Compile Include="ViewModels\OpenVm.cs" />
<Compile Include="ViewModels\RecentVm.cs" /> <Compile Include="ViewModels\RecentVm.cs" />
<Compile Include="ViewModels\SaveVm.cs" /> <Compile Include="ViewModels\SaveVm.cs" />
@@ -191,6 +195,10 @@
<SubType>Designer</SubType> <SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
</Page> </Page>
<Page Include="Pages\NewDatabasePage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Pages\OpenDatabasePage.xaml"> <Page Include="Pages\OpenDatabasePage.xaml">
<SubType>Designer</SubType> <SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>

View File

@@ -37,6 +37,13 @@
</SupportedFileTypes> </SupportedFileTypes>
</FileTypeAssociation> </FileTypeAssociation>
</Extension> </Extension>
<Extension Category="windows.fileSavePicker">
<FileSavePicker>
<SupportedFileTypes>
<FileType>.kdbx</FileType>
</SupportedFileTypes>
</FileSavePicker>
</Extension>
</Extensions> </Extensions>
</Application> </Application>
</Applications> </Applications>

View File

@@ -0,0 +1,23 @@
<Page
x:Class="ModernKeePass.Pages.NewDatabasePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="using:ModernKeePass.Controls"
xmlns:converters="using:ModernKeePass.Converters"
xmlns:viewModels="using:ModernKeePass.ViewModels"
mc:Ignorable="d">
<Page.Resources>
<converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</Page.Resources>
<Page.DataContext>
<viewModels:OpenVm />
</Page.DataContext>
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<HyperlinkButton Content="Create new..." Click="ButtonBase_OnClick" />
<TextBlock TextWrapping="Wrap" Text="{Binding Name}" Height="auto" Width="auto" FontSize="16" Margin="10,7,0,6" />
<controls:OpenDatabaseUserControl CreateNew="True" Visibility="{Binding ShowPasswordBox, Converter={StaticResource BooleanToVisibilityConverter}}" ValidationChecked="PasswordUserControl_PasswordChecked" />
</StackPanel>
</Page>

View File

@@ -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
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
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<string> { ".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);
}
}
}

View File

@@ -19,6 +19,6 @@
<HyperlinkButton Content="Browse files..." Click="ButtonBase_OnClick" /> <HyperlinkButton Content="Browse files..." Click="ButtonBase_OnClick" />
<HyperlinkButton Content="From Url..." IsEnabled="False" /> <HyperlinkButton Content="From Url..." IsEnabled="False" />
<TextBlock TextWrapping="Wrap" Text="{Binding Name}" Height="auto" Width="auto" FontSize="16" Margin="10,7,0,6" /> <TextBlock TextWrapping="Wrap" Text="{Binding Name}" Height="auto" Width="auto" FontSize="16" Margin="10,7,0,6" />
<local:OpenDatabaseUserControl DatabaseFile="{Binding File}" Visibility="{Binding ShowPasswordBox, Converter={StaticResource BooleanToVisibilityConverter}}" ValidationChecked="PasswordUserControl_PasswordChecked" /> <local:OpenDatabaseUserControl Visibility="{Binding ShowPasswordBox, Converter={StaticResource BooleanToVisibilityConverter}}" ValidationChecked="PasswordUserControl_PasswordChecked" />
</StackPanel> </StackPanel>
</Page> </Page>

View File

@@ -23,7 +23,7 @@
<DataTemplate> <DataTemplate>
<StackPanel Margin="10,0,10,0"> <StackPanel Margin="10,0,10,0">
<TextBlock Text="{Binding Name}" Width="350" Padding="5" /> <TextBlock Text="{Binding Name}" Width="350" Padding="5" />
<local:OpenDatabaseUserControl DatabaseFile="{Binding File}" Visibility="{Binding IsSelected, Converter={StaticResource BooleanToVisibilityConverter}}" ValidationChecked="PasswordUserControl_PasswordChecked" /> <local:OpenDatabaseUserControl Visibility="{Binding IsSelected, Converter={StaticResource BooleanToVisibilityConverter}}" ValidationChecked="PasswordUserControl_PasswordChecked" />
</StackPanel> </StackPanel>
</DataTemplate> </DataTemplate>
</ListView.ItemTemplate> </ListView.ItemTemplate>

View File

@@ -8,7 +8,6 @@ namespace ModernKeePass.ViewModels
private bool _isSelected; private bool _isSelected;
public string Token { get; set; } public string Token { get; set; }
public string Name { get; set; } public string Name { get; set; }
public StorageFile File { get; set; }
public bool IsSelected public bool IsSelected
{ {

View File

@@ -53,7 +53,10 @@ namespace ModernKeePass.ViewModels
Title = "Open", PageType = typeof(OpenDatabasePage), Destination = destinationFrame, Parameter = referenceFrame, SymbolIcon = Symbol.Page2, Title = "Open", PageType = typeof(OpenDatabasePage), Destination = destinationFrame, Parameter = referenceFrame, SymbolIcon = Symbol.Page2,
IsSelected = app.Database.Status == DatabaseHelper.DatabaseStatus.Opening 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 new MainMenuItemVm
{ {
Title = "Save" , PageType = typeof(SaveDatabasePage), Destination = destinationFrame, Parameter = referenceFrame, SymbolIcon = Symbol.Save, Title = "Save" , PageType = typeof(SaveDatabasePage), Destination = destinationFrame, Parameter = referenceFrame, SymbolIcon = Symbol.Save,

View File

@@ -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);
}
}
}

View File

@@ -1,25 +1,25 @@
using System.ComponentModel; using System.ComponentModel;
using Windows.Storage; using Windows.Storage;
using Windows.Storage.AccessCache; using Windows.Storage.AccessCache;
using Windows.UI.Xaml;
using ModernKeePass.Common;
namespace ModernKeePass.ViewModels namespace ModernKeePass.ViewModels
{ {
public class OpenVm: INotifyPropertyChanged public class OpenVm: INotifyPropertyChanged
{ {
public StorageFile File { get; set; }
public bool ShowPasswordBox public bool ShowPasswordBox
{ {
get { return File != null; } get { return ((App)Application.Current).Database.Status == DatabaseHelper.DatabaseStatus.Opening; }
} }
public string Name public string Name
{ {
get { return File?.Name; } get { return ((App)Application.Current).Database.Name; }
} }
public event PropertyChangedEventHandler PropertyChanged; public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName) private void NotifyPropertyChanged(string propertyName)
{ {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
@@ -27,7 +27,8 @@ namespace ModernKeePass.ViewModels
public void OpenFile(StorageFile file) public void OpenFile(StorageFile file)
{ {
File = file; var database = ((App)Application.Current).Database;
database.DatabaseFile = file;
NotifyPropertyChanged("Name"); NotifyPropertyChanged("Name");
NotifyPropertyChanged("ShowPasswordBox"); NotifyPropertyChanged("ShowPasswordBox");
AddToRecentList(file); AddToRecentList(file);

View File

@@ -2,6 +2,7 @@
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using Windows.Storage.AccessCache; using Windows.Storage.AccessCache;
using Windows.UI.Xaml;
using ModernKeePass.Common; using ModernKeePass.Common;
namespace ModernKeePass.ViewModels namespace ModernKeePass.ViewModels
@@ -36,7 +37,14 @@ namespace ModernKeePass.ViewModels
} }
var mru = StorageApplicationPermissions.MostRecentlyUsedList; 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)
{
}
} }
} }