Click here to Skip to main content
15,881,687 members
Articles / Desktop Programming / MFC

FTP Wanderer - FTP Client using WININET

Rate me:
Please Sign up or sign in to vote.
4.85/5 (49 votes)
30 Jul 20023 min read 307.3K   19.7K   112  
This article presents a fully functional implementation of a FTP client.
/****************************************************************/
/*																*/
/*  HistoryCombo.cpp											*/
/*																*/
/*  Implementation of the CHistoryCombo class.					*/
/*																*/
/*  Programmed by Pablo van der Meer							*/
/*  Copyright Pablo Software Solutions 2002						*/
/*	http://www.pablovandermeer.nl								*/
/*																*/
/*  Last updated: 15 may 2002									*/
/*																*/
/****************************************************************/


#include "stdafx.h"
#include "HistoryCombo.h"
#include "RegKey.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


CHistoryCombo::CHistoryCombo()
{
	m_nMaxHistoryLength = 10;
}

CHistoryCombo::~CHistoryCombo()
{
}


BEGIN_MESSAGE_MAP(CHistoryCombo, CComboBox)
	//{{AFX_MSG_MAP(CHistoryCombo)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/********************************************************************/
/*																	*/
/* Function name : LoadMRU											*/
/* Description   : Reads items from Registry.						*/
/*																	*/
/********************************************************************/
void CHistoryCombo::LoadMRU(LPCTSTR lpszName)
{
	CString strCmd = "Cmd";
	
	CString strName;
	
	strName.Format("Software\\%s\\%s", AfxGetApp()->m_pszRegistryKey, AfxGetApp()->m_pszProfileName);

	if (!m_mruCmds.Create(HKEY_CURRENT_USER, strName, CString(lpszName)))
		return;
	
	int iCmd = 0;
	CString strText;
	do 
	{
		m_mruCmds.GetString(iCmd, strCmd);
		if (!strCmd.IsEmpty())
		{
			CString *pItemData = new CString;
			
			AfxExtractSubString(strText, strCmd, 0, '|');
			AfxExtractSubString(*pItemData, strCmd, 1, '|');

			int nIndex = AddString(strText);
			SetItemDataPtr(nIndex, pItemData);
		}
		iCmd++;
	} while (!strCmd.IsEmpty());

//	SetWindowText(strText);
}


/********************************************************************/
/*																	*/
/* Function name : SaveMRU											*/
/* Description   : Saves items to Registry.							*/
/*																	*/
/********************************************************************/
void CHistoryCombo::SaveMRU()
{
	int i;
	CString strCmd;

	for (i = 0; i < GetCount(); i++)
	{
		GetLBText(i, strCmd);

		CString *pItemData = (CString *)GetItemDataPtr(i);
		if (pItemData)
		{
			strCmd += "|";
			strCmd += *pItemData;
		}
		m_mruCmds.SetString(i, strCmd);
	}
}


/********************************************************************/
/*																	*/
/* Function name : AddStringEx										*/
/* Description   : Add string to MRU list.							*/
/*																	*/
/********************************************************************/
int CHistoryCombo::AddStringEx(LPCTSTR lpszText)
{
	CString strText;
	int nIndex = -1;
	
	if (lpszText == NULL)
		GetWindowText(strText);
	else
	{
		AfxExtractSubString(strText, lpszText, 0, '|');
//		strText = lpszText;
	}
	// add new string to MRU list
	if (FindStringExact(0, strText) == CB_ERR)
	{
		// delete first string if max. entries
		if (GetCount() >= m_nMaxHistoryLength)
		{
			CString *pItemData = (CString *)GetItemDataPtr(0);
			if (pItemData)
				delete pItemData;

			DeleteString(0);
		}
		
		CString *pItemData = new CString;
		
		AfxExtractSubString(strText, lpszText, 0, '|');
		AfxExtractSubString(*pItemData, lpszText, 1, '|');

		nIndex = AddString(strText);
		SetItemDataPtr(nIndex, pItemData);

		SaveMRU();
	}
	return nIndex;
}


/********************************************************************/
/*																	*/
/* Function name : ResetMRU											*/
/* Description   : Clear MRU list.									*/
/*																	*/
/********************************************************************/
void CHistoryCombo::ResetMRU()
{
	m_mruCmds.Clear();

	for (int i=0; i< GetCount(); i++)
	{
		CString *pItemData = (CString *)GetItemDataPtr(i);
		if (pItemData)
			delete pItemData;
	}

	ResetContent();
	SetWindowText("");
}


/********************************************************************/
/*																	*/
/* Function name : GetString										*/
/* Description   : Get string from Registry.						*/
/*																	*/
/********************************************************************/
BOOL CHistoryCombo::CMRUStrings::GetString(int i, CString& strVal)
{
	CString strText;
	unsigned long lSize;

	strText.Format("%s%d", m_strName, i);
	strVal.Empty();
	
	m_regKey.QueryValue(strVal.GetBuffer(_MAX_PATH), strText, &lSize);
	strVal.ReleaseBuffer();

	return !strVal.IsEmpty();	
}


/********************************************************************/
/*																	*/
/* Function name : SetString										*/
/* Description   : Set string in Registry.							*/
/*																	*/
/********************************************************************/
BOOL CHistoryCombo::CMRUStrings::SetString(int i, CString &strVal)
{
	CString strText;

	strText.Format("%s%d", m_strName, i);
	long lResult = m_regKey.SetValue(strVal, strText);
	return (lResult == ERROR_SUCCESS);
}


/********************************************************************/
/*																	*/
/* Function name : DeleteString										*/
/* Description   : Delete string from Registry.						*/
/*																	*/
/********************************************************************/
BOOL CHistoryCombo::CMRUStrings::DeleteString(int i)
{
	CString strText;

	strText.Format("%s%d", m_strName, i);
	long lResult = m_regKey.DeleteValue(strText);
	return (lResult == ERROR_SUCCESS);
}


/********************************************************************/
/*																	*/
/* Function name : Create											*/
/* Description   : Create Registry key for storing MRU items.		*/
/*																	*/
/********************************************************************/
BOOL CHistoryCombo::CMRUStrings::Create(HKEY hKeyParent, LPCTSTR lpszKeyName, CString &strName)
{
	long lResult  = m_regKey.Create(hKeyParent, lpszKeyName);
	m_strName = strName;
	return(lResult == ERROR_SUCCESS);
}


/********************************************************************/
/*																	*/
/* Function name : Clear											*/
/* Description   : Clear MRU list.									*/
/*																	*/
/********************************************************************/
void CHistoryCombo::CMRUStrings::Clear()
{
	int iCmd = 0;
	while(DeleteString(iCmd++))
	{
		// delete all items
	}
}


/********************************************************************/
/*																	*/
/* Function name : OnDestroy										*/
/* Description   : Free allocated memory.							*/
/*																	*/
/********************************************************************/
void CHistoryCombo::OnDestroy() 
{
	CComboBox::OnDestroy();

	for (int i=0; i< GetCount(); i++)
	{
		CString *pItemData = (CString *)GetItemDataPtr(i);
		if (pItemData)
			delete pItemData;
	}
}


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions