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