www.pudn.com > CraftFTP_gb.rar > CustomCmdDialog.cpp


// CustomCmdDialog.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "CraftFTP.h" 
#include "CustomCmdDialog.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
void CCustomFtpCmd::CopyFrom(CCustomFtpCmd * pCustomFtpCmd) 
{ 
	nCommandID		=	pCustomFtpCmd->nCommandID; 
	szCmdLabel		=	pCustomFtpCmd->szCmdLabel; 
	szCommand		=	pCustomFtpCmd->szCommand; 
	szDescription	=	pCustomFtpCmd->szDescription; 
} 
 
void CCustomCmdList::CreateNewFromList(CCustomCmdList * pList) 
{ 
	if(pList) 
	{ 
		for(POSITION pos=pList->GetHeadPosition(); pos!=NULL;) 
		{ 
			CCustomFtpCmd * pCustomFtpCmd = pList->GetNext(pos); 
			if(pCustomFtpCmd) 
			{ 
				CCustomFtpCmd * pNew = new CCustomFtpCmd; 
				pNew->CopyFrom(pCustomFtpCmd); 
 
				AddTail(pNew); 
			} 
		} 
	} 
} 
//从文件加载 
BOOL CCustomCmdList::LoadFromFile(LPCTSTR szPathName) 
{ 
	CString szCmdsFile = _T("commands.dat"); 
	if(szPathName && *szPathName) 
		szCmdsFile = szPathName; 
 
	CFile File; 
	if(File.Open(szCmdsFile, CFile::modeRead)) 
	{ 
		BYTE szSignature[12] = {0}; 
 
		BOOL bSucceed = (10 == File.Read(szSignature, 10) ? TRUE : FALSE); 
		if(!bSucceed)	//不成功 
		{ 
			File.Close(); 
			return FALSE; 
		} 
		else			//成功 
		{ 
			BYTE chSignature[20] = {0}; 
			CCraftFTPApp::Decode7Bit(szSignature, 10, chSignature); 
 
			if(strcmp((const char*)chSignature, _T("NEWMOONSOFT"))!=0) 
			{ 
				//签名无效 
				File.Close(); 
				return FALSE; 
			} 
			else 
			{ 
				//清除以前的内容 
				Cleanup(); 
 
				DWORD dwRead = 10; 
 
				BOOL bContinue = TRUE; 
				while(dwRead < File.GetLength() && bContinue) 
				{ 
					DWORD nSize = 0; 
					bSucceed = (sizeof(DWORD) == File.Read(&nSize, sizeof(DWORD)) ? TRUE : FALSE); 
					if(bSucceed && nSize < 900000000) 
					{ 
						dwRead += sizeof(DWORD); 
 
						BYTE * szbuffer = new BYTE[nSize+1]; 
						szbuffer[nSize] = '\0'; 
 
						bSucceed = (nSize == File.Read(szbuffer, nSize) ? TRUE : FALSE); 
						if(bSucceed) 
						{ 
							CCustomFtpCmd * fc = new CCustomFtpCmd; 
							fc->nCommandID	=	m_nCommandID++; 
 
							dwRead += nSize; 
							BYTE * p = szbuffer; 
 
							//命令标签 
							int * iSize = (int*)p; 
							p		+=	sizeof(int); 
							fc->szCmdLabel.GetBuffer(*iSize); 
							memcpy((void*)(LPCTSTR)fc->szCmdLabel, p, (size_t)*iSize); 
							fc->szCmdLabel.ReleaseBuffer(); 
							for(int i=0; i<*iSize; i++) 
								fc->szCmdLabel.SetAt(i, CCraftFTPApp::ExchangeBit(p[i], 4)); 
							p		+=	*iSize; 
 
							//命令内容 
							iSize = (int*)p; 
							p		+=	sizeof(int); 
							fc->szCommand.GetBuffer(*iSize); 
							memcpy((void*)(LPCTSTR)fc->szCommand, p, (size_t)*iSize); 
							fc->szCommand.ReleaseBuffer(); 
							for(i=0; i<*iSize; i++) 
								fc->szCommand.SetAt(i, CCraftFTPApp::ExchangeBit(p[i], 4)); 
							p		+=	*iSize; 
 
							//命令描述 
							iSize = (int*)p; 
							p		+=	sizeof(int); 
							fc->szDescription.GetBuffer(*iSize); 
							memcpy((void*)(LPCTSTR)fc->szDescription, p, (size_t)*iSize); 
							fc->szDescription.ReleaseBuffer(); 
							for(i=0; i<*iSize; i++) 
								fc->szDescription.SetAt(i, CCraftFTPApp::ExchangeBit(p[i], 4)); 
							p		+=	*iSize;							 
 
							//添加到列表 
							AddTail(fc); 
						} 
						else 
							bContinue = FALSE; 
						 
						delete szbuffer; 
					} 
					else 
						bContinue = FALSE; 
				} 
				File.Close(); 
			} 
		} 
	} 
	return TRUE; 
} 
 
void CCustomCmdList::SaveToFile(LPCTSTR szPathName) 
{ 
	CString szCmdsFile = _T("commands.dat"); 
	if(szPathName && *szPathName) 
		szCmdsFile = szPathName; 
 
	CFileStatus st; 
	if(CFile::GetStatus(szCmdsFile, st)) 
	{ 
		st.m_attribute = CFile::normal|CFile::archive; 
		CFile::SetStatus(szCmdsFile, st); 
	} 
 
	CFile File; 
	if(File.Open(szCmdsFile, CFile::modeCreate|CFile::modeWrite)) 
	{ 
		//文件签名标志 
		BYTE szSignature[12] = {0}; 
		CCraftFTPApp::Encode7Bit((unsigned char*)_T("NEWMOONSOFT"), szSignature); 
		File.Write(szSignature, 10); 
 
		POSITION pos = GetHeadPosition(); 
		while(pos) 
		{ 
			CCustomFtpCmd * fc = GetNext(pos); 
 
			DWORD nSize	=	fc->szCmdLabel.GetLength() + 
							fc->szCommand.GetLength() + 
							fc->szDescription.GetLength() + 
							sizeof(int) * 3 + sizeof(UINT); 
 
			BYTE * szbuffer = new BYTE[nSize + sizeof(DWORD)]; 
			BYTE * p = szbuffer; 
 
			//总长度 
			DWORD * size = (DWORD*)p; 
			*size	=	nSize; 
			p		+=	sizeof(DWORD); 
 
			//命令标签 
			int * iSize = (int*)p; 
			*iSize	=	fc->szCmdLabel.GetLength(); 
			p		+=	sizeof(int); 
			memcpy(p, (LPCTSTR)fc->szCmdLabel, (size_t)*iSize); 
			for(int i=0; i<*iSize; i++) 
				p[i] = CCraftFTPApp::ExchangeBit(p[i], 4); 
			p		+=	*iSize; 
 
			//命令内容 
			iSize	= (int*)p; 
			*iSize	=	fc->szCommand.GetLength(); 
			p		+=	sizeof(int); 
			memcpy(p, (LPCTSTR)fc->szCommand, (size_t)*iSize); 
			for(i=0; i<*iSize; i++) 
				p[i] = CCraftFTPApp::ExchangeBit(p[i], 4); 
			p		+=	*iSize; 
 
			//命令描述 
			iSize	= (int*)p; 
			*iSize	=	fc->szDescription.GetLength(); 
			p		+=	sizeof(int); 
			memcpy(p, (LPCTSTR)fc->szDescription, (size_t)*iSize); 
			for(i=0; i<*iSize; i++) 
				p[i] = CCraftFTPApp::ExchangeBit(p[i], 4); 
			p		+=	*iSize;			 
 
			//写入文件 
			File.Write(szbuffer, nSize+sizeof(DWORD)); 
			delete szbuffer; 
		} 
		File.Close(); 
	} 
} 
 
CCustomFtpCmd * CCustomCmdList::FindByCommandID(UINT nCmdID) 
{ 
	for(POSITION pos=GetHeadPosition(); pos!=NULL;) 
	{ 
		CCustomFtpCmd * fc = GetNext(pos); 
		if(fc && fc->nCommandID==nCmdID) 
			return fc; 
	} 
	return NULL; 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CCustomCmdDialog dialog 
const DWORD CCustomCmdDialog::m_nHelpIDs[] = 
{ 
	IDC_LIST_CUSTOMCMDS,	HIDC_LIST_CUSTOMCMDS, 
	IDC_EDIT_FTPCMDLABEL,	HIDC_EDIT_FTPCMDLABEL, 
	IDC_EDIT_FTPCMDTEXT,	HIDC_EDIT_FTPCMDTEXT, 
	IDC_EDIT_FTPCMD_DESCRIPTION, HIDC_EDIT_FTPCMD_DESCRIPTION, 
	IDC_BUTTON_ADD_CUSTOMFTPCMD, HIDC_BUTTON_ADD_CUSTOMFTPCMD, 
	IDC_BUTTON_REMOVE_CUSTOMFTPCMD, HIDC_BUTTON_REMOVE_CUSTOMFTPCMD, 
	0 , 0 
}; 
 
CCustomCmdDialog::CCustomCmdDialog(CWnd* pParent /*=NULL*/) 
	: CDialogEx(CCustomCmdDialog::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CCustomCmdDialog) 
	m_szDescription = _T(""); 
	m_szCmdText = _T(""); 
	m_szCmdLabel = _T(""); 
	//}}AFX_DATA_INIT 
	m_nSelectedItem		=	1; 
} 
 
void CCustomCmdDialog::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialogEx::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CCustomCmdDialog) 
	DDX_Control(pDX, IDC_LIST_CUSTOMCMDS, m_wndCustomCmdCtrl); 
	DDX_Text(pDX, IDC_EDIT_FTPCMD_DESCRIPTION, m_szDescription); 
	DDX_Text(pDX, IDC_EDIT_FTPCMDTEXT, m_szCmdText); 
	DDX_Text(pDX, IDC_EDIT_FTPCMDLABEL, m_szCmdLabel); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CCustomCmdDialog, CDialogEx) 
	//{{AFX_MSG_MAP(CCustomCmdDialog) 
	ON_BN_CLICKED(IDC_BUTTON_ADD_CUSTOMFTPCMD, OnButtonAddCustomFtpCmd) 
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_CUSTOMCMDS, OnItemChangedListCustomCmds) 
	ON_BN_CLICKED(IDC_BUTTON_REMOVE_CUSTOMFTPCMD, OnButtonRemoveCustomFtpCmd) 
	ON_EN_CHANGE(IDC_EDIT_FTPCMDLABEL, OnChangeEditFtpCmdLabel) 
	ON_EN_CHANGE(IDC_EDIT_FTPCMDTEXT, OnChangeEditFtpCmdText) 
	ON_EN_CHANGE(IDC_EDIT_FTPCMD_DESCRIPTION, OnChangeEditFtpCmdDescription) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CCustomCmdDialog message handlers 
 
BOOL CCustomCmdDialog::OnInitDialog()  
{ 
	CDialogEx::OnInitDialog(); 
	 
	// TODO: Add extra initialization here 
	m_GroupFrame.SetFrame(this, IDC_STATIC_FRAME, 0); 
	m_GroupFrame.Enable(FALSE); 
 
	m_wndCustomCmdCtrl.SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT); 
	CRect rect; 
	m_wndCustomCmdCtrl.GetClientRect(&rect); 
 
	LV_COLUMN lvc; 
	lvc.mask		= LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM ; 
	lvc.iSubItem	= 0; 
	lvc.pszText		= _T(""); 
	lvc.cx			= rect.Width(); 
	lvc.fmt			= LVCFMT_LEFT; 
	m_wndCustomCmdCtrl.InsertColumn(0,&lvc); 
	m_wndCustomCmdCtrl.ModifyStyle(WS_HSCROLL, 0); 
 
	//显示 
	for(POSITION pos = m_list.GetHeadPosition(); pos!=NULL;) 
	{ 
		CCustomFtpCmd * pCustomFtpCmd = m_list.GetNext(pos); 
		if(pCustomFtpCmd) 
		{ 
			int nItem = m_wndCustomCmdCtrl.InsertItem(m_wndCustomCmdCtrl.GetItemCount(), pCustomFtpCmd->szCmdLabel); 
			m_wndCustomCmdCtrl.SetItemData(nItem, (DWORD)pCustomFtpCmd); 
		} 
	} 
 
	return TRUE;  // return TRUE unless you set the focus to a control 
	              // EXCEPTION: OCX Property Pages should return FALSE 
} 
 
void CCustomCmdDialog::OnButtonAddCustomFtpCmd()  
{ 
	// TODO: Add your control notification handler code here 
	CCustomFtpCmd * pCustomFtpCmd = new CCustomFtpCmd; 
	pCustomFtpCmd->nCommandID =	m_list.m_nCommandID++; 
	m_list.AddTail(pCustomFtpCmd); 
 
	int nItem = m_wndCustomCmdCtrl.InsertItem(m_wndCustomCmdCtrl.GetItemCount(), _T("")); 
	m_wndCustomCmdCtrl.SetItemData(nItem, (DWORD)pCustomFtpCmd); 
	m_wndCustomCmdCtrl.SetItemState(nItem, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED); 
 
	m_GroupFrame.Enable(TRUE); 
	CWnd * pCtrl = GetDlgItem(IDC_EDIT_FTPCMDLABEL); 
	ASSERT(pCtrl); 
	pCtrl->SetFocus(); 
} 
 
void CCustomCmdDialog::OnItemChangedListCustomCmds(NMHDR* pNMHDR, LRESULT* pResult)  
{ 
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR; 
	// TODO: Add your control notification handler code here 
	CWnd * pCtrl = GetDlgItem(IDC_BUTTON_REMOVE_CUSTOMFTPCMD); 
	ASSERT(pCtrl); 
 
	POSITION pos = m_wndCustomCmdCtrl.GetFirstSelectedItemPosition(); 
	if(pos) 
	{ 
		pCtrl->EnableWindow(); 
		m_GroupFrame.Enable(TRUE); 
		m_nSelectedItem = m_wndCustomCmdCtrl.GetNextSelectedItem(pos); 
 
		CCustomFtpCmd * pCustomFtpCmd = (CCustomFtpCmd*)m_wndCustomCmdCtrl.GetItemData(m_nSelectedItem); 
		ASSERT(pCustomFtpCmd); 
		m_szCmdLabel	=	pCustomFtpCmd->szCmdLabel; 
		m_szCmdText		=	pCustomFtpCmd->szCommand; 
		m_szDescription	=	pCustomFtpCmd->szDescription; 
	} 
	else 
	{ 
		pCtrl->EnableWindow(FALSE); 
		m_GroupFrame.Enable(FALSE); 
		m_nSelectedItem = -1; 
 
		m_szCmdLabel	=	_T(""); 
		m_szCmdText		=	_T(""); 
		m_szDescription	=	_T(""); 
	} 
 
	UpdateData(FALSE); 
	*pResult = 0; 
} 
 
void CCustomCmdDialog::OnButtonRemoveCustomFtpCmd()  
{ 
	// TODO: Add your control notification handler code here 
	CWnd * pCtrl = GetDlgItem(IDC_BUTTON_REMOVE_CUSTOMFTPCMD); 
	ASSERT(pCtrl); 
 
	if(m_nSelectedItem != -1) 
	{ 
		CCustomFtpCmd * pCustomFtpCmd = (CCustomFtpCmd*)m_wndCustomCmdCtrl.GetItemData(m_nSelectedItem); 
		ASSERT(pCustomFtpCmd); 
		 
		POSITION pos = m_list.Find(pCustomFtpCmd); 
		if(pos) 
		{ 
			m_list.RemoveAt(pos); 
			delete pCustomFtpCmd; 
			int nOldItem = m_nSelectedItem; 
			m_wndCustomCmdCtrl.DeleteItem(m_nSelectedItem); 
 
			pCtrl->EnableWindow(FALSE); 
			m_GroupFrame.Enable(FALSE); 
 
			m_nSelectedItem = nOldItem - 1; 
			if(m_nSelectedItem == -1 && m_wndCustomCmdCtrl.GetItemCount()>0) 
				m_nSelectedItem = 0; 
 
			if(m_nSelectedItem != -1) 
			{ 
				m_wndCustomCmdCtrl.SetItemState(m_nSelectedItem, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);			 
				m_wndCustomCmdCtrl.SetFocus(); 
			} 
		} 
	} 
} 
 
void CCustomCmdDialog::OnChangeEditFtpCmdLabel()  
{ 
	// TODO: If this is a RICHEDIT control, the control will not 
	// send this notification unless you override the CDialogEx::OnInitDialog() 
	// function and call CRichEditCtrl().SetEventMask() 
	// with the ENM_CHANGE flag ORed into the mask. 
	 
	// TODO: Add your control notification handler code here 
	UpdateData(); 
 
	if(m_nSelectedItem != -1) 
	{ 
		CCustomFtpCmd * pCustomFtpCmd = (CCustomFtpCmd*)m_wndCustomCmdCtrl.GetItemData(m_nSelectedItem); 
		ASSERT(pCustomFtpCmd); 
		pCustomFtpCmd->szCmdLabel	=	m_szCmdLabel; 
 
		m_wndCustomCmdCtrl.SetItemText(m_nSelectedItem, 0, m_szCmdLabel); 
	} 
} 
 
void CCustomCmdDialog::OnChangeEditFtpCmdText()  
{ 
	// TODO: If this is a RICHEDIT control, the control will not 
	// send this notification unless you override the CDialogEx::OnInitDialog() 
	// function and call CRichEditCtrl().SetEventMask() 
	// with the ENM_CHANGE flag ORed into the mask. 
	 
	// TODO: Add your control notification handler code here 
	UpdateData(); 
 
	if(m_nSelectedItem != -1) 
	{ 
		CCustomFtpCmd * pCustomFtpCmd = (CCustomFtpCmd*)m_wndCustomCmdCtrl.GetItemData(m_nSelectedItem); 
		ASSERT(pCustomFtpCmd); 
		pCustomFtpCmd->szCommand	=	m_szCmdText; 
	} 
} 
 
void CCustomCmdDialog::OnChangeEditFtpCmdDescription()  
{ 
	// TODO: If this is a RICHEDIT control, the control will not 
	// send this notification unless you override the CDialogEx::OnInitDialog() 
	// function and call CRichEditCtrl().SetEventMask() 
	// with the ENM_CHANGE flag ORed into the mask. 
	 
	// TODO: Add your control notification handler code here 
	UpdateData(); 
 
	if(m_nSelectedItem != -1) 
	{ 
		CCustomFtpCmd * pCustomFtpCmd = (CCustomFtpCmd*)m_wndCustomCmdCtrl.GetItemData(m_nSelectedItem); 
		ASSERT(pCustomFtpCmd); 
		pCustomFtpCmd->szDescription	=	m_szDescription; 
	} 
} 
void CCustomCmdDialog::OnOK()  
{ 
	// TODO: Add extra validation here 
	CDialogEx::OnOK(); 
 
	//确保所有的命令有效 
	CCustomCmdList	list; 
	for(POSITION pos = m_list.GetHeadPosition(); pos!=NULL;) 
	{ 
		CCustomFtpCmd * pCustomFtpCmd = m_list.GetNext(pos); 
		if(pCustomFtpCmd) 
		{ 
			if(pCustomFtpCmd->szCmdLabel.IsEmpty() ||  
				pCustomFtpCmd->szCommand.IsEmpty() ) 
				delete pCustomFtpCmd; 
			else 
				list.AddTail(pCustomFtpCmd); 
		} 
	} 
	m_list.RemoveAll(); 
 
	for(pos = list.GetHeadPosition(); pos!=NULL;) 
	{ 
		CCustomFtpCmd * pCustomFtpCmd = list.GetNext(pos); 
		if(pCustomFtpCmd) 
			m_list.AddTail(pCustomFtpCmd); 
	} 
	list.RemoveAll(); 
} 
 
void CCustomCmdDialog::OnCancel()  
{ 
	// TODO: Add extra cleanup here 
	m_list.Cleanup(); 
 
	CDialogEx::OnCancel(); 
}