home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue61 / System / MainForm.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2000-08-07  |  7.3 KB  |  225 lines

  1. unit MainForm;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls, Buttons, ComCtrls, WinINet, ExtCtrls, FTPReader;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     FileList: TListView;
  12.     CurrentDir: TLabel;
  13.     Panel1: TPanel;
  14.     Status: TLabel;
  15.     UpButton: TButton;
  16.     Download: TButton;
  17.     SaveDialog: TSaveDialog;
  18.     Animate1: TAnimate;
  19.     procedure FileListDblClick(Sender: TObject);
  20.     procedure UpButtonClick(Sender: TObject);
  21.     procedure FileListCompare(Sender: TObject; Item1, Item2: TListItem; Data: Integer; var Compare: Integer);
  22.     procedure FileListSelectItem(Sender: TObject; Item: TListItem; Selected: Boolean);
  23.     procedure DownloadClick(Sender: TObject);
  24.     procedure FormCreate(Sender: TObject);
  25.     procedure FormDestroy(Sender: TObject);
  26.   private
  27.     { Private declarations }
  28.     hSession: HInternet;
  29.     hFTP: HInternet;
  30.     hFind: HInternet;
  31.     ThisDir: String;
  32.     FileReader: TFTPFileReader;
  33.     procedure FileTransferComplete (Sender: TObject);
  34.     procedure FileTransferProgress (Sender: TObject);
  35.     procedure GetFileList (const Dir: String);
  36.   public
  37.     { Public declarations }
  38.   end;
  39.  
  40. var
  41.   Form1: TForm1;
  42.  
  43. implementation
  44.  
  45. {$R *.DFM}
  46.  
  47. procedure MyCallBack (hHandle: HINTERNET; Self: TForm1; dwStatus: DWord; pStatus: Pointer; dwLen: DWord); stdcall;
  48. var
  49.     ErrNum, BuffSize: DWord;
  50.     szBuff: array [0..1024] of Char;
  51. begin
  52.     BuffSize := sizeof (szBuff);
  53.     if InternetGetLastResponseInfo (ErrNum, szBuff, BuffSize) then
  54.         if (BuffSize > 0) and (szBuff [0] <> #0) then
  55.             Self.Status.Caption := szBuff;
  56. end;
  57.  
  58. procedure TForm1.FormCreate(Sender: TObject);
  59. begin
  60.     hSession := InternetOpen ('DelphiMagWinINetDemo', Internet_Open_Type_PreConfig, Nil, Nil, 0);
  61.     if hSession = Nil then begin
  62.         ShowMessage ('Can''t connect to the Internet');
  63.         Application.Terminate;
  64.     end;
  65.  
  66.     hFTP := InternetConnect (hSession, 'ftp.microsoft.com',
  67.                              Internet_Default_FTP_Port, Nil, Nil,
  68.                              Internet_Service_FTP, 0, Cardinal (Self));
  69.     if hFTP = Nil then begin
  70.         ShowMessage ('Can''t connect to ftp.microsoft.com');
  71.         Application.Terminate;
  72.     end;
  73.  
  74.     InternetSetStatusCallback (hFTP, @MyCallback);
  75.     Status.Caption := 'Connected to ftp.microsoft.com';
  76.     GetFileList (ThisDir);
  77. end;
  78.  
  79. procedure TForm1.FormDestroy(Sender: TObject);
  80. begin
  81.     if hFTP <> Nil then InternetCloseHandle (hFTP);
  82.     if hSession <> Nil then InternetCloseHandle (hSession);
  83. end;
  84.  
  85. procedure TForm1.GetFileList (const Dir: String);
  86. var
  87.     Item: TListItem;
  88.     szDirSize: Cardinal;
  89.     FileData: TWin32FindData;
  90.     szDirectory: array [0..512] of Char;
  91. begin
  92.     Screen.Cursor := crHourGlass;
  93.     try
  94.         if Dir <> '' then FtpSetCurrentDirectory (hFTP, PChar (Dir));
  95.  
  96.         // Get current directory
  97.         szDirSize := sizeof (szDirectory);
  98.         if FtpGetCurrentDirectory (hFTP, szDirectory, szDirSize) then begin
  99.             ThisDir := szDirectory;
  100.             CurrentDir.Caption := 'Current Directory = ' + szDirectory;
  101.         end;
  102.  
  103.         // The main enumeration loop
  104.         FileList.Items.Clear;
  105.         hFind := FtpFindFirstFile (hFTP, '*.*', FileData, 0, Cardinal (Self));
  106.         if hFind <> Nil then try
  107.             while True do begin
  108.                 if GetLastError = Error_No_More_Files then break;
  109.                 // We've got a file
  110.                 Item := FileList.Items.Add;
  111.                 Item.Caption := FileData.cFileName;
  112.                 // Is this a directory or a file?
  113.                 if FileData.dwFileAttributes = File_Attribute_Directory then begin
  114.                     Item.Data := Pointer (1);
  115.                     Item.SubItems.Add ('--dir--');
  116.                 end else begin
  117.                     Item.Data := Nil;
  118.                     Item.SubItems.Add (IntToStr (FileData.nFileSizeLow));
  119.                 end;
  120.  
  121.                 if not InternetFindNextFile (hFind, @FileData) then break;
  122.             end;
  123.         finally
  124.             InternetCloseHandle (hFind);
  125.         end;
  126.     finally
  127.         Screen.Cursor := crDefault;
  128.     end;
  129. end;
  130.  
  131. procedure TForm1.FileListDblClick(Sender: TObject);
  132. var
  133.     Item: TListItem;
  134.     NewDir: String;
  135. begin
  136.     Item := FileList.Selected;
  137.     // Is this a directory double-click?
  138.     if (Item <> Nil) and (Item.Data <> Nil) then begin
  139.         NewDir := ThisDir;
  140.         if NewDir = '' then NewDir := '/';
  141.         if NewDir [Length (NewDir)] <> '/' then NewDir := NewDir + '/';
  142.         ThisDir := NewDir + Item.Caption;
  143.         GetFileList (ThisDir);
  144.     end;
  145.  
  146.     // Is this a file double-click
  147.     if (Item <> Nil) and (Item.Data = Nil) then DownloadClick (Sender);
  148. end;
  149.  
  150. procedure TForm1.UpButtonClick(Sender: TObject);
  151. var
  152.     p: PChar;
  153.     NewDir: array [0..512] of Char;
  154. begin
  155.     if (ThisDir <> '') and (ThisDir <> '/') then begin
  156.         StrPCopy (NewDir, ThisDir);
  157.         p := StrRScan (NewDir, '/');
  158.         if p <> Nil then begin
  159.             p^ := #0;
  160.             ThisDir := NewDir;
  161.             if ThisDir = '' then ThisDir := '/';
  162.             GetFileList (ThisDir);
  163.         end;
  164.     end;
  165. end;
  166.  
  167. procedure TForm1.FileListCompare(Sender: TObject; Item1, Item2: TListItem; Data: Integer; var Compare: Integer);
  168. begin
  169.     Compare := Ord (Ord (Item1.Data <> Nil) < Ord (Item2.Data <> Nil));
  170. end;
  171.  
  172. procedure TForm1.FileListSelectItem (Sender: TObject; Item: TListItem; Selected: Boolean);
  173. begin
  174.     if Selected then Download.Enabled := Item.Data = Nil
  175.     else Download.Enabled := False;
  176. end;
  177.  
  178. procedure TForm1.FileTransferComplete (Sender: TObject);
  179. begin
  180.     Status.Caption := 'File Transfer Complete: Result = ' + FileReader.CompletionString;
  181.     FileReader.Free;  FileReader := Nil;
  182.     Screen.Cursor := crDefault;
  183.     Enabled := True;
  184.     MessageBeep (0);
  185. end;
  186.  
  187. procedure TForm1.FileTransferProgress (Sender: TObject);
  188. begin
  189.     Status.Caption := 'Bytes transferred: ' + IntToStr (FileReader.TotalBytesRead);
  190.     if FileReader.FileSize <> 0 then Status.Caption := Status.Caption + ' out of ' + IntToStr (FileReader.FileSize) + ' bytes';
  191. end;
  192.  
  193. procedure TForm1.DownloadClick(Sender: TObject);
  194. var
  195.     Item: TListItem;
  196.     DirName: String;
  197. begin
  198.     Item := FileList.Selected;
  199.     // Double-check...
  200.     if (Item <> Nil) and (Item.Data = Nil) then begin
  201.         DirName := Copy (CurrentDir.Caption, 21, MaxInt);
  202.         if MessageDlg ('Download ' + Item.Caption + ' from directory "' + DirName + '" ?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then begin
  203.             SaveDialog.FileName := Item.Caption;
  204.             if SaveDialog.Execute then begin
  205.                 Screen.Cursor := crHourGlass;
  206.                 Enabled := False;
  207.                 FileReader := TFTPFileReader.Create;
  208.                 FileReader.SourceFileName := Item.Caption;
  209.                 FileReader.DestFileName := SaveDialog.FileName;
  210.                 FileReader.ServerName := 'ftp.microsoft.com';
  211.                 FileReader.NetConnection := hSession;
  212.                 FileReader.FTPSession := hFTP;
  213.                 FileReader.OnCompletion := FileTransferComplete;
  214.                 FileReader.OnProgress := FileTransferProgress;
  215.                 FileReader.Execute;
  216.             end;
  217.         end;
  218.     end;
  219. end;
  220.  
  221. end.
  222.  
  223.  
  224.  
  225.