ProcessPaste.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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_bOnlyPaste_CF_TEXT = false;
  16. m_pastedFromGroup = false;
  17. }
  18. CProcessPaste::~CProcessPaste()
  19. {
  20. delete m_pOle;
  21. }
  22. BOOL CProcessPaste::DoPaste()
  23. {
  24. m_pOle->m_bOnlyPaste_CF_TEXT = m_bOnlyPaste_CF_TEXT;
  25. if(m_pOle->DoImmediateRender())
  26. {
  27. // MarkAsPasted() must be done first since it makes use of
  28. // m_pOle->m_ClipIDs and m_pOle is inaccessible after
  29. // SetClipboard is called.
  30. MarkAsPasted();
  31. // Ignore the clipboard change that we will cause IF:
  32. // 1) we are pasting a single element, since the element is already
  33. // in the db and its lDate was updated by MarkAsPasted().
  34. // OR
  35. // 2) we are pasting multiple, but g_Opt.m_bSaveMultiPaste is false
  36. if(GetClipIDs().GetSize() == 1 || !g_Opt.m_bSaveMultiPaste)
  37. {
  38. m_pOle->CacheGlobalData(theApp.m_cfIgnoreClipboard, NewGlobalP("Ignore", sizeof("Ignore")));
  39. }
  40. else
  41. {
  42. m_pOle->CacheGlobalData(theApp.m_cfDelaySavingData, NewGlobalP("Delay", sizeof("Delay")));
  43. }
  44. m_pOle->SetClipboard(); // m_pOle is now managed by the OLE clipboard
  45. // The Clipboard now owns the allocated memory
  46. // and will delete this data object
  47. // when new data is put on the Clipboard
  48. m_pOle = NULL; // m_pOle should not be accessed past this point
  49. if(m_bSendPaste)
  50. {
  51. Log(_T("Sending Paste to active window"));
  52. theApp.m_activeWnd.SendPaste(m_bActivateTarget);
  53. }
  54. else if(m_bActivateTarget)
  55. {
  56. Log(_T("Activating active window"));
  57. theApp.m_activeWnd.ActivateTarget();
  58. }
  59. return TRUE;
  60. }
  61. return FALSE;
  62. }
  63. BOOL CProcessPaste::DoDrag()
  64. {
  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. if(clips.GetSize() == 1)
  79. {
  80. CGetSetOptions::SetTripPasteCount(-1);
  81. CGetSetOptions::SetTotalPasteCount(-1);
  82. MarkAsPastedData* pData = new MarkAsPastedData();
  83. pData->clipId = clips.ElementAt(0);
  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. }
  88. Log(_T("End of MarkAsPasted"));
  89. }
  90. UINT CProcessPaste::MarkAsPastedThread(LPVOID pParam)
  91. {
  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. try
  103. {
  104. MarkAsPastedData* pData = (MarkAsPastedData*)pParam;
  105. if(pData)
  106. {
  107. if(g_Opt.m_bUpdateTimeOnPaste)
  108. {
  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"), pData->clipId, latestDate));
  119. theApp.m_db.execDMLEx(_T("UPDATE Main SET clipGroupOrder = %f where lID = %d;"), latestDate, pData->clipId);
  120. theApp.RefreshClipOrder(pData->clipId);
  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"), pData->clipId, latestDate));
  131. theApp.m_db.execDMLEx(_T("UPDATE Main SET clipOrder = %f where lID = %d;"), latestDate, pData->clipId);
  132. theApp.RefreshClipOrder(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;"), CTime::GetCurrentTime().GetTime(), pData);
  141. }
  142. CATCH_SQLITE_EXCEPTION
  143. delete pData;
  144. bRet = TRUE;
  145. }
  146. }
  147. CATCH_SQLITE_EXCEPTION
  148. Log(_T("End of MarkAsPastedThread"));
  149. UpdateTimeEvent.SetEvent();
  150. return bRet;
  151. }