QPasteWndThread.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. #include "stdafx.h"
  2. #include "QPasteWndThread.h"
  3. #include "Misc.h"
  4. #include "Options.h"
  5. #include "QPasteWnd.h"
  6. #include "cp_main.h"
  7. #include <vector>
  8. #include <algorithm>
  9. CQPasteWndThread::CQPasteWndThread(void)
  10. {
  11. m_rowHeight = 0;
  12. m_threadName = "CQPasteWndThread";
  13. m_waitTimeout = ONE_HOUR * 12;
  14. m_SearchingEvent = CreateEvent(NULL, TRUE, FALSE, _T(""));
  15. for(int eventEnum = 0; eventEnum < ECQPASTEWNDTHREADEVENTS_COUNT; eventEnum++)
  16. {
  17. AddEvent(eventEnum);
  18. }
  19. }
  20. CQPasteWndThread::~CQPasteWndThread(void)
  21. {
  22. CloseHandle(m_SearchingEvent);
  23. }
  24. void CQPasteWndThread::OnTimeOut(void *param)
  25. {
  26. }
  27. void CQPasteWndThread::OnEvent(int eventId, void *param)
  28. {
  29. DWORD startTick = GetTickCount();
  30. Log(StrF(_T("Start of OnEvent, eventId: %s"), EnumName((eCQPasteWndThreadEvents)eventId)));
  31. switch((eCQPasteWndThreadEvents)eventId)
  32. {
  33. case DO_SET_LIST_COUNT:
  34. OnSetListCount(param);
  35. break;
  36. case LOAD_ACCELERATORS:
  37. OnLoadAccelerators(param);
  38. break;
  39. case UNLOAD_ACCELERATORS:
  40. OnUnloadAccelerators(param);
  41. break;
  42. case LOAD_ITEMS:
  43. OnLoadItems(param);
  44. break;
  45. case LOAD_EXTRA_DATA:
  46. OnLoadExtraData(param);
  47. break;
  48. }
  49. DWORD length = GetTickCount() - startTick;
  50. Log(StrF(_T("End of OnEvent, eventId: %s, Time: %d(ms)"), EnumName((eCQPasteWndThreadEvents)eventId), length));
  51. }
  52. void CQPasteWndThread::OnSetListCount(void *param)
  53. {
  54. CQPasteWnd *pasteWnd = (CQPasteWnd*)param;
  55. static CEvent UpdateTimeEvent(TRUE, TRUE, _T("Ditto_Update_Clip_Time"), NULL);
  56. //If we pasted then wait for the time on the pasted event to be updated before we query the db
  57. DWORD dRet = WaitForSingleObject(UpdateTimeEvent, 2000);
  58. ResetEvent(m_SearchingEvent);
  59. long lTick = GetTickCount();
  60. CString CountSQL;
  61. {
  62. ATL::CCritSecLock csLock(pasteWnd->m_CritSection.m_sect);
  63. CountSQL = pasteWnd->m_CountSQL;
  64. }
  65. long lRecordCount = 0;
  66. try
  67. {
  68. lRecordCount = theApp.m_db.execScalar(CountSQL);
  69. ::PostMessage(pasteWnd->m_hWnd, NM_SET_LIST_COUNT, lRecordCount, 0);
  70. }
  71. CATCH_SQLITE_EXCEPTION
  72. SetEvent(m_SearchingEvent);
  73. Log(StrF(_T("Set list count = %d, time = %d"), lRecordCount, GetTickCount() - lTick));
  74. }
  75. void CQPasteWndThread::OnLoadItems(void *param)
  76. {
  77. CQPasteWnd *pasteWnd = (CQPasteWnd*)param;
  78. ResetEvent(m_SearchingEvent);
  79. while(true)
  80. {
  81. long startTick = GetTickCount();
  82. int loadItemsIndex = 0;
  83. int loadItemsCount = 0;
  84. int loadCount = 0;
  85. CString localSql;
  86. bool clearFirstLoadItem = false;
  87. bool firstLoad = false;
  88. {
  89. ATL::CCritSecLock csLock(pasteWnd->m_CritSection.m_sect);
  90. if(pasteWnd->m_loadItems.size() > 0)
  91. {
  92. firstLoad = (pasteWnd->m_loadItems.begin()->x == -1);
  93. loadItemsIndex = max(pasteWnd->m_loadItems.begin()->x, 0);
  94. loadItemsCount = pasteWnd->m_loadItems.begin()->y - pasteWnd->m_loadItems.begin()->x;
  95. localSql = pasteWnd->m_SQL;
  96. pasteWnd->m_bStopQuery = false;
  97. clearFirstLoadItem = true;
  98. }
  99. }
  100. if(clearFirstLoadItem)
  101. {
  102. try
  103. {
  104. Log(StrF(_T("Load Items start = %d, count = %d, list size: %d"), loadItemsIndex, loadItemsCount, pasteWnd->m_listItems.size()));
  105. int pos = loadItemsIndex;
  106. CString limit;
  107. limit.Format(_T(" LIMIT %d OFFSET %d"), loadItemsCount, loadItemsIndex);
  108. localSql += limit;
  109. CMainTable table;
  110. CppSQLite3Query q = theApp.m_db.execQuery(localSql);
  111. while(!q.eof())
  112. {
  113. pasteWnd->FillMainTable(table, q);
  114. int updateIndex = -1;
  115. {
  116. ATL::CCritSecLock csLock(pasteWnd->m_CritSection.m_sect);
  117. if (pos < pasteWnd->m_listItems.size())
  118. {
  119. pasteWnd->m_listItems[pos] = table;
  120. updateIndex = pos;
  121. //Log(StrF(_T("updating list pos = %d, id: %d, size: %d"), pos, table.m_lID, pasteWnd->m_listItems.size() - 1));
  122. }
  123. else if (pos == pasteWnd->m_listItems.size())
  124. {
  125. pasteWnd->m_listItems.push_back(table);
  126. updateIndex = (int)pasteWnd->m_listItems.size() - 1;
  127. //Log(StrF(_T("adding (same size) list pos = %d, id: %d, size: %d"), pasteWnd->m_listItems.size()-1, table.m_lID, pasteWnd->m_listItems.size() - 1));
  128. }
  129. else if (pos > pasteWnd->m_listItems.size())
  130. {
  131. for (int toAdd = (int)pasteWnd->m_listItems.size()-1; toAdd < pos - 1; toAdd++)
  132. {
  133. CMainTable empty;
  134. empty.m_lID = -1;
  135. pasteWnd->m_listItems.push_back(empty);
  136. //Log(StrF(_T("adding dummy row size: %d"), pasteWnd->m_listItems.size()-1));
  137. }
  138. pasteWnd->m_listItems.push_back(table);
  139. updateIndex = (int)pasteWnd->m_listItems.size() - 1;
  140. //Log(StrF(_T("adding list pos = %d, id: %d, size: %d"), pasteWnd->m_listItems.size()-1, table.m_lID, pasteWnd->m_listItems.size() - 1));
  141. }
  142. }
  143. if(pasteWnd->m_bStopQuery)
  144. {
  145. Log(StrF(_T("StopQuery called exiting filling cache count = %d"), loadItemsIndex));
  146. break;
  147. }
  148. q.nextRow();
  149. if(firstLoad == false)
  150. {
  151. /*if (updateIndex != loadItemsIndex)
  152. {
  153. Log(StrF(_T("index difference old: %d, new: %d"), loadItemsIndex, updateIndex));
  154. }*/
  155. ::PostMessage(pasteWnd->m_hWnd, NM_REFRESH_ROW, table.m_lID, updateIndex);
  156. }
  157. loadItemsIndex++;
  158. loadCount++;
  159. pos++;
  160. }
  161. DWORD loadCount = GetTickCount() - startTick;
  162. DWORD countCountStart = GetTickCount();
  163. DWORD countCount = 0;
  164. DWORD acceleratorCount = 0;
  165. if(firstLoad)
  166. {
  167. ::PostMessage(pasteWnd->m_hWnd, NM_REFRESH_ROW, -2, 0);
  168. //allow the next thread message to process, this should be the message to set the list count
  169. OnSetListCount(param);
  170. countCount = GetTickCount() - countCountStart;
  171. DWORD acceleratorCountStart = GetTickCount();
  172. OnLoadAccelerators(param);
  173. acceleratorCount = GetTickCount() - acceleratorCountStart;
  174. }
  175. else
  176. {
  177. ::PostMessage(pasteWnd->m_hWnd, NM_REFRESH_ROW, -1, 0);
  178. }
  179. if(clearFirstLoadItem)
  180. {
  181. ATL::CCritSecLock csLock(pasteWnd->m_CritSection.m_sect);
  182. pasteWnd->m_loadItems.erase(pasteWnd->m_loadItems.begin());
  183. }
  184. Log(StrF(_T("Load items End count = %d, Total Time = %d, LoadItems: %d, Count: %d, Accel: %d"), loadCount, GetTickCount() - startTick, loadCount, countCount, acceleratorCount));
  185. }
  186. catch (CppSQLite3Exception& e) \
  187. { \
  188. Log(StrF(_T("ONLoadItems - SQLITE Exception %d - %s"), e.errorCode(), e.errorMessage())); \
  189. ASSERT(FALSE); \
  190. break;
  191. }
  192. }
  193. else
  194. {
  195. break;
  196. }
  197. }
  198. SetEvent(m_SearchingEvent);
  199. }
  200. void ReduceMapItems(CF_DibTypeMap &mapItem, CCriticalSection &critSection, CString mapName)
  201. {
  202. ATL::CCritSecLock csLock(critSection.m_sect);
  203. int maxSize = 50;
  204. int reduceToSize = 30;
  205. if (mapItem.size() > maxSize)
  206. {
  207. //create a vector so we can sort and keep the last x number of events
  208. vector<INT64> counterArray;
  209. for (CF_DibTypeMap::iterator iterDib = mapItem.begin(); iterDib != mapItem.end(); iterDib++)
  210. {
  211. counterArray.push_back(iterDib->second.m_counter);
  212. }
  213. std::sort(counterArray.begin(), counterArray.end());
  214. counterArray.erase(counterArray.begin(), counterArray.end() - reduceToSize);
  215. //remove the oldest x number if bitmaps
  216. for (CF_DibTypeMap::iterator iterDib = mapItem.begin(); iterDib != mapItem.end();)
  217. {
  218. if (std::binary_search(counterArray.begin(), counterArray.end(), iterDib->second.m_counter) == false)
  219. {
  220. Log(StrF(_T("reduced size of %s cache, Id: %d, Row: %d"), mapName, iterDib->second.m_parentId, iterDib->second.m_clipRow));
  221. mapItem.erase(iterDib++);
  222. }
  223. else
  224. {
  225. ++iterDib;
  226. }
  227. }
  228. Log(StrF(_T("reduced size of %s cache, count: %d"), mapName, mapItem.size()));
  229. }
  230. }
  231. void CQPasteWndThread::OnLoadExtraData(void *param)
  232. {
  233. ResetEvent(m_SearchingEvent);
  234. CQPasteWnd *pasteWnd = (CQPasteWnd*)param;
  235. Log(_T("Start of load extra data, Bitmaps/rtf"));
  236. std::list<CClipFormatQListCtrl> localFormats;
  237. {
  238. ATL::CCritSecLock csLock(pasteWnd->m_CritSection.m_sect);
  239. for (std::list<CClipFormatQListCtrl>::iterator it = pasteWnd->m_ExtraDataLoadItems.begin(); it != pasteWnd->m_ExtraDataLoadItems.end(); it++)
  240. {
  241. localFormats.push_back(*it);
  242. }
  243. pasteWnd->m_ExtraDataLoadItems.clear();
  244. }
  245. for (std::list<CClipFormatQListCtrl>::iterator it = localFormats.begin(); it != localFormats.end(); it++)
  246. {
  247. bool loadClip = true;
  248. if (it->m_cfType == CF_DIB)
  249. {
  250. ATL::CCritSecLock csLock(pasteWnd->m_CritSection.m_sect);
  251. CF_DibTypeMap::iterator iterDib = pasteWnd->m_cf_dibCache.find(it->m_parentId);
  252. if (iterDib != pasteWnd->m_cf_dibCache.end())
  253. {
  254. loadClip = false;
  255. }
  256. else
  257. {
  258. CF_NoDibTypeMap::iterator iterNoDib = pasteWnd->m_cf_NO_dibCache.find(it->m_parentId);
  259. if (iterNoDib != pasteWnd->m_cf_NO_dibCache.end())
  260. {
  261. loadClip = false;
  262. }
  263. }
  264. }
  265. else if (it->m_cfType == theApp.m_RTFFormat)
  266. {
  267. ATL::CCritSecLock csLock(pasteWnd->m_CritSection.m_sect);
  268. CF_DibTypeMap::iterator iterDib = pasteWnd->m_cf_rtfCache.find(it->m_parentId);
  269. if (iterDib != pasteWnd->m_cf_rtfCache.end())
  270. {
  271. loadClip = false;
  272. }
  273. else
  274. {
  275. CF_NoDibTypeMap::iterator iterNoRtf = pasteWnd->m_cf_NO_rtfCache.find(it->m_parentId);
  276. if (iterNoRtf != pasteWnd->m_cf_NO_rtfCache.end())
  277. {
  278. loadClip = false;
  279. }
  280. }
  281. }
  282. if (loadClip)
  283. {
  284. DWORD startLoadClipData = GetTickCount();
  285. BOOL foundClipData = theApp.GetClipData(it->m_parentId, *it);
  286. if (foundClipData == false &&
  287. it->m_cfType == CF_DIB)
  288. {
  289. it->Free();
  290. it->m_cfType = theApp.m_PNG_Format;
  291. foundClipData = theApp.GetClipData(it->m_parentId, *it);
  292. }
  293. if (foundClipData)
  294. {
  295. DWORD timeTook = GetTickCount() - startLoadClipData;
  296. if (timeTook > 20)
  297. {
  298. Log(StrF(_T("GetClipData for clip %d, took: %d"), it->m_parentId, timeTook));
  299. }
  300. if (it->m_cfType == CF_DIB ||
  301. it->m_cfType == theApp.m_PNG_Format)
  302. {
  303. DWORD startConvertImage = GetTickCount();
  304. it->GetDibFittingToHeight(CDC::FromHandle(GetDC(NULL)), m_rowHeight);
  305. DWORD timeTook = GetTickCount() - startConvertImage;
  306. if (timeTook > 20)
  307. {
  308. Log(StrF(_T("GetDibFittingToHeight for clip %d, took: %d"), it->m_parentId, GetTickCount() - startConvertImage));
  309. }
  310. ATL::CCritSecLock csLock(pasteWnd->m_CritSection.m_sect);
  311. pasteWnd->m_cf_dibCache[it->m_parentId] = *it;
  312. //the cache now owns the format data, set it to delete the data in the destructor
  313. pasteWnd->m_cf_dibCache[it->m_parentId].m_autoDeleteData = true;
  314. Log(StrF(_T("Loaded, extra data for clipId: %d, Row: %d image cache count: %d"), it->m_parentId, it->m_clipRow, pasteWnd->m_cf_dibCache.size()));
  315. }
  316. else if (it->m_cfType == theApp.m_RTFFormat)
  317. {
  318. ATL::CCritSecLock csLock(pasteWnd->m_CritSection.m_sect);
  319. pasteWnd->m_cf_rtfCache[it->m_parentId] = *it;
  320. it->m_autoDeleteData = false;
  321. //the cache now owns the format data, set it to delete the data in the destructor
  322. pasteWnd->m_cf_rtfCache[it->m_parentId].m_autoDeleteData = true;
  323. Log(StrF(_T("Loaded, extra data for clip %d, rtf cache count: %d"), it->m_parentId, pasteWnd->m_cf_rtfCache.size()));
  324. }
  325. ::PostMessage(pasteWnd->m_hWnd, NM_REFRESH_ROW, it->m_parentId, it->m_clipRow);
  326. }
  327. else
  328. {
  329. ATL::CCritSecLock csLock(pasteWnd->m_CritSection.m_sect);
  330. if (it->m_cfType == CF_DIB ||
  331. it->m_cfType == theApp.m_PNG_Format)
  332. {
  333. pasteWnd->m_cf_NO_dibCache[it->m_parentId] = true;
  334. }
  335. else if (it->m_cfType == theApp.m_RTFFormat)
  336. {
  337. pasteWnd->m_cf_NO_rtfCache[it->m_parentId] = true;
  338. }
  339. }
  340. }
  341. if (it->m_cfType == CF_DIB)
  342. {
  343. ReduceMapItems(pasteWnd->m_cf_dibCache, pasteWnd->m_CritSection, _T("image"));
  344. }
  345. else if (it->m_cfType == theApp.m_RTFFormat)
  346. {
  347. ReduceMapItems(pasteWnd->m_cf_rtfCache, pasteWnd->m_CritSection, _T("rtf"));
  348. }
  349. }
  350. SetEvent(m_SearchingEvent);
  351. Log(_T("End of load extra data, Bitmaps/rtf"));
  352. }
  353. void CQPasteWndThread::OnLoadAccelerators(void *param)
  354. {
  355. CQPasteWnd *pasteWnd = (CQPasteWnd*)param;
  356. pasteWnd->m_lstHeader.DestroyAndCreateAccelerator(TRUE, theApp.m_db);
  357. }
  358. void CQPasteWndThread::OnUnloadAccelerators(void *param)
  359. {
  360. CQPasteWnd *pasteWnd = (CQPasteWnd*)param;
  361. pasteWnd->m_lstHeader.DestroyAndCreateAccelerator(FALSE, theApp.m_db);
  362. }
  363. CString CQPasteWndThread::EnumName(eCQPasteWndThreadEvents e)
  364. {
  365. switch(e)
  366. {
  367. case DO_SET_LIST_COUNT:
  368. return _T("Load List Count");
  369. case LOAD_ACCELERATORS:
  370. return _T("Load Accelerators");
  371. case UNLOAD_ACCELERATORS:
  372. return _T("Unload Accelerators");
  373. case LOAD_ITEMS:
  374. return _T("Load clips");
  375. case LOAD_EXTRA_DATA:
  376. return _T("Load Extra Data (rtf/bitmaps)");
  377. }
  378. return _T("");
  379. }