Clip.cpp 51 KB

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