CopyThread.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. CClipTypes* pSupportedTypes = m_LocalConfig.m_pSupportedTypes;
  57. bool bDeleteMemory = false;
  58. //If we are copying from a Ditto Buffer then save all to the database, so when we paste this it will paste
  59. //just like you were using Ctrl-V
  60. if(theApp.m_CopyBuffer.Active())
  61. {
  62. Log(_T("LoadFromClipboard - Copy buffer Active Start"));
  63. pSupportedTypes = new CClipTypes;
  64. if(pSupportedTypes)
  65. {
  66. bDeleteMemory = true;
  67. COleDataObject oleData;
  68. if(oleData.AttachClipboard())
  69. {
  70. oleData.BeginEnumFormats();
  71. FORMATETC format;
  72. while(oleData.GetNextFormat(&format))
  73. {
  74. pSupportedTypes->Add(format.cfFormat);
  75. }
  76. oleData.Release();
  77. }
  78. }
  79. else
  80. {
  81. pSupportedTypes = m_LocalConfig.m_pSupportedTypes;
  82. }
  83. Log(_T("LoadFromClipboard - Copy buffer Active End"));
  84. }
  85. Log(_T("LoadFromClipboard - Before"));
  86. int bResult = pClip->LoadFromClipboard(pSupportedTypes, true, activeWindow, activeWindowTitle);
  87. Log(_T("LoadFromClipboard - After"));
  88. if(bResult == FALSE)
  89. {
  90. DWORD delay = CGetSetOptions::GetNoFormatsRetryDelay();
  91. if(delay > 0)
  92. {
  93. Log(StrF(_T("LoadFromClipboard didn't find any clips to save, sleeping %dms, then trying again"), delay));
  94. Sleep(delay);
  95. Log(_T("LoadFromClipboard #2 - Before"));
  96. bResult = pClip->LoadFromClipboard(pSupportedTypes, activeWindow);
  97. Log(_T("LoadFromClipboard #2 - After"));
  98. }
  99. else
  100. {
  101. Log(_T("LoadFromClipboard didn't find any clips to save, retry setting is not set, not retrying"));
  102. }
  103. }
  104. if(bDeleteMemory)
  105. {
  106. delete pSupportedTypes;
  107. pSupportedTypes = NULL;
  108. }
  109. if(bResult != TRUE)
  110. {
  111. delete pClip;
  112. return; // error
  113. }
  114. if(pClip != NULL &&
  115. groupId > -1)
  116. {
  117. pClip->m_parentId = groupId;
  118. }
  119. if(m_LocalConfig.m_bAsyncCopy)
  120. ::PostMessage(m_LocalConfig.m_hClipHandler, WM_CLIPBOARD_COPIED, (WPARAM)pClip, 0);
  121. else
  122. ::SendMessage(m_LocalConfig.m_hClipHandler, WM_CLIPBOARD_COPIED, (WPARAM)pClip, 0);
  123. Log(_T("OnClipboardChange - End"));
  124. }
  125. void CCopyThread::SyncConfig()
  126. {
  127. // atomic read
  128. if(m_bConfigChanged)
  129. {
  130. CClipTypes* pTypes = NULL;
  131. ATL::CCritSecLock csLock(m_cs.m_sect);
  132. pTypes = m_LocalConfig.m_pSupportedTypes;
  133. m_LocalConfig = m_SharedConfig;
  134. // NULL means that it shouldn't have been sync'ed
  135. if( m_SharedConfig.m_pSupportedTypes == NULL )
  136. { // let m_LocalConfig keep its types
  137. m_LocalConfig.m_pSupportedTypes = pTypes; // undo sync
  138. pTypes = NULL; // nothing to delete
  139. }
  140. else
  141. m_SharedConfig.m_pSupportedTypes = NULL; // now owned by LocalConfig
  142. // delete old types
  143. if( pTypes )
  144. {
  145. delete pTypes;
  146. }
  147. }
  148. }
  149. bool CCopyThread::IsClipboardViewerConnected()
  150. {
  151. return m_pClipboardViewer->m_bIsConnected;
  152. }
  153. bool CCopyThread::GetConnectCV()
  154. {
  155. return m_pClipboardViewer->GetConnect();
  156. }
  157. void CCopyThread::SetConnectCV(bool bConnect)
  158. {
  159. if(m_pClipboardViewer != NULL && m_pClipboardViewer->m_hWnd != NULL)
  160. {
  161. ::SendMessage( m_pClipboardViewer->m_hWnd, WM_SETCONNECT, bConnect, 0 );
  162. }
  163. }
  164. void CCopyThread::SetSupportedTypes( CClipTypes* pTypes )
  165. {
  166. ATL::CCritSecLock csLock(m_cs.m_sect);
  167. if(m_SharedConfig.m_pSupportedTypes)
  168. {
  169. delete m_SharedConfig.m_pSupportedTypes;
  170. }
  171. m_SharedConfig.m_pSupportedTypes = pTypes;
  172. m_bConfigChanged = true;
  173. }
  174. HWND CCopyThread::SetClipHandler(HWND hWnd)
  175. {
  176. ATL::CCritSecLock csLock(m_cs.m_sect);
  177. HWND hRet = m_SharedConfig.m_hClipHandler;
  178. m_SharedConfig.m_hClipHandler = hWnd;
  179. m_bConfigChanged = (hRet != hWnd);
  180. return hRet;
  181. }
  182. HWND CCopyThread::GetClipHandler()
  183. {
  184. ATL::CCritSecLock csLock(m_cs.m_sect);
  185. HWND hRet = m_SharedConfig.m_hClipHandler;
  186. return hRet;
  187. }
  188. bool CCopyThread::SetCopyOnChange(bool bVal)
  189. {
  190. ATL::CCritSecLock csLock(m_cs.m_sect);
  191. bool bRet = m_SharedConfig.m_bCopyOnChange;
  192. m_SharedConfig.m_bCopyOnChange = bVal;
  193. m_bConfigChanged = (bRet != bVal);
  194. return bRet;
  195. }
  196. bool CCopyThread::GetCopyOnChange()
  197. {
  198. ATL::CCritSecLock csLock(m_cs.m_sect);
  199. bool bRet = m_SharedConfig.m_bCopyOnChange;
  200. return bRet;
  201. }
  202. bool CCopyThread::SetAsyncCopy(bool bVal)
  203. {
  204. ATL::CCritSecLock csLock(m_cs.m_sect);
  205. bool bRet = m_SharedConfig.m_bAsyncCopy;
  206. m_SharedConfig.m_bAsyncCopy = bVal;
  207. m_bConfigChanged = (bRet != bVal);
  208. return bRet;
  209. }
  210. bool CCopyThread::GetAsyncCopy()
  211. {
  212. ATL::CCritSecLock csLock(m_cs.m_sect);
  213. bool bRet = m_SharedConfig.m_bAsyncCopy;
  214. return bRet;
  215. }
  216. void CCopyThread::Init(CCopyConfig cfg)
  217. {
  218. ASSERT(m_LocalConfig.m_pSupportedTypes == NULL);
  219. m_LocalConfig = m_SharedConfig = cfg;
  220. // let m_LocalConfig own the m_pSupportedTypes
  221. m_SharedConfig.m_pSupportedTypes = NULL;
  222. }
  223. bool CCopyThread::Quit()
  224. {
  225. m_bQuit = true;
  226. m_pClipboardViewer->PostMessage( WM_QUIT );
  227. return CWinThread::PostThreadMessage( WM_QUIT, NULL, NULL ) != FALSE;
  228. }