2017-10-25 18:29:50 +02:00
|
|
|
|
using System;
|
2017-11-24 12:17:41 +01:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Windows.Storage.Pickers;
|
2017-10-25 18:29:50 +02:00
|
|
|
|
using Windows.UI.Popups;
|
2017-11-24 12:17:41 +01:00
|
|
|
|
using ModernKeePass.Exceptions;
|
|
|
|
|
using ModernKeePass.Interfaces;
|
2017-10-25 18:29:50 +02:00
|
|
|
|
|
2017-12-01 17:59:38 +01:00
|
|
|
|
namespace ModernKeePass.Common
|
2017-10-25 18:29:50 +02:00
|
|
|
|
{
|
2017-12-01 17:59:38 +01:00
|
|
|
|
public static class MessageDialogHelper
|
2017-10-25 18:29:50 +02:00
|
|
|
|
{
|
2018-01-08 18:52:03 +01:00
|
|
|
|
// TODO: include resources
|
|
|
|
|
public static async void ShowActionDialog(string title, string contentText, string actionButtonText, string cancelButtonText, UICommandInvokedHandler actionCommand, UICommandInvokedHandler cancelCommand)
|
2017-10-25 18:29:50 +02:00
|
|
|
|
{
|
|
|
|
|
// Create the message dialog and set its content
|
2018-01-09 18:40:11 +01:00
|
|
|
|
var messageDialog = CreateBasicDialog(title, contentText, cancelButtonText, cancelCommand);
|
2017-10-25 18:29:50 +02:00
|
|
|
|
|
|
|
|
|
// Add commands and set their callbacks; both buttons use the same callback function instead of inline event handlers
|
2018-01-08 18:52:03 +01:00
|
|
|
|
messageDialog.Commands.Add(new UICommand(actionButtonText, actionCommand));
|
2017-12-08 19:38:33 +01:00
|
|
|
|
|
2017-10-25 18:29:50 +02:00
|
|
|
|
// Show the message dialog
|
|
|
|
|
await messageDialog.ShowAsync();
|
|
|
|
|
}
|
2017-11-13 11:28:14 +01:00
|
|
|
|
|
2018-02-23 18:09:21 +01:00
|
|
|
|
public static void SaveErrorDialog(SaveException exception, IDatabaseService database)
|
2017-11-24 12:17:41 +01:00
|
|
|
|
{
|
|
|
|
|
ShowActionDialog("Save error", exception.InnerException.Message, "Save as", "Discard", async command =>
|
|
|
|
|
{
|
|
|
|
|
var savePicker = new FileSavePicker
|
|
|
|
|
{
|
|
|
|
|
SuggestedStartLocation = PickerLocationId.DocumentsLibrary,
|
|
|
|
|
SuggestedFileName = $"{database.DatabaseFile.DisplayName} - copy"
|
|
|
|
|
};
|
|
|
|
|
savePicker.FileTypeChoices.Add("KeePass 2.x database", new List<string> { ".kdbx" });
|
|
|
|
|
|
|
|
|
|
var file = await savePicker.PickSaveFileAsync();
|
|
|
|
|
if (file != null) database.Save(file);
|
2018-01-08 18:52:03 +01:00
|
|
|
|
}, null);
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-23 18:09:21 +01:00
|
|
|
|
public static void SaveUnchangedDialog(DatabaseOpenedException exception, IDatabaseService database)
|
2018-01-08 18:52:03 +01:00
|
|
|
|
{
|
|
|
|
|
ShowActionDialog("Opened database", $"Database {database.Name} is currently opened. What to you wish to do?", "Save changes", "Discard", command =>
|
|
|
|
|
{
|
|
|
|
|
database.Save();
|
|
|
|
|
database.Close();
|
|
|
|
|
},
|
|
|
|
|
command =>
|
|
|
|
|
{
|
|
|
|
|
database.Close();
|
2017-11-24 12:17:41 +01:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-13 11:28:14 +01:00
|
|
|
|
public static async void ShowErrorDialog(Exception exception)
|
|
|
|
|
{
|
2017-11-23 19:02:49 +01:00
|
|
|
|
if (exception == null) return;
|
2017-11-13 11:28:14 +01:00
|
|
|
|
// Create the message dialog and set its content
|
2018-03-09 18:06:06 +01:00
|
|
|
|
var messageDialog = CreateBasicDialog(exception.Message, exception.StackTrace, "OK");
|
2017-11-13 11:28:14 +01:00
|
|
|
|
|
|
|
|
|
// Show the message dialog
|
|
|
|
|
await messageDialog.ShowAsync();
|
|
|
|
|
}
|
2017-12-08 19:38:33 +01:00
|
|
|
|
|
|
|
|
|
public static async void ShowNotificationDialog(string title, string message)
|
|
|
|
|
{
|
|
|
|
|
var dialog = CreateBasicDialog(title, message, "OK");
|
|
|
|
|
|
|
|
|
|
// Show the message dialog
|
|
|
|
|
await dialog.ShowAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-09 18:40:11 +01:00
|
|
|
|
private static MessageDialog CreateBasicDialog(string title, string message, string dismissActionText, UICommandInvokedHandler cancelCommand = null)
|
2017-12-08 19:38:33 +01:00
|
|
|
|
{
|
|
|
|
|
// Create the message dialog and set its content
|
|
|
|
|
var messageDialog = new MessageDialog(message, title);
|
|
|
|
|
|
|
|
|
|
// Add commands and set their callbacks;
|
2018-01-09 18:40:11 +01:00
|
|
|
|
messageDialog.Commands.Add(new UICommand(dismissActionText, cancelCommand));
|
2017-12-08 19:38:33 +01:00
|
|
|
|
|
|
|
|
|
// Set the command that will be invoked by default
|
|
|
|
|
messageDialog.DefaultCommandIndex = 1;
|
|
|
|
|
|
|
|
|
|
// Set the command to be invoked when escape is pressed
|
|
|
|
|
messageDialog.CancelCommandIndex = 1;
|
|
|
|
|
|
|
|
|
|
return messageDialog;
|
|
|
|
|
}
|
2017-10-25 18:29:50 +02:00
|
|
|
|
}
|
|
|
|
|
}
|