1
0
Эх сурвалжийг харах

copy buffer changes
[SAB]


git-svn-id: svn://svn.code.sf.net/p/ditto-cp/code/trunk@370 595ec19a-5cb4-439b-94a8-42fb3063c22c

sabrogden 19 жил өмнө
parent
commit
9559d53cf3

+ 5 - 7
CP_Main.cpp

@@ -666,13 +666,6 @@ long CCP_MainApp::SaveCopyClips()
 		//go ahead and send the clips out even though it won't be added for a bit
 		count = 1;
 	}
-	else if(CDittoCopyBuffer::GetDittoCopyBuffer()->Active())
-	{
-		CDittoCopyBuffer::GetDittoCopyBuffer()->EndCopy(pClips);
-		
-		//go ahead and send the clips out even though it won't be added for a bit
-		count = 1;
-	}
 	else
 	{
 		count = pClips->AddToDB(true);
@@ -681,6 +674,11 @@ long CCP_MainApp::SaveCopyClips()
 		{
 			lID = pClips->GetTail()->m_ID;
 			OnCopyCompleted(lID, count);
+
+			if(m_CopyBuffer.Active())
+			{
+				m_CopyBuffer.EndCopy(lID);
+			}
 		}
 	}
 

+ 2 - 1
CP_Main.h

@@ -23,7 +23,7 @@
 #include "MultiLanguage.h"
 #include "CopyThread.h"
 #include "ClipboardSaveRestore.h"
-
+#include "DittoCopyBuffer.h"
 #include "sqlite\CppSQLite3.h"
 
 //#define GET_APP ((CMainWnd*)theApp)
@@ -187,6 +187,7 @@ public:
 	
 	bool m_bDittoHasFocus;
 
+	CDittoCopyBuffer m_CopyBuffer;
 	void PumpMessageEx();
 
 protected:

+ 3 - 1
ClipboardSaveRestore.cpp

@@ -12,11 +12,13 @@ CClipboardSaveRestore::~CClipboardSaveRestore(void)
 
 bool CClipboardSaveRestore::Save()
 {
+	m_Clipboard.RemoveAll();
+
 	bool bRet = false;
 	COleDataObjectEx oleData;
 	CClipFormat cf;
 	FORMATETC fm;
-	
+
 	//Attach to the clipboard
 	if(!oleData.AttachClipboard())
 	{

+ 1 - 1
CopyThread.cpp

@@ -79,7 +79,7 @@ void CCopyThread::OnClipboardChange()
 
 	//If we are copying from a Ditto Buffer then save all to the database, so when we paste this it will paste 
 	//just like you were using Ctrl-V
-	if(theApp.m_QuickPasteMode == CCP_MainApp::DITTO_BUFFER_QUICK_PASTE)
+	if(theApp.m_CopyBuffer.Active())
 	{
 		pSupportedTypes = new CClipTypes;
 		if(pSupportedTypes)

+ 71 - 70
DittoCopyBuffer.cpp

@@ -3,12 +3,11 @@
 #include "CP_Main.h"
 #include <Mmsystem.h> //play sound
 
-CEvent CDittoCopyBuffer::m_ActiveTimer(TRUE, TRUE);
-CEvent CDittoCopyBuffer::m_RestoreTimer(TRUE, TRUE);
-CEvent CDittoCopyBuffer::m_RestoreActive(TRUE, TRUE);
-CDittoCopyBuffer CDittoCopyBuffer::m_Singleton;
 
-CDittoCopyBuffer::CDittoCopyBuffer()
+CDittoCopyBuffer::CDittoCopyBuffer() :
+	m_ActiveTimer(TRUE, TRUE),
+	m_RestoreTimer(TRUE, TRUE),
+	m_RestoreActive(TRUE, TRUE)
 {
 	m_bActive = false;
 	m_dwLastPaste = 0;
@@ -23,7 +22,9 @@ bool CDittoCopyBuffer::StartCopy(long lCopyBuffer, bool bCut)
 {
 	Log(StrF(_T("Start of Ditto Copy buffer = %d"), lCopyBuffer));
 
+	//Tell the timer thread to exit
 	m_ActiveTimer.SetEvent();
+	//Make sure the end copy thread has exited
 	EndRestoreThread();
 
 	m_bActive = true;
@@ -38,6 +39,8 @@ bool CDittoCopyBuffer::StartCopy(long lCopyBuffer, bool bCut)
 		theApp.SendCopy();
 	}
 
+	//Create a thread to track if they have copied anything, if thread has exited before they have
+	//copied something then the copy buffer copy is canceled
 	AfxBeginThread(CDittoCopyBuffer::StartCopyTimer, (LPVOID)this, THREAD_PRIORITY_LOWEST);
 
 	return true;
@@ -45,12 +48,12 @@ bool CDittoCopyBuffer::StartCopy(long lCopyBuffer, bool bCut)
 
 UINT CDittoCopyBuffer::StartCopyTimer(LPVOID pParam)
 {
-	m_ActiveTimer.ResetEvent();
-
 	CDittoCopyBuffer *pBuffer = (CDittoCopyBuffer*)pParam;
 	if(pBuffer)
 	{
-		DWORD dRes = WaitForSingleObject(m_ActiveTimer, 1500);
+		pBuffer->m_ActiveTimer.ResetEvent();
+
+		DWORD dRes = WaitForSingleObject(pBuffer->m_ActiveTimer, 1500);
 		if(dRes == WAIT_TIMEOUT)
 		{
 			pBuffer->m_bActive = false;
@@ -60,7 +63,7 @@ UINT CDittoCopyBuffer::StartCopyTimer(LPVOID pParam)
 	return 0;
 }
 
-bool CDittoCopyBuffer::EndCopy(CClipList *pClips)
+bool CDittoCopyBuffer::EndCopy(long lID)
 {
 	if(m_lCurrentDittoBuffer < 0 || m_lCurrentDittoBuffer >= 10)
 	{
@@ -68,6 +71,12 @@ bool CDittoCopyBuffer::EndCopy(CClipList *pClips)
 		return false;
 	}
 
+	if(m_bActive == false)
+	{
+		Log(_T("Current buffer is not active can't save copy buffer to db"));
+		return false;
+	}
+
 	m_ActiveTimer.SetEvent();
 	m_bActive = false;
 
@@ -75,60 +84,50 @@ bool CDittoCopyBuffer::EndCopy(CClipList *pClips)
 
 	bool bRet = false;
 
-	if(pClips)
+	//put the data that we stored at the start of this action back on the standard clipboard
+	m_SavedClipboard.Restore();
+	
+	if(PutClipOnDittoCopyBuffer(lID, m_lCurrentDittoBuffer))
 	{
-		m_SavedClipboard.Restore();
+		Log(StrF(_T("Ditto end copy, saved clip successfully Clip ID = %d"), lID));
 
-		try
-		{	
-			CClip *pClip = pClips->GetHead();
-			if(pClip)
-			{
-				int nCount = pClips->AddToDB(true);
-				if(nCount > 0)
-				{
-					long lID = pClips->GetTail()->m_ID;
-					theApp.OnCopyCompleted(lID, nCount);
-
-					//enclose in brackets so the query closes before we update below
-					{
-						CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID FROM CopyBuffers WHERE lCopyBuffer = %d"), m_lCurrentDittoBuffer);
-						if(q.eof())
-						{
-							theApp.m_db.execDMLEx(_T("INSERT INTO CopyBuffers VALUES(NULL, -1, %d);"), m_lCurrentDittoBuffer);
-						}
-					}
+		CCopyBufferItem Item;
+		g_Opt.GetCopyBufferItem(m_lCurrentDittoBuffer, Item);
+		if(Item.m_bPlaySoundOnCopy)
+		{
+			PlaySound(_T("ding.wav"), NULL, SND_FILENAME|SND_ASYNC);
+		}
 
-					theApp.m_db.execDMLEx(_T("UPDATE CopyBuffers SET lClipID = %d WHERE lCopyBuffer = %d"), lID, m_lCurrentDittoBuffer);
+		bRet = true;
+	}
+	else
+	{
+		Log(StrF(_T("Ditto end copy, ERROR associating clip to Copy buffer ID = %d"), lID));
+	}
 
-					bRet = true;
-					
-					Log(StrF(_T("Ditto end copy, saved clip successfully Clip ID = %d"), lID));
+	return bRet;
+}
 
-					PlaySound(_T("ding.wav"), NULL, SND_FILENAME|SND_ASYNC);
-				}
-				else
-				{
-					Log(_T("Error saving Ditto Buffer to Database"));
-				}
-			}
-			else
+bool CDittoCopyBuffer::PutClipOnDittoCopyBuffer(long lClipId, long lBuffer)
+{
+	try
+	{
+		//enclose in brackets so the query closes before we update below
+		{
+			CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID FROM CopyBuffers WHERE lCopyBuffer = %d"), lBuffer);
+			if(q.eof())
 			{
-				Log(_T("Error getting clip from cliplist"));
+				theApp.m_db.execDMLEx(_T("INSERT INTO CopyBuffers VALUES(NULL, -1, %d);"), lBuffer);
 			}
 		}
-		catch (CppSQLite3Exception& e)
-		{
-			Log(StrF(_T("SQLITE Exception %d - %s"), e.errorCode(), e.errorMessage()));
-			ASSERT(FALSE);
-		}	
-	}
-	else
-	{
-		Log(_T("Ditto Buffer Ditto did not receive a copy"));
+
+		theApp.m_db.execDMLEx(_T("UPDATE CopyBuffers SET lClipID = %d WHERE lCopyBuffer = %d"), lClipId, lBuffer);
+
+		return true;
 	}
+	CATCH_SQLITE_EXCEPTION
 
-	return bRet;
+	return false;
 }
 
 bool CDittoCopyBuffer::PastCopyBuffer(long lCopyBuffer)
@@ -137,6 +136,7 @@ bool CDittoCopyBuffer::PastCopyBuffer(long lCopyBuffer)
 	if(((dNow - m_dwLastPaste) < 500) || (dNow < m_dwLastPaste))
 	{
 		Log(_T("Copy Buffer pasted to fast"));
+		m_dwLastPaste = GetTickCount();
 		return false;
 	}
 	m_dwLastPaste = GetTickCount();
@@ -152,12 +152,13 @@ bool CDittoCopyBuffer::PastCopyBuffer(long lCopyBuffer)
 
 		if(q.eof() == false)
 		{
-			CClipboardSaveRestoreCopyBuffer *pClipboard = new CClipboardSaveRestoreCopyBuffer;
-			if(pClipboard)
+			m_pClipboard = new CClipboardSaveRestoreCopyBuffer;
+			if(m_pClipboard)
 			{
+				//make sure the previouse paste is done
 				EndRestoreThread();
 
-				if(pClipboard->Save())
+				if(m_pClipboard->Save())
 				{
 					CProcessPaste paste;
 					paste.m_bSendPaste = true;
@@ -165,11 +166,11 @@ bool CDittoCopyBuffer::PastCopyBuffer(long lCopyBuffer)
 					paste.GetClipIDs().Add(q.getIntField(_T("lID")));
 					paste.DoPaste();
 
-					pClipboard->m_lRestoreDelay = g_Opt.GetDittoRestoreClipboardDelay();
+					m_pClipboard->m_lRestoreDelay = g_Opt.GetDittoRestoreClipboardDelay();
 
-					Log(StrF(_T("PastCopyBuffer sent paste, starting thread to restore clipboard, Delay = %d"), pClipboard->m_lRestoreDelay));
+					Log(StrF(_T("PastCopyBuffer sent paste, starting thread to restore clipboard, Delay = %d"), m_pClipboard->m_lRestoreDelay));
 
-					AfxBeginThread(CDittoCopyBuffer::DelayRestoreClipboard, (LPVOID)pClipboard, THREAD_PRIORITY_LOWEST);
+					AfxBeginThread(CDittoCopyBuffer::DelayRestoreClipboard, (LPVOID)this, THREAD_PRIORITY_LOWEST);
 
 					bRet = true;
 				}
@@ -195,13 +196,13 @@ void CDittoCopyBuffer::EndRestoreThread()
 
 UINT CDittoCopyBuffer::DelayRestoreClipboard(LPVOID pParam)
 {
-	m_RestoreTimer.ResetEvent();
-	m_RestoreActive.ResetEvent();
-
-	CClipboardSaveRestoreCopyBuffer *pClipboard = (CClipboardSaveRestoreCopyBuffer*)pParam;
-	if(pClipboard)
+	CDittoCopyBuffer *pBuffer = (CDittoCopyBuffer*)pParam;
+	if(pBuffer)
 	{
-		DWORD dRes = WaitForSingleObject(m_RestoreTimer, pClipboard->m_lRestoreDelay);
+		pBuffer->m_RestoreTimer.ResetEvent();
+		pBuffer->m_RestoreActive.ResetEvent();
+
+		DWORD dRes = WaitForSingleObject(pBuffer->m_RestoreTimer, pBuffer->m_pClipboard->m_lRestoreDelay);
 
 		if(GetKeyState(VK_SHIFT) & 0x8000)
 		{
@@ -209,14 +210,14 @@ UINT CDittoCopyBuffer::DelayRestoreClipboard(LPVOID pParam)
 		}
 		else
 		{
-			pClipboard->Restore();
+			pBuffer->m_pClipboard->Restore();
 		}
 
-		delete pClipboard;
-		pClipboard = NULL;
-	}
+		delete pBuffer->m_pClipboard;
+		pBuffer->m_pClipboard = NULL;
 
-	m_RestoreActive.SetEvent();
+		pBuffer->m_RestoreActive.SetEvent();
+	}
 
 	return TRUE;
 }

+ 7 - 10
DittoCopyBuffer.h

@@ -16,31 +16,28 @@ public:
 class CDittoCopyBuffer
 {
 public:
+	CDittoCopyBuffer();
 	~CDittoCopyBuffer(void);
 
-	static CDittoCopyBuffer *GetDittoCopyBuffer()	{ return &m_Singleton; }
-
 	bool Active()	{ return m_bActive; }
 	bool StartCopy(long lCopyBuffer, bool bCut = false);
-	bool EndCopy(CClipList *pClips);
+	bool EndCopy(long lID);
 	bool PastCopyBuffer(long lCopyBuffer);
 
+	static bool PutClipOnDittoCopyBuffer(long lClipId, long lBuffer);
 	static UINT DelayRestoreClipboard(LPVOID pParam);
 	static UINT StartCopyTimer(LPVOID pParam);
 
 protected:
 	void EndRestoreThread();
 
-protected:
-	CDittoCopyBuffer();
-	static CDittoCopyBuffer m_Singleton;
-
 protected:
 	long m_lCurrentDittoBuffer;
 	CClipboardSaveRestore m_SavedClipboard;
 	bool m_bActive;
 	DWORD m_dwLastPaste;
-	static CEvent m_ActiveTimer;
-	static CEvent m_RestoreTimer;
-	static CEvent m_RestoreActive;
+	CEvent m_ActiveTimer;
+	CEvent m_RestoreTimer;
+	CEvent m_RestoreActive;
+	CClipboardSaveRestoreCopyBuffer *m_pClipboard;
 };

+ 9 - 9
MainFrm.cpp

@@ -310,39 +310,39 @@ LRESULT CMainFrame::OnHotKey(WPARAM wParam, LPARAM lParam)
 	}
 	else if(wParam == theApp.m_pCopyBuffer1->m_Atom)
 	{
-		CDittoCopyBuffer::GetDittoCopyBuffer()->StartCopy(1);
+		theApp.m_CopyBuffer.StartCopy(1);
 	}
 	else if(wParam == theApp.m_pPasteBuffer1->m_Atom)
 	{
-		CDittoCopyBuffer::GetDittoCopyBuffer()->PastCopyBuffer(1);
+		theApp.m_CopyBuffer.PastCopyBuffer(1);
 	}
 	else if(wParam == theApp.m_pCutBuffer1->m_Atom)
 	{
-		CDittoCopyBuffer::GetDittoCopyBuffer()->StartCopy(1, true);
+		theApp.m_CopyBuffer.StartCopy(1, true);
 	}
 	else if(wParam == theApp.m_pCopyBuffer2->m_Atom)
 	{
-		CDittoCopyBuffer::GetDittoCopyBuffer()->StartCopy(2);
+		theApp.m_CopyBuffer.StartCopy(2);
 	}
 	else if(wParam == theApp.m_pPasteBuffer2->m_Atom)
 	{
-		CDittoCopyBuffer::GetDittoCopyBuffer()->PastCopyBuffer(2);
+		theApp.m_CopyBuffer.PastCopyBuffer(2);
 	}
 	else if(wParam == theApp.m_pCutBuffer2->m_Atom)
 	{
-		CDittoCopyBuffer::GetDittoCopyBuffer()->StartCopy(2, true);
+		theApp.m_CopyBuffer.StartCopy(2, true);
 	}
 	else if(wParam == theApp.m_pCopyBuffer3->m_Atom)
 	{
-		CDittoCopyBuffer::GetDittoCopyBuffer()->StartCopy(3);
+		theApp.m_CopyBuffer.StartCopy(3);
 	}
 	else if(wParam == theApp.m_pPasteBuffer3->m_Atom)
 	{	
-		CDittoCopyBuffer::GetDittoCopyBuffer()->PastCopyBuffer(3);
+		theApp.m_CopyBuffer.PastCopyBuffer(3);
 	}
 	else if(wParam == theApp.m_pCutBuffer3->m_Atom)
 	{
-		CDittoCopyBuffer::GetDittoCopyBuffer()->StartCopy(3, true);
+		theApp.m_CopyBuffer.StartCopy(3, true);
 	}
 
 	return TRUE;

+ 0 - 5
ProcessPaste.cpp

@@ -25,8 +25,6 @@ CProcessPaste::~CProcessPaste()
 
 BOOL CProcessPaste::DoPaste()
 {
-	Log(_T("Do Paste"));
-
 	m_pOle->m_bOnlyPaste_CF_TEXT = m_bOnlyPaste_CF_TEXT;
 	m_pOle->m_bPasteHTMLFormatAs_CF_TEXT = m_bPasteHTMLFormatAs_CF_TEXT;
 
@@ -69,9 +67,6 @@ BOOL CProcessPaste::DoPaste()
 			theApp.ActivateTarget();
 		}
 #endif
-
-		Log(_T("Do Paste RETURN TRUE"));
-
 		return TRUE;
 	}
 	return FALSE;