2017-09-11 15:13:04 +02:00
|
|
|
|
using System;
|
2017-11-23 19:02:49 +01:00
|
|
|
|
using System.Reflection;
|
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;
|
2017-09-11 15:13:04 +02:00
|
|
|
|
using Windows.UI.Xaml;
|
|
|
|
|
using Windows.UI.Xaml.Controls;
|
|
|
|
|
using Windows.UI.Xaml.Navigation;
|
2018-06-11 18:42:50 +02:00
|
|
|
|
using Microsoft.HockeyApp;
|
2017-09-18 18:40:43 +02:00
|
|
|
|
using ModernKeePass.Common;
|
2017-11-24 12:17:41 +01:00
|
|
|
|
using ModernKeePass.Exceptions;
|
2017-11-29 19:13:38 +01:00
|
|
|
|
using ModernKeePass.Services;
|
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
|
|
|
|
{
|
|
|
|
|
/// <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-06-11 18:55:55 +02:00
|
|
|
|
HockeyClient.Current.Configure("2fe83672887b4910b9de93a4398d0f8f");
|
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;
|
2017-09-11 15:13:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-09 18:06:06 +01:00
|
|
|
|
|
2017-10-18 10:32:51 +02:00
|
|
|
|
#region Event Handlers
|
2017-11-23 19:02:49 +01:00
|
|
|
|
|
|
|
|
|
private void OnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs)
|
|
|
|
|
{
|
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;
|
|
|
|
|
|
2018-01-08 18:52:03 +01:00
|
|
|
|
if (realException is SaveException)
|
|
|
|
|
{
|
|
|
|
|
unhandledExceptionEventArgs.Handled = true;
|
2018-02-23 18:09:21 +01:00
|
|
|
|
MessageDialogHelper.SaveErrorDialog(realException as SaveException, DatabaseService.Instance);
|
2018-01-08 18:52:03 +01:00
|
|
|
|
}
|
|
|
|
|
else if (realException is DatabaseOpenedException)
|
|
|
|
|
{
|
|
|
|
|
unhandledExceptionEventArgs.Handled = true;
|
2018-02-23 18:09:21 +01:00
|
|
|
|
MessageDialogHelper.SaveUnchangedDialog(realException as DatabaseOpenedException, DatabaseService.Instance);
|
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>
|
|
|
|
|
/// <param name="e">Details about the launch request and process.</param>
|
2018-06-11 18:42:50 +02:00
|
|
|
|
protected override async void OnLaunched(LaunchActivatedEventArgs e)
|
2017-09-11 15:13:04 +02:00
|
|
|
|
{
|
2017-10-17 18:46:05 +02:00
|
|
|
|
OnLaunchOrActivated(e);
|
2018-06-11 18:42:50 +02:00
|
|
|
|
await HockeyClient.Current.SendCrashesAsync(/* sendWithoutAsking: true */);
|
2017-10-17 18:46:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnActivated(IActivatedEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
OnLaunchOrActivated(args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnLaunchOrActivated(IActivatedEventArgs e)
|
|
|
|
|
{
|
2017-09-11 15:13:04 +02:00
|
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
|
if (System.Diagnostics.Debugger.IsAttached)
|
|
|
|
|
{
|
2017-10-27 12:33:35 +02:00
|
|
|
|
//DebugSettings.EnableFrameRateCounter = true;
|
2017-09-11 15:13:04 +02:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
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-03-09 18:06:06 +01:00
|
|
|
|
//TODO: Load state from previously terminated application
|
|
|
|
|
#if DEBUG
|
|
|
|
|
MessageDialogHelper.ShowNotificationDialog("App terminated", "Windows or an error made the app terminate");
|
|
|
|
|
#endif
|
2017-09-11 15:13:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Place the frame in the current Window
|
|
|
|
|
Window.Current.Content = rootFrame;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-17 18:46:05 +02:00
|
|
|
|
if (e is LaunchActivatedEventArgs)
|
|
|
|
|
{
|
2017-10-19 15:53:03 +02:00
|
|
|
|
var lauchActivatedEventArgs = (LaunchActivatedEventArgs) e;
|
2017-10-17 18:46:05 +02:00
|
|
|
|
if (rootFrame.Content == null)
|
|
|
|
|
{
|
|
|
|
|
// When the navigation stack isn't restored navigate to the first page,
|
|
|
|
|
// configuring the new page by passing required information as a navigation
|
|
|
|
|
// parameter
|
2017-12-08 19:38:33 +01:00
|
|
|
|
rootFrame.Navigate(typeof(MainPage), lauchActivatedEventArgs.Arguments);
|
2017-10-17 18:46:05 +02:00
|
|
|
|
}
|
2017-10-31 18:49:18 +01:00
|
|
|
|
/*else
|
2017-10-18 10:32:51 +02:00
|
|
|
|
{
|
|
|
|
|
// App is "launched" via the Toast Activation event
|
|
|
|
|
UndoEntityDelete(lauchActivatedEventArgs.Arguments);
|
2017-10-31 18:49:18 +01:00
|
|
|
|
}*/
|
2017-10-17 18:46:05 +02:00
|
|
|
|
}
|
2017-10-18 10:32:51 +02:00
|
|
|
|
// This is only available on Windows 10...
|
|
|
|
|
/*else if (e is ToastNotificationActivatedEventArgs)
|
2017-09-11 15:13:04 +02:00
|
|
|
|
{
|
2017-10-18 10:32:51 +02:00
|
|
|
|
var toastActivationArgs = e as ToastNotificationActivatedEventArgs;
|
2017-10-17 18:46:05 +02:00
|
|
|
|
|
|
|
|
|
// Parse the query string (using QueryString.NET)
|
2017-10-18 10:32:51 +02:00
|
|
|
|
UndoEntityDelete(QueryString.Parse(toastActivationArgs.Argument));
|
|
|
|
|
}*/
|
2017-09-11 15:13:04 +02:00
|
|
|
|
// Ensure the current window is active
|
|
|
|
|
Window.Current.Activate();
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-09 18:06:06 +01:00
|
|
|
|
private async void OnResuming(object sender, object e)
|
|
|
|
|
{
|
|
|
|
|
var currentFrame = Window.Current.Content as Frame;
|
|
|
|
|
var database = DatabaseService.Instance;
|
|
|
|
|
if (database.DatabaseFile == null)
|
|
|
|
|
{
|
|
|
|
|
#if DEBUG
|
|
|
|
|
ToastNotificationHelper.ShowGenericToast("App suspended", "Nothing to do, no previous database opened");
|
|
|
|
|
#endif
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (database.CompositeKey != null) await database.ReOpen();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
currentFrame?.Navigate(typeof(MainPage));
|
|
|
|
|
#if DEBUG
|
|
|
|
|
MessageDialogHelper.ShowErrorDialog(ex);
|
|
|
|
|
#endif
|
|
|
|
|
ToastNotificationHelper.ShowGenericToast("App suspended", "Database was closed (changes were saved)");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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>
|
|
|
|
|
void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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-01-09 18:40:11 +01:00
|
|
|
|
private async void OnSuspending(object sender, SuspendingEventArgs e)
|
2017-09-11 15:13:04 +02:00
|
|
|
|
{
|
|
|
|
|
var deferral = e.SuspendingOperation.GetDeferral();
|
2018-02-23 18:09:21 +01:00
|
|
|
|
var database = DatabaseService.Instance;
|
2018-03-12 12:45:12 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (SettingsService.Instance.GetSetting("SaveSuspend", true)) database.Save();
|
|
|
|
|
await database.Close(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception exception)
|
|
|
|
|
{
|
|
|
|
|
ToastNotificationHelper.ShowErrorToast(exception);
|
|
|
|
|
}
|
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-02-23 18:09:21 +01:00
|
|
|
|
DatabaseService.Instance.DatabaseFile = args.Files[0] as StorageFile;
|
2017-12-08 19:38:33 +01:00
|
|
|
|
rootFrame.Navigate(typeof(MainPage), args);
|
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
|
|
|
|
}
|
|
|
|
|
}
|