TaskFlow/app/DD_PM_WINDREAM/ModuleMySettings.vb
2019-04-16 14:01:35 +02:00

230 lines
9.7 KiB
VB.net

Imports System.IO
Imports System.Xml
Module ModuleMySettings
Public Const USER_CONFIG_FILE = "UserConfig.xml"
Public Const COMPUTER_CONFIG_FILE = "ComputerConfig.xml"
Public LOG_ERRORS_ONLY As Boolean = True
Public CONNECTION_STRING As String = ""
' Viewer Settings
Public VIEWER_UNIVERSAL As String = ""
Public VIEWER_XCHANGE As String = ""
Public VIEWER_SUMATRA As String = ""
Public VIEWER_ZOOM_LEVEL As Integer = 3
Public VIEWER_PDF As String = "internal"
Public VIEWER_ALL As String = "docview"
Public INDEX_DMS_ERSTELLT = "DMS erstellt"
Public INDEX_DMS_ERSTELLT_ZEIT = "DMS erstellt (Zeit)"
Public USER_MANAGER_PATH = ""
' These settings are loaded from the database
Public VERSION_DELIMITER As String = "~"
Public FILE_DELIMITER As String = "_"
Public WMSESSION_STARTSTOP_STARTUP As Boolean = False
Public Function GetUserConfigPath() As String
Return Path.Combine(Application.UserAppDataPath(), USER_CONFIG_FILE)
End Function
Public Function GetAllUsersConfigPath() As String
Return Path.Combine(Application.CommonAppDataPath(), COMPUTER_CONFIG_FILE)
End Function
Public Function GetCurrentConfigPath() As String
If File.Exists(GetUserConfigPath()) Then
Return GetUserConfigPath()
Else
Return GetAllUsersConfigPath()
End If
End Function
Public Function Settings_Load()
Try
Dim oDatatable As DataTable
' if file in %APPDATA% doesn't exist,
' check for file in %ALLUSERSPROFILE%,
' otherwise create the file with its default xml table
If File.Exists(GetUserConfigPath()) Then
oDatatable = GetTablefromXML(GetUserConfigPath())
ElseIf File.Exists(GetAllUsersConfigPath()) Then
oDatatable = GetTablefromXML(GetAllUsersConfigPath())
Else
oDatatable = CreateConfigTable()
oDatatable.WriteXml(GetUserConfigPath())
End If
For Each oRow As DataRow In oDatatable.Rows
Select Case oRow.Item("ConfigName")
Case "MyConnectionString"
Dim oConnectionString As String
'Den ConnectonString mit verschlüsseltem PW laden
Dim oBuilder As New SqlClient.SqlConnectionStringBuilder
oBuilder.ConnectionString = oRow.Item("Value")
If Not oBuilder.ConnectionString = "" Then
If oBuilder.ConnectionString.Contains("Password=") Then
'sa-
'Jetzt das Passwort entschlüsseln
Dim PWplainText As String
Dim wrapper As New ClassEncryption("!35452didalog=")
' DecryptData throws if the wrong password is used.
Try
PWplainText = wrapper.DecryptData(oBuilder.Password)
Catch ex As Exception
LOGGER.Error(ex)
LOGGER.Info("- the Password '" & oBuilder.Password & "' could not be decrypted", False)
PWplainText = oBuilder.Password
End Try
oConnectionString = oRow.Item("Value").ToString.Replace(oBuilder.Password, PWplainText)
Else
'Windows-Auth
oConnectionString = oRow.Item("Value").ToString
End If
CONNECTION_STRING = oConnectionString
ClassDatabase.Init()
Else
CONNECTION_STRING = ""
End If
Case "LogErrorsOnly"
LOG_ERRORS_ONLY = CBool(oRow.Item("Value"))
Case "UniversalViewer"
VIEWER_UNIVERSAL = oRow.Item("Value")
Case "PDFXChangeViewer"
VIEWER_XCHANGE = oRow.Item("Value")
Case "PDFViewer_ZoomMode"
VIEWER_ZOOM_LEVEL = oRow.Item("Value")
Case "vpdfviewer"
VIEWER_PDF = oRow.Item("Value")
Case "Viewer"
VIEWER_ALL = oRow.Item("Value")
Case "SumatraViewer"
VIEWER_SUMATRA = oRow.Item("Value")
Case "IDX_DMS_ERSTELLT"
INDEX_DMS_ERSTELLT = oRow.Item("Value")
Case "IDX_DMS_ERSTELLT_ZEIT"
INDEX_DMS_ERSTELLT_ZEIT = oRow.Item("Value")
Case "USRMNGRPATH"
USER_MANAGER_PATH = oRow.Item("Value")
End Select
Next
Return True
Catch ex As Exception
LOGGER.Error(ex)
MsgBox("Error in LoadMyConfig" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
Return False
End Try
End Function
Private Function GetTablefromXML(ConfigPath As String) As DataTable
Try
Dim oDataset As New DataSet
oDataset.ReadXml(ConfigPath)
Return oDataset.Tables(0)
Catch ex As Exception
LOGGER.Error(ex)
Dim oDataTable = CreateConfigTable()
oDataTable.WriteXml(GetUserConfigPath())
MsgBox("Fehler beim Laden der Konfiguration. Es wurde die Standard Konfiguration geladen. Fehler:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
Return oDataTable
End Try
End Function
Private Function CreateConfigTable() As DataTable
Try
' Create sample Customers table, in order
' to demonstrate the behavior of the DataTableReader.
Dim oTable As New DataTable
oTable.TableName = "MyConfig"
' Create two columns, ID and Name.
Dim oIdColumn As DataColumn = oTable.Columns.Add("ID",
GetType(System.Int32))
oIdColumn.AutoIncrement = True
oIdColumn.AutoIncrementSeed = 0
oIdColumn.AutoIncrementStep = 1
oTable.Columns.Add("ConfigName", GetType(System.String))
oTable.Columns.Add("Value", GetType(System.String))
'Set the ID column as the primary key column.
oTable.PrimaryKey = New DataColumn() {oIdColumn}
Dim newRow As DataRow = oTable.NewRow()
newRow("ConfigName") = "MyConnectionString"
newRow("Value") = ""
oTable.Rows.Add(newRow)
Dim newRow1 As DataRow = oTable.NewRow()
newRow1("ConfigName") = "LogErrorsOnly"
newRow1("Value") = "True"
oTable.Rows.Add(newRow1)
Dim newRow2 As DataRow = oTable.NewRow()
newRow2("ConfigName") = "UniversalViewer"
newRow2("Value") = ""
oTable.Rows.Add(newRow2)
Dim newRow3 As DataRow = oTable.NewRow()
newRow3("ConfigName") = "PDFXChangeViewer"
newRow3("Value") = ""
oTable.Rows.Add(newRow3)
Dim newRow7 As DataRow = oTable.NewRow()
newRow7("ConfigName") = "SumatraViewer"
newRow7("Value") = ""
oTable.Rows.Add(newRow7)
Dim newRow4 As DataRow = oTable.NewRow()
newRow4("ConfigName") = "PDFViewer_ZoomMode"
newRow4("Value") = "3"
oTable.Rows.Add(newRow4)
Dim newRow5 As DataRow = oTable.NewRow()
newRow5("ConfigName") = "vpdfviewer"
newRow5("Value") = "internal"
oTable.Rows.Add(newRow5)
Dim newRow6 As DataRow = oTable.NewRow()
newRow6("ConfigName") = "Viewer"
newRow6("Value") = "docview"
oTable.Rows.Add(newRow6)
Dim newRow8 As DataRow = oTable.NewRow()
newRow8("ConfigName") = "IDX_DMS_ERSTELLT"
newRow8("Value") = "DMS erstellt"
oTable.Rows.Add(newRow8)
Dim newRow9 As DataRow = oTable.NewRow()
newRow9("ConfigName") = "IDX_DMS_ERSTELLT_ZEIT"
newRow9("Value") = "DMS erstellt (Zeit)"
oTable.Rows.Add(newRow9)
Dim newRow10 As DataRow = oTable.NewRow()
newRow10("ConfigName") = "USRMNGRPATH"
newRow10("Value") = ""
oTable.Rows.Add(newRow10)
oTable.AcceptChanges()
Return oTable
Catch ex As Exception
LOGGER.Error(ex)
MsgBox("Error in CreateConfigTable" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
Return Nothing
End Try
End Function
''' <summary>
''' Save settings to user config, NOT to common config
''' </summary>
Public Function SaveMySettingsValue(name As String, value As String)
Try
Dim oUserConfigPath = GetUserConfigPath()
Dim oCurrentConfigPath = GetCurrentConfigPath()
Dim oDatatable As DataTable = GetTablefromXML(oCurrentConfigPath)
For Each Row As DataRow In oDatatable.Rows
If Row.Item("ConfigName") = name Then
Row.Item("Value") = value
End If
Next
oDatatable.AcceptChanges()
oDatatable.WriteXml(oUserConfigPath)
Return True
Catch ex As Exception
LOGGER.Error(ex)
MsgBox("Error in SaveConfigValue" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
Return False
End Try
End Function
End Module