ProcessPaste.cpp 6.5 KB

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