//----------------------------------------------------------------------------------
// StartDownload: Start the thread that will download the file.
//----------------------------------------------------------------------------------
void PatchDetailsCtrl::StartDownload(void)
{
	CustomInfo* pCustInfo = GetCustomInfo();

	if (m_sURL != "")
	{
		CreateDirectoryRecursive(pCustInfo->GetPatchFolder());
		m_sInfoFile = pCustInfo->GetPatchFolder();
		m_sInfoFile.append("PatchDetails.txt");

		// Remove it (make sure we don't accidentaly use some old leftover).
		DeleteFile(m_sInfoFile.c_str());

		if (IsFtpAddress(m_sURL))
		{
			m_pFtpGetFile = new FTPGetOp(m_sURL, true);
			m_pFtpGetFile->SetLocalPath(m_sInfoFile);
			m_pFtpGetFile->SetCompletion(new PatchDetailsCompletion(FtpDownloadDoneCallback));
			m_pFtpGetFile->RunAsync(static_cast<ULONG>(OP_TIMEOUT_INFINITE));
		}
		else
		{
			m_pHttpGetFile = new HTTPGetOp(m_sURL);
			m_pHttpGetFile->SetLocalPath(m_sInfoFile);
			m_pHttpGetFile->SetCompletion(new PatchDetailsCompletion(HttpDownloadDoneCallback));
			m_pHttpGetFile->RunAsync(static_cast<ULONG>(OP_TIMEOUT_INFINITE));
		}

		m_bDownloadStarted = true;
	}
}
//----------------------------------------------------------------------------------
// StartDownloads: Start the threads that will download the files.
//----------------------------------------------------------------------------------
void DownloadCtrl::StartDownloads(void)
{
	CustomInfo* pCustInfo = GetCustomInfo();

	CPatchData* pPatch = pCustInfo->GetSelectedPatch();
	if (pPatch)
	{
		#ifdef _DEBUG
			if (GetKeyState(VK_MENU) & GetKeyState(VK_CONTROL) & GetKeyState(VK_SHIFT) & 0x8000)
				pPatch->DebugBox(GetWindow());
		#endif

		GUIString sPatchFile = pPatch->GetPatchUrl();
		int nPos = sPatchFile.rFind('/');
		if (nPos == -1)
			nPos = sPatchFile.rFind('\\');
		if (nPos != -1)
			sPatchFile.erase(0, nPos + 1);
		else
			sPatchFile = TEXT("Update.exe");

		std::string aHostGraphic = pPatch->GetHostBmp();
		int aPos = aHostGraphic.find_last_of('.');
		std::string aHostGraphicExtension;
		if(aPos != std::string::npos && aHostGraphic.length() > aPos + 1)
			aHostGraphicExtension = aHostGraphic.substr(aPos+1);
		else
			aHostGraphicExtension = "bmp";

		m_sHostBitmapFile = GenerateTempFileName("FS", aHostGraphicExtension);

		CreateDirectoryRecursive(pCustInfo->GetPatchFolder());
		GUIString sPatchExe = pCustInfo->GetPatchFolder();
		sPatchExe.append(sPatchFile);

		pCustInfo->SetPatchFile(sPatchExe);

		mStartTime = time(NULL);
		mNumberCallbacks = 0;
		if (IsFtpAddress(pPatch->GetHostBmp()))
		{
			// Don't attempt to handle resumed downloads of bitmaps - not worth it.
			DeleteFile(std::string(m_sHostBitmapFile).c_str());
			m_pFtpGetBitmap = new FTPGetOp(pPatch->GetHostBmp(), true);
			m_pFtpGetBitmap->SetLocalPath(m_sHostBitmapFile);
			m_pFtpGetBitmap->SetCompletion(new DownloadCompletion(FtpBitmapDoneCallback));
			m_pFtpGetBitmap->RunAsync(static_cast<ULONG>(OP_TIMEOUT_INFINITE));
		}
		else
		{
			m_pHttpGetBitmap = new HTTPGetOp(pPatch->GetHostBmp());
			m_pHttpGetBitmap->SetLocalPath(m_sHostBitmapFile);
			m_pHttpGetBitmap->SetCompletion(new DownloadCompletion(HttpBitmapDoneCallback));
			m_pHttpGetBitmap->RunAsync(static_cast<ULONG>(OP_TIMEOUT_INFINITE));
		}

//		m_tDownloadStarted = GetTickCount();

		m_nPrevDownloadedFtpBytes = 0;
		if (IsFtpAddress(pPatch->GetPatchUrl()))
		{
			// If the previous patch profile does not match this patch profile, nuke the file.
			if (! pCustInfo->MatchFtpDownloadTag(sPatchExe, pCustInfo->GetNewVersion(), pPatch->GetPatchSize(), pPatch->GetChecksum()))
				DeleteFile(std::string(sPatchExe).c_str());
			else
				m_nPrevDownloadedFtpBytes = GetFileSize(sPatchExe);

			// Tag the patch's profile (so we can 'resume or not as appropriate later).
			pCustInfo->TagFtpDownload(sPatchExe, pCustInfo->GetNewVersion(), pPatch->GetPatchSize(), pPatch->GetChecksum());

			m_pFtpGetPatch = new FTPGetOp(pPatch->GetPatchUrl(), true);
			m_pFtpGetPatch->SetDoResume(true); // Allow resuming of downloads.
			m_pFtpGetPatch->SetLocalPath(sPatchExe);
			m_pFtpGetPatch->SetRecvChunkCompletion(new DownloadCompletion(PatchProgressCallback), CHUNCK_SIZE);
			m_pFtpGetPatch->SetCompletion(new DownloadCompletion(FtpPatchDoneCallback));
			m_pFtpGetPatch->RunAsync(static_cast<ULONG>(OP_TIMEOUT_INFINITE));
		}
		else
		{
			m_pHttpGetPatch = new HTTPGetOp(pPatch->GetPatchUrl());
			m_pHttpGetPatch->SetLocalPath(sPatchExe);
			m_pHttpGetPatch->SetRecvChunkCompletion(new DownloadCompletion(PatchProgressCallback), CHUNCK_SIZE);
			m_pHttpGetPatch->SetCompletion(new DownloadCompletion(HttpPatchDoneCallback));
			m_pHttpGetPatch->RunAsync(static_cast<ULONG>(OP_TIMEOUT_INFINITE));
		}

		m_bDownloadStarted = true;
	}
}