Clip.cpp 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  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_stickyClipOrder(0),
  141. m_stickyClipGroupOrder(0),
  142. m_clipGroupOrder(0),
  143. m_globalShortCut(FALSE)
  144. {
  145. }
  146. CClip::~CClip()
  147. {
  148. EmptyFormats();
  149. }
  150. void CClip::Clear()
  151. {
  152. m_id = -1;
  153. m_Time = 0;
  154. m_Desc = "";
  155. m_CRC = 0;
  156. m_parentId = -1;
  157. m_dontAutoDelete = FALSE;
  158. m_shortCut = 0;
  159. m_bIsGroup = FALSE;
  160. m_csQuickPaste = "";
  161. m_param1 = 0;
  162. EmptyFormats();
  163. }
  164. const CClip& CClip::operator=(const CClip &clip)
  165. {
  166. const CClipFormat* pCF;
  167. m_id = clip.m_id;
  168. m_Time = clip.m_Time;
  169. m_lastPasteDate = clip.m_lastPasteDate;
  170. m_CRC = clip.m_CRC;
  171. m_parentId = clip.m_parentId;
  172. m_dontAutoDelete = clip.m_dontAutoDelete;
  173. m_shortCut = clip.m_shortCut;
  174. m_bIsGroup = clip.m_bIsGroup;
  175. m_csQuickPaste = clip.m_csQuickPaste;
  176. INT_PTR nCount = clip.m_Formats.GetSize();
  177. for(int i = 0; i < nCount; i++)
  178. {
  179. pCF = &clip.m_Formats.GetData()[i];
  180. LPVOID pvData = GlobalLock(pCF->m_hgData);
  181. if(pvData)
  182. {
  183. AddFormat(pCF->m_cfType, pvData, (UINT)GlobalSize(pCF->m_hgData));
  184. }
  185. GlobalUnlock(pCF->m_hgData);
  186. }
  187. //Set this after since in could get the wrong description in AddFormat
  188. m_Desc = clip.m_Desc;
  189. return *this;
  190. }
  191. void CClip::EmptyFormats()
  192. {
  193. // free global memory in m_Formats
  194. for(INT_PTR i = m_Formats.GetSize()-1; i >= 0; i--)
  195. {
  196. m_Formats[i].Free();
  197. m_Formats.RemoveAt(i);
  198. }
  199. }
  200. // Adds a new Format to this Clip by copying the given data.
  201. bool CClip::AddFormat(CLIPFORMAT cfType, void* pData, UINT nLen)
  202. {
  203. ASSERT(pData && nLen);
  204. HGLOBAL hGlobal = ::NewGlobalP(pData, nLen);
  205. ASSERT(hGlobal);
  206. // update the Clip statistics
  207. m_Time = m_Time.GetCurrentTime();
  208. if(!SetDescFromText(hGlobal, true))
  209. SetDescFromType();
  210. CClipFormat format(cfType,hGlobal);
  211. CClipFormat *pFormat;
  212. pFormat = m_Formats.FindFormat(cfType);
  213. // if the format type already exists as part of this clip, replace the data
  214. if(pFormat)
  215. {
  216. pFormat->Free();
  217. pFormat->m_hgData = format.m_hgData;
  218. }
  219. else
  220. {
  221. m_Formats.Add(format);
  222. }
  223. format.m_hgData = 0; // now owned by m_Formats
  224. return true;
  225. }
  226. // Fills this CClip with the contents of the clipboard.
  227. bool CClip::LoadFromClipboard(CClipTypes* pClipTypes)
  228. {
  229. COleDataObjectEx oleData;
  230. CClipTypes defaultTypes;
  231. CClipTypes* pTypes = pClipTypes;
  232. // m_Formats should be empty when this is called.
  233. ASSERT(m_Formats.GetSize() == 0);
  234. // If the data is supposed to be private, then return
  235. if(::IsClipboardFormatAvailable(theApp.m_cfIgnoreClipboard))
  236. {
  237. Log(_T("Clipboard ignore type is on the clipboard, skipping this clipboard change"));
  238. return false;
  239. }
  240. //If we are saving a multi paste then delay us connecting to the clipboard
  241. //to allow the ctrl-v to do a paste
  242. if(::IsClipboardFormatAvailable(theApp.m_cfDelaySavingData))
  243. {
  244. Log(_T("Delay clipboard type is on the clipboard, delaying 1500 ms to allow ctrl-v to work"));
  245. Sleep(1500);
  246. }
  247. //Attach to the clipboard
  248. if(!oleData.AttachClipboard())
  249. {
  250. Log(_T("failed to attache to clipboard, skipping this clipboard change"));
  251. ASSERT(0); // does this ever happen?
  252. return false;
  253. }
  254. oleData.EnsureClipboardObject();
  255. // if no types were given, get only the first (most important) type.
  256. // (subsequent types could be synthetic due to automatic type conversions)
  257. if(pTypes == NULL || pTypes->GetSize() == 0)
  258. {
  259. ASSERT(0); // this feature is not currently used... it is an error if it is.
  260. FORMATETC formatEtc;
  261. oleData.BeginEnumFormats();
  262. oleData.GetNextFormat(&formatEtc);
  263. defaultTypes.Add(formatEtc.cfFormat);
  264. pTypes = &defaultTypes;
  265. }
  266. m_Desc = "[Ditto Error] BAD DESCRIPTION";
  267. // Get Description String
  268. // NOTE: We make sure that the description always corresponds to the
  269. // data saved by using the exact same globalmem instance as the source
  270. // for both... i.e. we only fetch the description format type once.
  271. CClipFormat cfDesc;
  272. bool bIsDescSet = false;
  273. cfDesc.m_cfType = CF_UNICODETEXT;
  274. if(oleData.IsDataAvailable(cfDesc.m_cfType))
  275. {
  276. cfDesc.m_hgData = oleData.GetGlobalData(cfDesc.m_cfType);
  277. bIsDescSet = SetDescFromText(cfDesc.m_hgData, true);
  278. Log(StrF(_T("Tried to set description from cf_unicode text, Set: %d, Desc: [%s]"), bIsDescSet, m_Desc.Left(30)));
  279. }
  280. if(bIsDescSet == false)
  281. {
  282. cfDesc.m_cfType = CF_TEXT;
  283. if(oleData.IsDataAvailable(cfDesc.m_cfType))
  284. {
  285. cfDesc.m_hgData = oleData.GetGlobalData(cfDesc.m_cfType);
  286. bIsDescSet = SetDescFromText(cfDesc.m_hgData, false);
  287. Log(StrF(_T("Tried to set description from cf_text text, Set: %d, Desc: [%s]"), bIsDescSet, m_Desc.Left(30)));
  288. }
  289. }
  290. INT_PTR nSize;
  291. CClipFormat cf;
  292. INT_PTR numTypes = pTypes->GetSize();
  293. Log(StrF(_T("Begin enumerating over supported types, Count: %d"), numTypes));
  294. for(int i = 0; i < numTypes; i++)
  295. {
  296. cf.m_cfType = pTypes->ElementAt(i);
  297. BOOL bSuccess = false;
  298. Log(StrF(_T("Begin try and load type %s"), GetFormatName(cf.m_cfType)));
  299. // is this the description we already fetched?
  300. if(cf.m_cfType == cfDesc.m_cfType)
  301. {
  302. cf = cfDesc;
  303. cfDesc.m_hgData = 0; // cf owns it now (to go into m_Formats)
  304. }
  305. else if(!oleData.IsDataAvailable(cf.m_cfType))
  306. {
  307. Log(StrF(_T("End of load - Data is not available for type %s"), GetFormatName(cf.m_cfType)));
  308. continue;
  309. }
  310. else
  311. {
  312. cf.m_hgData = oleData.GetGlobalData(cf.m_cfType);
  313. }
  314. if(cf.m_hgData)
  315. {
  316. nSize = GlobalSize(cf.m_hgData);
  317. if(nSize > 0)
  318. {
  319. if(g_Opt.m_lMaxClipSizeInBytes > 0 && (int)nSize > g_Opt.m_lMaxClipSizeInBytes)
  320. {
  321. CString cs;
  322. cs.Format(_T("Maximum clip size reached max size = %d, clip size = %d"), g_Opt.m_lMaxClipSizeInBytes, nSize);
  323. Log(cs);
  324. oleData.Release();
  325. return false;
  326. }
  327. ASSERT(::IsValid(cf.m_hgData));
  328. m_Formats.Add(cf);
  329. bSuccess = true;
  330. }
  331. else
  332. {
  333. ASSERT(FALSE); // a valid GlobalMem with 0 size is strange
  334. cf.Free();
  335. Log(StrF(_T("Data length is 0 for type %s"), GetFormatName(cf.m_cfType)));
  336. }
  337. cf.m_hgData = 0; // m_Formats owns it now
  338. }
  339. Log(StrF(_T("End of load - type %s, Success: %d"), GetFormatName(cf.m_cfType), bSuccess));
  340. }
  341. Log(StrF(_T("End enumerating over supported types, Count: %d"), numTypes));
  342. m_Time = CTime::GetCurrentTime();
  343. if(!bIsDescSet)
  344. {
  345. SetDescFromType();
  346. Log(StrF(_T("Setting description from type, Desc: [%s]"), m_Desc.Left(30)));
  347. }
  348. // if the description was in a type that is not supported,
  349. //we have to free it since it wasn't added to m_Formats
  350. if(cfDesc.m_hgData)
  351. {
  352. cfDesc.Free();
  353. }
  354. oleData.Release();
  355. if(m_Formats.GetSize() == 0)
  356. {
  357. Log(_T("No clip types were in supported types array"));
  358. return false;
  359. }
  360. return true;
  361. }
  362. bool CClip::SetDescFromText(HGLOBAL hgData, bool unicode)
  363. {
  364. if(hgData == 0)
  365. return false;
  366. bool bRet = false;
  367. INT_PTR bufLen = 0;
  368. if(unicode)
  369. {
  370. TCHAR* text = (TCHAR *) GlobalLock(hgData);
  371. bufLen = GlobalSize(hgData);
  372. m_Desc = text;
  373. bRet = true;
  374. }
  375. else
  376. {
  377. char* text = (char *) GlobalLock(hgData);
  378. bufLen = GlobalSize(hgData);
  379. m_Desc = text;
  380. bRet = true;
  381. }
  382. if(bufLen > g_Opt.m_bDescTextSize)
  383. {
  384. m_Desc = m_Desc.Left(g_Opt.m_bDescTextSize);
  385. }
  386. //Unlock the data
  387. GlobalUnlock(hgData);
  388. return bRet;
  389. }
  390. bool CClip::SetDescFromType()
  391. {
  392. INT_PTR size = m_Formats.GetSize();
  393. if(size <= 0)
  394. {
  395. return false;
  396. }
  397. int nCF_HDROPIndex = -1;
  398. for(int i = 0; i < size; i++)
  399. {
  400. if(m_Formats[i].m_cfType == CF_HDROP)
  401. {
  402. nCF_HDROPIndex = i;
  403. }
  404. }
  405. if(nCF_HDROPIndex >= 0)
  406. {
  407. using namespace nsPath;
  408. HDROP drop = (HDROP)GlobalLock(m_Formats[nCF_HDROPIndex].m_hgData);
  409. int nNumFiles = min(5, DragQueryFile(drop, -1, NULL, 0));
  410. if(nNumFiles > 1)
  411. m_Desc = "Copied Files - ";
  412. else
  413. m_Desc = "Copied File - ";
  414. TCHAR file[MAX_PATH];
  415. for(int nFile = 0; nFile < nNumFiles; nFile++)
  416. {
  417. if(DragQueryFile(drop, nFile, file, sizeof(file)) > 0)
  418. {
  419. CPath path(file);
  420. m_Desc += path.GetName();
  421. m_Desc += " - ";
  422. m_Desc += file;
  423. m_Desc += "\n";
  424. }
  425. }
  426. GlobalUnlock(m_Formats[nCF_HDROPIndex].m_hgData);
  427. }
  428. else
  429. {
  430. m_Desc = GetFormatName(m_Formats[0].m_cfType);
  431. }
  432. return m_Desc.GetLength() > 0;
  433. }
  434. bool CClip::AddToDB(bool bCheckForDuplicates)
  435. {
  436. bool bResult;
  437. try
  438. {
  439. m_CRC = GenerateCRC();
  440. if(bCheckForDuplicates)
  441. {
  442. int nID = FindDuplicate();
  443. if(nID >= 0)
  444. {
  445. MakeLatestOrder();
  446. CString sql;
  447. sql.Format(_T("UPDATE Main SET clipOrder = %f, lastPasteDate = %d where lID = %d;"),
  448. m_clipOrder, (int)CTime::GetCurrentTime().GetTime(), nID);
  449. int ret = theApp.m_db.execDML(sql);
  450. m_id = nID;
  451. 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));
  452. return true;
  453. }
  454. }
  455. }
  456. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  457. bResult = false;
  458. if(AddToMainTable())
  459. {
  460. bResult = AddToDataTable();
  461. }
  462. if(bResult)
  463. {
  464. if(g_Opt.m_csPlaySoundOnCopy.IsEmpty() == FALSE)
  465. PlaySound(g_Opt.m_csPlaySoundOnCopy, NULL, SND_FILENAME|SND_ASYNC);
  466. }
  467. // should be emptied by AddToDataTable
  468. //ASSERT(m_Formats.GetSize() == 0);
  469. return bResult;
  470. }
  471. // if a duplicate exists, set recset to the duplicate and return true
  472. int CClip::FindDuplicate()
  473. {
  474. try
  475. {
  476. //If they are allowing duplicates still check
  477. //the last copied item
  478. if(g_Opt.m_bAllowDuplicates)
  479. {
  480. if(m_CRC == m_LastAddedCRC)
  481. return m_lastAddedID;
  482. }
  483. else
  484. {
  485. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID FROM Main WHERE CRC = %d"), m_CRC);
  486. if(q.eof() == false)
  487. {
  488. return q.getIntField(_T("lID"));
  489. }
  490. }
  491. }
  492. CATCH_SQLITE_EXCEPTION
  493. return -1;
  494. }
  495. DWORD CClip::GenerateCRC()
  496. {
  497. CClipFormat* pCF;
  498. DWORD dwCRC = 0xFFFFFFFF;
  499. CCrc32Dynamic *pCrc32 = new CCrc32Dynamic;
  500. if(pCrc32)
  501. {
  502. //Generate a CRC value for all copied data
  503. INT_PTR size = m_Formats.GetSize();
  504. for(int i = 0; i < size ; i++)
  505. {
  506. pCF = & m_Formats.ElementAt(i);
  507. const unsigned char *Data = (const unsigned char *)GlobalLock(pCF->m_hgData);
  508. if(Data)
  509. {
  510. pCrc32->GenerateCrc32((const LPBYTE)Data, (DWORD)GlobalSize(pCF->m_hgData), dwCRC);
  511. }
  512. GlobalUnlock(pCF->m_hgData);
  513. }
  514. dwCRC = ~dwCRC;
  515. delete pCrc32;
  516. }
  517. return dwCRC;
  518. }
  519. // assigns m_ID
  520. bool CClip::AddToMainTable()
  521. {
  522. try
  523. {
  524. m_Desc.Replace(_T("'"), _T("''"));
  525. m_csQuickPaste.Replace(_T("'"), _T("''"));
  526. CString cs;
  527. cs.Format(_T("INSERT into Main (lDate, mText, lShortCut, lDontAutoDelete, CRC, bIsGroup, lParentID, QuickPasteText, clipOrder, clipGroupOrder, globalShortCut, lastPasteDate, stickyClipOrder, stickyClipGroupOrder) ")
  528. _T("values(%d, '%s', %d, %d, %d, %d, %d, '%s', %f, %f, %d, %d, %f, %f);"),
  529. (int)m_Time.GetTime(),
  530. m_Desc,
  531. m_shortCut,
  532. m_dontAutoDelete,
  533. m_CRC,
  534. m_bIsGroup,
  535. m_parentId,
  536. m_csQuickPaste,
  537. m_clipOrder,
  538. m_clipGroupOrder,
  539. m_globalShortCut,
  540. (int)CTime::GetCurrentTime().GetTime(),
  541. m_stickyClipOrder,
  542. m_stickyClipGroupOrder);
  543. theApp.m_db.execDML(cs);
  544. m_id = (long)theApp.m_db.lastRowId();
  545. Log(StrF(_T("Added clip to main table, Id: %d, Desc: %s, Order: %f, GroupOrder: %f"), m_id, m_Desc, m_clipOrder, m_clipGroupOrder));
  546. m_LastAddedCRC = m_CRC;
  547. m_lastAddedID = m_id;
  548. }
  549. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  550. return true;
  551. }
  552. bool CClip::ModifyMainTable()
  553. {
  554. bool bRet = false;
  555. try
  556. {
  557. m_Desc.Replace(_T("'"), _T("''"));
  558. m_csQuickPaste.Replace(_T("'"), _T("''"));
  559. theApp.m_db.execDMLEx(_T("UPDATE Main SET lShortCut = %d, ")
  560. _T("mText = '%s', ")
  561. _T("lParentID = %d, ")
  562. _T("lDontAutoDelete = %d, ")
  563. _T("QuickPasteText = '%s', ")
  564. _T("clipOrder = %f, ")
  565. _T("clipGroupOrder = %f, ")
  566. _T("globalShortCut = %d, ")
  567. _T("stickyClipOrder = %f, ")
  568. _T("stickyClipGroupOrder = %f ")
  569. _T("WHERE lID = %d;"),
  570. m_shortCut,
  571. m_Desc,
  572. m_parentId,
  573. m_dontAutoDelete,
  574. m_csQuickPaste,
  575. m_clipOrder,
  576. m_clipGroupOrder,
  577. m_globalShortCut,
  578. m_stickyClipOrder,
  579. m_stickyClipGroupOrder,
  580. m_id);
  581. bRet = true;
  582. }
  583. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  584. return bRet;
  585. }
  586. // Empties m_Formats as it saves them to the Data Table.
  587. bool CClip::AddToDataTable()
  588. {
  589. CClipFormat* pCF;
  590. try
  591. {
  592. CppSQLite3Statement stmt = theApp.m_db.compileStatement(_T("insert into Data values (NULL, ?, ?, ?);"));
  593. for(INT_PTR i = m_Formats.GetSize()-1; i >= 0 ; i--)
  594. {
  595. pCF = &m_Formats.ElementAt(i);
  596. CString formatName = GetFormatName(pCF->m_cfType);
  597. int clipSize = 0;
  598. stmt.bind(1, m_id);
  599. stmt.bind(2, formatName);
  600. const unsigned char *Data = (const unsigned char *)GlobalLock(pCF->m_hgData);
  601. if(Data)
  602. {
  603. clipSize = (int)GlobalSize(pCF->m_hgData);
  604. stmt.bind(3, Data, clipSize);
  605. }
  606. GlobalUnlock(pCF->m_hgData);
  607. stmt.execDML();
  608. stmt.reset();
  609. pCF->m_dbId = (long)theApp.m_db.lastRowId();
  610. Log(StrF(_T("Added ClipData to DB, Id: %d, ParentId: %d Type: %s, size: %d"), pCF->m_dbId, m_id, formatName, clipSize));
  611. }
  612. }
  613. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  614. return true;
  615. }
  616. void CClip::MoveUp()
  617. {
  618. if(m_stickyClipOrder == 0)
  619. {
  620. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, clipOrder FROM Main Where clipOrder > %f ORDER BY clipOrder ASC LIMIT 1"), m_clipOrder);
  621. if (q.eof() == false)
  622. {
  623. int idAbove = q.getIntField(_T("lID"));
  624. double orderAbove = q.getFloatField(_T("clipOrder"));
  625. CppSQLite3Query q2 = theApp.m_db.execQueryEx(_T("SELECT lID, clipOrder FROM Main Where clipOrder > %f ORDER BY clipOrder ASC LIMIT 1"), orderAbove);
  626. if (q2.eof() == false)
  627. {
  628. int idTwoAbove = q2.getIntField(_T("lID"));
  629. double orderTwoAbove = q2.getFloatField(_T("clipOrder"));
  630. m_clipOrder = orderAbove + (orderTwoAbove - orderAbove) / 2.0;
  631. }
  632. else
  633. {
  634. m_clipOrder = orderAbove + 1;
  635. }
  636. }
  637. }
  638. else
  639. {
  640. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, stickyClipOrder FROM Main Where stickyClipOrder > %f ORDER BY clipOrder ASC LIMIT 1"), m_stickyClipOrder);
  641. if (q.eof() == false)
  642. {
  643. int idAbove = q.getIntField(_T("lID"));
  644. double orderAbove = q.getFloatField(_T("stickyClipOrder"));
  645. CppSQLite3Query q2 = theApp.m_db.execQueryEx(_T("SELECT lID, stickyClipOrder FROM Main Where stickyClipOrder > %f ORDER BY clipOrder ASC LIMIT 1"), orderAbove);
  646. if (q2.eof() == false)
  647. {
  648. int idTwoAbove = q2.getIntField(_T("lID"));
  649. double orderTwoAbove = q2.getFloatField(_T("stickyClipOrder"));
  650. m_stickyClipOrder = orderAbove + (orderTwoAbove - orderAbove) / 2.0;
  651. }
  652. else
  653. {
  654. m_stickyClipOrder = orderAbove + 1;
  655. }
  656. }
  657. }
  658. }
  659. void CClip::MakeStickyTop(int parentId)
  660. {
  661. if (parentId < 0)
  662. {
  663. m_stickyClipOrder = GetNewTopSticky(parentId, m_id);
  664. }
  665. else
  666. {
  667. m_stickyClipGroupOrder = GetNewTopSticky(parentId, m_id);
  668. }
  669. }
  670. void CClip::MakeStickyLast(int parentId)
  671. {
  672. if (parentId < 0)
  673. {
  674. m_stickyClipOrder = GetNewLastSticky(parentId, m_id);
  675. }
  676. else
  677. {
  678. m_stickyClipGroupOrder = GetNewLastSticky(parentId, m_id);
  679. }
  680. }
  681. void CClip::RemoveStickySetting(int parentId)
  682. {
  683. if (parentId < 0)
  684. {
  685. m_stickyClipOrder = 0;
  686. }
  687. else
  688. {
  689. m_stickyClipGroupOrder = 0;
  690. }
  691. }
  692. double CClip::GetNewTopSticky(int parentId, int clipId)
  693. {
  694. double newOrder = 1;
  695. double existingMaxOrder = 0;
  696. CString existingDesc = _T("");
  697. try
  698. {
  699. if (parentId < 0)
  700. {
  701. CppSQLite3Query q = theApp.m_db.execQuery(_T("SELECT stickyClipOrder, mText FROM Main ORDER BY stickyClipOrder DESC LIMIT 1"));
  702. if (q.eof() == false)
  703. {
  704. existingMaxOrder = q.getFloatField(_T("stickyClipOrder"));
  705. existingDesc = q.getStringField(_T("mText"));
  706. newOrder = existingMaxOrder + 1;
  707. }
  708. }
  709. else
  710. {
  711. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT stickyClipGroupOrder, mText FROM Main WHERE lParentID = %d ORDER BY stickyClipGroupOrder DESC LIMIT 1"), parentId);
  712. if (q.eof() == false)
  713. {
  714. existingMaxOrder = q.getFloatField(_T("stickyClipGroupOrder"));
  715. newOrder = existingMaxOrder + 1;
  716. }
  717. }
  718. if (newOrder == 0.0)
  719. newOrder += 1;
  720. Log(StrF(_T("GetNewTopSticky, Id: %d, parentId: %d, CurrentMax: %f, CurrentDesc: %s, NewMax: %f"), clipId, parentId, existingMaxOrder, existingDesc, newOrder));
  721. }
  722. CATCH_SQLITE_EXCEPTION
  723. return newOrder;
  724. }
  725. double CClip::GetNewLastSticky(int parentId, int clipId)
  726. {
  727. double newOrder = 1;
  728. double existingMaxOrder = 0;
  729. CString existingDesc = _T("");
  730. try
  731. {
  732. if (parentId < 0)
  733. {
  734. CppSQLite3Query q = theApp.m_db.execQuery(_T("SELECT stickyClipOrder, mText FROM Main ORDER BY stickyClipOrder LIMIT 1"));
  735. if (q.eof() == false)
  736. {
  737. existingMaxOrder = q.getFloatField(_T("stickyClipOrder"));
  738. existingDesc = q.getStringField(_T("mText"));
  739. newOrder = existingMaxOrder - 1;
  740. }
  741. }
  742. else
  743. {
  744. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT stickyClipGroupOrder, mText FROM Main WHERE lParentID = %d ORDER BY stickyClipGroupOrder LIMIT 1"), parentId);
  745. if (q.eof() == false)
  746. {
  747. existingMaxOrder = q.getFloatField(_T("stickyClipGroupOrder"));
  748. newOrder = existingMaxOrder - 1;
  749. }
  750. }
  751. if (newOrder == 0.0)
  752. newOrder -= 1;
  753. Log(StrF(_T("GetNewLastSticky, Id: %d, parentId: %d, CurrentMax: %f, CurrentDesc: %s, NewMax: %f"), clipId, parentId, existingMaxOrder, existingDesc, newOrder));
  754. }
  755. CATCH_SQLITE_EXCEPTION
  756. return newOrder;
  757. }
  758. void CClip::MakeLatestOrder()
  759. {
  760. m_clipOrder = GetNewOrder(-1, m_id);
  761. }
  762. double CClip::GetNewOrder(int parentId, int clipId)
  763. {
  764. double newOrder = 0;
  765. double existingMaxOrder = 0;
  766. CString existingDesc = _T("");
  767. try
  768. {
  769. if(parentId < 0)
  770. {
  771. CppSQLite3Query q = theApp.m_db.execQuery(_T("SELECT clipOrder, mText FROM Main ORDER BY clipOrder DESC LIMIT 1"));
  772. if(q.eof() == false)
  773. {
  774. existingMaxOrder = q.getFloatField(_T("clipOrder"));
  775. existingDesc = q.getStringField(_T("mText"));
  776. newOrder = existingMaxOrder + 1;
  777. }
  778. }
  779. else
  780. {
  781. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT clipGroupOrder, mText FROM Main WHERE lParentID = %d ORDER BY clipOrder DESC LIMIT 1"), parentId);
  782. if(q.eof() == false)
  783. {
  784. existingMaxOrder = q.getFloatField(_T("clipGroupOrder"));
  785. newOrder = existingMaxOrder + 1;
  786. }
  787. }
  788. Log(StrF(_T("GetNewOrder, Id: %d, parentId: %d, CurrentMax: %f, CurrentDesc: %s, NewMax: %f"), clipId, parentId, existingMaxOrder, existingDesc, newOrder));
  789. }
  790. CATCH_SQLITE_EXCEPTION
  791. return newOrder;
  792. }
  793. BOOL CClip::LoadMainTable(int id)
  794. {
  795. bool bRet = false;
  796. try
  797. {
  798. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT * FROM Main WHERE lID = %d"), id);
  799. if(q.eof() == false)
  800. {
  801. m_Time = q.getIntField(_T("lDate"));
  802. m_Desc = q.getStringField(_T("mText"));
  803. m_CRC = q.getIntField(_T("CRC"));
  804. m_parentId = q.getIntField(_T("lParentID"));
  805. m_dontAutoDelete = q.getIntField(_T("lDontAutoDelete"));
  806. m_shortCut = q.getIntField(_T("lShortCut"));
  807. m_bIsGroup = q.getIntField(_T("bIsGroup"));
  808. m_csQuickPaste = q.getStringField(_T("QuickPasteText"));
  809. m_clipOrder = q.getFloatField(_T("clipOrder"));
  810. m_clipGroupOrder = q.getFloatField(_T("clipGroupOrder"));
  811. m_globalShortCut = q.getIntField(_T("globalShortCut"));
  812. m_lastPasteDate = q.getIntField(_T("lastPasteDate"));
  813. m_stickyClipOrder = q.getFloatField(_T("stickyClipOrder"));
  814. m_stickyClipGroupOrder = q.getFloatField(_T("stickyClipGroupOrder"));
  815. m_id = id;
  816. bRet = true;
  817. }
  818. }
  819. CATCH_SQLITE_EXCEPTION_AND_RETURN(FALSE)
  820. return bRet;
  821. }
  822. // STATICS
  823. // Allocates a Global containing the requested Clip Format Data
  824. HGLOBAL CClip::LoadFormat(int id, UINT cfType)
  825. {
  826. HGLOBAL hGlobal = 0;
  827. try
  828. {
  829. CString csSQL;
  830. csSQL.Format(
  831. _T("SELECT Data.ooData FROM Data ")
  832. _T("INNER JOIN Main ON Main.lID = Data.lParentID ")
  833. _T("WHERE Main.lID = %d ")
  834. _T("AND Data.strClipBoardFormat = \'%s\'"),
  835. id,
  836. GetFormatName(cfType));
  837. CppSQLite3Query q = theApp.m_db.execQuery(csSQL);
  838. if(q.eof() == false)
  839. {
  840. int nDataLen = 0;
  841. const unsigned char *cData = q.getBlobField(0, nDataLen);
  842. if(cData == NULL)
  843. {
  844. return false;
  845. }
  846. hGlobal = NewGlobalP((LPVOID)cData, nDataLen);
  847. }
  848. }
  849. CATCH_SQLITE_EXCEPTION
  850. return hGlobal;
  851. }
  852. bool CClip::LoadFormats(int id, bool bOnlyLoad_CF_TEXT)
  853. {
  854. DWORD startTick = GetTickCount();
  855. CClipFormat cf;
  856. HGLOBAL hGlobal = 0;
  857. m_Formats.RemoveAll();
  858. try
  859. {
  860. //Open the data table for all that have the parent id
  861. //Order by Data.lID so that when generating CRC it's always in the same order as the first time
  862. //we generated it
  863. CString csSQL;
  864. csSQL.Format(
  865. _T("SELECT Data.lID, strClipBoardFormat, ooData FROM Data ")
  866. _T("INNER JOIN Main ON Main.lID = Data.lParentID ")
  867. _T("WHERE Main.lID = %d ORDER BY Data.lID desc"), id);
  868. CppSQLite3Query q = theApp.m_db.execQuery(csSQL);
  869. while(q.eof() == false)
  870. {
  871. cf.m_dbId = q.getIntField(_T("lID"));
  872. cf.m_cfType = GetFormatID(q.getStringField(_T("strClipBoardFormat")));
  873. if(bOnlyLoad_CF_TEXT)
  874. {
  875. if(cf.m_cfType != CF_TEXT && cf.m_cfType != CF_UNICODETEXT)
  876. {
  877. q.nextRow();
  878. continue;
  879. }
  880. }
  881. int nDataLen = 0;
  882. const unsigned char *cData = q.getBlobField(_T("ooData"), nDataLen);
  883. if(cData != NULL)
  884. {
  885. hGlobal = NewGlobalP((LPVOID)cData, nDataLen);
  886. }
  887. cf.m_hgData = hGlobal;
  888. m_Formats.Add(cf);
  889. q.nextRow();
  890. }
  891. // formats owns all the data
  892. cf.m_hgData = 0;
  893. }
  894. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  895. DWORD endTick = GetTickCount();
  896. if((endTick-startTick) > 150)
  897. Log(StrF(_T("Paste Timing LoadFormats: %d, ClipId: %d"), endTick-startTick, id));
  898. return m_Formats.GetSize() > 0;
  899. }
  900. void CClip::LoadTypes(int id, CClipTypes& types)
  901. {
  902. types.RemoveAll();
  903. try
  904. {
  905. CString csSQL;
  906. // get formats for Clip "lID" (Main.lID) using the corresponding Main.lDataID
  907. //Order by Data.lID so that when generating CRC it's always in the same order as the first time
  908. //we generated it
  909. csSQL.Format(
  910. _T("SELECT strClipBoardFormat FROM Data ")
  911. _T("INNER JOIN Main ON Main.lID = Data.lParentID ")
  912. _T("WHERE Main.lID = %d ORDER BY Data.lID desc"), id);
  913. CppSQLite3Query q = theApp.m_db.execQuery(csSQL);
  914. while(q.eof() == false)
  915. {
  916. types.Add(GetFormatID(q.getStringField(0)));
  917. q.nextRow();
  918. }
  919. }
  920. CATCH_SQLITE_EXCEPTION
  921. }
  922. bool CClip::SaveFromEditWnd(BOOL bUpdateDesc)
  923. {
  924. bool bRet = false;
  925. try
  926. {
  927. theApp.m_db.execDMLEx(_T("DELETE FROM Data WHERE lParentID = %d;"), m_id);
  928. DWORD CRC = GenerateCRC();
  929. AddToDataTable();
  930. theApp.m_db.execDMLEx(_T("UPDATE Main SET CRC = %d WHERE lID = %d"), CRC, m_id);
  931. if(bUpdateDesc)
  932. {
  933. m_Desc.Replace(_T("'"), _T("''"));
  934. theApp.m_db.execDMLEx(_T("UPDATE Main SET mText = '%s' WHERE lID = %d"), m_Desc, m_id);
  935. }
  936. bRet = true;
  937. }
  938. CATCH_SQLITE_EXCEPTION
  939. return bRet;
  940. }
  941. /*----------------------------------------------------------------------------*\
  942. CClipList
  943. \*----------------------------------------------------------------------------*/
  944. CClipList::~CClipList()
  945. {
  946. CClip* pClip;
  947. while(GetCount())
  948. {
  949. pClip = RemoveHead();
  950. delete pClip;
  951. }
  952. }
  953. // returns the number of clips actually saved
  954. // while this does empty the Format Data, it does not delete the Clips.
  955. int CClipList::AddToDB(bool bLatestOrder)
  956. {
  957. Log(_T("AddToDB - Start"));
  958. int savedCount = 0;
  959. CClip* pClip;
  960. POSITION pos;
  961. bool bResult;
  962. INT_PTR remaining = GetCount();
  963. pos = GetHeadPosition();
  964. while(pos)
  965. {
  966. Log(StrF(_T("AddToDB - while(pos), Start Remaining %d"), remaining));
  967. remaining--;
  968. pClip = GetNext(pos);
  969. ASSERT(pClip);
  970. if(bLatestOrder)
  971. {
  972. pClip->MakeLatestOrder();
  973. }
  974. pClip->m_Time = CTime::GetCurrentTime().GetTime();
  975. bResult = pClip->AddToDB();
  976. if(bResult)
  977. {
  978. savedCount++;
  979. }
  980. Log(StrF(_T("AddToDB - while(pos), End Remaining %d, save count: %d"), remaining, savedCount));
  981. }
  982. Log(StrF(_T("AddToDB - Start, count: %d"), savedCount));
  983. return savedCount;
  984. }
  985. const CClipList& CClipList::operator=(const CClipList &cliplist)
  986. {
  987. POSITION pos;
  988. CClip* pClip;
  989. pos = cliplist.GetHeadPosition();
  990. while(pos)
  991. {
  992. pClip = cliplist.GetNext(pos);
  993. ASSERT(pClip);
  994. CClip *pNewClip = new CClip;
  995. if(pNewClip)
  996. {
  997. *pNewClip = *pClip;
  998. AddTail(pNewClip);
  999. }
  1000. }
  1001. return *this;
  1002. }