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;
|
|
|
|
|
using SafeExamBrowser.Contracts.Behaviour;
|
|
|
|
|
using SafeExamBrowser.Contracts.Configuration;
|
|
|
|
|
using SafeExamBrowser.Contracts.Configuration.Settings;
|
|
|
|
|
using SafeExamBrowser.Contracts.I18n;
|
|
|
|
|
using SafeExamBrowser.Contracts.Logging;
|
|
|
|
|
using SafeExamBrowser.Contracts.UserInterface;
|
|
|
|
|
|
|
|
|
|
namespace SafeExamBrowser.Runtime.Behaviour.Operations
|
|
|
|
|
{
|
|
|
|
|
internal class ConfigurationOperation : IOperation
|
|
|
|
|
{
|
|
|
|
|
private ILogger logger;
|
|
|
|
|
private IRuntimeInfo runtimeInfo;
|
|
|
|
|
private ISettingsRepository repository;
|
2018-01-19 09:23:09 +01:00
|
|
|
|
private IText text;
|
|
|
|
|
private IUserInterfaceFactory uiFactory;
|
2018-01-18 15:14:05 +01:00
|
|
|
|
private string[] commandLineArgs;
|
|
|
|
|
|
2018-01-19 14:04:12 +01:00
|
|
|
|
public bool AbortStartup { get; private set; }
|
2018-01-18 15:14:05 +01:00
|
|
|
|
public ISplashScreen SplashScreen { private get; set; }
|
|
|
|
|
|
|
|
|
|
public ConfigurationOperation(
|
|
|
|
|
ILogger logger,
|
|
|
|
|
IRuntimeInfo runtimeInfo,
|
|
|
|
|
ISettingsRepository repository,
|
2018-01-19 09:23:09 +01:00
|
|
|
|
IText text,
|
|
|
|
|
IUserInterfaceFactory uiFactory,
|
2018-01-18 15:14:05 +01:00
|
|
|
|
string[] commandLineArgs)
|
|
|
|
|
{
|
|
|
|
|
this.logger = logger;
|
|
|
|
|
this.commandLineArgs = commandLineArgs;
|
|
|
|
|
this.repository = repository;
|
|
|
|
|
this.runtimeInfo = runtimeInfo;
|
2018-01-19 09:23:09 +01:00
|
|
|
|
this.text = text;
|
|
|
|
|
this.uiFactory = uiFactory;
|
2018-01-18 15:14:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Perform()
|
|
|
|
|
{
|
|
|
|
|
logger.Info("Initializing application configuration...");
|
|
|
|
|
SplashScreen.UpdateText(TextKey.SplashScreen_InitializeConfiguration);
|
|
|
|
|
|
2018-01-19 09:23:09 +01:00
|
|
|
|
ISettings 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-01-19 09:23:09 +01:00
|
|
|
|
settings = repository.Load(uri);
|
2018-01-19 14:04:12 +01:00
|
|
|
|
|
2018-01-23 15:33:54 +01:00
|
|
|
|
if (settings.ConfigurationMode == ConfigurationMode.ConfigureClient && UserWantsToAbortStartup())
|
2018-01-19 14:04:12 +01:00
|
|
|
|
{
|
|
|
|
|
AbortStartup = true;
|
2018-01-23 15:33:54 +01:00
|
|
|
|
logger.Info($"The user chose to {(AbortStartup ? "abort" : "continue")} the application startup after successful client configuration.");
|
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-01-19 09:23:09 +01:00
|
|
|
|
settings = repository.LoadDefaults();
|
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-01-23 15:33:54 +01:00
|
|
|
|
private bool UserWantsToAbortStartup()
|
2018-01-19 14:04:12 +01:00
|
|
|
|
{
|
|
|
|
|
var message = text.Get(TextKey.MessageBox_ConfigureClientSuccess);
|
|
|
|
|
var title = text.Get(TextKey.MessageBox_ConfigureClientSuccessTitle);
|
2018-01-23 15:33:54 +01:00
|
|
|
|
var abort = uiFactory.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
|
|
|
|
}
|
|
|
|
|
}
|