diff --git a/ModernKeePass/Common/DatabaseHelper.cs b/ModernKeePass/Common/DatabaseHelper.cs index a65b4d2..9afae3d 100644 --- a/ModernKeePass/Common/DatabaseHelper.cs +++ b/ModernKeePass/Common/DatabaseHelper.cs @@ -14,6 +14,9 @@ namespace ModernKeePass.Common { public enum DatabaseStatus { + Error = -3, + NoCompositeKey = -2, + CompositeKeyError = -1, Closed = 0, Opening = 1, Opened = 2 @@ -77,11 +80,11 @@ namespace ModernKeePass.Common /// The database composite key /// True to create a new database before opening it /// An error message, if any - public string Open(CompositeKey key, bool createNew = false) + public void Open(CompositeKey key, bool createNew = false) { try { - if (key == null) return "No composite key"; + if (key == null) Status = DatabaseStatus.NoCompositeKey; var ioConnection = IOConnectionInfo.FromFile(DatabaseFile); if (createNew) _pwDatabase.New(ioConnection, key); else _pwDatabase.Open(ioConnection, key, new NullStatusLogger()); @@ -92,19 +95,14 @@ namespace ModernKeePass.Common RootGroup = new GroupVm(_pwDatabase.RootGroup, null, RecycleBinEnabled ? _pwDatabase.RecycleBinUuid : null); } } - catch (ArgumentNullException) - { - return "Password cannot be empty"; - } catch (InvalidCompositeKeyException) { - return "Wrong password"; + Status = DatabaseStatus.CompositeKeyError; } catch (Exception ex) { - return ex.Message; + Status = DatabaseStatus.Error; } - return string.Empty; } /// @@ -121,11 +119,19 @@ namespace ModernKeePass.Common /// /// Commit the changes to the currently opened database to file /// - public void Save() + public bool Save() { - // TODO: Save is disabled for now for Argon2Kdf because it corrupts DB (read works) - if (_pwDatabase == null || !_pwDatabase.IsOpen /*|| KdfPool.Get(KeyDerivation.KdfUuid) is Argon2Kdf*/) return; - _pwDatabase.Save(new NullStatusLogger()); + if (_pwDatabase == null || !_pwDatabase.IsOpen) return false; + try + { + _pwDatabase.Save(new NullStatusLogger()); + return true; + } + catch (Exception ex) + { + MessageDialogHelper.ShowErrorDialog(ex); + } + return false; } /// diff --git a/ModernKeePass/Common/MessageDialogHelper.cs b/ModernKeePass/Common/MessageDialogHelper.cs index 0ad69af..233bc9c 100644 --- a/ModernKeePass/Common/MessageDialogHelper.cs +++ b/ModernKeePass/Common/MessageDialogHelper.cs @@ -23,5 +23,17 @@ namespace ModernKeePass.Common // Show the message dialog await messageDialog.ShowAsync(); } + + public static async void ShowErrorDialog(Exception exception) + { + // Create the message dialog and set its content + var messageDialog = new MessageDialog(exception.Message, "Error occured"); + + // Add commands and set their callbacks; both buttons use the same callback function instead of inline event handlers + messageDialog.Commands.Add(new UICommand("OK")); + + // Show the message dialog + await messageDialog.ShowAsync(); + } } } diff --git a/ModernKeePass/Controls/CompositeKeyUserControl.xaml b/ModernKeePass/Controls/CompositeKeyUserControl.xaml index 5fd5667..ebe555b 100644 --- a/ModernKeePass/Controls/CompositeKeyUserControl.xaml +++ b/ModernKeePass/Controls/CompositeKeyUserControl.xaml @@ -47,7 +47,7 @@ Foreground="{Binding PasswordComplexityIndicator, ConverterParameter=128, Converter={StaticResource DoubleToForegroungBrushConverter}}" Visibility="{Binding ShowComplexityIndicator, ElementName=UserControl, Converter={StaticResource BooleanToVisibilityConverter}}" /> - +