5e9eede49d
* BUG: Try to retrieve the connection password only if the 'gb.desktop' component is loaded, and only display warning messages in that case. git-svn-id: svn://localhost/gambas/trunk@6575 867c0c6c-44f3-4631-809d-bfa615b0a4ec
125 lines
2.4 KiB
Text
125 lines
2.4 KiB
Text
' Gambas class file
|
|
|
|
Export
|
|
|
|
Class Desktop
|
|
|
|
Static Property Read Key As String
|
|
|
|
Static Private $cConn As New Collection
|
|
Static Private $aConn As String[]
|
|
Static Private $sKey As String
|
|
|
|
Static Public Sub Exist(Name As String) As Boolean
|
|
|
|
If $cConn.Exist(Name) Or If Exist("../.connection" &/ Name & ".connection") Then Return True
|
|
|
|
End
|
|
|
|
Static Private Sub Init()
|
|
|
|
Dim sFile As String
|
|
|
|
If $aConn Then Return
|
|
$aConn = New String[]
|
|
|
|
For Each sFile In Dir("../.connection", "*.connection")
|
|
$aConn.Add(File.BaseName(sFile))
|
|
Next
|
|
|
|
End
|
|
|
|
|
|
Static Public Sub _get(Name As String) As Connection
|
|
|
|
Dim sPath As String
|
|
Dim hFile As File
|
|
Dim hConn As Connection
|
|
Dim sLine As String
|
|
Dim aLine As String[]
|
|
Dim bPassword As Boolean
|
|
|
|
Init()
|
|
|
|
If $cConn.Exist(Name) Then Return $cConn[Name]
|
|
|
|
sPath = "../.connection" &/ Name & ".connection"
|
|
If Not Exist(sPath) Then Return
|
|
|
|
hConn = New Connection
|
|
hFile = Open sPath
|
|
|
|
While Not Eof(hFile)
|
|
|
|
Line Input #hFile, sLine
|
|
If Left(sLine) = "#" Then Continue
|
|
If Left(sLine) = "[" Then Continue
|
|
|
|
aLine = Scan(sLine, "*=*")
|
|
If aLine.Count < 2 Then Continue
|
|
|
|
Select Case LCase(aLine[0])
|
|
|
|
Case "type"
|
|
hConn.Type = UnQuote(aLine[1])
|
|
|
|
Case "path", "host"
|
|
hConn.Host = UnQuote(aLine[1])
|
|
|
|
Case "port"
|
|
hConn.Port = UnQuote(aLine[1])
|
|
|
|
Case "database"
|
|
hConn.Name = UnQuote(aLine[1])
|
|
|
|
Case "ignorecharset"
|
|
hConn.IgnoreCharset = LCase(aLine[1]) = "true"
|
|
|
|
Case "user"
|
|
hConn.User = UnQuote(aLine[1])
|
|
|
|
Case "rememberpassword"
|
|
bPassword = LCase(aLine[1]) = "true"
|
|
|
|
End Select
|
|
|
|
Wend
|
|
|
|
Close #hFile
|
|
|
|
If bPassword Then
|
|
If Component.IsLoaded("gb.desktop") Then
|
|
Try hConn.Password = Desktop.Passwords[Application.Name &/ Name]
|
|
If Error Then
|
|
Error "gb.db: warning: "; Application.Name &/ Name; ": unable to retrieve connection password: "; Error.Text
|
|
Endif
|
|
Endif
|
|
Endif
|
|
|
|
$cConn[Name] = hConn
|
|
Return hConn
|
|
|
|
End
|
|
|
|
Static Public Sub _next() As Connection
|
|
|
|
Init()
|
|
|
|
If Not Enum.Index Then Enum.Index = 0
|
|
|
|
If Enum.Index >= $aConn.Count Then
|
|
$sKey = ""
|
|
Enum.Stop
|
|
Else
|
|
Inc Enum.Index
|
|
$sKey = $aConn[Enum.Index - 1]
|
|
Return _get($sKey)
|
|
Endif
|
|
|
|
End
|
|
|
|
Static Private Function Key_Read() As String
|
|
|
|
Return $sKey
|
|
|
|
End
|