home *** CD-ROM | disk | FTP | other *** search
- { ----------------------------------------------------------------- }
- { Xceed FTP Library - FTP Client sample application }
- { Copyright (c) 2000 Xceed Software Inc. }
- { }
- { [frmXceedFTP.pas] }
- { }
- { This form contains all the code for the FTP Client }
- { }
- { This file is part of the Xceed FTP Library samples applications. }
- { The source code in this file is only intended as a supplement }
- { to Xceed FTP Library's documentation, and is provided "as is", }
- { without warranty of any kind, either expressed or implied. }
- { ----------------------------------------------------------------- }
-
- unit unMain;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls, ComCtrls, Menus, XceedFTPLib_TLB, unConnectInfo, OleCtrls;
-
- type
- TfrmMain = class(TForm)
- grpLocal : TGroupBox;
- grpRemote : TGroupBox;
- lstLocalView : TListView;
- lstRemoteView : TListView;
- lstConnectionInfo : TListBox;
- lstImages : TImageList;
- cboLocalPaths : TComboBox;
- cboRemotePaths : TComboBox;
- btDeleteLocalFile : TButton;
- btRenameLocalFile : TButton;
- btCreateLocalFolder : TButton;
- btDeleteLocalFolder : TButton;
- btParentLocalFolder : TButton;
- btParentRemoteFolder : TButton;
- btDeleteRemoteFile : TButton;
- btRenameRemoteFile : TButton;
- btCreateRemoteFolder : TButton;
- btDeleteRemoteFolder : TButton;
- btSendFiles : TButton;
- btReceiveFiles : TButton;
- btConnect : TButton;
- btDisconnect : TButton;
- btAbort : TButton;
- optBinary : TRadioButton;
- optAscii : TRadioButton;
- pgrProgess : TProgressBar;
- stbStatus : TStatusBar;
- mnuFtp : TMainMenu;
- miConnection : TMenuItem;
- miConnect : TMenuItem;
- miDisconnect : TMenuItem;
- miTransfer : TMenuItem;
- miSend : TMenuItem;
- miReceive : TMenuItem;
- miOperations : TMenuItem;
- miLocal : TMenuItem;
- miRemote : TMenuItem;
- miDeleteLocalFile : TMenuItem;
- miRenameLocalFile : TMenuItem;
- miCreateLocalFolder : TMenuItem;
- miDeleteLocalFolder : TMenuItem;
- miDeleteRemoteFile : TMenuItem;
- miRenameRemoteFile : TMenuItem;
- miCreateRemoteFolder : TMenuItem;
- miDeleteRemoteFolder : TMenuItem;
- miAbort : TMenuItem;
- popLocal : TPopupMenu;
- popRemote : TPopupMenu;
- pmiDeleteLocalFile : TMenuItem;
- pmiRenameLocalFile : TMenuItem;
- pmiCreateLocalFolder : TMenuItem;
- pmiDeleteLocalFolder : TMenuItem;
- pmiDeleteRemoteFile : TMenuItem;
- pmiRenameRemoteFile : TMenuItem;
- pmiCreateRemoteFolder : TMenuItem;
- pmiDeleteRemoteFolder : TMenuItem;
- xFtp : TXceedFtp;
-
- procedure FormCreate(Sender: TObject);
- procedure lstAnyViewCompare(Sender: TObject; Item1, Item2: TListItem;
- Data: Integer; var Compare: Integer);
- procedure lstDragDrop(Sender, Source: TObject; X, Y: Integer);
- procedure lstLocalViewDblClick(Sender: TObject);
- procedure lstRemoteViewDblClick(Sender: TObject);
- procedure lstLocalViewClick(Sender: TObject);
- procedure lstRemoteViewClick(Sender: TObject);
- procedure btParentLocalFolderClick(Sender: TObject);
- procedure btDeleteLocalFileClick(Sender: TObject);
- procedure btRenameLocalFileClick(Sender: TObject);
- procedure btCreateLocalFolderClick(Sender: TObject);
- procedure btDeleteLocalFolderClick(Sender: TObject);
- procedure btConnectClick(Sender: TObject);
- procedure btDisconnectClick(Sender: TObject);
- procedure btAbortClick(Sender: TObject);
- procedure btDeleteRemoteFileClick(Sender: TObject);
- procedure btRenameRemoteFileClick(Sender: TObject);
- procedure btCreateRemoteFolderClick(Sender: TObject);
- procedure btDeleteRemoteFolderClick(Sender: TObject);
- procedure btParentRemoteFolderClick(Sender: TObject);
- procedure btSendFilesClick(Sender: TObject);
- procedure btReceiveFilesClick(Sender: TObject);
- procedure miDeleteLocalFileClick(Sender: TObject);
- procedure miRenameLocalFileClick(Sender: TObject);
- procedure miCreateLocalFolderClick(Sender: TObject);
- procedure miDeleteLocalFolderClick(Sender: TObject);
- procedure miDisconnectClick(Sender: TObject);
- procedure miConnectClick(Sender: TObject);
- procedure miAbortClick(Sender: TObject);
- procedure miDeleteRemoteFileClick(Sender: TObject);
- procedure miRenameRemoteFileClick(Sender: TObject);
- procedure miCreateRemoteFolderClick(Sender: TObject);
- procedure miDeleteRemoteFolderClick(Sender: TObject);
- procedure miSendClick(Sender: TObject);
- procedure miReceiveClick(Sender: TObject);
- procedure pmiDeleteLocalFileClick(Sender: TObject);
- procedure pmiRenameLocalFileClick(Sender: TObject);
- procedure pmiCreateLocalFolderClick(Sender: TObject);
- procedure pmiDeleteLocalFolderClick(Sender: TObject);
- procedure pmiDeleteRemoteFileClick(Sender: TObject);
- procedure pmiRenameRemoteFileClick(Sender: TObject);
- procedure pmiCreateRemoteFolderClick(Sender: TObject);
- procedure pmiDeleteRemoteFolderClick(Sender: TObject);
- procedure cboLocalPathsClick(Sender: TObject);
- procedure cboRemotePathsClick(Sender: TObject);
- procedure optBinaryClick(Sender: TObject);
- procedure optAsciiClick(Sender: TObject);
-
- {XceedFtp events}
- procedure xFtpDisconnected(Sender: TObject);
- procedure xFtpListingFolderItem(Sender: TObject; const sName: WideString;
- dtDate: TDateTime; lFileSize: Integer;
- eItemType: TOleEnum;
- const sUserData: WideString);
- procedure xFtpFileTransferStatus(Sender: TObject; const sLocalFilename,
- sRemoteFilename: WideString; lFileSize, lBytesTransferred: Integer;
- nBytesPercent: Smallint; lTotalSize, lTotalBytesTransferred: Integer;
- nTotalBytesPercent: Smallint; lTotalFiles,
- lTotalFilesTransferred: Integer; nTotalFilesPercent: Smallint;
- lBytesPerSecond, lTotalBytesPerSecond: Integer);
- procedure xFtpLoggingCommandLine(Sender: TObject; const sLine: WideString;
- eCommandType: TOleEnum);
- procedure xFtpAccountRequired(Sender: TObject; var sAccountName: WideString);
- procedure xFtpPasswordRequired(Sender: TObject; var sPassword: WideString);
- procedure xFtpReceivingFile(Sender: TObject;
- const sRemoteFilename: WideString;
- var sLocalFilename: WideString;
- lFileSize: Integer);
- procedure xFtpReplacingFile(Sender: TObject; var sFilename: WideString;
- dtDate: TDateTime; lFileSize: Integer;
- const sRemoteFilename: WideString;
- var eAction: TOleEnum);
- procedure xFtpSendingFile(Sender: TObject; const sLocalFilename: WideString;
- var sRemoteFilename: WiDeString;
- lFileSize: Integer);
- procedure xFtpSkippingFile(Sender: TObject;
- const sLocalFilename, sRemoteFilename: WideString;
- lSkippingReason: Integer);
- procedure lstRemoteViewDragOver(Sender, Source: TObject; X, Y: Integer;
- State: TDragState; var Accept: Boolean);
- procedure lstLocalViewDragOver(Sender, Source: TObject; X, Y: Integer;
- State: TDragState; var Accept: Boolean);
- procedure FormDestroy(Sender: TObject);
-
-
- private
- {Private procedures and functions}
- procedure ClearStatusBars;
- procedure ClearLocalListView;
- procedure ClearRemoteListView;
- procedure RefreshLocalListView;
- procedure RefreshRemoteListView;
- procedure UpdateConnectionState(bIsConnected : boolean);
- procedure UpdateRemoteState;
- procedure UpdateLocalState;
- procedure NewLocalEntry(sName : string; dtDate : TDateTime; lSize : integer;
- nAttrib : integer);
- procedure NewRemoteEntry(sName : string; dtDate: TDateTime; lSize : longint;
- eItemType : EXFFolderItemType);
- procedure DisplayMessage(sMessage : string);
-
- function PathExists(sPathToCheck : string; bIsLocal : boolean) : boolean;
-
- {Procedures that perform FTP functionality}
- procedure DoConnect;
- procedure DoDisconnect;
- procedure DoSendFiles;
- procedure DoReceiveFiles;
- procedure DoAbort;
- procedure GetParentFolder;
- procedure RetrieveRemoteFolderContents;
-
- function GetCurrentConnectionState : EXFState;
- function GetRemoteFolder : string;
- function ChangeCurrentRemoteFolder(sNewCurrentFolder : string) : boolean;
-
- {Remote file and folder handling}
- procedure DeleteSelectedRemoteFile;
- procedure RenameRemoteFile;
- procedure CreateRemoteFolder;
- procedure DeleteRemoteFolder;
-
- {Local file and folder handling}
- procedure RetrieveLocalFolderContents(sPath : string);
- procedure RetrieveLogicalDrives;
- procedure DeleteSelectedLocalFile;
- procedure RenameLocalFile;
- procedure CreateLocalFolder;
- procedure DeleteLocalFolder;
-
- public
- m_sCurrentLocalPath : string; { Keeps current local path }
- m_sCurrentRemotePath : string; { Keeps current remmote path }
- m_bReplaceAll : boolean; { Set in the ReplacingFile event }
- end;
-
- var
- frmMain : TfrmMain;
- implementation
-
- {$R *.DFM}
- { ------------------------------------------------------------- }
- { Clear the progress bar and status panels }
- { ------------------------------------------------------------- }
- procedure TfrmMain.ClearStatusBars();
- begin
- pgrProgess.Position := 0;
- stbStatus.Panels[1].Text := '';
- end;
-
- { ------------------------------------------------------------- }
- { Clear the contents of the local file list ListView control. }
- { ------------------------------------------------------------- }
- procedure TfrmMain.ClearLocalListView();
- begin
- lstLocalView.Items.Clear;
- end;
-
- { ------------------------------------------------------------- }
- { Clear the contents of the remote file list ListView control. }
- { ------------------------------------------------------------- }
- procedure TfrmMain.ClearRemoteListView();
- begin
- lstRemoteView.Items.Clear;
- end;
-
- { ------------------------------------------------------------- }
- { Refresh the local file list ListView control and display the }
- { new contents by calling RetrieveLocalFolderContents procedure }
- { ------------------------------------------------------------- }
- procedure TfrmMain.RefreshLocalListView();
- begin
- ClearLocalListView();
- RetrieveLocalFolderContents(m_sCurrentLocalPath);
- end;
-
- { ------------------------------------------------------------- }
- { Refresh the remote file list ListView control and display the }
- { new contents by calling RetrieveRemoteFolderContents }
- { procedure }
- { ------------------------------------------------------------- }
- procedure TfrmMain.RefreshRemoteListView();
- begin
- ClearRemoteListView();
- RetrieveRemoteFolderContents();
- end;
-
- { ------------------------------------------------------------- }
- { Here we will enable or disable some of the controls depending }
- { on the connection state. }
- { ------------------------------------------------------------- }
- procedure TfrmMain.UpdateConnectionState(bIsConnected : boolean);
- begin
- btDisconnect.Enabled := bIsConnected;
- btAbort.Enabled := bIsConnected;
- btParentRemoteFolder.Enabled := bIsConnected;
- btSendFiles.Enabled := bIsConnected;
- btReceiveFiles.Enabled := bIsConnected;
-
- btCreateRemoteFolder.Enabled := bIsConnected;
- miCreateRemoteFolder.Enabled := bIsConnected;
-
- optAscii.Enabled := bIsConnected;
- optBinary.Enabled := bIsConnected;
-
- cboRemotePaths.Enabled := bIsConnected;
-
- miDisconnect.Enabled := bIsConnected;
- miTransfer.Enabled := bIsConnected;
- miSend.Enabled := bIsConnected;
- miReceive.Enabled := bIsConnected;
- miRemote.Enabled := bIsConnected;
-
- btConnect.Enabled := not bIsConnected;
- miConnect.Enabled := not bIsConnected;
- end;
-
- { ------------------------------------------------------------- }
- { Update the state of the buttons and menus for remote file and }
- { folder operations. The button states depend largely on the }
- { number and type of items selected in the remote file list }
- { ListView object. }
- { ------------------------------------------------------------- }
-
- procedure TfrmMain.UpdateRemoteState();
- var
- bMultipleItemsSelected : boolean;
- bIsNothing : boolean;
- nItemType : integer;
- begin
-
- { Make sure that the remote listview control is created }
-
- bIsNothing := not assigned(lstRemoteView.Selected);
- nItemType := -1;
- bMultipleItemsSelected := false;
-
- if not bIsNothing then
- begin
-
- { Check if more than one item is selected }
- bMultipleItemsSelected := lstRemoteView.SelCount > 1;
-
- if not bMultipleItemsSelected then
- nItemType := lstRemoteView.Selected.ImageIndex; { ImageIndex doubles as item type }
- end;
-
- btDeleteRemoteFile.Enabled := (not bIsNothing) and ((bMultipleItemsSelected) or
- (nItemType = 2));
- miDeleteRemoteFile.Enabled := btDeleteRemoteFile.Enabled;
- pmiDeleteRemoteFile.Enabled := btDeleteRemoteFile.Enabled;
-
- btRenameRemoteFile.Enabled := (not bIsNothing) and (nItemType = 2);
- miRenameRemoteFile.Enabled := btRenameRemoteFile.Enabled;
- pmiRenameRemoteFile.Enabled := btRenameRemoteFile.Enabled;
-
- btDeleteRemoteFolder.Enabled := (not bIsNothing) and (nItemType = 0);
- miDeleteRemoteFolder.Enabled := btDeleteRemoteFolder.Enabled;
- pmiDeleteRemoteFolder.Enabled := btDeleteRemoteFolder.Enabled;
-
- btReceiveFiles.Enabled := (not bIsNothing) and (lstRemoteView.SelCount > 0);
- miReceive.Enabled := btReceiveFiles.Enabled;
- end;
-
- { ------------------------------------------------------------- }
- { Update the state of the buttons and menus for local file and }
- { folder operations. The button states depend largely on the }
- { number and type of items selected in the local file list }
- { ListView object. }
- { ------------------------------------------------------------- }
- procedure TfrmMain.UpdateLocalState();
- var
- bMultipleItemsSelected : boolean;
- bIsNothing : boolean;
- nItemType : integer;
- begin
-
- { Make sure that the local listview control is created }
- bIsNothing := not assigned(lstLocalView.Selected);
-
- if not bIsNothing then
- begin
-
- { Check if more than one item is selected }
- bMultipleItemsSelected := lstLocalView.SelCount > 1;
-
- if not bMultipleItemsSelected then
- nItemType := lstLocalView.Selected.ImageIndex { ImageIndex represents the item type }
- else
- { If more than one item is selected, set the image index to -1 }
- nItemType := -1;
-
- end
- else
- begin
- nItemType := 0;
- bMultipleItemsSelected := False;
- end;
-
- btDeleteLocalFile.Enabled := (not bIsNothing) and ((bMultipleItemsSelected) or
- (nItemType = 2));
- miDeleteLocalFile.Enabled := btDeleteLocalFile.Enabled;
- pmiDeleteLocalFile.Enabled := btDeleteLocalFile.Enabled;
-
- btRenameLocalFile.Enabled := (not bIsNothing) and (nItemType = 2);
- miRenameLocalFile.Enabled := btRenameLocalFile.Enabled;
- pmiRenameLocalFile.Enabled := btRenameLocalFile.Enabled;
-
- btCreateLocalFolder.Enabled := true;
- miCreateLocalFolder.Enabled := true;
- pmiCreateLocalFolder.Enabled := true;
-
- btDeleteLocalFolder.Enabled := (not bIsNothing) and (nItemType = 0);
- miDeleteLocalFolder.Enabled := btDeleteLocalFolder.Enabled;
- pmiDeleteLocalFolder.Enabled := btDeleteLocalFolder.Enabled;
-
- btSendFiles.Enabled := (not bIsNothing) and (lstLocalView.SelCount > 0) and
- (GetCurrentConnectionState = fstConnected);
- miSend.Enabled := btSendFiles.Enabled;
- end;
-
- { ------------------------------------------------------------- }
- { Add a new entry to the local file list ListView control. We }
- { will also set the appropriate icon (depends on whether the }
- { item is a file a folder. }
- { ------------------------------------------------------------- }
- procedure TfrmMain.NewLocalEntry(sName : string; dtDate : TDateTime;
- lSize : integer; nAttrib : integer);
- var
- xItem : TListItem;
- begin
- xItem := lstLocalView.Items.Add;
-
- xItem.Caption := sName; { Name of the item being added to the local file listview }
- xItem.SubItems.Add(IntToStr( (lSize + 1023) div 1024)); { Size in KB }
- xItem.SubItems.Add(DateToStr(dtDate)); { Date }
-
- case (nAttrib and faDirectory) of
- faDirectory : xItem.ImageIndex := 0 { Item is a folder }
- else
- xItem.ImageIndex := 2; { Item is a file }
- end;
- end;
-
- { ------------------------------------------------------------- }
- { Add a new entry to the remote file list ListView control. We }
- { will also set the appropriate icon depending on if the item }
- { is a file a folder or a link. }
- { ------------------------------------------------------------- }
- procedure TfrmMain.NewRemoteEntry(sName : string; dtDate: TDateTime;
- lSize : longint;
- eItemType : EXFFolderItemType);
- var
- xItem : TListItem;
- begin
- xItem := lstRemoteView.Items.Add;
-
- xItem.Caption := sName; { Name of the item being added to the remote file listview }
- xItem.SubItems.Add(IntToStr( (lSize + 1023) div 1024)); { Size in KB }
- xItem.SubItems.Add(DateToStr(dtDate)); { Date }
-
- case eItemType of
- fitFolder : xItem.ImageIndex := 0; { Item is a folder }
- fitLink : xItem.ImageIndex := 1; { Item is a link }
- fitFile : xItem.ImageIndex := 2; { Item is a file }
- end
- end;
-
- { ------------------------------------------------------------- }
- { Display a message in the lstConnectionInfo listbox. This }
- { procedure can ba called from any other procedure or function }
- { ------------------------------------------------------------- }
- procedure TfrmMain.DisplayMessage(sMessage : string);
- begin
- lstConnectionInfo.Items.Add(sMessage);
- lstConnectionInfo.ItemIndex := lstConnectionInfo.Items.Count - 1;
- end;
-
- { ------------------------------------------------------------- }
- { This procedure verifies that the path to be added does not }
- { already exist in the corresponding combo box and returns true }
- { or false depending on the case. The second parameter of this }
- { procedure determines if the path to add is local or remote. }
- { ------------------------------------------------------------- }
- function TfrmMain.PathExists(sPathToCheck : string;
- bIsLocal : boolean) : boolean;
- var
- nCount : integer;
- begin
- nCount := 0;
- PathExists := false;
-
- if bIsLocal then { Verifying a local path }
- begin
- while nCount < cboLocalPaths.Items.Count do
- begin
- if sPathToCheck = cboLocalPaths.Items[nCount] then
- begin
- PathExists := true;
- nCount := cboLocalPaths.Items.Count;
- end;
- nCount := nCount + 1;
- end;
- end
- else { Verify a remote path }
- begin
- while nCount < cboRemotePaths.Items.Count do
- begin
- if sPathToCheck = cboRemotePaths.Items[nCount] then
- begin
- PathExists := true;
- nCount := cboRemotePaths.Items.Count;
- end;
- nCount := nCount + 1;
- end;
- end;
- end;
-
- { ------------------------------------------------------------- }
- { The following procedure contains all the steps necessary to }
- { connect to an FTP server. }
- {-------------------------------------------------------------- }
- procedure TfrmMain.DoConnect();
- var
- frmConnect : TfrmConnect;
- sHostName : string;
- nPort : integer;
- sUserName : string;
- sPassword : string;
- sRemoteFolder : string;
- begin
- frmConnect := TfrmConnect.Create(Self); { Create a new instance of frmConnect }
-
- if frmConnect.GetConnectInfo(sHostName, nPort, sUserName, sPassword) then
- begin
- xFtp.ServerAddress := sHostName; { Set the Ftp server address }
- xFtp.ServerPort := nPort; { Set the Ftp server port }
- xFtp.UserName := sUserName; { Set the username }
- xFtp.Password := sPassword; { Set the password }
-
- Screen.Cursor := crHourGlass;
- stbStatus.Panels[0].Text := 'Connecting to ' + sHostName + '...Please wait';
- try
- xFtp.Connect(); { Connect to the Ftp server }
- RetrieveRemoteFolderContents(); { Retrieve a listing of the remote folder contents }
- sRemoteFolder := GetRemoteFolder();
- cboRemotePaths.Items.Add(sRemoteFolder); { Add new path to the remote combo box }
- cboRemotePaths.Text := sRemoteFolder;
- m_sCurrentRemotePath := sRemoteFolder; { Set the new current remote path }
- UpdateConnectionState(true); { Update the controls on the form }
- stbStatus.Panels[0].Text := 'Connected to ' + sHostName;
- except
- on xErr: Exception do
- begin
- DisplayMessage(xErr.Message);
- Screen.Cursor := crDefault;
- stbStatus.Panels[0].Text := 'Could not connect.';
- end;
- end;
- end;
- frmConnect.Free; { Free the instance of frmConnect we created }
- end;
-
- { ------------------------------------------------------------- }
- { Disconnect from FTP server }
- { ------------------------------------------------------------- }
- procedure TfrmMain.DoDisconnect();
- begin
- try
- { Make sure that we are connected }
- if GetCurrentConnectionState() <> fstNotConnected then
- begin
- xFtp.Disconnect(); { Disconnect from Ftp server }
-
- cboRemotePaths.Clear;
- stbStatus.Panels[0].Text := '';
- stbStatus.Panels[2].Text := '';
-
- ClearRemoteListView();
- ClearStatusBars();
- UpdateConnectionState(false); { Update the controls on the form }
- end;
- except
- on xErr : Exception do
- DisplayMessage(xErr.Message);
- end;
- end;
-
- { ------------------------------------------------------------- }
- { This procedure will send a file(s) or folder(s) to the FTP }
- { server by using either the SendMultipleFiles or the SendFile }
- { methods. }
- { ------------------------------------------------------------- }
- procedure TfrmMain.DoSendFiles();
- var
- sFullFilename : string;
- sFilename : string;
- xItem : TListItem;
- nCount : integer;
- begin
- nCount := 0;
-
- { If the selected item is a folder, we will call the SendMultipleFiles method }
- if lstLocalView.Selected.ImageIndex = 0 then
- begin
- sFullFilename := m_sCurrentLocalPath + lstLocalView.Selected.Caption;
- sFilename := lstLocalView.Selected.Caption;
-
- try
- if MessageDlg('Do you want to include any encountered subfolders?',
- mtConfirmation, [mbYes, mbNo], 0) = mrYes then
- begin
- { Recursively send subfolders too }
- xFtp.SendMultipleFiles(sFullFilename + '\*', sFilename, true, true);
- RefreshRemoteListView();
- end
- else
- begin
- { Do not send subfolders }
- xFtp.SendMultipleFiles(sFullFilename + '\*', sFilename, true, false);
- RefreshRemoteListView();
- end;
- except
- on xErr: Exception do
- DisplayMessage(xErr.Message);
- end;
- end
- else
- begin
- while nCount < lstLocalView.Items.Count do
- begin
- xItem := lstLocalView.Items.Item[nCount];
- { Selected item is a file }
- if (xItem.ImageIndex = 2) and (xItem.Selected) then
- begin
- sFullFilename := m_sCurrentLocalPath + xItem.Caption;
- try
- xFtp.SendFile(sFullFilename, 0, xItem.Caption, true);
- except
- on xErr: Exception do
- DisplayMessage(xErr.Message);
- end;
- end;
- nCount := nCount + 1;
- end;
- stbStatus.Panels[0].Text := 'Transfer completed';
- RefreshRemoteListView();
- end;
- end;
-
- { ------------------------------------------------------------- }
- { This procedure will receive file(s) or folders from the FTP }
- { server by using either the ReceiveMultipleFiles or }
- { ReceiveFile methods. }
- { ------------------------------------------------------------- }
- procedure TfrmMain.DoReceiveFiles();
- var
- sFilename : string;
- nCount : integer;
- xItem : TListItem;
- sLocalFilename : string;
- xAnswer : Word;
- begin
- nCount := 0;
-
- { If the selected item is a folder, we will call the ReceiveMultipleFiles method
- and ask it to receive the folder, using the * wildcard }
-
- if lstRemoteView.Selected.ImageIndex = 0 then
- begin
- sFilename := lstRemoteView.Selected.Caption;
-
- try
- xAnswer := MessageDlg('Do you include any encountered subfolders?',
- mtConfirmation, [mbYes, mbNo], 0);
-
- xFtp.ReceiveMultipleFiles(sFilename + '\*',
- m_sCurrentLocalPath + sFilename,
- ( xAnswer = mrYes ));
- RefreshLocalListView();
- except
- on xErr: Exception do
- DisplayMessage(xErr.Message);
- end;
- end
- else
- begin
- while nCount < lstRemoteView.Items.Count do
- begin
- xItem := lstRemoteView.Items.Item[nCount];
- { Selected item is a file }
- if ((xItem.ImageIndex = 2) or (xItem.ImageIndex = 1)) and (xItem.Selected) then
- begin
- sFilename := xItem.Caption;
- sLocalFilename := m_sCurrentLocalPath + sFilename;
- try
- xFtp.ReceiveFile(sFilename, 0, sLocalFilename);
- except
- on xErr: Exception do
- DisplayMessage(xErr.Message);
- end;
- end;
- nCount := nCount + 1;
- end;
- stbStatus.Panels[0].Text := 'Transfer completed';
- RefreshLocalListView();
- end;
- end;
-
- { ------------------------------------------------------------- }
- { Retrieve the files in the current directory of the FTP server }
- { ------------------------------------------------------------- }
- procedure TfrmMain.RetrieveRemoteFolderContents();
- begin
- Screen.Cursor := crHourGlass;
- try
- xFtp.ListFolderContents('');
- except
- on xErr: Exception do
- DisplayMessage(xErr.Message);
- end;
- Screen.Cursor := crArrow;
- end;
-
- { ------------------------------------------------------------- }
- { Go to the parent folder of our current remote directory }
- { ------------------------------------------------------------- }
- procedure TfrmMain.GetParentFolder();
- begin
- try
- xFtp.ChangeToParentFolder();
- except
- on xErr: Exception do
- DisplayMessage(xErr.Message);
- end;
- end;
-
- { ------------------------------------------------------------- }
- { Abort the current operation }
- { ------------------------------------------------------------- }
- procedure TfrmMain.DoAbort();
- begin
- try
- xFtp.Abort := true;
- RefreshLocalListView();
- ClearStatusBars();
- stbStatus.Panels[0].Text := 'Transfer aborted';
- stbStatus.Panels[2].Text := '';
- except
- on xErr: Exception do
- DisplayMessage(xErr.Message);
- end;
- end;
-
- { ------------------------------------------------------------- }
- { Get the current connection state }
- { ------------------------------------------------------------- }
- function TfrmMain.GetCurrentConnectionState() : EXFState;
- begin
- GetCurrentConnectionState := xFtp.CurrentState;
- end;
-
- { ------------------------------------------------------------- }
- { Get the current remote folder }
- { ------------------------------------------------------------- }
- function TfrmMain.GetRemoteFolder() : string;
- begin
- try
- GetRemoteFolder := xFtp.CurrentFolder;
- except
- on xErr: Exception do
- DisplayMessage(xErr.Message);
- end;
- end;
-
- { ------------------------------------------------------------- }
- { Change the current remote folder to the new one specifed by }
- { the user. }
- { ------------------------------------------------------------- }
- function TfrmMain.ChangeCurrentRemoteFolder(sNewCurrentFolder : string) : boolean;
- begin
- ChangeCurrentRemoteFolder := false;
- try
- xFtp.ChangeCurrentFolder(sNewCurrentFolder);
- ChangeCurrentRemoteFolder := true;
- GetRemoteFolder();
- except
- on xErr: Exception do
- DisplayMessage(xErr.Message);
- end;
- end;
-
- { ------------------------------------------------------------- }
- { Delete a file on the FTP server }
- { ------------------------------------------------------------- }
- procedure TfrmMain.DeleteSelectedRemoteFile();
- var
- nCount : integer;
- xItem : TListItem;
- sFileToDelete : string;
- begin
- if MessageDlg('Are you sure you want to delete the selected remote file(s)?',
- mtConfirmation, [mbYes, mbNo], 0) = mrYes then
- begin
- nCount := 0;
-
- while nCount < lstRemoteView.Items.Count do
- begin
- xItem := lstRemoteView.Items[nCount];
- sFileToDelete := GetRemoteFolder() + '/' + xItem.Caption;
- if xItem.Selected then
- begin
- try
- xFtp.DeleteFile(sFileToDelete);
- except
- on xErr: Exception do
- ShowMessage('Cannot delete ' + sFileToDelete);
- end;
- end;
- nCount := nCount + 1;
- end;
- RefreshRemoteListView();
- end;
- end;
-
- { ------------------------------------------------------------- }
- { Rename a file on the FTP server }
- { ------------------------------------------------------------- }
- procedure TfrmMain.RenameRemoteFile();
- var
- sCurrentName : string;
- sNewName : string;
- begin
- sCurrentName := m_sCurrentRemotePath + '\' + lstRemoteView.Selected.Caption;
-
- if MessageDlg('Rename ' + sCurrentName + '?', mtConfirmation, [mbYes, mbNo],
- 0) = mrYes then
- begin
- sNewName := InputBox('Xceed Ftp Client Sample', 'Enter the new name', '');
- if sNewName <> '' then
- begin
- sNewName := m_sCurrentRemotePath + '\' + sNewName;
- try
- xFtp.RenameFile(sCurrentName, sNewName);
- RefreshRemoteListView();
- except
- on xErr: Exception do
- ShowMessage('Cannot rename ' + sCurrentName);
- end;
- end;
- end;
- end;
-
- { ------------------------------------------------------------- }
- { Create a new folder on the FTP server }
- { ------------------------------------------------------------- }
- procedure TfrmMain.CreateRemoteFolder();
- var
- sFolderToCreate : string;
- begin
- sFolderToCreate := InputBox('Xceed Ftp Client Sample', 'Enter the name of ' +
- 'the new folder to create', '');
- if sFolderToCreate <> '' then
- begin
- try
- xFtp.CreateFolder(m_sCurrentRemotePath + '\' + sFolderToCreate);
- RefreshRemoteListView();
- except
- on xErr: Exception do
- ShowMessage('Unable to create ' + sFolderToCreate);
- end;
- end;
- end;
-
- { ------------------------------------------------------------- }
- { Delete a folder on the FTP server }
- { ------------------------------------------------------------- }
- procedure TfrmMain.DeleteRemoteFolder();
- var
- sFolderToDelete : string;
- begin
- sFolderToDelete := m_sCurrentRemotePath + '\' + lstRemoteView.Selected.Caption;
- if MessageDlg('Delete ' + sFolderToDelete + '?', mtConfirmation,
- [mbYes, mbNo], 0) = mrYes then
- begin
- try
- xFtp.RemoveFolder(sFolderToDelete);
- RefreshRemoteListView();
- except
- on xErr: Exception do
- ShowMessage('Unable to delete ' + sFolderToDelete);
- end;
- end;
- end;
-
- { ------------------------------------------------------------- }
- { The following procedure will read the contents of the }
- { selected drive or folder. If the selected path is a file, the }
- { file's information will be also retrieved. }
- { ------------------------------------------------------------- }
- procedure TfrmMain.RetrieveLocalFolderContents(sPath : string);
- var
- dtDate : TDateTime;
- lSize : integer;
- nAttrib : integer;
- SearchRec : TSearchRec;
- nResult : integer;
- begin
- nResult := FindFirst(sPath + '*', faAnyFile, SearchRec);
-
- while (nResult = 0) do
- begin
- if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
- begin
- nAttrib := SearchRec.Attr; { Attributes }
- dtDate := SearchRec.Time; { Date }
- lSize := SearchRec.Size; { Size }
-
- NewLocalEntry(SearchRec.Name, dtDate, lSize, nAttrib);
- end;
- nResult := FindNext(SearchRec); { Go to the next item }
- end;
- FindClose(SearchRec);
- end;
-
- { ------------------------------------------------------------- }
- { Retrieve the logical drive names }
- { ------------------------------------------------------------- }
- procedure TfrmMain.RetrieveLogicalDrives();
- var
- szDrives : array[0..259] of char;
- pszPos : pchar;
- sDrive : string;
- begin
- GetLogicalDriveStrings(260, szDrives);
-
- pszPos := szDrives;
- while pszPos[0] > #0 do
- begin
- { Parse the string returned by the GetLogicalDriveStrings function }
- sDrive := pszPos;
- cboLocalPaths.Items.Add(sDrive); { Add the drive to our local combo box }
- pszPos := pszPos + StrLen(pszPos) + 1;
- end;
- end;
-
- { ------------------------------------------------------------- }
- { Delete the selected local file(s) }
- { ------------------------------------------------------------- }
- procedure TfrmMain.DeleteSelectedLocalFile();
- var
- nCount : integer;
- xItem : TListItem;
- sFileToDelete : string;
- begin
- if MessageDlg('Are you sure you want to delete the selected local file(s)?',
- mtConfirmation, [mbYes, mbNo], 0) = mrYes then
- begin
- nCount := 0;
-
- while nCount < lstLocalView.Items.Count do
- begin
- xItem := lstLocalView.Items[nCount];
- sFileToDelete := m_sCurrentLocalPath + xItem.Caption;
- if xItem.Selected then
- begin
- if not DeleteFile(sFileToDelete) then
- ShowMessage('Cannot delete ' + sFileToDelete);
- end;
- nCount := nCount + 1;
- end;
- RefreshLocalListView();
- end;
- end;
-
- { ------------------------------------------------------------- }
- { Rename the selected local file }
- { ------------------------------------------------------------- }
- procedure TfrmMain.RenameLocalFile();
- var
- sCurrentName : string;
- sNewName : string;
- begin
- sCurrentName := m_sCurrentLocalPath + lstLocalView.Selected.Caption;
- If MessageDlg('Rename ' + sCurrentName + '?', mtConfirmation, [mbYes, mbNo],
- 0) = mrYes then
- begin
- sNewName := InputBox('Xceed Ftp Client Sample', 'Enter the new name for ' +
- sCurrentName , '');
-
- if sNewName <> '' then
- begin
- If RenameFile(sCurrentName, m_sCurrentLocalPath + sNewName) then
- RefreshLocalListView()
- else
- ShowMessage('Cannot rename ' + sCurrentName);
- end;
- end;
- end;
-
- { ------------------------------------------------------------- }
- { Create a new local folder }
- { ------------------------------------------------------------- }
- procedure TfrmMain.CreateLocalFolder();
- var
- sNewFolder : string;
- begin
- sNewFolder := InputBox('Xceed Ftp Client Sample', 'Enter the name of the ' +
- 'folder to create.', '');
- if sNewFolder <> '' then
- begin
- try
- MkDir(m_sCurrentLocalPath + sNewFolder);
- RefreshLocalListView();
- except
- on xErr : Exception do
- ShowMessage('Cannot create ' + m_sCurrentLocalPath + sNewFolder);
- end;
- end;
- end;
-
- { ------------------------------------------------------------- }
- { Delete the selected local folder }
- { ------------------------------------------------------------- }
- procedure TfrmMain.DeleteLocalFolder();
- var
- sFolderToDelete : string;
- begin
- sFolderToDelete := m_sCurrentLocalPath + lstLocalView.Selected.Caption;
- if MessageDlg('Delete ' + sFolderToDelete + '?', mtConfirmation,
- [mbYes, mbNo], 0) = mrYes then
- begin
- try
- RmDir(sFolderToDelete);
- RefreshLocalListView();
- except
- on xErr: Exception do
- ShowMessage('Cannot delete ' + sFolderToDelete);
- end;
- end;
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.FormCreate(Sender: TObject);
- begin
- optBinary.Checked := true; { Set binary as the default transfer mode }
- m_bReplaceAll := false;
-
- UpdateLocalState();
- UpdateRemoteState();
- UpdateConnectionState(false);
-
- RetrieveLogicalDrives();
-
- m_sCurrentLocalPath := 'C:\'; { the default local path }
- cboLocalPaths.Text := m_sCurrentLocalPath;
- RetrieveLocalFolderContents(m_sCurrentLocalPath);
- end;
-
- { ------------------------------------------------------------- }
- { Sort the items in the list views depending on the image index }
- { ------------------------------------------------------------- }
- procedure TfrmMain.lstAnyViewCompare(Sender: TObject; Item1,
- Item2: TListItem; Data: Integer;
- var Compare: Integer);
- begin
- if Item1.ImageIndex < Item2.ImageIndex then
- Compare := -1
- else if (Item1.ImageIndex = Item2.ImageIndex)and (UpperCase(Item1.Caption) <
- UpperCase(Item2.Caption)) then
- Compare := -1
- else
- Compare := 1;
- end;
-
- { ------------------------------------------------------------- }
- { This procedure provides two possiblities. If the item }
- { selected is a file, it will call the SendFiles procedure and }
- { if the item selected is a folder, it will change the current }
- { local folder to the new folder and list it's contents. }
- { ------------------------------------------------------------- }
- procedure TfrmMain.lstLocalViewDblClick(Sender: TObject);
- var
- sSelectedItem : string;
- begin
- if lstLocalView.SelCount > 0 then
- begin
- if lstLocalView.Selected.ImageIndex = 0 then { Selected item is a folder }
- begin
- sSelectedItem := m_sCurrentLocalPath + lstLocalView.Selected.Caption + '\';
- ClearLocalListView();
- RetrieveLocalFolderContents(sSelectedItem);
- m_sCurrentLocalPath := sSelectedItem; { Set the new current local path }
-
- if not PathExists(m_sCurrentLocalPath, true) then
- begin
- cboLocalPaths.Items.Add(m_sCurrentLocalPath);
- cboLocalPaths.Text := m_sCurrentLocalPath;
- end
- else
- cboLocalPaths.Text := m_sCurrentLocalPath;
- end
- else { Selected item is a file }
- begin
- if GetCurrentConnectionState = fstConnected then
- DoSendFiles();
- end;
- end;
- end;
-
- { ------------------------------------------------------------- }
- { The items in the remote file list ListView control can be }
- { either files, folders or links. }
- { }
- { If the selected item is a file, then the ReceiveFiles }
- { procedure will be called. If it is a folder, then the current }
- { remote folder will be changed and the new contents will be }
- { listed. }
- { }
- { If the item is a link, then it can either link to a file or a }
- { folder. Since we do not have a way of determining beforehand }
- { if it is a file or a folder, we will first assume that it }
- { links to a folder and try to change the current }
- { remote folder. If this is successful, then we will list the }
- { contents of the new current remote folder. If this fails, we }
- { will consider the link to be a file and call the ReceiveFiles }
- { method. }
- { ------------------------------------------------------------- }
- procedure TfrmMain.lstRemoteViewDblClick(Sender: TObject);
- var
- sSelectedItem : string;
- begin
- if lstRemoteView.SelCount > 0 then
- begin
- sSelectedItem := m_sCurrentRemotePath + '\' + lstRemoteView.Selected.Caption;
-
- case lstRemoteView.Selected.ImageIndex of
- 0 : { Selected item is a folder }
- if ChangeCurrentRemoteFolder(sSelectedItem) then
- begin
- { Set the new remote path }
- m_sCurrentRemotePath := GetRemoteFolder();
- if not PathExists(m_sCurrentRemotePath, false) then
- cboRemotePaths.Items.Add(m_sCurrentRemotePath);
-
- cboRemotePaths.Text := m_sCurrentRemotePath;
- ClearRemoteListView();
- RetrieveRemoteFolderContents();
- end;
- 1 : { Selected item is a link }
- if ChangeCurrentRemoteFolder(sSelectedItem) then { Check if we can change folders into the link }
- begin
- { Yes, the link "links" to a folder so we set the new current remote path }
- m_sCurrentRemotePath := sSelectedItem;
- if not PathExists(m_sCurrentRemotePath, false) then
- cboRemotePaths.Items.Add(m_sCurrentRemotePath);
- cboRemotePaths.Text := m_sCurrentRemotePath;
- ClearRemoteListView();
- RetrieveRemoteFolderContents();
- end
- else
- { Failed changing folders into the link. So the link "links" to a file. }
- DoReceiveFiles();
- 2 : DoReceiveFiles(); { Selected item is a file }
- end;
- end;
- end;
-
- { ------------------------------------------------------------- }
- { Change the current local folder to its parent folder and list }
- { the contents of the parent folder. }
- { ------------------------------------------------------------- }
- procedure TfrmMain.btParentLocalFolderClick(Sender: TObject);
- var
- sNewCurrentLocalPath : string;
- begin
- if Length(m_sCurrentLocalPath) > 3 then { Check if not root }
- begin
- sNewCurrentLocalPath := Copy(m_sCurrentLocalPath, 0,
- Length(m_sCurrentLocalPath) - 1);
- sNewCurrentLocalPath := ExtractFilePath(sNewCurrentLocalPath);
-
- ClearLocalListView();
- RetrieveLocalFolderContents(sNewCurrentLocalPath);
- m_sCurrentLocalPath := sNewCurrentLocalPath; { Set the new current local path }
- cboLocalPaths.Text := m_sCurrentLocalPath;
- UpdateLocalState();
- end;
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.btDeleteLocalFileClick(Sender: TObject);
- begin
- DeleteSelectedLocalFile();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.btRenameLocalFileClick(Sender: TObject);
- begin
- RenameLocalFile();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.btCreateLocalFolderClick(Sender: TObject);
- begin
- CreateLocalFolder();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.btDeleteLocalFolderClick(Sender: TObject);
- begin
- DeleteLocalFolder();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.miDeleteLocalFileClick(Sender: TObject);
- begin
- DeleteSelectedLocalFile();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.miRenameLocalFileClick(Sender: TObject);
- begin
- RenameLocalFile();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.miCreateLocalFolderClick(Sender: TObject);
- begin
- CreateLocalFolder();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.miDeleteLocalFolderClick(Sender: TObject);
- begin
- DeleteLocalFolder();
- end;
-
- { ------------------------------------------------------------- }
- { When a path is selected from the local paths combo box, this }
- { procedure is triggered to update the contents of the local }
- { file list ListView control }
- { ------------------------------------------------------------- }
- procedure TfrmMain.cboLocalPathsClick(Sender: TObject);
- var
- sPath : string;
- begin
- sPath := cboLocalPaths.Items[cboLocalPaths.ItemIndex];
-
- ClearLocalListView();
- RetrieveLocalFolderContents(sPath);
-
- m_sCurrentLocalPath := sPath; { Set the new current local path }
- end;
-
- { ------------------------------------------------------------- }
- { When a path is selected from the remote paths combo box, this }
- { procedure is triggered to update the contents of the remote }
- { file list ListView control }
- { ------------------------------------------------------------- }
- procedure TfrmMain.cboRemotePathsClick(Sender: TObject);
- var
- sPath : string;
- begin
- sPath := cboRemotePaths.Items[cboRemotePaths.ItemIndex];
-
- ChangeCurrentRemoteFolder(sPath);
- ClearRemoteListView();
- RetrieveRemoteFolderContents();
- m_sCurrentRemotePath := sPath; { Set the enew remote path }
- end;
-
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.btConnectClick(Sender: TObject);
- begin
- DoConnect();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.miConnectClick(Sender: TObject);
- begin
- DoConnect();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.btDisconnectClick(Sender: TObject);
- begin
- DoDisconnect();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.miDisconnectClick(Sender: TObject);
- begin
- DoDisconnect();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.btAbortClick(Sender: TObject);
- begin
- DoAbort();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.miAbortClick(Sender: TObject);
- begin
- DoAbort();
- end;
-
- { ------------------------------------------------------------- }
- { Change the representation type to Binary }
- { ------------------------------------------------------------- }
- procedure TfrmMain.optBinaryClick(Sender: TObject);
- begin
- try
- xFtp.RepresentationType := frtBinary;
- except
- on xErr: Exception do
- DisplayMessage(xErr.Message);
- end;
- end;
-
- { ------------------------------------------------------------- }
- { Change the representation type to ASCII }
- { ------------------------------------------------------------- }
- procedure TfrmMain.optAsciiClick(Sender: TObject);
- begin
- try
- xFtp.RepresentationType := frtASCII;
- except
- on xErr: Exception do
- DisplayMessage(xErr.Message);
- end;
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.btDeleteRemoteFileClick(Sender: TObject);
- begin
- DeleteSelectedRemoteFile();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.miDeleteRemoteFileClick(Sender: TObject);
- begin
- DeleteSelectedRemoteFile();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.btRenameRemoteFileClick(Sender: TObject);
- begin
- RenameRemoteFile();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.miRenameRemoteFileClick(Sender: TObject);
- begin
- RenameRemoteFile();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.btCreateRemoteFolderClick(Sender: TObject);
- begin
- CreateRemoteFolder();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.miCreateRemoteFolderClick(Sender: TObject);
- begin
- CreateRemoteFolder();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.btDeleteRemoteFolderClick(Sender: TObject);
- begin
- DeleteRemoteFolder();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.miDeleteRemoteFolderClick(Sender: TObject);
- begin
- DeleteRemoteFolder();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.pmiDeleteLocalFileClick(Sender: TObject);
- begin
- DeleteSelectedLocalFile();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.pmiRenameLocalFileClick(Sender: TObject);
- begin
- RenameLocalFile();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.pmiCreateLocalFolderClick(Sender: TObject);
- begin
- CreateLocalFolder();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.pmiDeleteLocalFolderClick(Sender: TObject);
- begin
- DeleteLocalFolder();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.pmiDeleteRemoteFileClick(Sender: TObject);
- begin
- DeleteSelectedRemoteFile();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.pmiRenameRemoteFileClick(Sender: TObject);
- begin
- RenameRemoteFile();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.pmiCreateRemoteFolderClick(Sender: TObject);
- begin
- CreateRemoteFolder();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.pmiDeleteRemoteFolderClick(Sender: TObject);
- begin
- DeleteRemoteFolder();
- end;
-
- { ------------------------------------------------------------- }
- { Change the current remote folder to its parent folder and }
- { list the contents of the parent folder }
- { ------------------------------------------------------------- }
- procedure TfrmMain.btParentRemoteFolderClick(Sender: TObject);
- begin
- GetParentFolder();
- m_sCurrentRemotePath := GetRemoteFolder(); { Set the new current remote path }
- ClearRemoteListView();
- RetrieveRemoteFolderContents();
- cboRemotePaths.Text := m_sCurrentRemotePath;
- UpdateRemoteState();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.btSendFilesClick(Sender: TObject);
- begin
- DoSendFiles();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.miSendClick(Sender: TObject);
- begin
- DoSendFiles();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.btReceiveFilesClick(Sender: TObject);
- begin
- DoReceiveFiles();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.miReceiveClick(Sender: TObject);
- begin
- DoReceiveFiles();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.lstLocalViewClick(Sender: TObject);
- begin
- UpdateLocalState();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.lstRemoteViewClick(Sender: TObject);
- begin
- UpdateRemoteState();
- end;
-
- { ------------------------------------------------------------- }
- procedure TfrmMain.xFtpDisconnected(Sender: TObject);
- begin
- stbStatus.Panels[0].Text := 'Disconnected from server';
- end;
-
- { ------------------------------------------------------------- }
- { The ListingFolderItem event is triggered by the }
- { ListFolderContents method and provides us with information on }
- { every file, folder or link. }
- { ------------------------------------------------------------- }
- procedure TfrmMain.xFtpListingFolderItem(Sender: TObject;
- const sName: WideString; dtDate: TDateTime; lFileSize: Integer;
- eItemType: TOleEnum; const sUserData: WideString);
- begin
- NewRemoteEntry(sName, dtDate, lFileSize, eItemType);
- end;
-
- { ------------------------------------------------------------- }
- { The FileTransferStatus event provides us with a variety of }
- { information regarding the transfer status of each individual }
- { file and the information on the global progress of the }
- { current operation. }
- { ------------------------------------------------------------- }
- procedure TfrmMain.xFtpFileTransferStatus(Sender: TObject;
- const sLocalFilename, sRemoteFilename: WideString;
- lFileSize,lBytesTransferred: Integer;
- nBytesPercent: Smallint;
- lTotalSize, lTotalBytesTransferred: Integer;
- nTotalBytesPercent: Smallint;
- lTotalFiles, lTotalFilesTransferred: Integer;
- nTotalFilesPercent: Smallint;
- lBytesPerSecond, lTotalBytesPerSecond: Integer);
- begin
- stbStatus.Panels[1].Text := 'Kb : ' + IntToStr((lBytesTransferred + 1023)
- div 1024);
- stbStatus.Panels[2].Text := 'Total : ' + IntToStr((lTotalBytesTransferred
- + 1023) div 1024);
- pgrProgess.Position := nTotalBytesPercent;
- end;
-
- { ------------------------------------------------------------- }
- { The LoggingCommandLine event is triggered for every command }
- { sent and received from the FTP server. Instead of writing our }
- { own custom messages for every possible action we will be }
- { using the information provided by the FTP server. }
- { ------------------------------------------------------------- }
- procedure TfrmMain.xFtpLoggingCommandLine(Sender: TObject;
- const sLine: WideString; eCommandType: TOleEnum);
- begin
- DisplayMessage(sLine);
- end;
-
- { ------------------------------------------------------------- }
- { If an account is required the AccountRequired event will be }
- { triggered in order to prompt the user to enter the account }
- { information. }
- { ------------------------------------------------------------- }
- procedure TfrmMain.xFtpAccountRequired(Sender: TObject;
- var sAccountName: WideString);
- var
- sNewAccountName : string;
- begin
- sNewAccountName := InputBox('FTP Client sample application', 'Please enter a valid ' +
- 'account name.', '');
- if sNewAccountName <> '' then
- begin
- sAccountName := sNewAccountName; { Set the new account name }
- end;
- end;
-
- { ------------------------------------------------------------- }
- { If a password is required or if an invalid password was }
- { provided, the PasswordRequired event will be triggered. }
- { ------------------------------------------------------------- }
- procedure TfrmMain.xFtpPasswordRequired(Sender: TObject;
- var sPassword: WideString);
- var
- sNewPassword : string;
- begin
- sNewPassword := InputBox('Xceed Ftp Client Sample', 'Please enter a valid ' +
- 'password.', '');
- if sNewPassword <> '' then
- begin
- sPassword := sNewPassword; { Set the new password }
- end;
- end;
-
- { ------------------------------------------------------------- }
- { The ReceivingFile event is triggered for every file that is }
- { about to be received and provides us with information }
- { regarding that file. It also gives us the option to change }
- { the filename of the file we are receiving. }
- { ------------------------------------------------------------- }
- procedure TfrmMain.xFtpReceivingFile(Sender: TObject;
- const sRemoteFilename: WideString; var sLocalFilename: WideString;
- lFileSize: Integer);
- begin
- stbStatus.Panels[0].Text := 'Receiving ' + sRemoteFilename;
- end;
-
- { ------------------------------------------------------------- }
- { If the file being received or sent exists, the ReplacingFile }
- { event gives us the option of overwriting, skipping it, etc... }
- { ------------------------------------------------------------- }
- procedure TfrmMain.xFtpReplacingFile(Sender: TObject;
- var sFilename: WideString;
- dtDate: TDateTime; lFileSize: Integer;
- const sRemoteFilename: WideString;
- var eAction: TOleEnum);
- begin
- if m_bReplaceAll then
- eAction := fraOverwrite { Overwrite the existing file }
- else
- begin
- case MessageDlg(sFilename + ' already exists. Do you want to replace it?',
- mtConfirmation, [mbYes, mbNo, mbAll], 0) of
- mrYes : eAction := fraOverwrite; { Overwrite the existing file }
- mrNo : eAction := fraSkip; { Skip the existing file }
- mrAll :
- begin
- eAction := fraOverwrite; { Overwrite the existing file }
- m_bReplaceAll := true;
- end;
- end;
- end;
- end;
-
- { ------------------------------------------------------------ }
- { The SendFile event is triggered for evert file that is about }
- { to be sent and provides us with information regaring that }
- { file, It also gives us the option to change the filename of }
- { the file we are sending. }
- { ------------------------------------------------------------ }
- procedure TfrmMain.xFtpSendingFile(Sender: TObject;
- const sLocalFilename: WideString; var sRemoteFilename: WideString;
- lFileSize: Integer);
- begin
- stbStatus.Panels[0].Text := 'Sending ' + sRemoteFilename;
- end;
-
- { ------------------------------------------------------------- }
- { For every file that is skipping, wether sending or }
- { receiveing, the SkippingFile event will be trigger to let us }
- { know which file is being skipped and for what reason. }
- { ------------------------------------------------------------- }
- procedure TfrmMain.xFtpSkippingFile(Sender: TObject;
- const sLocalFilename, sRemoteFilename: WideString;
- lSkippingReason: Integer);
- begin
- if sLocalFilename = '' then
- DisplayMessage(sRemoteFilename + ' was skipped. Reason #' + IntToStr
- (lSkippingReason));
- if sRemoteFilename = ''then
- DisplayMessage(sLocalFilename + ' was skipped. Reason #' + IntToStr
- (lSkippingReason));
- end;
-
- { ------------------------------------------------------------- }
- { Only accept drag and drop in the remote listview control if }
- { the source is the local listview control and we are connected }
- { the the FTP server }
- { ------------------------------------------------------------- }
- procedure TfrmMain.lstRemoteViewDragOver(Sender, Source: TObject;
- X, Y: Integer; State: TDragState;
- var Accept: Boolean);
- begin
- if (Source = lstLocalView) and (GetCurrentConnectionState = fstConnected) then
- Accept := true
- else
- Accept := false;
- end;
-
- { ------------------------------------------------------------- }
- { Only accept drag and drop in the local listview control if }
- { the source is the remote }
- { ------------------------------------------------------------- }
- procedure TfrmMain.lstLocalViewDragOver(Sender, Source: TObject;
- X, Y: Integer; State: TDragState;
- var Accept: Boolean);
- begin
- if Source = lstRemoteView then
- Accept := true
- else
- Accept := false;
- end;
-
- { ------------------------------------------------------------- }
- { If the files are "dropped" into the remote listview control, }
- { we will call the DoSendFiles procedure to send the files. }
- { If they are "dropped" into the local listview control, we }
- { call the DoReceiveFiles procedure whic will receive the files }
- { ------------------------------------------------------------- }
- procedure TfrmMain.lstDragDrop(Sender, Source: TObject; X, Y: Integer);
- begin
- If Source = lstRemoteView then
- DoReceiveFiles()
- Else if Source = lstLocalView then
- DoSendFiles();
- end;
-
- { ------------------------------------------------------------- }
- { If form is closed, make sure we disconnect!
- { ------------------------------------------------------------- }
- procedure TfrmMain.FormDestroy(Sender: TObject);
- begin
- DoDisconnect();
- end;
- end.
-
-