Browse Source

send clips using CClistList instead of other array type.

git-svn-id: svn://svn.code.sf.net/p/ditto-cp/code/trunk@91 595ec19a-5cb4-439b-94a8-42fb3063c22c
sabrogden 21 years ago
parent
commit
480e6aecfa
11 changed files with 146 additions and 187 deletions
  1. 12 17
      CP_Main.cpp
  2. 75 92
      Client.cpp
  3. 16 46
      Client.h
  4. 0 10
      MainFrm.cpp
  5. 0 1
      MainFrm.h
  6. 1 1
      Misc.cpp
  7. 3 4
      Misc.h
  8. 23 3
      ProcessCopy.cpp
  9. 2 0
      ProcessCopy.h
  10. 12 11
      QPasteWnd.cpp
  11. 2 2
      Server.cpp

+ 12 - 17
CP_Main.cpp

@@ -382,6 +382,17 @@ long CCP_MainApp::SaveCopyClips()
 	if( !pClips )
 		return 0;
 
+	CClipList* pCopyOfClips = NULL;
+	if(g_Opt.m_lAutoSendClientCount > 0)
+	{
+		//The thread will free these
+		pCopyOfClips = new CClipList;
+		if(pCopyOfClips != NULL)
+		{
+			*pCopyOfClips = *pClips;
+		}
+	}
+
 	count = pClips->AddToDB( true );
 	if( count > 0 )
 	{		
@@ -390,23 +401,7 @@ long CCP_MainApp::SaveCopyClips()
 
 		if(g_Opt.m_lAutoSendClientCount > 0)
 		{
-			//The thread will free these
-			CIDArray *pIDArray = new CIDArray;
-			pIDArray->pIDs = new CID[count];
-			pIDArray->lCount = count;
-			
-			CClip* pClip;
-			POSITION pos;
-			int nArrayPos = 0;
-			pos = pClips->GetHeadPosition();
-			while(pos)
-			{
-				pClip = pClips->GetNext(pos);
-				pIDArray->pIDs[nArrayPos].lID = pClip->m_ID;
-				pIDArray->pIDs[nArrayPos].m_csDesc = pClip->m_Desc;
-			}
-
-			AfxBeginThread(SendClientThread, pIDArray);
+			AfxBeginThread(SendClientThread, pCopyOfClips);
 		}
 	}
 

+ 75 - 92
Client.cpp

@@ -17,91 +17,71 @@ static char THIS_FILE[]=__FILE__;
 // Construction/Destruction
 //////////////////////////////////////////////////////////////////////
 
-BOOL SendToFriend(CIDArray *pArray, long lPos, CString csIP, CPopup *pPopup)
+BOOL SendToFriend(CSendToFriendInfo &Info)
 {
 	LogSendRecieveInfo("@@@@@@@@@@@@@@@ - START OF Send To Friend - @@@@@@@@@@@@@@@");
 
-	CSendToFriendInfo Info;
-	Info.csIP = csIP;
-	Info.lPos = lPos;
-	Info.pArray = pArray;
-	Info.pPopup = pPopup;
-
-	AfxBeginThread(SendToFriendThread, &Info);
-
-	DWORD dwWaitResult;
-	MSG msg;
-	while(true)
+	if(Info.m_lPos > -1 && Info.m_lPos < MAX_SEND_CLIENTS)
 	{
-		dwWaitResult = MsgWaitForMultipleObjects(1, &Info.hThreadRunning, FALSE, 1000, QS_ALLINPUT);
-
-		// Window message to be processed
-		if(dwWaitResult == (WAIT_OBJECT_0+1))
-		{
-			while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
-			{
-				// get the next message in the queue
-				TranslateMessage(&msg);
-				DispatchMessage(&msg);
-			}
-		}	
-		else if (dwWaitResult == WAIT_OBJECT_0)
-		{
-			break;
-		}
+		Info.m_csIP = g_Opt.m_SendClients[Info.m_lPos].csIP;
 	}
-
-	LogSendRecieveInfo("@@@@@@@@@@@@@@@ - END OF Send To Friend - @@@@@@@@@@@@@@@");
-
-	return Info.bRet;
-}
-
-UINT SendToFriendThread(LPVOID pParam)
-{
-	LogSendRecieveInfo("@@@@@@@@@@@@@@@ - START OF Send To Friend Thread - @@@@@@@@@@@@@@@");
-
-	CSendToFriendInfo *pInfo = (CSendToFriendInfo*)pParam;
-	
-	if(pInfo->lPos > -1 && pInfo->lPos < MAX_SEND_CLIENTS)
+	else
 	{
-		pInfo->csIP = g_Opt.m_SendClients[pInfo->lPos].csIP;
+		Info.m_csErrorText = StrF("ERROR getting ip position - %d", Info.m_lPos);
+		LogSendRecieveInfo(Info.m_csErrorText);
+		return FALSE;
 	}
 
-	LogSendRecieveInfo(StrF("Sending clip to %s", pInfo->csIP));
+	LogSendRecieveInfo(StrF("Sending clip to %s", Info.m_csIP));
 	CClient client;
 
-	if(client.OpenConnection(pInfo->csIP) == FALSE)
+	if(client.OpenConnection(Info.m_csIP) == FALSE)
 	{
-		LogSendRecieveInfo(StrF("ERROR opening connection to %s", pInfo->csIP));
-		pInfo->bRet = FALSE;
-		SetEvent(pInfo->hThreadRunning);
+		Info.m_csErrorText = StrF("ERROR opening connection to %s", Info.m_csIP);
+		LogSendRecieveInfo(Info.m_csErrorText);
 		return FALSE;
 	}
 
-	for(int i = 0; i < pInfo->pArray->lCount; i++)
+	long lCount = Info.m_pClipList->GetCount();
+	int i = -1;
+
+	CClip* pClip;
+	POSITION pos;
+	pos = Info.m_pClipList->GetHeadPosition();
+	while(pos)
 	{
-		if(pInfo->pPopup)
+		pClip = Info.m_pClipList->GetNext(pos);
+		if(pClip == NULL)
+		{
+			ASSERT(FALSE);
+			continue;
+		}
+		i++;
+
+		if(Info.m_pPopup)
 		{
-			pInfo->pPopup->SendToolTipText(StrF("Sending %d of %d", i+1, pInfo->pArray->lCount));
+			Info.m_pPopup->SendToolTipText(StrF("Sending %d of %d", i+1, lCount));
 		}
 
-		LogSendRecieveInfo(StrF("Sending %d of %d clip to %s", i+1, pInfo->pArray->lCount, pInfo->csIP));
+		MSG	msg;
+		while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
+		{
+			TranslateMessage(&msg);
+			DispatchMessage(&msg);
+		}
+
+		LogSendRecieveInfo(StrF("Sending %d of %d clip to %s", i+1, lCount, Info.m_csIP));
 
-		if(client.SendItem(&pInfo->pArray->pIDs[i]) == FALSE)
+		if(client.SendItem(pClip) == FALSE)
 		{
-			LogSendRecieveInfo("ERROR SendItem Failed");
+			Info.m_csErrorText = "ERROR SendItem Failed";
+			LogSendRecieveInfo(Info.m_csErrorText);
 
-			pInfo->bRet = FALSE;
-			SetEvent(pInfo->hThreadRunning);
 			return FALSE;
 		}
 	}
 
-	pInfo->bRet = TRUE;
-
-	LogSendRecieveInfo("@@@@@@@@@@@@@@@ - END OF Send To Friend Thread - @@@@@@@@@@@@@@@");
-
-	SetEvent(pInfo->hThreadRunning);
+	LogSendRecieveInfo("@@@@@@@@@@@@@@@ - END OF Send To Friend - @@@@@@@@@@@@@@@");
 
 	return TRUE;
 }
@@ -112,15 +92,16 @@ UINT  SendClientThread(LPVOID pParam)
 
 	LogSendRecieveInfo("@@@@@@@@@@@@@@@ - START OF SendClientThread - @@@@@@@@@@@@@@@");
 
-	bool bError;
-	CIDArray *pArray = (CIDArray*)pParam;
-	if(pArray == NULL)
+	CClipList *pClipList = (CClipList*)pParam;
+	if(pClipList == NULL)
 	{
-		LogSendRecieveInfo("ERROR if(pArray == NULL)");
+		LogSendRecieveInfo("ERROR if(pClipList == NULL)");
 		return FALSE;
 	}
 
-	LogSendRecieveInfo(StrF("Start of Send ClientThread Count - %d", pArray->lCount));
+	long lCount = pClipList->GetCount();
+
+	LogSendRecieveInfo(StrF("Start of Send ClientThread Count - %d", lCount));
 	
 	for(int nClient = 0; nClient < MAX_SEND_CLIENTS; nClient++)
 	{
@@ -131,22 +112,31 @@ UINT  SendClientThread(LPVOID pParam)
 			if(client.OpenConnection(g_Opt.m_SendClients[nClient].csIP) == FALSE)
 			{
 				LogSendRecieveInfo(StrF("ERROR opening connection to %s", g_Opt.m_SendClients[nClient].csIP));
-				bError = true;
-			}
 
-			bError = false;
+				CString cs;
+				cs.Format("Error opening connection to %s",g_Opt.m_SendClients[nClient].csIP);
+				::SendMessage(theApp.m_MainhWnd, WM_SEND_RECIEVE_ERROR, (WPARAM)cs.GetBuffer(cs.GetLength()), 0);
+				cs.ReleaseBuffer();
 
-			for(int i = 0; i < pArray->lCount; i++)
+				continue;
+			}
+
+			CClip* pClip;
+			POSITION pos;
+			pos = pClipList->GetHeadPosition();
+			while(pos)
 			{
-				if(bError == false)
+				pClip = pClipList->GetNext(pos);
+				if(pClip == NULL)
 				{
-					LogSendRecieveInfo(StrF("Sending clip to %s", g_Opt.m_SendClients[nClient].csIP));
-				
-					if(client.SendItem(&pArray->pIDs[i]) == FALSE)
-						bError = true;
+					ASSERT(FALSE);
+					LogSendRecieveInfo("Error in GetNext");
+					break;
 				}
 
-				if(bError)
+				LogSendRecieveInfo(StrF("Sending clip to %s", g_Opt.m_SendClients[nClient].csIP));
+				
+				if(client.SendItem(pClip) == FALSE)
 				{
 					CString cs;
 					cs.Format("Error sending clip to %s",g_Opt.m_SendClients[nClient].csIP);
@@ -160,11 +150,8 @@ UINT  SendClientThread(LPVOID pParam)
 		}
 	}
 
-	delete []pArray->pIDs;
-	pArray->pIDs = NULL;
-
-	delete pArray;
-	pArray = NULL;
+	delete pClipList;
+	pClipList = NULL;
 	
 	LogSendRecieveInfo("@@@@@@@@@@@@@@@ - END OF SendClientThread - @@@@@@@@@@@@@@@");
 
@@ -218,6 +205,7 @@ BOOL CClient::OpenConnection(const char* servername)
 	{
 		LogSendRecieveInfo("ERROR - socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)");
 		
+		m_Connection = NULL;
 		return FALSE;
 	}
 
@@ -236,6 +224,7 @@ BOOL CClient::OpenConnection(const char* servername)
 		LogSendRecieveInfo("ERROR - if(hp==NULL)");
 
 		closesocket(m_Connection);
+		m_Connection = NULL;
 		return FALSE;
 	}
 
@@ -246,19 +235,20 @@ BOOL CClient::OpenConnection(const char* servername)
 	{
 		LogSendRecieveInfo("ERROR if(connect(m_Connection,(struct sockaddr*)&server,sizeof(server)))");
 		closesocket(m_Connection);
+		m_Connection = NULL;
 		return FALSE;	
 	}
 
 	return TRUE;
 }
 
-BOOL CClient::SendItem(CID *pData)
+BOOL CClient::SendItem(CClip *pClip)
 {
 	SendInfo Info;
 	
 	strncpy(Info.m_cComputerName, GetComputerName(), sizeof(Info.m_cComputerName));
 	strncpy(Info.m_cIP, GetIPAddress(), sizeof(Info.m_cIP));
-	strncpy(Info.m_cText, pData->m_csDesc, sizeof(Info.m_cText));
+	strncpy(Info.m_cText, pClip->m_Desc, sizeof(Info.m_cText));
 	
 	Info.m_cText[sizeof(Info.m_cText)-1] = 0;
 	Info.m_cComputerName[sizeof(Info.m_cComputerName)-1] = 0;
@@ -268,23 +258,16 @@ BOOL CClient::SendItem(CID *pData)
 		return FALSE;
 	
 	CClipFormat* pCF;
-
-	//If the data has not been loaded, then send a message to
-	//the main thread to load the data from the db
-	if(pData->m_Formats.GetSize() <= 0)
-	{
-		::SendMessage(theApp.m_MainhWnd, WM_LOAD_FORMATS, pData->lID, (LPARAM)&pData->m_Formats);
-	}
 	
-	int nCount = pData->m_Formats.GetSize();
+	int nCount = pClip->m_Formats.GetSize();
 	DWORD dwSize;
 	DWORD dwDataSent;
 
 	//For each data type
 	for( int i=0; i < nCount; i++ )
 	{
-		pCF = &pData->m_Formats[i];
-
+		pCF = &pClip->m_Formats.GetData()[i];
+		
 		Info.m_lParameter1 = dwSize = GlobalSize(pCF->m_hgData);
 		strncpy(Info.m_cText, GetFormatName(pCF->m_cfType), sizeof(Info.m_cText));
 		Info.m_cText[sizeof(Info.m_cText)-1] = 0;

+ 16 - 46
Client.h

@@ -11,59 +11,30 @@
 
 #include "Server.h"
 
-class CID
-{
-public:
-	CID()
-	{
-		lID = -1;
-	}
-
-	~CID()
-	{
-		m_Formats.RemoveAll();
-	}
-	
-	long lID;
-	CString m_csDesc;
-	CClipFormats m_Formats;
-};
-
-class CIDArray
-{
-public:
-	CIDArray()
-	{
-		pIDs = NULL;
-		lCount = 0;
-	}
-	CID *pIDs;
-	long lCount;
-};
-
 class CSendToFriendInfo
 {
 public:
 	CSendToFriendInfo()
 	{
-		pPopup = NULL;
-		pArray = NULL;
-		lPos = -1;
-		bRet = FALSE;
-		hThreadRunning = CreateEvent(NULL, FALSE, FALSE, "SendToFriend");
-		ResetEvent(hThreadRunning);
+		m_pPopup = NULL;
+		m_lPos = -1;
+		m_pClipList = NULL;
+		m_pPopup = NULL;
 	}
 	~CSendToFriendInfo()
 	{
-		CloseHandle(hThreadRunning);
+		if(m_pClipList)
+		{
+			delete m_pClipList;
+			m_pClipList = NULL;
+		}
 	}
 
-	CIDArray *pArray;
-	long lPos;
-	CString csIP;
-	CPopup *pPopup;
-	BOOL bRet;
-	HANDLE hThreadRunning;
+	CClipList *m_pClipList;
+	long m_lPos;
+	CString m_csIP;
+	CPopup *m_pPopup;
+	CString m_csErrorText;
 };
 
 class CClient  
@@ -72,7 +43,7 @@ public:
 	CClient();
 	virtual ~CClient();
 
-	BOOL SendItem(CID *pData);
+	BOOL SendItem(CClip *pClip);
 	
 	BOOL OpenConnection(const char* servername);
 	BOOL CloseConnection();
@@ -83,8 +54,7 @@ protected:
 	BOOL SendData(SendInfo *pInfo, MyEnums::eSendType type);
 };
 
-BOOL SendToFriend(CIDArray *pArray, long lPos, CString csIP, CPopup *pPopup);
-UINT SendToFriendThread(LPVOID pParam);
+BOOL SendToFriend(CSendToFriendInfo &Info);
 
 UINT  SendClientThread(LPVOID pParam);
 

+ 0 - 10
MainFrm.cpp

@@ -45,7 +45,6 @@ BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
 	ON_MESSAGE(WM_CLIPBOARD_COPIED, OnClipboardCopied)
 	ON_WM_CLOSE()
 	ON_MESSAGE(WM_ADD_TO_DATABASE_FROM_SOCKET, OnAddToDatabaseFromSocket)
-	ON_MESSAGE(WM_LOAD_FORMATS, OnLoadFormats)
 	ON_MESSAGE(WM_SEND_RECIEVE_ERROR, OnErrorOnSendRecieve)
 	ON_MESSAGE(WM_FOCUS_CHANGED, OnFocusChanged)
 END_MESSAGE_MAP()
@@ -485,7 +484,6 @@ LRESULT CMainFrame::OnAddToDatabaseFromSocket(WPARAM wParam, LPARAM lParam)
 
 	pClip->MakeLatestTime();
 	pClip->AddToDB(true);
-
 	LogSendRecieveInfo("---------After AddToDB");
 
 	theApp.RefreshView();
@@ -496,14 +494,6 @@ LRESULT CMainFrame::OnAddToDatabaseFromSocket(WPARAM wParam, LPARAM lParam)
 	return TRUE;
 }
 
-LRESULT CMainFrame::OnLoadFormats(WPARAM wParam, LPARAM lParam)
-{
-	int nID = (int)wParam;
-	CClipFormats *pFormats = (CClipFormats *)lParam;
-	CClip::LoadFormats(nID, *pFormats);
-	return TRUE;
-}
-
 LRESULT CMainFrame::OnErrorOnSendRecieve(WPARAM wParam, LPARAM lParam)
 {
 	KillTimer(HIDE_ERROR_POPUP);

+ 0 - 1
MainFrm.h

@@ -79,7 +79,6 @@ protected:
 	afx_msg LRESULT OnShutDown(WPARAM wParam, LPARAM lParam);
 	afx_msg LRESULT OnClipboardCopied(WPARAM wParam, LPARAM lParam);
 	afx_msg LRESULT OnAddToDatabaseFromSocket(WPARAM wParam, LPARAM lParam);
-	afx_msg LRESULT OnLoadFormats(WPARAM wParam, LPARAM lParam);
 	afx_msg LRESULT OnErrorOnSendRecieve(WPARAM wParam, LPARAM lParam);
 	afx_msg LRESULT OnFocusChanged(WPARAM wParam, LPARAM lParam);
 	DECLARE_MESSAGE_MAP()

+ 1 - 1
Misc.cpp

@@ -1050,7 +1050,7 @@ BOOL CGetSetOptions::GetLogSendReceiveErrors()
 
 BOOL CGetSetOptions::GetHideDittoOnHotKeyIfAlreadyShown()
 {
-	return GetProfileLong("HideDittoOnHotKeyIfAlreadyShown", FALSE);
+	return GetProfileLong("HideDittoOnHotKeyIfAlreadyShown", TRUE);
 }
 
 void CGetSetOptions::SetHideDittoOnHotKeyIfAlreadyShown(BOOL bVal)

+ 3 - 4
Misc.h

@@ -96,10 +96,9 @@ CString GetFilePath(CString csFullPath);
 #define WM_REFRESH_VIEW			WM_USER + 205
 #define WM_CLIPBOARD_COPIED		WM_USER + 206
 #define WM_ADD_TO_DATABASE_FROM_SOCKET		WM_USER + 207
-#define WM_LOAD_FORMATS			WM_USER + 208
-#define WM_SEND_RECIEVE_ERROR	WM_USER + 209
-#define WM_FOCUS_CHANGED		WM_USER + 210
-#define WM_CV_GETCONNECT		WM_USER + 211
+#define WM_SEND_RECIEVE_ERROR	WM_USER + 208
+#define WM_FOCUS_CHANGED		WM_USER + 209
+#define WM_CV_GETCONNECT		WM_USER + 210
 
 
 #define REG_PATH					"Software\\Ditto"

+ 23 - 3
ProcessCopy.cpp

@@ -119,9 +119,6 @@ CClip::CClip() : m_ID(0), m_DataID(0), m_lTotalCopySize(0)
 
 CClip::~CClip()
 {
-	int count = m_Formats.GetSize();
-	// in proper handling, m_Formats should be empty
-	ASSERT( count == 0 );
 	EmptyFormats();
 }
 
@@ -767,6 +764,29 @@ int CClipList::AddToDB( bool bLatestTime, bool bShowStatus )
 	return savedCount;
 }
 
+const CClipList& CClipList::operator=(const CClipList &cliplist)
+{
+	POSITION pos;
+	CClip* pClip;
+	
+	pos = cliplist.GetHeadPosition();
+	while(pos)
+	{
+		pClip = cliplist.GetNext(pos);
+		ASSERT(pClip);
+
+		CClip *pNewClip = new CClip;
+		if(pNewClip)
+		{
+			*pNewClip = *pClip;
+			
+			AddTail(pNewClip);
+		}
+	}
+	
+	return *this;
+}
+
 
 /*----------------------------------------------------------------------------*\
 CClipboardViewer

+ 2 - 0
ProcessCopy.h

@@ -120,6 +120,8 @@ public:
 	// returns the number of clips actually saved
 	// while this does empty the Format Data, it does not delete the Clips.
 	int AddToDB( bool bLatestTime = false, bool bShowStatus = true );
+
+	const CClipList& operator=(const CClipList &cliplist);
 };
 
 

+ 12 - 11
QPasteWnd.cpp

@@ -1378,9 +1378,8 @@ BOOL CQPasteWnd::SendToFriendbyPos(int nPos)
 
 	m_lstHeader.GetSelectionIndexes(IDs);
 
-	CIDArray *pIDArray = new CIDArray;
-	pIDArray->pIDs = new CID[lCount];
-	pIDArray->lCount = lCount;
+	CSendToFriendInfo Info;
+	Info.m_lPos = nPos;
 
 	BOOL bRet = FALSE;
 
@@ -1388,17 +1387,23 @@ BOOL CQPasteWnd::SendToFriendbyPos(int nPos)
 	{
 		CPopup Popup(0, 0, m_hWnd);
 		Popup.Show(StrF("Sending clip to %s", g_Opt.m_SendClients[nPos].csIP));
+
+		Info.m_pPopup = &Popup;
 		
+		Info.m_pClipList =  new CClipList;
 		for(int i = 0; i < lCount; i++)
 		{
 			m_Recset.SetAbsolutePosition(IDs[i]);
-			pIDArray->pIDs[i].lID = m_Recset.m_lID;
-			pIDArray->pIDs[i].m_csDesc = m_Recset.m_strText;
+			CClip *pClip = new CClip;
+			CClip::LoadFormats(m_Recset.m_lID, pClip->m_Formats);
+			pClip->m_Desc = m_Recset.m_strText;
+			pClip->m_ID = m_Recset.m_lID;
+			Info.m_pClipList->AddTail(pClip);
 		}
 		
-		if(SendToFriend(pIDArray, nPos, "", &Popup) == FALSE)
+		if(SendToFriend(Info) == FALSE)
 		{
-			MessageBox(StrF("Error Sending data to %s", g_Opt.m_SendClients[nPos].csIP), "Ditto");
+			MessageBox(StrF("Error Sending data to %s\n\n%s", g_Opt.m_SendClients[nPos].csIP, Info.m_csErrorText), "Ditto");
 		}
 		else
 		{
@@ -1412,10 +1417,6 @@ BOOL CQPasteWnd::SendToFriendbyPos(int nPos)
 		e->Delete();
 	}	
 
-	delete [] pIDArray->pIDs;
-	pIDArray->pIDs = NULL;
-	delete pIDArray;
-
 	m_bHideWnd = true;
 
 	return bRet;

+ 2 - 2
Server.cpp

@@ -184,8 +184,8 @@ UINT  ClientThread(LPVOID pParam)
 			{
 				if(bAddToDatabase)
 				{
-					LogSendRecieveInfo("::DATA_END");
-					::SendMessage(theApp.m_MainhWnd, WM_ADD_TO_DATABASE_FROM_SOCKET, (WPARAM)&NewClip, bSetToClipBoard);
+					LogSendRecieveInfo("::END");
+					//::SendMessage(theApp.m_MainhWnd, WM_ADD_TO_DATABASE_FROM_SOCKET, (WPARAM)&NewClip, bSetToClipBoard);
 				}
 			}
 			break;