2018-01-18 15:14:05 +01:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018 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;
|
|
|
|
|
using System.IO;
|
2018-03-06 11:49:51 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Behaviour.OperationModel;
|
2018-01-18 15:14:05 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Configuration;
|
|
|
|
|
using SafeExamBrowser.Contracts.Configuration.Settings;
|
|
|
|
|
using SafeExamBrowser.Contracts.I18n;
|
|
|
|
|
using SafeExamBrowser.Contracts.Logging;
|
|
|
|
|
using SafeExamBrowser.Contracts.UserInterface;
|
2018-03-14 12:07:20 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.UserInterface.MessageBox;
|
2018-01-18 15:14:05 +01:00
|
|
|
|
|
|
|
|
|
namespace SafeExamBrowser.Runtime.Behaviour.Operations
|
|
|
|
|
{
|
|
|
|
|
internal class ConfigurationOperation : IOperation
|
|
|
|
|
{
|
2018-02-06 15:12:11 +01:00
|
|
|
|
private IConfigurationRepository repository;
|
2018-01-18 15:14:05 +01:00
|
|
|
|
private ILogger logger;
|
2018-03-14 12:07:20 +01:00
|
|
|
|
private IMessageBox messageBox;
|
2018-02-15 15:42:54 +01:00
|
|
|
|
private RuntimeInfo runtimeInfo;
|
2018-01-19 09:23:09 +01:00
|
|
|
|
private IText text;
|
2018-01-18 15:14:05 +01:00
|
|
|
|
private string[] commandLineArgs;
|
|
|
|
|
|
2018-02-02 09:18:35 +01:00
|
|
|
|
public IProgressIndicator ProgressIndicator { private get; set; }
|
2018-01-18 15:14:05 +01:00
|
|
|
|
|
|
|
|
|
public ConfigurationOperation(
|
2018-02-06 15:12:11 +01:00
|
|
|
|
IConfigurationRepository repository,
|
2018-01-18 15:14:05 +01:00
|
|
|
|
ILogger logger,
|
2018-03-14 12:07:20 +01:00
|
|
|
|
IMessageBox messageBox,
|
2018-02-15 15:42:54 +01:00
|
|
|
|
RuntimeInfo runtimeInfo,
|
2018-01-19 09:23:09 +01:00
|
|
|
|
IText text,
|
2018-01-18 15:14:05 +01:00
|
|
|
|
string[] commandLineArgs)
|
|
|
|
|
{
|
2018-02-06 15:12:11 +01:00
|
|
|
|
this.repository = repository;
|
2018-01-18 15:14:05 +01:00
|
|
|
|
this.logger = logger;
|
2018-03-14 12:07:20 +01:00
|
|
|
|
this.messageBox = messageBox;
|
2018-01-18 15:14:05 +01:00
|
|
|
|
this.commandLineArgs = commandLineArgs;
|
|
|
|
|
this.runtimeInfo = runtimeInfo;
|
2018-01-19 09:23:09 +01:00
|
|
|
|
this.text = text;
|
2018-01-18 15:14:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
public OperationResult Perform()
|
2018-01-18 15:14:05 +01:00
|
|
|
|
{
|
|
|
|
|
logger.Info("Initializing application configuration...");
|
2018-02-02 09:30:41 +01:00
|
|
|
|
ProgressIndicator?.UpdateText(TextKey.ProgressIndicator_InitializeConfiguration);
|
2018-01-18 15:14:05 +01:00
|
|
|
|
|
2018-02-15 15:42:54 +01:00
|
|
|
|
Settings settings;
|
2018-01-18 15:14:05 +01:00
|
|
|
|
var isValidUri = TryGetSettingsUri(out Uri uri);
|
|
|
|
|
|
|
|
|
|
if (isValidUri)
|
|
|
|
|
{
|
|
|
|
|
logger.Info($"Loading configuration from '{uri.AbsolutePath}'...");
|
2018-02-06 15:12:11 +01:00
|
|
|
|
settings = repository.LoadSettings(uri);
|
2018-01-19 14:04:12 +01:00
|
|
|
|
|
2018-02-22 16:15:06 +01:00
|
|
|
|
if (settings.ConfigurationMode == ConfigurationMode.ConfigureClient)
|
2018-01-19 14:04:12 +01:00
|
|
|
|
{
|
2018-02-28 15:49:06 +01:00
|
|
|
|
var abort = IsConfigurationSufficient();
|
|
|
|
|
|
|
|
|
|
logger.Info($"The user chose to {(abort ? "abort" : "continue")} after successful client configuration.");
|
|
|
|
|
|
|
|
|
|
if (abort)
|
|
|
|
|
{
|
|
|
|
|
return OperationResult.Aborted;
|
|
|
|
|
}
|
2018-01-19 14:04:12 +01:00
|
|
|
|
}
|
2018-01-18 15:14:05 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Info("No valid settings file specified nor found in PROGRAMDATA or APPDATA - loading default settings...");
|
2018-02-06 15:12:11 +01:00
|
|
|
|
settings = repository.LoadDefaultSettings();
|
2018-01-18 15:14:05 +01:00
|
|
|
|
}
|
2018-02-28 15:49:06 +01:00
|
|
|
|
|
|
|
|
|
return OperationResult.Success;
|
2018-01-18 15:14:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-28 15:49:06 +01:00
|
|
|
|
public OperationResult Repeat()
|
2018-02-01 08:37:12 +01:00
|
|
|
|
{
|
2018-02-02 09:18:35 +01:00
|
|
|
|
// TODO: How will the new settings be retrieved? Uri passed to the repository? If yes, how does the Uri get here?!
|
2018-02-08 13:32:48 +01:00
|
|
|
|
// -> IDEA: Use configuration repository as container?
|
2018-02-22 16:15:06 +01:00
|
|
|
|
// -> IDEA: Introduce IRepeatParams or alike?
|
2018-02-28 15:49:06 +01:00
|
|
|
|
|
|
|
|
|
return OperationResult.Success;
|
2018-02-01 08:37:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-18 15:14:05 +01:00
|
|
|
|
public void Revert()
|
|
|
|
|
{
|
|
|
|
|
// Nothing to do here...
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool TryGetSettingsUri(out Uri uri)
|
|
|
|
|
{
|
|
|
|
|
var path = string.Empty;
|
|
|
|
|
var isValidUri = false;
|
|
|
|
|
var programDataSettings = Path.Combine(runtimeInfo.ProgramDataFolder, runtimeInfo.DefaultSettingsFileName);
|
|
|
|
|
var appDataSettings = Path.Combine(runtimeInfo.AppDataFolder, runtimeInfo.DefaultSettingsFileName);
|
|
|
|
|
|
|
|
|
|
uri = null;
|
|
|
|
|
|
|
|
|
|
if (commandLineArgs?.Length > 1)
|
|
|
|
|
{
|
|
|
|
|
path = commandLineArgs[1];
|
|
|
|
|
isValidUri = Uri.TryCreate(path, UriKind.Absolute, out uri);
|
|
|
|
|
logger.Info($"Found command-line argument for settings file: '{path}', the URI is {(isValidUri ? "valid" : "invalid")}.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isValidUri && File.Exists(programDataSettings))
|
|
|
|
|
{
|
|
|
|
|
path = programDataSettings;
|
|
|
|
|
isValidUri = Uri.TryCreate(path, UriKind.Absolute, out uri);
|
|
|
|
|
logger.Info($"Found settings file in PROGRAMDATA: '{path}', the URI is {(isValidUri ? "valid" : "invalid")}.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isValidUri && File.Exists(appDataSettings))
|
|
|
|
|
{
|
|
|
|
|
path = appDataSettings;
|
|
|
|
|
isValidUri = Uri.TryCreate(path, UriKind.Absolute, out uri);
|
|
|
|
|
logger.Info($"Found settings file in APPDATA: '{path}', the URI is {(isValidUri ? "valid" : "invalid")}.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return isValidUri;
|
|
|
|
|
}
|
2018-01-19 14:04:12 +01:00
|
|
|
|
|
2018-02-22 16:15:06 +01:00
|
|
|
|
private bool IsConfigurationSufficient()
|
2018-01-19 14:04:12 +01:00
|
|
|
|
{
|
2018-03-08 15:27:12 +01:00
|
|
|
|
var message = text.Get(TextKey.MessageBox_ClientConfigurationQuestion);
|
|
|
|
|
var title = text.Get(TextKey.MessageBox_ClientConfigurationQuestionTitle);
|
2018-03-14 12:07:20 +01:00
|
|
|
|
var abort = messageBox.Show(message, title, MessageBoxAction.YesNo, MessageBoxIcon.Question);
|
2018-01-19 14:04:12 +01:00
|
|
|
|
|
2018-01-23 15:33:54 +01:00
|
|
|
|
return abort == MessageBoxResult.Yes;
|
2018-01-19 14:04:12 +01:00
|
|
|
|
}
|
2018-01-18 15:14:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|