diff --git a/ModernKeePass/App.xaml.cs b/ModernKeePass/App.xaml.cs index 7d0121f..d905fbc 100644 --- a/ModernKeePass/App.xaml.cs +++ b/ModernKeePass/App.xaml.cs @@ -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 /// sealed partial class App : Application { + public DatabaseHelper Database { get; set; } /// /// 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(). diff --git a/ModernKeePass/Common/DatabaseHelper.cs b/ModernKeePass/Common/DatabaseHelper.cs new file mode 100644 index 0000000..6ab1877 --- /dev/null +++ b/ModernKeePass/Common/DatabaseHelper.cs @@ -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 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(); + } + } +} diff --git a/ModernKeePass/MainPage.xaml b/ModernKeePass/MainPage.xaml index bc4d1f4..dc194d8 100644 --- a/ModernKeePass/MainPage.xaml +++ b/ModernKeePass/MainPage.xaml @@ -9,7 +9,7 @@ mc:Ignorable="d" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" > - + @@ -31,11 +31,13 @@ +