Clip.cpp 45 KB

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