| 1 | unit Main;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
|
|---|
| 7 | StdCtrls, SimpleSFTP, ComCtrls;
|
|---|
| 8 |
|
|---|
| 9 | type
|
|---|
| 10 | TTestSFTPForm = class(TForm)
|
|---|
| 11 | Label1: TLabel;
|
|---|
| 12 | Label2: TLabel;
|
|---|
| 13 | HostEdit: TEdit;
|
|---|
| 14 | PortEdit: TEdit;
|
|---|
| 15 | Label3: TLabel;
|
|---|
| 16 | LoginEdit: TEdit;
|
|---|
| 17 | Label4: TLabel;
|
|---|
| 18 | PasswordEdit: TEdit;
|
|---|
| 19 | Label5: TLabel;
|
|---|
| 20 | CurrentDirEdit: TEdit;
|
|---|
| 21 | ConnectButton: TButton;
|
|---|
| 22 | FileListBox: TListBox;
|
|---|
| 23 | DeleteButton: TButton;
|
|---|
| 24 | GetFileButton: TButton;
|
|---|
| 25 | SendFileButton: TButton;
|
|---|
| 26 | OpenDialog: TOpenDialog;
|
|---|
| 27 | SaveDialog: TSaveDialog;
|
|---|
| 28 | ReloadButton: TButton;
|
|---|
| 29 | ProgressBar: TProgressBar;
|
|---|
| 30 | AbortButton: TButton;
|
|---|
| 31 | procedure FormCreate(Sender: TObject);
|
|---|
| 32 | procedure FormDestroy(Sender: TObject);
|
|---|
| 33 | procedure ConnectButtonClick(Sender: TObject);
|
|---|
| 34 | procedure FileListBoxDblClick(Sender: TObject);
|
|---|
| 35 | procedure SendFileButtonClick(Sender: TObject);
|
|---|
| 36 | procedure GetFileButtonClick(Sender: TObject);
|
|---|
| 37 | procedure DeleteButtonClick(Sender: TObject);
|
|---|
| 38 | procedure ReloadButtonClick(Sender: TObject);
|
|---|
| 39 | procedure AbortButtonClick(Sender: TObject);
|
|---|
| 40 | private
|
|---|
| 41 | { Private declarations }
|
|---|
| 42 | FSFTP:TSimpleSFTP;
|
|---|
| 43 | FFileList:TSFTPFileList;
|
|---|
| 44 | FConnected:Boolean;
|
|---|
| 45 | FAbortFlag:Boolean;
|
|---|
| 46 | procedure SetCurrentDir(DirName:string);
|
|---|
| 47 | function TransferProgress(UserData:Pointer;Current,Total:Int64):Boolean;
|
|---|
| 48 | public
|
|---|
| 49 | { Public declarations }
|
|---|
| 50 | end;
|
|---|
| 51 |
|
|---|
| 52 | var
|
|---|
| 53 | TestSFTPForm: TTestSFTPForm;
|
|---|
| 54 |
|
|---|
| 55 | implementation
|
|---|
| 56 |
|
|---|
| 57 | {$R *.DFM}
|
|---|
| 58 |
|
|---|
| 59 | procedure TTestSFTPForm.FormCreate(Sender: TObject);
|
|---|
| 60 | begin
|
|---|
| 61 | FSFTP:=TSimpleSFTP.Create;
|
|---|
| 62 | FFileList:=TSFTPFileList.Create;
|
|---|
| 63 | FConnected:=False;
|
|---|
| 64 | FAbortFlag:=False;
|
|---|
| 65 | end;
|
|---|
| 66 |
|
|---|
| 67 | procedure TTestSFTPForm.FormDestroy(Sender: TObject);
|
|---|
| 68 | begin
|
|---|
| 69 | FFileList.Free;
|
|---|
| 70 | FSFTP.Free;
|
|---|
| 71 | end;
|
|---|
| 72 |
|
|---|
| 73 | procedure TTestSFTPForm.ConnectButtonClick(Sender: TObject);
|
|---|
| 74 | begin
|
|---|
| 75 | if FConnected then
|
|---|
| 76 | begin
|
|---|
| 77 | FSFTP.Disconnect;
|
|---|
| 78 | FConnected:=False;
|
|---|
| 79 | ConnectButton.Caption:='Connect';
|
|---|
| 80 | end
|
|---|
| 81 | else
|
|---|
| 82 | begin
|
|---|
| 83 | FFileList.Clear;
|
|---|
| 84 | FileListBox.Clear;
|
|---|
| 85 | CurrentDirEdit.Text:='';
|
|---|
| 86 | FSFTP.Connect(HostEdit.Text,PortEdit.Text,LoginEdit.Text,PasswordEdit.Text);
|
|---|
| 87 | FConnected:=True;
|
|---|
| 88 | ConnectButton.Caption:='Disconnect';
|
|---|
| 89 | SetCurrentDir('.');
|
|---|
| 90 | end;
|
|---|
| 91 | end;
|
|---|
| 92 |
|
|---|
| 93 | procedure TTestSFTPForm.SetCurrentDir(DirName:string);
|
|---|
| 94 | var i:Integer;
|
|---|
| 95 | begin
|
|---|
| 96 | CurrentDirEdit.Text:=FSFTP.SetCurrentDir(DirName);
|
|---|
| 97 | FFileList.Clear;
|
|---|
| 98 | FileListBox.Clear;
|
|---|
| 99 | FSFTP.ListDir(CurrentDirEdit.Text,FFileList);
|
|---|
| 100 | for i:=0 to FFileList.Count-1 do FileListBox.Items.Add(FFileList[i].LongName);
|
|---|
| 101 | end;
|
|---|
| 102 |
|
|---|
| 103 | procedure TTestSFTPForm.ReloadButtonClick(Sender: TObject);
|
|---|
| 104 | begin
|
|---|
| 105 | SetCurrentDir(CurrentDirEdit.Text);
|
|---|
| 106 | end;
|
|---|
| 107 |
|
|---|
| 108 | procedure TTestSFTPForm.FileListBoxDblClick(Sender: TObject);
|
|---|
| 109 | begin
|
|---|
| 110 | if FileListBox.Items.Count=0 then Exit;
|
|---|
| 111 | with FFileList[FileListBox.ItemIndex]^ do
|
|---|
| 112 | if file_type=SSH_FILEXFER_TYPE_DIRECTORY then
|
|---|
| 113 | SetCurrentDir(FileName);
|
|---|
| 114 | end;
|
|---|
| 115 |
|
|---|
| 116 | procedure TTestSFTPForm.AbortButtonClick(Sender: TObject);
|
|---|
| 117 | begin
|
|---|
| 118 | FAbortFlag:=True;
|
|---|
| 119 | end;
|
|---|
| 120 |
|
|---|
| 121 | function TTestSFTPForm.TransferProgress(UserData:Pointer;Current,Total:Int64):Boolean;
|
|---|
| 122 | begin
|
|---|
| 123 | if Total=0 then ProgressBar.Position:=0
|
|---|
| 124 | else ProgressBar.Position:=Round(Current/Total*ProgressBar.Max);
|
|---|
| 125 | Application.ProcessMessages;
|
|---|
| 126 | Result:=not FAbortFlag;
|
|---|
| 127 | end;
|
|---|
| 128 |
|
|---|
| 129 | procedure TTestSFTPForm.SendFileButtonClick(Sender: TObject);
|
|---|
| 130 | begin
|
|---|
| 131 | if OpenDialog.Execute then
|
|---|
| 132 | begin
|
|---|
| 133 | FAbortFlag:=False;
|
|---|
| 134 | TransferProgress(nil,0,0);
|
|---|
| 135 | FSFTP.PutFile(OpenDialog.FileName,CurrentDirEdit.Text,True,True,False,0,TransferProgress,nil);
|
|---|
| 136 | ShowMessage('File transfer completed '+OpenDialog.FileName);
|
|---|
| 137 | SetCurrentDir(CurrentDirEdit.Text);
|
|---|
| 138 | end;
|
|---|
| 139 | end;
|
|---|
| 140 |
|
|---|
| 141 | procedure TTestSFTPForm.GetFileButtonClick(Sender: TObject);
|
|---|
| 142 | begin
|
|---|
| 143 | if (FileListBox.Items.Count=0) or (FileListBox.ItemIndex<0) then Exit;
|
|---|
| 144 | with FFileList[FileListBox.ItemIndex]^ do
|
|---|
| 145 | if file_type=SSH_FILEXFER_TYPE_DIRECTORY then ShowMessage('Not a file')
|
|---|
| 146 | else
|
|---|
| 147 | begin
|
|---|
| 148 | SaveDialog.FileName:=FileName;
|
|---|
| 149 | if SaveDialog.Execute then
|
|---|
| 150 | begin
|
|---|
| 151 | FAbortFlag:=False;
|
|---|
| 152 | TransferProgress(nil,0,0);
|
|---|
| 153 | FSFTP.GetFile(CurrentDirEdit.Text,FileName,SaveDialog.FileName,True,True,False,0,
|
|---|
| 154 | TransferProgress,nil);
|
|---|
| 155 | ShowMessage('File transfer completed '+SaveDialog.FileName);
|
|---|
| 156 | end;
|
|---|
| 157 | end;
|
|---|
| 158 | end;
|
|---|
| 159 |
|
|---|
| 160 | procedure TTestSFTPForm.DeleteButtonClick(Sender: TObject);
|
|---|
| 161 | begin
|
|---|
| 162 | if (FileListBox.Items.Count=0) then Exit;
|
|---|
| 163 | with FFileList[FileListBox.ItemIndex]^ do
|
|---|
| 164 | begin
|
|---|
| 165 | if file_type=SSH_FILEXFER_TYPE_DIRECTORY then
|
|---|
| 166 | begin
|
|---|
| 167 | if MessageDlg('Delete dir ?',mtConfirmation,[mbOK,mbCancel],0)<>mrOK then Exit;
|
|---|
| 168 | FSFTP.DeleteDir(FileName);
|
|---|
| 169 | end
|
|---|
| 170 | else
|
|---|
| 171 | begin
|
|---|
| 172 | if MessageDlg('Delete file ?',mtConfirmation,[mbOK,mbCancel],0)<>mrOK then Exit;
|
|---|
| 173 | FSFTP.DeleteFile(FileName);
|
|---|
| 174 | end;
|
|---|
| 175 | end;
|
|---|
| 176 | SetCurrentDir(CurrentDirEdit.Text);
|
|---|
| 177 | end;
|
|---|
| 178 |
|
|---|
| 179 | end.
|
|---|