2019-07-16 14:39:59 +02:00
/ *
2024-03-05 18:37:42 +01:00
* Copyright ( c ) 2024 ETH Zürich , IT Services
2019-07-16 14:39:59 +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 ;
2019-07-17 16:17:20 +02:00
using System.Collections.Generic ;
2019-07-16 14:39:59 +02:00
using System.IO ;
2019-07-17 16:17:20 +02:00
using SafeExamBrowser.Lockdown ;
2019-09-06 09:39:28 +02:00
using SafeExamBrowser.Lockdown.Contracts ;
2019-07-16 14:39:59 +02:00
using SafeExamBrowser.Logging ;
2019-09-06 09:39:28 +02:00
using SafeExamBrowser.Logging.Contracts ;
2019-07-16 14:39:59 +02:00
using SafeExamBrowser.ResetUtility.Procedure ;
2019-09-06 09:39:28 +02:00
using SafeExamBrowser.Settings.Logging ;
2019-07-19 10:07:45 +02:00
using SafeExamBrowser.SystemComponents ;
2019-07-16 14:39:59 +02:00
namespace SafeExamBrowser.ResetUtility
{
internal class CompositionRoot
{
2019-07-19 10:07:45 +02:00
private ProcedureContext context ;
2019-07-16 14:39:59 +02:00
private ILogger logger ;
internal ProcedureStep InitialStep { get ; private set ; }
internal NativeMethods NativeMethods { get ; private set ; }
internal void BuildObjectGraph ( )
{
InitializeLogging ( ) ;
2019-07-19 10:07:45 +02:00
InitializeContext ( ) ;
2019-07-16 14:39:59 +02:00
InitialStep = new Initialization ( context ) ;
NativeMethods = new NativeMethods ( ) ;
}
internal void LogStartupInformation ( )
{
logger . Log ( $"# Application started at {DateTime.Now.ToString(" yyyy - MM - dd HH : mm : ss . fff ")}" ) ;
logger . Log ( string . Empty ) ;
}
internal void LogShutdownInformation ( )
{
logger . Log ( string . Empty ) ;
logger ? . Log ( $"# Application terminated at {DateTime.Now.ToString(" yyyy - MM - dd HH : mm : ss . fff ")}" ) ;
}
2019-07-18 10:36:41 +02:00
private IList < MainMenuOption > BuildMainMenu ( ProcedureContext context )
2019-07-17 16:17:20 +02:00
{
return new List < MainMenuOption >
{
new MainMenuOption { IsSelected = true , NextStep = new Restore ( context ) , Result = ProcedureStepResult . Continue , Text = "Restore system configuration via backup mechanism" } ,
new MainMenuOption { NextStep = new Reset ( context ) , Result = ProcedureStepResult . Continue , Text = "Reset system configuration to default values" } ,
new MainMenuOption { NextStep = new Procedure . Version ( context ) , Result = ProcedureStepResult . Continue , Text = "Show version information" } ,
new MainMenuOption { NextStep = new Log ( context ) , Result = ProcedureStepResult . Continue , Text = "Show application log" } ,
new MainMenuOption { Result = ProcedureStepResult . Terminate , Text = "Exit" }
} ;
}
private IFeatureConfigurationBackup CreateBackup ( string filePath )
{
return new FeatureConfigurationBackup ( filePath , new ModuleLogger ( logger , nameof ( FeatureConfigurationBackup ) ) ) ;
}
2019-07-19 10:07:45 +02:00
private void InitializeContext ( )
{
context = new ProcedureContext ( ) ;
context . ConfigurationFactory = new FeatureConfigurationFactory ( new ModuleLogger ( logger , nameof ( FeatureConfigurationFactory ) ) ) ;
context . CreateBackup = CreateBackup ;
context . Logger = logger ;
context . MainMenu = BuildMainMenu ( context ) ;
context . Update = new SystemConfigurationUpdate ( new ModuleLogger ( logger , nameof ( SystemConfigurationUpdate ) ) ) ;
context . UserInfo = new UserInfo ( new ModuleLogger ( logger , nameof ( UserInfo ) ) ) ;
}
2019-07-16 14:39:59 +02:00
private void InitializeLogging ( )
{
var appDataFolder = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . LocalApplicationData ) , nameof ( SafeExamBrowser ) ) ;
var logFolder = Path . Combine ( appDataFolder , "Logs" ) ;
var logFilePrefix = DateTime . Now . ToString ( "yyyy-MM-dd\\_HH\\hmm\\mss\\s" ) ;
var logFilePath = Path . Combine ( logFolder , $"{logFilePrefix}_{nameof(ResetUtility)}.log" ) ;
var logFileWriter = new LogFileWriter ( new DefaultLogFormatter ( ) , logFilePath ) ;
logger = new Logger ( ) ;
logger . LogLevel = LogLevel . Debug ;
logger . Subscribe ( logFileWriter ) ;
logFileWriter . Initialize ( ) ;
}
}
}