ProcessPaste.cpp 7.2 KB

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