CopyThread.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // CopyThread.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "cp_main.h"
  5. #include "CopyThread.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CCopyThread
  13. IMPLEMENT_DYNCREATE(CCopyThread, CWinThread)
  14. CCopyThread::CCopyThread():
  15. m_bQuit(false),
  16. m_bConfigChanged(false),
  17. m_pClipboardViewer(NULL),
  18. m_connectOnStartup(true)
  19. {
  20. m_bAutoDelete = false;
  21. }
  22. CCopyThread::~CCopyThread()
  23. {
  24. m_LocalConfig.DeleteTypes();
  25. m_SharedConfig.DeleteTypes();
  26. delete m_pClipboardViewer;
  27. }
  28. BOOL CCopyThread::InitInstance()
  29. {
  30. m_pClipboardViewer = new CClipboardViewer(this);
  31. m_pClipboardViewer->m_connectOnStartup = m_connectOnStartup;
  32. // the window is created within this thread and therefore uses its message queue
  33. m_pClipboardViewer->Create();
  34. return TRUE;
  35. }
  36. int CCopyThread::ExitInstance()
  37. {
  38. m_pClipboardViewer->Disconnect(false);
  39. return CWinThread::ExitInstance();
  40. }
  41. // Called within Copy Thread:
  42. void CCopyThread::OnClipboardChange(CString activeWindow, CString activeWindowTitle)
  43. {
  44. Log(_T("OnClipboardChange - Start"));
  45. SyncConfig(); // synchronize with the main thread's copy configuration
  46. // if we are told not to copy on change, then we have nothing to do.
  47. if(!m_LocalConfig.m_bCopyOnChange)
  48. return;
  49. int groupId = theApp.GetActiveGroupId();
  50. if(groupId > -1)
  51. {
  52. Log(StrF(_T("LoadFromClipboard - loading clips into groupId: %d"), groupId));
  53. }
  54. CClip* pClip = new CClip;
  55. pClip->m_copyReason = theApp.GetCopyReason();
  56. COleDataObjectEx oleData;
  57. CClipTypes* pSupportedTypes = m_LocalConfig.m_pSupportedTypes;
  58. // If we are copying from a Ditto Buffer or use advanced option
  59. // then save all to the database, so when we paste this it will paste
  60. // just like you were using Ctrl-V
  61. std::shared_ptr<CClipTypes> availableTypes;
  62. if (theApp.m_CopyBuffer.Active() || CGetSetOptions::GetSupportAllTypes())
  63. {
  64. availableTypes = oleData.GetAvailableTypes();
  65. pSupportedTypes = availableTypes.get();
  66. }
  67. Log(_T("LoadFromClipboard - Before"));
  68. int bResult = pClip->LoadFromClipboard(pSupportedTypes, true, activeWindow, activeWindowTitle);
  69. Log(_T("LoadFromClipboard - After"));
  70. if(bResult == FALSE)
  71. {
  72. DWORD delay = CGetSetOptions::GetNoFormatsRetryDelay();
  73. if(delay > 0)
  74. {
  75. Log(StrF(_T("LoadFromClipboard didn't find any clips to save, sleeping %dms, then trying again"), delay));
  76. Sleep(delay);
  77. Log(_T("LoadFromClipboard #2 - Before"));
  78. bResult = pClip->LoadFromClipboard(pSupportedTypes, activeWindow);
  79. Log(_T("LoadFromClipboard #2 - After"));
  80. }
  81. else
  82. {
  83. Log(_T("LoadFromClipboard didn't find any clips to save, retry setting is not set, not retrying"));
  84. }
  85. }
  86. pSupportedTypes = NULL;
  87. if(bResult != TRUE)
  88. {
  89. delete pClip;
  90. return; // error
  91. }
  92. if(pClip != NULL &&
  93. groupId > -1)
  94. {
  95. pClip->m_parentId = groupId;
  96. }
  97. if(m_LocalConfig.m_bAsyncCopy)
  98. ::PostMessage(m_LocalConfig.m_hClipHandler, WM_CLIPBOARD_COPIED, (WPARAM)pClip, 0);
  99. else
  100. ::SendMessage(m_LocalConfig.m_hClipHandler, WM_CLIPBOARD_COPIED, (WPARAM)pClip, 0);
  101. Log(_T("OnClipboardChange - End"));
  102. }
  103. void CCopyThread::SyncConfig()
  104. {
  105. // atomic read
  106. if(m_bConfigChanged)
  107. {
  108. CClipTypes* pTypes = NULL;
  109. ATL::CCritSecLock csLock(m_cs.m_sect);
  110. pTypes = m_LocalConfig.m_pSupportedTypes;
  111. m_LocalConfig = m_SharedConfig;
  112. // NULL means that it shouldn't have been sync'ed
  113. if( m_SharedConfig.m_pSupportedTypes == NULL )
  114. { // let m_LocalConfig keep its types
  115. m_LocalConfig.m_pSupportedTypes = pTypes; // undo sync
  116. pTypes = NULL; // nothing to delete
  117. }
  118. else
  119. m_SharedConfig.m_pSupportedTypes = NULL; // now owned by LocalConfig
  120. // delete old types
  121. if( pTypes )
  122. {
  123. delete pTypes;
  124. }
  125. }
  126. }
  127. bool CCopyThread::IsClipboardViewerConnected()
  128. {
  129. return m_pClipboardViewer->m_bIsConnected;
  130. }
  131. bool CCopyThread::GetConnectCV()
  132. {
  133. return m_pClipboardViewer->GetConnect();
  134. }
  135. void CCopyThread::SetConnectCV(bool bConnect)
  136. {
  137. if(m_pClipboardViewer != NULL && m_pClipboardViewer->m_hWnd != NULL)
  138. {
  139. ::SendMessage( m_pClipboardViewer->m_hWnd, WM_SETCONNECT, bConnect, 0 );
  140. }
  141. }
  142. void CCopyThread::SetSupportedTypes( CClipTypes* pTypes )
  143. {
  144. ATL::CCritSecLock csLock(m_cs.m_sect);
  145. if(m_SharedConfig.m_pSupportedTypes)
  146. {
  147. delete m_SharedConfig.m_pSupportedTypes;
  148. }
  149. m_SharedConfig.m_pSupportedTypes = pTypes;
  150. m_bConfigChanged = true;
  151. }
  152. HWND CCopyThread::SetClipHandler(HWND hWnd)
  153. {
  154. ATL::CCritSecLock csLock(m_cs.m_sect);
  155. HWND hRet = m_SharedConfig.m_hClipHandler;
  156. m_SharedConfig.m_hClipHandler = hWnd;
  157. m_bConfigChanged = (hRet != hWnd);
  158. return hRet;
  159. }
  160. HWND CCopyThread::GetClipHandler()
  161. {
  162. ATL::CCritSecLock csLock(m_cs.m_sect);
  163. HWND hRet = m_SharedConfig.m_hClipHandler;
  164. return hRet;
  165. }
  166. bool CCopyThread::SetCopyOnChange(bool bVal)
  167. {
  168. ATL::CCritSecLock csLock(m_cs.m_sect);
  169. bool bRet = m_SharedConfig.m_bCopyOnChange;
  170. m_SharedConfig.m_bCopyOnChange = bVal;
  171. m_bConfigChanged = (bRet != bVal);
  172. return bRet;
  173. }
  174. bool CCopyThread::GetCopyOnChange()
  175. {
  176. ATL::CCritSecLock csLock(m_cs.m_sect);
  177. bool bRet = m_SharedConfig.m_bCopyOnChange;
  178. return bRet;
  179. }
  180. bool CCopyThread::SetAsyncCopy(bool bVal)
  181. {
  182. ATL::CCritSecLock csLock(m_cs.m_sect);
  183. bool bRet = m_SharedConfig.m_bAsyncCopy;
  184. m_SharedConfig.m_bAsyncCopy = bVal;
  185. m_bConfigChanged = (bRet != bVal);
  186. return bRet;
  187. }
  188. bool CCopyThread::GetAsyncCopy()
  189. {
  190. ATL::CCritSecLock csLock(m_cs.m_sect);
  191. bool bRet = m_SharedConfig.m_bAsyncCopy;
  192. return bRet;
  193. }
  194. void CCopyThread::Init(CCopyConfig cfg)
  195. {
  196. ASSERT(m_LocalConfig.m_pSupportedTypes == NULL);
  197. m_LocalConfig = m_SharedConfig = cfg;
  198. // let m_LocalConfig own the m_pSupportedTypes
  199. m_SharedConfig.m_pSupportedTypes = NULL;
  200. }
  201. bool CCopyThread::Quit()
  202. {
  203. m_bQuit = true;
  204. m_pClipboardViewer->PostMessage( WM_QUIT );
  205. return CWinThread::PostThreadMessage( WM_QUIT, NULL, NULL ) != FALSE;
  206. }