1
0

ProcessPaste.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. if(clips.GetSize() == 1)
  78. {
  79. CGetSetOptions::SetTripPasteCount(-1);
  80. CGetSetOptions::SetTotalPasteCount(-1);
  81. MarkAsPastedData* pData = new MarkAsPastedData();
  82. pData->clipId = clips.ElementAt(0);
  83. pData->pastedFromGroup = m_pastedFromGroup;
  84. //Moved to a thread because when running from from U3 devices the write is time consuming
  85. AfxBeginThread(CProcessPaste::MarkAsPastedThread, (LPVOID)pData, THREAD_PRIORITY_LOWEST);
  86. }
  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. //If running from a U3 device then wait a little before updating the db
  96. //updating the db can take a second or two and it delays the act of pasting
  97. if(g_Opt.m_bU3)
  98. {
  99. Sleep(350);
  100. }
  101. BOOL bRet = FALSE;
  102. int clipId = 0;
  103. try
  104. {
  105. MarkAsPastedData* pData = (MarkAsPastedData*)pParam;
  106. if(pData)
  107. {
  108. clipId = pData->clipId;
  109. if(g_Opt.m_bUpdateTimeOnPaste)
  110. {
  111. try
  112. {
  113. if(pData->pastedFromGroup)
  114. {
  115. CppSQLite3Query q = theApp.m_db.execQuery(_T("SELECT clipGroupOrder FROM Main ORDER BY clipGroupOrder DESC LIMIT 1"));
  116. if(q.eof() == false)
  117. {
  118. double latestDate = q.getFloatField(_T("clipGroupOrder"));
  119. latestDate += 1;
  120. Log(StrF(_T("Setting clipId: %d, GroupOrder: %f"), pData->clipId, latestDate));
  121. theApp.m_db.execDMLEx(_T("UPDATE Main SET clipGroupOrder = %f where lID = %d;"), latestDate, pData->clipId);
  122. }
  123. }
  124. else
  125. {
  126. CppSQLite3Query q = theApp.m_db.execQuery(_T("SELECT clipOrder FROM Main ORDER BY clipOrder DESC LIMIT 1"));
  127. if(q.eof() == false)
  128. {
  129. double latestDate = q.getFloatField(_T("clipOrder"));
  130. latestDate += 1;
  131. Log(StrF(_T("Setting clipId: %d, order: %f"), pData->clipId, latestDate));
  132. theApp.m_db.execDMLEx(_T("UPDATE Main SET clipOrder = %f where lID = %d;"), latestDate, pData->clipId);
  133. }
  134. }
  135. }
  136. CATCH_SQLITE_EXCEPTION
  137. }
  138. try
  139. {
  140. theApp.m_db.execDMLEx(_T("UPDATE Main SET lastPasteDate = %d where lID = %d;"), (int)CTime::GetCurrentTime().GetTime(), pData->clipId);
  141. }
  142. CATCH_SQLITE_EXCEPTION
  143. theApp.RefreshClipAfterPaste(pData->clipId);
  144. delete pData;
  145. bRet = TRUE;
  146. }
  147. }
  148. CATCH_SQLITE_EXCEPTION
  149. Log(_T("End of MarkAsPastedThread"));
  150. DWORD endTick = GetTickCount();
  151. if((endTick-startTick) > 350)
  152. Log(StrF(_T("Paste Timing MarkAsPastedThread: %d, ClipId: %d"), endTick-startTick, clipId));
  153. UpdateTimeEvent.SetEvent();
  154. return bRet;
  155. }