;Copyright Alexander Imka ©
#RequireAdmin
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <FTP_Ex.au3>

$serveradresse = "~~~~~"
$username = "~~~~~"
$passwort = "~~~~~"

$open = _FTPOpen("Homeworker by Alex")
$connection = _FTPConnect($open,$serveradresse,$username,$passwort)
$filelist = _FTPFilesListTo2DArray ($connection)

;GUI CODE STARTET HIER;
$Gui = GUICreate("mini FTP Client",310,500)
$header = GUICtrlCreateLabel("Datei auswählen",65,10)
$liste = GUICtrlCreateList("",20,30,200,450)
$downloadbutton = GUICtrlCreateButton("Download",235,30,55,50)
$uploadbutton = GUICtrlCreateButton("Upload",235,100,55,50)
$filedel = GUICtrlCreateButton("Delete",235,170,55,50)
$refresh = GUICtrlCreateButton("Refresh",235,240,55,50)
$rename = GUICtrlCreateButton("Rename",235,310,55,50)

For $i = 1 to UBound ($filelist) -1
	GUICtrlSetData ($liste, $filelist [$i][0]&"|")
Next

GUISetState(@SW_SHOW)
;GUI CODE ENDET HIER;

while 1
	$msg = GUIGetMsg()
	Switch $msg
		Case $GUI_EVENT_CLOSE
			_FTPClose($connection)
			_FTPUnInit()
			Exit
		Case $DownloadButton
			$DownFile = GUICtrlRead ($liste)
			If $DownFile = "" Then
				MsgBox (16, "STOP", "Du musst erst eine Datei auswählen")
			Else
			DirCreate (@ScriptDir&"\Download")
			_FTP_DownloadProgress($connection, @ScriptDir&"\Download\"&$DownFile, "/"&$DownFile)
		EndIf
	Case $uploadbutton
			$UploadPfad = FileOpenDialog("Auswählen",@ScriptDir,"Alle (*.*)")
			If $UploadPfad = "" Then
				MsgBox (0, "Achtung!", "Erst eine Datei auswählen!")
			Else
			$dateinamensplit = StringSplit($UploadPfad,"\")
			$teile = $dateinamensplit[0]
			Global $UploadVorgang = _FTP_UploadProgress($connection, $UploadPfad, "/"&$dateinamensplit[$teile])
			If $UploadVorgang = 1 Then
				MsgBox (64, "Okay", "hochladen erfolgreich")
						GUICtrlDelete($liste)
$open = _FTPOpen("Homeworker by Alex")
$connection = _FTPConnect($open,$serveradresse,$username,$passwort)
$filelist = _FTPFilesListTo2DArray ($connection,0,0x80000000)
$liste = GUICtrlCreateList("",20,30,200,450)
For $i = 1 to UBound ($filelist) -1
    GUICtrlSetData ($liste, $filelist [$i][0]&"|")
Next
			Else
				MsgBox (16, "Error", "Aufgrund eines Fehlers wird das Programm beendet!")
				Exit
			EndIf
		EndIf

 	Case $filedel
		$del = MsgBox(52,"Aktion bestätigen","Die datei " & GUICtrlRead($liste) & " wirklich löschen?")
		if $del = 6 Then
		$s_RemoteFile = GUICtrlRead($liste)
		_FTPDelFile($connection, $s_RemoteFile)
		MsgBox(64,"ok","Ok, Die Datei wurde erfolgreich gelöscht")
				GUICtrlDelete($liste)
$open = _FTPOpen("Homeworker by Alex")
$connection = _FTPConnect($open,$serveradresse,$username,$passwort)
$filelist = _FTPFilesListTo2DArray ($connection,0,0x80000000)
$liste = GUICtrlCreateList("",20,30,200,450)
For $i = 1 to UBound ($filelist) -1
    GUICtrlSetData ($liste, $filelist [$i][0]&"|")
Next
	Else
		MsgBox(16,"abbruch","Aktion abgebrochen")
	EndIf


	Case $refresh
		GUICtrlDelete($liste)
$open = _FTPOpen("Homeworker by Alex")
$connection = _FTPConnect($open,$serveradresse,$username,$passwort)
$filelist = _FTPFilesListTo2DArray ($connection,0,0x80000000)
$liste = GUICtrlCreateList("",20,30,200,450)
For $i = 1 to UBound ($filelist) -1
    GUICtrlSetData ($liste, $filelist [$i][0]&"|")
Next
MsgBox(0,"ok","Erfolgreich aktualisiert")
;~ MsgBox(64,"nönö","schon vergessen? das geht noch nit!")

	Case $rename
		$s_Existing = GUICtrlRead($liste)
		$s_New = InputBox("Rename","in welchen Namen soll die Datei umbenannt werden?",$s_Existing)
		if Not @error then
		_FTPRenameFile($connection, $s_Existing, $s_New)
		MsgBox(0,"ok","Die Datei wurde umbeannt")
		GUICtrlDelete($liste)
$open = _FTPOpen("Homeworker by Alex")
$connection = _FTPConnect($open,$serveradresse,$username,$passwort)
$filelist = _FTPFilesListTo2DArray ($connection,0,0x80000000)
$liste = GUICtrlCreateList("",20,30,200,450)
For $i = 1 to UBound ($filelist) -1
    GUICtrlSetData ($liste, $filelist [$i][0]&"|")
Next
	Else
		MsgBox(64,"oioi","Aktion abgebrochen!")
		EndIf

	EndSwitch
	WEnd
