ClipboardViewer.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. // ClipboardViewer.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "cp_main.h"
  5. #include "ClipboardViewer.h"
  6. #include "Misc.h"
  7. #include "shared/Tokenizer.h"
  8. #include "WildCardMatch.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CClipboardViewer
  16. CClipboardViewer::CClipboardViewer(CCopyThread* pHandler) :
  17. m_hNextClipboardViewer(0),
  18. m_bCalling_SetClipboardViewer(false),
  19. m_pHandler(pHandler),
  20. m_bPinging(false),
  21. m_bHandlingClipChange(false),
  22. m_bIsConnected(false),
  23. m_bConnect(false),
  24. m_dwLastCopy(0),
  25. m_connectOnStartup(true)
  26. {
  27. }
  28. CClipboardViewer::~CClipboardViewer()
  29. {
  30. }
  31. BEGIN_MESSAGE_MAP(CClipboardViewer, CWnd)
  32. //{{AFX_MSG_MAP(CClipboardViewer)
  33. ON_WM_CREATE()
  34. ON_WM_CHANGECBCHAIN()
  35. ON_WM_DRAWCLIPBOARD()
  36. ON_WM_TIMER()
  37. ON_WM_DESTROY()
  38. //}}AFX_MSG_MAP
  39. ON_MESSAGE(WM_SETCONNECT, OnSetConnect)
  40. END_MESSAGE_MAP()
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CClipboardViewer message handlers
  43. void CClipboardViewer::Create()
  44. {
  45. CString strParentClass = AfxRegisterWndClass(0);
  46. CWnd::CreateEx(0, strParentClass, _T("Ditto Clipboard Viewer"), 0, -1, -1, 0, 0, 0, 0);
  47. if(m_connectOnStartup)
  48. {
  49. SetConnect(true);
  50. }
  51. }
  52. // connects as a clipboard viewer
  53. void CClipboardViewer::Connect()
  54. {
  55. Log(_T("Connect to Clipboard"));
  56. //Set up the clip board viewer
  57. m_bCalling_SetClipboardViewer = true;
  58. m_hNextClipboardViewer = CWnd::SetClipboardViewer();
  59. m_bCalling_SetClipboardViewer = false;
  60. m_bIsConnected = true;
  61. m_bConnect = true;
  62. SetEnsureConnectedTimer();
  63. }
  64. void CClipboardViewer::SetEnsureConnectedTimer()
  65. {
  66. SetTimer(TIMER_ENSURE_VIEWER_IN_CHAIN, ONE_MINUTE*5, NULL);
  67. }
  68. // disconnects as a clipboard viewer
  69. void CClipboardViewer::Disconnect(bool bSendPing)
  70. {
  71. Log(_T("Disconnect From Clipboard"));
  72. KillTimer(TIMER_ENSURE_VIEWER_IN_CHAIN);
  73. BOOL bRet = CWnd::ChangeClipboardChain(m_hNextClipboardViewer);
  74. if(!bRet)
  75. {
  76. Log(_T("Error disconnecting from clipboard"));
  77. bRet = CWnd::ChangeClipboardChain(m_hNextClipboardViewer);
  78. if(!bRet)
  79. {
  80. Log(_T("Error disconnecting from clipboard2"));
  81. }
  82. }
  83. m_hNextClipboardViewer = 0;
  84. m_bConnect = false;
  85. m_bIsConnected = false;
  86. if(bSendPing)
  87. SendPing();
  88. }
  89. void CClipboardViewer::SendPing()
  90. {
  91. if(g_Opt.m_bEnsureConnectToClipboard)
  92. {
  93. if(OpenClipboard())
  94. {
  95. m_bPinging = true;
  96. SetClipboardData(theApp.m_PingFormat, NewGlobalP("Ditto Ping", sizeof("Ditto Ping")));
  97. SetClipboardData(theApp.m_cfIgnoreClipboard , NewGlobalP("Ignore", sizeof("Ignore")));
  98. SetTimer(TIMER_PING, 2000, NULL);
  99. CloseClipboard();
  100. }
  101. }
  102. }
  103. void CClipboardViewer::SetConnect(bool bConnect)
  104. {
  105. m_bConnect = bConnect;
  106. if(bConnect)
  107. {
  108. if(m_bIsConnected == false)
  109. {
  110. Connect();
  111. }
  112. else
  113. {
  114. SendPing();
  115. }
  116. }
  117. else
  118. {
  119. Disconnect();
  120. }
  121. }
  122. /////////////////////////////////////////////////////////////////////////////
  123. // CClipboardViewer message handlers
  124. int CClipboardViewer::OnCreate(LPCREATESTRUCT lpCreateStruct)
  125. {
  126. if(CWnd::OnCreate(lpCreateStruct) == -1)
  127. return -1;
  128. //Set up the clip board viewer
  129. if(m_connectOnStartup)
  130. {
  131. Connect();
  132. }
  133. return 0;
  134. }
  135. void CClipboardViewer::OnDestroy()
  136. {
  137. Disconnect();
  138. CWnd::OnDestroy();
  139. }
  140. void CClipboardViewer::OnChangeCbChain(HWND hWndRemove, HWND hWndAfter)
  141. {
  142. Log(_T("OnChangeCbChain"));
  143. // If the next window is closing, repair the chain.
  144. if(m_hNextClipboardViewer == hWndRemove)
  145. {
  146. m_hNextClipboardViewer = hWndAfter;
  147. }
  148. // Otherwise, pass the message to the next link.
  149. else if (m_hNextClipboardViewer != NULL)
  150. {
  151. if(m_hNextClipboardViewer != m_hWnd)
  152. {
  153. ::SendMessage(m_hNextClipboardViewer, WM_CHANGECBCHAIN, (WPARAM) hWndRemove, (LPARAM) hWndAfter);
  154. }
  155. else
  156. {
  157. m_hNextClipboardViewer = NULL;
  158. }
  159. }
  160. }
  161. //Message that the clipboard data has changed
  162. void CClipboardViewer::OnDrawClipboard()
  163. {
  164. if(::IsClipboardFormatAvailable(theApp.m_PingFormat))
  165. {
  166. m_bPinging = false;
  167. return;
  168. }
  169. // don't process the event when we first attach
  170. if(m_pHandler && !m_bCalling_SetClipboardViewer)
  171. {
  172. if(m_bIsConnected)
  173. {
  174. if(!::IsClipboardFormatAvailable(theApp.m_cfIgnoreClipboard))
  175. {
  176. if(ValidActiveWnd())
  177. {
  178. Log(StrF(_T("OnDrawClipboard:: *** SetTimer *** %d"), GetTickCount()));
  179. KillTimer(TIMER_DRAW_CLIPBOARD);
  180. SetTimer(TIMER_DRAW_CLIPBOARD, g_Opt.m_lProcessDrawClipboardDelay, NULL);
  181. }
  182. }
  183. }
  184. else
  185. {
  186. Log(_T("Not connected, ignore clipboard change"));
  187. }
  188. }
  189. // pass the event to the next Clipboard viewer in the chain
  190. if(m_hNextClipboardViewer != NULL)
  191. {
  192. if(m_hNextClipboardViewer != m_hWnd)
  193. {
  194. ::SendMessage(m_hNextClipboardViewer, WM_DRAWCLIPBOARD, 0, 0);
  195. }
  196. else
  197. {
  198. m_hNextClipboardViewer = NULL;
  199. }
  200. }
  201. }
  202. bool CClipboardViewer::ValidActiveWnd()
  203. {
  204. HWND active = ::GetForegroundWindow();
  205. CString activeApp = GetProcessName(active).MakeLower();
  206. CString includeApps = CGetSetOptions::GetCopyAppInclude().MakeLower();
  207. Log(StrF(_T("INCLUDE app names: %s, Active App: %s"), includeApps, activeApp));
  208. bool tokenMatch = false;
  209. CTokenizer token(includeApps, CGetSetOptions::GetCopyAppSeparator());
  210. CString line;
  211. while(token.Next(line))
  212. {
  213. if(line != "")
  214. {
  215. if(CWildCardMatch::WildMatch(line, activeApp, ""))
  216. {
  217. Log(StrF(_T("Inlclude app names Found Match %s - %s"), line, activeApp));
  218. tokenMatch = true;
  219. break;
  220. }
  221. }
  222. }
  223. if(tokenMatch)
  224. {
  225. CString excludeApps = CGetSetOptions::GetCopyAppExclude().MakeLower();
  226. if(excludeApps != "")
  227. {
  228. Log(StrF(_T("EXCLUDE app names %s, Active App: %s"), excludeApps, activeApp));
  229. CTokenizer token2(excludeApps, CGetSetOptions::GetCopyAppSeparator());
  230. CString line2;
  231. while(token2.Next(line2))
  232. {
  233. if(line2 != "")
  234. {
  235. if(CWildCardMatch::WildMatch(line2, activeApp, ""))
  236. {
  237. Log(StrF(_T("Exclude app names Found Match %s - %s - NOT SAVING COPY"), line2, activeApp));
  238. return false;
  239. }
  240. }
  241. }
  242. }
  243. }
  244. else
  245. {
  246. Log(StrF(_T("Didn't find a match to INCLUDE match %s, NOT SAVING COPY"), includeApps));
  247. return false;
  248. }
  249. return true;
  250. }
  251. void CClipboardViewer::OnTimer(UINT_PTR nIDEvent)
  252. {
  253. switch(nIDEvent)
  254. {
  255. case TIMER_ENSURE_VIEWER_IN_CHAIN:
  256. SendPing();
  257. break;
  258. case TIMER_DRAW_CLIPBOARD:
  259. KillTimer(nIDEvent);
  260. if(m_bHandlingClipChange == false)
  261. {
  262. m_bHandlingClipChange = true;
  263. DWORD dwNow = GetTickCount();
  264. if(dwNow - m_dwLastCopy > g_Opt.m_dwSaveClipDelay || m_dwLastCopy > dwNow)
  265. {
  266. if(!::IsClipboardFormatAvailable(theApp.m_cfIgnoreClipboard))
  267. {
  268. Log(StrF(_T("OnDrawClipboard::OnTimer %d"), dwNow));
  269. m_pHandler->OnClipboardChange();
  270. m_dwLastCopy = dwNow;
  271. }
  272. }
  273. else
  274. {
  275. Log(StrF(_T("Clip copy to fast difference from last copy = %d"), (dwNow - m_dwLastCopy)));
  276. }
  277. m_bHandlingClipChange = false;
  278. }
  279. else
  280. {
  281. Log(_T("HandlingClipChange is Set, ERROR"));
  282. }
  283. case TIMER_PING:
  284. KillTimer(TIMER_PING);
  285. //If we haven't received the change clipboard message then we are disconnected
  286. //if so reconnect
  287. if(m_bPinging)
  288. {
  289. if(m_bConnect)
  290. {
  291. Log(_T("Ping Failed Reconnecting to clipboard"));
  292. Connect();
  293. }
  294. else
  295. {
  296. Log(_T("Ping Failed but Connected set to FALSE so this is ok"));
  297. }
  298. }
  299. else
  300. {
  301. if(m_bConnect)
  302. {
  303. m_bIsConnected = true;
  304. }
  305. }
  306. break;
  307. }
  308. CWnd::OnTimer(nIDEvent);
  309. }
  310. LRESULT CClipboardViewer::OnSetConnect(WPARAM wParam, LPARAM lParam)
  311. {
  312. bool bConnect = wParam == TRUE;
  313. SetConnect(bConnect);
  314. return TRUE;
  315. }