Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
ApplicationEvents.vb 7.03 KiB
Newer Older
' Copyright 2017 European Union.
Terry Burns's avatar
Terry Burns committed
' Licensed under the EUPL (the 'Licence');
'
' * You may not use this work except in compliance with the Licence.
' * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl
' * Unless required by applicable law or agreed to in writing,
'   software distributed under the Licence is distributed on an "AS IS" basis,
'   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
'
' See the LICENSE.txt for the specific language governing permissions and limitations.
Imports System.Collections.Generic
Imports NLog
Imports NLog.Targets
Imports NLog.Targets.Wrappers
Imports TUGraz.VectoCommon.Utils
Imports TUGraz.VectoCore.Utils
Terry Burns's avatar
Terry Burns committed

Namespace My
	' The following events are available for MyApplication:
	' 
	' Startup: Raised when the application starts even before the creation of the Startup-forms.
	' Shutdown: Raised after closing all the application forms. This event is not raised if the application terminates abnormally.
	' UnhandledException: Raised if the application encounters an unhandled exception.
	' StartupNextInstance: Raised when launching a single-instance application, and one is already active.
	' NetworkAvailabilityChanged: Occurs when connecting or disconnecting to the network.
	' ReSharper disable once ClassNeverInstantiated.Global

		Const INSTALL_SETTINGS_FILE As String = "install.ini"
		Const CONFIG_FOLDER As string = "Config"
		Const CONFIG_FILE_HISTORY_FOLDER As string = "FileHistory"
		

		Const APP_DATA_VENDOR_PATH As String = "VECTO"

		Private Sub MyApplication_Startup(sender As Object, e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) _
			Handles Me.Startup
			MyAppPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..")
			MyConfPath = path.Combine(MyAppPath, CONFIG_FOLDER)
			if (InstallModeInstalled) Then
				MyConfPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), APP_DATA_VENDOR_PATH, VectoSimulationCore.VersionNumber, CONFIG_FOLDER)
				MyLogPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), APP_DATA_VENDOR_PATH, VectoSimulationCore.VersionNumber)

				Dim logTarget As FileTarget = TryCast(LogManager.Configuration.FindTargetByName("LogFile"), FileTarget)
				if (logTarget is Nothing) Then
					logTarget = CType(TryCast(LogManager.Configuration.FindTargetByName("LogFile"), WrapperTargetBase).WrappedTarget, FileTarget)
				End If

				if (Not (logTarget Is Nothing)) then
					logTarget.FileName = Path.Combine(MyLogPath, "logs", "log.txt")
					LogManager.ReconfigExistingLoggers()
				End If
			FileHistoryPath = path.Combine(MyConfPath, CONFIG_FILE_HISTORY_FOLDER)

			

			If Not String.IsNullOrWhiteSpace(MyLogPath) AndAlso Not Directory.Exists(MyLogPath) Then
				Try
					Directory.CreateDirectory(MyLogPath)
				Catch ex As Exception
					MsgBox("Failed to create directory '" & MyLogPath & "'!", MsgBoxStyle.Critical)
					'LogFile.WriteToLog(MessageType.Err, "Failed to create directory '" & MyLogPath & "'!")
					e.Cancel = True
				End Try
			End If

			'Log
			LogFile = New FileLogger
			If Not LogFile.StartLog() Then
				MsgBox("Error! Can't access log file. Application folder needs read/write permissions!")
				e.Cancel = True
			End If
			If Not Directory.Exists(MyConfPath) Then
					Directory.CreateDirectory(MyConfPath)
				Catch ex As Exception
					MsgBox("Failed to create directory '" & MyConfPath & "'!", MsgBoxStyle.Critical)
					LogFile.WriteToLog(MessageType.Err, "Failed to create directory '" & MyConfPath & "'!")
				File.Create(path.Combine(MyConfPath, CONFIG_JOBLIST_FILE)).Close()
				File.Create(path.Combine(MyConfPath, CONFIG_CYCLELIST_FILE)).Close()
			If Not Directory.Exists(FileHistoryPath) Then
					Directory.CreateDirectory(FileHistoryPath)
						s = Directory.GetParent(Application.Info.DirectoryPath).ToString & "\"
						Dim file As StreamWriter = Computer.FileSystem.OpenTextFileWriter(Path.Combine(FileHistoryPath, FILE_HISTORY_DIR_FILE), True,
						file.WriteLine(s)
						For i = 2 To 20
							file.WriteLine(" ")
						Next
						file.Close()
					Catch ex As Exception

					End Try

				Catch ex As Exception
					MsgBox("Failed to create directory '" & FileHistoryPath & "'!", MsgBoxStyle.Critical)
					LogFile.WriteToLog(MessageType.Err, "Failed to create directory '" & FileHistoryPath & "'!")
			If Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator <> "." Then
					Threading.Thread.CurrentThread.CurrentCulture = New Globalization.CultureInfo("en-US")
					Threading.Thread.CurrentThread.CurrentUICulture = New Globalization.CultureInfo("en-US")
					'MSGtoForm(8, "Set CurrentCulture to 'en-US'", True)
				Catch ex As Exception
							"Failed to set Application Regional Settings to 'en-US'! Check system decimal- and group- separators!")
				End Try
			End If

			'Initialise Classes
			JobFileList = New List(Of String)

			Cfg = New Configuration _
			'ACHTUNG: Configuration.New löst Configuration.SetDefault aus welches sKey benötigt dehalb muss sKey schon vorher initialisiert werden!!
			Cfg.FilePath = Path.Combine(MyConfPath, "settings.json")
			ProgBarCtrl = New ProgressbarControl

			'Config
			Cfg.Load()

			'Restart log if log file too large
			LogFile.SizeCheck()
		End Sub

		Private Sub ReadInstallMode()

			if (file.Exists(path.Combine(MyAppPath, INSTALL_SETTINGS_FILE))) Then
				Try 
					Dim lines As List(Of String) = file.readlines(path.Combine(MyAppPath, INSTALL_SETTINGS_FILE)).Where(function(s1) Not(s1.RemoveWhitespace().StartsWith("#"))).toList()
					Dim installSetting as String = lines.LastOrDefault(function(s1) s1.StartsWith("ExecutionMode", StringComparison.InvariantCultureIgnoreCase))
					if (Not(string.IsNullOrWhiteSpace(installSetting))) then
						Dim parts As String() = installSetting.split("="c)
						if (parts.Length > 1 AndAlso "install".Equals(parts(1).RemoveWhitespace(), StringComparison.InvariantCultureIgnoreCase)) then
							InstallModeInstalled = True
						End If
					End If
				Catch ex As Exception
					'do nothing...
				End Try
			End If
		End Sub

		Private Sub MyApplication_UnhandledException(ByVal sender As Object,
													ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) _
			Handles Me.UnhandledException
			e.ExitApplication = True
			MsgBox("ERROR!" & ChrW(10) & ChrW(10) & e.Exception.Message.ToString, MsgBoxStyle.Critical, "Unexpected Error")
			LogFile.WriteToLog(MessageType.Err, ">>>Unexpected Error:" & e.Exception.ToString())
Terry Burns's avatar
Terry Burns committed
End Namespace