MainFrmThread.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include "stdafx.h"
  2. #include "MainFrmThread.h"
  3. #include "DatabaseUtilities.h"
  4. #include "Options.h"
  5. #include "Misc.h"
  6. #include "cp_main.h"
  7. CMainFrmThread::CMainFrmThread(void)
  8. {
  9. m_threadName = "CMainFrmThread";
  10. for(int eventEnum = 0; eventEnum < ECMAINFRMTHREADEVENTS_COUNT; eventEnum++)
  11. {
  12. AddEvent(eventEnum);
  13. }
  14. }
  15. CMainFrmThread::~CMainFrmThread(void)
  16. {
  17. }
  18. void CMainFrmThread::AddClipToSave(CClip *pClip)
  19. {
  20. ATL::CCritSecLock csLock(m_cs.m_sect);
  21. Log(_T("Adding clip to thread for save to db"));
  22. m_saveClips.AddTail(pClip);
  23. FireEvent(SAVE_CLIPS);
  24. }
  25. void CMainFrmThread::AddRemoteClipToSave(CClipList *pClipList)
  26. {
  27. ATL::CCritSecLock csLock(m_cs.m_sect);
  28. Log(_T("Adding REMOTE clip to thread for save to db"));
  29. POSITION pos = pClipList->GetHeadPosition();
  30. while(pos)
  31. {
  32. CClip *pClip = pClipList->GetNext(pos);
  33. m_saveRemoteClips.AddTail(pClip);
  34. }
  35. //local cliplist now owns the clip memory
  36. pClipList->RemoveAll();
  37. FireEvent(SAVE_REMOTE_CLIPS);
  38. }
  39. void CMainFrmThread::OnEvent(int eventId, void *param)
  40. {
  41. switch((eCMainFrmThreadEvents)eventId)
  42. {
  43. case DELETE_ENTRIES:
  44. OnDeleteEntries();
  45. break;
  46. case REMOVE_TEMP_FILES:
  47. OnRemoveTempFiles();
  48. break;
  49. case SAVE_CLIPS:
  50. OnSaveClips();
  51. break;
  52. case SAVE_REMOTE_CLIPS:
  53. OnSaveRemoteClips();
  54. break;
  55. case READ_DB_FILE:
  56. OnReadDbFile();
  57. break;
  58. }
  59. }
  60. //try and keep our db file in windows cache by randomly reading some data
  61. //not sure if this does what i think it does but looking into issues with slow access on large dbs
  62. void CMainFrmThread::OnReadDbFile()
  63. {
  64. double idle = IdleSeconds();
  65. if (idle < CGetSetOptions::ReadRandomFileIdleMin())
  66. {
  67. CString dbFile = CGetSetOptions::GetDBPath();
  68. __int64 dbSize = FileSize(dbFile);
  69. srand((UINT)time(NULL));
  70. int random = rand() % (dbSize - 1024) + 1;
  71. CFile f;
  72. if (f.Open(dbFile, CFile::modeRead | CFile::shareDenyNone))
  73. {
  74. f.Seek(random, 0);
  75. char data[1024];
  76. f.Read(&data, 1024);
  77. f.Close();
  78. }
  79. }
  80. }
  81. void CMainFrmThread::OnDeleteEntries()
  82. {
  83. RemoveOldEntries(true);
  84. }
  85. void CMainFrmThread::OnRemoveTempFiles()
  86. {
  87. DeleteDittoTempFiles(TRUE);
  88. }
  89. void CMainFrmThread::OnSaveClips()
  90. {
  91. CClipList *pLocalClips = new CClipList();
  92. //Save the clips locally
  93. {
  94. ATL::CCritSecLock csLock(m_cs.m_sect);
  95. POSITION pos;
  96. CClip* pClip;
  97. pos = m_saveClips.GetHeadPosition();
  98. while(pos)
  99. {
  100. pClip = m_saveClips.GetNext(pos);
  101. pLocalClips->AddTail(pClip);
  102. }
  103. //pLocalClips now own, the clips
  104. m_saveClips.RemoveAll();
  105. }
  106. Log(_T("SaveCopyClips Before AddToDb"));
  107. int count = pLocalClips->AddToDB(true);
  108. Log(StrF(_T("SaveCopyclips After AddToDb, Count: %d"), count));
  109. if(count > 0)
  110. {
  111. int Id = pLocalClips->GetTail()->m_id;
  112. Log(StrF(_T("SaveCopyclips After AddToDb, Id: %d Before OnCopyCopyCompleted"), Id));
  113. theApp.OnCopyCompleted(Id, count);
  114. Log(StrF(_T("SaveCopyclips After AddToDb, Id: %d After OnCopyCopyCompleted"), Id));
  115. if (pLocalClips->GetTail()->m_copyReason == CopyReasonEnum::COPY_TO_GROUP &&
  116. CGetSetOptions::GetShowMsgWndOnCopyToGroup())
  117. {
  118. CString groupName;
  119. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT mText FROM Main WHERE lID = %d"), pLocalClips->GetTail()->m_parentId);
  120. if (q.eof() == false)
  121. {
  122. groupName = q.getStringField(0);
  123. }
  124. CString *pMsg = new CString();
  125. pMsg->Format(_T("Saved new clip \"%s\"\r\ndirectly to the group \"%s\""), pLocalClips->GetTail()->m_Desc.Left(35), groupName);
  126. theApp.m_pMainFrame->PostMessageW(WM_SHOW_MSG_WINDOW, (WPARAM) pMsg, pLocalClips->GetTail()->m_parentId);
  127. }
  128. if(g_Opt.m_lAutoSendClientCount > 0)
  129. {
  130. m_sendToClientThread.FireSendToClient(pLocalClips);
  131. }
  132. }
  133. delete pLocalClips;
  134. }
  135. void CMainFrmThread::OnSaveRemoteClips()
  136. {
  137. LogSendRecieveInfo("---------Start of OnSaveRemoteClips");
  138. CClipList *pLocalClips = new CClipList();
  139. //Save the clips locally
  140. {
  141. ATL::CCritSecLock csLock(m_cs.m_sect);
  142. POSITION pos;
  143. CClip* pClip;
  144. pos = m_saveRemoteClips.GetHeadPosition();
  145. while(pos)
  146. {
  147. pClip = m_saveRemoteClips.GetNext(pos);
  148. pLocalClips->AddTail(pClip);
  149. }
  150. //pLocalClips now own, the clips
  151. m_saveRemoteClips.RemoveAll();
  152. }
  153. LogSendRecieveInfo("---------OnSaveRemoteClips - Before AddToDB");
  154. int count = pLocalClips->AddToDB(true);
  155. LogSendRecieveInfo("---------OnSaveRemoteClips - After AddToDB");
  156. //are we supposed to add this clip to the clipboard
  157. CClip *pLastClip = pLocalClips->GetTail();
  158. if (CGetSetOptions::GetShowMsgWhenReceivingManualSentClip())
  159. {
  160. if (pLastClip && (pLastClip->m_param1 & REMOTE_CLIP_MANUAL_SEND))
  161. {
  162. CString *pMsg = new CString();
  163. //baloon message can only show 254 characters
  164. pMsg->Format(_T("Received remote clip\r\n\r\n%s"), pLastClip->m_Desc.Left(225));
  165. theApp.m_pMainFrame->PostMessageW(WM_SHOW_MSG_WINDOW, (WPARAM)pMsg, pLocalClips->GetTail()->m_parentId);
  166. }
  167. }
  168. if(pLastClip && (pLastClip->m_param1 & REMOTE_CLIP_ADD_TO_CLIPBOARD))
  169. {
  170. LogSendRecieveInfo("---------OnSaveRemoteClips - Before Posting msg to main thread to set clipboard");
  171. //set the clipboard on the main thread, i was having a problem with setting the clipboard on a thread
  172. //guess it needs to be set on the main thread
  173. //main window will clear this memory
  174. PostMessage(theApp.m_MainhWnd, WM_LOAD_ClIP_ON_CLIPBOARD, (LPARAM)pLastClip, 0);
  175. LogSendRecieveInfo("---------OnSaveRemoteClips - After Posting msg to main thread to set clipboard");
  176. pLocalClips->RemoveTail();
  177. }
  178. theApp.RefreshView();
  179. delete pLocalClips;
  180. LogSendRecieveInfo("---------End of OnSaveRemoteClips");
  181. }