Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

(Ebook - Pdf) Kick Ass Delphi Programming

.pdf
Скачиваний:
284
Добавлен:
17.08.2013
Размер:
5.02 Mб
Скачать

procedure TMainForm.ConnectBtnClick(Sender: TObject); var

Counter : Integer; begin

Shopper1.HostName := HostInput.Text;{RemoteHostName;}

{rest of code } Shopper1.Start;

{more code not shown here } end;

FIGURE 5.3 The finished FTP client application, FTPCLI

Listing 5.1 The FTP client demo main program unit

(*

Developed for KickAss Delphi Programming by John Penman

Ftp Client demo program

Copyright (c) 1996 The Coriolis Group, Inc. Last Updated 7/5/96

*)

unit Main;

interface

uses

SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, Forms, Dialogs, About, MakeDir, StdCtrls, Wsock, Shopper, ExtCtrls, FileCtrl;

type

TMainForm = class(TForm) ConnectBtn: TButton; AboutBtn: TButton; DisConBtn: TButton; ExitBtn: TButton; RemFilesList: TListBox; ViewBtn: TButton; MkDirBtn: TButton; CancelBtn: TButton; DelBtn: TButton; StatusMemo: TMemo; RefreshBtn: TButton; Progress: TEdit; Label1: TLabel; HelpBtn: TButton;

BinaryRBtn: TRadioButton;

AsciiRBtn: TRadioButton; LocalFileList: TFileListBox; LocalDirList: TDirectoryListBox; LocalDrive: TDriveComboBox; RemoteHostP: TPanel;

RemoteIPP: TPanel;

LocalHostP: TPanel;

Shopper1: TShopper;

AbortBtn: TButton;

HostInput: TEdit;

UserInput: TEdit; PassWordInput: TEdit; Label2: TLabel; Label3: TLabel; Label4: TLabel; SiteBtn: TButton;

procedure ConnectBtnClick(Sender: TObject); procedure AboutBtnClick(Sender: TObject); procedure DisConBtnClick(Sender: TObject); procedure ExitBtnClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure ViewBtnClick(Sender: TObject); procedure MkDirBtnClick(Sender: TObject); procedure Shopper1Change(Sender: TObject); procedure RemFilesListClick(Sender: TObject);

procedure RemFilesListDblClick(Sender: TObject); procedure RefreshBtnClick(Sender: TObject); procedure Shopper1UpDateProgress(Sender: TObject); procedure Shopper1UpDateList(Sender: TObject); procedure HelpBtnClick(Sender: TObject);

procedure LocalFileListChange(Sender: TObject); procedure LocalDriveChange(Sender: TObject); procedure LocalFileListDblClick(Sender: TObject); procedure LocalDirListChange(Sender: TObject); procedure Shopper1UpdateFileType(Sender: TObject); procedure Shopper1UpDateTimeTaken(Sender: TObject); procedure Shopper1UpDateBusyFlag(Sender: TObject); procedure DelBtnClick(Sender: TObject);

procedure AbortBtnClick(Sender: TObject); procedure CancelBtnClick(Sender: TObject); procedure Shopper1ChangeInfo(Sender: TObject); procedure SiteBtnClick(Sender: TObject);

private

{Private declarations } public

{Public declarations }

end; var

MainForm: TMainForm;

implementation

{$R *.DFM}

procedure TMainForm.ConnectBtnClick(Sender: TObject);

var

 

 

Counter : Integer;

 

begin

 

Shopper1.HostName

:= HostInput.Text;{RemoteHostName;}

if

Shopper1.Status = Success then

begin

 

RemoteHostP.Caption :=

 

Concat('Remote host : ',HostInput.Text);

RemoteIPP.Caption

:=

 

Concat('Remote IP

: ',Shopper1.HostName);

with Shopper1 do

 

begin

 

 

UserName := UserInput.Text;

 

Password := PasswordInput.Text;

end;

 

Shopper1.Start;

 

if Shopper1.Status = Success then

begin

 

 

ConnectBtn.Enabled

:= FALSE;

 

DisConBtn.Enabled

:= TRUE;

 

if Shopper1.Connected then

 

begin

 

 

RemFilesList.Clear;{951030JCP}

 

{populate the remote files list box}

 

for Counter := 0 to Shopper1.RemoteFiles.Count-1 do

 

RemFilesList.Items.Add(Shopper1.RemoteFiles.Strings[Counter]);

 

{Now enable buttons}

 

AbortBtn.Enabled

:= TRUE;

 

SiteBtn.Enabled

:= TRUE;

 

ViewBtn.Enabled

:= TRUE;

 

HelpBtn.Enabled

:= TRUE;

 

DelBtn.Enabled

:= TRUE;

 

CancelBtn.Enabled

:= TRUE;

 

MkDirBtn.Enabled

:= TRUE;

RefreshBtn.Enabled := TRUE; ExitBtn.Enabled := FALSE;

end;

end;

end;

end;

procedure TMainForm.AboutBtnClick(Sender: TObject); begin

AboutBox.Show;

end;

procedure TMainForm.DisConBtnClick(Sender: TObject);

begin

 

try

 

Shopper1.Finish;

{Quit server}

finally

 

AbortBtn.Enabled

:= FALSE;

SiteBtn.Enabled

:= FALSE;

DisConBtn.Enabled

:= FALSE;

ViewBtn.Enabled

:= FALSE;

HelpBtn.Enabled

:= FALSE;

DelBtn.Enabled

:= FALSE;

CancelBtn.Enabled

:= FALSE;

MkDirBtn.Enabled

:= FALSE;

RefreshBtn.Enabled

:= FALSE;

ConnectBtn.Enabled

:= TRUE;

ExitBtn.Enabled

:= TRUE;

RemFilesList.Clear;

 

 

 

RemoteHostP.Caption

:=

'Remote host : ';

RemoteIPP.Caption

:=

'Remote IP

: ';

end;

 

 

 

end;

procedure TMainForm.ExitBtnClick(Sender: TObject); begin

Shopper1.QuitSession;

Close;

end;

procedure TMainForm.FormCreate(Sender: TObject); begin

{Output name of local machine}

LocalHostP.Caption :=

 

Concat('Local host

: ',Shopper1.LocalName);

{assign default login info from published properties} HostInput.Text := Shopper1.HomeServer;

UserInput.Text := Shopper1.UserName; PasswordInput.Text := Shopper1.Password; {Disable certain controls}

AbortBtn.Enabled

:= FALSE;

SiteBtn.Enabled

:= FALSE;

ConnectBtn.Enabled

:= TRUE;

DisConBtn.Enabled

:= FALSE;

ViewBtn.Enabled

:= FALSE;

HelpBtn.Enabled

:= FALSE;

DelBtn.Enabled

:= FALSE;

CancelBtn.Enabled

:= FALSE;

MkDirBtn.Enabled

:= FALSE;

RefreshBtn.Enabled := FALSE; RemFilesList.Clear; StatusMemo.Clear;

end;

procedure TMainForm.ViewBtnClick(Sender: TObject); begin

Shopper1.View := RemFilesList.Items.Strings[RemFilesList.ItemIndex];

end;

procedure TMainForm.MkDirBtnClick(Sender: TObject); var

Counter : Integer;

begin

MkDirDlg := TMkDirDlg.Create(Application); try

if MkDirDlg.ShowModal = IDOK then; if NewDirName <> '' then

begin Shopper1.FilesList; RemFilesList.Clear;

for Counter := 0 to Shopper1.RemoteFiles.Count-1 do RemFilesList.Items.Add(Shopper1.RemoteFiles.Strings[Counter]);

RemFilesList.UpDate;

end; finally

MkDirDlg.Free;

end;

end;

procedure TMainForm.Shopper1Change(Sender: TObject); begin

StatusMemo.Lines.Add(Shopper1.Info);

end;

procedure TMainForm.RemFilesListClick(Sender: TObject); begin

if RemFilesList.ItemIndex = -1 then Exit; Shopper1.Selection := RemFilesList.ItemIndex;

end;

procedure TMainForm.RemFilesListDblClick(Sender: TObject); var

Counter : Integer; begin

if RemFilesList.ItemIndex = -1 then Exit; Shopper1.Get :=

RemFilesList.Items.Strings[RemFilesList.ItemIndex];

RemFilesList.Clear;

for Counter := 0 to Shopper1.RemoteFiles.Count-1 do RemFilesList.Items.Add(Shopper1.RemoteFiles.Strings[Counter]);

RemFilesList.UpDate;

LocalFileList.UpDate;

end;

procedure TMainForm.RefreshBtnClick(Sender: TObject); var

Counter : Integer; begin Shopper1.FilesList; RemFilesList.Clear;

for Counter := 0 to Shopper1.RemoteFiles.Count-1 do RemFilesList.Items.Add(Shopper1.RemoteFiles.Strings[Counter]); RemFilesList.UpDate;

end;

procedure TMainForm.Shopper1UpDateProgress(Sender: TObject); begin

Progress.Text := Concat(IntToStr(Shopper1.Progress),'%'); end;

procedure TMainForm.Shopper1UpDateList(Sender: TObject); var

Counter : Integer; begin RemFilesList.Clear;

for Counter := 0 to Shopper1.RemoteFiles.Count-1 do RemFilesList.Items.Add(Shopper1.RemoteFiles.Strings[Counter]); RemFilesList.UpDate;

end;

procedure TMainForm.HelpBtnClick(Sender: TObject); begin

Shopper1.HelpFtp;

end;

procedure TMainForm.LocalFileListChange(Sender: TObject); begin

LocalFileList.Drive := LocalDirList.Drive; LocalFileList.Directory := LocalDirList.Directory; end;

procedure TMainForm.LocalDriveChange(Sender: TObject);

begin

 

LocalDirList.Drive

:= LocalDrive.Drive;

LocalFileList.Drive

:= LocalDirList.Drive;

end;

 

procedure TMainForm.LocalFileListDblClick(Sender: TObject); var

Counter : Integer; begin

if LocalFileList.ItemIndex = -1 then Exit;

Shopper1.Put := LocalFileList.Items[LocalFileList.ItemIndex]; LocalFileList.Clear;

for Counter := 0 to Shopper1.RemoteFiles.Count-1 do LocalFileList.Items.Add(Shopper1.RemoteFiles.Strings[Counter]); LocalFileList.UpDate;

Shopper1.FilesList;

RemFilesList.Clear;

for Counter := 0 to Shopper1.RemoteFiles.Count-1 do RemFilesList.Items.Add(Shopper1.RemoteFiles.Strings[Counter]); RemFilesList.UpDate;

end;

procedure TMainForm.LocalDirListChange(Sender: TObject); begin

LocalFileList.Directory := LocalDirList.Directory; end;

procedure TMainForm.Shopper1UpdateFileType(Sender: TObject); begin

if Shopper1.FType = IMAGE then

begin

BinaryRBtn.Checked := TRUE; AsciiRBtn.Checked := FALSE; end;

if Shopper1.FType = ASCII then begin

BinaryRBtn.Checked := FALSE; AsciiRBtn.Checked := TRUE; end;

end;

procedure TMainForm.Shopper1UpDateTimeTaken(Sender: TObject); var

TimeStr : String; begin

TimeStr := 'Time taken is (secs) = ' + IntToStr(Shopper1.TimePassed);

StatusMemo.Lines.Add(TimeStr);

end;

procedure TMainForm.Shopper1UpDateBusyFlag(Sender: TObject); begin

if Shopper1.Busy = TRUE then begin

MkDirBtn.Enabled := FALSE;

DelBtn.Enabled := FALSE; RefreshBtn.Enabled := FALSE; HelpBtn.Enabled := FALSE; DisConBtn.Enabled := FALSE; RemFilesList.Enabled := FALSE; end else

begin

MkDirBtn.Enabled := TRUE;

DelBtn.Enabled := TRUE; RefreshBtn.Enabled := TRUE; HelpBtn.Enabled := TRUE; DisConBtn.Enabled := TRUE; RemFilesList.Enabled := TRUE; end;

end;

procedure TMainForm.DelBtnClick(Sender: TObject); var

Counter : Integer; begin Shopper1.RmDirName :=

RemFilesList.Items.Strings[RemFilesList.ItemIndex];

Shopper1.FilesList;

RemFilesList.Clear;

for Counter := 0 to Shopper1.RemoteFiles.Count-1 do RemFilesList.Items.Add(Shopper1.RemoteFiles.Strings[Counter]); RemFilesList.UpDate;

end;

procedure TMainForm.AbortBtnClick(Sender: TObject);

begin Shopper1.Abort; end;

procedure TMainForm.CancelBtnClick(Sender: TObject); begin

Shopper1.Cancel;

end;

procedure TMainForm.Shopper1ChangeInfo(Sender: TObject); begin

StatusMemo.Lines.Add(Shopper1.Info);

end;

procedure TMainForm.SiteBtnClick(Sender: TObject); begin

Shopper1.SiteFtp;

end;

end.

Products | Contact Us | About Us | Privacy | Ad Info | Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.

All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.

To access the contents, click the chapter and section titles.

Kick Ass Delphi Programming

Go!

Keyword

(Publisher: The Coriolis Group)

Author(s): Don Taylor, Jim Mischel, John Penman, Terence Goggin

ISBN: 1576100448

Publication Date: 09/01/96

Search this book:

Go!

-----------

Before you connect to an FTP server, FTPCLI requires that you enter the name of the FTP server, your user name, and your password into the HostInput, UserInput, and PasswordInput editboxes, respectively. In the case of anonymous login, enter “anonymous” in the UserInput editbox and your email address into the PasswordInput editbox. If you want to access a site that requires an account name, you’ll have to add an extra editbox and modify Shopper to send the ACCT command. To avoid having to type in the same information repeatedly, use the published properties to set the login information as shown previously in Figure 5.2.

Using the information you enter, the Shopper1.Start method calls GetHost to open the connection to the remote host. If this fails, WSAErrorMsg displays the possible cause of the problem, and Status is set to Failure. Otherwise, Status is set to Success. If the connection succeeds, Start calls FTPCommand to send USER, PASS, SYST, and PWD commands—in that sequence—with their appropriate arguments. Start then starts a data connection to transfer the listing of the remote host’s directories and files, using GetPort to set up the data port for the connection.

To retrieve the directory listing, Start sends the LIST command through FTPCommand. The result is stored for a later call to Decode to parse for directories and files. The mechanics of parsing are simple, but the mapping of the directories and files varies between systems. The parser in FTPCLI works well with servers that run on Unix and look-alikes. For other systems, the parser may occasionally return garbled directory listings.

Decode checks the first character of each line in FTPFILE.TMP for a ‘d’, which denotes a directory, or the first 2 characters ‘-’ and ‘r’ which denote a file. When a ‘d’ character is found, it is stripped, and the line is scanned for the directory name and then converted to the familiar \ddd format. The backslash tells FTPCLI that this is a directory. Likewise, in the case of a file, the ‘-’ and ‘r’ characters are removed, and the line is scanned for the name, time, date, and file size, which are chopped into substrings. These substrings are rearranged to form an acceptable string for viewing in FTPCLI’s listbox, as shown in Figure 5.4.

FIGURE 5.4 FTPCLI application with the directory listing of files from the remote FTP host.

The FRemFiles.Add method in Decode reads each line, formats it into a string, and adds it to the FRemFiles property. FRemFiles is a string list, derived from the TStringList class, created by the TShopper.Create constructor. FTPCLI accesses this string list through the RemoteFiles public property. After Shopper1.Start has executed successfully, FTPCLI calls the following code to add the list of directories and files to RemFilesList listbox:

for Counter := 0 to Shopper1.RemoteFiles.Count-1 do RemFilesList.Items.Add(Shopper1.RemoteFiles.Strings[Counter]);

To end our business with the FTP server, we need to kill the connection by sending a QUIT command. Clicking on the Close button calls Shopper1.Quit and terminates the session as shown here:

procedure TMainForm.DisConBtnClick(Sender: TObject);

begin

 

try

 

Shopper1.Finish;

{Quit server}

finally

 

AbortBtn.Enabled

:= FALSE;

SiteBtn.Enabled

:= FALSE;

DisConBtn.Enabled

:= FALSE;

ViewBtn.Enabled

:= FALSE;

HelpBtn.Enabled

:= FALSE;

DelBtn.Enabled

:= FALSE;

CancelBtn.Enabled

:= FALSE;

MkDirBtn.Enabled

:= FALSE;

RefreshBtn.Enabled

:= FALSE;

ConnectBtn.Enabled

:= TRUE;

ExitBtn.Enabled

:= TRUE;

RemFilesList.Clear;

 

 

 

 

 

 

 

 

RemoteHostP.Caption

:=

'Remote

host

: ';

RemoteIPP.Caption

:=

'Remote

IP

: ';

end;

 

 

 

 

 

 

 

 

end;

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Products | Contact Us | About Us | Privacy | Ad Info | Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.

All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.