Clip.cpp 47 KB

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