Clip.cpp 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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 <Mmsystem.h>
  13. #include "Path.h"
  14. #ifdef _DEBUG
  15. #undef THIS_FILE
  16. static char THIS_FILE[]=__FILE__;
  17. #define new DEBUG_NEW
  18. #endif
  19. /*----------------------------------------------------------------------------*\
  20. COleDataObjectEx
  21. \*----------------------------------------------------------------------------*/
  22. HGLOBAL COleDataObjectEx::GetGlobalData(CLIPFORMAT cfFormat, LPFORMATETC lpFormatEtc)
  23. {
  24. HGLOBAL hGlobal = COleDataObject::GetGlobalData(cfFormat, lpFormatEtc);
  25. if(hGlobal)
  26. {
  27. if(!::IsValid(hGlobal))
  28. {
  29. Log( StrF(
  30. _T("COleDataObjectEx::GetGlobalData(\"%s\"): ERROR: Invalid (NULL) data returned."),
  31. GetFormatName(cfFormat) ) );
  32. ::GlobalFree( hGlobal );
  33. hGlobal = NULL;
  34. }
  35. return hGlobal;
  36. }
  37. // The data isn't in global memory, so try getting an IStream interface to it.
  38. STGMEDIUM stg;
  39. if(!GetData(cfFormat, &stg))
  40. {
  41. return 0;
  42. }
  43. switch(stg.tymed)
  44. {
  45. case TYMED_HGLOBAL:
  46. hGlobal = stg.hGlobal;
  47. break;
  48. case TYMED_ISTREAM:
  49. {
  50. UINT uDataSize;
  51. LARGE_INTEGER li;
  52. ULARGE_INTEGER uli;
  53. li.HighPart = li.LowPart = 0;
  54. if ( SUCCEEDED( stg.pstm->Seek ( li, STREAM_SEEK_END, &uli )))
  55. {
  56. hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, uli.LowPart );
  57. void* pv = GlobalLock(hGlobal);
  58. stg.pstm->Seek(li, STREAM_SEEK_SET, NULL);
  59. HRESULT result = stg.pstm->Read(pv, uli.LowPart, (PULONG)&uDataSize);
  60. GlobalUnlock(hGlobal);
  61. if( FAILED(result) )
  62. hGlobal = GlobalFree(hGlobal);
  63. }
  64. break; // case TYMED_ISTREAM
  65. }
  66. } // end switch
  67. ReleaseStgMedium(&stg);
  68. if(hGlobal && !::IsValid(hGlobal))
  69. {
  70. Log( StrF(
  71. _T("COleDataObjectEx::GetGlobalData(\"%s\"): ERROR: Invalid (NULL) data returned."),
  72. GetFormatName(cfFormat)));
  73. ::GlobalFree(hGlobal);
  74. hGlobal = NULL;
  75. }
  76. return hGlobal;
  77. }
  78. /*----------------------------------------------------------------------------*\
  79. CClipFormat - holds the data of one clip format.
  80. \*----------------------------------------------------------------------------*/
  81. CClipFormat::CClipFormat(CLIPFORMAT cfType, HGLOBAL hgData, int dbId)
  82. {
  83. m_cfType = cfType;
  84. m_hgData = hgData;
  85. m_autoDeleteData = true;
  86. m_dbId = dbId;
  87. }
  88. CClipFormat::~CClipFormat()
  89. {
  90. Free();
  91. }
  92. void CClipFormat::Clear()
  93. {
  94. m_cfType = 0;
  95. m_hgData = 0;
  96. m_dbId = -1;
  97. }
  98. void CClipFormat::Free()
  99. {
  100. if(m_autoDeleteData)
  101. {
  102. if(m_hgData)
  103. {
  104. m_hgData = ::GlobalFree( m_hgData );
  105. m_hgData = NULL;
  106. }
  107. }
  108. }
  109. /*----------------------------------------------------------------------------*\
  110. CClipFormats - holds an array of CClipFormat
  111. \*----------------------------------------------------------------------------*/
  112. // returns a pointer to the CClipFormat in this array which matches the given type
  113. // or NULL if that type doesn't exist in this array.
  114. CClipFormat* CClipFormats::FindFormat(UINT cfType)
  115. {
  116. CClipFormat* pCF;
  117. INT_PTR count = GetSize();
  118. for(int i=0; i < count; i++)
  119. {
  120. pCF = &ElementAt(i);
  121. if(pCF->m_cfType == cfType)
  122. return pCF;
  123. }
  124. return NULL;
  125. }
  126. /*----------------------------------------------------------------------------*\
  127. CClip - holds multiple CClipFormats and CopyClipboard() statistics
  128. \*----------------------------------------------------------------------------*/
  129. DWORD CClip::m_LastAddedCRC = 0;
  130. int CClip::m_lastAddedID = -1;
  131. CClip::CClip() :
  132. m_id(0),
  133. m_CRC(0),
  134. m_parentId(-1),
  135. m_dontAutoDelete(FALSE),
  136. m_shortCut(0),
  137. m_bIsGroup(FALSE),
  138. m_param1(0),
  139. m_clipOrder(0),
  140. m_clipGroupOrder(0),
  141. m_globalShortCut(FALSE)
  142. {
  143. }
  144. CClip::~CClip()
  145. {
  146. EmptyFormats();
  147. }
  148. void CClip::Clear()
  149. {
  150. m_id = -1;
  151. m_Time = 0;
  152. m_Desc = "";
  153. m_CRC = 0;
  154. m_parentId = -1;
  155. m_dontAutoDelete = FALSE;
  156. m_shortCut = 0;
  157. m_bIsGroup = FALSE;
  158. m_csQuickPaste = "";
  159. m_param1 = 0;
  160. EmptyFormats();
  161. }
  162. const CClip& CClip::operator=(const CClip &clip)
  163. {
  164. const CClipFormat* pCF;
  165. m_id = clip.m_id;
  166. m_Time = clip.m_Time;
  167. m_lastPasteDate = clip.m_lastPasteDate;
  168. m_CRC = clip.m_CRC;
  169. m_parentId = clip.m_parentId;
  170. m_dontAutoDelete = clip.m_dontAutoDelete;
  171. m_shortCut = clip.m_shortCut;
  172. m_bIsGroup = clip.m_bIsGroup;
  173. m_csQuickPaste = clip.m_csQuickPaste;
  174. INT_PTR nCount = clip.m_Formats.GetSize();
  175. for(int i = 0; i < nCount; i++)
  176. {
  177. pCF = &clip.m_Formats.GetData()[i];
  178. LPVOID pvData = GlobalLock(pCF->m_hgData);
  179. if(pvData)
  180. {
  181. AddFormat(pCF->m_cfType, pvData, (UINT)GlobalSize(pCF->m_hgData));
  182. }
  183. GlobalUnlock(pCF->m_hgData);
  184. }
  185. //Set this after since in could get the wrong description in AddFormat
  186. m_Desc = clip.m_Desc;
  187. return *this;
  188. }
  189. void CClip::EmptyFormats()
  190. {
  191. // free global memory in m_Formats
  192. for(INT_PTR i = m_Formats.GetSize()-1; i >= 0; i--)
  193. {
  194. m_Formats[i].Free();
  195. m_Formats.RemoveAt(i);
  196. }
  197. }
  198. // Adds a new Format to this Clip by copying the given data.
  199. bool CClip::AddFormat(CLIPFORMAT cfType, void* pData, UINT nLen)
  200. {
  201. ASSERT(pData && nLen);
  202. HGLOBAL hGlobal = ::NewGlobalP(pData, nLen);
  203. ASSERT(hGlobal);
  204. // update the Clip statistics
  205. m_Time = m_Time.GetCurrentTime();
  206. if(!SetDescFromText(hGlobal))
  207. SetDescFromType();
  208. CClipFormat format(cfType,hGlobal);
  209. CClipFormat *pFormat;
  210. pFormat = m_Formats.FindFormat(cfType);
  211. // if the format type already exists as part of this clip, replace the data
  212. if(pFormat)
  213. {
  214. pFormat->Free();
  215. pFormat->m_hgData = format.m_hgData;
  216. }
  217. else
  218. {
  219. m_Formats.Add(format);
  220. }
  221. format.m_hgData = 0; // now owned by m_Formats
  222. return true;
  223. }
  224. // Fills this CClip with the contents of the clipboard.
  225. bool CClip::LoadFromClipboard(CClipTypes* pClipTypes)
  226. {
  227. COleDataObjectEx oleData;
  228. CClipTypes defaultTypes;
  229. CClipTypes* pTypes = pClipTypes;
  230. // m_Formats should be empty when this is called.
  231. ASSERT(m_Formats.GetSize() == 0);
  232. // If the data is supposed to be private, then return
  233. if(::IsClipboardFormatAvailable(theApp.m_cfIgnoreClipboard))
  234. {
  235. Log(_T("Clipboard ignore type is on the clipboard, skipping this clipboard change"));
  236. return false;
  237. }
  238. //If we are saving a multi paste then delay us connecting to the clipboard
  239. //to allow the ctrl-v to do a paste
  240. if(::IsClipboardFormatAvailable(theApp.m_cfDelaySavingData))
  241. {
  242. Log(_T("Delay clipboard type is on the clipboard, delaying 1500 ms to allow ctrl-v to work"));
  243. Sleep(1500);
  244. }
  245. //Attach to the clipboard
  246. if(!oleData.AttachClipboard())
  247. {
  248. Log(_T("failed to attache to clipboard, skipping this clipboard change"));
  249. ASSERT(0); // does this ever happen?
  250. return false;
  251. }
  252. oleData.EnsureClipboardObject();
  253. // if no types were given, get only the first (most important) type.
  254. // (subsequent types could be synthetic due to automatic type conversions)
  255. if(pTypes == NULL || pTypes->GetSize() == 0)
  256. {
  257. ASSERT(0); // this feature is not currently used... it is an error if it is.
  258. FORMATETC formatEtc;
  259. oleData.BeginEnumFormats();
  260. oleData.GetNextFormat(&formatEtc);
  261. defaultTypes.Add(formatEtc.cfFormat);
  262. pTypes = &defaultTypes;
  263. }
  264. m_Desc = "[Ditto Error] BAD DESCRIPTION";
  265. // Get Description String
  266. // NOTE: We make sure that the description always corresponds to the
  267. // data saved by using the exact same globalmem instance as the source
  268. // for both... i.e. we only fetch the description format type once.
  269. CClipFormat cfDesc;
  270. bool bIsDescSet = false;
  271. #ifdef _UNICODE
  272. cfDesc.m_cfType = CF_UNICODETEXT;
  273. #else
  274. cfDesc.m_cfType = CF_TEXT;
  275. #endif
  276. if(oleData.IsDataAvailable(cfDesc.m_cfType))
  277. {
  278. cfDesc.m_hgData = oleData.GetGlobalData(cfDesc.m_cfType);
  279. bIsDescSet = SetDescFromText(cfDesc.m_hgData);
  280. }
  281. INT_PTR nSize;
  282. CClipFormat cf;
  283. INT_PTR numTypes = pTypes->GetSize();
  284. Log(StrF(_T("Begin enumerating over supported types, Count: %d"), numTypes));
  285. for(int i = 0; i < numTypes; i++)
  286. {
  287. cf.m_cfType = pTypes->ElementAt(i);
  288. BOOL bSuccess = false;
  289. Log(StrF(_T("Begin try and load type %s"), GetFormatName(cf.m_cfType)));
  290. // is this the description we already fetched?
  291. if(cf.m_cfType == cfDesc.m_cfType)
  292. {
  293. cf = cfDesc;
  294. cfDesc.m_hgData = 0; // cf owns it now (to go into m_Formats)
  295. }
  296. else if(!oleData.IsDataAvailable(cf.m_cfType))
  297. {
  298. Log(StrF(_T("End of load - Data is not available for type %s"), GetFormatName(cf.m_cfType)));
  299. continue;
  300. }
  301. else
  302. {
  303. cf.m_hgData = oleData.GetGlobalData(cf.m_cfType);
  304. }
  305. if(cf.m_hgData)
  306. {
  307. nSize = GlobalSize(cf.m_hgData);
  308. if(nSize > 0)
  309. {
  310. if(g_Opt.m_lMaxClipSizeInBytes > 0 && (int)nSize > g_Opt.m_lMaxClipSizeInBytes)
  311. {
  312. CString cs;
  313. cs.Format(_T("Maximum clip size reached max size = %d, clip size = %d"), g_Opt.m_lMaxClipSizeInBytes, nSize);
  314. Log(cs);
  315. oleData.Release();
  316. return false;
  317. }
  318. ASSERT(::IsValid(cf.m_hgData));
  319. m_Formats.Add(cf);
  320. bSuccess = true;
  321. }
  322. else
  323. {
  324. ASSERT(FALSE); // a valid GlobalMem with 0 size is strange
  325. cf.Free();
  326. Log(StrF(_T("Data length is 0 for type %s"), GetFormatName(cf.m_cfType)));
  327. }
  328. cf.m_hgData = 0; // m_Formats owns it now
  329. }
  330. Log(StrF(_T("End of load - type %s, Success: %d"), GetFormatName(cf.m_cfType), bSuccess));
  331. }
  332. Log(StrF(_T("End enumerating over supported types, Count: %d"), numTypes));
  333. m_Time = CTime::GetCurrentTime();
  334. if(!bIsDescSet)
  335. {
  336. SetDescFromType();
  337. }
  338. // if the description was in a type that is not supported,
  339. //we have to free it since it wasn't added to m_Formats
  340. if(cfDesc.m_hgData)
  341. {
  342. cfDesc.Free();
  343. }
  344. oleData.Release();
  345. if(m_Formats.GetSize() == 0)
  346. {
  347. Log(_T("No clip types were in supported types array"));
  348. return false;
  349. }
  350. return true;
  351. }
  352. bool CClip::SetDescFromText(HGLOBAL hgData)
  353. {
  354. if(hgData == 0)
  355. return false;
  356. bool bRet = false;
  357. TCHAR* text = (TCHAR *) GlobalLock(hgData);
  358. INT_PTR bufLen = GlobalSize(hgData);
  359. m_Desc = text;
  360. bRet = true;
  361. if(bufLen > g_Opt.m_bDescTextSize)
  362. {
  363. m_Desc = m_Desc.Left(g_Opt.m_bDescTextSize);
  364. }
  365. //Unlock the data
  366. GlobalUnlock(hgData);
  367. return bRet;
  368. }
  369. bool CClip::SetDescFromType()
  370. {
  371. INT_PTR size = m_Formats.GetSize();
  372. if(size <= 0)
  373. {
  374. return false;
  375. }
  376. int nCF_HDROPIndex = -1;
  377. for(int i = 0; i < size; i++)
  378. {
  379. if(m_Formats[i].m_cfType == CF_HDROP)
  380. {
  381. nCF_HDROPIndex = i;
  382. }
  383. }
  384. if(nCF_HDROPIndex >= 0)
  385. {
  386. using namespace nsPath;
  387. HDROP drop = (HDROP)GlobalLock(m_Formats[nCF_HDROPIndex].m_hgData);
  388. int nNumFiles = min(5, DragQueryFile(drop, -1, NULL, 0));
  389. if(nNumFiles > 1)
  390. m_Desc = "Copied Files - ";
  391. else
  392. m_Desc = "Copied File - ";
  393. TCHAR file[MAX_PATH];
  394. for(int nFile = 0; nFile < nNumFiles; nFile++)
  395. {
  396. if(DragQueryFile(drop, nFile, file, sizeof(file)) > 0)
  397. {
  398. CPath path(file);
  399. m_Desc += path.GetName();
  400. m_Desc += " - ";
  401. m_Desc += file;
  402. m_Desc += "\n";
  403. }
  404. }
  405. GlobalUnlock(m_Formats[nCF_HDROPIndex].m_hgData);
  406. }
  407. else
  408. {
  409. m_Desc = GetFormatName(m_Formats[0].m_cfType);
  410. }
  411. return m_Desc.GetLength() > 0;
  412. }
  413. bool CClip::AddToDB(bool bCheckForDuplicates)
  414. {
  415. bool bResult;
  416. try
  417. {
  418. m_CRC = GenerateCRC();
  419. if(bCheckForDuplicates)
  420. {
  421. int nID = FindDuplicate();
  422. if(nID >= 0)
  423. {
  424. MakeLatestOrder();
  425. theApp.m_db.execDMLEx(_T("UPDATE Main SET clipOrder = %f, lastPasteDate = %d where lID = %d;"),
  426. m_clipOrder, CTime::GetCurrentTime().GetTime(), nID);
  427. m_id = nID;
  428. Log(StrF(_T("Found duplicate clip in db, Id: %d, crc: %d, NewOrder: %f"), nID, m_CRC, m_clipOrder));
  429. return true;
  430. }
  431. }
  432. }
  433. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  434. bResult = false;
  435. if(AddToMainTable())
  436. {
  437. bResult = AddToDataTable();
  438. }
  439. if(bResult)
  440. {
  441. if(g_Opt.m_csPlaySoundOnCopy.IsEmpty() == FALSE)
  442. PlaySound(g_Opt.m_csPlaySoundOnCopy, NULL, SND_FILENAME|SND_ASYNC);
  443. }
  444. // should be emptied by AddToDataTable
  445. //ASSERT(m_Formats.GetSize() == 0);
  446. return bResult;
  447. }
  448. // if a duplicate exists, set recset to the duplicate and return true
  449. int CClip::FindDuplicate()
  450. {
  451. try
  452. {
  453. //If they are allowing duplicates still check
  454. //the last copied item
  455. if(g_Opt.m_bAllowDuplicates)
  456. {
  457. if(m_CRC == m_LastAddedCRC)
  458. return m_lastAddedID;
  459. }
  460. else
  461. {
  462. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID FROM Main WHERE CRC = %d"), m_CRC);
  463. if(q.eof() == false)
  464. {
  465. return q.getIntField(_T("lID"));
  466. }
  467. }
  468. }
  469. CATCH_SQLITE_EXCEPTION
  470. return -1;
  471. }
  472. DWORD CClip::GenerateCRC()
  473. {
  474. CClipFormat* pCF;
  475. DWORD dwCRC = 0xFFFFFFFF;
  476. CCrc32Dynamic *pCrc32 = new CCrc32Dynamic;
  477. if(pCrc32)
  478. {
  479. //Generate a CRC value for all copied data
  480. INT_PTR size = m_Formats.GetSize();
  481. for(int i = 0; i < size ; i++)
  482. {
  483. pCF = & m_Formats.ElementAt(i);
  484. const unsigned char *Data = (const unsigned char *)GlobalLock(pCF->m_hgData);
  485. if(Data)
  486. {
  487. pCrc32->GenerateCrc32((const LPBYTE)Data, (DWORD)GlobalSize(pCF->m_hgData), dwCRC);
  488. }
  489. GlobalUnlock(pCF->m_hgData);
  490. }
  491. dwCRC = ~dwCRC;
  492. delete pCrc32;
  493. }
  494. return dwCRC;
  495. }
  496. // assigns m_ID
  497. bool CClip::AddToMainTable()
  498. {
  499. try
  500. {
  501. m_Desc.Replace(_T("'"), _T("''"));
  502. m_csQuickPaste.Replace(_T("'"), _T("''"));
  503. CString cs;
  504. cs.Format(_T("INSERT into Main values(NULL, %d, '%s', %d, %d, %d, %d, %d, '%s', %f, %f, %d, %d);"),
  505. (long)m_Time.GetTime(),
  506. m_Desc,
  507. m_shortCut,
  508. m_dontAutoDelete,
  509. m_CRC,
  510. m_bIsGroup,
  511. m_parentId,
  512. m_csQuickPaste,
  513. m_clipOrder,
  514. m_clipGroupOrder,
  515. m_globalShortCut,
  516. CTime::GetCurrentTime().GetTime());
  517. theApp.m_db.execDML(cs);
  518. m_id = (long)theApp.m_db.lastRowId();
  519. Log(StrF(_T("Added clip to main table, Id: %d, Desc: %s, Order: %f, GroupOrder: %f"), m_id, m_Desc, m_clipOrder, m_clipGroupOrder));
  520. m_LastAddedCRC = m_CRC;
  521. m_lastAddedID = m_id;
  522. }
  523. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  524. return true;
  525. }
  526. bool CClip::ModifyMainTable()
  527. {
  528. bool bRet = false;
  529. try
  530. {
  531. m_Desc.Replace(_T("'"), _T("''"));
  532. m_csQuickPaste.Replace(_T("'"), _T("''"));
  533. theApp.m_db.execDMLEx(_T("UPDATE Main SET lShortCut = %d, ")
  534. _T("mText = '%s', ")
  535. _T("lParentID = %d, ")
  536. _T("lDontAutoDelete = %d, ")
  537. _T("QuickPasteText = '%s', ")
  538. _T("clipOrder = %f, ")
  539. _T("clipGroupOrder = %f, ")
  540. _T("globalShortCut = %d ")
  541. _T("WHERE lID = %d;"),
  542. m_shortCut,
  543. m_Desc,
  544. m_parentId,
  545. m_dontAutoDelete,
  546. m_csQuickPaste,
  547. m_clipOrder,
  548. m_clipGroupOrder,
  549. m_globalShortCut,
  550. m_id);
  551. bRet = true;
  552. }
  553. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  554. return bRet;
  555. }
  556. // Empties m_Formats as it saves them to the Data Table.
  557. bool CClip::AddToDataTable()
  558. {
  559. CClipFormat* pCF;
  560. try
  561. {
  562. CppSQLite3Statement stmt = theApp.m_db.compileStatement(_T("insert into Data values (NULL, ?, ?, ?);"));
  563. for(INT_PTR i = m_Formats.GetSize()-1; i >= 0 ; i--)
  564. {
  565. pCF = &m_Formats.ElementAt(i);
  566. CString formatName = GetFormatName(pCF->m_cfType);
  567. int clipSize = 0;
  568. stmt.bind(1, m_id);
  569. stmt.bind(2, formatName);
  570. const unsigned char *Data = (const unsigned char *)GlobalLock(pCF->m_hgData);
  571. if(Data)
  572. {
  573. clipSize = (int)GlobalSize(pCF->m_hgData);
  574. stmt.bind(3, Data, clipSize);
  575. }
  576. GlobalUnlock(pCF->m_hgData);
  577. stmt.execDML();
  578. stmt.reset();
  579. pCF->m_dbId = (long)theApp.m_db.lastRowId();
  580. Log(StrF(_T("Added ClipData to DB, Id: %d, ParentId: %d Type: %s, size: %d"), pCF->m_dbId, m_id, formatName, clipSize));
  581. }
  582. }
  583. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  584. return true;
  585. }
  586. // changes m_Time to be later than the latest clip entry in the db
  587. // ensures that pClip's time is not older than the last clip added
  588. // old times can happen on fast copies (<1 sec).
  589. void CClip::MakeLatestOrder()
  590. {
  591. m_clipOrder = GetNewOrder(-1, m_id);
  592. }
  593. double CClip::GetNewOrder(int parentId, int clipId)
  594. {
  595. double newOrder = 0;
  596. double existingMaxOrder = 0;
  597. CString existingDesc = _T("");
  598. try
  599. {
  600. if(parentId < 0)
  601. {
  602. CppSQLite3Query q = theApp.m_db.execQuery(_T("SELECT clipOrder, mText FROM Main ORDER BY clipOrder DESC LIMIT 1"));
  603. if(q.eof() == false)
  604. {
  605. existingMaxOrder = q.getFloatField(_T("clipOrder"));
  606. existingDesc = q.getStringField(_T("mText"));
  607. newOrder = existingMaxOrder + 1;
  608. }
  609. }
  610. else
  611. {
  612. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT clipGroupOrder, mText FROM Main WHERE lParentID = %d ORDER BY clipOrder DESC LIMIT 1"), parentId);
  613. if(q.eof() == false)
  614. {
  615. existingMaxOrder = q.getFloatField(_T("clipGroupOrder"));
  616. newOrder = existingMaxOrder + 1;
  617. }
  618. }
  619. Log(StrF(_T("GetNewOrder, Id: %d, parentId: %d, CurrentMax: %f, CurrentDesc: %s, NewMax: %f"), clipId, parentId, existingMaxOrder, existingDesc, newOrder));
  620. }
  621. CATCH_SQLITE_EXCEPTION
  622. return newOrder;
  623. }
  624. BOOL CClip::LoadMainTable(int id)
  625. {
  626. bool bRet = false;
  627. try
  628. {
  629. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT * FROM Main WHERE lID = %d"), id);
  630. if(q.eof() == false)
  631. {
  632. m_Time = q.getIntField(_T("lDate"));
  633. m_Desc = q.getStringField(_T("mText"));
  634. m_CRC = q.getIntField(_T("CRC"));
  635. m_parentId = q.getIntField(_T("lParentID"));
  636. m_dontAutoDelete = q.getIntField(_T("lDontAutoDelete"));
  637. m_shortCut = q.getIntField(_T("lShortCut"));
  638. m_bIsGroup = q.getIntField(_T("bIsGroup"));
  639. m_csQuickPaste = q.getStringField(_T("QuickPasteText"));
  640. m_clipOrder = q.getFloatField(_T("clipOrder"));
  641. m_clipGroupOrder = q.getFloatField(_T("clipGroupOrder"));
  642. m_globalShortCut = q.getIntField(_T("globalShortCut"));
  643. m_lastPasteDate = q.getIntField(_T("lastPasteDate"));
  644. m_id = id;
  645. bRet = true;
  646. }
  647. }
  648. CATCH_SQLITE_EXCEPTION_AND_RETURN(FALSE)
  649. return bRet;
  650. }
  651. // STATICS
  652. // Allocates a Global containing the requested Clip Format Data
  653. HGLOBAL CClip::LoadFormat(int id, UINT cfType)
  654. {
  655. HGLOBAL hGlobal = 0;
  656. try
  657. {
  658. CString csSQL;
  659. csSQL.Format(
  660. _T("SELECT Data.ooData FROM Data ")
  661. _T("INNER JOIN Main ON Main.lID = Data.lParentID ")
  662. _T("WHERE Main.lID = %d ")
  663. _T("AND Data.strClipBoardFormat = \'%s\'"),
  664. id,
  665. GetFormatName(cfType));
  666. CppSQLite3Query q = theApp.m_db.execQuery(csSQL);
  667. if(q.eof() == false)
  668. {
  669. int nDataLen = 0;
  670. const unsigned char *cData = q.getBlobField(0, nDataLen);
  671. if(cData == NULL)
  672. {
  673. return false;
  674. }
  675. hGlobal = NewGlobalP((LPVOID)cData, nDataLen);
  676. }
  677. }
  678. CATCH_SQLITE_EXCEPTION
  679. return hGlobal;
  680. }
  681. bool CClip::LoadFormats(int id, bool bOnlyLoad_CF_TEXT)
  682. {
  683. CClipFormat cf;
  684. HGLOBAL hGlobal = 0;
  685. m_Formats.RemoveAll();
  686. try
  687. {
  688. //Open the data table for all that have the parent id
  689. //Order by Data.lID so that when generating CRC it's always in the same order as the first time
  690. //we generated it
  691. CString csSQL;
  692. csSQL.Format(
  693. _T("SELECT Data.lID, strClipBoardFormat, ooData FROM Data ")
  694. _T("INNER JOIN Main ON Main.lID = Data.lParentID ")
  695. _T("WHERE Main.lID = %d ORDER BY Data.lID desc"), id);
  696. CppSQLite3Query q = theApp.m_db.execQuery(csSQL);
  697. while(q.eof() == false)
  698. {
  699. cf.m_dbId = q.getIntField(_T("lID"));
  700. cf.m_cfType = GetFormatID(q.getStringField(_T("strClipBoardFormat")));
  701. if(bOnlyLoad_CF_TEXT)
  702. {
  703. if(cf.m_cfType != CF_TEXT && cf.m_cfType != CF_UNICODETEXT)
  704. {
  705. q.nextRow();
  706. continue;
  707. }
  708. }
  709. int nDataLen = 0;
  710. const unsigned char *cData = q.getBlobField(_T("ooData"), nDataLen);
  711. if(cData != NULL)
  712. {
  713. hGlobal = NewGlobalP((LPVOID)cData, nDataLen);
  714. }
  715. cf.m_hgData = hGlobal;
  716. m_Formats.Add(cf);
  717. q.nextRow();
  718. }
  719. // formats owns all the data
  720. cf.m_hgData = 0;
  721. }
  722. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  723. return m_Formats.GetSize() > 0;
  724. }
  725. void CClip::LoadTypes(int id, CClipTypes& types)
  726. {
  727. types.RemoveAll();
  728. try
  729. {
  730. CString csSQL;
  731. // get formats for Clip "lID" (Main.lID) using the corresponding Main.lDataID
  732. //Order by Data.lID so that when generating CRC it's always in the same order as the first time
  733. //we generated it
  734. csSQL.Format(
  735. _T("SELECT strClipBoardFormat FROM Data ")
  736. _T("INNER JOIN Main ON Main.lID = Data.lParentID ")
  737. _T("WHERE Main.lID = %d ORDER BY Data.lID desc"), id);
  738. CppSQLite3Query q = theApp.m_db.execQuery(csSQL);
  739. while(q.eof() == false)
  740. {
  741. types.Add(GetFormatID(q.getStringField(0)));
  742. q.nextRow();
  743. }
  744. }
  745. CATCH_SQLITE_EXCEPTION
  746. }
  747. bool CClip::SaveFromEditWnd(BOOL bUpdateDesc)
  748. {
  749. bool bRet = false;
  750. try
  751. {
  752. theApp.m_db.execDMLEx(_T("DELETE FROM Data WHERE lParentID = %d;"), m_id);
  753. DWORD CRC = GenerateCRC();
  754. AddToDataTable();
  755. theApp.m_db.execDMLEx(_T("UPDATE Main SET CRC = %d WHERE lID = %d"), CRC, m_id);
  756. if(bUpdateDesc)
  757. {
  758. m_Desc.Replace(_T("'"), _T("''"));
  759. theApp.m_db.execDMLEx(_T("UPDATE Main SET mText = '%s' WHERE lID = %d"), m_Desc, m_id);
  760. }
  761. bRet = true;
  762. }
  763. CATCH_SQLITE_EXCEPTION
  764. return bRet;
  765. }
  766. /*----------------------------------------------------------------------------*\
  767. CClipList
  768. \*----------------------------------------------------------------------------*/
  769. CClipList::~CClipList()
  770. {
  771. CClip* pClip;
  772. while(GetCount())
  773. {
  774. pClip = RemoveHead();
  775. delete pClip;
  776. }
  777. }
  778. // returns the number of clips actually saved
  779. // while this does empty the Format Data, it does not delete the Clips.
  780. int CClipList::AddToDB(bool bLatestOrder)
  781. {
  782. Log(_T("AddToDB - Start"));
  783. int savedCount = 0;
  784. CClip* pClip;
  785. POSITION pos;
  786. bool bResult;
  787. INT_PTR remaining = GetCount();
  788. pos = GetHeadPosition();
  789. while(pos)
  790. {
  791. Log(StrF(_T("AddToDB - while(pos), Start Remaining %d"), remaining));
  792. remaining--;
  793. pClip = GetNext(pos);
  794. ASSERT(pClip);
  795. if(bLatestOrder)
  796. {
  797. pClip->MakeLatestOrder();
  798. }
  799. pClip->m_Time = CTime::GetCurrentTime().GetTime();
  800. bResult = pClip->AddToDB();
  801. if(bResult)
  802. {
  803. savedCount++;
  804. }
  805. Log(StrF(_T("AddToDB - while(pos), End Remaining %d, save count: %d"), remaining, savedCount));
  806. }
  807. Log(StrF(_T("AddToDB - Start, count: %d"), savedCount));
  808. return savedCount;
  809. }
  810. const CClipList& CClipList::operator=(const CClipList &cliplist)
  811. {
  812. POSITION pos;
  813. CClip* pClip;
  814. pos = cliplist.GetHeadPosition();
  815. while(pos)
  816. {
  817. pClip = cliplist.GetNext(pos);
  818. ASSERT(pClip);
  819. CClip *pNewClip = new CClip;
  820. if(pNewClip)
  821. {
  822. *pNewClip = *pClip;
  823. AddTail(pNewClip);
  824. }
  825. }
  826. return *this;
  827. }