Clip.cpp 54 KB

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