Clip.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512
  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 "Misc.h"
  13. #include <Mmsystem.h>
  14. #include "Path.h"
  15. #ifdef _DEBUG
  16. #undef THIS_FILE
  17. static char THIS_FILE[]=__FILE__;
  18. #define new DEBUG_NEW
  19. #endif
  20. /*----------------------------------------------------------------------------*\
  21. COleDataObjectEx
  22. \*----------------------------------------------------------------------------*/
  23. HGLOBAL COleDataObjectEx::GetGlobalData(CLIPFORMAT cfFormat, LPFORMATETC lpFormatEtc)
  24. {
  25. HGLOBAL hGlobal = COleDataObject::GetGlobalData(cfFormat, lpFormatEtc);
  26. if(hGlobal)
  27. {
  28. if(!::IsValid(hGlobal))
  29. {
  30. Log( StrF(
  31. _T("COleDataObjectEx::GetGlobalData(\"%s\"): ERROR: Invalid (NULL) data returned."),
  32. GetFormatName(cfFormat) ) );
  33. ::GlobalFree( hGlobal );
  34. hGlobal = NULL;
  35. }
  36. return hGlobal;
  37. }
  38. // The data isn't in global memory, so try getting an IStream interface to it.
  39. STGMEDIUM stg;
  40. if(!GetData(cfFormat, &stg))
  41. {
  42. return 0;
  43. }
  44. switch(stg.tymed)
  45. {
  46. case TYMED_HGLOBAL:
  47. hGlobal = stg.hGlobal;
  48. break;
  49. case TYMED_ISTREAM:
  50. {
  51. UINT uDataSize;
  52. LARGE_INTEGER li;
  53. ULARGE_INTEGER uli;
  54. li.HighPart = li.LowPart = 0;
  55. if ( SUCCEEDED( stg.pstm->Seek ( li, STREAM_SEEK_END, &uli )))
  56. {
  57. hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, uli.LowPart );
  58. void* pv = GlobalLock(hGlobal);
  59. stg.pstm->Seek(li, STREAM_SEEK_SET, NULL);
  60. HRESULT result = stg.pstm->Read(pv, uli.LowPart, (PULONG)&uDataSize);
  61. GlobalUnlock(hGlobal);
  62. if( FAILED(result) )
  63. hGlobal = GlobalFree(hGlobal);
  64. }
  65. break; // case TYMED_ISTREAM
  66. }
  67. } // end switch
  68. ReleaseStgMedium(&stg);
  69. if(hGlobal && !::IsValid(hGlobal))
  70. {
  71. Log( StrF(
  72. _T("COleDataObjectEx::GetGlobalData(\"%s\"): ERROR: Invalid (NULL) data returned."),
  73. GetFormatName(cfFormat)));
  74. ::GlobalFree(hGlobal);
  75. hGlobal = NULL;
  76. }
  77. return hGlobal;
  78. }
  79. /*----------------------------------------------------------------------------*\
  80. CClipFormat - holds the data of one clip format.
  81. \*----------------------------------------------------------------------------*/
  82. CClipFormat::CClipFormat(CLIPFORMAT cfType, HGLOBAL hgData, int parentId)
  83. {
  84. m_cfType = cfType;
  85. m_hgData = hgData;
  86. m_autoDeleteData = true;
  87. m_parentId = parentId;
  88. }
  89. CClipFormat::~CClipFormat()
  90. {
  91. Free();
  92. }
  93. void CClipFormat::Clear()
  94. {
  95. m_cfType = 0;
  96. m_hgData = 0;
  97. m_dataId = -1;
  98. m_parentId = -1;
  99. }
  100. void CClipFormat::Free()
  101. {
  102. if(m_autoDeleteData)
  103. {
  104. if(m_hgData)
  105. {
  106. m_hgData = ::GlobalFree( m_hgData );
  107. m_hgData = NULL;
  108. }
  109. }
  110. }
  111. /*----------------------------------------------------------------------------*\
  112. CClipFormats - holds an array of CClipFormat
  113. \*----------------------------------------------------------------------------*/
  114. // returns a pointer to the CClipFormat in this array which matches the given type
  115. // or NULL if that type doesn't exist in this array.
  116. CClipFormat* CClipFormats::FindFormat(UINT cfType)
  117. {
  118. CClipFormat* pCF;
  119. INT_PTR count = GetSize();
  120. for(int i=0; i < count; i++)
  121. {
  122. pCF = &ElementAt(i);
  123. if(pCF->m_cfType == cfType)
  124. return pCF;
  125. }
  126. return NULL;
  127. }
  128. /*----------------------------------------------------------------------------*\
  129. CClip - holds multiple CClipFormats and CopyClipboard() statistics
  130. \*----------------------------------------------------------------------------*/
  131. DWORD CClip::m_LastAddedCRC = 0;
  132. int CClip::m_lastAddedID = -1;
  133. CClip::CClip() :
  134. m_id(0),
  135. m_CRC(0),
  136. m_parentId(-1),
  137. m_dontAutoDelete(FALSE),
  138. m_shortCut(0),
  139. m_bIsGroup(FALSE),
  140. m_param1(0),
  141. m_clipOrder(0),
  142. m_stickyClipOrder(INVALID_STICKY),
  143. m_stickyClipGroupOrder(INVALID_STICKY),
  144. m_clipGroupOrder(0),
  145. m_globalShortCut(FALSE),
  146. m_moveToGroupShortCut(0),
  147. m_globalMoveToGroupShortCut(FALSE)
  148. {
  149. m_copyReason = CopyReasonEnum::COPY_TO_UNKOWN;
  150. }
  151. CClip::~CClip()
  152. {
  153. EmptyFormats();
  154. }
  155. void CClip::Clear()
  156. {
  157. m_id = -1;
  158. m_Time = 0;
  159. m_Desc = "";
  160. m_CRC = 0;
  161. m_parentId = -1;
  162. m_dontAutoDelete = FALSE;
  163. m_shortCut = 0;
  164. m_bIsGroup = FALSE;
  165. m_csQuickPaste = "";
  166. m_param1 = 0;
  167. m_globalShortCut = FALSE;
  168. m_moveToGroupShortCut = 0;
  169. m_globalMoveToGroupShortCut = 0;
  170. EmptyFormats();
  171. }
  172. const CClip& CClip::operator=(const CClip &clip)
  173. {
  174. const CClipFormat* pCF;
  175. m_id = clip.m_id;
  176. m_Time = clip.m_Time;
  177. m_lastPasteDate = clip.m_lastPasteDate;
  178. m_CRC = clip.m_CRC;
  179. m_parentId = clip.m_parentId;
  180. m_dontAutoDelete = clip.m_dontAutoDelete;
  181. m_shortCut = clip.m_shortCut;
  182. m_bIsGroup = clip.m_bIsGroup;
  183. m_csQuickPaste = clip.m_csQuickPaste;
  184. m_moveToGroupShortCut = clip.m_moveToGroupShortCut;
  185. m_globalMoveToGroupShortCut = clip.m_globalMoveToGroupShortCut;
  186. INT_PTR nCount = clip.m_Formats.GetSize();
  187. for(int i = 0; i < nCount; i++)
  188. {
  189. pCF = &clip.m_Formats.GetData()[i];
  190. LPVOID pvData = GlobalLock(pCF->m_hgData);
  191. if(pvData)
  192. {
  193. AddFormat(pCF->m_cfType, pvData, (UINT)GlobalSize(pCF->m_hgData));
  194. }
  195. GlobalUnlock(pCF->m_hgData);
  196. }
  197. //Set this after since in could get the wrong description in AddFormat
  198. m_Desc = clip.m_Desc;
  199. return *this;
  200. }
  201. void CClip::EmptyFormats()
  202. {
  203. // free global memory in m_Formats
  204. for(INT_PTR i = m_Formats.GetSize()-1; i >= 0; i--)
  205. {
  206. m_Formats[i].Free();
  207. m_Formats.RemoveAt(i);
  208. }
  209. }
  210. // Adds a new Format to this Clip by copying the given data.
  211. bool CClip::AddFormat(CLIPFORMAT cfType, void* pData, UINT nLen)
  212. {
  213. ASSERT(pData && nLen);
  214. HGLOBAL hGlobal = ::NewGlobalP(pData, nLen);
  215. ASSERT(hGlobal);
  216. // update the Clip statistics
  217. m_Time = m_Time.GetCurrentTime();
  218. if(!SetDescFromText(hGlobal, true))
  219. SetDescFromType();
  220. CClipFormat format(cfType,hGlobal);
  221. CClipFormat *pFormat;
  222. pFormat = m_Formats.FindFormat(cfType);
  223. // if the format type already exists as part of this clip, replace the data
  224. if(pFormat)
  225. {
  226. pFormat->Free();
  227. pFormat->m_hgData = format.m_hgData;
  228. }
  229. else
  230. {
  231. m_Formats.Add(format);
  232. }
  233. format.m_hgData = 0; // now owned by m_Formats
  234. return true;
  235. }
  236. // Fills this CClip with the contents of the clipboard.
  237. bool CClip::LoadFromClipboard(CClipTypes* pClipTypes, bool checkClipboardIgnore)
  238. {
  239. COleDataObjectEx oleData;
  240. CClipTypes defaultTypes;
  241. CClipTypes* pTypes = pClipTypes;
  242. // m_Formats should be empty when this is called.
  243. ASSERT(m_Formats.GetSize() == 0);
  244. // If the data is supposed to be private, then return
  245. if(::IsClipboardFormatAvailable(theApp.m_cfIgnoreClipboard))
  246. {
  247. Log(_T("Clipboard ignore type is on the clipboard, skipping this clipboard change"));
  248. return false;
  249. }
  250. //If we are saving a multi paste then delay us connecting to the clipboard
  251. //to allow the ctrl-v to do a paste
  252. if(::IsClipboardFormatAvailable(theApp.m_cfDelaySavingData))
  253. {
  254. Log(_T("Delay clipboard type is on the clipboard, delaying 1500 ms to allow ctrl-v to work"));
  255. Sleep(1500);
  256. }
  257. //Attach to the clipboard
  258. if(!oleData.AttachClipboard())
  259. {
  260. Log(_T("failed to attache to clipboard, skipping this clipboard change"));
  261. ASSERT(0); // does this ever happen?
  262. return false;
  263. }
  264. oleData.EnsureClipboardObject();
  265. // if no types were given, get only the first (most important) type.
  266. // (subsequent types could be synthetic due to automatic type conversions)
  267. if(pTypes == NULL || pTypes->GetSize() == 0)
  268. {
  269. ASSERT(0); // this feature is not currently used... it is an error if it is.
  270. FORMATETC formatEtc;
  271. oleData.BeginEnumFormats();
  272. oleData.GetNextFormat(&formatEtc);
  273. defaultTypes.Add(formatEtc.cfFormat);
  274. pTypes = &defaultTypes;
  275. }
  276. m_Desc = "[Ditto Error] BAD DESCRIPTION";
  277. // Get Description String
  278. // NOTE: We make sure that the description always corresponds to the
  279. // data saved by using the exact same globalmem instance as the source
  280. // for both... i.e. we only fetch the description format type once.
  281. CClipFormat cfDesc;
  282. bool bIsDescSet = false;
  283. cfDesc.m_cfType = CF_UNICODETEXT;
  284. if(oleData.IsDataAvailable(cfDesc.m_cfType))
  285. {
  286. cfDesc.m_hgData = oleData.GetGlobalData(cfDesc.m_cfType);
  287. bIsDescSet = SetDescFromText(cfDesc.m_hgData, true);
  288. Log(StrF(_T("Tried to set description from cf_unicode text, Set: %d, Desc: [%s]"), bIsDescSet, m_Desc.Left(30)));
  289. }
  290. if(bIsDescSet == false)
  291. {
  292. cfDesc.m_cfType = CF_TEXT;
  293. if(oleData.IsDataAvailable(cfDesc.m_cfType))
  294. {
  295. cfDesc.m_hgData = oleData.GetGlobalData(cfDesc.m_cfType);
  296. bIsDescSet = SetDescFromText(cfDesc.m_hgData, false);
  297. Log(StrF(_T("Tried to set description from cf_text text, Set: %d, Desc: [%s]"), bIsDescSet, m_Desc.Left(30)));
  298. }
  299. }
  300. INT_PTR nSize;
  301. CClipFormat cf;
  302. INT_PTR numTypes = pTypes->GetSize();
  303. Log(StrF(_T("Begin enumerating over supported types, Count: %d"), numTypes));
  304. for(int i = 0; i < numTypes; i++)
  305. {
  306. cf.m_cfType = pTypes->ElementAt(i);
  307. BOOL bSuccess = false;
  308. Log(StrF(_T("Begin try and load type %s"), GetFormatName(cf.m_cfType)));
  309. // is this the description we already fetched?
  310. if(cf.m_cfType == cfDesc.m_cfType)
  311. {
  312. cf = cfDesc;
  313. cfDesc.m_hgData = 0; // cf owns it now (to go into m_Formats)
  314. }
  315. else if(!oleData.IsDataAvailable(cf.m_cfType))
  316. {
  317. Log(StrF(_T("End of load - Data is not available for type %s"), GetFormatName(cf.m_cfType)));
  318. continue;
  319. }
  320. else
  321. {
  322. cf.m_hgData = oleData.GetGlobalData(cf.m_cfType);
  323. }
  324. if(cf.m_hgData)
  325. {
  326. nSize = GlobalSize(cf.m_hgData);
  327. if(nSize > 0)
  328. {
  329. if(g_Opt.m_lMaxClipSizeInBytes > 0 && (int)nSize > g_Opt.m_lMaxClipSizeInBytes)
  330. {
  331. CString cs;
  332. cs.Format(_T("Maximum clip size reached max size = %d, clip size = %d"), g_Opt.m_lMaxClipSizeInBytes, nSize);
  333. Log(cs);
  334. oleData.Release();
  335. return false;
  336. }
  337. ASSERT(::IsValid(cf.m_hgData));
  338. m_Formats.Add(cf);
  339. bSuccess = true;
  340. }
  341. else
  342. {
  343. ASSERT(FALSE); // a valid GlobalMem with 0 size is strange
  344. cf.Free();
  345. Log(StrF(_T("Data length is 0 for type %s"), GetFormatName(cf.m_cfType)));
  346. }
  347. cf.m_hgData = 0; // m_Formats owns it now
  348. }
  349. Log(StrF(_T("End of load - type %s, Success: %d"), GetFormatName(cf.m_cfType), bSuccess));
  350. }
  351. Log(StrF(_T("End enumerating over supported types, Count: %d"), numTypes));
  352. m_Time = CTime::GetCurrentTime();
  353. if(!bIsDescSet)
  354. {
  355. SetDescFromType();
  356. Log(StrF(_T("Setting description from type, Desc: [%s]"), m_Desc.Left(30)));
  357. }
  358. // if the description was in a type that is not supported,
  359. //we have to free it since it wasn't added to m_Formats
  360. if(cfDesc.m_hgData)
  361. {
  362. cfDesc.Free();
  363. }
  364. oleData.Release();
  365. if(m_Formats.GetSize() == 0)
  366. {
  367. Log(_T("No clip types were in supported types array"));
  368. return false;
  369. }
  370. return true;
  371. }
  372. bool CClip::SetDescFromText(HGLOBAL hgData, bool unicode)
  373. {
  374. if(hgData == 0)
  375. return false;
  376. bool bRet = false;
  377. INT_PTR bufLen = 0;
  378. if(unicode)
  379. {
  380. TCHAR* text = (TCHAR *) GlobalLock(hgData);
  381. bufLen = GlobalSize(hgData);
  382. m_Desc = text;
  383. bRet = true;
  384. }
  385. else
  386. {
  387. char* text = (char *) GlobalLock(hgData);
  388. bufLen = GlobalSize(hgData);
  389. m_Desc = text;
  390. bRet = true;
  391. }
  392. if(bufLen > g_Opt.m_bDescTextSize)
  393. {
  394. m_Desc = m_Desc.Left(g_Opt.m_bDescTextSize);
  395. }
  396. //Unlock the data
  397. GlobalUnlock(hgData);
  398. return bRet;
  399. }
  400. bool CClip::SetDescFromType()
  401. {
  402. INT_PTR size = m_Formats.GetSize();
  403. if(size <= 0)
  404. {
  405. return false;
  406. }
  407. int nCF_HDROPIndex = -1;
  408. for(int i = 0; i < size; i++)
  409. {
  410. if(m_Formats[i].m_cfType == CF_HDROP)
  411. {
  412. nCF_HDROPIndex = i;
  413. }
  414. }
  415. if(nCF_HDROPIndex >= 0)
  416. {
  417. using namespace nsPath;
  418. HDROP drop = (HDROP)GlobalLock(m_Formats[nCF_HDROPIndex].m_hgData);
  419. int nNumFiles = min(5, DragQueryFile(drop, -1, NULL, 0));
  420. if(nNumFiles > 1)
  421. m_Desc = "Copied Files - ";
  422. else
  423. m_Desc = "Copied File - ";
  424. TCHAR file[MAX_PATH];
  425. for(int nFile = 0; nFile < nNumFiles; nFile++)
  426. {
  427. if(DragQueryFile(drop, nFile, file, sizeof(file)) > 0)
  428. {
  429. CPath path(file);
  430. m_Desc += path.GetName();
  431. m_Desc += " - ";
  432. m_Desc += file;
  433. m_Desc += "\n";
  434. }
  435. }
  436. GlobalUnlock(m_Formats[nCF_HDROPIndex].m_hgData);
  437. }
  438. else
  439. {
  440. m_Desc = GetFormatName(m_Formats[0].m_cfType);
  441. }
  442. return m_Desc.GetLength() > 0;
  443. }
  444. bool CClip::AddToDB(bool bCheckForDuplicates)
  445. {
  446. bool bResult;
  447. try
  448. {
  449. m_CRC = GenerateCRC();
  450. if(bCheckForDuplicates &&
  451. m_parentId < 0)
  452. {
  453. int nID = FindDuplicate();
  454. if(nID >= 0)
  455. {
  456. MakeLatestOrder();
  457. MakeLatestGroupOrder();
  458. CString sql;
  459. sql.Format(_T("UPDATE Main SET clipOrder = %f, lastPasteDate = %d where lID = %d;"),
  460. m_clipOrder, (int)CTime::GetCurrentTime().GetTime(), nID);
  461. int ret = theApp.m_db.execDML(sql);
  462. int groupRet = -1;
  463. if(m_parentId > -1)
  464. {
  465. sql.Format(_T("UPDATE Main SET clipGroupOrder = %f where lID = %d;"),
  466. m_clipGroupOrder, nID);
  467. groupRet = theApp.m_db.execDML(sql);
  468. }
  469. m_id = nID;
  470. Log(StrF(_T("Found duplicate clip in db, Id: %d, ParentId: %d crc: %d, NewOrder: %f, GroupOrder %f, Ret: %d, GroupRet: %d, SQL: %s"),
  471. nID, m_parentId, m_CRC, m_clipOrder, m_clipGroupOrder, ret, groupRet, sql));
  472. return true;
  473. }
  474. }
  475. }
  476. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  477. bResult = false;
  478. if(AddToMainTable())
  479. {
  480. bResult = AddToDataTable();
  481. }
  482. if(bResult)
  483. {
  484. if(g_Opt.m_csPlaySoundOnCopy.IsEmpty() == FALSE)
  485. PlaySound(g_Opt.m_csPlaySoundOnCopy, NULL, SND_FILENAME|SND_ASYNC);
  486. }
  487. // should be emptied by AddToDataTable
  488. //ASSERT(m_Formats.GetSize() == 0);
  489. return bResult;
  490. }
  491. // if a duplicate exists, set recset to the duplicate and return true
  492. int CClip::FindDuplicate()
  493. {
  494. try
  495. {
  496. //If they are allowing duplicates still check
  497. //the last copied item
  498. if(g_Opt.m_bAllowDuplicates)
  499. {
  500. if(m_CRC == m_LastAddedCRC)
  501. return m_lastAddedID;
  502. }
  503. else
  504. {
  505. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID FROM Main WHERE CRC = %d"), m_CRC);
  506. if(q.eof() == false)
  507. {
  508. return q.getIntField(_T("lID"));
  509. }
  510. }
  511. }
  512. CATCH_SQLITE_EXCEPTION
  513. return -1;
  514. }
  515. DWORD CClip::GenerateCRC()
  516. {
  517. CClipFormat* pCF;
  518. DWORD dwCRC = 0xFFFFFFFF;
  519. CCrc32Dynamic *pCrc32 = new CCrc32Dynamic;
  520. if(pCrc32)
  521. {
  522. //Generate a CRC value for all copied data
  523. INT_PTR size = m_Formats.GetSize();
  524. for(int i = 0; i < size ; i++)
  525. {
  526. pCF = & m_Formats.ElementAt(i);
  527. const unsigned char *Data = (const unsigned char *)GlobalLock(pCF->m_hgData);
  528. if(Data)
  529. {
  530. pCrc32->GenerateCrc32((const LPBYTE)Data, (DWORD)GlobalSize(pCF->m_hgData), dwCRC);
  531. }
  532. GlobalUnlock(pCF->m_hgData);
  533. }
  534. dwCRC = ~dwCRC;
  535. delete pCrc32;
  536. }
  537. return dwCRC;
  538. }
  539. // assigns m_ID
  540. bool CClip::AddToMainTable()
  541. {
  542. try
  543. {
  544. m_Desc.Replace(_T("'"), _T("''"));
  545. m_csQuickPaste.Replace(_T("'"), _T("''"));
  546. CString cs;
  547. cs.Format(_T("INSERT into Main (lDate, mText, lShortCut, lDontAutoDelete, CRC, bIsGroup, lParentID, QuickPasteText, clipOrder, clipGroupOrder, globalShortCut, lastPasteDate, stickyClipOrder, stickyClipGroupOrder, MoveToGroupShortCut, GlobalMoveToGroupShortCut) ")
  548. _T("values(%d, '%s', %d, %d, %d, %d, %d, '%s', %f, %f, %d, %d, %f, %f, %d, %d);"),
  549. (int)m_Time.GetTime(),
  550. m_Desc,
  551. m_shortCut,
  552. m_dontAutoDelete,
  553. m_CRC,
  554. m_bIsGroup,
  555. m_parentId,
  556. m_csQuickPaste,
  557. m_clipOrder,
  558. m_clipGroupOrder,
  559. m_globalShortCut,
  560. (int)CTime::GetCurrentTime().GetTime(),
  561. m_stickyClipOrder,
  562. m_stickyClipGroupOrder,
  563. m_moveToGroupShortCut,
  564. m_globalMoveToGroupShortCut);
  565. theApp.m_db.execDML(cs);
  566. m_id = (long)theApp.m_db.lastRowId();
  567. Log(StrF(_T("Added clip to main table, Id: %d, ParentId: %d Desc: %s, Order: %f, GroupOrder: %f"), m_id, m_parentId, m_Desc, m_clipOrder, m_clipGroupOrder));
  568. m_LastAddedCRC = m_CRC;
  569. m_lastAddedID = m_id;
  570. }
  571. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  572. return true;
  573. }
  574. bool CClip::ModifyMainTable()
  575. {
  576. bool bRet = false;
  577. try
  578. {
  579. m_Desc.Replace(_T("'"), _T("''"));
  580. m_csQuickPaste.Replace(_T("'"), _T("''"));
  581. theApp.m_db.execDMLEx(_T("UPDATE Main SET lShortCut = %d, ")
  582. _T("mText = '%s', ")
  583. _T("lParentID = %d, ")
  584. _T("lDontAutoDelete = %d, ")
  585. _T("QuickPasteText = '%s', ")
  586. _T("clipOrder = %f, ")
  587. _T("clipGroupOrder = %f, ")
  588. _T("globalShortCut = %d, ")
  589. _T("stickyClipOrder = %f, ")
  590. _T("stickyClipGroupOrder = %f, ")
  591. _T("MoveToGroupShortCut = %d, ")
  592. _T("GlobalMoveToGroupShortCut = %d ")
  593. _T("WHERE lID = %d;"),
  594. m_shortCut,
  595. m_Desc,
  596. m_parentId,
  597. m_dontAutoDelete,
  598. m_csQuickPaste,
  599. m_clipOrder,
  600. m_clipGroupOrder,
  601. m_globalShortCut,
  602. m_stickyClipOrder,
  603. m_stickyClipGroupOrder,
  604. m_moveToGroupShortCut,
  605. m_globalMoveToGroupShortCut,
  606. m_id);
  607. bRet = true;
  608. }
  609. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  610. return bRet;
  611. }
  612. // Empties m_Formats as it saves them to the Data Table.
  613. bool CClip::AddToDataTable()
  614. {
  615. CClipFormat* pCF;
  616. try
  617. {
  618. CppSQLite3Statement stmt = theApp.m_db.compileStatement(_T("insert into Data values (NULL, ?, ?, ?);"));
  619. for(INT_PTR i = m_Formats.GetSize()-1; i >= 0 ; i--)
  620. {
  621. pCF = &m_Formats.ElementAt(i);
  622. CString formatName = GetFormatName(pCF->m_cfType);
  623. int clipSize = 0;
  624. stmt.bind(1, m_id);
  625. stmt.bind(2, formatName);
  626. const unsigned char *Data = (const unsigned char *)GlobalLock(pCF->m_hgData);
  627. if(Data)
  628. {
  629. clipSize = (int)GlobalSize(pCF->m_hgData);
  630. stmt.bind(3, Data, clipSize);
  631. }
  632. GlobalUnlock(pCF->m_hgData);
  633. stmt.execDML();
  634. stmt.reset();
  635. pCF->m_dataId = (long)theApp.m_db.lastRowId();
  636. Log(StrF(_T("Added ClipData to DB, Id: %d, ParentId: %d Type: %s, size: %d"), pCF->m_dataId, m_id, formatName, clipSize));
  637. }
  638. }
  639. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  640. return true;
  641. }
  642. void CClip::MoveUp(int parentId)
  643. {
  644. try
  645. {
  646. //In a group, not a sticky
  647. if(parentId > -1 && m_stickyClipGroupOrder == INVALID_STICKY)
  648. {
  649. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, clipGroupOrder FROM Main Where lParentID = %d AND stickyClipGroupOrder == -(2147483647) AND clipGroupOrder > %f ORDER BY clipGroupOrder ASC LIMIT 1"), parentId, m_clipGroupOrder);
  650. if (q.eof() == false)
  651. {
  652. int idAbove = q.getIntField(_T("lID"));
  653. double orderAbove = q.getFloatField(_T("clipGroupOrder"));
  654. CppSQLite3Query q2 = theApp.m_db.execQueryEx(_T("SELECT lID, clipGroupOrder FROM Main Where lParentID = %d AND clipGroupOrder > %f AND stickyClipGroupOrder == -(2147483647) ORDER BY clipGroupOrder ASC LIMIT 1"), parentId, orderAbove);
  655. if (q2.eof() == false)
  656. {
  657. int idTwoAbove = q2.getIntField(_T("lID"));
  658. double orderTwoAbove = q2.getFloatField(_T("clipGroupOrder"));
  659. m_clipGroupOrder = orderAbove + (orderTwoAbove - orderAbove) / 2.0;
  660. }
  661. else
  662. {
  663. m_clipGroupOrder = orderAbove + 1;
  664. }
  665. }
  666. }
  667. // main group, not a sticky
  668. else if(parentId <= -1 && m_stickyClipOrder == INVALID_STICKY)
  669. {
  670. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, clipOrder FROM Main Where clipOrder > %f AND stickyClipOrder == -(2147483647) ORDER BY clipOrder ASC LIMIT 1"), m_clipOrder);
  671. if (q.eof() == false)
  672. {
  673. int idAbove = q.getIntField(_T("lID"));
  674. double orderAbove = q.getFloatField(_T("clipOrder"));
  675. CppSQLite3Query q2 = theApp.m_db.execQueryEx(_T("SELECT lID, clipOrder FROM Main Where clipOrder > %f AND stickyClipOrder == -(2147483647) ORDER BY clipOrder ASC LIMIT 1"), orderAbove);
  676. if (q2.eof() == false)
  677. {
  678. int idTwoAbove = q2.getIntField(_T("lID"));
  679. double orderTwoAbove = q2.getFloatField(_T("clipOrder"));
  680. m_clipOrder = orderAbove + (orderTwoAbove - orderAbove) / 2.0;
  681. }
  682. else
  683. {
  684. m_clipOrder = orderAbove + 1;
  685. }
  686. }
  687. }
  688. //In a group, a sticky clip
  689. else if(parentId > -1 && m_stickyClipGroupOrder != INVALID_STICKY)
  690. {
  691. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, stickyClipGroupOrder FROM Main Where lParentID = %d AND stickyClipGroupOrder <> -(2147483647) AND stickyClipGroupOrder > %f ORDER BY stickyClipGroupOrder ASC LIMIT 1"), parentId, m_stickyClipGroupOrder);
  692. if (q.eof() == false)
  693. {
  694. int idAbove = q.getIntField(_T("lID"));
  695. double orderAbove = q.getFloatField(_T("stickyClipGroupOrder"));
  696. CppSQLite3Query q2 = theApp.m_db.execQueryEx(_T("SELECT lID, stickyClipGroupOrder FROM Main Where lParentID = %d AND stickyClipGroupOrder <> -(2147483647) AND stickyClipGroupOrder > %f ORDER BY stickyClipGroupOrder ASC LIMIT 1"), parentId, orderAbove);
  697. if (q2.eof() == false)
  698. {
  699. int idTwoAbove = q2.getIntField(_T("lID"));
  700. double orderTwoAbove = q2.getFloatField(_T("stickyClipGroupOrder"));
  701. m_stickyClipGroupOrder = orderAbove + (orderTwoAbove - orderAbove) / 2.0;
  702. }
  703. else
  704. {
  705. m_stickyClipGroupOrder = orderAbove + 1;
  706. }
  707. }
  708. }
  709. //not in a group, a sticky clip
  710. else if(parentId <= -1 && m_stickyClipOrder != INVALID_STICKY)
  711. {
  712. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, stickyClipOrder FROM Main Where stickyClipOrder <> -(2147483647) AND stickyClipOrder > %f ORDER BY stickyClipOrder ASC LIMIT 1"), m_stickyClipOrder);
  713. if (q.eof() == false)
  714. {
  715. int idAbove = q.getIntField(_T("lID"));
  716. double orderAbove = q.getFloatField(_T("stickyClipOrder"));
  717. CppSQLite3Query q2 = theApp.m_db.execQueryEx(_T("SELECT lID, stickyClipOrder FROM Main Where stickyClipOrder <> -(2147483647) AND stickyClipOrder > %f ORDER BY stickyClipOrder ASC LIMIT 1"), orderAbove);
  718. if (q2.eof() == false)
  719. {
  720. int idTwoAbove = q2.getIntField(_T("lID"));
  721. double orderTwoAbove = q2.getFloatField(_T("stickyClipOrder"));
  722. m_stickyClipOrder = orderAbove + (orderTwoAbove - orderAbove) / 2.0;
  723. }
  724. else
  725. {
  726. m_stickyClipOrder = orderAbove + 1;
  727. }
  728. }
  729. }
  730. }
  731. CATCH_SQLITE_EXCEPTION
  732. }
  733. void CClip::MoveDown(int parentId)
  734. {
  735. try
  736. {
  737. //In a group, not a sticky
  738. if(parentId > -1 && m_stickyClipGroupOrder == INVALID_STICKY)
  739. {
  740. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, clipGroupOrder FROM Main Where lParentID = %d AND clipGroupOrder < %f AND stickyClipGroupOrder = -(2147483647) ORDER BY clipGroupOrder DESC LIMIT 1"), parentId, m_clipGroupOrder);
  741. if (q.eof() == false)
  742. {
  743. int idBelow = q.getIntField(_T("lID"));
  744. double orderBelow = q.getFloatField(_T("clipGroupOrder"));
  745. CppSQLite3Query q2 = theApp.m_db.execQueryEx(_T("SELECT lID, clipGroupOrder FROM Main Where lParentID = %d AND clipGroupOrder < %f AND stickyClipGroupOrder = -(2147483647) ORDER BY clipGroupOrder DESC LIMIT 1"), parentId, orderBelow);
  746. if (q2.eof() == false)
  747. {
  748. int idTwoBelow = q2.getIntField(_T("lID"));
  749. double orderTwoBelow = q2.getFloatField(_T("clipGroupOrder"));
  750. m_clipGroupOrder = orderBelow + (orderTwoBelow - orderBelow) / 2.0;
  751. }
  752. else
  753. {
  754. m_clipGroupOrder = orderBelow - 1;
  755. }
  756. }
  757. }
  758. // main group, not a sticky
  759. else if(parentId <= -1 && m_stickyClipOrder == INVALID_STICKY)
  760. {
  761. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, clipOrder FROM Main Where clipOrder < %f AND stickyClipOrder = -(2147483647) ORDER BY clipOrder DESC LIMIT 1"), m_clipOrder);
  762. if (q.eof() == false)
  763. {
  764. int idBelow = q.getIntField(_T("lID"));
  765. double orderBelow = q.getFloatField(_T("clipOrder"));
  766. CppSQLite3Query q2 = theApp.m_db.execQueryEx(_T("SELECT lID, clipOrder FROM Main Where clipOrder < %f AND stickyClipOrder = -(2147483647) ORDER BY clipOrder DESC LIMIT 1"), orderBelow);
  767. if (q2.eof() == false)
  768. {
  769. int idTwoBelow = q2.getIntField(_T("lID"));
  770. double orderTwoBelow = q2.getFloatField(_T("clipOrder"));
  771. m_clipOrder = orderBelow + (orderTwoBelow - orderBelow) / 2.0;
  772. }
  773. else
  774. {
  775. m_clipOrder = orderBelow - 1;
  776. }
  777. }
  778. }
  779. //In a group, a sticky clip
  780. else if(parentId > -1 && m_stickyClipGroupOrder != INVALID_STICKY)
  781. {
  782. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, stickyClipGroupOrder FROM Main Where lParentID = %d AND stickyClipGroupOrder <> -(2147483647) AND stickyClipGroupOrder < %f ORDER BY stickyClipGroupOrder DESC LIMIT 1"), parentId, m_stickyClipGroupOrder);
  783. if (q.eof() == false)
  784. {
  785. int idBelow = q.getIntField(_T("lID"));
  786. double orderBelow = q.getFloatField(_T("stickyClipGroupOrder"));
  787. CppSQLite3Query q2 = theApp.m_db.execQueryEx(_T("SELECT lID, stickyClipGroupOrder FROM Main Where lParentID = %d AND stickyClipGroupOrder <> -(2147483647) AND stickyClipGroupOrder < %f ORDER BY stickyClipGroupOrder DESC LIMIT 1"), parentId, orderBelow);
  788. if (q2.eof() == false)
  789. {
  790. int idTwoBelow = q2.getIntField(_T("lID"));
  791. double orderTwoBelow = q2.getFloatField(_T("stickyClipGroupOrder"));
  792. m_stickyClipGroupOrder = orderBelow + (orderTwoBelow - orderBelow) / 2.0;
  793. }
  794. else
  795. {
  796. m_stickyClipGroupOrder = orderBelow - 1;
  797. }
  798. }
  799. }
  800. //not in a group, a sticky clip
  801. else if(parentId <= -1 && m_stickyClipOrder != INVALID_STICKY)
  802. {
  803. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID, stickyClipOrder FROM Main Where stickyClipOrder <> -(2147483647) AND stickyClipOrder < %f ORDER BY stickyClipOrder DESC LIMIT 1"), m_stickyClipOrder);
  804. if (q.eof() == false)
  805. {
  806. int idBelow = q.getIntField(_T("lID"));
  807. double orderBelow = q.getFloatField(_T("stickyClipOrder"));
  808. CppSQLite3Query q2 = theApp.m_db.execQueryEx(_T("SELECT lID, stickyClipOrder FROM Main Where stickyClipOrder <> -(2147483647) AND stickyClipOrder < %f ORDER BY stickyClipOrder DESC LIMIT 1"), orderBelow);
  809. if (q2.eof() == false)
  810. {
  811. int idTwoBelow = q2.getIntField(_T("lID"));
  812. double orderTwoBelow = q2.getFloatField(_T("stickyClipOrder"));
  813. m_stickyClipOrder = orderBelow + (orderTwoBelow - orderBelow) / 2.0;
  814. }
  815. else
  816. {
  817. m_stickyClipOrder = orderBelow - 1;
  818. }
  819. }
  820. }
  821. }
  822. CATCH_SQLITE_EXCEPTION
  823. }
  824. void CClip::MakeStickyTop(int parentId)
  825. {
  826. if (parentId < 0)
  827. {
  828. m_stickyClipOrder = GetNewTopSticky(parentId, m_id);
  829. }
  830. else
  831. {
  832. m_stickyClipGroupOrder = GetNewTopSticky(parentId, m_id);
  833. }
  834. }
  835. void CClip::MakeStickyLast(int parentId)
  836. {
  837. if (parentId < 0)
  838. {
  839. m_stickyClipOrder = GetNewLastSticky(parentId, m_id);
  840. }
  841. else
  842. {
  843. m_stickyClipGroupOrder = GetNewLastSticky(parentId, m_id);
  844. }
  845. }
  846. void CClip::RemoveStickySetting(int parentId)
  847. {
  848. if (parentId < 0)
  849. {
  850. m_stickyClipOrder = INVALID_STICKY;
  851. }
  852. else
  853. {
  854. m_stickyClipGroupOrder = INVALID_STICKY;
  855. }
  856. }
  857. double CClip::GetNewTopSticky(int parentId, int clipId)
  858. {
  859. double newOrder = 1;
  860. double existingMaxOrder = 0;
  861. CString existingDesc = _T("");
  862. try
  863. {
  864. if (parentId < 0)
  865. {
  866. CppSQLite3Query q = theApp.m_db.execQuery(_T("SELECT stickyClipOrder, mText FROM Main WHERE stickyClipOrder <> -(2147483647) ORDER BY stickyClipOrder DESC LIMIT 1"));
  867. if (q.eof() == false)
  868. {
  869. existingMaxOrder = q.getFloatField(_T("stickyClipOrder"));
  870. existingDesc = q.getStringField(_T("mText"));
  871. newOrder = existingMaxOrder + 1;
  872. }
  873. }
  874. else
  875. {
  876. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT stickyClipGroupOrder, mText FROM Main WHERE lParentID = %d AND stickyClipGroupOrder <> -(2147483647) ORDER BY stickyClipGroupOrder DESC LIMIT 1"), parentId);
  877. if (q.eof() == false)
  878. {
  879. existingMaxOrder = q.getFloatField(_T("stickyClipGroupOrder"));
  880. newOrder = existingMaxOrder + 1;
  881. }
  882. }
  883. if (newOrder == 0.0)
  884. newOrder += 1;
  885. Log(StrF(_T("GetNewTopSticky, Id: %d, parentId: %d, CurrentMax: %f, CurrentDesc: %s, NewMax: %f"), clipId, parentId, existingMaxOrder, existingDesc, newOrder));
  886. }
  887. CATCH_SQLITE_EXCEPTION
  888. return newOrder;
  889. }
  890. double CClip::GetNewLastSticky(int parentId, int clipId)
  891. {
  892. double newOrder = 1;
  893. double existingMaxOrder = 0;
  894. CString existingDesc = _T("");
  895. try
  896. {
  897. if (parentId < 0)
  898. {
  899. CppSQLite3Query q = theApp.m_db.execQuery(_T("SELECT stickyClipOrder, mText FROM Main WHERE stickyClipOrder <> -(2147483647) ORDER BY stickyClipOrder LIMIT 1"));
  900. if (q.eof() == false)
  901. {
  902. existingMaxOrder = q.getFloatField(_T("stickyClipOrder"));
  903. existingDesc = q.getStringField(_T("mText"));
  904. newOrder = existingMaxOrder - 1;
  905. }
  906. }
  907. else
  908. {
  909. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT stickyClipGroupOrder, mText FROM Main WHERE lParentID = %d AND stickyClipGroupOrder <> -(2147483647) ORDER BY stickyClipGroupOrder LIMIT 1"), parentId);
  910. if (q.eof() == false)
  911. {
  912. existingMaxOrder = q.getFloatField(_T("stickyClipGroupOrder"));
  913. newOrder = existingMaxOrder - 1;
  914. }
  915. }
  916. if (newOrder == 0.0)
  917. newOrder -= 1;
  918. Log(StrF(_T("GetNewLastSticky, Id: %d, parentId: %d, CurrentMax: %f, CurrentDesc: %s, NewMax: %f"), clipId, parentId, existingMaxOrder, existingDesc, newOrder));
  919. }
  920. CATCH_SQLITE_EXCEPTION
  921. return newOrder;
  922. }
  923. void CClip::MakeLatestOrder()
  924. {
  925. m_clipOrder = GetNewOrder(-1, m_id);
  926. }
  927. void CClip::MakeLatestGroupOrder()
  928. {
  929. if(m_parentId > -1)
  930. {
  931. m_clipGroupOrder = GetNewOrder(m_parentId, m_id);
  932. }
  933. }
  934. double CClip::GetNewOrder(int parentId, int clipId)
  935. {
  936. double newOrder = 0;
  937. double existingMaxOrder = 0;
  938. CString existingDesc = _T("");
  939. try
  940. {
  941. if(parentId < 0)
  942. {
  943. CppSQLite3Query q = theApp.m_db.execQuery(_T("SELECT clipOrder, mText FROM Main ORDER BY clipOrder DESC LIMIT 1"));
  944. if(q.eof() == false)
  945. {
  946. existingMaxOrder = q.getFloatField(_T("clipOrder"));
  947. existingDesc = q.getStringField(_T("mText"));
  948. newOrder = existingMaxOrder + 1;
  949. }
  950. }
  951. else
  952. {
  953. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT clipGroupOrder, mText FROM Main WHERE lParentID = %d ORDER BY clipGroupOrder DESC LIMIT 1"), parentId);
  954. if(q.eof() == false)
  955. {
  956. existingMaxOrder = q.getFloatField(_T("clipGroupOrder"));
  957. newOrder = existingMaxOrder + 1;
  958. }
  959. }
  960. Log(StrF(_T("GetNewOrder, Id: %d, parentId: %d, CurrentMax: %f, CurrentDesc: %s, NewMax: %f"), clipId, parentId, existingMaxOrder, existingDesc, newOrder));
  961. }
  962. CATCH_SQLITE_EXCEPTION
  963. return newOrder;
  964. }
  965. BOOL CClip::LoadMainTable(int id)
  966. {
  967. bool bRet = false;
  968. try
  969. {
  970. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT * FROM Main WHERE lID = %d"), id);
  971. if(q.eof() == false)
  972. {
  973. m_Time = q.getIntField(_T("lDate"));
  974. m_Desc = q.getStringField(_T("mText"));
  975. m_CRC = q.getIntField(_T("CRC"));
  976. m_parentId = q.getIntField(_T("lParentID"));
  977. m_dontAutoDelete = q.getIntField(_T("lDontAutoDelete"));
  978. m_shortCut = q.getIntField(_T("lShortCut"));
  979. m_bIsGroup = q.getIntField(_T("bIsGroup"));
  980. m_csQuickPaste = q.getStringField(_T("QuickPasteText"));
  981. m_clipOrder = q.getFloatField(_T("clipOrder"));
  982. m_clipGroupOrder = q.getFloatField(_T("clipGroupOrder"));
  983. m_globalShortCut = q.getIntField(_T("globalShortCut"));
  984. m_lastPasteDate = q.getIntField(_T("lastPasteDate"));
  985. m_stickyClipOrder = q.getFloatField(_T("stickyClipOrder"));
  986. m_stickyClipGroupOrder = q.getFloatField(_T("stickyClipGroupOrder"));
  987. m_moveToGroupShortCut = q.getIntField(_T("MoveToGroupShortCut"));
  988. m_globalMoveToGroupShortCut = q.getIntField(_T("GlobalMoveToGroupShortCut"));
  989. m_id = id;
  990. bRet = true;
  991. }
  992. }
  993. CATCH_SQLITE_EXCEPTION_AND_RETURN(FALSE)
  994. return bRet;
  995. }
  996. // STATICS
  997. // Allocates a Global containing the requested Clip Format Data
  998. HGLOBAL CClip::LoadFormat(int id, UINT cfType)
  999. {
  1000. HGLOBAL hGlobal = 0;
  1001. try
  1002. {
  1003. CString csSQL;
  1004. csSQL.Format(
  1005. _T("SELECT Data.ooData FROM Data ")
  1006. _T("INNER JOIN Main ON Main.lID = Data.lParentID ")
  1007. _T("WHERE Main.lID = %d ")
  1008. _T("AND Data.strClipBoardFormat = \'%s\'"),
  1009. id,
  1010. GetFormatName(cfType));
  1011. CppSQLite3Query q = theApp.m_db.execQuery(csSQL);
  1012. if(q.eof() == false)
  1013. {
  1014. int nDataLen = 0;
  1015. const unsigned char *cData = q.getBlobField(0, nDataLen);
  1016. if(cData == NULL)
  1017. {
  1018. return false;
  1019. }
  1020. hGlobal = NewGlobalP((LPVOID)cData, nDataLen);
  1021. }
  1022. }
  1023. CATCH_SQLITE_EXCEPTION
  1024. return hGlobal;
  1025. }
  1026. bool CClip::LoadFormats(int id, bool bOnlyLoad_CF_TEXT, bool includeRichTextForTextOnly)
  1027. {
  1028. DWORD startTick = GetTickCount();
  1029. CClipFormat cf;
  1030. HGLOBAL hGlobal = 0;
  1031. m_Formats.RemoveAll();
  1032. try
  1033. {
  1034. //Open the data table for all that have the parent id
  1035. //Order by Data.lID so that when generating CRC it's always in the same order as the first time
  1036. //we generated it
  1037. CString csSQL;
  1038. CString textFilter = _T("");
  1039. if(bOnlyLoad_CF_TEXT)
  1040. {
  1041. textFilter = _T("(strClipBoardFormat = 'CF_TEXT' OR strClipBoardFormat = 'CF_UNICODETEXT' OR strClipBoardFormat = 'CF_HDROP'");
  1042. if(includeRichTextForTextOnly)
  1043. {
  1044. textFilter = textFilter + _T(" OR strClipBoardFormat = 'Rich Text Format') AND ");
  1045. }
  1046. else
  1047. {
  1048. textFilter = textFilter + _T(") AND ");
  1049. }
  1050. }
  1051. csSQL.Format(
  1052. _T("SELECT lID, lParentID, strClipBoardFormat, ooData FROM Data ")
  1053. _T("WHERE %s lParentID = %d ORDER BY Data.lID desc"), textFilter, id);
  1054. CppSQLite3Query q = theApp.m_db.execQuery(csSQL);
  1055. while(q.eof() == false)
  1056. {
  1057. cf.m_dataId = q.getIntField(_T("lID"));
  1058. cf.m_parentId = q.getIntField(_T("lParentID"));
  1059. cf.m_cfType = GetFormatID(q.getStringField(_T("strClipBoardFormat")));
  1060. if(bOnlyLoad_CF_TEXT)
  1061. {
  1062. if(cf.m_cfType != CF_TEXT &&
  1063. cf.m_cfType != CF_UNICODETEXT &&
  1064. cf.m_cfType != CF_HDROP &&
  1065. (cf.m_cfType != theApp.m_RTFFormat && !includeRichTextForTextOnly))
  1066. {
  1067. q.nextRow();
  1068. continue;
  1069. }
  1070. }
  1071. int nDataLen = 0;
  1072. const unsigned char *cData = q.getBlobField(_T("ooData"), nDataLen);
  1073. if(cData != NULL)
  1074. {
  1075. hGlobal = NewGlobalP((LPVOID)cData, nDataLen);
  1076. }
  1077. cf.m_hgData = hGlobal;
  1078. m_Formats.Add(cf);
  1079. q.nextRow();
  1080. }
  1081. // formats owns all the data
  1082. cf.m_hgData = 0;
  1083. }
  1084. CATCH_SQLITE_EXCEPTION_AND_RETURN(false)
  1085. DWORD endTick = GetTickCount();
  1086. if((endTick-startTick) > 150)
  1087. Log(StrF(_T("Paste Timing LoadFormats: %d, ClipId: %d"), endTick-startTick, id));
  1088. return m_Formats.GetSize() > 0;
  1089. }
  1090. void CClip::LoadTypes(int id, CClipTypes& types)
  1091. {
  1092. types.RemoveAll();
  1093. try
  1094. {
  1095. CString csSQL;
  1096. // get formats for Clip "lID" (Main.lID) using the corresponding Main.lDataID
  1097. //Order by Data.lID so that when generating CRC it's always in the same order as the first time
  1098. //we generated it
  1099. csSQL.Format(
  1100. _T("SELECT strClipBoardFormat FROM Data ")
  1101. _T("INNER JOIN Main ON Main.lID = Data.lParentID ")
  1102. _T("WHERE Main.lID = %d ORDER BY Data.lID desc"), id);
  1103. CppSQLite3Query q = theApp.m_db.execQuery(csSQL);
  1104. while(q.eof() == false)
  1105. {
  1106. types.Add(GetFormatID(q.getStringField(0)));
  1107. q.nextRow();
  1108. }
  1109. }
  1110. CATCH_SQLITE_EXCEPTION
  1111. }
  1112. bool CClip::SaveFromEditWnd(BOOL bUpdateDesc)
  1113. {
  1114. bool bRet = false;
  1115. try
  1116. {
  1117. theApp.m_db.execDMLEx(_T("DELETE FROM Data WHERE lParentID = %d;"), m_id);
  1118. DWORD CRC = GenerateCRC();
  1119. AddToDataTable();
  1120. theApp.m_db.execDMLEx(_T("UPDATE Main SET CRC = %d WHERE lID = %d"), CRC, m_id);
  1121. if(bUpdateDesc)
  1122. {
  1123. m_Desc.Replace(_T("'"), _T("''"));
  1124. theApp.m_db.execDMLEx(_T("UPDATE Main SET mText = '%s' WHERE lID = %d"), m_Desc, m_id);
  1125. }
  1126. bRet = true;
  1127. }
  1128. CATCH_SQLITE_EXCEPTION
  1129. return bRet;
  1130. }
  1131. CStringW CClip::GetUnicodeTextFormat()
  1132. {
  1133. IClipFormat *pFormat = this->Clips()->FindFormatEx(CF_UNICODETEXT);
  1134. if(pFormat != NULL)
  1135. {
  1136. wchar_t *stringData = (wchar_t *)GlobalLock(pFormat->Data());
  1137. if(stringData != NULL)
  1138. {
  1139. CStringW string(stringData);
  1140. GlobalUnlock(pFormat->Data());
  1141. return string;
  1142. }
  1143. }
  1144. return _T("");
  1145. }
  1146. CStringA CClip::GetCFTextTextFormat()
  1147. {
  1148. IClipFormat *pFormat = this->Clips()->FindFormatEx(CF_TEXT);
  1149. if(pFormat != NULL)
  1150. {
  1151. char *stringData = (char *)GlobalLock(pFormat->Data());
  1152. if(stringData != NULL)
  1153. {
  1154. CStringA string(stringData);
  1155. GlobalUnlock(pFormat->Data());
  1156. return string;
  1157. }
  1158. }
  1159. return _T("");
  1160. }
  1161. BOOL CClip::WriteTextToFile(CString path, BOOL unicode, BOOL asci, BOOL utf8)
  1162. {
  1163. BOOL ret = false;
  1164. CFile f;
  1165. if(f.Open(path, CFile::modeWrite|CFile::modeCreate))
  1166. {
  1167. CStringW w = GetUnicodeTextFormat();
  1168. CStringA a = GetCFTextTextFormat();
  1169. if(utf8 && w != _T(""))
  1170. {
  1171. CStringA convToUtf8;
  1172. CTextConvert::ConvertToUTF8(w, convToUtf8);
  1173. byte header[2];
  1174. header[0] = 0xEF;
  1175. header[1] = 0xBB;
  1176. f.Write(&header, 2);
  1177. f.Write(convToUtf8.GetBuffer(), convToUtf8.GetLength());
  1178. ret = true;
  1179. }
  1180. else if(unicode && w != _T(""))
  1181. {
  1182. byte header[2];
  1183. header[0] = 0xFF;
  1184. header[1] = 0xFE;
  1185. f.Write(&header, 2);
  1186. f.Write(w.GetBuffer(), w.GetLength() * sizeof(wchar_t));
  1187. ret = true;
  1188. }
  1189. else if(asci && a != _T(""))
  1190. {
  1191. f.Write(a.GetBuffer(), a.GetLength());
  1192. ret = true;
  1193. }
  1194. f.Close();
  1195. }
  1196. return ret;
  1197. }
  1198. /*----------------------------------------------------------------------------*\
  1199. CClipList
  1200. \*----------------------------------------------------------------------------*/
  1201. CClipList::~CClipList()
  1202. {
  1203. CClip* pClip;
  1204. while(GetCount())
  1205. {
  1206. pClip = RemoveHead();
  1207. delete pClip;
  1208. }
  1209. }
  1210. // returns the number of clips actually saved
  1211. // while this does empty the Format Data, it does not delete the Clips.
  1212. int CClipList::AddToDB(bool bLatestOrder)
  1213. {
  1214. Log(_T("AddToDB - Start"));
  1215. int savedCount = 0;
  1216. CClip* pClip;
  1217. POSITION pos;
  1218. bool bResult;
  1219. INT_PTR remaining = GetCount();
  1220. pos = GetHeadPosition();
  1221. while(pos)
  1222. {
  1223. Log(StrF(_T("AddToDB - while(pos), Start Remaining %d"), remaining));
  1224. remaining--;
  1225. pClip = GetNext(pos);
  1226. ASSERT(pClip);
  1227. if(bLatestOrder)
  1228. {
  1229. pClip->MakeLatestOrder();
  1230. pClip->MakeLatestGroupOrder();
  1231. }
  1232. pClip->m_Time = CTime::GetCurrentTime().GetTime();
  1233. bResult = pClip->AddToDB();
  1234. if(bResult)
  1235. {
  1236. savedCount++;
  1237. }
  1238. Log(StrF(_T("AddToDB - while(pos), End Remaining %d, save count: %d"), remaining, savedCount));
  1239. }
  1240. Log(StrF(_T("AddToDB - Start, count: %d"), savedCount));
  1241. return savedCount;
  1242. }
  1243. const CClipList& CClipList::operator=(const CClipList &cliplist)
  1244. {
  1245. POSITION pos;
  1246. CClip* pClip;
  1247. pos = cliplist.GetHeadPosition();
  1248. while(pos)
  1249. {
  1250. pClip = cliplist.GetNext(pos);
  1251. ASSERT(pClip);
  1252. CClip *pNewClip = new CClip;
  1253. if(pNewClip)
  1254. {
  1255. *pNewClip = *pClip;
  1256. AddTail(pNewClip);
  1257. }
  1258. }
  1259. return *this;
  1260. }