MainFrmThread.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. CopyReasonEnum::CopyReason copyReason = CopyReasonEnum::COPY_TO_UNKOWN;
  93. //Save the clips locally
  94. {
  95. ATL::CCritSecLock csLock(m_cs.m_sect);
  96. POSITION pos;
  97. CClip* pClip;
  98. pos = m_saveClips.GetHeadPosition();
  99. while(pos)
  100. {
  101. pClip = m_saveClips.GetNext(pos);
  102. copyReason = pClip->m_copyReason;
  103. pLocalClips->AddTail(pClip);
  104. }
  105. //pLocalClips now own, the clips
  106. m_saveClips.RemoveAll();
  107. }
  108. Log(_T("SaveCopyClips Before AddToDb"));
  109. int count = pLocalClips->AddToDB(true);
  110. Log(StrF(_T("SaveCopyclips After AddToDb, Count: %d"), count));
  111. if(count > 0)
  112. {
  113. int Id = pLocalClips->GetTail()->m_id;
  114. Log(StrF(_T("SaveCopyclips After AddToDb, Id: %d Before OnCopyCopyCompleted"), Id));
  115. theApp.OnCopyCompleted(Id, count, copyReason);
  116. Log(StrF(_T("SaveCopyclips After AddToDb, Id: %d After OnCopyCopyCompleted"), Id));
  117. if (pLocalClips->GetTail()->m_copyReason == CopyReasonEnum::COPY_TO_GROUP &&
  118. CGetSetOptions::GetShowMsgWndOnCopyToGroup())
  119. {
  120. CString groupName;
  121. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT mText FROM Main WHERE lID = %d"), pLocalClips->GetTail()->m_parentId);
  122. if (q.eof() == false)
  123. {
  124. groupName = q.getStringField(0);
  125. }
  126. CString *pMsg = new CString();
  127. pMsg->Format(_T("Saved new clip \"%s\"\r\ndirectly to the group \"%s\""), pLocalClips->GetTail()->m_Desc.Left(35), groupName);
  128. theApp.m_pMainFrame->PostMessageW(WM_SHOW_MSG_WINDOW, (WPARAM) pMsg, pLocalClips->GetTail()->m_parentId);
  129. }
  130. if(g_Opt.m_lAutoSendClientCount > 0)
  131. {
  132. m_sendToClientThread.FireSendToClient(pLocalClips);
  133. }
  134. }
  135. delete pLocalClips;
  136. }
  137. void CMainFrmThread::OnSaveRemoteClips()
  138. {
  139. LogSendRecieveInfo("---------Start of OnSaveRemoteClips");
  140. CClipList *pLocalClips = new CClipList();
  141. //Save the clips locally
  142. {
  143. ATL::CCritSecLock csLock(m_cs.m_sect);
  144. POSITION pos;
  145. CClip* pClip;
  146. pos = m_saveRemoteClips.GetHeadPosition();
  147. while(pos)
  148. {
  149. pClip = m_saveRemoteClips.GetNext(pos);
  150. pLocalClips->AddTail(pClip);
  151. }
  152. //pLocalClips now own, the clips
  153. m_saveRemoteClips.RemoveAll();
  154. }
  155. LogSendRecieveInfo("---------OnSaveRemoteClips - Before AddToDB");
  156. int count = pLocalClips->AddToDB(true);
  157. LogSendRecieveInfo("---------OnSaveRemoteClips - After AddToDB");
  158. //are we supposed to add this clip to the clipboard
  159. CClip *pLastClip = pLocalClips->GetTail();
  160. if (CGetSetOptions::GetShowMsgWhenReceivingManualSentClip())
  161. {
  162. if (pLastClip && (pLastClip->m_param1 & REMOTE_CLIP_MANUAL_SEND))
  163. {
  164. CString *pMsg = new CString();
  165. //baloon message can only show 254 characters
  166. pMsg->Format(_T("Received remote clip\r\n\r\n%s"), pLastClip->m_Desc.Left(225));
  167. theApp.m_pMainFrame->PostMessageW(WM_SHOW_MSG_WINDOW, (WPARAM)pMsg, pLocalClips->GetTail()->m_parentId);
  168. }
  169. }
  170. if(pLastClip && (pLastClip->m_param1 & REMOTE_CLIP_ADD_TO_CLIPBOARD))
  171. {
  172. LogSendRecieveInfo("---------OnSaveRemoteClips - Before Posting msg to main thread to set clipboard");
  173. //set the clipboard on the main thread, i was having a problem with setting the clipboard on a thread
  174. //guess it needs to be set on the main thread
  175. //main window will clear this memory
  176. PostMessage(theApp.m_MainhWnd, WM_LOAD_ClIP_ON_CLIPBOARD, (LPARAM)pLastClip, 0);
  177. LogSendRecieveInfo("---------OnSaveRemoteClips - After Posting msg to main thread to set clipboard");
  178. pLocalClips->RemoveTail();
  179. }
  180. theApp.RefreshView();
  181. delete pLocalClips;
  182. LogSendRecieveInfo("---------End of OnSaveRemoteClips");
  183. }