ProcessPaste.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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->DoDelayRender();
  65. DROPEFFECT de = m_pOle->DoDragDrop(DROPEFFECT_COPY);
  66. if(de != DROPEFFECT_NONE)
  67. {
  68. MarkAsPasted();
  69. return TRUE;
  70. }
  71. return FALSE;
  72. }
  73. void CProcessPaste::MarkAsPasted()
  74. {
  75. Log(_T("start of MarkAsPasted"));
  76. CClipIDs& clips = GetClipIDs();
  77. CGetSetOptions::SetTripPasteCount(-1);
  78. CGetSetOptions::SetTotalPasteCount(-1);
  79. MarkAsPastedData* pData = new MarkAsPastedData();
  80. for (int i = 0; i < clips.GetCount(); i++)
  81. {
  82. pData->ids.Add(clips.ElementAt(i));
  83. }
  84. pData->pastedFromGroup = m_pastedFromGroup;
  85. //Moved to a thread because when running from from U3 devices the write is time consuming
  86. AfxBeginThread(CProcessPaste::MarkAsPastedThread, (LPVOID)pData, THREAD_PRIORITY_LOWEST);
  87. Log(_T("End of MarkAsPasted"));
  88. }
  89. UINT CProcessPaste::MarkAsPastedThread(LPVOID pParam)
  90. {
  91. DWORD startTick = GetTickCount();
  92. static CEvent UpdateTimeEvent(TRUE, TRUE, _T("Ditto_Update_Clip_Time"), NULL);
  93. UpdateTimeEvent.ResetEvent();
  94. Log(_T("Start of MarkAsPastedThread"));
  95. BOOL bRet = FALSE;
  96. int clipId = 0;
  97. try
  98. {
  99. MarkAsPastedData* pData = (MarkAsPastedData*)pParam;
  100. if(pData)
  101. {
  102. int clipCount = pData->ids.GetCount();
  103. if(g_Opt.m_bUpdateTimeOnPaste &&
  104. clipCount == 1)
  105. {
  106. for (int i = 0; i < clipCount; i++)
  107. {
  108. int id = pData->ids.ElementAt(i);
  109. try
  110. {
  111. if (pData->pastedFromGroup)
  112. {
  113. CppSQLite3Query q = theApp.m_db.execQuery(_T("SELECT clipGroupOrder FROM Main ORDER BY clipGroupOrder DESC LIMIT 1"));
  114. if (q.eof() == false)
  115. {
  116. double latestDate = q.getFloatField(_T("clipGroupOrder"));
  117. latestDate += 1;
  118. Log(StrF(_T("Setting clipId: %d, GroupOrder: %f"), id, latestDate));
  119. theApp.m_db.execDMLEx(_T("UPDATE Main SET clipGroupOrder = %f where lID = %d;"), latestDate, id);
  120. }
  121. }
  122. else
  123. {
  124. CppSQLite3Query q = theApp.m_db.execQuery(_T("SELECT clipOrder FROM Main ORDER BY clipOrder DESC LIMIT 1"));
  125. if (q.eof() == false)
  126. {
  127. double latestDate = q.getFloatField(_T("clipOrder"));
  128. latestDate += 1;
  129. Log(StrF(_T("Setting clipId: %d, order: %f"), id, latestDate));
  130. theApp.m_db.execDMLEx(_T("UPDATE Main SET clipOrder = %f where lID = %d;"), latestDate, id);
  131. }
  132. }
  133. }
  134. CATCH_SQLITE_EXCEPTION
  135. }
  136. }
  137. try
  138. {
  139. for (int i = 0; i < clipCount; i++)
  140. {
  141. int id = pData->ids.ElementAt(i);
  142. theApp.m_db.execDMLEx(_T("UPDATE Main SET lastPasteDate = %d where lID = %d;"), (int)CTime::GetCurrentTime().GetTime(), id);
  143. }
  144. }
  145. CATCH_SQLITE_EXCEPTION
  146. int refreshFlags = 0;
  147. //if multiple clips are selected then don't change selection
  148. if (clipCount == 1)
  149. {
  150. refreshFlags |= UPDATE_AFTER_PASTE_SELECT_CLIP;
  151. }
  152. for (int i = 0; i < clipCount; i++)
  153. {
  154. int id = pData->ids.ElementAt(i);
  155. //refresh the window on the last clip
  156. if (i == clipCount - 1)
  157. {
  158. refreshFlags |= UPDATE_AFTER_PASTE_REFRESH_VISIBLE;
  159. }
  160. theApp.RefreshClipAfterPaste(id, refreshFlags);
  161. }
  162. delete pData;
  163. bRet = TRUE;
  164. }
  165. }
  166. CATCH_SQLITE_EXCEPTION
  167. Log(_T("End of MarkAsPastedThread"));
  168. DWORD endTick = GetTickCount();
  169. if((endTick-startTick) > 350)
  170. Log(StrF(_T("Paste Timing MarkAsPastedThread: %d, ClipId: %d"), endTick-startTick, clipId));
  171. UpdateTimeEvent.SetEvent();
  172. return bRet;
  173. }