⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filetransfer.cpp

📁 tightvnc源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	fdr.fNameSize = Swap16IfLE(len);
	m_clientconn->WriteExact((char *)&fdr, sz_rfbFileDownloadRequestMsg);
	m_clientconn->WriteExact(path, len);
	return TRUE;
}

void 
FileTransfer::OnGetDispClientInfo(NMLVDISPINFO *plvdi) 
{
  switch (plvdi->item.iSubItem)
    {
    case 0:
		plvdi->item.pszText = m_FTClientItemInfo.GetNameAt(plvdi->item.iItem);
      break;
    case 1:
		plvdi->item.pszText = m_FTClientItemInfo.GetSizeAt(plvdi->item.iItem);
      break;
    default:
      break;
    }
 } 

void 
FileTransfer::OnGetDispServerInfo(NMLVDISPINFO *plvdi) 
{
  switch (plvdi->item.iSubItem)
  {
    case 0:
		plvdi->item.pszText = m_FTServerItemInfo.GetNameAt(plvdi->item.iItem);
      break;
    case 1:
		plvdi->item.pszText = m_FTServerItemInfo.GetSizeAt(plvdi->item.iItem);
      break;
    default:
      break;
    }
 } 

void 
FileTransfer::FileTransferUpload()
{
	int numOfFilesToUpload = 0, currentUploadIndex = -1;
	DWORD sz_rfbFileSize;
	DWORD sz_rfbBlockSize= 8192;
	DWORD dwNumberOfBytesRead = 0;
	unsigned int mTime = 0;
	char path[rfbMAX_PATH + rfbMAX_PATH + 2];
	BOOL bResult;
	numOfFilesToUpload = ListView_GetSelectedCount(m_hwndFTClientList);
	if (numOfFilesToUpload < 0) {
		SetWindowText(m_hwndFTStatus, "No file selected, nothing to upload.");
		BlockingFileTransferDialog(TRUE);
		EnableWindow(GetDlgItem(m_hwndFileTransfer, IDC_FTCANCEL), FALSE);
		return;
	}

	for (int i = 0; i < numOfFilesToUpload; i++) {
		BlockingFileTransferDialog(FALSE);
		EnableWindow(GetDlgItem(m_hwndFileTransfer, IDC_FTCANCEL), TRUE);
		int index = ListView_GetNextItem(m_hwndFTClientList, currentUploadIndex, LVNI_SELECTED);
		if (index < 0) {
			SetWindowText(m_hwndFTStatus, "No file is selected, nothing to download.");
			return;
		}
		currentUploadIndex = index;
		ListView_GetItemText(m_hwndFTClientList, currentUploadIndex, 0, m_ClientFilename, rfbMAX_PATH);
		sprintf(path, "%s\\%s", m_ClientPath, m_ClientFilename);
		strcpy(m_UploadFilename, path);
		WIN32_FIND_DATA FindFileData;
		SetErrorMode(SEM_FAILCRITICALERRORS);
		HANDLE hFile = FindFirstFile(path, &FindFileData);
		SetErrorMode(0);
		if (hFile == INVALID_HANDLE_VALUE) {
			SetWindowText(m_hwndFTStatus, "Could not find selected file, can't upload");
			// Continue with upload of other files.
			continue;
		} else if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) {
			SetWindowText(m_hwndFTStatus, "Cannot upload a directory");
			// Continue with upload of other files.
			continue;
		} else {
			sz_rfbFileSize = FindFileData.nFileSizeLow;
			mTime = FiletimeToTime70(FindFileData.ftLastWriteTime);
			strcpy(m_ServerFilename, FindFileData.cFileName);
		}
		FindClose(hFile);
		if ((sz_rfbFileSize != 0) && (sz_rfbFileSize <= sz_rfbBlockSize)) sz_rfbBlockSize = sz_rfbFileSize;
		m_hFiletoRead = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
		if (m_hFiletoRead == INVALID_HANDLE_VALUE) {
			SetWindowText(m_hwndFTStatus, "Upload failed: could not open selected file");
			BlockingFileTransferDialog(TRUE);
			EnableWindow(GetDlgItem(m_hwndFileTransfer, IDC_FTCANCEL), FALSE);
			return;
		}
		char buffer[rfbMAX_PATH + rfbMAX_PATH + rfbMAX_PATH + rfbMAX_PATH + 20];
		sprintf(buffer, "Uploading: %s\\%s -> %s\\%s ...",
				m_ClientPath, m_ClientFilename, m_ServerPath, m_ServerFilename);
		SetWindowText(m_hwndFTStatus, buffer);
		sprintf(path, "%s\\%s", m_ServerPath, m_ClientFilename);
		ConvertPath(path);
		int pathLen = strlen(path);

		char *pAllFURMessage = new char[sz_rfbFileUploadRequestMsg + pathLen];
		rfbFileUploadRequestMsg *pFUR = (rfbFileUploadRequestMsg *) pAllFURMessage;
		char *pFollowMsg = &pAllFURMessage[sz_rfbFileUploadRequestMsg];
		pFUR->type = rfbFileUploadRequest;
		pFUR->compressedLevel = 0;
		pFUR->fNameSize = Swap16IfLE(pathLen);
		pFUR->position = Swap32IfLE(0);
		memcpy(pFollowMsg, path, pathLen);
		m_clientconn->WriteExact(pAllFURMessage, sz_rfbFileUploadRequestMsg + pathLen); 
		delete [] pAllFURMessage;

		if (sz_rfbFileSize == 0) {
			SendFileUploadDataMessage(mTime);
		} else {
			int amount = sz_rfbFileSize / (sz_rfbBlockSize * 10);

			InitProgressBar(0, 0, amount, 1);

			DWORD dwPortionRead = 0;
			char *pBuff = new char [sz_rfbBlockSize];
			m_bUploadStarted = TRUE;
			while(m_bUploadStarted) {
				ProcessDlgMessage(m_hwndFileTransfer);
				if (m_bTransferEnable == FALSE) {
					SetWindowText(m_hwndFTStatus, "File transfer canceled");
					EnableWindow(GetDlgItem(m_hwndFileTransfer, IDC_FTCANCEL), FALSE);
					BlockingFileTransferDialog(TRUE);
					char reason[] = "File transfer canceled by user";
					int reasonLen = strlen(reason);
					char *pFUFMessage = new char[sz_rfbFileUploadFailedMsg + reasonLen];
					rfbFileUploadFailedMsg *pFUF = (rfbFileUploadFailedMsg *) pFUFMessage;
					char *pReason = &pFUFMessage[sz_rfbFileUploadFailedMsg];
					pFUF->type = rfbFileUploadFailed;
					pFUF->reasonLen = Swap16IfLE(reasonLen);
					memcpy(pReason, reason, reasonLen);
					m_clientconn->WriteExact(pFUFMessage, sz_rfbFileUploadFailedMsg + reasonLen);
					delete [] pFUFMessage;
					break;
				}
				bResult = ReadFile(m_hFiletoRead, pBuff, sz_rfbBlockSize, &dwNumberOfBytesRead, NULL);
				if (bResult && dwNumberOfBytesRead == 0) {
					/* This is the end of the file. */
					SendFileUploadDataMessage(mTime);
					break;
				}
				SendFileUploadDataMessage((unsigned short)dwNumberOfBytesRead, pBuff);
				dwPortionRead += dwNumberOfBytesRead;
				if (dwPortionRead >= (10 * sz_rfbBlockSize)) {
					dwPortionRead = 0;
					SendMessage(m_hwndFTProgress, PBM_STEPIT, 0, 0);
				}
			}
			if (m_bTransferEnable == FALSE)
				break;
			m_bUploadStarted = FALSE;
			delete [] pBuff;
		}
		SendMessage(m_hwndFTProgress, PBM_SETPOS, 0, 0);
		SetWindowText(m_hwndFTStatus, "");
		CloseHandle(m_hFiletoRead);
	}
	EnableWindow(GetDlgItem(m_hwndFileTransfer, IDC_FTCANCEL), FALSE);
	BlockingFileTransferDialog(TRUE);
	SendFileListRequestMessage(m_ServerPath, 0);
}

void
FileTransfer::CloseUndoneFileTransfers()
{
  if (m_bUploadStarted) {
    m_bUploadStarted = FALSE;
    CloseHandle(m_hFiletoRead);
  }
  if (m_bDownloadStarted) {
    m_bDownloadStarted = FALSE;
	m_bFirstFileDownloadMsg = TRUE;
	m_currentDownloadIndex = -1;
	m_numOfFilesToDownload = -1;
    CloseHandle(m_hFiletoWrite);
    DeleteFile(m_DownloadFilename);
  }
}

void 
FileTransfer::FileTransferDownload()
{
	rfbFileDownloadDataMsg fdd;
	m_clientconn->ReadExact((char *)&fdd, sz_rfbFileDownloadDataMsg);
	fdd.realSize = Swap16IfLE(fdd.realSize);
	fdd.compressedSize = Swap16IfLE(fdd.compressedSize);

	char path[rfbMAX_PATH + rfbMAX_PATH + 3];
	
	if (m_bFirstFileDownloadMsg) {
		m_dwDownloadBlockSize = fdd.compressedSize;
		sprintf(path, "%s\\%s", m_ClientPath, m_ServerFilename);
		strcpy(m_DownloadFilename, path);
		m_hFiletoWrite = CreateFile(path, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
									NULL, CREATE_ALWAYS, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
		int amount = m_sizeDownloadFile / ((m_dwDownloadBlockSize + 1) * 10);
		InitProgressBar(0, 0, amount, 1);
		m_bFirstFileDownloadMsg = FALSE;
		m_bDownloadStarted = TRUE;
	}
	if ((fdd.realSize == 0) && (fdd.compressedSize == 0)) {
		unsigned int mTime;
		m_clientconn->ReadExact((char *) &mTime, sizeof(unsigned int));
		if (m_hFiletoWrite == INVALID_HANDLE_VALUE) {
			CancelDownload("Could not create file");
			MessageBox(m_hwndFileTransfer, "Download failed: could not create local file",
					   "Download Failed", MB_ICONEXCLAMATION | MB_OK);
			SendMessage(m_hwndFTProgress, PBM_SETPOS, 0, 0);
			// Send message to start download for next selected file.
			PostMessage(m_hwndFileTransfer, WM_COMMAND, IDC_FTCOPY, 0);
			return;
		}
		FILETIME Filetime;
		Time70ToFiletime(mTime, &Filetime);
		SetFileTime(m_hFiletoWrite, &Filetime, &Filetime, &Filetime);
		SendMessage(m_hwndFTProgress, PBM_SETPOS, 0, 0);
		SetWindowText(m_hwndFTStatus, "");
		CloseHandle(m_hFiletoWrite);
		ShowClientItems(m_ClientPath);
		m_bFirstFileDownloadMsg = TRUE;
		m_bDownloadStarted = FALSE;
		// Send message to start download for next selected file.
		PostMessage(m_hwndFileTransfer, WM_COMMAND, IDC_FTCOPY, 0);
		return;
	}
	char * pBuff = new char [fdd.compressedSize];
	DWORD dwNumberOfBytesWritten;
	m_clientconn->ReadExact(pBuff, fdd.compressedSize);
	ProcessDlgMessage(m_hwndFileTransfer);
	if (!m_bTransferEnable) {
		CancelDownload("Download cancelled by user");
		delete [] pBuff;
		return;
	}
	if (m_hFiletoWrite == INVALID_HANDLE_VALUE) {
		CancelDownload("Could not create file");
		MessageBox(m_hwndFileTransfer, "Download failed: could not create local file",
				   "Download Failed", MB_ICONEXCLAMATION | MB_OK);
		delete [] pBuff;
		return;
	}
	WriteFile(m_hFiletoWrite, pBuff, fdd.compressedSize, &dwNumberOfBytesWritten, NULL);
	m_dwDownloadRead += dwNumberOfBytesWritten;
	if (m_dwDownloadRead >= (10 * m_dwDownloadBlockSize)) {
		m_dwDownloadRead = 0;
		SendMessage(m_hwndFTProgress, PBM_STEPIT, 0, 0); 
	}
	delete [] pBuff;
}

void
FileTransfer::CancelDownload(char *reason)
{
	SendFileDownloadCancelMessage(strlen(reason), reason);
	SetWindowText(m_hwndFTStatus, reason);
	CloseUndoneFileTransfers();
	EnableWindow(GetDlgItem(m_hwndFileTransfer, IDC_FTCANCEL), FALSE);
	BlockingFileTransferDialog(TRUE);
	m_bDownloadStarted = FALSE;
}

void 
FileTransfer::ShowClientItems(char *path)
{
	if (strlen(path) == 0) {
		//Show Logical Drives
		ListView_DeleteAllItems(m_hwndFTClientList);
		m_FTClientItemInfo.Free();
		int LengthDrivesString = 0;
		char DrivesString[256];
		char DriveName[rfbMAX_PATH] = "?:";
		LengthDrivesString = GetLogicalDriveStrings(256, DrivesString);
		if ((LengthDrivesString == 0) || (LengthDrivesString > 256)) {
			BlockingFileTransferDialog(TRUE);
			strcpy(m_ClientPathTmp, m_ClientPath);
			return;
		} else {
			strcpy(m_ClientPath, m_ClientPathTmp);
			SetWindowText(m_hwndFTClientPath, m_ClientPath);
			for (int i=0; i<256; i++) {
				DriveName[0] = DrivesString[i];
				char txt[16];
				strcpy(txt, m_FTClientItemInfo.folderText);
				m_FTClientItemInfo.Add(DriveName, txt, 0);
				DriveName[0] = '\0';
				i+=3;
				if ((DrivesString[i] == '\0') && (DrivesString[i+1] == '\0')) break;
			}
			m_FTClientItemInfo.Sort();
			ShowListViewItems(m_hwndFTClientList, &m_FTClientItemInfo);
		}
	} else {
		//Show Files
		HANDLE m_handle;
		int n = 0;
		WIN32_FIND_DATA m_FindFileData;
		strcat(path, "\\*");
		SetErrorMode(SEM_FAILCRITICALERRORS);
		m_handle = FindFirstFile(path, &m_FindFileData);
		DWORD LastError = GetLastError();
		SetErrorMode(0);
		if (m_handle == INVALID_HANDLE_VALUE) {
			if (LastError != ERROR_SUCCESS && LastError != ERROR_FILE_NOT_FOUND) {
				strcpy(m_ClientPathTmp, m_ClientPath);
				FindClose(m_handle);
				BlockingFileTransferDialog(TRUE);
				return;
			}
			path[strlen(path) - 2] = '\0';
			strcpy(m_ClientPath, m_ClientPathTmp);
			SetWindowText(m_hwndFTClientPath, m_ClientPath);
			FindClose(m_handle);
			ListView_DeleteAllItems(m_hwndFTClientList);
			BlockingFileTransferDialog(TRUE);
			return;
		}
		ListView_DeleteAllItems(m_hwndFTClientList);
		m_FTClientItemInfo.Free();
		char buffer[16];
		while(1) {
			if ((strcmp(m_FindFileData.cFileName, ".") != 0) &&
		       (strcmp(m_FindFileData.cFileName, "..") != 0)) {
				if (!(m_FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
					sprintf(buffer, "%d", m_FindFileData.nFileSizeLow);
					LARGE_INTEGER li;
					li.LowPart = m_FindFileData.ftLastWriteTime.dwLowDateTime;
					li.HighPart = m_FindFileData.ftLastWriteTime.dwHighDateTime;
					li.QuadPart = (li.QuadPart - 116444736000000000) / 10000000;
					m_FTClientItemInfo.Add(m_FindFileData.cFileName, buffer, li.LowPart);
				} else {
					strcpy(buffer, m_FTClientItemInfo.folderText);
					m_FTClientItemInfo.Add(m_FindFileData.cFileName, buffer, 0);
				}
			}
			if (!FindNextFile(m_handle, &m_FindFileData)) break;
		}
		FindClose(m_handle);
		m_FTClientItemInfo.Sort();
		ShowListViewItems(m_hwndFTClientList, &m_FTClientItemInfo);
		path[strlen(path) - 2] = '\0';
		strcpy(m_ClientPath, m_ClientPathTmp);
		SetWindowText(m_hwndFTClientPath, m_ClientPath);
	}
	BlockingFileTransferDialog(TRUE);
}

BOOL CALLBACK 
FileTransfer::FTBrowseDlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	FileTransfer *_this = (FileTransfer *) GetWindowLong(hwnd, GWL_USERDATA);
	switch (uMsg)
	{
	case WM_INITDIALOG:
		{
			SetWindowLong(hwnd, GWL_USERDATA, lParam);
			_this = (FileTransfer *) lParam;
			CentreWindow(hwnd);
			_this->m_hwndFTBrowse = hwnd;
			if (_this->m_bServerBrowseRequest) {
				_this->SendFileListRequestMessage("", 0x10);
				return TRUE;
			} else {
				TVITEM TVItem;
				TVINSERTSTRUCT tvins; 
				char DrivesString[256];
				char drive[] = "?:";
				int LengthDriveString = GetLogicalDriveStrings(256, DrivesString);
				TVItem.mask = TVIF_CHILDREN | TVIF_TEXT | TVIF_HANDLE;
				for (int i=0; i<LengthDriveString; i++) {
					drive[0] = DrivesString[i];
					TVItem.pszText = drive;
					TVItem.cChildren = 1;
					tvins.item = TVItem;
					tvins.hParent = TreeView_InsertItem(GetDlgItem(hwnd, IDC_FTBROWSETREE), &tvins);
					tvins.item = TVItem;
					TreeView_InsertItem(GetDlgItem(hwnd, IDC_FTBROWSETREE), &tvins);
					tvins.hParent = NULL;
					i += 3;
				}
			}
			return TRUE;
		}
	break;
	case WM_COMMAND:
		{
		switch (LOWORD(wParam))
		{
			case IDC_FTBROWSECANCEL:
				EndDialog(hwnd, TRUE);
				_this->m_bServerBrowseRequest = FALSE;
				return TRUE;
			case IDC_FTBROWSEOK:
				char path[rfbMAX_PATH];
				if (GetWindowText(GetDlgItem(hwnd, IDC_FTBROWSEEDIT), path, rfbMAX_PATH) == 0) {
					EndDialog(hwnd, TRUE);
					_this->m_bServerBrowseRequest = FALSE;
					return TRUE;
				}
				if (_this->m_bServerBrowseRequest) {
					strcpy(_this->m_ServerPathTmp, path);
					EndDialog(hwnd,TRUE);
					_this->m_bServerBrowseRequest = FALSE;
					_this->SendFileListRequestMessage(_this->m_ServerPathTmp, 0);
					return TRUE;
				} else {
					strcpy(_this->m_ClientPathTmp, path);
					EndDialog(hwnd,TRUE);
					_this->ShowClientItems(_this->m_ClientPathTmp);
				}
				return TRUE;
		}
		}
	break;
	case WM_NOTIFY:
		switch (LOWORD(wParam))
		{
		case IDC_FTBROWSETREE:
			switch (((LPNMHDR) lParam)->code)
			{
			case TVN_SELCHANGED:
				{
					NMTREEVIEW *m_lParam = (NMTREEVIEW *) lParam;
					char path[rfbMAX_PATH];
					_this->GetTVPath(GetDlgItem(hwnd, IDC_FTBROWSETREE), m_lParam->itemNew.hItem, path);
					SetWindowText(GetDlgItem(hwnd, IDC_FTBROWSEEDIT), path);
					return TRUE;
				}
				break;
			case TVN_ITEMEXPANDING:
				{
				NMTREEVIEW *m_lParam = (NMTREEVIEW *) lParam;
				char Path[rfbMAX_PATH];
				if (m_lParam -> action == 2) {
					if (_this->m_bServerBrowseRequest) {
						_this->m_hTreeItem = m_lParam->itemNew.hItem;
						_this->GetTVPath(GetDlgItem(hwnd, IDC_FTBROWSETREE), m_lParam->itemNew.hItem, Path);
						_this->SendFileListRequestMessage(Path, 0x10);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -