QPasteWndThread.cpp 13 KB

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