Clip.cpp 21 KB

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