MainFrmThread.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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(g_Opt.m_lAutoSendClientCount > 0)
  119. {
  120. m_sendToClientThread.FireSendToClient(pLocalClips);
  121. }
  122. }
  123. delete pLocalClips;
  124. }
  125. void CMainFrmThread::OnSaveRemoteClips()
  126. {
  127. LogSendRecieveInfo("---------Start of OnSaveRemoteClips");
  128. CClipList *pLocalClips = new CClipList();
  129. //Save the clips locally
  130. {
  131. ATL::CCritSecLock csLock(m_cs.m_sect);
  132. POSITION pos;
  133. CClip* pClip;
  134. pos = m_saveRemoteClips.GetHeadPosition();
  135. while(pos)
  136. {
  137. pClip = m_saveRemoteClips.GetNext(pos);
  138. pLocalClips->AddTail(pClip);
  139. }
  140. //pLocalClips now own, the clips
  141. m_saveRemoteClips.RemoveAll();
  142. }
  143. LogSendRecieveInfo("---------OnSaveRemoteClips - Before AddToDB");
  144. int count = pLocalClips->AddToDB(true);
  145. LogSendRecieveInfo("---------OnSaveRemoteClips - After AddToDB");
  146. //are we supposed to add this clip to the clipboard
  147. CClip *pLastClip = pLocalClips->GetTail();
  148. if(pLastClip && pLastClip->m_param1 == TRUE)
  149. {
  150. LogSendRecieveInfo("---------OnSaveRemoteClips - Before Posting msg to main thread to set clipboard");
  151. //set the clipboard on the main thread, i was having a problem with setting the clipboard on a thread
  152. //guess it needs to be set on the main thread
  153. //main window will clear this memory
  154. PostMessage(theApp.m_MainhWnd, WM_LOAD_ClIP_ON_CLIPBOARD, (LPARAM)pLastClip, 0);
  155. LogSendRecieveInfo("---------OnSaveRemoteClips - After Posting msg to main thread to set clipboard");
  156. pLocalClips->RemoveTail();
  157. }
  158. theApp.RefreshView();
  159. delete pLocalClips;
  160. LogSendRecieveInfo("---------End of OnSaveRemoteClips");
  161. }