ProcessPaste.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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_bPasteHTMLFormatAs_CF_TEXT = false;
  17. }
  18. CProcessPaste::~CProcessPaste()
  19. {
  20. DELETE_PTR(m_pOle);
  21. }
  22. BOOL CProcessPaste::DoPaste()
  23. {
  24. m_pOle->m_bOnlyPaste_CF_TEXT = m_bOnlyPaste_CF_TEXT;
  25. m_pOle->m_bPasteHTMLFormatAs_CF_TEXT = m_bPasteHTMLFormatAs_CF_TEXT;
  26. if(m_pOle->DoImmediateRender())
  27. {
  28. // MarkAsPasted() must be done first since it makes use of
  29. // m_pOle->m_ClipIDs and m_pOle is inaccessible after
  30. // SetClipboard is called.
  31. MarkAsPasted();
  32. // Ignore the clipboard change that we will cause IF:
  33. // 1) we are pasting a single element, since the element is already
  34. // in the db and its lDate was updated by MarkAsPasted().
  35. // OR
  36. // 2) we are pasting multiple, but g_Opt.m_bSaveMultiPaste is false
  37. if(GetClipIDs().GetSize() == 1 || !g_Opt.m_bSaveMultiPaste)
  38. {
  39. m_pOle->CacheGlobalData(theApp.m_cfIgnoreClipboard, NewGlobalP("Ignore", sizeof("Ignore")));
  40. }
  41. else
  42. {
  43. m_pOle->CacheGlobalData(theApp.m_cfDelaySavingData, NewGlobalP("Delay", sizeof("Delay")));
  44. }
  45. m_pOle->SetClipboard(); // m_pOle is now managed by the OLE clipboard
  46. // The Clipboard now owns the allocated memory
  47. // and will delete this data object
  48. // when new data is put on the Clipboard
  49. m_pOle = NULL; // m_pOle should not be accessed past this point
  50. #ifndef _DEBUG
  51. if(m_bSendPaste)
  52. {
  53. theApp.SendPaste(m_bActivateTarget);
  54. }
  55. #else
  56. if(m_bActivateTarget)
  57. {
  58. theApp.ActivateTarget();
  59. }
  60. #endif
  61. return TRUE;
  62. }
  63. return FALSE;
  64. }
  65. BOOL CProcessPaste::DoDrag()
  66. {
  67. m_pOle->DoDelayRender();
  68. DROPEFFECT de = m_pOle->DoDragDrop(DROPEFFECT_COPY);
  69. if(de != DROPEFFECT_NONE)
  70. {
  71. MarkAsPasted();
  72. return TRUE;
  73. }
  74. return FALSE;
  75. }
  76. void CProcessPaste::MarkAsPasted()
  77. {
  78. // Log(_T("start of MarkAsPasted"));
  79. CClipIDs& clips = GetClipIDs();
  80. if(clips.GetSize() == 1)
  81. {
  82. CGetSetOptions::SetTripPasteCount(-1);
  83. CGetSetOptions::SetTotalPasteCount(-1);
  84. if(!g_Opt.m_bUpdateTimeOnPaste)
  85. return;
  86. long lID = (long)clips.ElementAt(0);
  87. //Moved to a thread because when running from from U3 devices the write is time consuming
  88. AfxBeginThread(CProcessPaste::MarkAsPastedThread, (LPVOID)lID, THREAD_PRIORITY_LOWEST);
  89. }
  90. // Log(_T("End of MarkAsPasted"));
  91. }
  92. UINT CProcessPaste::MarkAsPastedThread(LPVOID pParam)
  93. {
  94. static CEvent UpdateTimeEvent(TRUE, TRUE, _T("Ditto_Update_Clip_Time"), NULL);
  95. UpdateTimeEvent.ResetEvent();
  96. // Log(_T("Start of MarkAsPastedThread"));
  97. //If running from a U3 device then wait a little before updating the db
  98. //updating the db can take a second or two and it delays the act of pasting
  99. if(g_Opt.m_bU3)
  100. {
  101. Sleep(350);
  102. }
  103. long lID = (long)pParam;
  104. BOOL bRet = FALSE;
  105. try
  106. {
  107. CppSQLite3DB Local_db;
  108. Local_db.open(CGetSetOptions::GetDBPath());
  109. //Update the time it was copied so that it appears at the top of the
  110. //paste list. Items are sorted by this time.
  111. CTime now = CTime::GetCurrentTime();
  112. try
  113. {
  114. CppSQLite3Query q = Local_db.execQuery(_T("SELECT lDate FROM Main ORDER BY lDate DESC LIMIT 1"));
  115. if(q.eof() == false)
  116. {
  117. long lLatestDate = q.getIntField(_T("lDate"));
  118. if(now.GetTime() <= lLatestDate)
  119. {
  120. now = lLatestDate + 1;
  121. }
  122. }
  123. }
  124. CATCH_SQLITE_EXCEPTION
  125. Local_db.execDMLEx(_T("UPDATE Main SET lDate = %d where lID = %d;"), (long)now.GetTime(), lID);
  126. Local_db.close();
  127. bRet = TRUE;
  128. }
  129. CATCH_SQLITE_EXCEPTION
  130. // Log(_T("End of MarkAsPastedThread"));
  131. UpdateTimeEvent.SetEvent();
  132. return bRet;
  133. }