void FileTransfer::ReadDownloadFailed() { // We'll report the error only if we're actually downloading BOOL downloadActive = m_bDownloadStarted; // Stop file transfer CloseUndoneFileTransfers(); // Read the message rfbFileDownloadFailedMsg msg; m_clientconn->ReadExact((char *)&msg, sz_rfbFileDownloadFailedMsg); int len = Swap16IfLE(msg.reasonLen); char *reason = new char[len + 1]; m_clientconn->ReadExact(reason, len); reason[len] = '\0'; // Report error if (downloadActive) { char *errmsg = new char[128 + len]; sprintf(errmsg, "Download failed: %s", reason); MessageBox(m_hwndFileTransfer, errmsg, "Download Failed", MB_ICONEXCLAMATION | MB_OK); SetWindowText(m_hwndFTStatus, errmsg); vnclog.Print(1, _T("Download failed: %s\n"), reason); delete[] errmsg; } delete[] reason; // Enable dialog EnableWindow(GetDlgItem(m_hwndFileTransfer, IDC_FTCANCEL), FALSE); BlockingFileTransferDialog(TRUE); }
void FileTransfer::ReadUploadCancel() { if (m_bUploadStarted) { m_bUploadStarted = FALSE; CloseHandle(m_hFiletoRead); } // Stop file transfer CloseUndoneFileTransfers(); // Read the message rfbFileUploadCancelMsg msg; m_clientconn->ReadExact((char *)&msg, sz_rfbFileUploadCancelMsg); int len = Swap16IfLE(msg.reasonLen); char *reason = new char[len + 1]; m_clientconn->ReadExact(reason, len); reason[len] = '\0'; // Report error (only once per upload) if (m_bReportUploadCancel) { char *errmsg = new char[128 + len]; sprintf(errmsg, "Upload failed: %s", reason); MessageBox(m_hwndFileTransfer, errmsg, "Upload Failed", MB_ICONEXCLAMATION | MB_OK); SetWindowText(m_hwndFTStatus, errmsg); vnclog.Print(1, _T("Upload failed: %s\n"), reason); m_bReportUploadCancel = FALSE; delete[] errmsg; } delete[] reason; // Enable dialog EnableWindow(GetDlgItem(m_hwndFileTransfer, IDC_FTCANCEL), FALSE); BlockingFileTransferDialog(TRUE); }
void FileTransfer::CancelDownload(char *reason) { SendFileDownloadCancelMessage((unsigned short)strlen(reason), reason); SetWindowText(m_hwndFTStatus, reason); CloseUndoneFileTransfers(); EnableWindow(GetDlgItem(m_hwndFileTransfer, IDC_FTCANCEL), FALSE); BlockingFileTransferDialog(TRUE); m_bDownloadStarted = FALSE; }
void FileTransfer::ProcessListViewDBLCLK(HWND hwnd, char *Path, char *PathTmp, int iItem) { SendMessage(m_hwndFTProgress, PBM_SETPOS, 0, 0); SetWindowText(m_hwndFTStatus, ""); strcpy(PathTmp, Path); char buffer[rfbMAX_PATH]; char buffer_tmp[16]; ListView_GetItemText(hwnd, iItem, 0, buffer, rfbMAX_PATH); ListView_GetItemText(hwnd, iItem, 1, buffer_tmp, 16); if (strcmp(buffer_tmp, m_FTClientItemInfo.folderText) == 0) { BlockingFileTransferDialog(FALSE); if (strlen(PathTmp) >= 2) strcat(PathTmp, "\\"); strcat(PathTmp, buffer); if (hwnd == m_hwndFTClientList) ShowClientItems(PathTmp); if (hwnd == m_hwndFTServerList) SendFileListRequestMessage(PathTmp, 0); } }
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); }
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); }
// This method will be called, each time window message 'IDC_FTCOPY' is sent for // file download. In following cases IDC_FTCOPY will be sent for file download. // 1. From UI, when download button is clicked // 2. From 'FileTransferDownload' method after each file download is complete. // This method will keep track of number of selected files for which download // request is not sent. With each call it will send download request for one file // and will return. After the completion of file download 'FileTransferDownload' // method will do a PostMessage for 'IDC_FTCOPY', which will invoke this method // again and it will send another request for file download if download request for // some of the selected files is still pending. BOOL FileTransfer:: SendMultipleFileDownloadRequests() { // Download Request for all the selected files is sent. if(m_numOfFilesToDownload == 0) { m_numOfFilesToDownload = -1 ; m_currentDownloadIndex = -1; EnableWindow(GetDlgItem(m_hwndFileTransfer, IDC_FTCANCEL), FALSE); BlockingFileTransferDialog(TRUE); return TRUE; } // This is the first call for currently select file list. // Count of slected files is not calculated yet if(m_numOfFilesToDownload == -1) { m_numOfFilesToDownload = ListView_GetSelectedCount(m_hwndFTServerList); if (m_numOfFilesToDownload <= 0) { SetWindowText(m_hwndFTStatus, "No file is selected, nothing to download."); m_numOfFilesToDownload = -1; m_currentDownloadIndex = -1; EnableWindow(GetDlgItem(m_hwndFileTransfer, IDC_FTCANCEL), FALSE); BlockingFileTransferDialog(TRUE); return TRUE; } else { // file transfer will start for all the selected files now. set m_bTransferEnable to true // Enable cancel button and disable rest of the UI components. m_bTransferEnable = TRUE; BlockingFileTransferDialog(FALSE); EnableWindow(GetDlgItem(m_hwndFileTransfer, IDC_FTCANCEL), TRUE); } } // Calculate the next selected index for which file download request has to be sent. int index = -1; index = ListView_GetNextItem(m_hwndFTServerList, m_currentDownloadIndex, LVNI_SELECTED); if (index < 0) { SetWindowText(m_hwndFTStatus, "No file is selected, nothing to download."); m_numOfFilesToDownload = -1; m_currentDownloadIndex = -1; EnableWindow(GetDlgItem(m_hwndFileTransfer, IDC_FTCANCEL), FALSE); BlockingFileTransferDialog(TRUE); return TRUE; } // If Cancel button is clicked, dont send the file download request. if(m_bTransferEnable == FALSE) { m_numOfFilesToDownload = -1; m_currentDownloadIndex = -1; EnableWindow(GetDlgItem(m_hwndFileTransfer, IDC_FTCANCEL), FALSE); BlockingFileTransferDialog(TRUE); return TRUE; } // Update member variables for next call. m_currentDownloadIndex = index; m_numOfFilesToDownload -= 1; return SendFileDownloadRequest(); }
void FileTransfer::ShowServerItems() { rfbFileListDataMsg fld; m_clientconn->ReadExact((char *) &fld, sz_rfbFileListDataMsg); if ((fld.flags & 0x80) && !m_bServerBrowseRequest) { BlockingFileTransferDialog(TRUE); return; } fld.numFiles = Swap16IfLE(fld.numFiles); fld.dataSize = Swap16IfLE(fld.dataSize); fld.compressedSize = Swap16IfLE(fld.compressedSize); FTSIZEDATA *pftSD = new FTSIZEDATA[fld.numFiles]; char *pFilenames = new char[fld.dataSize]; m_clientconn->ReadExact((char *)pftSD, fld.numFiles * 8); m_clientconn->ReadExact(pFilenames, fld.dataSize); if (!m_bServerBrowseRequest) { if (fld.numFiles == 0) { BlockingFileTransferDialog(TRUE); strcpy(m_ServerPath, m_ServerPathTmp); SetWindowText(m_hwndFTServerPath, m_ServerPath); ListView_DeleteAllItems(m_hwndFTServerList); delete [] pftSD; delete [] pFilenames; return; } else { m_FTServerItemInfo.Free(); ListView_DeleteAllItems(m_hwndFTServerList); strcpy(m_ServerPath, m_ServerPathTmp); SetWindowText(m_hwndFTServerPath, m_ServerPath); CreateServerItemInfoList(&m_FTServerItemInfo, pftSD, fld.numFiles, pFilenames, fld.dataSize); m_FTServerItemInfo.Sort(); ShowListViewItems(m_hwndFTServerList, &m_FTServerItemInfo); } } else { while (TreeView_GetChild(GetDlgItem(m_hwndFTBrowse, IDC_FTBROWSETREE), m_hTreeItem) != NULL) { TreeView_DeleteItem(GetDlgItem(m_hwndFTBrowse, IDC_FTBROWSETREE), TreeView_GetChild(GetDlgItem(m_hwndFTBrowse, IDC_FTBROWSETREE), m_hTreeItem)); } TVITEM TVItem; TVINSERTSTRUCT tvins; TVItem.mask = TVIF_CHILDREN | TVIF_TEXT | TVIF_HANDLE; TVItem.cChildren = 1; int pos = 0; for (int i = 0; i < fld.numFiles; i++) { if (pftSD[i].size == -1) { TVItem.pszText = pFilenames + pos; TVItem.cChildren = 1; tvins.item = TVItem; tvins.hParent = m_hTreeItem; tvins.hParent = TreeView_InsertItem(GetDlgItem(m_hwndFTBrowse, IDC_FTBROWSETREE), &tvins); tvins.item = TVItem; TreeView_InsertItem(GetDlgItem(m_hwndFTBrowse, IDC_FTBROWSETREE), &tvins); tvins.hParent = m_hTreeItem;; } pos += strlen(pFilenames + pos) + 1; } } delete [] pftSD; delete [] pFilenames; BlockingFileTransferDialog(TRUE); }