VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsAliases"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Compare Text
Public Aliases As New Collection
Const Separator As String = "זק"

Public Sub LoadAliases()
    Set Aliases = New Collection
    Dim r As String, S() As String, I As Integer
    r = modFileIO.ReadFile(AppData & "Data\Preferences\Aliases.ini")
    S = Split(r, vbNewLine)
    For I = 0 To UBound(S)
        If InStr(S(I), Separator) Then
            Dim t() As String
            t = Split(S(I), Separator, 3)
            If UBound(t) = 2 Then
                AddAlias t(0), t(1), t(2), False
            End If
        End If
    Next
    SaveAliases
End Sub

Public Sub SaveAliases()
    Dim r As String, I As Integer, A As objAlias
    For I = 1 To Aliases.Count
        Set A = Aliases(I)
        r = r & A.Username & Separator & A.Alias & Separator & A.ServerName & vbNewLine
    Next
    SaveFile AppData & "Data\Preferences\Aliases.ini", r
End Sub

Public Function AddAlias(ByVal nUsername As String, Alias As String, ServerName As String, Optional Save As Boolean = True) As Boolean
    If GatewayExists(ServerName) = False Then Exit Function
    Dim Username As String
    Username = StripGateway(Suffix(nUsername))
    If AliasExists(Username, ServerName) Then
        Dim I As Integer
        For I = 1 To Aliases.Count
            If Aliases(I).Username = Username And Aliases(I).ServerName = ServerName Then
                Aliases(I).Alias = Alias
                AddAlias = True
            End If
        Next
    Else
        Dim A As New objAlias
        A.Username = Username
        A.Alias = Alias
        A.ServerName = ServerName
        Aliases.Add A
        AddAlias = True
    End If
    If Save Then SaveAliases
End Function

Public Function AliasExists(ByVal Username As String, ServerName As String) As Boolean
On Error Resume Next
    Dim I As Integer
    For I = 1 To Aliases.Count
        If Aliases(I).Username = StripGateway(Suffix(Username)) And Aliases(I).ServerName = ServerName Then
            AliasExists = True
            Exit Function
        End If
    Next
End Function

Public Function GetAlias(ByVal Username As String, ServerName As String) As String
On Error Resume Next
    Dim I As Integer
    For I = 1 To Aliases.Count
        If Aliases(I).Username = StripGateway(Suffix(Username)) And Aliases(I).ServerName = ServerName Then
            GetAlias = Aliases(I).Alias
            Exit Function
        End If
    Next
End Function

Public Function RemoveAlias(ByVal Username As String, ServerName As String) As Boolean
On Error Resume Next
    Dim I As Integer
    For I = Aliases.Count To 1 Step -1
        If Aliases(I).Username = StripGateway(Suffix(Username)) And Aliases(I).ServerName = ServerName Then
            Aliases.Remove I
            RemoveAlias = True
            Exit Function
        End If
    Next
    SaveAliases
End Function

