; ******************************************************************************
; **::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::**
; **::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::**
; **::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::**
; **      __          _   _____        _                                      **
; **     / _|        | | |  __ \      | |                                     **
; **    | |_ __ _ ___| |_| |  | | __ _| |_ __ _                               **
; **    |  _/ _` / __| __| |  | |/ _` | __/ _` |                              **
; **    | || (_| \__ \ |_| |__| | (_| | || (_| |                              **
; **    |_| \__,_|___/\__|_____/ \__,_|\__\__,_|                              **
; **                                                                          **
; **::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::**
; **::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::**
; **::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::**
; ******************************************************************************

#region includes
#include <Array.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <File.au3>
#include <FTPEx.au3>
#include <GUIConstantsEx.au3>
#include <GuiImageList.au3>
#include <GuiListView.au3>
#include <String.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#endregion includes

#region GUI
$oMain         = GUICreate( "fastData v0.13", 322, 303, Default, Default, -1, BitOR( $WS_EX_ACCEPTFILES, $WS_EX_WINDOWEDGE ) )
$iListView     = GUICtrlCreateListView( "", 8, 8, 305, 200 )
$sFile         = GUICtrlCreateInput( "", 8, 216, 305, 21 )
                 GUICtrlSetState( -1, $GUI_DROPACCEPTED )
$iBtn1         = GUICtrlCreateButton( "Upload!", 8, 245, 147, 21 )
$iBtn2         = GUICtrlCreateButton( "Download!", 166, 245, 147, 21 )
$iBtn3         = GUICtrlCreateButton( "Delete!", 8, 274, 305, 21 )
GUISetState(@SW_SHOW)
#endregion GUI

#region declaration
$sRemote        = "/html/verzeichnisebene1/verzeichnisebene2/fastData/"                        ; the specific directory for upload, download, delete
$sLocal         = @DesktopDir & "\"                                                            ; the directory for the downloaded files
$sServer        = "website.com"                                                                ; e.g. website.com (have to be edited)
$sUsername      = "username"                                                                ; e.g. username (have to be edited)
$sPass          = "pwd"                                                                        ; e.g. pwd (have to be edited)
#endregion declaration

#region init
Func _init()
    ; open a FTP-Session
    Global $hForCon = _FTP_Open( "FTP" )
    If $hForCon = 0 Then
        MsgBox( 16, "Error", "FTP-Session konnte nicht geöffnet werden." )
        Exit
    EndIf

    ; open a FTP-Connection
    Global $con = _FTP_Connect( $hForCon, $sServer, $sUsername, $sPass, 1 )            
    If $con = 0 Then
        MsgBox( 16, "Error", "Es konnte keine Verbindung aufgebaut werden." )
        _FTP_Close( $hForCon )
        Exit
    EndIf

    ; set the specific path on the FTP-Environment
    $iSetPath = _FTP_DirSetCurrent( $con, $sRemote )
    If $iSetPath = 0 Then
        MsgBox( 16, "Error", "Es konnte nicht zum Verzeichnis ""fastData"" navigiert werden." )
        _FTP_Close( $hForCon )
        Exit
    EndIf

    ; reset the ListView content
    _GUICtrlListView_DeleteColumn( $iListView, 0 )
    _GUICtrlListView_DeleteAllItems( GUICtrlGetHandle( $iListView ) )

    ; create a file list of the specific path
    $aList = _FTP_ListToArray( $con, 2, $INTERNET_FLAG_RELOAD )                            ; don't remove the flag (reload)

    ; fill the ListView with the array (with the file list)
    _GUICtrlListView_AddColumn( $iListView, "Dateien zum Download", 301 )
    $iCount = $aList[0]
    If $aList[0] <> 0 Then
        Do
            _GUICtrlListView_AddItem( $iListView, $aList[$iCount] )
            $iCount = $iCount - 1
        Until $iCount = 0
    EndIf
EndFunc

; call this function
_init()
#endregion init

#region events
While 1
    $eMsg = GUIGetMsg()
    Switch $eMsg
        Case $GUI_EVENT_CLOSE
            If MsgBox( 262144 + 308, "Hinweis", "Soll das Programm tatsächlich geschlossen werden?" ) = 6 Then
                _FTP_Close( $hForCon )
                Exit
            EndIf
        Case $iBtn1
            _ftpUpload()
        Case $iBtn2
            _ftpDownload()
        Case $iBtn3
            _ftpDelete()
    EndSwitch
WEnd
#endregion events

#region functions
Func _ftpUpload()
    If GUICtrlRead( $sFile ) = "" Then
        MsgBox( 48, "Hinweis", "Es wurde nichts in die Inputbox eingegeben bzw. keine Datei wurde hineingezogen." )
        Return
    EndIf

    ; get just the file without the path
    $sReverseFile = _StringReverse( GUICtrlRead( $sFile ) )
    $aFilename  = _StringBetween( $sReverseFile, "", "\" )
    $sFilename1   = _StringReverse( $aFilename[0] )
    $sOnServer    = $sRemote & $sFilename1

    ; upload
    $iUpload = _FTP_FilePut( $con, GUICtrlRead( $sFile ), $sOnServer )
    If $iUpload = 0 Then
        MsgBox( 16, "Error", "Fehler beim Upload!" )
        _FTP_Close( $hForCon )
        Exit
    EndIf
    MsgBox( 64, "Information", "Datei """ & $sFilename1 & """ ist erfolgreich hochgeladen." )

    ; reset and renew
    GUICtrlSetData( $sFile, "" )
    _init()
EndFunc

Func _ftpDownload()
    ; download just the selected one
    $aSelectedItem = _GUICtrlListView_GetSelectedIndices( $iListView, True )
    If $aSelectedItem[0] <> 0 Then
        $sFilename2     = _GUICtrlListView_GetItemTextString( $iListView, $aSelectedItem[1] )
        $sFilename2     = StringReplace( $sFilename2, "|", "" )

        ; download
        $iDownload  = _FTP_FileGet( $con, $sRemote & $sFilename2, $sLocal & $sFilename2, False, 0, $INTERNET_FLAG_RELOAD )
        If $iDownload = 0 Then
            MsgBox( 16, "Error", "Fehler beim Download!" )
            Return
        EndIf

        MsgBox( 64, "Information", "Datei """ & $sFilename2 & """ ist erfolgreich heruntergeladen (siehe Desktop)." )
    Else
        MsgBox( 48, "Hinweis", "Es wurde nichts zum Download ausgewählt." )
    EndIf
EndFunc

Func _ftpDelete()
    ; delete just the selected one
    $aSelectedItem = _GUICtrlListView_GetSelectedIndices( $iListView, True )
    If $aSelectedItem[0] <> 0 Then
        $sFilename2     = _GUICtrlListView_GetItemTextString( $iListView, $aSelectedItem[1] )
        $sFilename2     = StringReplace( $sFilename2, "|", "" )

        ; delete
        $iDel = _FTP_FileDelete( $con, $sRemote & $sFilename2 )
        If $iDel = 0 Then
            MsgBox( 16, "Error", "Fehler beim Löschen!" )
            Return
        EndIf

        MsgBox( 64, "Information", "Datei """ & $sFilename2 & """ ist erfolgreich gelöscht" )
    Else
        MsgBox( 48, "Hinweis", "Es wurde nichts zum Löschen ausgewählt." )
    EndIf

    ; reset and renew
    _init()
EndFunc
#endregion functions

; ----------------------------------- EOF --------------------------------------