Clip.cpp 21 KB

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