2017-07-07 15:46:32 +02:00
|
|
|
|
/*
|
2018-01-16 08:24:00 +01:00
|
|
|
|
* Copyright (c) 2018 ETH Zürich, Educational Development and Technology (LET)
|
2017-07-07 15:46:32 +02:00
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
2017-07-19 14:43:54 +02:00
|
|
|
|
using System.Collections.Generic;
|
2017-07-12 15:36:30 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Behaviour;
|
2018-01-17 14:08:39 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Configuration;
|
2017-07-28 14:52:15 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Configuration.Settings;
|
2017-07-07 15:46:32 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.I18n;
|
|
|
|
|
using SafeExamBrowser.Contracts.Logging;
|
|
|
|
|
using SafeExamBrowser.Contracts.UserInterface;
|
|
|
|
|
|
2017-07-12 15:36:30 +02:00
|
|
|
|
namespace SafeExamBrowser.Core.Behaviour
|
2017-07-07 15:46:32 +02:00
|
|
|
|
{
|
|
|
|
|
public class ShutdownController : IShutdownController
|
|
|
|
|
{
|
|
|
|
|
private ILogger logger;
|
2018-01-17 14:08:39 +01:00
|
|
|
|
private IRuntimeInfo runtimeInfo;
|
2017-07-19 14:43:54 +02:00
|
|
|
|
private ISplashScreen splashScreen;
|
2017-07-07 15:46:32 +02:00
|
|
|
|
private IText text;
|
2017-07-27 14:45:54 +02:00
|
|
|
|
private IUserInterfaceFactory uiFactory;
|
2017-07-07 15:46:32 +02:00
|
|
|
|
|
2018-01-17 14:08:39 +01:00
|
|
|
|
public ShutdownController(ILogger logger, IRuntimeInfo runtimeInfo, IText text, IUserInterfaceFactory uiFactory)
|
2017-07-07 15:46:32 +02:00
|
|
|
|
{
|
|
|
|
|
this.logger = logger;
|
2018-01-17 14:08:39 +01:00
|
|
|
|
this.runtimeInfo = runtimeInfo;
|
2017-07-07 15:46:32 +02:00
|
|
|
|
this.text = text;
|
2017-07-19 14:43:54 +02:00
|
|
|
|
this.uiFactory = uiFactory;
|
2017-07-07 15:46:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-21 12:05:31 +02:00
|
|
|
|
public void FinalizeApplication(Queue<IOperation> operations)
|
2017-07-07 15:46:32 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-07-26 14:36:20 +02:00
|
|
|
|
Initialize();
|
|
|
|
|
Revert(operations);
|
|
|
|
|
Finish();
|
2017-07-07 15:46:32 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2017-07-21 10:04:27 +02:00
|
|
|
|
LogAndShowException(e);
|
2017-07-26 14:36:20 +02:00
|
|
|
|
Finish(false);
|
2017-07-21 10:04:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-26 14:36:20 +02:00
|
|
|
|
private void Revert(Queue<IOperation> operations)
|
2017-07-21 10:04:27 +02:00
|
|
|
|
{
|
2017-07-21 12:05:31 +02:00
|
|
|
|
foreach (var operation in operations)
|
2017-07-21 10:04:27 +02:00
|
|
|
|
{
|
|
|
|
|
operation.SplashScreen = splashScreen;
|
2017-10-09 12:11:33 +02:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
operation.Revert();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
logger.Error($"Failed to revert operation '{operation.GetType().Name}'!", e);
|
|
|
|
|
}
|
2017-07-07 15:46:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-19 14:43:54 +02:00
|
|
|
|
|
2017-07-26 14:36:20 +02:00
|
|
|
|
private void Initialize()
|
2017-07-19 14:43:54 +02:00
|
|
|
|
{
|
2017-07-26 14:36:20 +02:00
|
|
|
|
logger.Log(string.Empty);
|
|
|
|
|
logger.Info("--- Initiating shutdown procedure ---");
|
|
|
|
|
|
2018-01-17 14:08:39 +01:00
|
|
|
|
splashScreen = uiFactory.CreateSplashScreen(runtimeInfo, text);
|
2017-07-21 10:04:27 +02:00
|
|
|
|
splashScreen.SetIndeterminate();
|
2017-08-03 15:35:22 +02:00
|
|
|
|
splashScreen.UpdateText(TextKey.SplashScreen_ShutdownProcedure);
|
2017-07-19 14:43:54 +02:00
|
|
|
|
splashScreen.InvokeShow();
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-21 10:04:27 +02:00
|
|
|
|
private void LogAndShowException(Exception e)
|
2017-07-19 14:43:54 +02:00
|
|
|
|
{
|
2017-07-21 10:04:27 +02:00
|
|
|
|
logger.Error($"Failed to finalize application!", e);
|
2017-08-03 15:35:22 +02:00
|
|
|
|
uiFactory.Show(text.Get(TextKey.MessageBox_ShutdownError), text.Get(TextKey.MessageBox_ShutdownErrorTitle), icon: MessageBoxIcon.Error);
|
2017-07-19 14:43:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-26 14:36:20 +02:00
|
|
|
|
private void Finish(bool success = true)
|
2017-07-19 14:43:54 +02:00
|
|
|
|
{
|
2017-07-21 10:04:27 +02:00
|
|
|
|
if (success)
|
|
|
|
|
{
|
|
|
|
|
logger.Info("--- Application successfully finalized! ---");
|
|
|
|
|
}
|
2017-07-24 15:29:17 +02:00
|
|
|
|
|
|
|
|
|
logger.Log($"{Environment.NewLine}# Application terminated at {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}");
|
2017-07-25 09:02:32 +02:00
|
|
|
|
splashScreen?.InvokeClose();
|
2017-07-19 14:43:54 +02:00
|
|
|
|
}
|
2017-07-07 15:46:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|