Attribute VB_Name = "Ini"
Option Explicit

'----------------------------------------------------------
'API deklarace funkce ktera umi obsluhovat INI soubory
'----------------------------------------------------------

Declare Function GetPrivateProfileString Lib "kernel32" Alias _
"GetPrivateProfileStringA" (ByVal lpSectionName As String, _
ByVal lpKeyName As Any, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long, _
ByVal lpFileName As String) As Long

Declare Function GetPrivateProfileInt Lib "kernel32" Alias _
"GetPrivateProfileIntA" (ByVal lpSectionName As String, _
ByVal lpKeyName As String, _
ByVal nDefault As Long, _
ByVal lpFileName As String) As Long

Declare Function WritePrivateProfileString Lib "kernel32" Alias _
"WritePrivateProfileStringA" (ByVal lpSectionName As String, _
ByVal lpKeyName As Any, _
ByVal lpString As Any, _
ByVal lpFileName As String) As Long


'----------------------------------------------------------
'nacteni setupu z ini
'----------------------------------------------------------
   
Public Sub CteniIni()
    
    Dim i As Integer
    Dim kam As String
    Dim PomR As String
    Dim j As Integer
    
    On Error GoTo chyba
    
        Bafr = Space(100)
    
        Delka = GetPrivateProfileString _
            ("Config", "CestaSoubory", "C:\", Bafr, 100, (Cesta & "\RDS.ini"))
            CestaSoubory = Left(Bafr, Delka)
        CestaSouboryZal = CestaSoubory
        
        Delka = GetPrivateProfileString _
            ("Com", "Rychlost", "0", Bafr, 100, (Cesta & "\RDS.ini"))
            ComRychlostIndex = Left(Bafr, Delka)
        ComRychlostIndexZAL = ComRychlostIndex
        
        Delka = GetPrivateProfileString _
            ("Com", "Port", "0", Bafr, 100, (Cesta & "\RDS.ini"))
            ComPortIndex = Left(Bafr, Delka)
        ComPortIndexZAL = ComPortIndex
      
        For i = 0 To 2
       
            Select Case i
            
                Case 0: kam = "Priorita_Textu"
                Case 1: kam = "Priorita_Promo"
                Case 2: kam = "Priorita_Flash"
            
            End Select
         
            Delka = GetPrivateProfileString _
                ("Config", kam, Str(i + 1), Bafr, 100, (Cesta & "\RDS.ini"))
            Priorita(i) = Left(Bafr, Delka)
    
            If (Priorita(i) = "1") Then
                Frm1.OptSetup1(i).Value = 1
            ElseIf (Priorita(i) = "2") Then
                Frm1.OptSetup2(i).Value = 1
            ElseIf (Priorita(i) = "3") Then
                Frm1.OptSetup3(i).Value = 1
            End If
        
        Next i
        
        Delka = GetPrivateProfileString _
            ("TA", "Port", "0", Bafr, 100, (Cesta & "\RDS.ini"))
            TAPortIndex = Left(Bafr, Delka)
        TAPortIndexZAL = TAPortIndex
            
        Delka = GetPrivateProfileString _
            ("TA", "Vstup", "0", Bafr, 100, (Cesta & "\RDS.ini"))
            TAVstupIndex = Left(Bafr, Delka)
        TAVstupIndexZAL = TAVstupIndex
        
        Delka = GetPrivateProfileString _
            ("TA", "Orientace", "0", Bafr, 100, (Cesta & "\RDS.ini"))
            TAOrientace = Left(Bafr, Delka)
        TAOrientaceZAL = TAOrientace
        
        Delka = GetPrivateProfileString _
            ("TA", "Provoz", "0", Bafr, 100, (Cesta & "\RDS.ini"))
            TAProvoz = Left(Bafr, Delka)
        TAProvozZAL = TAProvoz
        
        If (Dir(CestaSoubory & "/RDSEdit.Ini") <> "") Then
            Delka = GetPrivateProfileString _
                ("Spravce", "Heslo", "", Bafr, 100, (CestaSoubory & "\RDSEdit.ini"))
            HesloSpravce = DEKrypt(Left(Bafr, Delka))
        End If
        
        If (Dir(CestaSoubory & "/RDSEdit.Ini") <> "") Then
            Delka = GetPrivateProfileString _
                ("Config", "Maska", "%yyy-%m-%d", Bafr, 100, (CestaSoubory & "\RDSEdit.ini"))
                Maska = Left(Bafr, Delka)
            MaskaZal = Maska
        End If
        
        Delka = GetPrivateProfileString _
                ("Email", "Server", "", Bafr, 100, (Cesta & "\RDS.ini"))
            EmailServer = Left(Bafr, Delka)
            
        Delka = GetPrivateProfileString _
                ("Email", "MailFrom", "", Bafr, 100, (Cesta & "\RDS.ini"))
            EmailMailFrom = Left(Bafr, Delka)
            
        Delka = GetPrivateProfileString _
                ("Email", "Predmet", "%%", Bafr, 100, (Cesta & "\RDS.ini"))
            EmailPredmet = Left(Bafr, Delka)
            
        Delka = GetPrivateProfileString _
                ("Email", "Login", "", Bafr, 100, (Cesta & "\RDS.ini"))
            EmailLogin = Left(Bafr, Delka)
            
        Delka = GetPrivateProfileString _
                ("Email", "Heslo", "", Bafr, 100, (Cesta & "\RDS.ini"))
            If (Left(Bafr, Delka) <> "") Then EmailHeslo = DEKrypt(Left(Bafr, Delka))
            
        Delka = GetPrivateProfileString _
                ("Email", "Autentifikace", "0", Bafr, 100, (Cesta & "\RDS.ini"))
            EmailAut = Left(Bafr, Delka)
            
        Delka = GetPrivateProfileString _
                ("Email", "Zapnuto", "0", Bafr, 100, (Cesta & "\RDS.ini"))
            EmailZap = Left(Bafr, Delka)
        
        For i = 0 To 9
        
            Delka = GetPrivateProfileString _
                ("Email", "Uzivatel" & CStr(i + 1), ",0000", Bafr, 100, (Cesta & "\RDS.ini"))
            PomR = Left(Bafr, Delka)
            
            j = InStr(PomR, ",")
            EmailUsr(i, 0) = Left(PomR, j - 1)
            EmailUsr(i, 1) = Mid(PomR, j + 1, 1)
            EmailUsr(i, 2) = Mid(PomR, j + 2, 1)
            EmailUsr(i, 3) = Mid(PomR, j + 3, 1)
            EmailUsr(i, 4) = Mid(PomR, j + 4, 1)
            
        Next i
        
        ZapisEmailUziv
        
        Frm1.TxtEmailS.Text = EmailServer
        Frm1.TxtEmailMF.Text = EmailMailFrom
        Frm1.TxtEmailP.Text = EmailPredmet
        Frm1.TxtEmailL.Text = EmailLogin
        Frm1.TxtEmailH.Text = EmailHeslo
        Frm1.CboSetupAut.ListIndex = EmailAut
        Frm1.ChckEmail.Value = EmailZap
        
        Frm1.CboPort.ListIndex = ComPortIndex
        Frm1.CboRychlost.ListIndex = ComRychlostIndex
        Frm1.CboTAVstup.ListIndex = TAVstupIndex
        Frm1.CboTAPort.ListIndex = TAPortIndex
        Frm1.ChckTAProvoz.Value = TAProvoz
        If (TAProvoz = 0) Then
            Frm1.LblStTA(0).Caption = "TA Off"
            Frm1.LblStTA(1).Caption = "TA Off"
            Frm1.Timer6.Enabled = False
        Else
            If (Dir(CestaSoubory & "/TAOff.rds") <> "") Then
                Kill (CestaSoubory & "/TAOff.rds")
            End If
        End If
        Frm1.OptTAOr(TAOrientace).Value = True
        port
        
        DoEvents
        
    Exit Sub
    
chyba:
       OdesliEmail "RDSManager Error", 1, "Nepodarilo se nacist ini nebo cfg soubor!!"
       
End Sub


'----------------------------------------------------------
'ulozi setup do ini
'----------------------------------------------------------

Public Sub ZapisIni()

    Dim i As Integer
    Dim Stav As Integer
    Dim PomR As String
    
    If (Dir(CestaSoubory & "/RDSEdit.Ini") <> "") Then
        Maska = Frm1.TxtSetMaska.Text
        Stav = WritePrivateProfileString _
            ("Config", "Maska", Maska, (CestaSoubory & "\RDSEdit.ini"))
    Else:
        Frm1.FraERRORAdr.Visible = True
    End If
            
    Stav = WritePrivateProfileString _
        ("Config", "CestaSoubory", CestaSoubory, (Cesta & "\RDS.ini"))
     
    ComPortIndex = Frm1.CboPort.ListIndex
    Stav = WritePrivateProfileString _
        ("Com", "Port", CStr(ComPortIndex), (Cesta & "\RDS.ini"))
            
    ComRychlostIndex = Frm1.CboRychlost.ListIndex
    Stav = WritePrivateProfileString _
        ("Com", "Rychlost", CStr(ComRychlostIndex), (Cesta & "\RDS.ini"))
   
    For i = 0 To 2
   
        Select Case i
        
            Case 0: kam = "Priorita_Textu"
            Case 1: kam = "Priorita_Promo"
            Case 2: kam = "Priorita_Flash"
        
        End Select
  
        Stav = WritePrivateProfileString _
            ("Config", kam, Priorita(i), (Cesta & "\RDS.ini"))
            
    Next i
    
    TAPortIndex = Frm1.CboTAPort.ListIndex
    Stav = WritePrivateProfileString _
        ("TA", "Port", CStr(TAPortIndex), (Cesta & "\RDS.ini"))
        
    TAVstupIndex = Frm1.CboTAVstup.ListIndex
    Stav = WritePrivateProfileString _
        ("TA", "Vstup", CStr(TAVstupIndex), (Cesta & "\RDS.ini"))
        
    
    If (Frm1.OptTAOr(0) = True) Then
        TAOrientace = 0
    Else:
        TAOrientace = 1
    End If
    
    Stav = WritePrivateProfileString _
        ("TA", "Orientace", CStr(TAOrientace), (Cesta & "\RDS.ini"))
        
    TAProvoz = Frm1.ChckTAProvoz.Value
    Stav = WritePrivateProfileString _
        ("TA", "Provoz", CStr(TAProvoz), (Cesta & "\RDS.ini"))
       
    For i = 0 To 9
    
        PomR = EmailUsr(i, 0) & "," & EmailUsr(i, 1) & EmailUsr(i, 2) & EmailUsr(i, 3) & EmailUsr(i, 4)
        Stav = WritePrivateProfileString _
        ("Email", "Uzivatel" & CStr(i + 1), PomR, (Cesta & "\RDS.ini"))
    
    Next i
    
    EmailServer = Frm1.TxtEmailS.Text
    Stav = WritePrivateProfileString _
        ("Email", "Server", EmailServer, (Cesta & "\RDS.ini"))
        
    EmailMailFrom = Frm1.TxtEmailMF.Text
    Stav = WritePrivateProfileString _
        ("Email", "MailFrom", EmailMailFrom, (Cesta & "\RDS.ini"))
        
    EmailPredmet = Frm1.TxtEmailP.Text
    Stav = WritePrivateProfileString _
        ("Email", "Predmet", EmailPredmet, (Cesta & "\RDS.ini"))
        
    EmailLogin = Frm1.TxtEmailL.Text
    Stav = WritePrivateProfileString _
        ("Email", "Login", EmailLogin, (Cesta & "\RDS.ini"))
        
    If (Frm1.TxtEmailH.Text <> "") Then
        EmailHeslo = Frm1.TxtEmailH.Text
        Stav = WritePrivateProfileString _
            ("Email", "Heslo", Krypt(EmailHeslo), (Cesta & "\RDS.ini"))
    End If
        
    EmailAut = Frm1.CboSetupAut.ListIndex
    Stav = WritePrivateProfileString _
        ("Email", "Autentifikace", CStr(EmailAut), (Cesta & "\RDS.ini"))
    
    EmailZap = Frm1.ChckEmail.Value
    Stav = WritePrivateProfileString _
        ("Email", "Zapnuto", CStr(EmailZap), (Cesta & "\RDS.ini"))
       
    DoEvents
     
End Sub


'----------------------------------------------------------
'zakryptuje heslo pred ulozenim
'----------------------------------------------------------

Public Function Krypt(ss As String) As String

    Dim i As Integer
    Dim L As Integer
    Dim X As Integer
    Dim Y As Integer
    Dim Kr As String

    L = Len(ss)
    Y = Asc(ss)
    X = (L + 150) / Y

    For i = 1 To L
    
        Kr = Mid(ss, i, 1)
        Mid(ss, i, 1) = Chr(Asc(Kr) + ((-1) ^ i * i * X))
        
    Next i

    Krypt = ss & Chr(Y)

End Function


'----------------------------------------------------------
'odkryptuje heslo pri nacteni z ini
'----------------------------------------------------------

Public Function DEKrypt(ss As String) As String

    Dim i As Integer
    Dim L As Integer
    Dim X As Integer
    Dim Y As Integer
    Dim Kr As String
    Dim ss1 As String

    L = Len(ss) - 1
    Y = Asc(Right(ss, 1))
    X = (L + 150) / Y

    For i = 1 To L
    
        Kr = Mid(ss, i, 1)
        ss1 = ss1 & Chr(Asc(Kr) - ((-1) ^ i * i * X))
        
    Next i
    
    DEKrypt = ss1

End Function

