Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.76 KB | None | 0 0
  1. { $HDR$}
  2.  
  3. unit mainf;
  4.  
  5. interface
  6.  
  7. uses
  8. Windows, Messages, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, ComCtrls,
  9. Menus, SysUtils, Classes, IdIntercept, IdBaseComponent, IdComponent, IdTCPConnection,
  10. IdTCPClient, IdFTP, IdAntiFreezeBase, IdAntiFreeze, IdLogBase, IdLogDebug, IdGlobal,
  11. IdLogEvent, IdFTPCommon, IdFTPList, FileCtrl;
  12.  
  13. type
  14. TMainForm = class(TForm)
  15. DirectoryListBox: TListBox;
  16. IdFTP1: TIdFTP;
  17. DebugListBox: TListBox;
  18. UploadOpenDialog1: TOpenDialog;
  19. SaveDialog1: TSaveDialog;
  20. StatusBar1: TStatusBar;
  21. IdAntiFreeze1: TIdAntiFreeze;
  22. ProgressBar1: TProgressBar;
  23. PopupMenu1: TPopupMenu;
  24. Download1: TMenuItem;
  25. Upload1: TMenuItem;
  26. Delete1: TMenuItem;
  27. N1: TMenuItem;
  28. Back1: TMenuItem;
  29. IdLogEvent1: TIdLogEvent;
  30. HeaderControl1: THeaderControl;
  31. CurrentDirEdit: TEdit;
  32. ChDirButton: TButton;
  33. CreateDirButton: TButton;
  34. Label4: TLabel;
  35. Label1: TLabel;
  36. FtpServerEdit: TEdit;
  37. Label2: TLabel;
  38. UserIDEdit: TEdit;
  39. PasswordEdit: TEdit;
  40. Label3: TLabel;
  41. ConnectButton: TButton;
  42. TraceCheckBox: TCheckBox;
  43. UsePassive: TCheckBox;
  44. DownloadButton: TButton;
  45. UploadButton: TButton;
  46. DeleteButton: TButton;
  47. BackButton: TButton;
  48. AbortButton: TButton;
  49. procedure ConnectButtonClick(Sender: TObject);
  50. procedure UploadButtonClick(Sender: TObject);
  51. procedure DirectoryListBoxDblClick(Sender: TObject);
  52. procedure DeleteButtonClick(Sender: TObject);
  53. procedure IdFTP1Disconnected(Sender: TObject);
  54. procedure AbortButtonClick(Sender: TObject);
  55. procedure BackButtonClick(Sender: TObject);
  56. procedure IdFTP1Status(axSender: TObject; const axStatus: TIdStatus;
  57. const asStatusText: String);
  58. procedure TraceCheckBoxClick(Sender: TObject);
  59. procedure FormCreate(Sender: TObject);
  60. procedure DirectoryListBoxClick(Sender: TObject);
  61. procedure IdFTP1Work(Sender: TObject; AWorkMode: TWorkMode;
  62. const AWorkCount: Integer);
  63. procedure IdFTP1WorkBegin(Sender: TObject; AWorkMode: TWorkMode;
  64. const AWorkCountMax: Integer);
  65. procedure IdFTP1WorkEnd(Sender: TObject; AWorkMode: TWorkMode);
  66. procedure UsePassiveClick(Sender: TObject);
  67. procedure ChDirButtonClick(Sender: TObject);
  68. procedure CreateDirButtonClick(Sender: TObject);
  69. procedure IdLogEvent1Received(ASender: TComponent; const AText,
  70. AData: String);
  71. procedure IdLogEvent1Sent(ASender: TComponent; const AText,
  72. AData: String);
  73. procedure DebugListBoxDrawItem(Control: TWinControl; Index: Integer;
  74. Rect: TRect; State: TOwnerDrawState);
  75. procedure DirectoryListBoxDrawItem(Control: TWinControl;
  76. Index: Integer; Rect: TRect; State: TOwnerDrawState);
  77. procedure HeaderControl1SectionResize(HeaderControl: THeaderControl;
  78. Section: THeaderSection);
  79. procedure DebugListBoxClick(Sender: TObject);
  80. private
  81. { Private declarations }
  82. AbortTransfer: Boolean;
  83. TransferrignData: Boolean;
  84. BytesToTransfer: LongWord;
  85. STime: TDateTime;
  86. procedure ChageDir(DirName: string);
  87. procedure PutToDebugLog(Operation, S1: string);
  88. public
  89. { Public declarations }
  90. end;
  91.  
  92. var
  93. MainForm: TMainForm;
  94.  
  95. implementation
  96.  
  97. {$R *.dfm}
  98.  
  99. var
  100. AverageSpeed: Double=0;
  101.  
  102. procedure TMainForm.ConnectButtonClick(Sender: TObject);
  103. begin
  104. ConnectButton.Enabled:=false;
  105. if IdFTP1.Connected
  106. then
  107. try
  108. if TransferrignData
  109. then IdFTP1.Abort;
  110. IdFTP1.Quit;
  111. finally
  112. CurrentDirEdit.Text:='/';
  113. DirectoryListBox.Items.Clear;
  114. ConnectButton.Caption:='Connect';
  115. ConnectButton.Enabled:=true;
  116. ConnectButton.Default:=true;
  117. end
  118. else
  119. with IdFTP1 do try
  120. Username:=UserIDEdit.Text;
  121. Password:=PasswordEdit.Text;
  122. Host:=FtpServerEdit.Text;
  123. Connect;
  124. Self.ChageDir(CurrentDirEdit.Text);
  125. finally
  126. ConnectButton.Enabled:=true;
  127. if Connected
  128. then
  129. begin
  130. ConnectButton.Caption:='Disconnect';
  131. ConnectButton.Default:=false;
  132. end;
  133. end;
  134. end;
  135.  
  136. procedure TMainForm.UploadButtonClick(Sender: TObject);
  137. begin
  138. if IdFTP1.Connected
  139. then
  140. begin
  141. if UploadOpenDialog1.Execute
  142. then
  143. try
  144. IdFTP1.TransferType:=ftBinary;
  145. IdFTP1.Put(UploadOpenDialog1.FileName, ExtractFileName(UploadOpenDialog1.FileName));
  146. ChageDir(idftp1.RetrieveCurrentDir);
  147. finally
  148.  
  149. end;
  150. end;
  151. end;
  152.  
  153. procedure TMainForm.ChageDir(DirName: string);
  154. var
  155. LS: TStringList;
  156. begin
  157. LS:=TStringList.Create;
  158. try
  159. IdFTP1.ChangeDir(DirName);
  160. IdFTP1.TransferType:=ftASCII;
  161. CurrentDirEdit.Text:=IdFTP1.RetrieveCurrentDir;
  162. DirectoryListBox.Items.Clear;
  163. IdFTP1.List(LS);
  164. DirectoryListBox.Items.Assign(LS);
  165. if DirectoryListBox.Items.Count>0
  166. then
  167. if AnsiPos('total', DirectoryListBox.Items[0])>0
  168. then DirectoryListBox.Items.Delete(0);
  169. finally
  170. LS.Free;
  171. end;
  172. end;
  173.  
  174. procedure TMainForm.DirectoryListBoxDblClick(Sender: TObject);
  175. var
  176. Name: string;
  177. begin
  178. if not IdFTP1.Connected
  179. then Exit;
  180. Name:=IdFTP1.DirectoryListing.Items[DirectoryListBox.ItemIndex].FileName;
  181. if IdFTP1.DirectoryListing.Items[DirectoryListBox.ItemIndex].ItemType=ditDirectory
  182. then
  183. begin
  184. // Change directory
  185. ChageDir(Name);
  186. end
  187. else
  188. begin
  189. try
  190. SaveDialog1.FileName:=Name;
  191. if SaveDialog1.Execute
  192. then
  193. begin
  194. IdFTP1.TransferType:=ftBinary;
  195. BytesToTransfer:=IdFTP1.Size(Name);
  196. if FileExists(Name)
  197. then
  198. begin
  199. case MessageDlg('File aready exists. Do you want to resume the download operation?',
  200. mtConfirmation, mbYesNoCancel, 0) of
  201. mrYes:
  202. begin
  203. BytesToTransfer:=BytesToTransfer-FileSizeByName(Name);
  204. IdFTP1.Get(Name, SaveDialog1.FileName, false, true);
  205. end;
  206. mrNo:
  207. begin
  208. IdFTP1.Get(Name, SaveDialog1.FileName, true);
  209. end;
  210. mrCancel:
  211. begin
  212. Exit;
  213. end;
  214. end;
  215. end
  216. else IdFTP1.Get(Name, SaveDialog1.FileName, false);
  217. end;
  218. finally
  219.  
  220. end;
  221. end;
  222. end;
  223.  
  224. procedure TMainForm.DeleteButtonClick(Sender: TObject);
  225. var
  226. Name: string;
  227. begin
  228. if not IdFTP1.Connected
  229. then Exit;
  230. Name:=IdFTP1.DirectoryListing.Items[DirectoryListBox.ItemIndex].FileName;
  231. if IdFTP1.DirectoryListing.Items[DirectoryListBox.ItemIndex].ItemType=ditDirectory
  232. then
  233. try
  234. idftp1.RemoveDir(Name);
  235. ChageDir(idftp1.RetrieveCurrentDir);
  236. finally
  237.  
  238. end
  239. else
  240. try
  241. idftp1.Delete(Name);
  242. ChageDir(idftp1.RetrieveCurrentDir);
  243. finally
  244.  
  245. end;
  246. end;
  247.  
  248. procedure TMainForm.IdFTP1Disconnected(Sender: TObject);
  249. begin
  250. StatusBar1.Panels[1].Text:='Disconnected.';
  251. end;
  252.  
  253. procedure TMainForm.AbortButtonClick(Sender: TObject);
  254. begin
  255. AbortTransfer:=true;
  256. end;
  257.  
  258. procedure TMainForm.BackButtonClick(Sender: TObject);
  259. begin
  260. if not IdFTP1.Connected
  261. then Exit;
  262. try
  263. ChageDir('..');
  264. finally
  265.  
  266. end;
  267. end;
  268.  
  269. procedure TMainForm.IdFTP1Status(axSender: TObject; const axStatus: TIdStatus;
  270. const asStatusText: string);
  271. begin
  272. DebugListBox.ItemIndex:=DebugListBox.Items.Add(asStatusText);
  273. StatusBar1.Panels[1].Text:=asStatusText;
  274. end;
  275.  
  276. procedure TMainForm.TraceCheckBoxClick(Sender: TObject);
  277. begin
  278. if TraceCheckBox.Checked
  279. then IdFtp1.Intercept:=IdLogEvent1
  280. else IdFtp1.Intercept:=nil;
  281. DebugListBox.Visible:=TraceCheckBox.Checked;
  282. end;
  283.  
  284. procedure TMainForm.FormCreate(Sender: TObject);
  285. begin
  286. IdFtp1.Intercept:=IdLogEvent1;
  287. end;
  288.  
  289. procedure TMainForm.DirectoryListBoxClick(Sender: TObject);
  290. begin
  291. if not IdFTP1.Connected
  292. then Exit;
  293. if DirectoryListBox.ItemIndex>-1
  294. then
  295. begin
  296. if IdFTP1.DirectoryListing.Items[DirectoryListBox.ItemIndex].ItemType=ditDirectory
  297. then DownloadButton.Caption:='Change dir'
  298. else DownloadButton.Caption:='Download';
  299. end;
  300. end;
  301.  
  302. procedure TMainForm.IdFTP1Work(Sender: TObject; AWorkMode: TWorkMode;
  303. const AWorkCount: Integer);
  304. var
  305. S: string;
  306. TotalTime: TDateTime;
  307. // RemainingTime: TDateTime;
  308. H, M, Sec, MS: Word;
  309. DLTime: Double;
  310. begin
  311. TotalTime:=Now-STime;
  312. DecodeTime(TotalTime, H, M, Sec, MS);
  313. Sec:=Sec+M*60+H*3600;
  314. DLTime:=Sec+MS/1000;
  315. if DLTime>0
  316. then AverageSpeed:={(AverageSpeed + }(AWorkCount/1024)/DLTime{)/2};
  317. if AverageSpeed>0
  318. then
  319. begin
  320. Sec:=Trunc(((ProgressBar1.Max-AWorkCount)/1024/AverageSpeed));
  321. S:=Format('%2d:%2d:%2d', [Sec div 3600, (Sec div 60) mod 60, Sec mod 60]);
  322. S:='Time remaining '+S;
  323. end
  324. else S:='';
  325. //
  326. S:=FormatFloat('0.00 KB/s', AverageSpeed)+'; '+S;
  327. case AWorkMode of
  328. wmRead: StatusBar1.Panels[1].Text:='Download speed '+S;
  329. wmWrite: StatusBar1.Panels[1].Text:='Uploade speed '+S;
  330. end;
  331. if AbortTransfer
  332. then IdFTP1.Abort;
  333. ProgressBar1.Position:=AWorkCount;
  334. AbortTransfer:=false;
  335. end;
  336.  
  337. procedure TMainForm.IdFTP1WorkBegin(Sender: TObject; AWorkMode: TWorkMode;
  338. const AWorkCountMax: Integer);
  339. begin
  340. TransferrignData:=true;
  341. AbortButton.Visible:=true;
  342. AbortTransfer:=false;
  343. STime:=Now;
  344. if AWorkCountMax>0
  345. then ProgressBar1.Max:=AWorkCountMax
  346. else ProgressBar1.Max:=BytesToTransfer;
  347. AverageSpeed:=0;
  348. end;
  349.  
  350. procedure TMainForm.IdFTP1WorkEnd(Sender: TObject; AWorkMode: TWorkMode);
  351. begin
  352. AbortButton.Visible:=false;
  353. StatusBar1.Panels[1].Text:='Transfer complete.';
  354. BytesToTransfer:=0;
  355. TransferrignData:=false;
  356. ProgressBar1.Position:=0;
  357. AverageSpeed:=0;
  358. end;
  359.  
  360. procedure TMainForm.UsePassiveClick(Sender: TObject);
  361. begin
  362. IdFTP1.Passive:=UsePassive.Checked;
  363. end;
  364.  
  365. procedure TMainForm.ChDirButtonClick(Sender: TObject);
  366. begin
  367. ChageDir(CurrentDirEdit.Text);
  368. end;
  369.  
  370. procedure TMainForm.CreateDirButtonClick(Sender: TObject);
  371. var
  372. S: string;
  373. begin
  374. S:=InputBox('Make new directory', 'Name', '');
  375. if S<>''
  376. then
  377. try
  378. IdFTP1.MakeDir(S);
  379. ChageDir(CurrentDirEdit.Text);
  380. finally
  381.  
  382. end;
  383. end;
  384.  
  385. procedure TMainForm.PutToDebugLog(Operation, S1: String);
  386. var
  387. S: string;
  388. begin
  389. while Length(S1)>0 do
  390. begin
  391. if Pos(#13, S1)>0
  392. then
  393. begin
  394. S:=Copy(S1, 1, Pos(#13, S1)-1);
  395. Delete(S1, 1, Pos(#13, S1));
  396. if S1[1]=#10
  397. then Delete(S1, 1, 1);
  398. end
  399. else S := S1;
  400. DebugListBox.ItemIndex := DebugListBox.Items.Add(Operation + S);
  401. end;
  402. end;
  403.  
  404. procedure TMainForm.IdLogEvent1Received(ASender: TComponent; const AText,
  405. AData: string);
  406. begin
  407. PutToDebugLog('<<- ', AData);
  408. end;
  409.  
  410. procedure TMainForm.IdLogEvent1Sent(ASender: TComponent; const AText,
  411. AData: string);
  412. begin
  413. PutToDebugLog('->> ', AData);
  414. end;
  415.  
  416. {$IFDEF Linux}
  417. procedure TMainForm.DebugListBoxDrawItem(Sender: TObject; Index: Integer;
  418. Rect: TRect; State: TOwnerDrawState; var Handled: Boolean);
  419. {$ELSE}
  420. procedure TMainForm.DebugListBoxDrawItem(Control: TWinControl;
  421. Index: Integer; Rect: TRect; State: TOwnerDrawState);
  422. {$ENDIF}
  423. begin
  424. if Pos('>>', DebugListBox.Items[index])>1
  425. then DebugListBox.Canvas.Font.Color:=clRed
  426. else DebugListBox.Canvas.Font.Color:=clBlue;
  427. if odSelected in State
  428. then
  429. begin
  430. DebugListBox.Canvas.Brush.Color:=$00895F0A;
  431. DebugListBox.Canvas.Font.Color:=clWhite;
  432. end
  433. else DebugListBox.Canvas.Brush.Color := clWindow;
  434. DebugListBox.Canvas.FillRect(Rect);
  435. DebugListBox.Canvas.TextOut(Rect.Left, Rect.Top, DebugListBox.Items[index]);
  436. end;
  437.  
  438. {$IFDEF Linux}
  439. procedure TMainForm.DirectoryListBoxDrawItem(Sender: TObject; Index: Integer;
  440. Rect: TRect; State: TOwnerDrawState; var Handled: Boolean);
  441. {$ELSE}
  442. procedure TMainForm.DirectoryListBoxDrawItem(Control: TWinControl;
  443. Index: Integer; Rect: TRect; State: TOwnerDrawState);
  444. {$ENDIF}
  445. var
  446. R: TRect;
  447. begin
  448. if odSelected in State
  449. then
  450. begin
  451. DirectoryListBox.Canvas.Brush.Color:=$00895F0A;
  452. DirectoryListBox.Canvas.Font.Color:=clWhite;
  453. end
  454. else DirectoryListBox.Canvas.Brush.Color:=clWindow;
  455. //
  456. if Assigned(IdFTP1.DirectoryListing) and (IdFTP1.DirectoryListing.Count>Index)
  457. then
  458. begin
  459. DirectoryListBox.Canvas.FillRect(Rect);
  460. with IdFTP1.DirectoryListing.Items[Index] do
  461. begin
  462. DirectoryListBox.Canvas.TextOut(Rect.Left, Rect.Top, FileName);
  463. R:=Rect;
  464. R.Left:=Rect.Left+HeaderControl1.Sections.Items[0].Width;
  465. R.Right:=R.Left+HeaderControl1.Sections.Items[1].Width;
  466. DirectoryListBox.Canvas.FillRect(R);
  467. DirectoryListBox.Canvas.TextOut(R.Left, Rect.Top, IntToStr(Size));
  468.  
  469. R.Left:=R.Right;
  470. R.Right:=R.Left+HeaderControl1.Sections.Items[2].Width;
  471. DirectoryListBox.Canvas.FillRect(R);
  472.  
  473. if ItemType=ditDirectory
  474. then DirectoryListBox.Canvas.TextOut(R.Left, Rect.Top, 'Directory')
  475. else DirectoryListBox.Canvas.TextOut(R.Left, Rect.Top, 'File');
  476.  
  477. R.Left:=R.Right;
  478. R.Right:=R.Left+HeaderControl1.Sections.Items[3].Width;
  479. DirectoryListBox.Canvas.FillRect(R);
  480. DirectoryListBox.Canvas.TextOut(R.Left, Rect.Top, FormatDateTime('mm/dd/yyyy hh:mm', ModifiedDate));
  481.  
  482. R.Left:=R.Right;
  483. R.Right:=R.Left+HeaderControl1.Sections.Items[4].Width;
  484. DirectoryListBox.Canvas.FillRect(R);
  485. DirectoryListBox.Canvas.TextOut(R.Left, Rect.Top, GroupName);
  486.  
  487. R.Left:=R.Right;
  488. R.Right:=R.Left+HeaderControl1.Sections.Items[5].Width;
  489. DirectoryListBox.Canvas.FillRect(R);
  490. DirectoryListBox.Canvas.TextOut(R.Left, Rect.Top, OwnerName);
  491.  
  492. R.Left:=R.Right;
  493. R.Right:=R.Left+HeaderControl1.Sections.Items[6].Width;
  494. DirectoryListBox.Canvas.FillRect(R);
  495. DirectoryListBox.Canvas.TextOut(R.Left, Rect.Top, OwnerPermissions + GroupPermissions + UserPermissions);
  496. end;
  497. end;
  498. end;
  499.  
  500. {$IFDEF Linux}
  501. procedure TMainForm.HeaderControl1SectionResize(HeaderControl: TCustomHeaderControl;
  502. Section: TCustomHeaderSection);
  503. {$ELSE}
  504. procedure TMainForm.HeaderControl1SectionResize(HeaderControl: THeaderControl; Section: THeaderSection);
  505. {$ENDIF}
  506. begin
  507. DirectoryListBox.Repaint;
  508. end;
  509.  
  510. procedure TMainForm.DebugListBoxClick(Sender: TObject);
  511. begin
  512.  
  513. end;
  514.  
  515. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement