Clip.cpp 19 KB

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