source: trunk/Packages/synapse/source/demo/sftp/Demo/Main.pas

Last change on this file was 4, checked in by chronos, 12 years ago
  • Přidáno: Zkušební stažení stránky přes https.
File size: 4.9 KB
Line 
1unit Main;
2
3interface
4
5uses
6 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
7 StdCtrls, SimpleSFTP, ComCtrls;
8
9type
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
52var
53 TestSFTPForm: TTestSFTPForm;
54
55implementation
56
57{$R *.DFM}
58
59procedure TTestSFTPForm.FormCreate(Sender: TObject);
60begin
61 FSFTP:=TSimpleSFTP.Create;
62 FFileList:=TSFTPFileList.Create;
63 FConnected:=False;
64 FAbortFlag:=False;
65end;
66
67procedure TTestSFTPForm.FormDestroy(Sender: TObject);
68begin
69 FFileList.Free;
70 FSFTP.Free;
71end;
72
73procedure TTestSFTPForm.ConnectButtonClick(Sender: TObject);
74begin
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;
91end;
92
93procedure TTestSFTPForm.SetCurrentDir(DirName:string);
94var i:Integer;
95begin
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);
101end;
102
103procedure TTestSFTPForm.ReloadButtonClick(Sender: TObject);
104begin
105 SetCurrentDir(CurrentDirEdit.Text);
106end;
107
108procedure TTestSFTPForm.FileListBoxDblClick(Sender: TObject);
109begin
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);
114end;
115
116procedure TTestSFTPForm.AbortButtonClick(Sender: TObject);
117begin
118 FAbortFlag:=True;
119end;
120
121function TTestSFTPForm.TransferProgress(UserData:Pointer;Current,Total:Int64):Boolean;
122begin
123 if Total=0 then ProgressBar.Position:=0
124 else ProgressBar.Position:=Round(Current/Total*ProgressBar.Max);
125 Application.ProcessMessages;
126 Result:=not FAbortFlag;
127end;
128
129procedure TTestSFTPForm.SendFileButtonClick(Sender: TObject);
130begin
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;
139end;
140
141procedure TTestSFTPForm.GetFileButtonClick(Sender: TObject);
142begin
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;
158end;
159
160procedure TTestSFTPForm.DeleteButtonClick(Sender: TObject);
161begin
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);
177end;
178
179end.
Note: See TracBrowser for help on using the repository browser.