Clip.cpp 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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, true))
  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. cfDesc.m_cfType = CF_UNICODETEXT;
  272. if(oleData.IsDataAvailable(cfDesc.m_cfType))
  273. {
  274. cfDesc.m_hgData = oleData.GetGlobalData(cfDesc.m_cfType);
  275. bIsDescSet = SetDescFromText(cfDesc.m_hgData, true);
  276. Log(StrF(_T("Tried to set description from cf_unicode text, Set: %d, Desc: [%s]"), bIsDescSet, m_Desc.Left(30)));
  277. }
  278. if(bIsDescSet == false)
  279. {
  280. cfDesc.m_cfType = CF_TEXT;
  281. if(oleData.IsDataAvailable(cfDesc.m_cfType))
  282. {
  283. cfDesc.m_hgData = oleData.GetGlobalData(cfDesc.m_cfType);
  284. bIsDescSet = SetDescFromText(cfDesc.m_hgData, false);
  285. Log(StrF(_T("Tried to set description from cf_text text, Set: %d, Desc: [%s]"), bIsDescSet, m_Desc.Left(30)));
  286. }
  287. }
  288. INT_PTR nSize;
  289. CClipFormat cf;
  290. INT_PTR numTypes = pTypes->GetSize();
  291. Log(StrF(_T("Begin enumerating over supported types, Count: %d"), numTypes));
  292. for(int i = 0; i < numTypes; i++)
  293. {
  294. cf.m_cfType = pTypes->ElementAt(i);
  295. BOOL bSuccess = false;
  296. Log(StrF(_T("Begin try and load type %s"), GetFormatName(cf.m_cfType)));
  297. // is this the description we already fetched?
  298. if(cf.m_cfType == cfDesc.m_cfType)
  299. {
  300. cf = cfDesc;
  301. cfDesc.m_hgData = 0; // cf owns it now (to go into m_Formats)
  302. }
  303. else if(!oleData.IsDataAvailable(cf.m_cfType))
  304. {
  305. Log(StrF(_T("End of load - Data is not available for type %s"), GetFormatName(cf.m_cfType)));
  306. continue;
  307. }
  308. else
  309. {
  310. cf.m_hgData = oleData.GetGlobalData(cf.m_cfType);
  311. }
  312. if(cf.m_hgData)
  313. {
  314. nSize = GlobalSize(cf.m_hgData);
  315. if(nSize > 0)
  316. {
  317. if(g_Opt.m_lMaxClipSizeInBytes > 0 && (int)nSize > g_Opt.m_lMaxClipSizeInBytes)
  318. {
  319. CString cs;
  320. cs.Format(_T("Maximum clip size reached max size = %d, clip size = %d"), g_Opt.m_lMaxClipSizeInBytes, nSize);
  321. Log(cs);
  322. oleData.Release();
  323. return false;
  324. }
  325. ASSERT(::IsValid(cf.m_hgData));
  326. m_Formats.Add(cf);
  327. bSuccess = true;
  328. }
  329. else
  330. {
  331. ASSERT(FALSE); // a valid GlobalMem with 0 size is strange
  332. cf.Free();
  333. Log(StrF(_T("Data length is 0 for type %s"), GetFormatName(cf.m_cfType)));
  334. }
  335. cf.m_hgData = 0; // m_Formats owns it now
  336. }
  337. Log(StrF(_T("End of load - type %s, Success: %d"), GetFormatName(cf.m_cfType), bSuccess));
  338. }
  339. Log(StrF(_T("End enumerating over supported types, Count: %d"), numTypes));
  340. m_Time = CTime::GetCurrentTime();
  341. if(!bIsDescSet)
  342. {
  343. SetDescFromType();
  344. Log(StrF(_T("Setting description from type, Desc: [%s]"), m_Desc.Left(30)));
  345. }
  346. // if the description was in a type that is not supported,
  347. //we have to free it since it wasn't added to m_Formats
  348. if(cfDesc.m_hgData)
  349. {
  350. cfDesc.Free();
  351. }
  352. oleData.Release();
  353. if(m_Formats.GetSize() == 0)
  354. {
  355. Log(_T("No clip types were in supported types array"));
  356. return false;
  357. }
  358. return true;
  359. }
  360. bool CClip::SetDescFromText(HGLOBAL hgData, bool unicode)
  361. {
  362. if(hgData == 0)
  363. return false;
  364. bool bRet = false;
  365. INT_PTR bufLen = 0;
  366. if(unicode)
  367. {
  368. TCHAR* text = (TCHAR *) GlobalLock(hgData);
  369. bufLen = GlobalSize(hgData);
  370. m_Desc = text;
  371. bRet = true;
  372. }
  373. else
  374. {
  375. char* text = (char *) GlobalLock(hgData);
  376. bufLen = GlobalSize(hgData);
  377. m_Desc = text;
  378. bRet = true;
  379. }
  380. if(bufLen > g_Opt.m_bDescTextSize)
  381. {
  382. m_Desc = m_Desc.Left(g_Opt.m_bDescTextSize);
  383. }
  384. //Unlock the data
  385. GlobalUnlock(hgData);
  386. return bRet;
  387. }
  388. bool CClip::SetDescFromType()
  389. {
  390. INT_PTR size = m_Formats.GetSize();
  391. if(size <= 0)
  392. {
  393. return false;
  394. }
  395. int nCF_HDROPIndex = -1;
  396. for(int i = 0; i < size; i++)
  397. {
  398. if(m_Formats[i].m_cfType == CF_HDROP)
  399. {
  400. nCF_HDROPIndex = i;
  401. }
  402. }
  403. if(nCF_HDROPIndex >= 0)
  404. {
  405. using namespace nsPath;
  406. HDROP drop = (HDROP)GlobalLock(m_Formats[nCF_HDROPIndex].m_hgData);
  407. int nNumFiles = min(5, DragQueryFile(drop, -1, NULL, 0));
  408. if(nNumFiles > 1)
  409. m_Desc = "Copied Files - ";
  410. else
  411. m_Desc = "Copied File - ";
  412. TCHAR file[MAX_PATH];
  413. for(int nFile = 0; nFile < nNumFiles; nFile++)
  414. {
  415. if(DragQueryFile(drop, nFile, file, sizeof(file)) > 0)
  416. {
  417. CPath path(file);
  418. m_Desc += path.GetName();
  419. m_Desc += " - ";
  420. m_Desc += file;
  421. m_Desc += "\n";
  422. }
  423. }
  424. GlobalUnlock(m_Formats[nCF_HDROPIndex].m_hgData);
  425. }
  426. else
  427. {
  428. m_Desc = GetFormatName(m_Formats[0].m_cfType);
  429. }
  430. return m_Desc.GetLength() > 0;
  431. }
  432. bool CClip::AddToDB(bool bCheckForDuplicates)
  433. {
  434. bool bResult;
  435. try
  436. {
  437. m_CRC = GenerateCRC();
  438. if(bCheckForDuplicates)
  439. {
  440. int nID = FindDuplicate();
  441. if(nID >= 0)
  442. {
  443. MakeLatestOrder();
  444. CString sql;
  445. sql.Format(_T("UPDATE Main SET clipOrder = %f, lastPasteDate = %d where lID = %d;"),
  446. m_clipOrder, (int)CTime::GetCurrentTime().GetTime(), nID);
  447. int ret = theApp.m_db.execDML(sql);
  448. m_id = nID;
  449. Log(StrF(_T("Found duplicate clip in db, Id: %d, crc: %d, NewOrder: %f, Ret: %d, SQL: %s"), nID, m_CRC, m_clipOrder, ret, sql));
  450. return true;
  451. }
  452. }
  453. }
  454. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  455. bResult = false;
  456. if(AddToMainTable())
  457. {
  458. bResult = AddToDataTable();
  459. }
  460. if(bResult)
  461. {
  462. if(g_Opt.m_csPlaySoundOnCopy.IsEmpty() == FALSE)
  463. PlaySound(g_Opt.m_csPlaySoundOnCopy, NULL, SND_FILENAME|SND_ASYNC);
  464. }
  465. // should be emptied by AddToDataTable
  466. //ASSERT(m_Formats.GetSize() == 0);
  467. return bResult;
  468. }
  469. // if a duplicate exists, set recset to the duplicate and return true
  470. int CClip::FindDuplicate()
  471. {
  472. try
  473. {
  474. //If they are allowing duplicates still check
  475. //the last copied item
  476. if(g_Opt.m_bAllowDuplicates)
  477. {
  478. if(m_CRC == m_LastAddedCRC)
  479. return m_lastAddedID;
  480. }
  481. else
  482. {
  483. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID FROM Main WHERE CRC = %d"), m_CRC);
  484. if(q.eof() == false)
  485. {
  486. return q.getIntField(_T("lID"));
  487. }
  488. }
  489. }
  490. CATCH_SQLITE_EXCEPTION
  491. return -1;
  492. }
  493. DWORD CClip::GenerateCRC()
  494. {
  495. CClipFormat* pCF;
  496. DWORD dwCRC = 0xFFFFFFFF;
  497. CCrc32Dynamic *pCrc32 = new CCrc32Dynamic;
  498. if(pCrc32)
  499. {
  500. //Generate a CRC value for all copied data
  501. INT_PTR size = m_Formats.GetSize();
  502. for(int i = 0; i < size ; i++)
  503. {
  504. pCF = & m_Formats.ElementAt(i);
  505. const unsigned char *Data = (const unsigned char *)GlobalLock(pCF->m_hgData);
  506. if(Data)
  507. {
  508. pCrc32->GenerateCrc32((const LPBYTE)Data, (DWORD)GlobalSize(pCF->m_hgData), dwCRC);
  509. }
  510. GlobalUnlock(pCF->m_hgData);
  511. }
  512. dwCRC = ~dwCRC;
  513. delete pCrc32;
  514. }
  515. return dwCRC;
  516. }
  517. // assigns m_ID
  518. bool CClip::AddToMainTable()
  519. {
  520. try
  521. {
  522. m_Desc.Replace(_T("'"), _T("''"));
  523. m_csQuickPaste.Replace(_T("'"), _T("''"));
  524. CString cs;
  525. cs.Format(_T("INSERT into Main values(NULL, %d, '%s', %d, %d, %d, %d, %d, '%s', %f, %f, %d, %d);"),
  526. (int)m_Time.GetTime(),
  527. m_Desc,
  528. m_shortCut,
  529. m_dontAutoDelete,
  530. m_CRC,
  531. m_bIsGroup,
  532. m_parentId,
  533. m_csQuickPaste,
  534. m_clipOrder,
  535. m_clipGroupOrder,
  536. m_globalShortCut,
  537. (int)CTime::GetCurrentTime().GetTime());
  538. theApp.m_db.execDML(cs);
  539. m_id = (long)theApp.m_db.lastRowId();
  540. Log(StrF(_T("Added clip to main table, Id: %d, Desc: %s, Order: %f, GroupOrder: %f"), m_id, m_Desc, m_clipOrder, m_clipGroupOrder));
  541. m_LastAddedCRC = m_CRC;
  542. m_lastAddedID = m_id;
  543. }
  544. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  545. return true;
  546. }
  547. bool CClip::ModifyMainTable()
  548. {
  549. bool bRet = false;
  550. try
  551. {
  552. m_Desc.Replace(_T("'"), _T("''"));
  553. m_csQuickPaste.Replace(_T("'"), _T("''"));
  554. theApp.m_db.execDMLEx(_T("UPDATE Main SET lShortCut = %d, ")
  555. _T("mText = '%s', ")
  556. _T("lParentID = %d, ")
  557. _T("lDontAutoDelete = %d, ")
  558. _T("QuickPasteText = '%s', ")
  559. _T("clipOrder = %f, ")
  560. _T("clipGroupOrder = %f, ")
  561. _T("globalShortCut = %d ")
  562. _T("WHERE lID = %d;"),
  563. m_shortCut,
  564. m_Desc,
  565. m_parentId,
  566. m_dontAutoDelete,
  567. m_csQuickPaste,
  568. m_clipOrder,
  569. m_clipGroupOrder,
  570. m_globalShortCut,
  571. m_id);
  572. bRet = true;
  573. }
  574. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  575. return bRet;
  576. }
  577. // Empties m_Formats as it saves them to the Data Table.
  578. bool CClip::AddToDataTable()
  579. {
  580. CClipFormat* pCF;
  581. try
  582. {
  583. CppSQLite3Statement stmt = theApp.m_db.compileStatement(_T("insert into Data values (NULL, ?, ?, ?);"));
  584. for(INT_PTR i = m_Formats.GetSize()-1; i >= 0 ; i--)
  585. {
  586. pCF = &m_Formats.ElementAt(i);
  587. CString formatName = GetFormatName(pCF->m_cfType);
  588. int clipSize = 0;
  589. stmt.bind(1, m_id);
  590. stmt.bind(2, formatName);
  591. const unsigned char *Data = (const unsigned char *)GlobalLock(pCF->m_hgData);
  592. if(Data)
  593. {
  594. clipSize = (int)GlobalSize(pCF->m_hgData);
  595. stmt.bind(3, Data, clipSize);
  596. }
  597. GlobalUnlock(pCF->m_hgData);
  598. stmt.execDML();
  599. stmt.reset();
  600. pCF->m_dbId = (long)theApp.m_db.lastRowId();
  601. Log(StrF(_T("Added ClipData to DB, Id: %d, ParentId: %d Type: %s, size: %d"), pCF->m_dbId, m_id, formatName, clipSize));
  602. }
  603. }
  604. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  605. return true;
  606. }
  607. void CClip::MakeLatestOrder()
  608. {
  609. m_clipOrder = GetNewOrder(-1, m_id);
  610. }
  611. double CClip::GetNewOrder(int parentId, int clipId)
  612. {
  613. double newOrder = 0;
  614. double existingMaxOrder = 0;
  615. CString existingDesc = _T("");
  616. try
  617. {
  618. if(parentId < 0)
  619. {
  620. CppSQLite3Query q = theApp.m_db.execQuery(_T("SELECT clipOrder, mText FROM Main ORDER BY clipOrder DESC LIMIT 1"));
  621. if(q.eof() == false)
  622. {
  623. existingMaxOrder = q.getFloatField(_T("clipOrder"));
  624. existingDesc = q.getStringField(_T("mText"));
  625. newOrder = existingMaxOrder + 1;
  626. }
  627. }
  628. else
  629. {
  630. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT clipGroupOrder, mText FROM Main WHERE lParentID = %d ORDER BY clipOrder DESC LIMIT 1"), parentId);
  631. if(q.eof() == false)
  632. {
  633. existingMaxOrder = q.getFloatField(_T("clipGroupOrder"));
  634. newOrder = existingMaxOrder + 1;
  635. }
  636. }
  637. Log(StrF(_T("GetNewOrder, Id: %d, parentId: %d, CurrentMax: %f, CurrentDesc: %s, NewMax: %f"), clipId, parentId, existingMaxOrder, existingDesc, newOrder));
  638. }
  639. CATCH_SQLITE_EXCEPTION
  640. return newOrder;
  641. }
  642. BOOL CClip::LoadMainTable(int id)
  643. {
  644. bool bRet = false;
  645. try
  646. {
  647. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT * FROM Main WHERE lID = %d"), id);
  648. if(q.eof() == false)
  649. {
  650. m_Time = q.getIntField(_T("lDate"));
  651. m_Desc = q.getStringField(_T("mText"));
  652. m_CRC = q.getIntField(_T("CRC"));
  653. m_parentId = q.getIntField(_T("lParentID"));
  654. m_dontAutoDelete = q.getIntField(_T("lDontAutoDelete"));
  655. m_shortCut = q.getIntField(_T("lShortCut"));
  656. m_bIsGroup = q.getIntField(_T("bIsGroup"));
  657. m_csQuickPaste = q.getStringField(_T("QuickPasteText"));
  658. m_clipOrder = q.getFloatField(_T("clipOrder"));
  659. m_clipGroupOrder = q.getFloatField(_T("clipGroupOrder"));
  660. m_globalShortCut = q.getIntField(_T("globalShortCut"));
  661. m_lastPasteDate = q.getIntField(_T("lastPasteDate"));
  662. m_id = id;
  663. bRet = true;
  664. }
  665. }
  666. CATCH_SQLITE_EXCEPTION_AND_RETURN(FALSE)
  667. return bRet;
  668. }
  669. // STATICS
  670. // Allocates a Global containing the requested Clip Format Data
  671. HGLOBAL CClip::LoadFormat(int id, UINT cfType)
  672. {
  673. HGLOBAL hGlobal = 0;
  674. try
  675. {
  676. CString csSQL;
  677. csSQL.Format(
  678. _T("SELECT Data.ooData FROM Data ")
  679. _T("INNER JOIN Main ON Main.lID = Data.lParentID ")
  680. _T("WHERE Main.lID = %d ")
  681. _T("AND Data.strClipBoardFormat = \'%s\'"),
  682. id,
  683. GetFormatName(cfType));
  684. CppSQLite3Query q = theApp.m_db.execQuery(csSQL);
  685. if(q.eof() == false)
  686. {
  687. int nDataLen = 0;
  688. const unsigned char *cData = q.getBlobField(0, nDataLen);
  689. if(cData == NULL)
  690. {
  691. return false;
  692. }
  693. hGlobal = NewGlobalP((LPVOID)cData, nDataLen);
  694. }
  695. }
  696. CATCH_SQLITE_EXCEPTION
  697. return hGlobal;
  698. }
  699. bool CClip::LoadFormats(int id, bool bOnlyLoad_CF_TEXT)
  700. {
  701. DWORD startTick = GetTickCount();
  702. CClipFormat cf;
  703. HGLOBAL hGlobal = 0;
  704. m_Formats.RemoveAll();
  705. try
  706. {
  707. //Open the data table for all that have the parent id
  708. //Order by Data.lID so that when generating CRC it's always in the same order as the first time
  709. //we generated it
  710. CString csSQL;
  711. csSQL.Format(
  712. _T("SELECT Data.lID, strClipBoardFormat, ooData FROM Data ")
  713. _T("INNER JOIN Main ON Main.lID = Data.lParentID ")
  714. _T("WHERE Main.lID = %d ORDER BY Data.lID desc"), id);
  715. CppSQLite3Query q = theApp.m_db.execQuery(csSQL);
  716. while(q.eof() == false)
  717. {
  718. cf.m_dbId = q.getIntField(_T("lID"));
  719. cf.m_cfType = GetFormatID(q.getStringField(_T("strClipBoardFormat")));
  720. if(bOnlyLoad_CF_TEXT)
  721. {
  722. if(cf.m_cfType != CF_TEXT && cf.m_cfType != CF_UNICODETEXT)
  723. {
  724. q.nextRow();
  725. continue;
  726. }
  727. }
  728. int nDataLen = 0;
  729. const unsigned char *cData = q.getBlobField(_T("ooData"), nDataLen);
  730. if(cData != NULL)
  731. {
  732. hGlobal = NewGlobalP((LPVOID)cData, nDataLen);
  733. }
  734. cf.m_hgData = hGlobal;
  735. m_Formats.Add(cf);
  736. q.nextRow();
  737. }
  738. // formats owns all the data
  739. cf.m_hgData = 0;
  740. }
  741. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  742. DWORD endTick = GetTickCount();
  743. if((endTick-startTick) > 150)
  744. Log(StrF(_T("Paste Timing LoadFormats: %d, ClipId: %d"), endTick-startTick, id));
  745. return m_Formats.GetSize() > 0;
  746. }
  747. void CClip::LoadTypes(int id, CClipTypes& types)
  748. {
  749. types.RemoveAll();
  750. try
  751. {
  752. CString csSQL;
  753. // get formats for Clip "lID" (Main.lID) using the corresponding Main.lDataID
  754. //Order by Data.lID so that when generating CRC it's always in the same order as the first time
  755. //we generated it
  756. csSQL.Format(
  757. _T("SELECT strClipBoardFormat FROM Data ")
  758. _T("INNER JOIN Main ON Main.lID = Data.lParentID ")
  759. _T("WHERE Main.lID = %d ORDER BY Data.lID desc"), id);
  760. CppSQLite3Query q = theApp.m_db.execQuery(csSQL);
  761. while(q.eof() == false)
  762. {
  763. types.Add(GetFormatID(q.getStringField(0)));
  764. q.nextRow();
  765. }
  766. }
  767. CATCH_SQLITE_EXCEPTION
  768. }
  769. bool CClip::SaveFromEditWnd(BOOL bUpdateDesc)
  770. {
  771. bool bRet = false;
  772. try
  773. {
  774. theApp.m_db.execDMLEx(_T("DELETE FROM Data WHERE lParentID = %d;"), m_id);
  775. DWORD CRC = GenerateCRC();
  776. AddToDataTable();
  777. theApp.m_db.execDMLEx(_T("UPDATE Main SET CRC = %d WHERE lID = %d"), CRC, m_id);
  778. if(bUpdateDesc)
  779. {
  780. m_Desc.Replace(_T("'"), _T("''"));
  781. theApp.m_db.execDMLEx(_T("UPDATE Main SET mText = '%s' WHERE lID = %d"), m_Desc, m_id);
  782. }
  783. bRet = true;
  784. }
  785. CATCH_SQLITE_EXCEPTION
  786. return bRet;
  787. }
  788. /*----------------------------------------------------------------------------*\
  789. CClipList
  790. \*----------------------------------------------------------------------------*/
  791. CClipList::~CClipList()
  792. {
  793. CClip* pClip;
  794. while(GetCount())
  795. {
  796. pClip = RemoveHead();
  797. delete pClip;
  798. }
  799. }
  800. // returns the number of clips actually saved
  801. // while this does empty the Format Data, it does not delete the Clips.
  802. int CClipList::AddToDB(bool bLatestOrder)
  803. {
  804. Log(_T("AddToDB - Start"));
  805. int savedCount = 0;
  806. CClip* pClip;
  807. POSITION pos;
  808. bool bResult;
  809. INT_PTR remaining = GetCount();
  810. pos = GetHeadPosition();
  811. while(pos)
  812. {
  813. Log(StrF(_T("AddToDB - while(pos), Start Remaining %d"), remaining));
  814. remaining--;
  815. pClip = GetNext(pos);
  816. ASSERT(pClip);
  817. if(bLatestOrder)
  818. {
  819. pClip->MakeLatestOrder();
  820. }
  821. pClip->m_Time = CTime::GetCurrentTime().GetTime();
  822. bResult = pClip->AddToDB();
  823. if(bResult)
  824. {
  825. savedCount++;
  826. }
  827. Log(StrF(_T("AddToDB - while(pos), End Remaining %d, save count: %d"), remaining, savedCount));
  828. }
  829. Log(StrF(_T("AddToDB - Start, count: %d"), savedCount));
  830. return savedCount;
  831. }
  832. const CClipList& CClipList::operator=(const CClipList &cliplist)
  833. {
  834. POSITION pos;
  835. CClip* pClip;
  836. pos = cliplist.GetHeadPosition();
  837. while(pos)
  838. {
  839. pClip = cliplist.GetNext(pos);
  840. ASSERT(pClip);
  841. CClip *pNewClip = new CClip;
  842. if(pNewClip)
  843. {
  844. *pNewClip = *pClip;
  845. AddTail(pNewClip);
  846. }
  847. }
  848. return *this;
  849. }