ProcessPaste.cpp 5.3 KB

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