Re-enabled write mode in KeePassLib

Set database as part of App (so, Singleton)
Changed main page view model
WIP and testing saving mechanism
This commit is contained in:
2017-09-18 18:40:43 +02:00
parent 717ef693f8
commit 00897307a4
14 changed files with 200 additions and 174 deletions

View File

@@ -1,20 +1,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using ModernKeePass.Common;
// The Blank Application template is documented at http://go.microsoft.com/fwlink/?LinkId=234227
namespace ModernKeePass
@@ -24,6 +16,7 @@ namespace ModernKeePass
/// </summary>
sealed partial class App : Application
{
public DatabaseHelper Database { get; set; }
/// <summary>
/// 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().

View File

@@ -0,0 +1,59 @@
using System;
using Windows.Storage;
using System.Threading.Tasks;
using ModernKeePass.ViewModels;
using ModernKeePassLib;
using ModernKeePassLib.Interfaces;
using ModernKeePassLib.Keys;
using ModernKeePassLib.Serialization;
namespace ModernKeePass.Common
{
public class DatabaseHelper
{
private PwDatabase _pwDatabase = new PwDatabase();
private StorageFile databaseFile;
public GroupVm RootGroup { get; set; }
public bool IsOpen { get; set; }
public string Name { get; set; }
public DatabaseHelper(StorageFile databaseFile)
{
this.databaseFile = databaseFile;
}
public async Task<string> Open(string password)
{
var key = new CompositeKey();
try
{
key.AddUserKey(new KcpPassword(password));
await _pwDatabase.Open(IOConnectionInfo.FromFile(databaseFile), key, new NullStatusLogger());
IsOpen = _pwDatabase.IsOpen;
Name = databaseFile.DisplayName;
RootGroup = new GroupVm(_pwDatabase.RootGroup);
}
catch (ArgumentNullException)
{
return "Password cannot be empty";
}
catch (InvalidCompositeKeyException)
{
return "Wrong password";
}
/*finally
{
// TODO: move this when implementing write mode
_pwDatabase.Close();
}*/
return string.Empty;
}
public void Save()
{
_pwDatabase.Save(new NullStatusLogger());
_pwDatabase.Close();
}
}
}

View File

@@ -9,7 +9,7 @@
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" >
<Page.DataContext>
<ViewModels:DatabaseVm/>
<ViewModels:HomeVm/>
</Page.DataContext>
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
@@ -31,11 +31,13 @@
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0" x:Name="passwordTextBlock" HorizontalAlignment="Left" TextWrapping="Wrap" Text="Password" VerticalAlignment="Center" Height="auto" Width="auto" FontSize="16" Margin="10,7,0,6" />
<PasswordBox Grid.Column="1" Grid.Row="0" x:Name="passwordBox" VerticalAlignment="Top" HorizontalAlignment="Right" Width="130" Password="{Binding Password, Mode=TwoWay}" IsPasswordRevealButtonEnabled="True" Margin="0,0,10,0"/>
<Button Grid.Column="1" Grid.Row="1" x:Name="openBbutton" Content="Open" HorizontalAlignment="Right" VerticalAlignment="Top" Click="openBbutton_Click" Margin="0,0,7,0" Width="auto"/>
<TextBlock Grid.Column="1" Grid.Row="2" x:Name="errorTextBlock" TextWrapping="Wrap" Text="{Binding ErrorMessage}" HorizontalAlignment="Right" Height="auto" Width="auto" Foreground="#FF9E0909" Margin="0,0,10,0"/>
</Grid>
<Button Grid.Column="1" Grid.Row="3" x:Name="saveBbutton" Content="Save" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,0,7,0" Width="auto" Click="saveBbutton_Click"/>
</Grid>
</Page>

View File

@@ -1,6 +1,6 @@
using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using ModernKeePass.Pages;
using ModernKeePass.ViewModels;
@@ -19,7 +19,7 @@ namespace ModernKeePass
this.InitializeComponent();
}
private async void Button_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
private async void Button_Click(object sender, RoutedEventArgs e)
{
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.List;
@@ -31,19 +31,31 @@ namespace ModernKeePass
if (file != null)
{
// Application now has read/write access to the picked file
DataContext = new DatabaseVm(file);
}
else
{
//DataContext = new DatabaseVm(file);
((App)Application.Current).Database = new Common.DatabaseHelper(file);
var homeVm = DataContext as HomeVm;
homeVm.Visibility = Visibility.Visible;
homeVm.NotifyPropertyChanged("Visibility");
}
}
private void openBbutton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
private async void openBbutton_Click(object sender, RoutedEventArgs e)
{
var database = DataContext as DatabaseVm;
/*var database = DataContext as DatabaseVm;
database.Open();
if (database.IsOpen)
Frame.Navigate(typeof(GroupDetailPage), database.RootGroup);
Frame.Navigate(typeof(GroupDetailPage), database.RootGroup);*/
var homeVm = DataContext as HomeVm;
var app = ((App)Application.Current);
homeVm.ErrorMessage = await app.Database.Open(homeVm.Password);
if (!app.Database.IsOpen) homeVm.NotifyPropertyChanged("ErrorMessage");
else Frame.Navigate(typeof(GroupDetailPage), app.Database.RootGroup);
}
private void saveBbutton_Click(object sender, RoutedEventArgs e)
{
var database = DataContext as HomeVm;
((App)Application.Current).Database.Save();
}
}
}

View File

@@ -110,6 +110,7 @@
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
</Compile>
<Compile Include="Common\DatabaseHelper.cs" />
<Compile Include="Common\NavigationHelper.cs" />
<Compile Include="Common\ObservableDictionary.cs" />
<Compile Include="Common\RelayCommand.cs" />
@@ -124,7 +125,7 @@
<DependentUpon>GroupDetailPage.xaml</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ViewModels\DatabaseVm.cs" />
<Compile Include="ViewModels\HomeVm.cs" />
<Compile Include="ViewModels\EntryVm.cs" />
<Compile Include="ViewModels\GroupVm.cs" />
</ItemGroup>

View File

@@ -77,6 +77,8 @@
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button x:Name="addGroupButton" VerticalAlignment="Bottom" HorizontalAlignment="Right" Click="addGroupButton_Click" Content="Add Group" />
</StackPanel>
</GridView.Header>
<GridView.ItemContainerStyle>

View File

@@ -89,5 +89,11 @@ namespace ModernKeePass.Pages
var listView = sender as ListView;
Frame.Navigate(typeof(EntryDetailPage), listView.SelectedItem as EntryVm);
}
private void addGroupButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
var group = DataContext as GroupVm;
group.AddGroup("New group");
}
}
}

View File

@@ -1,69 +0,0 @@
using System.ComponentModel;
using Windows.Storage;
using ModernKeePassLib;
using ModernKeePassLib.Keys;
using ModernKeePassLib.Serialization;
using ModernKeePassLib.Interfaces;
using Windows.UI.Xaml;
using System;
namespace ModernKeePass.ViewModels
{
public class DatabaseVm : INotifyPropertyChanged
{
private PwDatabase database = new PwDatabase();
private StorageFile databaseFile;
public string Password { get; set; }
public bool IsOpen { get; set; }
public Visibility Visibility { get; private set; }
public string ErrorMessage { get; set; }
public string Name { get; set; }
public GroupVm RootGroup { get; set; }
public DatabaseVm()
{
Visibility = Visibility.Collapsed;
}
public DatabaseVm(StorageFile databaseFile)
{
this.databaseFile = databaseFile;
Visibility = Visibility.Visible;
}
public async void Open()
{
var key = new CompositeKey();
try
{
key.AddUserKey(new KcpPassword(Password));
await database.Open(IOConnectionInfo.FromFile(databaseFile), key, new NullStatusLogger());
IsOpen = database.IsOpen;
Name = databaseFile.DisplayName;
RootGroup = new GroupVm (database.RootGroup);
}
catch (ArgumentNullException)
{
ErrorMessage = "Password cannot be empty";
NotifyPropertyChanged("ErrorMessage");
}
catch (InvalidCompositeKeyException)
{
ErrorMessage = "Wrong password";
NotifyPropertyChanged("ErrorMessage");
}
finally
{
// TODO: move this when implementing write mode
database.Close();
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}

View File

@@ -7,6 +7,8 @@ namespace ModernKeePass.ViewModels
{
public class GroupVm : INotifyPropertyChanged
{
private PwGroup _pwGroup;
public ObservableCollection<EntryVm> Entries { get; set; }
public ObservableCollection<GroupVm> Groups { get; set; }
public string Name { get; set; }
@@ -35,11 +37,23 @@ namespace ModernKeePass.ViewModels
public GroupVm(PwGroup group)
{
_pwGroup = group;
Name = group.Name;
Entries = new ObservableCollection<EntryVm>(group.Entries.Select(e => new EntryVm(e)));
Groups = new ObservableCollection<GroupVm>(group.Groups.Select(g => new GroupVm(g)));
}
public void AddGroup(string title)
{
var pwGroup = new PwGroup
{
Name = title
};
Groups.Add(new GroupVm(pwGroup));
NotifyPropertyChanged("Groups");
this._pwGroup.Groups.Add(pwGroup);
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)

View File

@@ -0,0 +1,30 @@
using System.ComponentModel;
using Windows.Storage;
using ModernKeePassLib;
using ModernKeePassLib.Keys;
using ModernKeePassLib.Serialization;
using ModernKeePassLib.Interfaces;
using Windows.UI.Xaml;
using System;
namespace ModernKeePass.ViewModels
{
public class HomeVm : INotifyPropertyChanged
{
public string Password { get; set; }
public Visibility Visibility { get; set; }
public string ErrorMessage { get; set; }
public HomeVm()
{
Visibility = Visibility.Collapsed;
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}