2017-07-07 15:46:32 +02:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2017 ETH Zürich, Educational Development and Technology (LET)
|
|
|
|
|
*
|
|
|
|
|
* 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;
|
|
|
|
|
using System.Threading;
|
2017-07-12 15:36:30 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Behaviour;
|
2017-07-19 14:43:54 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Configuration;
|
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;
|
2017-07-19 14:43:54 +02:00
|
|
|
|
private ISettings settings;
|
|
|
|
|
private ISplashScreen splashScreen;
|
2017-07-07 15:46:32 +02:00
|
|
|
|
private IText text;
|
2017-07-19 14:43:54 +02:00
|
|
|
|
private IUiElementFactory uiFactory;
|
2017-07-07 15:46:32 +02:00
|
|
|
|
|
2017-07-24 15:29:17 +02:00
|
|
|
|
public ShutdownController(ILogger logger, ISettings settings, IText text, IUiElementFactory uiFactory)
|
2017-07-07 15:46:32 +02:00
|
|
|
|
{
|
|
|
|
|
this.logger = logger;
|
2017-07-19 14:43:54 +02:00
|
|
|
|
this.settings = settings;
|
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-19 14:43:54 +02:00
|
|
|
|
InitializeSplashScreen();
|
2017-07-21 10:04:27 +02:00
|
|
|
|
RevertOperations(operations);
|
|
|
|
|
FinalizeApplicationLog();
|
2017-07-07 15:46:32 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2017-07-21 10:04:27 +02:00
|
|
|
|
LogAndShowException(e);
|
|
|
|
|
FinalizeApplicationLog(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-21 12:05:31 +02:00
|
|
|
|
private void RevertOperations(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;
|
|
|
|
|
operation.Revert();
|
|
|
|
|
|
|
|
|
|
// TODO: Remove!
|
|
|
|
|
Thread.Sleep(250);
|
2017-07-07 15:46:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-19 14:43:54 +02:00
|
|
|
|
|
|
|
|
|
private void InitializeSplashScreen()
|
|
|
|
|
{
|
|
|
|
|
splashScreen = uiFactory.CreateSplashScreen(settings, text);
|
2017-07-21 10:04:27 +02:00
|
|
|
|
splashScreen.SetIndeterminate();
|
2017-07-19 14:43:54 +02:00
|
|
|
|
splashScreen.UpdateText(Key.SplashScreen_ShutdownProcedure);
|
|
|
|
|
splashScreen.InvokeShow();
|
2017-07-20 14:16:47 +02:00
|
|
|
|
logger.Info("--- Initiating shutdown procedure ---");
|
2017-07-19 14:43:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
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-07-24 15:29:17 +02:00
|
|
|
|
uiFactory.Show(text.Get(Key.MessageBox_ShutdownError), text.Get(Key.MessageBox_ShutdownErrorTitle), icon: MessageBoxIcon.Error);
|
2017-07-19 14:43:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-21 10:04:27 +02:00
|
|
|
|
private void FinalizeApplicationLog(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-19 14:43:54 +02:00
|
|
|
|
}
|
2017-07-07 15:46:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|