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-13 08:51:00 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2017-07-12 15:36:30 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Behaviour;
|
2017-08-14 17:44:16 +02: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 StartupController : IStartupController
|
|
|
|
|
{
|
|
|
|
|
private ILogger logger;
|
2017-07-10 15:47:12 +02:00
|
|
|
|
private ISettings settings;
|
2017-07-07 15:46:32 +02:00
|
|
|
|
private ISplashScreen splashScreen;
|
2017-08-14 17:44:16 +02:00
|
|
|
|
private ISystemInfo systemInfo;
|
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
|
|
|
|
|
2017-07-21 12:05:31 +02:00
|
|
|
|
private Stack<IOperation> stack = new Stack<IOperation>();
|
2017-07-13 08:51:00 +02:00
|
|
|
|
|
2017-08-14 17:44:16 +02:00
|
|
|
|
public StartupController(ILogger logger, ISettings settings, ISystemInfo systemInfo, IText text, IUserInterfaceFactory uiFactory)
|
2017-07-07 15:46:32 +02:00
|
|
|
|
{
|
2017-07-17 16:59:50 +02:00
|
|
|
|
this.logger = logger;
|
|
|
|
|
this.settings = settings;
|
2017-08-14 17:44:16 +02:00
|
|
|
|
this.systemInfo = systemInfo;
|
2017-07-17 16:59:50 +02:00
|
|
|
|
this.text = text;
|
|
|
|
|
this.uiFactory = uiFactory;
|
2017-07-07 15:46:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-21 12:05:31 +02:00
|
|
|
|
public bool TryInitializeApplication(Queue<IOperation> operations)
|
2017-07-07 15:46:32 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-07-26 14:36:20 +02:00
|
|
|
|
Initialize(operations.Count);
|
2017-07-26 08:50:36 +02:00
|
|
|
|
Perform(operations);
|
2017-07-26 14:36:20 +02:00
|
|
|
|
Finish();
|
2017-07-12 15:36:30 +02:00
|
|
|
|
|
2017-07-13 08:51:00 +02:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2017-07-21 10:04:27 +02:00
|
|
|
|
LogAndShowException(e);
|
2017-07-21 12:05:31 +02:00
|
|
|
|
RevertOperations();
|
2017-07-26 14:36:20 +02:00
|
|
|
|
Finish(false);
|
2017-07-12 15:36:30 +02:00
|
|
|
|
|
2017-07-13 08:51:00 +02:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-07 15:46:32 +02:00
|
|
|
|
|
2017-07-26 08:50:36 +02:00
|
|
|
|
private void Perform(Queue<IOperation> operations)
|
2017-07-13 08:51:00 +02:00
|
|
|
|
{
|
2017-07-21 12:05:31 +02:00
|
|
|
|
foreach (var operation in operations)
|
2017-07-21 10:04:27 +02:00
|
|
|
|
{
|
2017-07-21 12:05:31 +02:00
|
|
|
|
stack.Push(operation);
|
2017-07-07 15:46:32 +02:00
|
|
|
|
|
2017-07-21 10:04:27 +02:00
|
|
|
|
operation.SplashScreen = splashScreen;
|
|
|
|
|
operation.Perform();
|
2017-07-07 15:46:32 +02:00
|
|
|
|
|
2017-07-21 10:04:27 +02:00
|
|
|
|
splashScreen.Progress();
|
|
|
|
|
}
|
2017-07-13 08:51:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-21 12:05:31 +02:00
|
|
|
|
private void RevertOperations()
|
2017-07-13 08:51:00 +02:00
|
|
|
|
{
|
2017-07-21 12:05:31 +02:00
|
|
|
|
while (stack.Any())
|
2017-07-21 10:04:27 +02:00
|
|
|
|
{
|
2017-07-21 12:05:31 +02:00
|
|
|
|
var operation = stack.Pop();
|
2017-07-13 08:51:00 +02:00
|
|
|
|
|
2017-07-21 10:04:27 +02:00
|
|
|
|
operation.Revert();
|
|
|
|
|
splashScreen.Regress();
|
|
|
|
|
}
|
2017-07-13 08:51:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-26 14:36:20 +02:00
|
|
|
|
private void Initialize(int operationCount)
|
2017-07-13 08:51:00 +02:00
|
|
|
|
{
|
2017-07-21 10:04:27 +02:00
|
|
|
|
var titleLine = $"/* {settings.ProgramTitle}, Version {settings.ProgramVersion}{Environment.NewLine}";
|
|
|
|
|
var copyrightLine = $"/* {settings.ProgramCopyright}{Environment.NewLine}";
|
|
|
|
|
var emptyLine = $"/* {Environment.NewLine}";
|
|
|
|
|
var githubLine = $"/* Please visit https://github.com/SafeExamBrowser for more information.";
|
2017-07-17 16:59:50 +02:00
|
|
|
|
|
2017-07-21 10:04:27 +02:00
|
|
|
|
logger.Log($"{titleLine}{copyrightLine}{emptyLine}{githubLine}");
|
2017-08-17 14:00:43 +02:00
|
|
|
|
logger.Log(string.Empty);
|
|
|
|
|
logger.Log($"# Application started at {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}");
|
|
|
|
|
logger.Log($"# Running on {systemInfo.OperatingSystemInfo}");
|
|
|
|
|
logger.Log(string.Empty);
|
|
|
|
|
|
2017-07-21 10:04:27 +02:00
|
|
|
|
logger.Info("--- Initiating startup procedure ---");
|
2017-07-13 08:51:00 +02:00
|
|
|
|
|
2017-07-21 10:04:27 +02:00
|
|
|
|
splashScreen = uiFactory.CreateSplashScreen(settings, text);
|
2017-07-21 12:05:31 +02:00
|
|
|
|
splashScreen.SetMaxProgress(operationCount);
|
2017-08-03 15:35:22 +02:00
|
|
|
|
splashScreen.UpdateText(TextKey.SplashScreen_StartupProcedure);
|
2017-07-21 10:04:27 +02:00
|
|
|
|
splashScreen.InvokeShow();
|
|
|
|
|
}
|
2017-07-17 08:28:18 +02:00
|
|
|
|
|
2017-07-21 10:04:27 +02:00
|
|
|
|
private void LogAndShowException(Exception e)
|
|
|
|
|
{
|
|
|
|
|
logger.Error($"Failed to initialize application!", e);
|
2017-08-03 15:35:22 +02:00
|
|
|
|
uiFactory.Show(text.Get(TextKey.MessageBox_StartupError), text.Get(TextKey.MessageBox_StartupErrorTitle), icon: MessageBoxIcon.Error);
|
2017-07-21 10:04:27 +02:00
|
|
|
|
logger.Info("Reverting operations...");
|
2017-07-13 08:51:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-26 14:36:20 +02:00
|
|
|
|
private void Finish(bool success = true)
|
2017-07-13 08:51:00 +02:00
|
|
|
|
{
|
2017-07-21 10:04:27 +02:00
|
|
|
|
if (success)
|
|
|
|
|
{
|
|
|
|
|
logger.Info("--- Application successfully initialized! ---");
|
2017-07-26 14:36:20 +02:00
|
|
|
|
logger.Log(string.Empty);
|
2017-07-25 09:02:32 +02:00
|
|
|
|
splashScreen?.InvokeClose();
|
2017-07-21 10:04:27 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Log($"{Environment.NewLine}# Application terminated at {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}");
|
|
|
|
|
}
|
2017-07-07 15:46:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|