mainform.cpp
来自「著名的SecureBlackBox控件完整源码」· C++ 代码 · 共 524 行 · 第 1/2 页
CPP
524 行
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#undef SetPort
#include "MainForm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "SBSimpleSftp"
#pragma resource "*.dfm"
TfrmMain *frmMain;
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
: TForm(Owner)
{
SetLicenseKey((AnsiString)
"ADDCD14AD06709806817E0B3D7BFD0A2222D536FE156466C5D5FE65DB5DEAE76" +
"FFDEBC07E915A5751C12C01C783958872A38E4A5EDA140E7247E0F2E56442A3C" +
"F3E9347AD8FDE52083A0DFC86BC00ECB0FD0CF1B51159A2BCB84F6EA6349EF47" +
"5C15A59AFCC55F7C3AAD26C279628B5D91B1DC94BD2385354A70CCA3B76101D9" +
"F41C84A639FC3CCE4BA8F0CC4A66DCD150114A3F58C1AD46B7B94643741BC20A" +
"8DCA83AB921480951B423CAA19EF1863A47CA2C3422E7E5634BED98939A5AE43" +
"DE1E4BAD79E66D8A5C973B3455656C8C9B6FF024FADD6CDA02D0F506D98493C8" +
"BD1ED7B237DB75FA31F2C82654490CDDDEE24E19939137B9E1DB05508733B22F");
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::FormCreate(TObject *Sender)
{
FKeyStorage = new TElSSHMemoryKeyStorage(this);
SftpClient->KeyStorage = FKeyStorage;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::FormDestroy(TObject *Sender)
{
delete FKeyStorage;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::tbConnectClick(TObject *Sender)
{
Connect();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::tbDisconnectClick(TObject *Sender)
{
Disconnect();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::tbRenameClick(TObject *Sender)
{
Rename();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::tbMakeDirClick(TObject *Sender)
{
MakeDir();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::tbDeleteClick(TObject *Sender)
{
Delete();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::tbDownloadClick(TObject *Sender)
{
Download();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::tbUploadClick(TObject *Sender)
{
Upload();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::tbRefreshClick(TObject *Sender)
{
Refresh();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::SftpClientAuthenticationFailed(TObject *Sender,
int AuthenticationType)
{
Log((AnsiString)"Authentication type [" + IntToStr(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 " + IntToStr(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::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 TfrmMain::Connect(void)
{
if (SftpClient->Active)
{
MessageDlg("Already connected", mtInformation, TMsgDlgButtons()<<mbOK, 0);
return;
}
if (frmConnProps->ShowModal() == mrOk)
{
if (frmConnProps->editHost->Text.Pos(":") > 0)
{
SftpClient->Address = frmConnProps->editHost->Text.SubString(1, frmConnProps->editHost->Text.Pos(":") - 1);
SftpClient->Port = StrToIntDef(frmConnProps->editHost->Text.SubString(frmConnProps->editHost->Text.Pos(":") + 1, frmConnProps->editHost->Text.Length()), 22);
}
else
{
SftpClient->Address = frmConnProps->editHost->Text;
SftpClient->Port = 22;
}
//SftpClient.CompressionAlgorithms[SSH_CA_ZLIB] := false;
SftpClient->EncryptionAlgorithms[SSH_EA_3DES] = false;
SftpClient->EncryptionAlgorithms[SSH_EA_DES] = false;
SftpClient->EncryptionAlgorithms[SSH_EA_BLOWFISH] = false;
//SftpClient->EncryptionAlgorithms[SSH_EA_AES128] = false;
//SftpClient->EncryptionAlgorithms[SSH_EA_AES192] = false;
//SftpClient->EncryptionAlgorithms[SSH_EA_AES256] = false;
SftpClient->Username = frmConnProps->editUsername->Text;
SftpClient->Password = frmConnProps->editPassword->Text;
FKeyStorage->Clear();
TElSSHKey* Key = new TElSSHKey;
if ((!frmConnProps->edPrivateKey->Text.IsEmpty()) && FileExists(frmConnProps->edPrivateKey->Text) &&
(Key->LoadPrivateKey(frmConnProps->edPrivateKey->Text, "") == 0))
{
FKeyStorage->Add(Key);
SftpClient->AuthenticationTypes = SftpClient->AuthenticationTypes | SSH_AUTH_TYPE_PUBLICKEY;
}
else
SftpClient->AuthenticationTypes = SftpClient->AuthenticationTypes & (~SSH_AUTH_TYPE_PUBLICKEY);
delete Key;
Log((AnsiString)"Connecting to " + SftpClient->Address);
try
{
SftpClient->Open();
}
catch(Exception &e)
{
Log((AnsiString)"Sftp connection failed with message [" + e.Message + "]", true);
SftpClient->Close(false);
return;
}
Log("Sftp connection established");
FCurrentDir = ".";
Refresh();
}
}
//---------------------------------------------------------------------------
void TfrmMain::Disconnect(void)
{
Log("Disconnecting");
SftpClient->Close(false);
}
//---------------------------------------------------------------------------
void TfrmMain::Rename(void)
{
AnsiString NewName;
if (SftpClient->Active && (lvFiles->Selected != NULL) && (lvFiles->Selected->Data != NULL))
{
NewName = InputBox("Rename", (AnsiString)"Please enter the new name for " + ((TElSftpFileInfo*)(lvFiles->Selected->Data))->Name, "");
if (NewName.IsEmpty())
return;
Log((AnsiString)"Renaming " + ((TElSftpFileInfo*)(lvFiles->Selected->Data))->Name + " to " + NewName);
try
{
SftpClient->RenameFile(FCurrentDir + "/" + ((TElSftpFileInfo*)(lvFiles->Selected->Data))->Name,
FCurrentDir + "/" + NewName);
}
catch(Exception &e)
{
Log((AnsiString)"Failed to rename file \"" + ((TElSftpFileInfo*)(lvFiles->Selected->Data))->Name + "\" to \"" +
NewName + "\", " + e.Message, true);
}
Refresh();
}
}
//---------------------------------------------------------------------------
void TfrmMain::MakeDir(void)
{
AnsiString DirName;
if (SftpClient->Active)
{
DirName = InputBox("Make directory", "Please enter the name for new directory", "");
if (DirName.IsEmpty())
return;
Log((AnsiString)"Creating directory " + DirName);
try
{
SftpClient->MakeDirectory(FCurrentDir + "/" + DirName, NULL);
}
catch(Exception &e)
{
Log((AnsiString)"Failed to create directory \"" + DirName + "\", " + e.Message, true);
}
Refresh();
}
}
//---------------------------------------------------------------------------
void TfrmMain::Delete(void)
{
if (SftpClient->Active && (lvFiles->Selected != NULL) && (lvFiles->Selected->Data != NULL))
{
if (MessageDlg((AnsiString)"Please confirm that you want to delete \"" +
((TElSftpFileInfo*)(lvFiles->Selected->Data))->Name + "\"", mtConfirmation,
TMsgDlgButtons()<<mbYes<<mbNo, 0) == mrYes)
{
Log((AnsiString)"Removing item " + ((TElSftpFileInfo*)(lvFiles->Selected->Data))->Name);
try
{
if (((TElSftpFileInfo*)(lvFiles->Selected->Data))->Attributes->Directory)
SftpClient->SftpRemoveDirectory(FCurrentDir + "/" + ((TElSftpFileInfo*)(lvFiles->Selected->Data))->Name);
else
SftpClient->SftpRemoveFile(FCurrentDir + "/" + ((TElSftpFileInfo*)(lvFiles->Selected->Data))->Name);
}
catch(Exception &e)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?