Clip.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795
  1. // ProcessCopy.cpp: implementation of the CProcessCopy class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "CP_Main.h"
  6. #include "Clip.h"
  7. #include "DatabaseUtilities.h"
  8. #include "Crc32Dynamic.h"
  9. #include "sqlite\CppSQLite3.h"
  10. #include "shared/TextConvert.h"
  11. #include "zlib/zlib.h"
  12. #include "Misc.h"
  13. #include "Md5.h"
  14. #include <Mmsystem.h>
  15. #include "Path.h"
  16. #ifdef _DEBUG
  17. #undef THIS_FILE
  18. static char THIS_FILE[]=__FILE__;
  19. #define new DEBUG_NEW
  20. #endif
  21. /*----------------------------------------------------------------------------*\
  22. COleDataObjectEx
  23. \*----------------------------------------------------------------------------*/
  24. HGLOBAL COleDataObjectEx::GetGlobalData(CLIPFORMAT cfFormat, LPFORMATETC lpFormatEtc)
  25. {
  26. HGLOBAL hGlobal = COleDataObject::GetGlobalData(cfFormat, lpFormatEtc);
  27. if(hGlobal)
  28. {
  29. if(!::IsValid(hGlobal))
  30. {
  31. Log( StrF(
  32. _T("COleDataObjectEx::GetGlobalData(\"%s\"): ERROR: Invalid (NULL) data returned."),
  33. GetFormatName(cfFormat) ) );
  34. ::GlobalFree( hGlobal );
  35. hGlobal = NULL;
  36. }
  37. return hGlobal;
  38. }
  39. // The data isn't in global memory, so try getting an IStream interface to it.
  40. STGMEDIUM stg;
  41. if(!GetData(cfFormat, &stg))
  42. {
  43. return 0;
  44. }
  45. switch(stg.tymed)
  46. {
  47. case TYMED_HGLOBAL:
  48. hGlobal = stg.hGlobal;
  49. break;
  50. case TYMED_ISTREAM:
  51. {
  52. UINT uDataSize;
  53. LARGE_INTEGER li;
  54. ULARGE_INTEGER uli;
  55. li.HighPart = li.LowPart = 0;
  56. if ( SUCCEEDED( stg.pstm->Seek ( li, STREAM_SEEK_END, &uli )))
  57. {
  58. hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, uli.LowPart );
  59. void* pv = GlobalLock(hGlobal);
  60. stg.pstm->Seek(li, STREAM_SEEK_SET, NULL);
  61. HRESULT result = stg.pstm->Read(pv, uli.LowPart, (PULONG)&uDataSize);
  62. GlobalUnlock(hGlobal);
  63. if( FAILED(result) )
  64. hGlobal = GlobalFree(hGlobal);
  65. }
  66. break; // case TYMED_ISTREAM
  67. }
  68. } // end switch
  69. ReleaseStgMedium(&stg);
  70. if(hGlobal && !::IsValid(hGlobal))
  71. {
  72. Log( StrF(
  73. _T("COleDataObjectEx::GetGlobalData(\"%s\"): ERROR: Invalid (NULL) data returned."),
  74. GetFormatName(cfFormat)));
  75. ::GlobalFree(hGlobal);
  76. hGlobal = NULL;
  77. }
  78. return hGlobal;
  79. }
  80. /*----------------------------------------------------------------------------*\
  81. CClipFormat - holds the data of one clip format.
  82. \*----------------------------------------------------------------------------*/
  83. CClipFormat::CClipFormat(CLIPFORMAT cfType, HGLOBAL hgData, int parentId)
  84. {
  85. m_cfType = cfType;
  86. m_hgData = hgData;
  87. m_autoDeleteData = true;
  88. m_parentId = parentId;
  89. }
  90. CClipFormat::~CClipFormat()
  91. {
  92. Free();
  93. }
  94. void CClipFormat::Clear()
  95. {
  96. m_cfType = 0;
  97. m_hgData = 0;
  98. m_dataId = -1;
  99. m_parentId = -1;
  100. }
  101. void CClipFormat::Free()
  102. {
  103. if(m_autoDeleteData)
  104. {
  105. if(m_hgData)
  106. {
  107. m_hgData = ::GlobalFree( m_hgData );
  108. m_hgData = NULL;
  109. }
  110. }
  111. }
  112. /*----------------------------------------------------------------------------*\
  113. CClipFormats - holds an array of CClipFormat
  114. \*----------------------------------------------------------------------------*/
  115. // returns a pointer to the CClipFormat in this array which matches the given type
  116. // or NULL if that type doesn't exist in this array.
  117. CClipFormat* CClipFormats::FindFormat(UINT cfType)
  118. {
  119. CClipFormat* pCF;
  120. INT_PTR count = GetSize();
  121. for(int i=0; i < count; i++)
  122. {
  123. pCF = &ElementAt(i);
  124. if(pCF->m_cfType == cfType)
  125. return pCF;
  126. }
  127. return NULL;
  128. }
  129. /*----------------------------------------------------------------------------*\
  130. CClip - holds multiple CClipFormats and CopyClipboard() statistics
  131. \*----------------------------------------------------------------------------*/
  132. DWORD CClip::m_LastAddedCRC = 0;
  133. int CClip::m_lastAddedID = -1;
  134. CClip::CClip() :
  135. m_id(0),
  136. m_CRC(0),
  137. m_parentId(-1),
  138. m_dontAutoDelete(FALSE),
  139. m_shortCut(0),
  140. m_bIsGroup(FALSE),
  141. m_param1(0),
  142. m_clipOrder(0),
  143. m_stickyClipOrder(INVALID_STICKY),
  144. m_stickyClipGroupOrder(INVALID_STICKY),
  145. m_clipGroupOrder(0),
  146. m_globalShortCut(FALSE),
  147. m_moveToGroupShortCut(0),
  148. m_globalMoveToGroupShortCut(FALSE)
  149. {
  150. m_copyReason = CopyReasonEnum::COPY_TO_UNKOWN;
  151. }
  152. CClip::~CClip()
  153. {
  154. EmptyFormats();
  155. }
  156. void CClip::Clear()
  157. {
  158. m_id = -1;
  159. m_Time = 0;
  160. m_Desc = "";
  161. m_CRC = 0;
  162. m_parentId = -1;
  163. m_dontAutoDelete = FALSE;
  164. m_shortCut = 0;
  165. m_bIsGroup = FALSE;
  166. m_csQuickPaste = "";
  167. m_param1 = 0;
  168. m_globalShortCut = FALSE;
  169. m_moveToGroupShortCut = 0;
  170. m_globalMoveToGroupShortCut = 0;
  171. EmptyFormats();
  172. }
  173. const CClip& CClip::operator=(const CClip &clip)
  174. {
  175. const CClipFormat* pCF;
  176. m_id = clip.m_id;
  177. m_Time = clip.m_Time;
  178. m_lastPasteDate = clip.m_lastPasteDate;
  179. m_CRC = clip.m_CRC;
  180. m_parentId = clip.m_parentId;
  181. m_dontAutoDelete = clip.m_dontAutoDelete;
  182. m_shortCut = clip.m_shortCut;
  183. m_bIsGroup = clip.m_bIsGroup;
  184. m_csQuickPaste = clip.m_csQuickPaste;
  185. m_moveToGroupShortCut = clip.m_moveToGroupShortCut;
  186. m_globalMoveToGroupShortCut = clip.m_globalMoveToGroupShortCut;
  187. INT_PTR nCount = clip.m_Formats.GetSize();
  188. for(int i = 0; i < nCount; i++)
  189. {
  190. pCF = &clip.m_Formats.GetData()[i];
  191. LPVOID pvData = GlobalLock(pCF->m_hgData);
  192. if(pvData)
  193. {
  194. AddFormat(pCF->m_cfType, pvData, (UINT)GlobalSize(pCF->m_hgData));
  195. }
  196. GlobalUnlock(pCF->m_hgData);
  197. }
  198. //Set this after since in could get the wrong description in AddFormat
  199. m_Desc = clip.m_Desc;
  200. return *this;
  201. }
  202. void CClip::EmptyFormats()
  203. {
  204. // free global memory in m_Formats
  205. for(INT_PTR i = m_Formats.GetSize()-1; i >= 0; i--)
  206. {
  207. m_Formats[i].Free();
  208. m_Formats.RemoveAt(i);
  209. }
  210. }
  211. // Adds a new Format to this Clip by copying the given data.
  212. bool CClip::AddFormat(CLIPFORMAT cfType, void* pData, UINT nLen, bool setDesc)
  213. {
  214. ASSERT(pData && nLen);
  215. HGLOBAL hGlobal = ::NewGlobalP(pData, nLen);
  216. ASSERT(hGlobal);
  217. // update the Clip statistics
  218. m_Time = m_Time.GetCurrentTime();
  219. if (setDesc)
  220. {
  221. if (cfType != CF_UNICODETEXT || !SetDescFromText(hGlobal, true))
  222. SetDescFromType();
  223. }
  224. CClipFormat format(cfType,hGlobal);
  225. CClipFormat *pFormat;
  226. pFormat = m_Formats.FindFormat(cfType);
  227. // if the format type already exists as part of this clip, replace the data
  228. if(pFormat)
  229. {
  230. pFormat->Free();
  231. pFormat->m_hgData = format.m_hgData;
  232. }
  233. else
  234. {
  235. m_Formats.Add(format);
  236. }
  237. format.m_hgData = 0; // now owned by m_Formats
  238. return true;
  239. }
  240. // Fills this CClip with the contents of the clipboard.
  241. int CClip::LoadFromClipboard(CClipTypes* pClipTypes, bool checkClipboardIgnore, CString activeApp)
  242. {
  243. COleDataObjectEx oleData;
  244. CClipTypes defaultTypes;
  245. CClipTypes* pTypes = pClipTypes;
  246. // m_Formats should be empty when this is called.
  247. ASSERT(m_Formats.GetSize() == 0);
  248. // If the data is supposed to be private, then return
  249. if(::IsClipboardFormatAvailable(theApp.m_cfIgnoreClipboard))
  250. {
  251. Log(_T("Clipboard ignore type is on the clipboard, skipping this clipboard change"));
  252. return FALSE;
  253. }
  254. //If we are saving a multi paste then delay us connecting to the clipboard
  255. //to allow the ctrl-v to do a paste
  256. if(::IsClipboardFormatAvailable(theApp.m_cfDelaySavingData))
  257. {
  258. Log(_T("Delay clipboard type is on the clipboard, delaying 1500 ms to allow ctrl-v to work"));
  259. Sleep(1500);
  260. }
  261. //Attach to the clipboard
  262. if(!oleData.AttachClipboard())
  263. {
  264. Log(_T("failed to attache to clipboard, skipping this clipboard change"));
  265. ASSERT(0); // does this ever happen?
  266. return FALSE;
  267. }
  268. oleData.EnsureClipboardObject();
  269. // if no types were given, get only the first (most important) type.
  270. // (subsequent types could be synthetic due to automatic type conversions)
  271. if(pTypes == NULL || pTypes->GetSize() == 0)
  272. {
  273. ASSERT(0); // this feature is not currently used... it is an error if it is.
  274. FORMATETC formatEtc;
  275. oleData.BeginEnumFormats();
  276. oleData.GetNextFormat(&formatEtc);
  277. defaultTypes.Add(formatEtc.cfFormat);
  278. pTypes = &defaultTypes;
  279. }
  280. m_Desc = "[Ditto Error] BAD DESCRIPTION";
  281. // Get Description String
  282. // NOTE: We make sure that the description always corresponds to the
  283. // data saved by using the exact same globalmem instance as the source
  284. // for both... i.e. we only fetch the description format type once.
  285. CClipFormat cfDesc;
  286. bool bIsDescSet = false;
  287. cfDesc.m_cfType = CF_UNICODETEXT;
  288. if(oleData.IsDataAvailable(cfDesc.m_cfType))
  289. {
  290. for (int i = 0; i < 10; i++)
  291. {
  292. cfDesc.m_hgData = oleData.GetGlobalData(cfDesc.m_cfType);
  293. if (cfDesc.m_hgData == NULL)
  294. {
  295. Log(StrF(_T("Tried to set description from cf_unicode, data is NULL, try: %d"), i+1));
  296. }
  297. else
  298. {
  299. break;
  300. }
  301. Sleep(10);
  302. }
  303. bIsDescSet = SetDescFromText(cfDesc.m_hgData, true);
  304. if (activeApp != _T(""))
  305. {
  306. TCHAR* text = (TCHAR *)GlobalLock(cfDesc.m_hgData);
  307. if (text != NULL)
  308. {
  309. std::wstring stringData(text);
  310. GlobalUnlock(cfDesc.m_hgData);
  311. if (g_Opt.m_regexHelper.TextMatchFilters(activeApp, stringData))
  312. {
  313. return -1;
  314. }
  315. }
  316. else
  317. {
  318. GlobalUnlock(cfDesc.m_hgData);
  319. }
  320. }
  321. Log(StrF(_T("Tried to set description from cf_unicode text, Set: %d, Desc: [%s]"), bIsDescSet, m_Desc.Left(30)));
  322. }
  323. if(bIsDescSet == false)
  324. {
  325. cfDesc.m_cfType = CF_TEXT;
  326. if(oleData.IsDataAvailable(cfDesc.m_cfType))
  327. {
  328. for (int i = 0; i < 10; i++)
  329. {
  330. cfDesc.m_hgData = oleData.GetGlobalData(cfDesc.m_cfType);
  331. if (cfDesc.m_hgData == NULL)
  332. {
  333. Log(StrF(_T("Tried to set description from cf_text, data is NULL, try: %d"), i + 1));
  334. }
  335. else
  336. {
  337. break;
  338. }
  339. Sleep(10);
  340. }
  341. bIsDescSet = SetDescFromText(cfDesc.m_hgData, false);
  342. Log(StrF(_T("Tried to set description from cf_text text, Set: %d, Desc: [%s]"), bIsDescSet, m_Desc.Left(30)));
  343. }
  344. }
  345. INT_PTR nSize;
  346. CClipFormat cf;
  347. INT_PTR numTypes = pTypes->GetSize();
  348. Log(StrF(_T("Begin enumerating over supported types, Count: %d"), numTypes));
  349. for(int i = 0; i < numTypes; i++)
  350. {
  351. cf.m_cfType = pTypes->ElementAt(i);
  352. if (cf.m_cfType == CF_DIB &&
  353. g_Opt.m_excludeCF_DIBInExcel &&
  354. activeApp.MakeLower() == _T("excel.exe"))
  355. {
  356. continue;
  357. }
  358. BOOL bSuccess = false;
  359. Log(StrF(_T("Begin try and load type %s"), GetFormatName(cf.m_cfType)));
  360. // is this the description we already fetched?
  361. if(cf.m_cfType == cfDesc.m_cfType)
  362. {
  363. cf = cfDesc;
  364. cfDesc.m_hgData = 0; // cf owns it now (to go into m_Formats)
  365. }
  366. else if(!oleData.IsDataAvailable(cf.m_cfType))
  367. {
  368. Log(StrF(_T("End of load - Data is not available for type %s"), GetFormatName(cf.m_cfType)));
  369. continue;
  370. }
  371. else
  372. {
  373. for (int i = 0; i < 2; i++)
  374. {
  375. cf.m_hgData = oleData.GetGlobalData(cf.m_cfType);
  376. if (cf.m_hgData != NULL)
  377. {
  378. break;
  379. }
  380. Log(StrF(_T("Tried to get data for type: %s, data is NULL, try: %d"), GetFormatName(cf.m_cfType), i + 1));
  381. Sleep(5);
  382. }
  383. }
  384. if(cf.m_hgData)
  385. {
  386. nSize = GlobalSize(cf.m_hgData);
  387. if(nSize > 0)
  388. {
  389. if(g_Opt.m_lMaxClipSizeInBytes > 0 && (int)nSize > g_Opt.m_lMaxClipSizeInBytes)
  390. {
  391. CString cs;
  392. cs.Format(_T("Maximum clip size reached max size = %d, clip size = %d"), g_Opt.m_lMaxClipSizeInBytes, nSize);
  393. Log(cs);
  394. oleData.Release();
  395. return -1;
  396. }
  397. ASSERT(::IsValid(cf.m_hgData));
  398. m_Formats.Add(cf);
  399. bSuccess = true;
  400. }
  401. else
  402. {
  403. ASSERT(FALSE); // a valid GlobalMem with 0 size is strange
  404. cf.Free();
  405. Log(StrF(_T("Data length is 0 for type %s"), GetFormatName(cf.m_cfType)));
  406. }
  407. cf.m_hgData = 0; // m_Formats owns it now
  408. }
  409. Log(StrF(_T("End of load - type %s, Success: %d"), GetFormatName(cf.m_cfType), bSuccess));
  410. }
  411. Log(StrF(_T("End enumerating over supported types, Count: %d"), numTypes));
  412. m_Time = CTime::GetCurrentTime();
  413. if(!bIsDescSet)
  414. {
  415. SetDescFromType();
  416. Log(StrF(_T("Setting description from type, Desc: [%s]"), m_Desc.Left(30)));
  417. }
  418. // if the description was in a type that is not supported,
  419. //we have to free it since it wasn't added to m_Formats
  420. if(cfDesc.m_hgData)
  421. {
  422. cfDesc.Free();
  423. }
  424. oleData.Release();
  425. if (!bIsDescSet &&
  426. this->m_Desc != _T(""))
  427. {
  428. std::wstring stringData(this->m_Desc);
  429. if (g_Opt.m_regexHelper.TextMatchFilters(activeApp, stringData))
  430. {
  431. return -1;
  432. }
  433. }
  434. if(m_Formats.GetSize() == 0)
  435. {
  436. Log(_T("No clip types were in supported types array"));
  437. return FALSE;
  438. }
  439. return TRUE;
  440. }
  441. bool CClip::SetDescFromText(HGLOBAL hgData, bool unicode)
  442. {
  443. if(hgData == 0)
  444. return false;
  445. bool bRet = false;
  446. INT_PTR bufLen = 0;
  447. if(unicode)
  448. {
  449. TCHAR* text = (TCHAR *) GlobalLock(hgData);
  450. bufLen = GlobalSize(hgData);
  451. m_Desc = text;
  452. bRet = true;
  453. }
  454. else
  455. {
  456. char* text = (char *) GlobalLock(hgData);
  457. bufLen = GlobalSize(hgData);
  458. m_Desc = text;
  459. bRet = true;
  460. }
  461. if(bufLen > g_Opt.m_bDescTextSize)
  462. {
  463. m_Desc = m_Desc.Left(g_Opt.m_bDescTextSize);
  464. }
  465. //Unlock the data
  466. GlobalUnlock(hgData);
  467. return bRet;
  468. }
  469. bool CClip::SetDescFromType()
  470. {
  471. INT_PTR size = m_Formats.GetSize();
  472. if(size <= 0)
  473. {
  474. return false;
  475. }
  476. int nCF_HDROPIndex = -1;
  477. for(int i = 0; i < size; i++)
  478. {
  479. if(m_Formats[i].m_cfType == CF_HDROP)
  480. {
  481. nCF_HDROPIndex = i;
  482. }
  483. }
  484. if(nCF_HDROPIndex >= 0)
  485. {
  486. using namespace nsPath;
  487. HDROP drop = (HDROP)GlobalLock(m_Formats[nCF_HDROPIndex].m_hgData);
  488. int nNumFiles = min(5, DragQueryFile(drop, -1, NULL, 0));
  489. if(nNumFiles > 1)
  490. m_Desc = "Copied Files - ";
  491. else
  492. m_Desc = "Copied File - ";
  493. TCHAR file[MAX_PATH];
  494. for(int nFile = 0; nFile < nNumFiles; nFile++)
  495. {
  496. if(DragQueryFile(drop, nFile, file, sizeof(file)) > 0)
  497. {
  498. CPath path(file);
  499. m_Desc += path.GetName();
  500. m_Desc += " - ";
  501. m_Desc += file;
  502. m_Desc += "\n";
  503. }
  504. }
  505. GlobalUnlock(m_Formats[nCF_HDROPIndex].m_hgData);
  506. }
  507. else
  508. {
  509. m_Desc = GetFormatName(m_Formats[0].m_cfType);
  510. }
  511. return m_Desc.GetLength() > 0;
  512. }
  513. bool CClip::AddToDB(bool bCheckForDuplicates)
  514. {
  515. bool bResult;
  516. try
  517. {
  518. m_CRC = GenerateCRC();
  519. if(bCheckForDuplicates &&
  520. m_parentId < 0)
  521. {
  522. int nID = FindDuplicate();
  523. if(nID >= 0)
  524. {
  525. MakeLatestOrder();
  526. MakeLatestGroupOrder();
  527. CString sql;
  528. sql.Format(_T("UPDATE Main SET clipOrder = %f where lID = %d;"),
  529. m_clipOrder, nID);
  530. int ret = theApp.m_db.execDML(sql);
  531. int groupRet = -1;
  532. if(m_parentId > -1)
  533. {
  534. sql.Format(_T("UPDATE Main SET clipGroupOrder = %f where lID = %d;"),
  535. m_clipGroupOrder, nID);
  536. groupRet = theApp.m_db.execDML(sql);
  537. }
  538. m_id = nID;
  539. Log(StrF(_T("Found duplicate clip in db, Id: %d, ParentId: %d crc: %d, NewOrder: %f, GroupOrder %f, Ret: %d, GroupRet: %d, SQL: %s"),
  540. nID, m_parentId, m_CRC, m_clipOrder, m_clipGroupOrder, ret, groupRet, sql));
  541. return true;
  542. }
  543. }
  544. }
  545. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  546. bResult = false;
  547. if(AddToMainTable())
  548. {
  549. bResult = AddToDataTable();
  550. }
  551. if(bResult)
  552. {
  553. if(g_Opt.m_csPlaySoundOnCopy.IsEmpty() == FALSE)
  554. PlaySound(g_Opt.m_csPlaySoundOnCopy, NULL, SND_FILENAME|SND_ASYNC);
  555. }
  556. // should be emptied by AddToDataTable
  557. //ASSERT(m_Formats.GetSize() == 0);
  558. return bResult;
  559. }
  560. // if a duplicate exists, set recset to the duplicate and return true
  561. int CClip::FindDuplicate()
  562. {
  563. try
  564. {
  565. //If they are allowing duplicates still check
  566. //the last copied item
  567. if(g_Opt.m_bAllowDuplicates)
  568. {
  569. if(m_CRC == m_LastAddedCRC)
  570. return m_lastAddedID;
  571. }
  572. else
  573. {
  574. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID FROM Main WHERE CRC = %d"), m_CRC);
  575. if(q.eof() == false)
  576. {
  577. return q.getIntField(_T("lID"));
  578. }
  579. }
  580. }
  581. CATCH_SQLITE_EXCEPTION
  582. return -1;
  583. }
  584. DWORD CClip::GenerateCRC()
  585. {
  586. CClipFormat* pCF;
  587. DWORD dwCRC = 0xFFFFFFFF;
  588. CCrc32Dynamic *pCrc32 = new CCrc32Dynamic;
  589. if(pCrc32)
  590. {
  591. //Generate a CRC value for all copied data
  592. INT_PTR size = m_Formats.GetSize();
  593. for(int i = 0; i < size ; i++)
  594. {
  595. pCF = & m_Formats.ElementAt(i);
  596. const unsigned char *Data = (const unsigned char *)GlobalLock(pCF->m_hgData);
  597. if(Data)
  598. {
  599. if (CGetSetOptions::GetAdjustClipsForCRC())
  600. {
  601. //Try and remove known things that change in rtf (word and outlook)
  602. if (pCF->m_cfType == theApp.m_RTFFormat)
  603. {
  604. CStringA CStringData((char*)Data);
  605. //In word and outlook I was finding that data in the \\datastore section was always changing, remove this for the crc check
  606. RemoveRTFSection(CStringData, "{\\*\\datastore");
  607. //In word and outlook rsid values are always changing, remove these for the crc check
  608. DeleteParamFromRTF(CStringData, "\\rsid", true);
  609. DeleteParamFromRTF(CStringData, "\\insrsid", true);
  610. DeleteParamFromRTF(CStringData, "\\mdispDef1", false);
  611. pCrc32->GenerateCrc32((const LPBYTE)CStringData.GetBuffer(), (DWORD)CStringData.GetLength(), dwCRC);
  612. }
  613. else
  614. {
  615. //i've seen examble where the text size was 10 but the data size was 20, leading to random crc values
  616. //try and only check the crc for the actual text
  617. int dataLength = GlobalSize(pCF->m_hgData);
  618. if (pCF->m_cfType == CF_TEXT)
  619. {
  620. dataLength = min(dataLength, (strlen((char*)Data) + 1));
  621. }
  622. else if (pCF->m_cfType == CF_UNICODETEXT)
  623. {
  624. dataLength = min(dataLength, ((wcslen((wchar_t*)Data) + 1) * 2));
  625. }
  626. pCrc32->GenerateCrc32((const LPBYTE)Data, (DWORD)dataLength, dwCRC);
  627. }
  628. }
  629. else
  630. {
  631. pCrc32->GenerateCrc32((const LPBYTE)Data, (DWORD)GlobalSize(pCF->m_hgData), dwCRC);
  632. }
  633. }
  634. GlobalUnlock(pCF->m_hgData);
  635. }
  636. dwCRC = ~dwCRC;
  637. delete pCrc32;
  638. }
  639. return dwCRC;
  640. }
  641. // assigns m_ID
  642. bool CClip::AddToMainTable()
  643. {
  644. try
  645. {
  646. m_Desc.Replace(_T("'"), _T("''"));
  647. m_csQuickPaste.Replace(_T("'"), _T("''"));
  648. CString cs;
  649. cs.Format(_T("INSERT into Main (lDate, mText, lShortCut, lDontAutoDelete, CRC, bIsGroup, lParentID, QuickPasteText, clipOrder, clipGroupOrder, globalShortCut, lastPasteDate, stickyClipOrder, stickyClipGroupOrder, MoveToGroupShortCut, GlobalMoveToGroupShortCut) ")
  650. _T("values(%d, '%s', %d, %d, %d, %d, %d, '%s', %f, %f, %d, %d, %f, %f, %d, %d);"),
  651. (int)m_Time.GetTime(),
  652. m_Desc,
  653. m_shortCut,
  654. m_dontAutoDelete,
  655. m_CRC,
  656. m_bIsGroup,
  657. m_parentId,
  658. m_csQuickPaste,
  659. m_clipOrder,
  660. m_clipGroupOrder,
  661. m_globalShortCut,
  662. (int)CTime::GetCurrentTime().GetTime(),
  663. m_stickyClipOrder,
  664. m_stickyClipGroupOrder,
  665. m_moveToGroupShortCut,
  666. m_globalMoveToGroupShortCut);
  667. theApp.m_db.execDML(cs);
  668. m_id = (long)theApp.m_db.lastRowId();
  669. Log(StrF(_T("Added clip to main table, Id: %d, ParentId: %d Desc: %s, Order: %f, GroupOrder: %f"), m_id, m_parentId, m_Desc, m_clipOrder, m_clipGroupOrder));
  670. m_LastAddedCRC = m_CRC;
  671. m_lastAddedID = m_id;
  672. }
  673. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  674. return true;
  675. }
  676. bool CClip::ModifyMainTable()
  677. {
  678. bool bRet = false;
  679. try
  680. {
  681. m_Desc.Replace(_T("'"), _T("''"));
  682. m_csQuickPaste.Replace(_T("'"), _T("''"));
  683. theApp.m_db.execDMLEx(_T("UPDATE Main SET lShortCut = %d, ")
  684. _T("mText = '%s', ")
  685. _T("lParentID = %d, ")
  686. _T("lDontAutoDelete = %d, ")
  687. _T("QuickPasteText = '%s', ")
  688. _T("clipOrder = %f, ")
  689. _T("clipGroupOrder = %f, ")
  690. _T("globalShortCut = %d, ")
  691. _T("stickyClipOrder = %f, ")
  692. _T("stickyClipGroupOrder = %f, ")
  693. _T("MoveToGroupShortCut = %d, ")
  694. _T("GlobalMoveToGroupShortCut = %d ")
  695. _T("WHERE lID = %d;"),
  696. m_shortCut,
  697. m_Desc,
  698. m_parentId,
  699. m_dontAutoDelete,
  700. m_csQuickPaste,
  701. m_clipOrder,
  702. m_clipGroupOrder,
  703. m_globalShortCut,
  704. m_stickyClipOrder,
  705. m_stickyClipGroupOrder,
  706. m_moveToGroupShortCut,
  707. m_globalMoveToGroupShortCut,
  708. m_id);
  709. bRet = true;
  710. }
  711. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  712. return bRet;
  713. }
  714. bool CClip::ModifyDescription()
  715. {
  716. bool bRet = false;
  717. try
  718. {
  719. m_Desc.Replace(_T("'"), _T("''"));
  720. theApp.m_db.execDMLEx(_T("UPDATE Main SET mText = '%s' ")
  721. _T("WHERE lID = %d;"),
  722. m_Desc,
  723. m_id);
  724. bRet = true;
  725. }
  726. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  727. return bRet;
  728. }
  729. // Empties m_Formats as it saves them to the Data Table.
  730. bool CClip::AddToDataTable()
  731. {
  732. CClipFormat* pCF;
  733. try
  734. {
  735. CppSQLite3Statement stmt = theApp.m_db.compileStatement(_T("insert into Data values (NULL, ?, ?, ?);"));
  736. for(INT_PTR i = m_Formats.GetSize()-1; i >= 0 ; i--)
  737. {
  738. pCF = &m_Formats.ElementAt(i);
  739. CString formatName = GetFormatName(pCF->m_cfType);
  740. int clipSize = 0;
  741. stmt.bind(1, m_id);
  742. stmt.bind(2, formatName);
  743. const unsigned char *Data = (const unsigned char *)GlobalLock(pCF->m_hgData);
  744. if(Data)
  745. {
  746. clipSize = (int)GlobalSize(pCF->m_hgData);
  747. stmt.bind(3, Data, clipSize);
  748. }
  749. GlobalUnlock(pCF->m_hgData);
  750. stmt.execDML();
  751. stmt.reset();
  752. pCF->m_dataId = (long)theApp.m_db.lastRowId();
  753. Log(StrF(_T("Added ClipData to DB, Id: %d, ParentId: %d Type: %s, size: %d"), pCF->m_dataId, m_id, formatName, clipSize));
  754. }
  755. }
  756. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  757. return true;
  758. }
  759. void CClip::MoveUp(int parentId)
  760. {
  761. try
  762. {
  763. //In a group, not a sticky
  764. if(parentId > -1 && m_stickyClipGroupOrder == INVALID_STICKY)
  765. {
  766. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, clipGroupOrder FROM Main Where lParentID = %d AND stickyClipGroupOrder == -(2147483647) AND clipGroupOrder > %f ORDER BY clipGroupOrder ASC LIMIT 1"), parentId, m_clipGroupOrder);
  767. if (q.eof() == false)
  768. {
  769. int idAbove = q.getIntField(_T("lID"));
  770. double orderAbove = q.getFloatField(_T("clipGroupOrder"));
  771. CppSQLite3Query q2 = theApp.m_db.execQueryEx(_T("SELECT lID, clipGroupOrder FROM Main Where lParentID = %d AND clipGroupOrder > %f AND stickyClipGroupOrder == -(2147483647) ORDER BY clipGroupOrder ASC LIMIT 1"), parentId, orderAbove);
  772. if (q2.eof() == false)
  773. {
  774. int idTwoAbove = q2.getIntField(_T("lID"));
  775. double orderTwoAbove = q2.getFloatField(_T("clipGroupOrder"));
  776. m_clipGroupOrder = orderAbove + (orderTwoAbove - orderAbove) / 2.0;
  777. }
  778. else
  779. {
  780. m_clipGroupOrder = orderAbove + 1;
  781. }
  782. }
  783. }
  784. // main group, not a sticky
  785. else if(parentId <= -1 && m_stickyClipOrder == INVALID_STICKY)
  786. {
  787. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, clipOrder FROM Main Where clipOrder > %f AND stickyClipOrder == -(2147483647) ORDER BY clipOrder ASC LIMIT 1"), m_clipOrder);
  788. if (q.eof() == false)
  789. {
  790. int idAbove = q.getIntField(_T("lID"));
  791. double orderAbove = q.getFloatField(_T("clipOrder"));
  792. CppSQLite3Query q2 = theApp.m_db.execQueryEx(_T("SELECT lID, clipOrder FROM Main Where clipOrder > %f AND stickyClipOrder == -(2147483647) ORDER BY clipOrder ASC LIMIT 1"), orderAbove);
  793. if (q2.eof() == false)
  794. {
  795. int idTwoAbove = q2.getIntField(_T("lID"));
  796. double orderTwoAbove = q2.getFloatField(_T("clipOrder"));
  797. m_clipOrder = orderAbove + (orderTwoAbove - orderAbove) / 2.0;
  798. }
  799. else
  800. {
  801. m_clipOrder = orderAbove + 1;
  802. }
  803. }
  804. }
  805. //In a group, a sticky clip
  806. else if(parentId > -1 && m_stickyClipGroupOrder != INVALID_STICKY)
  807. {
  808. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, stickyClipGroupOrder FROM Main Where lParentID = %d AND stickyClipGroupOrder <> -(2147483647) AND stickyClipGroupOrder > %f ORDER BY stickyClipGroupOrder ASC LIMIT 1"), parentId, m_stickyClipGroupOrder);
  809. if (q.eof() == false)
  810. {
  811. int idAbove = q.getIntField(_T("lID"));
  812. double orderAbove = q.getFloatField(_T("stickyClipGroupOrder"));
  813. CppSQLite3Query q2 = theApp.m_db.execQueryEx(_T("SELECT lID, stickyClipGroupOrder FROM Main Where lParentID = %d AND stickyClipGroupOrder <> -(2147483647) AND stickyClipGroupOrder > %f ORDER BY stickyClipGroupOrder ASC LIMIT 1"), parentId, orderAbove);
  814. if (q2.eof() == false)
  815. {
  816. int idTwoAbove = q2.getIntField(_T("lID"));
  817. double orderTwoAbove = q2.getFloatField(_T("stickyClipGroupOrder"));
  818. m_stickyClipGroupOrder = orderAbove + (orderTwoAbove - orderAbove) / 2.0;
  819. }
  820. else
  821. {
  822. m_stickyClipGroupOrder = orderAbove + 1;
  823. }
  824. }
  825. }
  826. //not in a group, a sticky clip
  827. else if(parentId <= -1 && m_stickyClipOrder != INVALID_STICKY)
  828. {
  829. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, stickyClipOrder FROM Main Where stickyClipOrder <> -(2147483647) AND stickyClipOrder > %f ORDER BY stickyClipOrder ASC LIMIT 1"), m_stickyClipOrder);
  830. if (q.eof() == false)
  831. {
  832. int idAbove = q.getIntField(_T("lID"));
  833. double orderAbove = q.getFloatField(_T("stickyClipOrder"));
  834. CppSQLite3Query q2 = theApp.m_db.execQueryEx(_T("SELECT lID, stickyClipOrder FROM Main Where stickyClipOrder <> -(2147483647) AND stickyClipOrder > %f ORDER BY stickyClipOrder ASC LIMIT 1"), orderAbove);
  835. if (q2.eof() == false)
  836. {
  837. int idTwoAbove = q2.getIntField(_T("lID"));
  838. double orderTwoAbove = q2.getFloatField(_T("stickyClipOrder"));
  839. m_stickyClipOrder = orderAbove + (orderTwoAbove - orderAbove) / 2.0;
  840. }
  841. else
  842. {
  843. m_stickyClipOrder = orderAbove + 1;
  844. }
  845. }
  846. }
  847. }
  848. CATCH_SQLITE_EXCEPTION
  849. }
  850. void CClip::MoveDown(int parentId)
  851. {
  852. try
  853. {
  854. //In a group, not a sticky
  855. if(parentId > -1 && m_stickyClipGroupOrder == INVALID_STICKY)
  856. {
  857. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, clipGroupOrder FROM Main Where lParentID = %d AND clipGroupOrder < %f AND stickyClipGroupOrder = -(2147483647) ORDER BY clipGroupOrder DESC LIMIT 1"), parentId, m_clipGroupOrder);
  858. if (q.eof() == false)
  859. {
  860. int idBelow = q.getIntField(_T("lID"));
  861. double orderBelow = q.getFloatField(_T("clipGroupOrder"));
  862. CppSQLite3Query q2 = theApp.m_db.execQueryEx(_T("SELECT lID, clipGroupOrder FROM Main Where lParentID = %d AND clipGroupOrder < %f AND stickyClipGroupOrder = -(2147483647) ORDER BY clipGroupOrder DESC LIMIT 1"), parentId, orderBelow);
  863. if (q2.eof() == false)
  864. {
  865. int idTwoBelow = q2.getIntField(_T("lID"));
  866. double orderTwoBelow = q2.getFloatField(_T("clipGroupOrder"));
  867. m_clipGroupOrder = orderBelow + (orderTwoBelow - orderBelow) / 2.0;
  868. }
  869. else
  870. {
  871. m_clipGroupOrder = orderBelow - 1;
  872. }
  873. }
  874. }
  875. // main group, not a sticky
  876. else if(parentId <= -1 && m_stickyClipOrder == INVALID_STICKY)
  877. {
  878. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, clipOrder FROM Main Where clipOrder < %f AND stickyClipOrder = -(2147483647) ORDER BY clipOrder DESC LIMIT 1"), m_clipOrder);
  879. if (q.eof() == false)
  880. {
  881. int idBelow = q.getIntField(_T("lID"));
  882. double orderBelow = q.getFloatField(_T("clipOrder"));
  883. CppSQLite3Query q2 = theApp.m_db.execQueryEx(_T("SELECT lID, clipOrder FROM Main Where clipOrder < %f AND stickyClipOrder = -(2147483647) ORDER BY clipOrder DESC LIMIT 1"), orderBelow);
  884. if (q2.eof() == false)
  885. {
  886. int idTwoBelow = q2.getIntField(_T("lID"));
  887. double orderTwoBelow = q2.getFloatField(_T("clipOrder"));
  888. m_clipOrder = orderBelow + (orderTwoBelow - orderBelow) / 2.0;
  889. }
  890. else
  891. {
  892. m_clipOrder = orderBelow - 1;
  893. }
  894. }
  895. }
  896. //In a group, a sticky clip
  897. else if(parentId > -1 && m_stickyClipGroupOrder != INVALID_STICKY)
  898. {
  899. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, stickyClipGroupOrder FROM Main Where lParentID = %d AND stickyClipGroupOrder <> -(2147483647) AND stickyClipGroupOrder < %f ORDER BY stickyClipGroupOrder DESC LIMIT 1"), parentId, m_stickyClipGroupOrder);
  900. if (q.eof() == false)
  901. {
  902. int idBelow = q.getIntField(_T("lID"));
  903. double orderBelow = q.getFloatField(_T("stickyClipGroupOrder"));
  904. CppSQLite3Query q2 = theApp.m_db.execQueryEx(_T("SELECT lID, stickyClipGroupOrder FROM Main Where lParentID = %d AND stickyClipGroupOrder <> -(2147483647) AND stickyClipGroupOrder < %f ORDER BY stickyClipGroupOrder DESC LIMIT 1"), parentId, orderBelow);
  905. if (q2.eof() == false)
  906. {
  907. int idTwoBelow = q2.getIntField(_T("lID"));
  908. double orderTwoBelow = q2.getFloatField(_T("stickyClipGroupOrder"));
  909. m_stickyClipGroupOrder = orderBelow + (orderTwoBelow - orderBelow) / 2.0;
  910. }
  911. else
  912. {
  913. m_stickyClipGroupOrder = orderBelow - 1;
  914. }
  915. }
  916. }
  917. //not in a group, a sticky clip
  918. else if(parentId <= -1 && m_stickyClipOrder != INVALID_STICKY)
  919. {
  920. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, stickyClipOrder FROM Main Where stickyClipOrder <> -(2147483647) AND stickyClipOrder < %f ORDER BY stickyClipOrder DESC LIMIT 1"), m_stickyClipOrder);
  921. if (q.eof() == false)
  922. {
  923. int idBelow = q.getIntField(_T("lID"));
  924. double orderBelow = q.getFloatField(_T("stickyClipOrder"));
  925. CppSQLite3Query q2 = theApp.m_db.execQueryEx(_T("SELECT lID, stickyClipOrder FROM Main Where stickyClipOrder <> -(2147483647) AND stickyClipOrder < %f ORDER BY stickyClipOrder DESC LIMIT 1"), orderBelow);
  926. if (q2.eof() == false)
  927. {
  928. int idTwoBelow = q2.getIntField(_T("lID"));
  929. double orderTwoBelow = q2.getFloatField(_T("stickyClipOrder"));
  930. m_stickyClipOrder = orderBelow + (orderTwoBelow - orderBelow) / 2.0;
  931. }
  932. else
  933. {
  934. m_stickyClipOrder = orderBelow - 1;
  935. }
  936. }
  937. }
  938. }
  939. CATCH_SQLITE_EXCEPTION
  940. }
  941. void CClip::MakeStickyTop(int parentId)
  942. {
  943. if (parentId < 0)
  944. {
  945. m_stickyClipOrder = GetNewTopSticky(parentId, m_id);
  946. }
  947. else
  948. {
  949. m_stickyClipGroupOrder = GetNewTopSticky(parentId, m_id);
  950. }
  951. }
  952. void CClip::MakeStickyLast(int parentId)
  953. {
  954. if (parentId < 0)
  955. {
  956. m_stickyClipOrder = GetNewLastSticky(parentId, m_id);
  957. }
  958. else
  959. {
  960. m_stickyClipGroupOrder = GetNewLastSticky(parentId, m_id);
  961. }
  962. }
  963. bool CClip::RemoveStickySetting(int parentId)
  964. {
  965. bool reset = false;
  966. if (parentId < 0)
  967. {
  968. if (m_stickyClipOrder != INVALID_STICKY)
  969. {
  970. m_stickyClipOrder = INVALID_STICKY;
  971. reset = true;
  972. }
  973. }
  974. else
  975. {
  976. if (m_stickyClipGroupOrder != INVALID_STICKY)
  977. {
  978. m_stickyClipGroupOrder = INVALID_STICKY;
  979. reset = true;
  980. }
  981. }
  982. return reset;
  983. }
  984. double CClip::GetNewTopSticky(int parentId, int clipId)
  985. {
  986. double newOrder = 1;
  987. double existingMaxOrder = 0;
  988. CString existingDesc = _T("");
  989. try
  990. {
  991. if (parentId < 0)
  992. {
  993. CppSQLite3Query q = theApp.m_db.execQuery(_T("SELECT stickyClipOrder, mText FROM Main WHERE stickyClipOrder <> -(2147483647) ORDER BY stickyClipOrder DESC LIMIT 1"));
  994. if (q.eof() == false)
  995. {
  996. existingMaxOrder = q.getFloatField(_T("stickyClipOrder"));
  997. existingDesc = q.getStringField(_T("mText"));
  998. newOrder = existingMaxOrder + 1;
  999. }
  1000. }
  1001. else
  1002. {
  1003. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT stickyClipGroupOrder, mText FROM Main WHERE lParentID = %d AND stickyClipGroupOrder <> -(2147483647) ORDER BY stickyClipGroupOrder DESC LIMIT 1"), parentId);
  1004. if (q.eof() == false)
  1005. {
  1006. existingMaxOrder = q.getFloatField(_T("stickyClipGroupOrder"));
  1007. newOrder = existingMaxOrder + 1;
  1008. }
  1009. }
  1010. if (newOrder == 0.0)
  1011. newOrder += 1;
  1012. Log(StrF(_T("GetNewTopSticky, Id: %d, parentId: %d, CurrentMax: %f, CurrentDesc: %s, NewMax: %f"), clipId, parentId, existingMaxOrder, existingDesc, newOrder));
  1013. }
  1014. CATCH_SQLITE_EXCEPTION
  1015. return newOrder;
  1016. }
  1017. double CClip::GetNewLastSticky(int parentId, int clipId)
  1018. {
  1019. double newOrder = 1;
  1020. double existingMaxOrder = 0;
  1021. CString existingDesc = _T("");
  1022. try
  1023. {
  1024. if (parentId < 0)
  1025. {
  1026. CppSQLite3Query q = theApp.m_db.execQuery(_T("SELECT stickyClipOrder, mText FROM Main WHERE stickyClipOrder <> -(2147483647) ORDER BY stickyClipOrder LIMIT 1"));
  1027. if (q.eof() == false)
  1028. {
  1029. existingMaxOrder = q.getFloatField(_T("stickyClipOrder"));
  1030. existingDesc = q.getStringField(_T("mText"));
  1031. newOrder = existingMaxOrder - 1;
  1032. }
  1033. }
  1034. else
  1035. {
  1036. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT stickyClipGroupOrder, mText FROM Main WHERE lParentID = %d AND stickyClipGroupOrder <> -(2147483647) ORDER BY stickyClipGroupOrder LIMIT 1"), parentId);
  1037. if (q.eof() == false)
  1038. {
  1039. existingMaxOrder = q.getFloatField(_T("stickyClipGroupOrder"));
  1040. newOrder = existingMaxOrder - 1;
  1041. }
  1042. }
  1043. if (newOrder == 0.0)
  1044. newOrder -= 1;
  1045. Log(StrF(_T("GetNewLastSticky, Id: %d, parentId: %d, CurrentMax: %f, CurrentDesc: %s, NewMax: %f"), clipId, parentId, existingMaxOrder, existingDesc, newOrder));
  1046. }
  1047. CATCH_SQLITE_EXCEPTION
  1048. return newOrder;
  1049. }
  1050. void CClip::MakeLatestOrder()
  1051. {
  1052. m_clipOrder = GetNewOrder(-1, m_id);
  1053. }
  1054. void CClip::MakeLatestGroupOrder()
  1055. {
  1056. if(m_parentId > -1)
  1057. {
  1058. m_clipGroupOrder = GetNewOrder(m_parentId, m_id);
  1059. }
  1060. }
  1061. double CClip::GetNewOrder(int parentId, int clipId)
  1062. {
  1063. double newOrder = 0;
  1064. double existingMaxOrder = 0;
  1065. CString existingDesc = _T("");
  1066. try
  1067. {
  1068. if(parentId < 0)
  1069. {
  1070. CppSQLite3Query q = theApp.m_db.execQuery(_T("SELECT clipOrder, mText FROM Main ORDER BY clipOrder DESC LIMIT 1"));
  1071. if(q.eof() == false)
  1072. {
  1073. existingMaxOrder = q.getFloatField(_T("clipOrder"));
  1074. existingDesc = q.getStringField(_T("mText"));
  1075. newOrder = existingMaxOrder + 1;
  1076. }
  1077. }
  1078. else
  1079. {
  1080. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT clipGroupOrder, mText FROM Main WHERE lParentID = %d ORDER BY clipGroupOrder DESC LIMIT 1"), parentId);
  1081. if(q.eof() == false)
  1082. {
  1083. existingMaxOrder = q.getFloatField(_T("clipGroupOrder"));
  1084. newOrder = existingMaxOrder + 1;
  1085. }
  1086. }
  1087. Log(StrF(_T("GetNewOrder, Id: %d, parentId: %d, CurrentMax: %f, CurrentDesc: %s, NewMax: %f"), clipId, parentId, existingMaxOrder, existingDesc, newOrder));
  1088. }
  1089. CATCH_SQLITE_EXCEPTION
  1090. return newOrder;
  1091. }
  1092. BOOL CClip::LoadMainTable(int id)
  1093. {
  1094. bool bRet = false;
  1095. try
  1096. {
  1097. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT * FROM Main WHERE lID = %d"), id);
  1098. if(q.eof() == false)
  1099. {
  1100. m_Time = q.getIntField(_T("lDate"));
  1101. m_Desc = q.getStringField(_T("mText"));
  1102. m_CRC = q.getIntField(_T("CRC"));
  1103. m_parentId = q.getIntField(_T("lParentID"));
  1104. m_dontAutoDelete = q.getIntField(_T("lDontAutoDelete"));
  1105. m_shortCut = q.getIntField(_T("lShortCut"));
  1106. m_bIsGroup = q.getIntField(_T("bIsGroup"));
  1107. m_csQuickPaste = q.getStringField(_T("QuickPasteText"));
  1108. m_clipOrder = q.getFloatField(_T("clipOrder"));
  1109. m_clipGroupOrder = q.getFloatField(_T("clipGroupOrder"));
  1110. m_globalShortCut = q.getIntField(_T("globalShortCut"));
  1111. m_lastPasteDate = q.getIntField(_T("lastPasteDate"));
  1112. m_stickyClipOrder = q.getFloatField(_T("stickyClipOrder"));
  1113. m_stickyClipGroupOrder = q.getFloatField(_T("stickyClipGroupOrder"));
  1114. m_moveToGroupShortCut = q.getIntField(_T("MoveToGroupShortCut"));
  1115. m_globalMoveToGroupShortCut = q.getIntField(_T("GlobalMoveToGroupShortCut"));
  1116. m_id = id;
  1117. bRet = true;
  1118. }
  1119. }
  1120. CATCH_SQLITE_EXCEPTION_AND_RETURN(FALSE)
  1121. return bRet;
  1122. }
  1123. // STATICS
  1124. // Allocates a Global containing the requested Clip Format Data
  1125. HGLOBAL CClip::LoadFormat(int id, UINT cfType)
  1126. {
  1127. HGLOBAL hGlobal = 0;
  1128. try
  1129. {
  1130. CString csSQL;
  1131. csSQL.Format(
  1132. _T("SELECT Data.ooData FROM Data ")
  1133. _T("INNER JOIN Main ON Main.lID = Data.lParentID ")
  1134. _T("WHERE Main.lID = %d ")
  1135. _T("AND Data.strClipBoardFormat = \'%s\'"),
  1136. id,
  1137. GetFormatName(cfType));
  1138. CppSQLite3Query q = theApp.m_db.execQuery(csSQL);
  1139. if(q.eof() == false)
  1140. {
  1141. int nDataLen = 0;
  1142. const unsigned char *cData = q.getBlobField(0, nDataLen);
  1143. if(cData == NULL)
  1144. {
  1145. return false;
  1146. }
  1147. hGlobal = NewGlobalP((LPVOID)cData, nDataLen);
  1148. }
  1149. }
  1150. CATCH_SQLITE_EXCEPTION
  1151. return hGlobal;
  1152. }
  1153. bool CClip::LoadFormats(int id, bool bOnlyLoad_CF_TEXT, bool includeRichTextForTextOnly)
  1154. {
  1155. DWORD startTick = GetTickCount();
  1156. CClipFormat cf;
  1157. HGLOBAL hGlobal = 0;
  1158. m_Formats.RemoveAll();
  1159. try
  1160. {
  1161. //Open the data table for all that have the parent id
  1162. //Order by Data.lID so that when generating CRC it's always in the same order as the first time
  1163. //we generated it
  1164. CString csSQL;
  1165. CString textFilter = _T("");
  1166. if(bOnlyLoad_CF_TEXT)
  1167. {
  1168. textFilter = _T("(strClipBoardFormat = 'CF_TEXT' OR strClipBoardFormat = 'CF_UNICODETEXT' OR strClipBoardFormat = 'CF_HDROP'");
  1169. if(includeRichTextForTextOnly)
  1170. {
  1171. textFilter = textFilter + _T(" OR strClipBoardFormat = 'Rich Text Format') AND ");
  1172. }
  1173. else
  1174. {
  1175. textFilter = textFilter + _T(") AND ");
  1176. }
  1177. }
  1178. csSQL.Format(
  1179. _T("SELECT lID, lParentID, strClipBoardFormat, ooData FROM Data ")
  1180. _T("WHERE %s lParentID = %d ORDER BY Data.lID desc"), textFilter, id);
  1181. CppSQLite3Query q = theApp.m_db.execQuery(csSQL);
  1182. while(q.eof() == false)
  1183. {
  1184. cf.m_dataId = q.getIntField(_T("lID"));
  1185. cf.m_parentId = q.getIntField(_T("lParentID"));
  1186. cf.m_cfType = GetFormatID(q.getStringField(_T("strClipBoardFormat")));
  1187. if(bOnlyLoad_CF_TEXT)
  1188. {
  1189. if(cf.m_cfType != CF_TEXT &&
  1190. cf.m_cfType != CF_UNICODETEXT &&
  1191. cf.m_cfType != CF_HDROP &&
  1192. (cf.m_cfType != theApp.m_RTFFormat && !includeRichTextForTextOnly))
  1193. {
  1194. q.nextRow();
  1195. continue;
  1196. }
  1197. }
  1198. int nDataLen = 0;
  1199. const unsigned char *cData = q.getBlobField(_T("ooData"), nDataLen);
  1200. if(cData != NULL)
  1201. {
  1202. hGlobal = NewGlobalP((LPVOID)cData, nDataLen);
  1203. }
  1204. cf.m_hgData = hGlobal;
  1205. m_Formats.Add(cf);
  1206. q.nextRow();
  1207. }
  1208. // formats owns all the data
  1209. cf.m_hgData = 0;
  1210. }
  1211. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  1212. DWORD endTick = GetTickCount();
  1213. if((endTick-startTick) > 150)
  1214. Log(StrF(_T("Paste Timing LoadFormats: %d, ClipId: %d"), endTick-startTick, id));
  1215. return m_Formats.GetSize() > 0;
  1216. }
  1217. void CClip::LoadTypes(int id, CClipTypes& types)
  1218. {
  1219. types.RemoveAll();
  1220. try
  1221. {
  1222. CString csSQL;
  1223. // get formats for Clip "lID" (Main.lID) using the corresponding Main.lDataID
  1224. //Order by Data.lID so that when generating CRC it's always in the same order as the first time
  1225. //we generated it
  1226. csSQL.Format(
  1227. _T("SELECT strClipBoardFormat FROM Data ")
  1228. _T("INNER JOIN Main ON Main.lID = Data.lParentID ")
  1229. _T("WHERE Main.lID = %d ORDER BY Data.lID desc"), id);
  1230. CppSQLite3Query q = theApp.m_db.execQuery(csSQL);
  1231. while(q.eof() == false)
  1232. {
  1233. types.Add(GetFormatID(q.getStringField(0)));
  1234. q.nextRow();
  1235. }
  1236. }
  1237. CATCH_SQLITE_EXCEPTION
  1238. }
  1239. bool CClip::SaveFromEditWnd(BOOL bUpdateDesc)
  1240. {
  1241. bool bRet = false;
  1242. try
  1243. {
  1244. theApp.m_db.execDMLEx(_T("DELETE FROM Data WHERE lParentID = %d;"), m_id);
  1245. DWORD CRC = GenerateCRC();
  1246. AddToDataTable();
  1247. theApp.m_db.execDMLEx(_T("UPDATE Main SET CRC = %d WHERE lID = %d"), CRC, m_id);
  1248. if(bUpdateDesc)
  1249. {
  1250. m_Desc.Replace(_T("'"), _T("''"));
  1251. theApp.m_db.execDMLEx(_T("UPDATE Main SET mText = '%s' WHERE lID = %d"), m_Desc, m_id);
  1252. }
  1253. bRet = true;
  1254. }
  1255. CATCH_SQLITE_EXCEPTION
  1256. return bRet;
  1257. }
  1258. CStringW CClip::GetUnicodeTextFormat()
  1259. {
  1260. IClipFormat *pFormat = this->Clips()->FindFormatEx(CF_UNICODETEXT);
  1261. if(pFormat != NULL)
  1262. {
  1263. wchar_t *stringData = (wchar_t *)GlobalLock(pFormat->Data());
  1264. if(stringData != NULL)
  1265. {
  1266. CStringW string(stringData);
  1267. GlobalUnlock(pFormat->Data());
  1268. return string;
  1269. }
  1270. }
  1271. return _T("");
  1272. }
  1273. CStringA CClip::GetCFTextTextFormat()
  1274. {
  1275. IClipFormat *pFormat = this->Clips()->FindFormatEx(CF_TEXT);
  1276. if(pFormat != NULL)
  1277. {
  1278. char *stringData = (char *)GlobalLock(pFormat->Data());
  1279. if(stringData != NULL)
  1280. {
  1281. CStringA string(stringData);
  1282. GlobalUnlock(pFormat->Data());
  1283. return string;
  1284. }
  1285. }
  1286. return _T("");
  1287. }
  1288. BOOL CClip::WriteTextToFile(CString path, BOOL unicode, BOOL asci, BOOL utf8)
  1289. {
  1290. BOOL ret = false;
  1291. CFile f;
  1292. if(f.Open(path, CFile::modeWrite|CFile::modeCreate))
  1293. {
  1294. CStringW w = GetUnicodeTextFormat();
  1295. CStringA a = GetCFTextTextFormat();
  1296. if(utf8 && w != _T(""))
  1297. {
  1298. CStringA convToUtf8;
  1299. CTextConvert::ConvertToUTF8(w, convToUtf8);
  1300. byte header[2];
  1301. header[0] = 0xEF;
  1302. header[1] = 0xBB;
  1303. f.Write(&header, 2);
  1304. f.Write(convToUtf8.GetBuffer(), convToUtf8.GetLength());
  1305. ret = true;
  1306. }
  1307. else if(unicode && w != _T(""))
  1308. {
  1309. byte header[2];
  1310. header[0] = 0xFF;
  1311. header[1] = 0xFE;
  1312. f.Write(&header, 2);
  1313. f.Write(w.GetBuffer(), w.GetLength() * sizeof(wchar_t));
  1314. ret = true;
  1315. }
  1316. else if(asci && a != _T(""))
  1317. {
  1318. f.Write(a.GetBuffer(), a.GetLength());
  1319. ret = true;
  1320. }
  1321. f.Close();
  1322. }
  1323. return ret;
  1324. }
  1325. bool CClip::AddFileDataToData(CString &errorMessage)
  1326. {
  1327. INT_PTR size = m_Formats.GetSize();
  1328. if (size <= 0)
  1329. {
  1330. errorMessage = _T("No CF_HDROP formats to convert");
  1331. return false;
  1332. }
  1333. bool addedFileData = false;
  1334. int nCF_HDROPIndex = -1;
  1335. int dittoDataIndex = -1;
  1336. for (int i = 0; i < size; i++)
  1337. {
  1338. if (m_Formats[i].m_cfType == CF_HDROP)
  1339. {
  1340. nCF_HDROPIndex = i;
  1341. }
  1342. else if(m_Formats[i].m_cfType == theApp.m_DittoFileData)
  1343. {
  1344. dittoDataIndex = i;
  1345. }
  1346. }
  1347. if (nCF_HDROPIndex < 0)
  1348. {
  1349. errorMessage = _T("No CF_HDROP formats to convert");
  1350. return false;
  1351. }
  1352. else if (dittoDataIndex >= 0)
  1353. {
  1354. return false;
  1355. }
  1356. else
  1357. {
  1358. using namespace nsPath;
  1359. HDROP drop = (HDROP)GlobalLock(m_Formats[nCF_HDROPIndex].m_hgData);
  1360. int nNumFiles = DragQueryFile(drop, -1, NULL, 0);
  1361. TCHAR filePath[MAX_PATH];
  1362. CString newDesc = _T("File Contents - ");
  1363. for (int nFile = 0; nFile < nNumFiles; nFile++)
  1364. {
  1365. if (DragQueryFile(drop, nFile, filePath, sizeof(filePath)) > 0)
  1366. {
  1367. CFile file;
  1368. CFileException ex;
  1369. if (file.Open(filePath, CFile::modeRead | CFile::typeBinary | CFile::shareDenyNone, &ex))
  1370. {
  1371. ULONGLONG fileSize = file.GetLength();
  1372. int maxSize = CGetSetOptions::GetMaxFileContentsSize();
  1373. if (fileSize < maxSize)
  1374. {
  1375. CString src(filePath);
  1376. CStringA csFilePath;
  1377. CTextConvert::ConvertToUTF8(src, csFilePath);
  1378. int bufferSize = fileSize + csFilePath.GetLength() + 1 + md5StringLength + 1;;
  1379. char *pBuffer = new char[bufferSize];
  1380. if (pBuffer != NULL)
  1381. {
  1382. //data contents
  1383. //original file<null terminator>md5<null terminator>file data
  1384. memset(pBuffer, 0, bufferSize);
  1385. strncpy(pBuffer, csFilePath, csFilePath.GetLength());
  1386. CClipFormat* pCF;
  1387. //move the buffer start past the file path and md5 string
  1388. char *bufferStart = pBuffer + csFilePath.GetLength() + 1 + md5StringLength + 1;
  1389. int readBytes = file.Read(bufferStart, fileSize);
  1390. CMd5 md5;
  1391. CStringA md5String = md5.CalcMD5FromString(bufferStart, fileSize);
  1392. char *bufferMd5 = pBuffer + csFilePath.GetLength() + 1;
  1393. strncpy(bufferMd5, md5String, md5StringLength);
  1394. AddFormat(theApp.m_DittoFileData, pBuffer, bufferSize);
  1395. addedFileData = true;
  1396. newDesc += filePath;
  1397. newDesc += _T("\n");
  1398. Log(StrF(_T("Saving file contents to Ditto Database, file: %s, size: %d, md5: %s"), filePath, fileSize, md5String));
  1399. }
  1400. }
  1401. else
  1402. {
  1403. const int MAX_FILE_SIZE_BUFFER = 255;
  1404. TCHAR szFileSize[MAX_FILE_SIZE_BUFFER];
  1405. TCHAR szMaxFileSize[MAX_FILE_SIZE_BUFFER];
  1406. StrFormatByteSize(fileSize, szFileSize, MAX_FILE_SIZE_BUFFER);
  1407. StrFormatByteSize(maxSize, szMaxFileSize, MAX_FILE_SIZE_BUFFER);
  1408. errorMessage += StrF(_T("File is to large: %s, Size: %s, Max Size: %s\r\n"), filePath, szFileSize, szMaxFileSize);
  1409. }
  1410. }
  1411. else
  1412. {
  1413. TCHAR szError[200];
  1414. ex.GetErrorMessage(szError, 200);
  1415. errorMessage += StrF(_T("Error opening file: %s, Error: %s\r\n"), filePath, szError);
  1416. }
  1417. }
  1418. }
  1419. GlobalUnlock(m_Formats[nCF_HDROPIndex].m_hgData);
  1420. if (addedFileData)
  1421. {
  1422. for (int i = 0; i < size; i++)
  1423. {
  1424. this->m_Formats.RemoveAt(i, 1);
  1425. }
  1426. this->m_Desc = newDesc;
  1427. if (this->ModifyDescription())
  1428. {
  1429. if (this->AddToDataTable() == FALSE)
  1430. {
  1431. errorMessage += _T("Error saving data to database.");
  1432. }
  1433. }
  1434. else
  1435. {
  1436. errorMessage += _T("Error saving main table to database.");
  1437. }
  1438. }
  1439. }
  1440. return addedFileData;
  1441. }
  1442. /*----------------------------------------------------------------------------*\
  1443. CClipList
  1444. \*----------------------------------------------------------------------------*/
  1445. CClipList::~CClipList()
  1446. {
  1447. CClip* pClip;
  1448. while(GetCount())
  1449. {
  1450. pClip = RemoveHead();
  1451. delete pClip;
  1452. }
  1453. }
  1454. // returns the number of clips actually saved
  1455. // while this does empty the Format Data, it does not delete the Clips.
  1456. int CClipList::AddToDB(bool bLatestOrder)
  1457. {
  1458. Log(_T("AddToDB - Start"));
  1459. int savedCount = 0;
  1460. CClip* pClip;
  1461. POSITION pos;
  1462. bool bResult;
  1463. INT_PTR remaining = GetCount();
  1464. pos = GetHeadPosition();
  1465. while(pos)
  1466. {
  1467. Log(StrF(_T("AddToDB - while(pos), Start Remaining %d"), remaining));
  1468. remaining--;
  1469. pClip = GetNext(pos);
  1470. ASSERT(pClip);
  1471. if(bLatestOrder)
  1472. {
  1473. pClip->MakeLatestOrder();
  1474. pClip->MakeLatestGroupOrder();
  1475. }
  1476. pClip->m_Time = CTime::GetCurrentTime().GetTime();
  1477. bResult = pClip->AddToDB();
  1478. if(bResult)
  1479. {
  1480. savedCount++;
  1481. }
  1482. Log(StrF(_T("AddToDB - while(pos), End Remaining %d, save count: %d"), remaining, savedCount));
  1483. }
  1484. Log(StrF(_T("AddToDB - Start, count: %d"), savedCount));
  1485. return savedCount;
  1486. }
  1487. const CClipList& CClipList::operator=(const CClipList &cliplist)
  1488. {
  1489. POSITION pos;
  1490. CClip* pClip;
  1491. pos = cliplist.GetHeadPosition();
  1492. while(pos)
  1493. {
  1494. pClip = cliplist.GetNext(pos);
  1495. ASSERT(pClip);
  1496. CClip *pNewClip = new CClip;
  1497. if(pNewClip)
  1498. {
  1499. *pNewClip = *pClip;
  1500. AddTail(pNewClip);
  1501. }
  1502. }
  1503. return *this;
  1504. }