Attribute VB_Name = "FileAdmin"
Global remoteDir As String
Global localDir As String
Global remoteSendingDir As String
Global remoteDrive As String
Global remoteFileSize As Long
Global remoteFile As String
Global remoteRuteSelect As String
Global localFileRoute As String

Global sendingData As Boolean

Global sending As Boolean
Global receiving As Boolean
Global transCancel As Boolean

Sub updateMenu()
With client
Dim nodeItem As Node
Set nodeItem = .files(1).SelectedItem
If nodeItem Is Nothing Then Exit Sub
Dim rRute As String
rRute = combineRute(remoteDir, nodeItem.Text)
.l(11) = "Selección: " & nodeItem.Text
.l(13).Visible = nodeItem.Image > 2
If .menu(1).Visible Then waitSend "a1" & rRute & "?"
waitSend "a3" & rRute & "?"
waitSend "a4" & rRute & "?"
End With
End Sub

Sub waitSend(data As String)
With client.TCP
sendingData = True
    .SendData data
    While sendingData
    DoEvents
    Wend
End With
End Sub

Sub sendFile(LocalRoute As String, Optional remoteRoute As String)
On Error GoTo etError
    With client
    Dim dat As String
            'Enviamos el Archivo
                Dim Buf As String * 1024
                Open LocalRoute For Binary As #1
                'Enviamos el tamaño del archivo y ubicación remota del archivo
                    waitSend 2 & LOF(1) & "?" & remoteRoute
                    If LOF(1) = 0 Then
                        MsgBox "El archivo selecionado está vacío (0 Bytes)", vbCritical
                        GoTo etEnd
                    End If
                    sending = True
                    chgTransState
                    DoEvents
                    .Barra.Min = 0
                    .Barra.Max = LOF(1)
                    .Barra.Value = 0
                    Do While Not EOF(1)
                        Get #1, , Buf
                        DoEvents
                        dat = dat & Buf
                        If Len(dat) <= .Barra.Max Then .Barra.Value = Len(dat)
                        If Not transCancel Then
                            waitSend Buf
                        Else
                            transCancel = False
                            Exit Do
                        End If
                    Loop
etEnd:
                Close #1
        sending = False
        chgTransState
        If .p(3).Visible Then remoteDisplayDirectory remoteDir
    End With
Exit Sub
etError:
MsgBox "Error (" & Err & ") al enviar el archivo " & LocalRoute & ":" & vbCrLf & Err.Description, vbCritical
End Sub

Sub FileAdminUpdate(Optional OnlyRemote As Boolean)
Dim conn As Boolean
With client
    conn = .TCP.State = sckConnected
    If Not OnlyRemote Then
        If .txt(5) = "" Then
        displayDirectory Left(CurDir$, 3)
        Else
        displayDirectory .txt(5)
        End If
    End If
    If .txt(6) = "" Then
        remoteDisplayDirectory
    Else
        remoteDisplayDirectory .txt(6)
    End If
    .bFileAdmin(0).Visible = conn
    .bFileAdmin(1).Visible = conn
End With
End Sub

Sub fileReceive(dat As String)
On Error GoTo etError
With client.Barra
        receiving = True
        chgTransState
        remoteFile = remoteFile & dat
        If Len(remoteFile) >= remoteFileSize Then
        remoteFile = Left(remoteFile, remoteFileSize)
        If FileExist(localFileRoute) Then Kill localFileRoute
        Open localFileRoute For Binary As #1
            Put #1, , remoteFile
            remoteFileSize = 0
            remoteFile = ""
            receiving = False
            chgTransState
            displayDirectory desglosarRuta(localFileRoute, DIRECTORIO)
            localFileRoute = ""
        Close #1
        Else
            .Value = Len(remoteFile)
        End If
End With
Exit Sub
etError:
MsgBox "Error (" & Err & ") al recivir el archivo " & LocalRoute & ":" & vbCrLf & Err.Description, vbCritical
End Sub

Sub chgTransState()
Dim boo As Boolean
With client
boo = sending Or receiving
.comm(10).Visible = boo
.Barra.Visible = boo
.sb.Panels(1).Text = ""
If boo Then .sb.Panels(1).Text = IIf(sending, "Enviando...", "Reciviendo...")
End With
End Sub

Sub waitFileReceive()
While localFileRoute <> "" 'Esperar a recivir el archivo
    DoEvents
Wend
End Sub

Sub extractFile(LocalRoute As String, remoteRoute As String)
localFileRoute = LocalRoute
waitSend "9" & remoteRoute
End Sub

Sub remoteDisplayDirectory(Optional szDir As String)
On Error Resume Next
remoteSendingDir = szDir
client.sh(1).FillColor = vbRed
waitSend "7" & szDir
End Sub

Sub displayDirScript(szScript As String, tipo As Integer)
Dim Ini As Long
Ini = 1
Dim Inserting As Boolean
Dim Text As String
With client
    If tipo = 0 Then
    .files(1).Nodes.Clear
    'Desplegamos directorios
        For i = 1 To Len(szScript)
            If Mid(szScript, i, 1) = "?" Then
                If Inserting Then
                    Inserting = False
                    .files(1).Nodes.Add , , , Text, CInt(Mid(szScript, Ini, i - Ini))
                Else
                    Inserting = True
                    Text = Mid(szScript, Ini, i - Ini)
                End If
                Ini = i + 1
            End If
        Next i
    .files(1).Sorted = True
    .files(1).Sorted = False
    ElseIf tipo = 1 Then
        With .lst
            .Clear
            Inserting = False
            Ini = 1
            'Desplegamos los archivos
            For i = 1 To Len(szScript)
                    If Mid(szScript, i, 1) = "?" Then
                        .AddItem Mid(szScript, Ini, i - Ini)
                        Ini = i + 1
                    End If
            Next i
            For i = 0 To .ListCount - 1
                client.files(1).Nodes.Add , , , Left(.List(i), Len(.List(i)) - 1), CInt(Right(.List(i), 1))
            Next i
        End With
        .sh(1).FillColor = vbGreen
        If remoteSendingDir = "" Then
            .txt(6) = remoteDrive
        Else
            .txt(6) = remoteSendingDir
        End If
        remoteDir = .txt(6)
    End If
End With
End Sub

Sub displayDirectory(szDir As String)
On Error Resume Next
Dim item As String
client.sh(0).FillColor = vbRed
If Dir(szDir, vbDirectory + vbArchive + vbHidden + vbReadOnly + vbSystem) = "" Then GoTo noExiste
With client.lst
.Clear
'Desplegamos los directorios
client.files(0).Nodes.Clear
ChDir szDir
client.txt(5) = szDir
item = Dir$("*.*", vbDirectory + vbArchive + vbHidden + vbReadOnly + vbSystem)
While item <> ""
    If IsDirectory(item) Then client.files(0).Nodes.Add , , , item, 1 + -CInt(IsHiddenFile(item))
    item = Dir$
Wend
client.files(0).Sorted = True
client.files(0).Sorted = False
'Desplegamos los archivos
item = Dir$("*.*", vbArchive + vbHidden + vbReadOnly + vbSystem + vbVolume)
While item <> ""
    .AddItem item
    DoEvents
    item = Dir$
Wend
For i = 0 To .ListCount - 1
    client.files(0).Nodes.Add , , , .List(i), 3 + -CInt(IsHiddenFile(.List(i)))
Next i
End With
localDir = szDir
client.sh(client.txt(5).Index - 5).FillColor = vbGreen
Exit Sub
noExiste:
client.sh(client.txt(5).Index - 5).FillColor = vbRed
MsgBox "El directorio " & szDir & " no existe...", vbCritical
End Sub
