LRESULT CMainFrame::OnLoadClipOnClipboard(WPARAM wParam, LPARAM lParam) { CClip *pClip = (CClip*)wParam; if(pClip == NULL) { LogSendRecieveInfo("---------ERROR OnLoadClipOnClipboard pClip == NULL"); return FALSE; } if(pClip) { CProcessPaste paste; paste.m_bSendPaste = false; paste.m_bActivateTarget = false; LogSendRecieveInfo("---------OnLoadClipOnClipboard - Before PutFormats on clipboard"); paste.m_pOle->PutFormatOnClipboard(&pClip->m_Formats); paste.m_pOle->CacheGlobalData(theApp.m_cfIgnoreClipboard, NewGlobalP("Ignore", sizeof("Ignore"))); LogSendRecieveInfo("---------OnLoadClipOnClipboard - After PutFormats on clipboard"); LogSendRecieveInfo(StrF(_T("---------OnLoadClipOnClipboard - Setting clip id: %d on ole clipboard"), pClip->m_id)); paste.GetClipIDs().Add(pClip->m_id); paste.DoPaste(); LogSendRecieveInfo(StrF(_T("---------OnLoadClipOnClipboard - After paste clip id: %d on ole clipboard"), pClip->m_id)); } delete pClip; return TRUE; }
LRESULT CMainFrame::OnAddToDatabaseFromSocket(WPARAM wParam, LPARAM lParam) { CClipList *pClipList = (CClipList*)wParam; if(pClipList == NULL) { LogSendRecieveInfo("---------OnAddToDatabaseFromSocket - ERROR pClipList == NULL"); return FALSE; } BOOL bSetToClipBoard = (BOOL)lParam; if(bSetToClipBoard) { CClip *pClip = pClipList->GetTail(); if(pClip) { LogSendRecieveInfo("OnAddToDatabaseFromSocket - Adding clip from socket setting clip to be put on clipboard"); pClip->m_param1 = TRUE; } } m_thread.AddRemoteClipToSave(pClipList); delete pClipList; return TRUE; }
BOOL CClient::SendClipFormat(CClipFormat* pCF) { CSendInfo Info; LPVOID pvData = GlobalLock(pCF->m_hgData); INT_PTR length = GlobalSize(pCF->m_hgData); UCHAR* pOutput = NULL; int nLenOutput = 0; CTextConvert Convert; BOOL bRet = FALSE; LogSendRecieveInfo(StrF(_T("BEFORE Encrypt clip data %d"), length)); if(m_SendSocket.m_pEncryptor) { if(m_SendSocket.m_pEncryptor->Encrypt((UCHAR*)pvData, (int)length, g_Opt.m_csPassword, pOutput, nLenOutput)) { LogSendRecieveInfo(StrF(_T("AFTER Encrypt clip data %d"), nLenOutput)); Info.m_lParameter1 = nLenOutput; //Send over as UTF-8 CStringA dest; if(CTextConvert::ConvertToUTF8(GetFormatName(pCF->m_cfType), dest)) { strncpy(Info.m_cDesc, dest, sizeof(Info.m_cDesc)); Info.m_cDesc[sizeof(Info.m_cDesc)-1] = 0; } if(m_SendSocket.SendCSendData(Info, MyEnums::DATA_START) == FALSE) return FALSE; m_SendSocket.SendExactSize((char*)pOutput, nLenOutput, false); m_SendSocket.m_pEncryptor->FreeBuffer(pOutput); bRet = TRUE; } else { LogSendRecieveInfo("Failed to encrypt data"); return FALSE; } } else { ASSERT(!"SendItem::Encryption not initialized"); LogSendRecieveInfo("SendItem::Encryption not initialized"); } GlobalUnlock(pCF->m_hgData); if(m_SendSocket.SendCSendData(Info, MyEnums::DATA_END) == FALSE) return FALSE; return bRet; }
INT_PTR COleClipSource::PutFormatOnClipboard(CClipFormats *pFormats) { Log(_T("Start of put format on clipboard")); CClipFormat* pCF; INT_PTR count = pFormats->GetSize(); bool bDelayedRenderCF_HDROP = false; INT_PTR i = 0; //see if the html format is in the list //if it is the list we will not paste CF_TEXT for(i = 0; i < count; i++) { pCF = &pFormats->ElementAt(i); if(pCF->m_cfType == theApp.m_RemoteCF_HDROP) { bDelayedRenderCF_HDROP = true; } } for(i = 0; i < count; i++) { pCF = &pFormats->ElementAt(i); if(bDelayedRenderCF_HDROP) { if(pCF->m_cfType == CF_HDROP) { LogSendRecieveInfo("Added delayed cf_hdrop to clipboard"); DelayRenderData(pCF->m_cfType); } continue; } wchar_t * stringData = (wchar_t *) GlobalLock(pCF->m_hgData); int size = (int) GlobalSize(pCF->m_hgData); CString cs(stringData); GlobalUnlock(pCF->m_hgData); Log(StrF(_T("Setting clipboard type: %s to the clipboard"), GetFormatName(pCF->m_cfType))); CacheGlobalData(pCF->m_cfType, pCF->m_hgData); pCF->m_hgData = 0; // OLE owns it now } pFormats->RemoveAll(); m_bLoadedFormats = true; Log(_T("End of put format on clipboard")); return count; }
void CMainFrmThread::OnSaveRemoteClips() { LogSendRecieveInfo("---------Start of OnSaveRemoteClips"); CClipList *pLocalClips = new CClipList(); //Save the clips locally { ATL::CCritSecLock csLock(m_cs.m_sect); POSITION pos; CClip* pClip; pos = m_saveRemoteClips.GetHeadPosition(); while(pos) { pClip = m_saveRemoteClips.GetNext(pos); pLocalClips->AddTail(pClip); } //pLocalClips now own, the clips m_saveRemoteClips.RemoveAll(); } LogSendRecieveInfo("---------OnSaveRemoteClips - Before AddToDB"); int count = pLocalClips->AddToDB(true); LogSendRecieveInfo("---------OnSaveRemoteClips - After AddToDB"); //are we supposed to add this clip to the clipboard CClip *pLastClip = pLocalClips->GetTail(); if(pLastClip && pLastClip->m_param1 == TRUE) { LogSendRecieveInfo("---------OnSaveRemoteClips - Before Posting msg to main thread to set clipboard"); //set the clipboard on the main thread, i was having a problem with setting the clipboard on a thread //guess it needs to be set on the main thread //main window will clear this memory PostMessage(theApp.m_MainhWnd, WM_LOAD_ClIP_ON_CLIPBOARD, (LPARAM)pLastClip, 0); LogSendRecieveInfo("---------OnSaveRemoteClips - After Posting msg to main thread to set clipboard"); pLocalClips->RemoveTail(); } theApp.RefreshView(); delete pLocalClips; LogSendRecieveInfo("---------End of OnSaveRemoteClips"); }
BOOL COleClipSource::OnRenderGlobalData(LPFORMATETC lpFormatEtc, HGLOBAL* phGlobal) { static bool bInHere = false; if(bInHere) { return FALSE; } bInHere = true; HGLOBAL hData = NULL; CClipFormat *pFind = m_DelayRenderedFormats.FindFormat(lpFormatEtc->cfFormat); if(pFind) { if(pFind->m_hgData) { hData = NewGlobalH(pFind->m_hgData, GlobalSize(pFind->m_hgData)); } } else { LogSendRecieveInfo("Delayed Render, getting data from remote machine"); CClip clip; if(m_ClipIDs.GetCount() > 0) { clip.LoadFormats(m_ClipIDs[0]); CClipFormat *pDittoDelayCF_HDROP = clip.m_Formats.FindFormat(theApp.m_RemoteCF_HDROP); CClipFormat *pCF_HDROP = clip.m_Formats.FindFormat(CF_HDROP); if(pDittoDelayCF_HDROP && pCF_HDROP) { CDittoCF_HDROP *pData = (CDittoCF_HDROP*)GlobalLock(pDittoDelayCF_HDROP->m_hgData); if(pData) { CString csComputerName; CString csIP; CTextConvert::ConvertFromUTF8(pData->m_cIP, csIP); CTextConvert::ConvertFromUTF8(pData->m_cComputerName, csComputerName); GlobalUnlock(pDittoDelayCF_HDROP->m_hgData); CClient cl; hData = cl.RequestCopiedFiles(*pCF_HDROP, csIP, csComputerName); } } else { hData = m_ClipIDs.Render(lpFormatEtc->cfFormat); if (m_convertToHDROPOnDelayRender && hData == NULL && lpFormatEtc->cfFormat == CF_HDROP) { hData = ConvertToFileDrop(); } } } //Add to a cache of already rendered data //Windows seems to call this function multiple times //so only the first time do we need to go get the data HGLOBAL hCopy = NULL; if(hData) { hCopy = NewGlobalH(hData, GlobalSize(hData)); } CClipFormat format(lpFormatEtc->cfFormat, hCopy); m_DelayRenderedFormats.Add(format); format.m_autoDeleteData = false; //owned by m_DelayRenderedFormats } BOOL bRet = FALSE; if(hData) { // if phGlobal is null, we can just give the allocated mem // else, our data must fit within the GlobalSize(*phGlobal) if(*phGlobal == 0) { *phGlobal = hData; } else { SIZE_T len = min(::GlobalSize(*phGlobal), ::GlobalSize(hData)); if(len) { CopyToGlobalHH(*phGlobal, hData, len); } ::GlobalFree(hData); } bRet = TRUE; } bInHere = false; return bRet; }
HGLOBAL CClient::RequestCopiedFiles(CClipFormat &HDropFormat, CString csIP, CString csComputerName) { CSendInfo Info; bool bBreak = false; HGLOBAL hReturn = NULL; CString csErrorString; CFileTransferProgressDlg *pProgress = new CFileTransferProgressDlg; if(pProgress == NULL) return NULL; LogSendRecieveInfo(StrF(_T("************** START of requesting files from cpu %s, ip: %s **************"), csComputerName, csIP)); pProgress->Create(IDD_DIALOG_REMOTE_FILE); pProgress->ShowWindow(SW_SHOW); pProgress->SetMessage(StrF(_T("Opening Connection to %s (%s)"), csComputerName, csIP)); pProgress->PumpMessages(); CString requestFrom; if(g_Opt.GetRequestFilesUsingIP()) { requestFrom = csIP; } else { requestFrom = csComputerName; } do { if(OpenConnection(requestFrom) == FALSE) { csErrorString.Format(_T("Error Opening Connection to %s (%s)"), csComputerName, csIP); break; } m_SendSocket.SetSocket(m_Connection); m_SendSocket.SetProgressBar(pProgress); if(m_SendSocket.SendCSendData(Info, MyEnums::START) == FALSE) break; if(SendClipFormat(&HDropFormat) == FALSE) { csErrorString = _T("Error sending data request."); break; } if(m_SendSocket.SendCSendData(Info, MyEnums::END) == FALSE) break; pProgress->SetMessage(StrF(_T("Requesting Files from %s (%s)"), csComputerName, csIP)); if(m_SendSocket.SendCSendData(Info, MyEnums::REQUEST_FILES) == FALSE) break; CFileRecieve Recieve; long lRet = Recieve.RecieveFiles(m_Connection, csIP, pProgress); if(lRet == TRUE) { hReturn = Recieve.CreateCF_HDROPBuffer(); } else if(lRet == FALSE) { if(pProgress != NULL && pProgress->Cancelled()) { //Don't show an error message the user canceled things } else { csErrorString = _T("Error recieving files."); } } } while(false); CloseConnection(); if(hReturn == NULL && csErrorString.IsEmpty() == FALSE) { MessageBox(pProgress->m_hWnd, csErrorString, _T("Ditto"), MB_OK|MB_ICONEXCLAMATION); } pProgress->DestroyWindow(); LogSendRecieveInfo(StrF(_T("************** END of requesting files from cpu %s, ip: %s **************************"), csComputerName, csIP)); return hReturn; }
BOOL SendToFriend(CSendToFriendInfo &Info) { LogSendRecieveInfo("@@@@@@@@@@@@@@@ - START OF Send To Friend - @@@@@@@@@@@@@@@"); if(Info.m_pos > -1 && Info.m_pos < MAX_SEND_CLIENTS) { Info.m_csIP = g_Opt.m_SendClients[Info.m_pos].csIP; } else { Info.m_csErrorText = StrF(_T("ERROR getting ip position - %d"), Info.m_pos); LogSendRecieveInfo(Info.m_csErrorText); return FALSE; } LogSendRecieveInfo(StrF(_T("Sending clip to %s"), Info.m_csIP)); CClient client; if(client.OpenConnection(Info.m_csIP) == FALSE) { Info.m_csErrorText = StrF(_T("ERROR opening connection to %s"), Info.m_csIP); LogSendRecieveInfo(Info.m_csErrorText); return FALSE; } INT_PTR count = Info.m_pClipList->GetCount(); int i = -1; CClip* pClip; POSITION pos; pos = Info.m_pClipList->GetHeadPosition(); while(pos) { pClip = Info.m_pClipList->GetNext(pos); if(pClip == NULL) { ASSERT(FALSE); continue; } i++; if(Info.m_pPopup) { Info.m_pPopup->SendToolTipText(StrF(_T("Sending %d of %d"), i+1, count)); } MSG msg; while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } LogSendRecieveInfo(StrF(_T("Sending %d of %d clip to %s"), i+1, count, Info.m_csIP)); if(client.SendItem(pClip) == FALSE) { Info.m_csErrorText = "ERROR SendItem Failed"; LogSendRecieveInfo(Info.m_csErrorText); return FALSE; } } LogSendRecieveInfo("@@@@@@@@@@@@@@@ - END OF Send To Friend - @@@@@@@@@@@@@@@"); return TRUE; }
BOOL CClient::OpenConnection(const TCHAR* servername) { WSADATA wsaData; unsigned int addr = INADDR_NONE; struct sockaddr_in server; int wsaret=WSAStartup(0x101,&wsaData); if(wsaret) { LogSendRecieveInfo("ERROR - WSAStartup(0x101,&wsaData)"); return FALSE; } m_Connection = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if(m_Connection == INVALID_SOCKET) { LogSendRecieveInfo("ERROR - socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)"); m_Connection = NULL; return FALSE; } CStringA csServerNameA = CTextConvert::ConvertToChar(servername); //11-5-06 Serge Baranov found that if we are passing in an ip then //don't look the name up using gethostbyname/gethostbyaddr-> //on simple networks that don't use DNS these will fail. //So now only lookup the host name if they don't provide an IP. addr = inet_addr(csServerNameA); if(addr == INADDR_NONE) { struct hostent *hp = gethostbyname(csServerNameA); if(hp != NULL) { addr = *(unsigned int*)hp->h_addr; } } if(addr == NULL || addr == INADDR_NONE) { LogSendRecieveInfo("addr == NULL || addr == INADDR_NONE"); closesocket(m_Connection); m_Connection = NULL; return FALSE; } server.sin_addr.s_addr = addr; server.sin_family = AF_INET; server.sin_port = htons((u_short)g_Opt.m_lPort); if(connect(m_Connection, (struct sockaddr*)&server, sizeof(server))) { int nWhy = WSAGetLastError(); LogSendRecieveInfo(StrF(_T("ERROR if(connect(m_Connection,(struct sockaddr*)&server,sizeof(server))) why = %d"), nWhy)); closesocket(m_Connection); m_Connection = NULL; return FALSE; } return TRUE; }
//##ModelId=474D307E0022 long CFileRecieve::RecieveFiles(SOCKET sock, CString csIP, CFileTransferProgressDlg *pProgress) { CSendInfo Info; BOOL bBreak = false; BOOL lRet = FALSE; int nNumFiles = 0; int nFilesRecieved = 0; m_pProgress = pProgress; m_csRecievingFromIP = csIP; m_Sock.SetSocket(sock); while(true) { if(m_Sock.RecieveCSendInfo(&Info) == FALSE) break; switch(Info.m_Type) { case MyEnums::START: nNumFiles = Info.m_lParameter1; m_pProgress->SetNumFiles(nNumFiles); break; case MyEnums::DATA_START: { CString csFileName; nFilesRecieved++; CTextConvert::ConvertFromUTF8(Info.m_cDesc, csFileName); m_pProgress->StepAllFiles(); m_pProgress->SetMessage(StrF(_T("Copying File %d of %d"), nFilesRecieved, nNumFiles)); m_pProgress->SetFileMessage(StrF(_T("Copying %s"), csFileName)); m_pProgress->PumpMessages(); if(m_pProgress->Cancelled()) { lRet = USER_CANCELED; bBreak = true; break; } ULONG lFileSize = (ULONG)Info.m_lParameter1; long lRecieveRet = RecieveFileData(lFileSize, csFileName); if(lRecieveRet == USER_CANCELED) { lRet = USER_CANCELED; bBreak = true; break; } else if(lRecieveRet == FALSE) { LogSendRecieveInfo(StrF(_T("Error recieving the file %s"), csFileName)); } else { lRet = TRUE; } } break; case MyEnums::DATA_END: break; case MyEnums::END: bBreak = true; break; default: LogSendRecieveInfo("::ERROR unknown action type exiting"); bBreak = true; } if(bBreak || theApp.m_bAppExiting) break; } return lRet; }
//##ModelId=474D307E0041 long CFileRecieve::RecieveFileData(ULONG lFileSize, CString csFileName) { CString csFile = CGetSetOptions::GetPath(PATH_REMOTE_FILES); CreateDirectory(csFile, NULL); csFile += m_csRecievingFromIP + "\\"; CreateDirectory(csFile, NULL); nsPath::CPath path(csFileName); csFile += path.GetName(); CFile File; CFileException ex; if(File.Open(csFile, CFile::modeWrite|CFile::modeCreate|CFile::typeBinary, &ex) == FALSE) { TCHAR szError[200]; ex.GetErrorMessage(szError, 200); LogSendRecieveInfo(StrF(_T("Error opening file in RequestCopiedFiles, error: %s"), szError)); return FALSE; } ULONG lBytesRead = 0; long lBytesNeeded = 0; int nPercent = 0; int nPrevPercent = 0; char *pBuffer = new char[CHUNK_WRITE_SIZE]; if(pBuffer == NULL) { LogSendRecieveInfo("Error creating buffer in RequestCopiedFiles"); return FALSE; } BOOL bRet = FALSE; while(true) { lBytesNeeded = CHUNK_WRITE_SIZE; if(lFileSize - lBytesRead < CHUNK_WRITE_SIZE) lBytesNeeded = lFileSize - lBytesRead; if(m_Sock.RecieveExactSize(pBuffer, lBytesNeeded) == FALSE) { break; } File.Write(pBuffer, lBytesNeeded); lBytesRead += lBytesNeeded; if(lBytesRead >= lFileSize) { m_pProgress->SetSingleFilePos(100); bRet = TRUE; break; } if(lBytesNeeded > 0) { nPercent = (int)((lBytesRead / (double)lFileSize) * 100); if((nPercent - nPrevPercent) > 5) { m_pProgress->SetSingleFilePos(nPercent); m_pProgress->PumpMessages(); if(m_pProgress->Cancelled()) { bRet = USER_CANCELED; break; } nPrevPercent = nPercent; } } } File.Close(); if(bRet) { m_RecievedFiles.Add(csFile); } delete []pBuffer; pBuffer = NULL; return bRet; }