
Private Const PacketSize As Long = 65536 '1024 * 24
Private sFileName, rFileName As String
Private iFileNum, rFileNum, totalFilesToSend, totalFilesSent As Integer
Private FileSize As Long
Private BytesReceived As Long

Private Sub PicShowPercentage(Pic As PictureBox, ByVal Percentage As Single, Optional ByVal ForeColor As Long = -2, Optional ByVal BackColor As Long = -2)
    If ForeColor = -2 Then ForeColor = Pic.ForeColor
    If BackColor = -2 Then BackColor = Pic.BackColor
    
    Pic.Line (0, 0)-(Pic.ScaleWidth * Percentage, Pic.ScaleHeight), ForeColor, BF
    Pic.Line (Pic.ScaleWidth * Percentage, Pic.ScaleHeight)-(Pic.ScaleWidth, 0), BackColor, BF
End Sub

Private Sub cmdSendFile()
'    If lblProgress.Caption = "0.0% Sent" Or lblProgress.Caption = "0.00% Done" Then
        If Len(Label11(totalFilesSent).Caption) > 0 Then
            'MsgBox lblFN(totalFilesSent).Caption
            If Len(Dir(lblFN(totalFilesSent).Caption, vbNormal + vbArchive)) > 0 Then
                picProgress.Visible = True
                SendFile Trim(lblFN(totalFilesSent).Caption)
            Else
                'vionjo
                totalFilesSent = totalFilesSent + 1
                If totalFilesSent <= totalFilesToSend Then
                    'Pause 20
                    'PicShowPercentage Me.picProgress, 0
                    cmdSendFile
                End If
            End If
        Else
        'vionjo
            totalFilesSent = totalFilesSent + 1
            If totalFilesSent <= totalFilesToSend Then
                'Pause 50
                'PicShowPercentage Me.picProgress, 0
                cmdSendFile
            End If
            
        End If
'    Else
'        'If SckSendFile.State <> sckClosed Then
'        'SckSendFile_Close
'        lblProgress.Caption = "0.0% Sent"
'        'End If
'    End If
End Sub

Private Sub SendFile(ByVal FileName As String)
    tmrSendFile.Enabled = False
    SckSendFile.Close
    DoEvents
    
    SckSendFile.RemoteHost = RemoteServer
    SckSendFile.RemotePort = RemoteServerPortSend
    SckSendFile.Connect
    
    sFileName = FileName
End Sub

Private Sub SckSendFile_Close()
    tmrSendFile.Enabled = False
    tmrSendFile.Interval = 0
    
    Close iFileNum ' close file
    iFileNum = 0 ' set file number to 0, timer will exit if another timer event

    SckSendFile.Close
        

    totalFilesSent = totalFilesSent + 1
    If totalFilesSent <= totalFilesToSend Then
            Pause 50
            PicShowPercentage Me.picProgress, 0
            cmdSendFile
            Exit Sub
    End If
    picProgress.Visible = False
    picProgress.Cls
    PicShowPercentage Me.picProgress, 0
    Me.MousePointer = vbNormal
End Sub

Private Sub SckSendFile_Connect()
    Dim Buffer() As Byte, P As Long
    
    iFileNum = FreeFile
    Open sFileName For Binary Access Read Lock Write As iFileNum
    
    ReDim Buffer(lngMIN(LOF(iFileNum), PacketSize) - 1)
    Get iFileNum, , Buffer ' read data
    SckSendFile.SendData CStr(LOF(iFileNum)) & ","   ' send the file size
    P = InStrRev(sFileName, "\")
    SckSendFile.SendData Mid(sFileName, P + 1) & ":" ' send the file name
    SckSendFile.SendData Buffer                      ' send first packet
End Sub

Private Sub SckSendFile_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
    MsgBox Number & ": " & Description, vbCritical, "Error"
    SckSendFile_Close
End Sub

Private Sub SckSendFile_SendComplete()
    ' can't call SendData here, so enable timer to do it...
    tmrSendFile.Enabled = False
    tmrSendFile.Interval = 1
    tmrSendFile.Enabled = True
End Sub
Private Sub tmrSendFile_Timer()
    Dim Buffer() As Byte, BuffSize As Long
    
    tmrSendFile.Enabled = False
    If iFileNum <= 0 Or SckSendFile.State <> sckConnected Then Exit Sub
    
    If Loc(iFileNum) >= LOF(iFileNum) Then ' FILE COMPLETE
        Close iFileNum ' close file
        iFileNum = 0 ' set file number to 0, timer will exit if another timer event

        
        Exit Sub
    End If
    
    'if the remaining size in the file is smaller then PacketSize, the read only whatever is left
    BuffSize = lngMIN(LOF(iFileNum) - Loc(iFileNum), PacketSize)
    
    ReDim Buffer(BuffSize - 1) ' resize buffer
    Get iFileNum, , Buffer ' read data
    SckSendFile.SendData Buffer ' send data

    ' Show progress
    PicShowPercentage picProgress, Loc(iFileNum) / CDbl(LOF(iFileNum))
End Sub

Public Function lngMIN(ByVal l1 As Long, ByVal L2 As Long) As Long
    If l1 < L2 Then
        lngMIN = l1
    Else
        lngMIN = L2
    End If
End Function
'xxxxxxxxxxxxxxxxxxReceive file from Serverxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Private Sub cmdReceiveFiles()
    On Error GoTo myError
        Pause 100
       If Len(lblViewFileName(totalFilesSent).Caption) > 0 Then
            If totalFilesSent > totalFilesToSend Then Exit Sub
            If Len(Dir(App.Path & "\ReportFiles\" & lblViewFileName(totalFilesSent).Caption, vbNormal + vbArchive)) > 0 Then
                Kill App.Path & "\ReportFiles\" & lblViewFileName(totalFilesSent).Caption
            End If
            picView.Visible = True
            ReceiveFiles lblViewFileName(totalFilesSent).Caption
         Else
            If lblViewFileName.UBound > 0 And totalFilesSent <= totalFilesToSend Then
                totalFilesSent = totalFilesSent + 1
                cmdReceiveFiles
            End If
            
         End If
myError:
    If Err.Number > 0 Then
        MsgBox "File " & App.Path & "\ReportFiles\" & lblViewFileName(totalFilesSent).Caption & " " & Err.Description, vbInformation, "Error"
        If lblViewFileName.UBound > 0 And totalFilesSent <= totalFilesToSend Then
            totalFilesSent = totalFilesSent + 1
            cmdReceiveFiles
        End If
    End If
End Sub

Private Sub ReceiveFiles(ByVal FileName As String)
    tmrSendFile.Enabled = False
    SckReceiveFile.Close
    DoEvents
    
    'SckReceiveFile.RemoteHost = RemoteServer
    SckReceiveFile.RemoteHost = RemoteServer
    SckReceiveFile.RemotePort = RemoteServerPortReceive
    SckReceiveFile.Connect
    
    rFileName = FileName
    
End Sub

Private Sub SckReceiveFile_Close()
    'tmrSendFile.Enabled = False
    'tmrSendFile.Interval = 0
    
    Close rFileNum ' close file
    rFileNum = 0 ' set file number to 0, timer will exit if another timer event
    rFileName = ""
    FileSize = 0
    BytesReceived = 0
    SckReceiveFile.Close
    
'    PicShowPercentage Me.picView, 0
'    lblProgress.Caption = "0.00% Done"
    
    totalFilesSent = totalFilesSent + 1
    If totalFilesSent <= totalFilesToSend Then
            cmdReceiveFiles
            Exit Sub
    End If
    
    cmdEdit.Enabled = True
    totalFilesSent = 0
    totalFilesToSend = 0
    flxAcTaken.Enabled = True
    picView.Visible = False
    PicShowPercentage Me.picView, 0
    For i = 0 To lblViewFileName.UBound
        If lblViewFileName(i) <> "" Then
        lblViewFileName(i).Visible = True
        picIconViewFileName(i).Visible = True
        SHGetFileInfoA App.Path & "\ReportFiles\" & Trim(lblViewFileName(i).Caption), 0, f, leng, SHGFI_ICON Or SHGFI_SMALLICON
        DisplayIcon f.hIcon, picIconViewFileName(i), 16
        DoEvents
        'Pause 50
        End If
    Next i
    Me.MousePointer = vbNormal
End Sub
Private Sub SckReceiveFile_DataArrival(ByVal bytesTotal As Long)
    Dim sData As String, Pos As Long, Pos2 As Long
    
    SckReceiveFile.GetData sData, vbString
    If sData = "*ACCEPT*" Then
        SckReceiveFile.SendData rFileName & ":*FNa->*"
        DoEvents
        Exit Sub
    End If
    
    'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
'    If sData = ":*FileNameMiss->*" Then
'        tmrReceiveFile.Interval = 0
'        tmrReceiveFile.Enabled = False
'        rFileNum = 0 ' set file number to 0, timer will exit if another timer event
'        rFileName = ""
'        FileSize = 0
'        BytesReceived = 0
'        SckReceiveFile.Close
'        'MsgBox "File ->" & lblViewFileName(totalFilesSent).Caption & "<- Is Missing/Deleted", vbInformation, "VMissing File"
'        totalFilesSent = totalFilesSent + 1
'        If totalFilesSent <= totalFilesToSend Then
'            cmdReceiveFiles
'        End If
'
'        cmdEdit.Enabled = True
'        totalFilesSent = 0
'        totalFilesToSend = 0
'        flxAcTaken.Enabled = True
'
'        PicShowPercentage Me.picView, 0
'
'        Exit Sub
'    End If
    'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    If FileSize = 0 And InStr(1, sData, ":*FSz*") > 0 Then
        Pos = InStr(1, sData, ":*FSz*")
        FileSize = Mid(sData, 1, (Pos - 1))
        rFileNum = FreeFile
        Open App.Path & "\ReportFiles\" & rFileName For Binary Access Write Lock Write As rFileNum
        Exit Sub
    End If
    If Len(sData) > 0 And rFileNum <> 0 Then
        tmrReceiveFile.Enabled = True
        tmrReceiveFile.Interval = 1
        BytesReceived = BytesReceived + Len(sData)
        Put rFileNum, , sData
        If BytesReceived >= FileSize Then
            tmrReceiveFile.Interval = 0
            tmrReceiveFile.Enabled = False
            SckReceiveFile_Close
        End If
    End If
End Sub

Private Sub SckReceiveFile_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
    MsgBox Number & ": " & Description, vbCritical, "Error"
    If Number = 10061 Then
        totalFilesSent = totalFilesToSend
        For i = 1 To lblViewFileName.UBound
            lblViewFileName(0).Caption = ""
            lblViewFileName(0).Visible = False
            picIconViewFileName(0).Visible = False
            Unload lblViewFileName(i)
            Unload picIconViewFileName(i)
        Next i
    End If
    SckReceiveFile_Close
End Sub

Private Sub SckReceiveFile_SendComplete()
    ' can't call SendData here, so enable timer to do it...
    tmrSendFile.Enabled = False
    tmrSendFile.Interval = 1
    tmrSendFile.Enabled = True
End Sub
Private Sub tmrReceiveFile_Timer()
    Dim Buffer() As Byte, BuffSize As Long
    ' Show progress
'    lblProgress.Caption = Format(Loc(rFileNum) / CDbl(FileSize), "Percent") & " Done"
    PicShowPercentage picView, Loc(rFileNum) / CDbl(FileSize)
End Sub

'wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
Function Bool(ByVal b%) As Byte
  Bool = 0
  If b <> 0 Then Bool = 1
End Function
Sub DisplayIcon(ByVal hIcon&, ByVal Pic As PictureBox, ByVal Size As Byte)
  Pic.Cls
  If DrawIconEx(Pic.hdc, 0, 0, hIcon, Size, Size, 0, 0, DI_NORMAL) Then
     Pic.Refresh
     DestroyIcon hIcon
  Else
     MsgBox "File Missing ", vbInformation, "File Missing" '"Error getting or drawing the icon."
  End If

End Sub

Private Sub loadFileIcons(id As Integer)
   Dim i As Integer
    Pause 100
    
    Me.MousePointer = vbHourglass
    If getECDConnection = False Then
        Me.MousePointer = vbNormal
        Exit Sub
    End If
            picIconViewFileName(0).Visible = True
            lblViewFileName(0).Visible = True
    Set xRs = New ADODB.Recordset
    xRs.CursorLocation = adUseClient
    strData = "Select * From nemcReportFiles Where [fileActionCode]=" & id
    xRs.Open strData, Conn, adOpenKeyset, adLockOptimistic
    If xRs.EOF = False Then
        For i = 0 To xRs.RecordCount - 1 Step 1
            If i = 0 Then
                lblViewFileName(0).Caption = xRs!FileName
                SHGetFileInfoA App.Path & "\ReportFiles\" & Trim(xRs!FileName), 0, f, leng, SHGFI_ICON Or SHGFI_SMALLICON
                DisplayIcon f.hIcon, picIconViewFileName(i), 16
                DoEvents
                Pause 200
            Else
                Load lblViewFileName(i)
                Load picIconViewFileName(i)
                DoEvents
                lblViewFileName(i).Visible = True
                picIconViewFileName(i).Visible = True
                
                picIconViewFileName(i).Top = picIconViewFileName(i - 1).Top + 400
                lblViewFileName(i).Top = picIconViewFileName(i).Top + (picIconViewFileName(i).Height - lblViewFileName(i).Height) / 2
                lblViewFileName(i).Caption = xRs!FileName
                Pause 200
                SHGetFileInfoA App.Path & "\ReportFiles\" & Trim(xRs!FileName), 0, f, leng, SHGFI_ICON Or SHGFI_SMALLICON
                DisplayIcon f.hIcon, picIconViewFileName(i), 16
                DoEvents
            End If
            xRs.MoveNext
        Next i
     End If
        xRs.Close
        Set xRs = Nothing
        Conn.Close
        Set Conn = Nothing
Me.MousePointer = vbNormal
End Sub
