//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "ftpf.h"
#include "ftploginf.h"
#include "ftpdialogf.h"
#include "ftpprogressf.h"
#include <dir.h>
#include <io.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "ipwftp"
#pragma resource "*.dfm"
TFormFtpf *FormFtpf;
//---------------------------------------------------------------------------
__fastcall TFormFtpf::TFormFtpf(TComponent* Owner)
: TForm(Owner)
{
State = OffLineConst;
LocFileSizes = new TStringList();
RemFileSizes = new TStringList();
ComboBoxLocHistory->Items->Insert(0, GetCurrentDir());
ComboBoxLocHistory->ItemIndex = 0;
UpdateLocal();
UpdateButtons();
}
//---------------------------------------------------------------------------
void TFormFtpf::UpdateLocal()
{
TSearchRec SearchRec;
// Show updated file list for the local system
// we know the value of CurrentLocDirectory here
Screen->Cursor = crAppStart;
ListBoxLocFiles->Clear();
LocFileSizes->Clear();
if( FindFirst("*", faAnyFile, SearchRec) == 0 )
do {
if( (SearchRec.Attr & faDirectory) != 0 )
ListBoxLocFiles->Items->Add("<DIR> " + SearchRec.Name);
else
ListBoxLocFiles->Items->Add(SearchRec.Name);
LocFileSizes->Add(IntToStr(SearchRec.Size));
} while (FindNext(SearchRec) == 0);
FindClose(SearchRec);
Screen->Cursor = crDefault;
}
//---------------------------------------------------------------------------
void TFormFtpf::UpdateButtons()
{
ButtonRemChgDir->Enabled = State == OnLineConst;
ButtonRemMkDir->Enabled = State == OnLineConst;
ButtonRemRename->Enabled = State == OnLineConst;
ButtonRemDelete->Enabled = State == OnLineConst;
ButtonRemRefresh->Enabled = State == OnLineConst;
ButtonDownload->Enabled = State == OnLineConst;
ButtonUpload->Enabled = State == OnLineConst;
if( State == OnLineConst )
ButtonConnectDisconnect->Caption = "Disconnect";
else
{
ComboBoxRemHistory->Clear();
ButtonConnectDisconnect->Caption = "Connect";
}
//ListBoxRemFiles->Enabled = State == OnLineConst;
ComboBoxRemHistory->Enabled = State == OnLineConst;
}
//---------------------------------------------------------------------------
void TFormFtpf::UpdateRemote()
{
// Show updated file list for the remote host
// Called only when connected
Screen->Cursor = crAppStart;
ListBoxRemFiles->Clear();
RemFileSizes->Clear();
ipwFTP1->RemoteFile = "";
try {
ipwFTP1->ListDirectoryLong();
}
catch (Exception &exception) {
try {
ipwFTP1->Interrupt();
}
catch (Exception &exception) {
UpdateNotes(exception.Message);
}
State = OffLineConst;
UpdateButtons();
}
Screen->Cursor = crDefault;
}
//---------------------------------------------------------------------------
void TFormFtpf::UpdateNotes(AnsiString Note)
{
// Add a message to the PI Trail box and scroll down to make it visible
ListBoxPiTrail->Items->Add(Note);
if( ListBoxPiTrail->Items->Count > 255 )
ListBoxPiTrail->Items->Delete(0);
if( ListBoxPiTrail->Items->Count > 3 )
ListBoxPiTrail->TopIndex = ListBoxPiTrail->Items->Count - 3;
}
//---------------------------------------------------------------------------
void TFormFtpf::UpdateComboBox(TComboBox *ComboBox, AnsiString ItemName)
{
int Pos;
Pos = ComboBox->Items->IndexOf(ItemName);
if( Pos > -1 )
ComboBox->Items->Delete(Pos);
ComboBox->Items->Insert(0, ItemName);
ComboBox->ItemIndex = 0;
}
//---------------------------------------------------------------------------
void __fastcall TFormFtpf::ButtonConnectDisconnectClick(TObject *Sender)
{
Screen->Cursor = crAppStart;
if( State == OffLineConst ) {
TFormFtploginf* formLogin = new TFormFtploginf(this);
if( formLogin->ShowModal() == mrOk )
{
ipwFTP1->RemotePath = "";
ipwFTP1->RemoteFile = "";
ipwFTP1->RemoteHost = formLogin->EditHostName->Text;
ipwFTP1->User = formLogin->EditUser->Text;
ipwFTP1->Password = formLogin->EditPassword->Text;
ComboBoxRemHistory->Clear();
try {
ipwFTP1->Logon();
State = OnLineConst;
ComboBoxRemHistory->Items->Insert(0, ipwFTP1->RemotePath);
ComboBoxRemHistory->ItemIndex = 0;
UpdateRemote();
}
catch (Exception &exception) {
UpdateNotes(exception.Message);
}
}
delete formLogin;
}
else
{
try {
ipwFTP1->Logoff();
}
catch (Exception &exception) {
UpdateNotes(exception.Message);
}
State = OffLineConst;
}
UpdateButtons();
Screen->Cursor = crDefault;
}
//---------------------------------------------------------------------------
void __fastcall TFormFtpf::ButtonExitClick(TObject *Sender)
{
if( State == OnLineConst )
{
Screen->Cursor = crAppStart;
try {
ipwFTP1->Logoff();
}
catch (Exception &exception) {
UpdateNotes(exception.Message);
}
Screen->Cursor = crDefault;
}
Close();
}
//---------------------------------------------------------------------------
void __fastcall TFormFtpf::ListBoxLocFilesDblClick(TObject *Sender)
{
AnsiString Name;
// Local System Box: if double-clicked on a file, send it
// if double-clicked on a directory, open it
Name = ListBoxLocFiles->Items->Strings[ListBoxLocFiles->ItemIndex];
if( Name[1] == '<' )
{
Name.Delete(1, 7); // Remove first seven characters, "<DIR> "
chdir(Name.c_str());
UpdateComboBox(ComboBoxLocHistory, GetCurrentDir());
UpdateLocal();
} else
ButtonUploadClick(NULL);
}
//---------------------------------------------------------------------------
void __fastcall TFormFtpf::ListBoxRemFilesDblClick(TObject *Sender)
{
AnsiString Name;
// Remote Host Box: if double-clicked on a file, get it
// if double-clicked on a directory, open it
Name = ListBoxRemFiles->Items->Strings[ListBoxRemFiles->ItemIndex];
if( Name[1] == '<' )
{
Name.Delete(1, 7); // Remove first seven characters, "<DIR> "
ipwFTP1->RemotePath = ipwFTP1->RemotePath + "/" + Name;
UpdateComboBox(ComboBoxRemHistory, ipwFTP1->RemotePath);
UpdateRemote();
} else
ButtonDownloadClick(NULL);
}
//---------------------------------------------------------------------------
void __fastcall TFormFtpf::RadioGroupTransferModeClick(TObject *Sender)
{
// Set Transfer Mode
Screen->Cursor = crAppStart;
try {
if( RadioGroupTransferMode->ItemIndex == 0 )
ipwFTP1->TransferMode = tmASCII;
else if( RadioGroupTransferMode->ItemIndex == 1 )
ipwFTP1->TransferMode = tmBinary;
else if( RadioGroupTransferMode->ItemIndex == 2 )
ipwFTP1->TransferMode = tmDefault;
}
catch (Exception &exception) {
UpdateNotes(exception.Message);
}
Screen->Cursor = crDefault;
}
//---------------------------------------------------------------------------
void __fastcall TFormFtpf::ButtonLocChgDirClick(TObject *Sender)
{
// Local ChgDir Button
TFormFtpdialogf * formTextLine = new TFormFtpdialogf(this);
formTextLine->Caption = "Change Directory";
formTextLine->LabelWhat->Caption = "New directory:";
formTextLine->EditLine->Text = GetCurrentDir();
if( formTextLine->ShowModal() == mrOk )
{
chdir((char*)formTextLine->EditLine->Text.c_str());
UpdateComboBox(ComboBoxLocHistory, GetCurrentDir());
评论22
最新资源