2017-09-11 15:13:04 +02:00
|
|
|
|
using System;
|
2018-06-19 18:47:37 +02:00
|
|
|
|
using System.Collections.Generic;
|
2017-11-23 19:02:49 +01:00
|
|
|
|
using System.Reflection;
|
2018-06-20 18:41:56 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2017-09-11 15:13:04 +02:00
|
|
|
|
using Windows.ApplicationModel;
|
|
|
|
|
using Windows.ApplicationModel.Activation;
|
2017-10-10 15:00:31 +02:00
|
|
|
|
using Windows.Storage;
|
2020-03-24 17:31:34 +01:00
|
|
|
|
using Windows.Storage.AccessCache;
|
2018-06-19 18:47:37 +02:00
|
|
|
|
using Windows.Storage.Pickers;
|
2017-09-11 15:13:04 +02:00
|
|
|
|
using Windows.UI.Xaml;
|
|
|
|
|
using Windows.UI.Xaml.Controls;
|
|
|
|
|
using Windows.UI.Xaml.Navigation;
|
2020-03-24 17:31:34 +01:00
|
|
|
|
using MediatR;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2018-06-11 18:42:50 +02:00
|
|
|
|
using Microsoft.HockeyApp;
|
2020-03-24 17:31:34 +01:00
|
|
|
|
using ModernKeePass.Application;
|
2020-04-06 20:20:45 +02:00
|
|
|
|
using ModernKeePass.Application.Common.Interfaces;
|
2020-03-24 17:31:34 +01:00
|
|
|
|
using ModernKeePass.Application.Database.Commands.CloseDatabase;
|
|
|
|
|
using ModernKeePass.Application.Database.Commands.SaveDatabase;
|
|
|
|
|
using ModernKeePass.Application.Database.Queries.GetDatabase;
|
2020-03-26 12:25:22 +01:00
|
|
|
|
using ModernKeePass.Application.Database.Queries.ReOpenDatabase;
|
2017-09-18 18:40:43 +02:00
|
|
|
|
using ModernKeePass.Common;
|
2020-04-06 20:20:45 +02:00
|
|
|
|
using ModernKeePass.Domain.Dtos;
|
2020-03-24 17:31:34 +01:00
|
|
|
|
using ModernKeePass.Domain.Exceptions;
|
|
|
|
|
using ModernKeePass.Infrastructure;
|
2017-12-08 19:38:33 +01:00
|
|
|
|
using ModernKeePass.Views;
|
2017-09-18 18:40:43 +02:00
|
|
|
|
|
2017-09-11 15:13:04 +02:00
|
|
|
|
// The Blank Application template is documented at http://go.microsoft.com/fwlink/?LinkId=234227
|
|
|
|
|
|
|
|
|
|
namespace ModernKeePass
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provides application-specific behavior to supplement the default Application class.
|
|
|
|
|
/// </summary>
|
2017-10-26 18:57:39 +02:00
|
|
|
|
sealed partial class App
|
2017-09-11 15:13:04 +02:00
|
|
|
|
{
|
2020-04-06 20:20:45 +02:00
|
|
|
|
private readonly IResourceProxy _resource;
|
|
|
|
|
private readonly IMediator _mediator;
|
|
|
|
|
private readonly ISettingsProxy _settings;
|
2020-03-24 17:31:34 +01:00
|
|
|
|
|
2020-04-06 20:20:45 +02:00
|
|
|
|
public static IServiceProvider Services { get; private set; }
|
2020-03-24 17:31:34 +01:00
|
|
|
|
|
2017-09-11 15:13:04 +02:00
|
|
|
|
/// <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().
|
|
|
|
|
/// </summary>
|
|
|
|
|
public App()
|
|
|
|
|
{
|
2018-07-24 10:55:31 +02:00
|
|
|
|
#if DEBUG
|
2020-04-08 20:02:13 +02:00
|
|
|
|
HockeyClient.Current.Configure("2fe83672-887b-4910-b9de-93a4398d0f8f");
|
2018-07-24 10:55:31 +02:00
|
|
|
|
#else
|
|
|
|
|
HockeyClient.Current.Configure("9eb5fbb79b484fbd8daf04635e975c84");
|
|
|
|
|
#endif
|
2017-10-10 15:00:31 +02:00
|
|
|
|
InitializeComponent();
|
|
|
|
|
Suspending += OnSuspending;
|
2018-03-09 18:06:06 +01:00
|
|
|
|
Resuming += OnResuming;
|
2017-11-23 19:02:49 +01:00
|
|
|
|
UnhandledException += OnUnhandledException;
|
2020-03-24 17:31:34 +01:00
|
|
|
|
|
|
|
|
|
// Setup DI
|
|
|
|
|
IServiceCollection serviceCollection = new ServiceCollection();
|
|
|
|
|
serviceCollection.AddApplication();
|
2020-04-06 20:20:45 +02:00
|
|
|
|
serviceCollection.AddInfrastructureCommon();
|
|
|
|
|
serviceCollection.AddInfrastructureKeePass();
|
|
|
|
|
serviceCollection.AddInfrastructureUwp();
|
2020-03-30 19:43:04 +02:00
|
|
|
|
serviceCollection.AddAppAutomapper();
|
2020-03-24 17:31:34 +01:00
|
|
|
|
Services = serviceCollection.BuildServiceProvider();
|
2020-04-06 20:20:45 +02:00
|
|
|
|
|
|
|
|
|
_mediator = Services.GetService<IMediator>();
|
|
|
|
|
_resource = Services.GetService<IResourceProxy>();
|
|
|
|
|
_settings = Services.GetService<ISettingsProxy>();
|
2017-09-11 15:13:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-18 10:32:51 +02:00
|
|
|
|
#region Event Handlers
|
2017-11-23 19:02:49 +01:00
|
|
|
|
|
2018-06-20 17:20:15 +02:00
|
|
|
|
private async void OnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs)
|
2017-11-23 19:02:49 +01:00
|
|
|
|
{
|
2017-11-24 12:17:41 +01:00
|
|
|
|
// Save the argument exception because it's cleared on first access
|
2017-11-23 19:02:49 +01:00
|
|
|
|
var exception = unhandledExceptionEventArgs.Exception;
|
2017-11-24 12:17:41 +01:00
|
|
|
|
var realException =
|
2017-11-23 19:02:49 +01:00
|
|
|
|
exception is TargetInvocationException &&
|
|
|
|
|
exception.InnerException != null
|
|
|
|
|
? exception.InnerException
|
2017-11-24 12:17:41 +01:00
|
|
|
|
: exception;
|
2020-03-24 17:31:34 +01:00
|
|
|
|
|
2018-01-08 18:52:03 +01:00
|
|
|
|
if (realException is SaveException)
|
|
|
|
|
{
|
2020-04-16 19:43:17 +02:00
|
|
|
|
var innerException = realException.InnerException;
|
2018-01-08 18:52:03 +01:00
|
|
|
|
unhandledExceptionEventArgs.Handled = true;
|
2020-04-16 19:43:17 +02:00
|
|
|
|
HockeyClient.Current.TrackException(innerException);
|
2020-04-06 20:20:45 +02:00
|
|
|
|
await MessageDialogHelper.ShowActionDialog(_resource.GetResourceValue("MessageDialogSaveErrorTitle"),
|
2020-04-16 19:43:17 +02:00
|
|
|
|
innerException?.Message,
|
2020-04-06 20:20:45 +02:00
|
|
|
|
_resource.GetResourceValue("MessageDialogSaveErrorButtonSaveAs"),
|
|
|
|
|
_resource.GetResourceValue("MessageDialogSaveErrorButtonDiscard"),
|
2018-06-19 18:47:37 +02:00
|
|
|
|
async command =>
|
|
|
|
|
{
|
2020-04-06 20:20:45 +02:00
|
|
|
|
var database = await _mediator.Send(new GetDatabaseQuery());
|
2018-06-19 18:47:37 +02:00
|
|
|
|
var savePicker = new FileSavePicker
|
|
|
|
|
{
|
|
|
|
|
SuggestedStartLocation = PickerLocationId.DocumentsLibrary,
|
|
|
|
|
SuggestedFileName = $"{database.Name} - copy"
|
|
|
|
|
};
|
2020-04-06 20:20:45 +02:00
|
|
|
|
savePicker.FileTypeChoices.Add(_resource.GetResourceValue("MessageDialogSaveErrorFileTypeDesc"),
|
2018-06-19 18:47:37 +02:00
|
|
|
|
new List<string> {".kdbx"});
|
|
|
|
|
|
2020-04-09 19:43:06 +02:00
|
|
|
|
var file = await savePicker.PickSaveFileAsync().AsTask();
|
2020-03-24 17:31:34 +01:00
|
|
|
|
if (file != null)
|
|
|
|
|
{
|
|
|
|
|
var token = StorageApplicationPermissions.FutureAccessList.Add(file);
|
2020-04-06 20:20:45 +02:00
|
|
|
|
await _mediator.Send(new SaveDatabaseCommand { FilePath = token });
|
2020-03-24 17:31:34 +01:00
|
|
|
|
}
|
2018-06-19 18:47:37 +02:00
|
|
|
|
}, null);
|
2018-01-08 18:52:03 +01:00
|
|
|
|
}
|
2017-11-23 19:02:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-11 15:13:04 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Invoked when the application is launched normally by the end user. Other entry points
|
|
|
|
|
/// will be used such as when the application is launched to open a specific file.
|
|
|
|
|
/// </summary>
|
2018-06-18 14:58:01 +02:00
|
|
|
|
/// <param name="args">Details about the launch request and process.</param>
|
|
|
|
|
protected override async void OnLaunched(LaunchActivatedEventArgs args)
|
2017-09-11 15:13:04 +02:00
|
|
|
|
{
|
2018-06-20 18:41:56 +02:00
|
|
|
|
await OnLaunchOrActivated(args);
|
2018-06-11 18:42:50 +02:00
|
|
|
|
await HockeyClient.Current.SendCrashesAsync(/* sendWithoutAsking: true */);
|
2017-10-17 18:46:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-20 18:41:56 +02:00
|
|
|
|
protected override async void OnActivated(IActivatedEventArgs args)
|
2017-10-17 18:46:05 +02:00
|
|
|
|
{
|
2018-06-20 18:41:56 +02:00
|
|
|
|
await OnLaunchOrActivated(args);
|
2017-10-17 18:46:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-20 18:41:56 +02:00
|
|
|
|
private async Task OnLaunchOrActivated(IActivatedEventArgs e)
|
2017-10-17 18:46:05 +02:00
|
|
|
|
{
|
2017-10-10 15:00:31 +02:00
|
|
|
|
var rootFrame = Window.Current.Content as Frame;
|
2017-09-11 15:13:04 +02:00
|
|
|
|
|
|
|
|
|
// Do not repeat app initialization when the Window already has content,
|
|
|
|
|
// just ensure that the window is active
|
|
|
|
|
if (rootFrame == null)
|
|
|
|
|
{
|
|
|
|
|
// Create a Frame to act as the navigation context and navigate to the first page
|
2017-10-19 15:53:03 +02:00
|
|
|
|
rootFrame = new Frame {Language = Windows.Globalization.ApplicationLanguages.Languages[0]};
|
2017-09-11 15:13:04 +02:00
|
|
|
|
// Set the default language
|
|
|
|
|
|
|
|
|
|
rootFrame.NavigationFailed += OnNavigationFailed;
|
|
|
|
|
|
|
|
|
|
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
|
|
|
|
|
{
|
2018-06-21 11:13:40 +02:00
|
|
|
|
// Load state from previously terminated application
|
|
|
|
|
await SuspensionManager.RestoreAsync();
|
2018-03-09 18:06:06 +01:00
|
|
|
|
#if DEBUG
|
2018-06-20 17:20:15 +02:00
|
|
|
|
await MessageDialogHelper.ShowNotificationDialog("App terminated", "Windows or an error made the app terminate");
|
2018-03-09 18:06:06 +01:00
|
|
|
|
#endif
|
2017-09-11 15:13:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Place the frame in the current Window
|
|
|
|
|
Window.Current.Content = rootFrame;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-26 12:25:22 +01:00
|
|
|
|
var launchActivatedEventArgs = e as LaunchActivatedEventArgs;
|
|
|
|
|
if (launchActivatedEventArgs != null && rootFrame.Content == null)
|
|
|
|
|
rootFrame.Navigate(typeof(MainPage), launchActivatedEventArgs.Arguments);
|
2018-06-19 18:47:37 +02:00
|
|
|
|
|
2017-09-11 15:13:04 +02:00
|
|
|
|
// Ensure the current window is active
|
|
|
|
|
Window.Current.Activate();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-24 17:31:34 +01:00
|
|
|
|
private async void OnResuming(object sender, object e)
|
2018-03-09 18:06:06 +01:00
|
|
|
|
{
|
|
|
|
|
var currentFrame = Window.Current.Content as Frame;
|
2018-06-21 16:40:04 +02:00
|
|
|
|
|
|
|
|
|
try
|
2018-03-09 18:06:06 +01:00
|
|
|
|
{
|
2020-04-06 20:20:45 +02:00
|
|
|
|
await _mediator.Send(new ReOpenDatabaseQuery());
|
2018-03-09 18:06:06 +01:00
|
|
|
|
#if DEBUG
|
2020-04-02 19:12:16 +02:00
|
|
|
|
ToastNotificationHelper.ShowGenericToast("App resumed", "Database reopened (changes were saved)");
|
2018-03-09 18:06:06 +01:00
|
|
|
|
#endif
|
|
|
|
|
}
|
2018-06-21 16:40:04 +02:00
|
|
|
|
catch (Exception)
|
2018-03-09 18:06:06 +01:00
|
|
|
|
{
|
|
|
|
|
currentFrame?.Navigate(typeof(MainPage));
|
|
|
|
|
#if DEBUG
|
2018-06-21 16:40:04 +02:00
|
|
|
|
ToastNotificationHelper.ShowGenericToast("App resumed", "Nothing to do, no previous database opened");
|
2018-03-09 18:06:06 +01:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-11 15:13:04 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Invoked when Navigation to a certain page fails
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">The Frame which failed navigation</param>
|
|
|
|
|
/// <param name="e">Details about the navigation failure</param>
|
2018-06-21 16:40:04 +02:00
|
|
|
|
private void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
|
2017-09-11 15:13:04 +02:00
|
|
|
|
{
|
2018-06-21 11:13:40 +02:00
|
|
|
|
throw new NavigationException(e.SourcePageType);
|
2017-09-11 15:13:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Invoked when application execution is being suspended. Application state is saved
|
|
|
|
|
/// without knowing whether the application will be terminated or resumed with the contents
|
|
|
|
|
/// of memory still intact.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">The source of the suspend request.</param>
|
|
|
|
|
/// <param name="e">Details about the suspend request.</param>
|
2018-06-21 11:13:40 +02:00
|
|
|
|
private async void OnSuspending(object sender, SuspendingEventArgs e)
|
2017-09-11 15:13:04 +02:00
|
|
|
|
{
|
|
|
|
|
var deferral = e.SuspendingOperation.GetDeferral();
|
2018-03-12 12:45:12 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
2020-04-14 13:44:07 +02:00
|
|
|
|
var database = await _mediator.Send(new GetDatabaseQuery());
|
|
|
|
|
if (database.IsOpen)
|
2020-03-24 17:31:34 +01:00
|
|
|
|
{
|
2020-04-14 13:44:07 +02:00
|
|
|
|
if (database.Size < Constants.File.OneMegaByte && database.IsDirty &&
|
|
|
|
|
_settings.GetSetting(Constants.Settings.SaveSuspend, true))
|
|
|
|
|
{
|
|
|
|
|
await _mediator.Send(new SaveDatabaseCommand()).ConfigureAwait(false);
|
|
|
|
|
}
|
2020-04-09 19:43:06 +02:00
|
|
|
|
|
2020-04-14 13:44:07 +02:00
|
|
|
|
await _mediator.Send(new CloseDatabaseCommand()).ConfigureAwait(false);
|
|
|
|
|
}
|
2018-03-12 12:45:12 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception exception)
|
|
|
|
|
{
|
|
|
|
|
ToastNotificationHelper.ShowErrorToast(exception);
|
|
|
|
|
}
|
2020-04-09 19:43:06 +02:00
|
|
|
|
await SuspensionManager.SaveAsync().ConfigureAwait(false);
|
2017-09-11 15:13:04 +02:00
|
|
|
|
deferral.Complete();
|
|
|
|
|
}
|
2017-10-10 18:55:16 +02:00
|
|
|
|
|
2017-10-18 10:32:51 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Invoked when application is launched from opening a file in Windows Explorer
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="args">Details about the file being opened</param>
|
2017-10-10 15:00:31 +02:00
|
|
|
|
protected override void OnFileActivated(FileActivatedEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
base.OnFileActivated(args);
|
|
|
|
|
var rootFrame = new Frame();
|
2018-06-19 18:47:37 +02:00
|
|
|
|
var file = args.Files[0] as StorageFile;
|
2020-04-06 20:20:45 +02:00
|
|
|
|
|
|
|
|
|
if (file != null)
|
|
|
|
|
{
|
|
|
|
|
var token = StorageApplicationPermissions.FutureAccessList.Add(file);
|
|
|
|
|
var fileInfo = new FileInfo
|
|
|
|
|
{
|
2020-04-07 12:48:18 +02:00
|
|
|
|
Id = token,
|
|
|
|
|
Name = file.DisplayName,
|
|
|
|
|
Path = file.Path
|
2020-04-06 20:20:45 +02:00
|
|
|
|
};
|
|
|
|
|
rootFrame.Navigate(typeof(MainPage), fileInfo);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
rootFrame.Navigate(typeof(MainPage));
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-10 15:00:31 +02:00
|
|
|
|
Window.Current.Content = rootFrame;
|
2017-10-11 18:43:27 +02:00
|
|
|
|
Window.Current.Activate();
|
2017-10-10 15:00:31 +02:00
|
|
|
|
}
|
2017-11-23 19:02:49 +01:00
|
|
|
|
|
2017-10-18 10:32:51 +02:00
|
|
|
|
#endregion
|
2017-09-11 15:13:04 +02:00
|
|
|
|
}
|
|
|
|
|
}
|