ProcessPaste.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #include "stdafx.h"
  2. #include "CP_Main.h"
  3. #include "ProcessPaste.h"
  4. #include "ClipIds.h"
  5. #ifdef _DEBUG
  6. #undef THIS_FILE
  7. static char THIS_FILE[]=__FILE__;
  8. #define new DEBUG_NEW
  9. #endif
  10. CProcessPaste::CProcessPaste()
  11. {
  12. m_pOle = new COleClipSource;
  13. m_bSendPaste = true;
  14. m_bActivateTarget = true;
  15. m_pastedFromGroup = false;
  16. }
  17. CProcessPaste::~CProcessPaste()
  18. {
  19. delete m_pOle;
  20. }
  21. BOOL CProcessPaste::DoPaste()
  22. {
  23. BOOL ret = FALSE;
  24. try
  25. {
  26. m_pOle->m_pasteOptions = m_pasteOptions;
  27. if (!m_pOle->DoImmediateRender())
  28. {
  29. return ret;
  30. }
  31. // MarkAsPasted() must be done first since it makes use of
  32. // m_pOle->m_ClipIDs and m_pOle is inaccessible after
  33. // SetClipboard is called.
  34. MarkAsPasted(m_pasteOptions.m_updateClipOrder);
  35. // Ignore the clipboard change that we will cause IF:
  36. // 1) we are pasting a single element, since the element is already
  37. // in the db and its lDate was updated by MarkAsPasted().
  38. // OR
  39. // 2) we are pasting multiple, but g_Opt.m_bSaveMultiPaste is false
  40. if (GetClipIDs().GetSize() == 1 || !g_Opt.m_bSaveMultiPaste)
  41. {
  42. m_pOle->CacheGlobalData(theApp.m_cfIgnoreClipboard, NewGlobalP("Ignore", sizeof("Ignore")));
  43. }
  44. else
  45. {
  46. m_pOle->CacheGlobalData(theApp.m_cfDelaySavingData, NewGlobalP("Delay", sizeof("Delay")));
  47. }
  48. m_pOle->SetClipboard(); // m_pOle is now managed by the OLE clipboard
  49. if (m_bSendPaste)
  50. {
  51. Log(_T("Sending Paste to active window"));
  52. theApp.m_activeWnd.SendPaste(m_bActivateTarget);
  53. }
  54. else if (m_bActivateTarget)
  55. {
  56. Log(_T("Activating active window"));
  57. theApp.m_activeWnd.ActivateTarget();
  58. }
  59. ret = TRUE;
  60. }
  61. catch (CException *ex)
  62. {
  63. TCHAR szCause[255];
  64. ex->GetErrorMessage(szCause, 255);
  65. m_lastErrorMessage.Format(_T("Paste exception: %s"), szCause);
  66. Log(m_lastErrorMessage);
  67. }
  68. catch (...)
  69. {
  70. m_lastErrorMessage = _T("Paste generic exception");
  71. Log(m_lastErrorMessage);
  72. }
  73. // The Clipboard now owns the allocated memory
  74. // and will delete this data object
  75. // when new data is put on the Clipboard
  76. m_pOle = NULL; // m_pOle should not be accessed past this point
  77. return ret;
  78. }
  79. BOOL CProcessPaste::DoDrag()
  80. {
  81. BOOL ret = FALSE;
  82. try
  83. {
  84. m_pOle->m_pasteOptions = m_pasteOptions;
  85. m_pOle->DoDelayRender();
  86. DROPEFFECT de = m_pOle->DoDragDrop(DROPEFFECT_COPY);
  87. if (de != DROPEFFECT_NONE)
  88. {
  89. MarkAsPasted(m_pasteOptions.m_updateClipOrder);
  90. ret = TRUE;
  91. }
  92. }
  93. catch (CException *ex)
  94. {
  95. TCHAR szCause[255];
  96. ex->GetErrorMessage(szCause, 255);
  97. m_lastErrorMessage.Format(_T("Drag drop exception: %s"), szCause);
  98. Log(m_lastErrorMessage);
  99. }
  100. catch (...)
  101. {
  102. m_lastErrorMessage = _T("Drag drop generic exception");
  103. Log(m_lastErrorMessage);
  104. }
  105. try
  106. {
  107. //from https://www.codeproject.com/Articles/886711/Drag-Drop-Images-and-Drop-Descriptions-for-MFC-App
  108. //You may have noted the InternalRelease() function call.This is required here to delete the object.While it is possible to use
  109. //delete or create the object on the stack with Drag & Drop operations, it is not recommended to do so.
  110. m_pOle->InternalRelease();
  111. }
  112. catch (CException *ex)
  113. {
  114. TCHAR szCause[255];
  115. ex->GetErrorMessage(szCause, 255);
  116. m_lastErrorMessage.Format(_T("Drag drop exception 2: %s"), szCause);
  117. Log(m_lastErrorMessage);
  118. }
  119. catch (...)
  120. {
  121. m_lastErrorMessage = _T("Drag drop generic exception 2");
  122. Log(m_lastErrorMessage);
  123. }
  124. // The Clipboard now owns the allocated memory
  125. // and will delete this data object
  126. // when new data is put on the Clipboard
  127. m_pOle = NULL; // m_pOle should not be accessed past this point
  128. return ret;
  129. }
  130. void CProcessPaste::MarkAsPasted(bool updateClipOrder)
  131. {
  132. Log(_T("start of MarkAsPasted"));
  133. CClipIDs& clips = GetClipIDs();
  134. CGetSetOptions::SetTripPasteCount(-1);
  135. CGetSetOptions::SetTotalPasteCount(-1);
  136. MarkAsPastedData* pData = new MarkAsPastedData();
  137. for (int i = 0; i < clips.GetCount(); i++)
  138. {
  139. pData->ids.Add(clips.ElementAt(i));
  140. }
  141. pData->pastedFromGroup = m_pastedFromGroup;
  142. pData->updateClipOrder = updateClipOrder;
  143. //Moved to a thread because when running from from U3 devices the write is time consuming
  144. AfxBeginThread(CProcessPaste::MarkAsPastedThread, (LPVOID)pData, THREAD_PRIORITY_LOWEST);
  145. Log(_T("End of MarkAsPasted"));
  146. }
  147. UINT CProcessPaste::MarkAsPastedThread(LPVOID pParam)
  148. {
  149. DWORD startTick = GetTickCount();
  150. static CEvent UpdateTimeEvent(TRUE, TRUE, _T("Ditto_Update_Clip_Time"), NULL);
  151. UpdateTimeEvent.ResetEvent();
  152. Log(_T("Start of MarkAsPastedThread"));
  153. BOOL bRet = FALSE;
  154. int clipId = 0;
  155. try
  156. {
  157. int refreshFlags = 0;
  158. MarkAsPastedData* pData = (MarkAsPastedData*)pParam;
  159. if(pData)
  160. {
  161. int clipCount = (int)pData->ids.GetCount();
  162. if(g_Opt.m_bUpdateTimeOnPaste &&
  163. pData->updateClipOrder &&
  164. clipCount == 1)
  165. {
  166. if (CGetSetOptions::m_refreshViewAfterPasting)
  167. {
  168. refreshFlags |= UPDATE_AFTER_PASTE_SELECT_CLIP;
  169. }
  170. for (int i = 0; i < clipCount; i++)
  171. {
  172. int id = pData->ids.ElementAt(i);
  173. try
  174. {
  175. if (pData->pastedFromGroup)
  176. {
  177. CppSQLite3Query q = theApp.m_db.execQuery(_T("SELECT clipGroupOrder FROM Main ORDER BY clipGroupOrder DESC LIMIT 1"));
  178. if (q.eof() == false)
  179. {
  180. double latestDate = q.getFloatField(_T("clipGroupOrder"));
  181. latestDate += 1;
  182. Log(StrF(_T("Setting clipId: %d, GroupOrder: %f"), id, latestDate));
  183. theApp.m_db.execDMLEx(_T("UPDATE Main SET clipGroupOrder = %f where lID = %d;"), latestDate, id);
  184. }
  185. }
  186. else
  187. {
  188. CppSQLite3Query q = theApp.m_db.execQuery(_T("SELECT clipOrder FROM Main ORDER BY clipOrder DESC LIMIT 1"));
  189. if (q.eof() == false)
  190. {
  191. double latestDate = q.getFloatField(_T("clipOrder"));
  192. latestDate += 1;
  193. Log(StrF(_T("Setting clipId: %d, order: %f"), id, latestDate));
  194. theApp.m_db.execDMLEx(_T("UPDATE Main SET clipOrder = %f where lID = %d;"), latestDate, id);
  195. }
  196. }
  197. }
  198. CATCH_SQLITE_EXCEPTION
  199. }
  200. }
  201. try
  202. {
  203. for (int i = 0; i < clipCount; i++)
  204. {
  205. int id = pData->ids.ElementAt(i);
  206. theApp.m_db.execDMLEx(_T("UPDATE Main SET lastPasteDate = %d where lID = %d;"), (int)CTime::GetCurrentTime().GetTime(), id);
  207. }
  208. }
  209. CATCH_SQLITE_EXCEPTION
  210. for (int i = 0; i < clipCount; i++)
  211. {
  212. int id = pData->ids.ElementAt(i);
  213. theApp.RefreshClipAfterPaste(id, refreshFlags);
  214. break;
  215. }
  216. delete pData;
  217. bRet = TRUE;
  218. }
  219. }
  220. CATCH_SQLITE_EXCEPTION
  221. Log(_T("End of MarkAsPastedThread"));
  222. DWORD endTick = GetTickCount();
  223. if((endTick-startTick) > 350)
  224. Log(StrF(_T("Paste Timing MarkAsPastedThread: %d, ClipId: %d"), endTick-startTick, clipId));
  225. UpdateTimeEvent.SetEvent();
  226. return bRet;
  227. }