Clip.cpp 20 KB

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