ProcessPaste.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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();
  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();
  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()
  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. //Moved to a thread because when running from from U3 devices the write is time consuming
  146. AfxBeginThread(CProcessPaste::MarkAsPastedThread, (LPVOID)pData, THREAD_PRIORITY_LOWEST);
  147. Log(_T("End of MarkAsPasted"));
  148. }
  149. UINT CProcessPaste::MarkAsPastedThread(LPVOID pParam)
  150. {
  151. DWORD startTick = GetTickCount();
  152. static CEvent UpdateTimeEvent(TRUE, TRUE, _T("Ditto_Update_Clip_Time"), NULL);
  153. UpdateTimeEvent.ResetEvent();
  154. Log(_T("Start of MarkAsPastedThread"));
  155. BOOL bRet = FALSE;
  156. int clipId = 0;
  157. try
  158. {
  159. MarkAsPastedData* pData = (MarkAsPastedData*)pParam;
  160. if(pData)
  161. {
  162. int clipCount = pData->ids.GetCount();
  163. if(g_Opt.m_bUpdateTimeOnPaste &&
  164. clipCount == 1)
  165. {
  166. for (int i = 0; i < clipCount; i++)
  167. {
  168. int id = pData->ids.ElementAt(i);
  169. try
  170. {
  171. if (pData->pastedFromGroup)
  172. {
  173. CppSQLite3Query q = theApp.m_db.execQuery(_T("SELECT clipGroupOrder FROM Main ORDER BY clipGroupOrder DESC LIMIT 1"));
  174. if (q.eof() == false)
  175. {
  176. double latestDate = q.getFloatField(_T("clipGroupOrder"));
  177. latestDate += 1;
  178. Log(StrF(_T("Setting clipId: %d, GroupOrder: %f"), id, latestDate));
  179. theApp.m_db.execDMLEx(_T("UPDATE Main SET clipGroupOrder = %f where lID = %d;"), latestDate, id);
  180. }
  181. }
  182. else
  183. {
  184. CppSQLite3Query q = theApp.m_db.execQuery(_T("SELECT clipOrder FROM Main ORDER BY clipOrder DESC LIMIT 1"));
  185. if (q.eof() == false)
  186. {
  187. double latestDate = q.getFloatField(_T("clipOrder"));
  188. latestDate += 1;
  189. Log(StrF(_T("Setting clipId: %d, order: %f"), id, latestDate));
  190. theApp.m_db.execDMLEx(_T("UPDATE Main SET clipOrder = %f where lID = %d;"), latestDate, id);
  191. }
  192. }
  193. }
  194. CATCH_SQLITE_EXCEPTION
  195. }
  196. }
  197. try
  198. {
  199. for (int i = 0; i < clipCount; i++)
  200. {
  201. int id = pData->ids.ElementAt(i);
  202. theApp.m_db.execDMLEx(_T("UPDATE Main SET lastPasteDate = %d where lID = %d;"), (int)CTime::GetCurrentTime().GetTime(), id);
  203. }
  204. }
  205. CATCH_SQLITE_EXCEPTION
  206. int refreshFlags = 0;
  207. //if multiple clips are selected then don't change selection
  208. if (clipCount == 1)
  209. {
  210. refreshFlags |= UPDATE_AFTER_PASTE_SELECT_CLIP;
  211. }
  212. for (int i = 0; i < clipCount; i++)
  213. {
  214. int id = pData->ids.ElementAt(i);
  215. //refresh the window on the last clip
  216. if (i == clipCount - 1)
  217. {
  218. refreshFlags |= UPDATE_AFTER_PASTE_REFRESH_VISIBLE;
  219. }
  220. theApp.RefreshClipAfterPaste(id, refreshFlags);
  221. }
  222. delete pData;
  223. bRet = TRUE;
  224. }
  225. }
  226. CATCH_SQLITE_EXCEPTION
  227. Log(_T("End of MarkAsPastedThread"));
  228. DWORD endTick = GetTickCount();
  229. if((endTick-startTick) > 350)
  230. Log(StrF(_T("Paste Timing MarkAsPastedThread: %d, ClipId: %d"), endTick-startTick, clipId));
  231. UpdateTimeEvent.SetEvent();
  232. return bRet;
  233. }