ProcessPaste.cpp 3.7 KB

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