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-13 08:51:00 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2018-02-01 08:37:12 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Behaviour.Operations;
|
2017-08-14 17:44:16 +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;
|
|
|
|
|
|
2018-02-01 08:37:12 +01:00
|
|
|
|
namespace SafeExamBrowser.Core.Behaviour.Operations
|
2017-07-07 15:46:32 +02:00
|
|
|
|
{
|
2018-02-01 08:37:12 +01:00
|
|
|
|
public class OperationSequence : IOperationSequence
|
2017-07-07 15:46:32 +02:00
|
|
|
|
{
|
|
|
|
|
private ILogger logger;
|
2018-01-17 14:08:39 +01:00
|
|
|
|
private IRuntimeInfo runtimeInfo;
|
2017-07-07 15:46:32 +02:00
|
|
|
|
private ISplashScreen splashScreen;
|
|
|
|
|
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
|
|
|
|
|
2018-02-01 08:37:12 +01:00
|
|
|
|
public OperationSequence(ILogger logger, IRuntimeInfo runtimeInfo, IText text, IUserInterfaceFactory uiFactory)
|
2017-07-07 15:46:32 +02:00
|
|
|
|
{
|
2017-07-17 16:59:50 +02:00
|
|
|
|
this.logger = logger;
|
2018-01-17 14:08:39 +01:00
|
|
|
|
this.runtimeInfo = runtimeInfo;
|
2017-07-17 16:59:50 +02:00
|
|
|
|
this.text = text;
|
|
|
|
|
this.uiFactory = uiFactory;
|
2017-07-07 15:46:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-01 08:37:12 +01:00
|
|
|
|
public bool TryPerform(Queue<IOperation> operations)
|
2017-07-07 15:46:32 +02:00
|
|
|
|
{
|
2018-01-19 14:04:12 +01:00
|
|
|
|
var success = false;
|
|
|
|
|
|
2017-07-07 15:46:32 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2017-07-26 14:36:20 +02:00
|
|
|
|
Initialize(operations.Count);
|
2018-01-19 14:04:12 +01:00
|
|
|
|
success = Perform(operations);
|
|
|
|
|
|
|
|
|
|
if (!success)
|
|
|
|
|
{
|
|
|
|
|
RevertOperations();
|
|
|
|
|
}
|
2018-02-01 08:37:12 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
logger.Error($"Failed to perform operations!", e);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Finish();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool TryRepeat()
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool TryRevert()
|
|
|
|
|
{
|
|
|
|
|
var success = false;
|
2017-07-12 15:36:30 +02:00
|
|
|
|
|
2018-02-01 08:37:12 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Initialize();
|
|
|
|
|
success = RevertOperations(false);
|
2017-07-13 08:51:00 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-02-01 08:37:12 +01:00
|
|
|
|
logger.Error($"Failed to revert operations!", e);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Finish();
|
2017-07-13 08:51:00 +02:00
|
|
|
|
}
|
2018-01-19 14:04:12 +01:00
|
|
|
|
|
|
|
|
|
return success;
|
2017-07-13 08:51:00 +02:00
|
|
|
|
}
|
2017-07-07 15:46:32 +02:00
|
|
|
|
|
2018-02-01 08:37:12 +01:00
|
|
|
|
private void Initialize(int? operationCount = null)
|
|
|
|
|
{
|
|
|
|
|
splashScreen = uiFactory.CreateSplashScreen(runtimeInfo, text);
|
|
|
|
|
|
|
|
|
|
if (operationCount.HasValue)
|
|
|
|
|
{
|
|
|
|
|
splashScreen.SetMaxProgress(operationCount.Value);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
splashScreen.SetIndeterminate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
splashScreen.UpdateText(TextKey.SplashScreen_StartupProcedure);
|
|
|
|
|
splashScreen.Show();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-19 14:04:12 +01:00
|
|
|
|
private bool 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-21 10:04:27 +02:00
|
|
|
|
operation.SplashScreen = splashScreen;
|
2018-01-19 14:04:12 +01:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
operation.Perform();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-02-01 08:37:12 +01:00
|
|
|
|
logger.Error($"Failed to perform operation '{operation.GetType().Name}'!", e);
|
2018-01-19 14:04:12 +01:00
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-01 08:37:12 +01:00
|
|
|
|
if (operation.Abort)
|
2018-01-19 14:04:12 +01:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-07-07 15:46:32 +02:00
|
|
|
|
|
2017-07-21 10:04:27 +02:00
|
|
|
|
splashScreen.Progress();
|
|
|
|
|
}
|
2018-01-19 14:04:12 +01:00
|
|
|
|
|
|
|
|
|
return true;
|
2017-07-13 08:51:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-01 08:37:12 +01:00
|
|
|
|
private bool RevertOperations(bool regress = true)
|
2017-07-13 08:51:00 +02:00
|
|
|
|
{
|
2018-02-01 08:37:12 +01:00
|
|
|
|
var success = true;
|
|
|
|
|
|
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-10-09 12:11:33 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
operation.Revert();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
logger.Error($"Failed to revert operation '{operation.GetType().Name}'!", e);
|
2018-02-01 08:37:12 +01:00
|
|
|
|
success = false;
|
2017-10-09 12:11:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-01 08:37:12 +01:00
|
|
|
|
if (regress)
|
|
|
|
|
{
|
|
|
|
|
splashScreen.Regress();
|
|
|
|
|
}
|
2017-07-21 10:04:27 +02:00
|
|
|
|
}
|
2017-07-13 08:51:00 +02:00
|
|
|
|
|
2018-02-01 08:37:12 +01:00
|
|
|
|
return success;
|
2017-07-13 08:51:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-01 08:37:12 +01:00
|
|
|
|
private void Finish()
|
2017-07-13 08:51:00 +02:00
|
|
|
|
{
|
2018-01-30 14:41:36 +01:00
|
|
|
|
splashScreen?.Close();
|
2017-07-07 15:46:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|