MainFrmThread.cpp 5.1 KB

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