//----------------------------------------------------------------------------------
// TimerEvent: Handle a timer event.
//----------------------------------------------------------------------------------
bool DownloadCtrl::TimerEvent(int tDelta)
{
	Container::TimerEvent(tDelta);
	m_tDownloading += tDelta;

	if (! m_bShownTimeoutWarning)
	{
		CustomInfo* pCustInfo = GetCustomInfo();
		ResourceManager* pResMgr = pCustInfo->GetResourceManager();

		if (m_tDownloading > pCustInfo->GetPatchTimeout())
		{
			m_bShownTimeoutWarning = true;
			MessageBox(GetWindow(), pResMgr->GetFinishedString(Download_NoData_String), pResMgr->GetFinishedString(Global_Warning_String), MD_OK);
		}
	}
	return true;
}
//----------------------------------------------------------------------------------
// FtpDownloadComplete: The ftp transfer has completed for the patch.  Check the 
// status to see if it worked.
//----------------------------------------------------------------------------------
void PatchDetailsCtrl::FtpDownloadComplete(FTPGetOp* pGetOp)
{
	CustomInfo* pCustInfo = GetCustomInfo();
	ResourceManager* pResMgr = pCustInfo->GetResourceManager();

	WONStatus aStatus = pGetOp->GetStatus();

	if (aStatus == WS_Success)
	{
		if (! m_bAborted)
			LoadInfoTextFromFile(m_sInfoFile);

		m_bDownloadSucceeded = true;
	}
	else
	{
		if (! m_bAborted) // If the user aborted don't pop up an error telling them they aborted...
		{
			// Clear the file name (so no one tries to use it), but don't deleted it, 
			// we can resume the download later.
///			pCustInfo->SetPatchFile("");

			GUIString sTitle = pResMgr->GetFinishedString(Download_DownloadError_String);
			GUIString sMsg;

			switch (aStatus)
			{
				case WS_FTP_StatusError:
					switch (pGetOp->GetFTPStatus())
					{
						case 500:
							MessageBox(GetWindow(), pResMgr->GetFinishedString(Download_InvalidFtpUser_String), sTitle, MD_OK);
							break;
						case 530:
							MessageBox(GetWindow(), pResMgr->GetFinishedString(Download_InvalidFtpPassword_String), sTitle, MD_OK);
							break;
						case 550:
							MessageBox(GetWindow(), pResMgr->GetFinishedString(Download_InvalidFtpFile_String), sTitle, MD_OK);
							break;
						default:
							sMsg = pResMgr->GetFinishedString(Download_FtpError_String);
							ReplaceSubInt(sMsg, "%ERROR%", pGetOp->GetFTPStatus());
							MessageBox(GetWindow(), sMsg, sTitle, MD_OK);
					} //lint !e788
					break;
				case WS_FTP_InvalidResponse:
					MessageBox(GetWindow(), pResMgr->GetFinishedString(Download_InvalidFtpResp_String), sTitle, MD_OK);
					break;
				case WS_FTP_InvalidPasvResponse:
					MessageBox(GetWindow(), pResMgr->GetFinishedString(Download_InvalidFtpPasvResp_String), sTitle, MD_OK);
					break;
				case WS_WSAECONNREFUSED:
					MessageBox(GetWindow(), pResMgr->GetFinishedString(Download_ConnectionRefused_String), sTitle, MD_OK);
					break;
				default:
					sMsg = pResMgr->GetFinishedString(Download_UnknownFtpError_String);
					ReplaceSubString(sMsg, "%ERROR%", WONStatusToString(aStatus));
					MessageBox(GetWindow(), sMsg, sTitle, MD_OK);
			} //lint !e788
		}
	}

	m_bDownloadComplete = true;
	EnableControls();
}
//----------------------------------------------------------------------------------
// FtpPatchDownloadComplete: The ftp transfer has completed for the patch.  Check 
// the status to see if it worked.
//----------------------------------------------------------------------------------
void DownloadCtrl::FtpPatchDownloadComplete(FTPGetOp* pGetOp)
{
	CustomInfo* pCustInfo = GetCustomInfo();
	ResourceManager* pResMgr = pCustInfo->GetResourceManager();

	// Turn off the timer.
	RequestTimer(false);

	WONStatus aStatus = pGetOp->GetStatus();
	CPatchData* pPatch = pCustInfo->GetSelectedPatch();

	if (aStatus == WS_Success)
	{
		if (pPatch)
			pCustInfo->SaveDefaultSelection(pPatch->GetHostName());
		m_bPatchDownloadSucceeded = true;

		if (! m_bAborted)
		{
			// Set the progress bar to 100%.
			int nLower, nUpper;
			m_pProgressBar->GetRange(nLower, nUpper);
			UpdateProgressBar(nUpper, nUpper);
			ValidatePatch();
		}
	}
	else
	{
		if (! m_bAborted) // If the user aborted don't pop up an error telling them they aborted...
		{
			// Clear the file name (so no one tries to use it), but don't deleted it, 
			// we can resume the download later.
			pCustInfo->SetPatchFile("");

			GUIString sTitle = pResMgr->GetFinishedString(Download_DownloadError_String);
			GUIString sMsg;

			if (pPatch)
				pPatch->SetDownloadFailures(pPatch->GetDownloadFailures() + 1);

			switch (aStatus)
			{
				case WS_FTP_StatusError:
					switch (pGetOp->GetFTPStatus())
					{
						case 500:
							NamePasswordDialog(pGetOp, sTitle, pResMgr->GetFinishedString(Download_InvalidFtpUser_String));
							return;
						case 530:
							NamePasswordDialog(pGetOp, sTitle, pResMgr->GetFinishedString(Download_InvalidFtpPassword_String));
							return;
						case 550:
							MessageBox(GetWindow(), pResMgr->GetFinishedString(Download_InvalidFtpFile_String), sTitle, MD_OK);
							break;
						default:
							sMsg = pResMgr->GetFinishedString(Download_FtpError_String);
							ReplaceSubInt(sMsg, "%ERROR%", pGetOp->GetFTPStatus());
							MessageBox(GetWindow(), sMsg, sTitle, MD_OK);
					} //lint !e788
					break;
				case WS_FTP_InvalidResponse:
					MessageBox(GetWindow(), pResMgr->GetFinishedString(Download_InvalidFtpResp_String), sTitle, MD_OK);
					break;
				case WS_FTP_InvalidPasvResponse:
					MessageBox(GetWindow(), pResMgr->GetFinishedString(Download_InvalidFtpPasvResp_String), sTitle, MD_OK);
					break;
				case WS_WSAECONNREFUSED:
					MessageBox(GetWindow(), pResMgr->GetFinishedString(Download_ConnectionRefused_String), sTitle, MD_OK);
					break;
				default:
					sMsg = pResMgr->GetFinishedString(Download_UnknownFtpError_String);
					ReplaceSubString(sMsg, "%ERROR%", WONStatusToString(aStatus));
					MessageBox(GetWindow(), sMsg, sTitle, MD_OK);
			} //lint !e788

			FireBackButton();
		}
	}

	m_bPatchDownloadComplete = true;
	SendReport();
	EnableControls();
}
//----------------------------------------------------------------------------------
// Show: Make this dialog visible (or invisible), and start (or stop) any threads 
// that are needed.
//----------------------------------------------------------------------------------
void DownloadCtrl::Show(bool bShow)
{
	SetVisible(bShow);

	if (bShow)
	{
		m_bPatchIsValid = false;
		m_bShownTimeoutWarning = false;
//		m_nReturnResult = SUR_UNKNOWN;
		GetMainControl()->SetReturnResult(SUR_UNKNOWN);

		CustomInfo* pCustInfo = GetCustomInfo();
		ResourceManager* pResMgr = pCustInfo->GetResourceManager();

		// Some of the text changes dynamically based on patch selection, update it.

		// Informational (Main) text.
		GUIString sInfo = pResMgr->BuildInfoString(Download_Info1_String_Id);
		m_pInfoText->Clear();
		m_pInfoText->AddFormatedText(sInfo);
		m_pInfoText->SetVertOffset(0); // Scroll to the top.

		// Visit Host prompt.
		m_pVisitHostLabel->SetText(pResMgr->GetFinishedString(Download_Host_String));
		m_pVisitHostLabel->SetDesiredSize();

		// Visit Host button.
		m_pVisitHostButton->SetText(pResMgr->GetFinishedString(Download_VisitHost_String));

		ResetProgressBar();

		// Make sure the Next button is the default button.
		m_pNextButton->RequestFocus();

		// Start the download clock.
		m_tDownloading = 0;
		m_bDownloadStarted = false;
		m_bPatchDownloadComplete = false;
		m_bPatchDownloadSucceeded = false;
		m_bBitmapDownloadComplete = false;
		m_bBitmapDownloadSucceeded = false;
		m_bAborted = false;
		RequestTimer(true);

		m_pVisitHostButton->SetVisible(true);
		m_pVisitHostImageButton->SetVisible(false);

		EnableControls();

		// Check to see if we are using a previously downloaded patch.
		if (pCustInfo->GetPatchFile() != "")
		{
			m_bPatchDownloadComplete = true;

			int nLower, nUpper;
			m_pProgressBar->GetRange(nLower, nUpper);
			UpdateProgressBar(nUpper, nUpper);

			ValidatePatch();  // Check it (we are paranoid).
			EnableControls();
		}
		else
			StartDownloads(); // Start the download itself.
	}
	else
	{
		// Turn off the timer.
		RequestTimer(false);

		// If the download is still in progress, kill it.
		KillThreads();
	}
}
//----------------------------------------------------------------------------------
// UpdatePatchList: Update the list of patches (and results).
//----------------------------------------------------------------------------------
void SelectHostCtrl::UpdatePatchList(void)
{
	CustomInfo* pCustInfo = GetCustomInfo();
	ResourceManager* pResMgr = pCustInfo->GetResourceManager();

	GUIString sError;
	int nManualPatches = 0;
	int nAutoPatches = 0;

	// Clear the list box of old refuse.
	m_pHostList->Clear();

	m_pHostList->BeginMultiChange();

	GUIString sDefault = pCustInfo->LoadDefaultSelection();
	sDefault.toLowerCase();
	CPatchDataList* pPatches = pCustInfo->GetPatchList();

	// Go through the list and decide if we have to mark manual downloads.
	CPatchDataList::iterator Itr = pPatches->begin();

	while (Itr != pPatches->end())
	{
		CPatchData* pData = *Itr;

		if (pData)
		{
			if (pData->GetMustVisitHost())
				nManualPatches++;
			else
				nAutoPatches++;
		}

		++Itr;
	}

	m_bMixedPatches = nManualPatches != 0 && nAutoPatches != 0;

	// Now go through the list and add all of the items to the list box.
	Itr = pPatches->begin();
	int nIndex = 0;
	int nSelectIndex = -1;

	while (Itr != pPatches->end())
	{
		CPatchData* pData = *Itr;
		//pData->MessageBox(GetWindow()); // Debugging Aide.

		if (pData)
		{
			// Start with the Patch Name (we will display this).
			std::string sLine = pData->GetPatchName();

			// Add the Manual flag (if needed).
			if (pData->GetMustVisitHost() && m_bMixedPatches)
				sLine += " *";

			// Now add the results of previous download atempts.
			int nFailures = pData->GetDownloadFailures();
			int nAborts = pData->GetDownloadAborts();

			if (nFailures)
			{
				sError = pResMgr->GetFinishedString(Global_DownloadFailures_String);
				ReplaceSubInt(sError, "%NUM_FAILURES%", nFailures);
			}
			else if (nAborts)
			{
				sError = pResMgr->GetFinishedString(Global_DownloadAborts_String);
				ReplaceSubInt(sError, "%NUM_ABORTS%", nAborts);
			}
			else
				sError = "";

			PatchDataItem* pItem = static_cast<PatchDataItem*>(m_pHostList->InsertItem(new PatchDataItem));
			m_pHostList->SetString(nIndex, 0, sLine);
			m_pHostList->SetString(nIndex, 1, sError);
			pItem->m_pPatchData = pData;

			// Check to see if this was the last 'successful download site', if so, select it.
			GUIString sHost = pData->GetHostName();
			sHost.toLowerCase();
			if (sDefault == sHost)
				nSelectIndex = nIndex;

			++nIndex;
		}

		++Itr;
	}

	// If no site was selected, choose one at random.
	if (nSelectIndex == -1 && nIndex > 0)
	{
		srand(GetTickCount());
		nSelectIndex = rand() % nIndex;
	}

	// Actually make the selection.
	if (nSelectIndex != -1)
		m_pHostList->SetSelItem(nSelectIndex);

	m_pHostList->EndMultiChange();

	// Changing the patch list can change the Info Text, so update it.
	UpdateInfoText();
	EnableControls();
}