📄 listcontent.cpp
字号:
// ListContent.cpp : implementation file
//
#include "stdafx.h"
#include "IntelligentFtp.h"
#include "ListContent.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CListContent dialog
CListContent::CListContent(CWnd* pParent /*=NULL*/)
: CDialog(CListContent::IDD, pParent)
{
//{{AFX_DATA_INIT(CListContent)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_FtpSite = "202.112.154.157";
m_Directory = "/";
m_FileName = "" ;
SEPARATOR = ".";
pSession = NULL;
pConnection = NULL;
pFileFind = NULL;
p_Database = NULL;
p_Record = NULL;
m_IsConnected = FALSE;
m_IsDbOpen = FALSE;
m_Paths.RemoveAll();
m_URLs .RemoveAll();
}
void CListContent::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CListContent)
DDX_Control(pDX, IDC_MSG, m_wndMsg);
DDX_Control(pDX, IDC_DOWNTODB, m_btnDownToDb);
DDX_Control(pDX, IDC_GETCONTENT, m_btnGetContent);
DDX_Control(pDX, IDC_CONTENTLIST, m_listContent);
DDX_Control(pDX, IDC_IPADDRESS_DEST, m_IPAddrDest);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CListContent, CDialog)
//{{AFX_MSG_MAP(CListContent)
ON_BN_CLICKED(IDC_GETCONTENT, OnGetContent)
ON_NOTIFY(NM_DBLCLK, IDC_CONTENTLIST, OnDblclkContentlist)
ON_BN_CLICKED(IDC_DOWNTODB, OnDownToDB)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CListContent message handlers
void CListContent::OnOK()
{
// TODO: Add extra validation here
// CDialog::OnOK();
}
void CListContent::OnCancel()
{
// TODO: Add extra cleanup here
// CDialog::OnCancel();
}
BOOL CListContent::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
m_IPAddrDest.SetWindowText(m_FtpSite);
m_IPAddrDest.SetFieldRange(0,1,254);
m_IPAddrDest.SetFieldRange(1,1,254);
m_IPAddrDest.SetFieldRange(2,1,254);
m_IPAddrDest.SetFieldRange(3,1,254);
m_listContent.InsertColumn(0,"URL",LVCFMT_LEFT,150);
m_listContent.InsertColumn(0,"FtpSite",LVCFMT_LEFT,100);
m_listContent.InsertColumn(0,"FileLength",LVCFMT_LEFT,100);
m_listContent.InsertColumn(0,"FileName",LVCFMT_LEFT,100);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CListContent::OnGetContent()
{
m_IPAddrDest.GetWindowText(m_FtpSite);
if(!m_IsConnected) {
if(ConnectToServer()) {
m_IsConnected = TRUE;
m_btnGetContent.SetWindowText("断开连接");
}
else {
return ;
}
m_Directory = "/";
DownLoadContent();
}
else {
DisconnectedFromServer();
m_IsConnected = FALSE;
m_btnGetContent.SetWindowText("连接主机");
m_listContent.DeleteAllItems();
}
}
BOOL CListContent::ConnectToServer()
{
pSession = new CInternetSession(AfxGetAppName(),
1,
PRE_CONFIG_INTERNET_ACCESS);
try
{
pConnection = pSession->GetFtpConnection(m_FtpSite);
}
catch(CInternetException* e)
{
pConnection = NULL;
e->Delete();
}
if(pConnection != NULL)
{
return TRUE;
}
return FALSE;
}
void CListContent::DisconnectedFromServer()
{
if(m_IsConnected) {
if(pFileFind != NULL)
{
pFileFind->Close();
delete pFileFind;
pFileFind = NULL;
}
if(pConnection != NULL)
{
pConnection->Close();
delete pConnection;
pConnection = NULL;
}
m_IsConnected = FALSE;
m_btnGetContent.EnableWindow(TRUE);
}
}
void CListContent::DownLoadContent()
{
BOOL IsContinue = FALSE;
CString strLen = "0";
if(m_IsConnected) {
pFileFind = new CFtpFileFind(pConnection);
m_wndMsg.SetWindowText(m_Directory);
if(!pConnection->SetCurrentDirectory(m_Directory))
{
pFileFind->Close();
delete pFileFind;
pFileFind = NULL;
return ;
}
IsContinue = pFileFind->FindFile("*");
m_listContent.DeleteAllItems();
while(IsContinue)
{
IsContinue = pFileFind->FindNextFile();
m_FileName = pFileFind->GetFileName();
if(pFileFind->IsDirectory())
{
int i = m_listContent.InsertItem(0,m_FileName);
m_listContent.SetItemText(i,1,"Directory");
m_listContent.SetItemText(i,2,m_FtpSite);
}
else
{
int i = m_listContent.InsertItem(0,m_FileName);
strLen.Format("%d",pFileFind->GetLength());
m_listContent.SetItemText(i,1,strLen);
m_listContent.SetItemText(i,2,m_FtpSite);
}
}
if(m_Directory != "/")
{
int i = m_listContent.InsertItem(0,"..");
m_listContent.SetItemText(i,1,"Parent");
}
pFileFind->Close();
delete pFileFind;
pFileFind = NULL;
}
}
void CListContent::OnDblclkContentlist(NMHDR* pNMHDR, LRESULT* pResult)
{
CString pPath = "";
//
if(m_listContent.GetItemText(m_listContent.GetSelectionMark(),1) == "Directory") {
pPath = m_listContent.GetItemText(m_listContent.GetSelectionMark(),0);
m_Paths.Add(pPath);
ReAssembleDirectory(TRUE);
DownLoadContent();
}
else if(m_listContent.GetItemText(m_listContent.GetSelectionMark(),1) == "Parent") {
ReAssembleDirectory(FALSE);
DownLoadContent();
}
*pResult = 0;
}
void CListContent::ReAssembleDirectory(BOOL bFlag)
{
m_Directory = "/";
if(!bFlag) {
m_Paths.RemoveAt(m_Paths.GetSize()-1);
}
//ShowMessage(TRUE);
int i = m_Paths.GetSize();
for(int j =0; j < i; j ++) {
if(m_Paths[j] != "/") {
m_Directory += m_Paths[j];
m_Directory += "/";
}
}
}
void CListContent::OnDownToDB()
{
if(ConnectToServer()) {
m_IsConnected = TRUE;
m_btnGetContent.EnableWindow(FALSE);
m_btnDownToDb.EnableWindow(FALSE);
}
else {
return ;
}
if(!OpenDb()) {
return ;
}
m_Directory = "/";
m_Paths.RemoveAll();
m_URLs .RemoveAll();
m_URLs.Add(m_Directory);
DownLoadAllContent();
}
BOOL CListContent::ReAssemblePath()
{
int i = m_URLs.GetSize();
int j = m_Paths.GetSize();
if(m_URLs[i-1] == ".") {
while( i ) {
if(m_URLs[i-1] == ".") {
m_URLs .RemoveAt(i-1);
m_Paths.RemoveAt(j-1);
}
else {
break; //
}
i = m_URLs .GetSize();
j = m_Paths.GetSize();
}
}
if((m_Paths.GetSize() > 1)&&(m_Paths[1] == "windows")) {
ReAssemblePath();
}
//ShowMessage(TRUE);
//ShowMessage(FALSE);
if(m_URLs.GetSize()) {
m_Paths.Add(m_URLs.GetAt(i-1));
m_URLs.RemoveAt(i-1);
m_URLs.Add(SEPARATOR);
//ShowMessage(TRUE);
//ShowMessage(FALSE);
ReAssembleDirectory(TRUE);
}
else {
return FALSE;
}
return TRUE;
}
void CListContent::DownLoadAllContent()
{
BOOL IsContinue = FALSE;
CString strLen = "0";
if(m_IsConnected && m_IsDbOpen) {
pFileFind = new CFtpFileFind(pConnection);
while(m_URLs.GetSize())
{
Sleep(100);
//ShowMessage(TRUE);
//ShowMessage(FALSE);
if(!ReAssemblePath()) {
break;
}
m_wndMsg.SetWindowText(m_Directory);
if(!pConnection->SetCurrentDirectory(m_Directory))
{
pFileFind->Close();
delete pFileFind;
pFileFind = NULL;
return ;
}
IsContinue = pFileFind->FindFile("*");
m_listContent.DeleteAllItems();
while(IsContinue)
{
IsContinue = pFileFind->FindNextFile();
m_FileName = pFileFind->GetFileName();
if(pFileFind->IsDirectory())
{
int i = m_listContent.InsertItem(0,m_FileName);
m_listContent.SetItemText(i,1,"Directory");
m_listContent.SetItemText(i,2,m_FtpSite);
p_Record->AddNew();
p_Record->m_FileName = m_FileName;
p_Record->m_FileLength = 0;
p_Record->m_IP = m_FtpSite;
p_Record->m_IsDirectory = TRUE;
p_Record->m_Path = m_Directory;
p_Record->m_Port = 21;
p_Record->Update();
m_URLs.Add(m_FileName);
}
else
{
int i = m_listContent.InsertItem(0,m_FileName);
strLen.Format("%d",pFileFind->GetLength());
m_listContent.SetItemText(i,1,strLen);
m_listContent.SetItemText(i,2,m_FtpSite);
p_Record->AddNew();
p_Record->m_FileName = m_FileName;
p_Record->m_FileLength = pFileFind->GetLength();
p_Record->m_IP = m_FtpSite;
p_Record->m_IsDirectory = FALSE;
p_Record->m_Path = m_Directory;
p_Record->m_Port = 21;
p_Record->Update();
}
}
m_listContent.UpdateWindow();
}
pFileFind->Close();
delete pFileFind;
pFileFind = NULL;
}
}
void CListContent::ShowMessage(BOOL bFlag)
{
CString msg;
if(bFlag) {
msg = "m_Paths : ";
int i = m_Paths.GetSize();
while(i) {
msg += m_Paths.GetAt(i-1);
msg += "\n";
i--;
}
}
else {
msg = "m_URLs : ";
int i = m_URLs.GetSize();
while(i) {
msg += m_URLs.GetAt(i-1);
msg += "\n";
i--;
}
}
MessageBox(msg);
}
BOOL CListContent::OpenDb()
{
if(m_IsDbOpen) return TRUE;
p_Database = new CDatabase;
p_Database->SetLoginTimeout(5);
BOOL p_Status = FALSE;
try
{
p_Status = p_Database->Open("ftpdb");
if(p_Status)
TRACE("\nDB opened successfully\n");
else
TRACE("\nOpen DSN failed.\n");
}
catch(CMemoryException* pEx)
{
pEx->ReportError();
}
catch(CDBException* pDBEx)
{
pDBEx->ReportError();
TRACE("RetCOde:%d strError:[%d] strState: [%d]\n",
pDBEx->m_nRetCode,
pDBEx->m_strError,
pDBEx->m_strStateNativeOrigin);
}
if(!p_Status)
{
MessageBox("Open DSN Failed.");
if(p_Database->IsOpen()) p_Database->Close();
if(p_Database != NULL) delete p_Database;
return FALSE;
}
p_Record = new CFtpContent(p_Database);
if(!p_Record->Open(AFX_DB_USE_DEFAULT_TYPE,(LPCTSTR)"Content"))
{
MessageBox("Open Table SiteData Failed.");
if(p_Record->IsOpen()) p_Record->Close();
if(p_Database->IsOpen()) p_Database->Close();
delete p_Record;
delete p_Database;
return FALSE;
}
m_IsDbOpen = TRUE;
return TRUE;
}
void CListContent::CloseDb()
{
if(!m_IsDbOpen) return;
if(p_Record->IsOpen()) p_Record->Close();
if(p_Database->IsOpen()) p_Database->Close();
delete p_Record;
delete p_Database;
m_IsDbOpen = FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -