ProcessPaste.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. }
  17. CProcessPaste::~CProcessPaste()
  18. {
  19. DELETE_PTR(m_pOle);
  20. }
  21. BOOL CProcessPaste::DoPaste()
  22. {
  23. m_pOle->m_bOnlyPaste_CF_TEXT = m_bOnlyPaste_CF_TEXT;
  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 MarkAsPasted().
  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. if(!g_Opt.m_bUpdateTimeOnPaste)
  82. return;
  83. int id = clips.ElementAt(0);
  84. //Moved to a thread because when running from from U3 devices the write is time consuming
  85. AfxBeginThread(CProcessPaste::MarkAsPastedThread, (LPVOID)id, THREAD_PRIORITY_LOWEST);
  86. }
  87. Log(_T("End of MarkAsPasted"));
  88. }
  89. UINT CProcessPaste::MarkAsPastedThread(LPVOID pParam)
  90. {
  91. static CEvent UpdateTimeEvent(TRUE, TRUE, _T("Ditto_Update_Clip_Time"), NULL);
  92. UpdateTimeEvent.ResetEvent();
  93. Log(_T("Start of MarkAsPastedThread"));
  94. //If running from a U3 device then wait a little before updating the db
  95. //updating the db can take a second or two and it delays the act of pasting
  96. if(g_Opt.m_bU3)
  97. {
  98. Sleep(350);
  99. }
  100. int id = (int)pParam;
  101. BOOL bRet = FALSE;
  102. try
  103. {
  104. try
  105. {
  106. CppSQLite3Query q = theApp.m_db.execQuery(_T("SELECT clipOrder FROM Main ORDER BY clipOrder DESC LIMIT 1"));
  107. if(q.eof() == false)
  108. {
  109. double latestDate = q.getFloatField(_T("clipOrder"));
  110. latestDate += 1;
  111. Log(StrF(_T("Setting clipId: %d, order: %f"), id, latestDate));
  112. theApp.m_db.execDMLEx(_T("UPDATE Main SET clipOrder = %f where lID = %d;"), latestDate, id);
  113. theApp.RefreshClipOrder(id);
  114. }
  115. }
  116. CATCH_SQLITE_EXCEPTION
  117. bRet = TRUE;
  118. }
  119. CATCH_SQLITE_EXCEPTION
  120. Log(_T("End of MarkAsPastedThread"));
  121. UpdateTimeEvent.SetEvent();
  122. return bRet;
  123. }