Clip.cpp 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  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. CString textFilter = _T("");
  865. if(bOnlyLoad_CF_TEXT)
  866. {
  867. textFilter = _T("(strClipBoardFormat = 'CF_TEXT' OR strClipBoardFormat = 'CF_UNICODETEXT') AND ");
  868. }
  869. csSQL.Format(
  870. _T("SELECT lParentID, strClipBoardFormat, ooData FROM Data ")
  871. _T("WHERE %s lParentID = %d ORDER BY Data.lID desc"), textFilter, id);
  872. CppSQLite3Query q = theApp.m_db.execQuery(csSQL);
  873. while(q.eof() == false)
  874. {
  875. cf.m_dbId = q.getIntField(_T("lParentID"));
  876. cf.m_cfType = GetFormatID(q.getStringField(_T("strClipBoardFormat")));
  877. if(bOnlyLoad_CF_TEXT)
  878. {
  879. if(cf.m_cfType != CF_TEXT && cf.m_cfType != CF_UNICODETEXT)
  880. {
  881. q.nextRow();
  882. continue;
  883. }
  884. }
  885. int nDataLen = 0;
  886. const unsigned char *cData = q.getBlobField(_T("ooData"), nDataLen);
  887. if(cData != NULL)
  888. {
  889. hGlobal = NewGlobalP((LPVOID)cData, nDataLen);
  890. }
  891. cf.m_hgData = hGlobal;
  892. m_Formats.Add(cf);
  893. q.nextRow();
  894. }
  895. // formats owns all the data
  896. cf.m_hgData = 0;
  897. }
  898. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  899. DWORD endTick = GetTickCount();
  900. if((endTick-startTick) > 150)
  901. Log(StrF(_T("Paste Timing LoadFormats: %d, ClipId: %d"), endTick-startTick, id));
  902. return m_Formats.GetSize() > 0;
  903. }
  904. void CClip::LoadTypes(int id, CClipTypes& types)
  905. {
  906. types.RemoveAll();
  907. try
  908. {
  909. CString csSQL;
  910. // get formats for Clip "lID" (Main.lID) using the corresponding Main.lDataID
  911. //Order by Data.lID so that when generating CRC it's always in the same order as the first time
  912. //we generated it
  913. csSQL.Format(
  914. _T("SELECT strClipBoardFormat FROM Data ")
  915. _T("INNER JOIN Main ON Main.lID = Data.lParentID ")
  916. _T("WHERE Main.lID = %d ORDER BY Data.lID desc"), id);
  917. CppSQLite3Query q = theApp.m_db.execQuery(csSQL);
  918. while(q.eof() == false)
  919. {
  920. types.Add(GetFormatID(q.getStringField(0)));
  921. q.nextRow();
  922. }
  923. }
  924. CATCH_SQLITE_EXCEPTION
  925. }
  926. bool CClip::SaveFromEditWnd(BOOL bUpdateDesc)
  927. {
  928. bool bRet = false;
  929. try
  930. {
  931. theApp.m_db.execDMLEx(_T("DELETE FROM Data WHERE lParentID = %d;"), m_id);
  932. DWORD CRC = GenerateCRC();
  933. AddToDataTable();
  934. theApp.m_db.execDMLEx(_T("UPDATE Main SET CRC = %d WHERE lID = %d"), CRC, m_id);
  935. if(bUpdateDesc)
  936. {
  937. m_Desc.Replace(_T("'"), _T("''"));
  938. theApp.m_db.execDMLEx(_T("UPDATE Main SET mText = '%s' WHERE lID = %d"), m_Desc, m_id);
  939. }
  940. bRet = true;
  941. }
  942. CATCH_SQLITE_EXCEPTION
  943. return bRet;
  944. }
  945. CStringW CClip::GetUnicodeTextFormat()
  946. {
  947. IClipFormat *pFormat = this->Clips()->FindFormatEx(CF_UNICODETEXT);
  948. if(pFormat != NULL)
  949. {
  950. wchar_t *stringData = (wchar_t *)GlobalLock(pFormat->Data());
  951. if(stringData != NULL)
  952. {
  953. CStringW string(stringData);
  954. GlobalUnlock(pFormat->Data());
  955. return string;
  956. }
  957. }
  958. return _T("");
  959. }
  960. CStringA CClip::GetCFTextTextFormat()
  961. {
  962. IClipFormat *pFormat = this->Clips()->FindFormatEx(CF_TEXT);
  963. if(pFormat != NULL)
  964. {
  965. char *stringData = (char *)GlobalLock(pFormat->Data());
  966. if(stringData != NULL)
  967. {
  968. CStringA string(stringData);
  969. GlobalUnlock(pFormat->Data());
  970. return string;
  971. }
  972. }
  973. return _T("");
  974. }
  975. BOOL CClip::WriteTextToFile(CString path, BOOL unicode, BOOL asci, BOOL utf8)
  976. {
  977. BOOL ret = false;
  978. CFile f;
  979. if(f.Open(path, CFile::modeWrite|CFile::modeCreate))
  980. {
  981. CStringW w = GetUnicodeTextFormat();
  982. CStringA a = GetCFTextTextFormat();
  983. if(utf8 && w != _T(""))
  984. {
  985. CStringA convToUtf8;
  986. CTextConvert::ConvertToUTF8(w, convToUtf8);
  987. byte header[2];
  988. header[0] = 0xEF;
  989. header[1] = 0xBB;
  990. f.Write(&header, 2);
  991. f.Write(convToUtf8.GetBuffer(), convToUtf8.GetLength());
  992. ret = true;
  993. }
  994. else if(unicode && w != _T(""))
  995. {
  996. byte header[2];
  997. header[0] = 0xFF;
  998. header[1] = 0xFE;
  999. f.Write(&header, 2);
  1000. f.Write(w.GetBuffer(), w.GetLength() * sizeof(wchar_t));
  1001. ret = true;
  1002. }
  1003. else if(asci && a != _T(""))
  1004. {
  1005. f.Write(a.GetBuffer(), a.GetLength());
  1006. ret = true;
  1007. }
  1008. f.Close();
  1009. }
  1010. return ret;
  1011. }
  1012. /*----------------------------------------------------------------------------*\
  1013. CClipList
  1014. \*----------------------------------------------------------------------------*/
  1015. CClipList::~CClipList()
  1016. {
  1017. CClip* pClip;
  1018. while(GetCount())
  1019. {
  1020. pClip = RemoveHead();
  1021. delete pClip;
  1022. }
  1023. }
  1024. // returns the number of clips actually saved
  1025. // while this does empty the Format Data, it does not delete the Clips.
  1026. int CClipList::AddToDB(bool bLatestOrder)
  1027. {
  1028. Log(_T("AddToDB - Start"));
  1029. int savedCount = 0;
  1030. CClip* pClip;
  1031. POSITION pos;
  1032. bool bResult;
  1033. INT_PTR remaining = GetCount();
  1034. pos = GetHeadPosition();
  1035. while(pos)
  1036. {
  1037. Log(StrF(_T("AddToDB - while(pos), Start Remaining %d"), remaining));
  1038. remaining--;
  1039. pClip = GetNext(pos);
  1040. ASSERT(pClip);
  1041. if(bLatestOrder)
  1042. {
  1043. pClip->MakeLatestOrder();
  1044. }
  1045. pClip->m_Time = CTime::GetCurrentTime().GetTime();
  1046. bResult = pClip->AddToDB();
  1047. if(bResult)
  1048. {
  1049. savedCount++;
  1050. }
  1051. Log(StrF(_T("AddToDB - while(pos), End Remaining %d, save count: %d"), remaining, savedCount));
  1052. }
  1053. Log(StrF(_T("AddToDB - Start, count: %d"), savedCount));
  1054. return savedCount;
  1055. }
  1056. const CClipList& CClipList::operator=(const CClipList &cliplist)
  1057. {
  1058. POSITION pos;
  1059. CClip* pClip;
  1060. pos = cliplist.GetHeadPosition();
  1061. while(pos)
  1062. {
  1063. pClip = cliplist.GetNext(pos);
  1064. ASSERT(pClip);
  1065. CClip *pNewClip = new CClip;
  1066. if(pNewClip)
  1067. {
  1068. *pNewClip = *pClip;
  1069. AddTail(pNewClip);
  1070. }
  1071. }
  1072. return *this;
  1073. }