mainform.cpp
来自「著名的SecureBlackBox控件完整源码」· C++ 代码 · 共 568 行 · 第 1/2 页
CPP
568 行
FCurrentDir = SftpClient->RequestAbsolutePath(FCurrentDir + "/" + ((TElSftpFileInfo*)(lvFiles->Selected->Data))->Name);
}
catch(Exception &e)
{
FCurrentDir = ".";
}
Refresh();
}
}
//---------------------------------------------------------------------------
int __fastcall FileListSort(void* Item1, void* Item2)
{
TElSftpFileInfo* Info1 = (TElSftpFileInfo*)Item1;
TElSftpFileInfo* Info2 = (TElSftpFileInfo*)Item2;
if (Info1->Attributes->Directory == Info2->Attributes->Directory)
return CompareText(Info1->Name, Info2->Name);
else
{
if (Info1->Attributes->Directory)
return -1;
else
return 1;
}
}
//---------------------------------------------------------------------------
void TfrmMain::Refresh(void)
{
ClearFileList();
if (!SftpClient->Active) return;
try
{
FCurrentDir = SftpClient->RequestAbsolutePath(FCurrentDir);
}
catch(Exception &e)
{
FCurrentDir = ".";
}
lPath->Caption = FCurrentDir;
Log("Retrieving file list");
try
{
AnsiString dirHandle = SftpClient->OpenDirectory(FCurrentDir);
TList* dirList = new TList;
try
{
SftpClient->ReadDirectory(dirHandle, dirList);
dirList->Sort(FileListSort);
for (int i = 0; i< dirList->Count; i++)
{
TElSftpFileInfo* Info = new TElSftpFileInfo;
((TElSftpFileInfo*)(dirList->Items[i]))->CopyTo(Info);
TListItem*Item = lvFiles->Items->Add();
Item->Data = Info;
Item->Caption = Info->Name;
if (!Info->Attributes->Directory)
{
Item->SubItems->Add(IntToStr(Info->Attributes->Size));
Item->ImageIndex = 9;
}
else
{
Item->SubItems->Add("");
Item->ImageIndex = 8;
}
Item->SubItems->Add(DateTimeToStr(Info->Attributes->MTime));
Item->SubItems->Add(Info->Attributes->Owner);
Item->SubItems->Add(FormatRights(Info->Attributes));
}
}
__finally
{
delete dirList;
SftpClient->CloseHandle(dirHandle);
}
}
catch(Exception &e)
{
Log("Failed to retrieve file list");
return;
}
}
//---------------------------------------------------------------------------
void TfrmMain::Log(AnsiString const S, bool Error)
{
TListItem* Item = lvLog->Items->Add();
Item->Caption = TimeToStr(TDateTime::CurrentDateTime());
Item->SubItems->Add(S);
if (Error)
Item->ImageIndex = 11;
else
Item->ImageIndex = 10;
}
//---------------------------------------------------------------------------
void TfrmMain::ClearFileList(void)
{
try
{
for (int i = 0; i < lvFiles->Items->Count; i++)
delete ((TElSftpFileInfo*)(lvFiles->Items->Item[i]->Data));
}
__finally
{
lvFiles->Items->Clear();
}
}
//---------------------------------------------------------------------------
AnsiString TfrmMain::FormatRights(TElSftpFileAttributes* Attributes)
{
AnsiString Result = "";
if (Attributes->Directory)
Result = Result + "d";
if (Attributes->UserRead)
Result = Result + "r";
else
Result = Result + "-";
if (Attributes->UserWrite)
Result = Result + "w";
else
Result = Result + "-";
if (Attributes->UserExecute)
Result = Result + "x";
else
Result = Result + "-";
if (Attributes->GroupRead)
Result = Result + "r";
else
Result = Result + "-";
if (Attributes->GroupWrite)
Result = Result + "w";
else
Result = Result + "-";
if (Attributes->GroupExecute)
Result = Result + "x";
else
Result = Result + "-";
if (Attributes->OtherRead)
Result = Result + "r";
else
Result = Result + "-";
if (Attributes->OtherWrite)
Result = Result + "w";
else
Result = Result + "-";
if (Attributes->OtherExecute)
Result = Result + "x";
else
Result = Result + "-";
return Result;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::SftpClientAuthenticationFailed(TObject *Sender,
int AuthenticationType)
{
Log((AnsiString)"Authentication type [" + AuthenticationType + "] failed", true);
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::SftpClientAuthenticationSuccess(TObject *Sender)
{
Log("Authentication succeeded");
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::SftpClientCloseConnection(TObject *Sender)
{
Log("Sftp connection closed");
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::SftpClientError(TObject *Sender, int ErrorCode)
{
Log((AnsiString)"Error " + ErrorCode, true);
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::SftpClientKeyValidate(TObject *Sender,
TElSSHKey *ServerKey, bool &Validate)
{
Log((AnsiString)"Server key [" + DigestToStr(ServerKey->FingerprintMD5, false) + "] received");
Validate = true;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::SftpClientReceive(TObject *Sender,
Pointer Buffer, int MaxSize, int Written)
{
///////////////////////////////////
// int Written <=> out Written: Integer
///////////////////////////////////
if (sckClient->InputBuffer->Size == 0)
{
sckClient->ReadFromStack(false, 10, false);
sckClient->CheckForDisconnect(false, true);
}
if (sckClient->Connected() && (sckClient->InputBuffer->Size > 0))
{
Written = Sbutils::Min(int(MaxSize), int(sckClient->InputBuffer->Size));
sckClient->InputBuffer->Position = 0;
Written = sckClient->InputBuffer->Read(Buffer, Written);
sckClient->InputBuffer->Remove(Written);
}
else
Written = 0;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::SftpClientSend(TObject *Sender, Pointer Buffer,
int Size)
{
sckClient->WriteBuffer(Buffer, Size, true);
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::sckClientDisconnected(TObject *Sender)
{
Log("Socket connection closed");
}
/*
procedure TfrmMain.sckClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Log('Socket connection closed');
end;
*/
//---------------------------------------------------------------------------
/*
procedure TfrmMain.sckClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
Log('Socket error [' + IntToStr(ErrorCode) + ']', true);
end;
*/
//---------------------------------------------------------------------------
void __fastcall TfrmMain::lvFilesDblClick(TObject *Sender)
{
ChangeDir();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::mnuConnectClick(TObject *Sender)
{
Connect();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::mnuDisconnectClick(TObject *Sender)
{
Disconnect();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::mnuExitClick(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::mnuAboutClick(TObject *Sender)
{
frmAbout->ShowModal();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::FormCreate(TObject *Sender)
{
FKeyStorage = new TElSSHMemoryKeyStorage(this);
SftpClient->KeyStorage = FKeyStorage;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::FormDestroy(TObject *Sender)
{
delete FKeyStorage;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::SftpClientProgress(TObject *Sender,
__int64 Total, __int64 Current, bool &Cancel)
{
frmProgress->lProgress->Caption = "0 / " + IntToStr(Total);
frmProgress->pbProgress->Position = 0;
frmProgress->pbProgress->Position = Current * 100 / Total;
frmProgress->lProgress->Caption = IntToStr(Current) + " / " + IntToStr(Total);
Cancel = frmProgress->Canceled;
if ((Current == Total) || (frmProgress->Canceled))
{
frmProgress->Hide();
}
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?