OleClipSource.cpp 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  1. #include "stdafx.h"
  2. #include "CP_Main.h"
  3. #include "OleClipSource.h"
  4. #include "shared/TextConvert.h"
  5. #include "CF_HDropAggregator.h"
  6. #include "CF_UnicodeTextAggregator.h"
  7. #include "CF_TextAggregator.h"
  8. #include "richtextaggregator.h"
  9. #include "htmlformataggregator.h"
  10. #include "Shared\Tokenizer.h"
  11. #include <random>
  12. #include "Client.h"
  13. #include "Path.h"
  14. #include "Md5.h"
  15. #include "DittoChaiScript.h"
  16. #include "ChaiScriptOnCopy.h"
  17. #include "Slugify.h"
  18. /*------------------------------------------------------------------*\
  19. COleClipSource
  20. \*------------------------------------------------------------------*/
  21. //IMPLEMENT_DYNAMIC(COleClipSource, COleDataSource)
  22. COleClipSource::COleClipSource()
  23. {
  24. m_bLoadedFormats = false;
  25. m_convertToHDROPOnDelayRender = false;
  26. }
  27. COleClipSource::~COleClipSource()
  28. {
  29. }
  30. BOOL COleClipSource::DoDelayRender()
  31. {
  32. CClipTypes types;
  33. m_ClipIDs.GetTypes(types);
  34. bool foundHDrop = false;
  35. INT_PTR count = types.GetSize();
  36. for(int i=0; i < count; i++)
  37. {
  38. if (m_pasteOptions.m_dragDropFilesOnly)
  39. {
  40. if (types[i] == CF_HDROP)
  41. {
  42. DelayRenderData(types[i]);
  43. }
  44. }
  45. else
  46. {
  47. DelayRenderData(types[i]);
  48. }
  49. if (types[i] == CF_HDROP)
  50. {
  51. foundHDrop = true;
  52. }
  53. }
  54. if (m_pasteOptions.m_placeCF_HDROP_OnDrag &&
  55. foundHDrop == false)
  56. {
  57. DelayRenderData(CF_HDROP);
  58. m_convertToHDROPOnDelayRender = true;
  59. }
  60. return count > 0;
  61. }
  62. BOOL COleClipSource::DoImmediateRender()
  63. {
  64. if(m_bLoadedFormats)
  65. return TRUE;
  66. m_bLoadedFormats = true;
  67. if(m_pasteOptions.m_pPasteFormats != NULL)
  68. {
  69. return PutFormatOnClipboard(m_pasteOptions.m_pPasteFormats) > 0;
  70. }
  71. INT_PTR count = m_ClipIDs.GetSize();
  72. if(count <= 0)
  73. return 0;
  74. CClip clip;
  75. if(count > 1)
  76. {
  77. CStringA SepA = CTextConvert::UnicodeToAnsi(g_Opt.GetMultiPasteSeparator());
  78. CCF_TextAggregator CFText(SepA);
  79. if(m_ClipIDs.AggregateData(CFText, CF_TEXT, g_Opt.m_bMultiPasteReverse, m_pasteOptions.LimitFormatsToText()))
  80. {
  81. CClipFormat cf(CF_TEXT, CFText.GetHGlobal());
  82. clip.m_Formats.Add(cf);
  83. //clip.m_Formats now owns the global data
  84. cf.m_autoDeleteData = false;
  85. }
  86. CStringW SepW = g_Opt.GetMultiPasteSeparator();
  87. CCF_UnicodeTextAggregator CFUnicodeText(SepW);
  88. if(m_ClipIDs.AggregateData(CFUnicodeText, CF_UNICODETEXT, g_Opt.m_bMultiPasteReverse, m_pasteOptions.LimitFormatsToText()))
  89. {
  90. CClipFormat cf(CF_UNICODETEXT, CFUnicodeText.GetHGlobal());
  91. clip.m_Formats.Add(cf);
  92. //clip.m_Formats now owns the global data
  93. cf.m_autoDeleteData = false;
  94. }
  95. if (m_pasteOptions.LimitFormatsToText() == false)
  96. {
  97. CCF_HDropAggregator HDrop;
  98. if(m_ClipIDs.AggregateData(HDrop, CF_HDROP, g_Opt.m_bMultiPasteReverse, m_pasteOptions.LimitFormatsToText()))
  99. {
  100. CClipFormat cf(CF_HDROP, HDrop.GetHGlobal());
  101. clip.m_Formats.Add(cf);
  102. //clip.m_Formats now owns the global data
  103. cf.m_autoDeleteData = false;
  104. }
  105. CRichTextAggregator RichText(SepA);
  106. if(m_ClipIDs.AggregateData(RichText, theApp.m_RTFFormat, g_Opt.m_bMultiPasteReverse, m_pasteOptions.LimitFormatsToText()))
  107. {
  108. CClipFormat cf(theApp.m_RTFFormat, RichText.GetHGlobal());
  109. clip.m_Formats.Add(cf);
  110. //clip.m_Formats now owns the global data
  111. cf.m_autoDeleteData = false;
  112. }
  113. CHTMLFormatAggregator Html(SepA);
  114. if(m_ClipIDs.AggregateData(Html, theApp.m_HTML_Format, g_Opt.m_bMultiPasteReverse, m_pasteOptions.LimitFormatsToText()))
  115. {
  116. CClipFormat cf(theApp.m_HTML_Format, Html.GetHGlobal());
  117. clip.m_Formats.Add(cf);
  118. //clip.m_Formats now owns the global data
  119. cf.m_autoDeleteData = false;
  120. }
  121. }
  122. }
  123. if (count >= 1 && clip.m_Formats.GetCount() == 0)
  124. {
  125. clip.LoadFormats(m_ClipIDs[0], m_pasteOptions.LimitFormatsToText(), m_pasteOptions.IncludeRTFForTextOnly());
  126. }
  127. if (m_pasteOptions.LimitFormatsToText())
  128. {
  129. PlainTextFilter(clip);
  130. }
  131. if(m_pasteOptions.m_pasteUpperCase ||
  132. m_pasteOptions.m_pasteLowerCase)
  133. {
  134. DoUpperLowerCase(clip, m_pasteOptions.m_pasteUpperCase);
  135. }
  136. else if(m_pasteOptions.m_pasteCapitalize)
  137. {
  138. Capitalize(clip);
  139. }
  140. else if(m_pasteOptions.m_pasteSentenceCase)
  141. {
  142. SentenceCase(clip);
  143. }
  144. else if(m_pasteOptions.m_pasteRemoveLineFeeds)
  145. {
  146. RemoveLineFeeds(clip);
  147. }
  148. else if(m_pasteOptions.m_pasteAddOneLineFeed)
  149. {
  150. AddLineFeeds(clip, 1);
  151. }
  152. else if (m_pasteOptions.m_pasteAddTwoLineFeeds)
  153. {
  154. AddLineFeeds(clip, 2);
  155. }
  156. else if (m_pasteOptions.m_pasteTypoglycemia)
  157. {
  158. Typoglycemia(clip);
  159. }
  160. else if (m_pasteOptions.m_pasteAddingDateTime)
  161. {
  162. AddDateTime(clip);
  163. }
  164. else if (m_pasteOptions.m_trimWhiteSpace)
  165. {
  166. TrimWhiteSpace(clip);
  167. }
  168. else if (m_pasteOptions.m_pasteSlugify)
  169. {
  170. Slugify(clip);
  171. }
  172. else if (m_pasteOptions.m_invertCase)
  173. {
  174. InvertCase(clip);
  175. }
  176. else if (m_pasteOptions.m_pasteCamelCase)
  177. {
  178. CamelCase(clip);
  179. }
  180. SaveDittoFileDataToFile(clip);
  181. if (m_pasteOptions.m_pasteScriptGuid != _T(""))
  182. {
  183. for (auto & element : g_Opt.m_pasteScripts.m_list)
  184. {
  185. if (element.m_guid == m_pasteOptions.m_pasteScriptGuid)
  186. {
  187. try
  188. {
  189. Log(StrF(_T("Start of paste script name: %s, script: %s"), element.m_name, element.m_script));
  190. ChaiScriptOnCopy onPaste;
  191. CDittoChaiScript clipData(&clip, "", "");
  192. if (onPaste.ProcessScript(clipData, (LPCSTR)CTextConvert::UnicodeToAnsi(element.m_script)) == false)
  193. {
  194. Log(StrF(_T("End of paste script name: %s, returned false, not saving this copy to Ditto, last Error: %s"), element.m_name, onPaste.m_lastError));
  195. return FALSE;
  196. }
  197. Log(StrF(_T("End of paste script name: %s, returned true, last Error: %s"), element.m_name, onPaste.m_lastError));
  198. }
  199. catch (CException *ex)
  200. {
  201. TCHAR szCause[255];
  202. ex->GetErrorMessage(szCause, 255);
  203. CString cs;
  204. cs.Format(_T("chai script paste exception: %s"), szCause);
  205. Log(cs);
  206. }
  207. catch (...)
  208. {
  209. Log(_T("chai script paste exception 2"));
  210. }
  211. break;
  212. }
  213. }
  214. }
  215. return PutFormatOnClipboard(&clip.m_Formats) > 0;
  216. }
  217. void COleClipSource::DoUpperLowerCase(CClip &clip, bool upper)
  218. {
  219. IClipFormat *unicodeTextFormat = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
  220. if (unicodeTextFormat != NULL)
  221. {
  222. CString cs = unicodeTextFormat->GetAsCString();
  223. //free the old text we are going to replace it below with an upper case version
  224. unicodeTextFormat->Free();
  225. CString val;
  226. if (upper)
  227. {
  228. val = theApp.m_icuString.ToUpperStringEx(cs);
  229. }
  230. else
  231. {
  232. val = theApp.m_icuString.ToLowerStringEx(cs);
  233. }
  234. long lLen = val.GetLength();
  235. HGLOBAL hGlobal = NewGlobalP(val.GetBuffer(lLen), ((lLen+1) * sizeof(wchar_t)));
  236. val.ReleaseBuffer();
  237. unicodeTextFormat->Data(hGlobal);
  238. }
  239. IClipFormat *asciiTextFormat = clip.m_Formats.FindFormatEx(CF_TEXT);
  240. if (asciiTextFormat != NULL)
  241. {
  242. CStringA cs(asciiTextFormat->GetAsCStringA());
  243. //free the old text we are going to replace it below with an upper case version
  244. asciiTextFormat->Free();
  245. CString val;
  246. if (upper)
  247. {
  248. val = cs.MakeUpper();
  249. }
  250. else
  251. {
  252. val = cs.MakeLower();
  253. }
  254. long lLen = val.GetLength();
  255. HGLOBAL hGlobal = NewGlobalP(val.GetBuffer(lLen), lLen + sizeof(char));
  256. val.ReleaseBuffer();
  257. asciiTextFormat->Data(hGlobal);
  258. }
  259. }
  260. void COleClipSource::InvertCase(CClip &clip)
  261. {
  262. IClipFormat *unicodeTextFormat = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
  263. if (unicodeTextFormat != NULL)
  264. {
  265. CString cs(unicodeTextFormat->GetAsCString());
  266. //free the old text we are going to replace it below with an upper case version
  267. unicodeTextFormat->Free();
  268. const int len = cs.GetLength();
  269. if (len > 0)
  270. {
  271. wchar_t* pText = cs.GetBuffer();
  272. for (int i = 0; i < len; i++)
  273. {
  274. wchar_t item = pText[i];
  275. if (theApp.m_icuString.IsUpperEx(item))
  276. {
  277. pText[i] = theApp.m_icuString.ToLowerEx(item);
  278. }
  279. else
  280. {
  281. pText[i] = theApp.m_icuString.ToUpperEx(item);
  282. }
  283. }
  284. }
  285. cs.ReleaseBuffer();
  286. HGLOBAL hGlobal = NewGlobalP(cs.GetBuffer(), ((len + 1) * sizeof(wchar_t)));
  287. unicodeTextFormat->Data(hGlobal);
  288. }
  289. IClipFormat *asciiTextFormat = clip.m_Formats.FindFormatEx(CF_TEXT);
  290. if (asciiTextFormat != NULL)
  291. {
  292. CStringA cs(asciiTextFormat->GetAsCStringA());
  293. //free the old text we are going to replace it below with an upper case version
  294. asciiTextFormat->Free();
  295. long len = cs.GetLength();
  296. if (len > 0)
  297. {
  298. char * pText = cs.GetBuffer();
  299. for (int i = 0; i < len; i++)
  300. {
  301. char item = pText[i];
  302. if (::isupper(item))
  303. {
  304. pText[i] = ::tolower(item);
  305. }
  306. else
  307. {
  308. pText[i] = ::toupper(item);
  309. }
  310. }
  311. }
  312. cs.ReleaseBuffer();
  313. HGLOBAL hGlobal = NewGlobalP(cs.GetBuffer(), (len + 1));
  314. asciiTextFormat->Data(hGlobal);
  315. }
  316. }
  317. void COleClipSource::CamelCase(CClip& clip)
  318. {
  319. IClipFormat* unicodeTextFormat = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
  320. if (unicodeTextFormat != NULL)
  321. {
  322. CString cs(unicodeTextFormat->GetAsCString());
  323. //free the old text we are going to replace it below with an upper case version
  324. unicodeTextFormat->Free();
  325. const int len = cs.GetLength();
  326. if (len > 0)
  327. {
  328. wchar_t* pText = cs.GetBuffer();
  329. bool setCapital = false;
  330. for (int i = 0; i < len; i++)
  331. {
  332. wchar_t item = pText[i];
  333. if (item == ' ')
  334. {
  335. setCapital = true;
  336. }
  337. else if (setCapital || i == 0)
  338. {
  339. if (theApp.m_icuString.IsUpperEx(item) == false)
  340. {
  341. pText[i] = theApp.m_icuString.ToUpperEx(item);
  342. }
  343. setCapital = false;
  344. }
  345. else if (theApp.m_icuString.IsUpperEx(item))
  346. {
  347. pText[i] = theApp.m_icuString.ToLowerEx(item);
  348. }
  349. }
  350. }
  351. cs.ReleaseBuffer();
  352. cs.Remove(' ');
  353. HGLOBAL hGlobal = NewGlobalP(cs.GetBuffer(), ((len + 1) * sizeof(wchar_t)));
  354. unicodeTextFormat->Data(hGlobal);
  355. }
  356. IClipFormat* asciiTextFormat = clip.m_Formats.FindFormatEx(CF_TEXT);
  357. if (asciiTextFormat != NULL)
  358. {
  359. CStringA cs(asciiTextFormat->GetAsCStringA());
  360. //free the old text we are going to replace it below with an upper case version
  361. asciiTextFormat->Free();
  362. long len = cs.GetLength();
  363. if (len > 0)
  364. {
  365. char* pText = cs.GetBuffer();
  366. bool setCapital = false;
  367. for (int i = 0; i < len; i++)
  368. {
  369. char item = pText[i];
  370. if (item == ' ')
  371. {
  372. setCapital = true;
  373. }
  374. else if (setCapital || i == 0)
  375. {
  376. if (::isupper(item) == false)
  377. {
  378. pText[i] = ::toupper(item);
  379. }
  380. setCapital = false;
  381. }
  382. else if(::isupper(item))
  383. {
  384. pText[i] = ::tolower(item);
  385. }
  386. }
  387. }
  388. cs.ReleaseBuffer();
  389. cs.Remove(' ');
  390. HGLOBAL hGlobal = NewGlobalP(cs.GetBuffer(), (len + 1));
  391. asciiTextFormat->Data(hGlobal);
  392. }
  393. }
  394. void COleClipSource::Capitalize(CClip &clip)
  395. {
  396. IClipFormat *unicodeTextFormat = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
  397. if (unicodeTextFormat != NULL)
  398. {
  399. CString cs(unicodeTextFormat->GetAsCString());
  400. //free the old text we are going to replace it below with an upper case version
  401. unicodeTextFormat->Free();
  402. CString val = theApp.m_icuString.ToLowerStringEx(cs);
  403. long len = val.GetLength();
  404. if (len > 0)
  405. {
  406. wchar_t * pText = val.GetBuffer();
  407. pText[0] = theApp.m_icuString.ToUpperEx(pText[0]);
  408. bool capitalize = false;
  409. for (int i = 1; i < len; i++)
  410. {
  411. wchar_t item = pText[i];
  412. if (item == ' ')
  413. {
  414. capitalize = true;
  415. }
  416. else if (capitalize)
  417. {
  418. pText[i] = theApp.m_icuString.ToUpperEx(item);
  419. capitalize = false;
  420. }
  421. }
  422. }
  423. val.ReleaseBuffer();
  424. HGLOBAL hGlobal = NewGlobalP(val.GetBuffer(), ((len + 1) * sizeof(wchar_t)));
  425. unicodeTextFormat->Data(hGlobal);
  426. }
  427. //my change
  428. //test
  429. //second test
  430. IClipFormat *asciiTextFormat = clip.m_Formats.FindFormatEx(CF_TEXT);
  431. if (asciiTextFormat != NULL)
  432. {
  433. CStringA cs(asciiTextFormat->GetAsCStringA());
  434. //free the old text we are going to replace it below with an upper case version
  435. asciiTextFormat->Free();
  436. CStringA val = cs.MakeLower();
  437. long len = val.GetLength();
  438. if (len > 0)
  439. {
  440. char * pText = val.GetBuffer();
  441. pText[0] = toupper(pText[0]);
  442. bool capitalize = false;
  443. for (int i = 1; i < len; i++)
  444. {
  445. char item = pText[i];
  446. if (item == ' ')
  447. {
  448. capitalize = true;
  449. }
  450. else if (capitalize)
  451. {
  452. pText[i] = toupper(item);
  453. capitalize = false;
  454. }
  455. }
  456. }
  457. val.ReleaseBuffer();
  458. HGLOBAL hGlobal = NewGlobalP(val.GetBuffer(), (len + 1));
  459. asciiTextFormat->Data(hGlobal);
  460. }
  461. }
  462. void COleClipSource::SentenceCase(CClip &clip)
  463. {
  464. IClipFormat *unicodeTextFormat = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
  465. if (unicodeTextFormat != NULL)
  466. {
  467. CString cs(unicodeTextFormat->GetAsCString());
  468. //free the old text we are going to replace it below with an upper case version
  469. unicodeTextFormat->Free();
  470. CString val = theApp.m_icuString.ToLowerStringEx(cs);;
  471. long len = val.GetLength();
  472. if (len > 0)
  473. {
  474. wchar_t * pText = val.GetBuffer();
  475. pText[0] = theApp.m_icuString.ToUpperEx(pText[0]);
  476. bool capitalize = false;
  477. for (int i = 1; i < len; i++)
  478. {
  479. wchar_t item = pText[i];
  480. if (item == '.' ||
  481. item == '!' ||
  482. item == '?')
  483. {
  484. capitalize = true;
  485. }
  486. else if (capitalize && item != ' ')
  487. {
  488. pText[i] = theApp.m_icuString.ToUpperEx(item);
  489. capitalize = false;
  490. }
  491. }
  492. }
  493. val.ReleaseBuffer();
  494. HGLOBAL hGlobal = NewGlobalP(val.GetBuffer(), ((len + 1) * sizeof(wchar_t)));
  495. unicodeTextFormat->Data(hGlobal);
  496. }
  497. IClipFormat *asciiTextFormat = clip.m_Formats.FindFormatEx(CF_TEXT);
  498. if (asciiTextFormat != NULL)
  499. {
  500. CStringA cs(asciiTextFormat->GetAsCStringA());
  501. //free the old text we are going to replace it below with an upper case version
  502. asciiTextFormat->Free();
  503. CStringA val = cs.MakeLower();
  504. long len = val.GetLength();
  505. if (len > 0)
  506. {
  507. char * pText = val.GetBuffer();
  508. pText[0] = toupper(pText[0]);
  509. bool capitalize = false;
  510. for (int i = 1; i < len; i++)
  511. {
  512. char item = pText[i];
  513. if (item == '.' ||
  514. item == '!' ||
  515. item == '?')
  516. {
  517. capitalize = true;
  518. }
  519. else if (capitalize && item != ' ')
  520. {
  521. pText[i] = toupper(item);
  522. capitalize = false;
  523. }
  524. }
  525. }
  526. val.ReleaseBuffer();
  527. HGLOBAL hGlobal = NewGlobalP(val.GetBuffer(), (len + 1));
  528. asciiTextFormat->Data(hGlobal);
  529. }
  530. }
  531. void COleClipSource::PlainTextFilter(CClip &clip)
  532. {
  533. bool foundText = false;
  534. INT_PTR hDropIndex = -1;
  535. INT_PTR count = clip.m_Formats.GetCount();
  536. for (INT_PTR i = 0; i < count; i++)
  537. {
  538. CClipFormat *pCF = &clip.m_Formats.ElementAt(i);
  539. if (pCF->m_cfType == CF_TEXT ||
  540. pCF->m_cfType == CF_UNICODETEXT)
  541. {
  542. foundText = true;
  543. }
  544. else if (pCF->m_cfType == CF_HDROP)
  545. {
  546. hDropIndex = i;
  547. }
  548. }
  549. if (foundText &&
  550. hDropIndex > -1)
  551. {
  552. clip.m_Formats.RemoveAt(hDropIndex);
  553. }
  554. else if (foundText == false &&
  555. hDropIndex > -1)
  556. {
  557. CCF_HDropAggregator HDrop;
  558. if (m_ClipIDs.AggregateData(HDrop, CF_HDROP, g_Opt.m_bMultiPasteReverse, m_pasteOptions.LimitFormatsToText()))
  559. {
  560. clip.m_Formats.RemoveAt(hDropIndex);
  561. CClipFormat format(CF_UNICODETEXT, HDrop.GetHGlobalAsString());
  562. clip.m_Formats.Add(format);
  563. format.m_autoDeleteData = false; //owned by m_DelayRenderedFormats
  564. }
  565. }
  566. }
  567. void COleClipSource::RemoveLineFeeds(CClip &clip)
  568. {
  569. IClipFormat *pUnicodeText = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
  570. if (pUnicodeText != NULL)
  571. {
  572. CStringW string(pUnicodeText->GetAsCString());
  573. pUnicodeText->Free();
  574. int count = string.Replace(_T("\r\n"), _T(" "));
  575. count = string.Replace(_T("\r"), _T(" "));
  576. count = string.Replace(_T("\n"), _T(" "));
  577. HGLOBAL hGlobal = NewGlobalP(string.GetBuffer(), ((string.GetLength() + 1) * sizeof(wchar_t)));
  578. pUnicodeText->Data(hGlobal);
  579. }
  580. IClipFormat *pAsciiText = clip.m_Formats.FindFormatEx(CF_TEXT);
  581. if (pAsciiText != NULL)
  582. {
  583. CStringA string(pAsciiText->GetAsCStringA());
  584. pAsciiText->Free();
  585. int count = string.Replace("\r\n", " ");
  586. count = string.Replace("\r", " ");
  587. count = string.Replace("\n", " ");
  588. HGLOBAL hGlobal = NewGlobalP(string.GetBuffer(), ((string.GetLength() + 1)));
  589. pAsciiText->Data(hGlobal);
  590. }
  591. IClipFormat *pRTFFormat = clip.m_Formats.FindFormatEx(theApp.m_RTFFormat);
  592. if (pRTFFormat != NULL)
  593. {
  594. CStringA string(pRTFFormat->GetAsCStringA());
  595. pRTFFormat->Free();
  596. int count = string.Replace("\\par\r\n", " ");
  597. int count2 = string.Replace("\\par ", " ");
  598. int count3 = string.Replace("\\line ", " ");
  599. HGLOBAL hGlobal = NewGlobalP(string.GetBuffer(), ((string.GetLength() + 1)));
  600. pRTFFormat->Data(hGlobal);
  601. }
  602. }
  603. void COleClipSource::AddLineFeeds(CClip &clip, int count)
  604. {
  605. IClipFormat *pUnicodeText = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
  606. if (pUnicodeText != NULL)
  607. {
  608. CStringW string(pUnicodeText->GetAsCString());
  609. pUnicodeText->Free();
  610. for(int i = 0; i < count; i++)
  611. {
  612. string += _T("\r\n");
  613. }
  614. HGLOBAL hGlobal = NewGlobalP(string.GetBuffer(), ((string.GetLength() + 1) * sizeof(wchar_t)));
  615. pUnicodeText->Data(hGlobal);
  616. }
  617. IClipFormat *pAsciiText = clip.m_Formats.FindFormatEx(CF_TEXT);
  618. if (pAsciiText != NULL)
  619. {
  620. CStringA string(pAsciiText->GetAsCStringA());
  621. pAsciiText->Free();
  622. for (int i = 0; i < count; i++)
  623. {
  624. string += "\r\n";
  625. }
  626. HGLOBAL hGlobal = NewGlobalP(string.GetBuffer(), ((string.GetLength() + 1)));
  627. pAsciiText->Data(hGlobal);
  628. }
  629. IClipFormat *pRTFFormat = clip.m_Formats.FindFormatEx(theApp.m_RTFFormat);
  630. if (pRTFFormat != NULL)
  631. {
  632. CStringA string(pRTFFormat->GetAsCStringA());
  633. pRTFFormat->Free();
  634. for (int i = 0; i < count; i++)
  635. {
  636. int pos = string.ReverseFind('}');
  637. if (pos >= 0)
  638. {
  639. int count = string.Insert(pos, "\\par\r\n");
  640. }
  641. }
  642. HGLOBAL hGlobal = NewGlobalP(string.GetBuffer(), ((string.GetLength() + 1)));
  643. pRTFFormat->Data(hGlobal);
  644. }
  645. }
  646. void COleClipSource::AddDateTime(CClip &clip)
  647. {
  648. IClipFormat *pUnicodeText = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
  649. if (pUnicodeText != NULL)
  650. {
  651. CStringW string(pUnicodeText->GetAsCString());
  652. pUnicodeText->Free();
  653. string += _T("\r\n");
  654. COleDateTime now(COleDateTime::GetCurrentTime());
  655. string += now.Format();
  656. HGLOBAL hGlobal = NewGlobalP(string.GetBuffer(), ((string.GetLength() + 1) * sizeof(wchar_t)));
  657. pUnicodeText->Data(hGlobal);
  658. }
  659. IClipFormat *pAsciiText = clip.m_Formats.FindFormatEx(CF_TEXT);
  660. if (pAsciiText != NULL)
  661. {
  662. CStringA string(pAsciiText->GetAsCStringA());
  663. pAsciiText->Free();
  664. string += "\r\n\r\n";
  665. COleDateTime now(COleDateTime::GetCurrentTime());
  666. string += CTextConvert::UnicodeToAnsi(now.Format());
  667. HGLOBAL hGlobal = NewGlobalP(string.GetBuffer(), ((string.GetLength() + 1)));
  668. pAsciiText->Data(hGlobal);
  669. }
  670. IClipFormat *pRTFFormat = clip.m_Formats.FindFormatEx(theApp.m_RTFFormat);
  671. if (pRTFFormat != NULL)
  672. {
  673. CStringA string(pRTFFormat->GetAsCStringA());
  674. pRTFFormat->Free();
  675. int pos = string.ReverseFind('}');
  676. if (pos >= 0)
  677. {
  678. string += _T("\r\n\r\n");
  679. COleDateTime now(COleDateTime::GetCurrentTime());
  680. CStringA insert;
  681. insert.Format("\\par\r\n\\par\r\n%s", CTextConvert::UnicodeToAnsi(now.Format()));
  682. int count = string.Insert(pos, insert);
  683. }
  684. HGLOBAL hGlobal = NewGlobalP(string.GetBuffer(), ((string.GetLength() + 1)));
  685. pRTFFormat->Data(hGlobal);
  686. }
  687. }
  688. void COleClipSource::TrimWhiteSpace(CClip &clip)
  689. {
  690. IClipFormat *pUnicodeText = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
  691. if (pUnicodeText != NULL)
  692. {
  693. CStringW string(pUnicodeText->GetAsCString());
  694. pUnicodeText->Free();
  695. string = string.Trim();
  696. string = string.Trim(_T("\t"));
  697. string = string.Trim(_T("\r"));
  698. string = string.Trim(_T("\n"));
  699. HGLOBAL hGlobal = NewGlobalP(string.GetBuffer(), ((string.GetLength() + 1) * sizeof(wchar_t)));
  700. pUnicodeText->Data(hGlobal);
  701. }
  702. IClipFormat *pAsciiText = clip.m_Formats.FindFormatEx(CF_TEXT);
  703. if (pAsciiText != NULL)
  704. {
  705. CStringA string(pAsciiText->GetAsCStringA());
  706. pAsciiText->Free();
  707. string = string.Trim();
  708. string = string.Trim("\t");
  709. string = string.Trim("\r");
  710. string = string.Trim("\n");
  711. HGLOBAL hGlobal = NewGlobalP(string.GetBuffer(), ((string.GetLength() + 1)));
  712. pAsciiText->Data(hGlobal);
  713. }
  714. }
  715. void COleClipSource::SaveDittoFileDataToFile(CClip &clip)
  716. {
  717. CFileRecieve hDrpData;
  718. CClipFormat* pCF;
  719. int hDropIndex = -1;
  720. bool savedFile = false;
  721. INT_PTR count = clip.m_Formats.GetSize();
  722. for (int i = 0; i < count; i++)
  723. {
  724. pCF = &clip.m_Formats.ElementAt(i);
  725. if (pCF->m_cfType == theApp.m_DittoFileData)
  726. {
  727. IClipFormat *dittoFileData = &clip.m_Formats.ElementAt(i);
  728. if (dittoFileData != NULL)
  729. {
  730. HGLOBAL data = dittoFileData->Data();
  731. char * stringData = (char *)GlobalLock(data);
  732. //original source is store in the first string ending in the null terminator
  733. CStringA src(stringData);
  734. stringData += src.GetLength() + 1;
  735. CStringA originalMd5(stringData);
  736. stringData += originalMd5.GetLength() + 1;
  737. int dataSize = (int)GlobalSize(data) - (src.GetLength() + 1) - (originalMd5.GetLength() + 1);
  738. CMd5 calcMd5;
  739. CStringA md5String = calcMd5.CalcMD5FromString(stringData, dataSize);
  740. CString unicodeFilePath = CTextConvert::Utf8ToUnicode(src);
  741. CString unicodeMd5 = CTextConvert::Utf8ToUnicode(md5String);
  742. Log(StrF(_T("Saving file contents from Ditto, original file: %s, size: %d, md5: %s"), unicodeFilePath, dataSize, unicodeMd5));
  743. if (md5String == originalMd5)
  744. {
  745. using namespace nsPath;
  746. CPath path(unicodeFilePath);
  747. CString fileName = path.GetName();
  748. CString newFilePath = CGetSetOptions::GetPath(PATH_DRAG_FILES);
  749. newFilePath += fileName;
  750. CFile f;
  751. if (f.Open(newFilePath, CFile::modeWrite | CFile::modeCreate))
  752. {
  753. f.Write(stringData, dataSize);
  754. f.Close();
  755. savedFile = true;
  756. hDrpData.AddFile(newFilePath);
  757. }
  758. else
  759. {
  760. Log(StrF(_T("Error saving file: %s"), unicodeFilePath));
  761. }
  762. }
  763. else
  764. {
  765. Log(StrF(_T("MD5 ERROR, file: %s, original md5: %s, calc md5: %s"), unicodeFilePath, originalMd5, md5String));
  766. }
  767. }
  768. }
  769. else if (pCF->m_cfType == CF_HDROP)
  770. {
  771. hDropIndex = i;
  772. }
  773. }
  774. if (savedFile)
  775. {
  776. if (hDropIndex >= 0)
  777. {
  778. clip.m_Formats.RemoveAt(hDropIndex);
  779. }
  780. CClipFormat cf(CF_HDROP, hDrpData.CreateCF_HDROPBuffer());
  781. clip.m_Formats.Add(cf);
  782. //clip.m_Formats now owns the global data
  783. cf.m_autoDeleteData = false;
  784. }
  785. }
  786. void COleClipSource::Typoglycemia(CClip &clip)
  787. {
  788. IClipFormat *unicodeTextFormat = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
  789. if (unicodeTextFormat != NULL)
  790. {
  791. CString cs(unicodeTextFormat->GetAsCString());
  792. //free the old text we are going to replace it below with an upper case version
  793. unicodeTextFormat->Free();
  794. CString newString;
  795. CTokenizer token(cs, _T(' '));
  796. CString word;
  797. while (token.Next(word))
  798. {
  799. if(word.GetLength() > 3)
  800. {
  801. int end = word.GetLength();
  802. for (int i = end-1; i >= 0; i--)
  803. {
  804. if(word[i] == _T('.') ||
  805. word[i] == _T('!') ||
  806. word[i] == _T('?'))
  807. {
  808. end--;
  809. }
  810. else
  811. {
  812. break;
  813. }
  814. }
  815. if (end > 3)
  816. {
  817. std::uniform_int_distribution<int> dist(1, end - 2);
  818. std::random_device rd;
  819. for (int i = 1; i < end - 1; i++)
  820. {
  821. int newPos = dist(rd);
  822. CString cs;
  823. cs.Format(_T("pos: %d, rnd: %d\r\n"), i, newPos);
  824. OutputDebugString(cs);
  825. TCHAR temp = word.GetAt(i);
  826. word.SetAt(i, word.GetAt(newPos));
  827. word.SetAt(newPos, temp);
  828. }
  829. }
  830. newString += word;
  831. }
  832. else
  833. {
  834. newString += word;
  835. }
  836. newString += _T(' ');
  837. }
  838. long len = newString.GetLength();
  839. HGLOBAL hGlobal = NewGlobalP(newString.GetBuffer(), ((len + 1) * sizeof(wchar_t)));
  840. unicodeTextFormat->Data(hGlobal);
  841. }
  842. }
  843. INT_PTR COleClipSource::PutFormatOnClipboard(CClipFormats *pFormats)
  844. {
  845. Log(_T("Start of put format on clipboard"));
  846. CClipFormat* pCF;
  847. INT_PTR count = pFormats->GetSize();
  848. bool bDelayedRenderCF_HDROP = false;
  849. bool dittoFileData = false;
  850. INT_PTR i = 0;
  851. //see if the html format is in the list
  852. //if it is the list we will not paste CF_TEXT
  853. for(i = 0; i < count; i++)
  854. {
  855. pCF = &pFormats->ElementAt(i);
  856. if(pCF->m_cfType == theApp.m_RemoteCF_HDROP)
  857. {
  858. bDelayedRenderCF_HDROP = true;
  859. }
  860. if (pCF->m_cfType == theApp.m_DittoFileData)
  861. {
  862. dittoFileData = true;
  863. //save file data
  864. //adjust hdrop
  865. }
  866. }
  867. for(i = 0; i < count; i++)
  868. {
  869. pCF = &pFormats->ElementAt(i);
  870. if(bDelayedRenderCF_HDROP)
  871. {
  872. if(pCF->m_cfType == CF_HDROP)
  873. {
  874. LogSendRecieveInfo("Added delayed cf_hdrop to clipboard");
  875. DelayRenderData(pCF->m_cfType);
  876. }
  877. continue;
  878. }
  879. Log(StrF(_T("Setting clipboard type: %s to the clipboard"), GetFormatName(pCF->m_cfType)));
  880. CacheGlobalData(pCF->m_cfType, pCF->m_hgData);
  881. pCF->m_hgData = 0; // OLE owns it now
  882. }
  883. pFormats->RemoveAll();
  884. m_bLoadedFormats = true;
  885. Log(_T("End of put format on clipboard"));
  886. return count;
  887. }
  888. BOOL COleClipSource::OnRenderGlobalData(LPFORMATETC lpFormatEtc, HGLOBAL* phGlobal)
  889. {
  890. static bool bInHere = false;
  891. if(bInHere)
  892. {
  893. return FALSE;
  894. }
  895. bInHere = true;
  896. HGLOBAL hData = NULL;
  897. CClipFormat *pFind = m_DelayRenderedFormats.FindFormat(lpFormatEtc->cfFormat);
  898. if(pFind)
  899. {
  900. if(pFind->m_hgData)
  901. {
  902. hData = NewGlobalH(pFind->m_hgData, GlobalSize(pFind->m_hgData));
  903. }
  904. }
  905. else
  906. {
  907. if (m_pasteOptions.m_delayRenderLockout > 0 &&
  908. (GetTickCount() - m_pasteOptions.m_delayRenderLockout) < (DWORD)CGetSetOptions::GetDelayRenderLockout())
  909. {
  910. bInHere = false;
  911. return false;
  912. }
  913. LogSendRecieveInfo("Delayed Render, getting data from remote machine");
  914. CClip clip;
  915. if(m_ClipIDs.GetCount() > 0)
  916. {
  917. clip.LoadFormats(m_ClipIDs[0]);
  918. CClipFormat *pDittoDelayCF_HDROP = clip.m_Formats.FindFormat(theApp.m_RemoteCF_HDROP);
  919. CClipFormat *pCF_HDROP = clip.m_Formats.FindFormat(CF_HDROP);
  920. if(pDittoDelayCF_HDROP && pCF_HDROP)
  921. {
  922. CDittoCF_HDROP *pData = (CDittoCF_HDROP*)GlobalLock(pDittoDelayCF_HDROP->m_hgData);
  923. if(pData)
  924. {
  925. CString csComputerName;
  926. CString csIP;
  927. csIP = CTextConvert::Utf8ToUnicode(pData->m_cIP);
  928. csComputerName = CTextConvert::Utf8ToUnicode(pData->m_cComputerName);
  929. GlobalUnlock(pDittoDelayCF_HDROP->m_hgData);
  930. CString ipPort = csIP;
  931. if (pData->respondPort > 0)
  932. {
  933. ipPort.Format(_T("%s:%d"), csIP, pData->respondPort);
  934. }
  935. CString namePort = csComputerName;
  936. if (pData->respondPort > 0)
  937. {
  938. namePort.Format(_T("%s:%d"), csComputerName, pData->respondPort);
  939. }
  940. CClient cl;
  941. hData = cl.RequestCopiedFiles(*pCF_HDROP, ipPort, namePort);
  942. }
  943. }
  944. else
  945. {
  946. hData = m_ClipIDs.Render(lpFormatEtc->cfFormat);
  947. if (m_convertToHDROPOnDelayRender &&
  948. hData == NULL &&
  949. lpFormatEtc->cfFormat == CF_HDROP)
  950. {
  951. hData = ConvertToFileDrop();
  952. }
  953. }
  954. }
  955. //Add to a cache of already rendered data
  956. //Windows seems to call this function multiple times
  957. //so only the first time do we need to go get the data
  958. HGLOBAL hCopy = NULL;
  959. if(hData)
  960. {
  961. hCopy = NewGlobalH(hData, GlobalSize(hData));
  962. }
  963. CClipFormat format(lpFormatEtc->cfFormat, hCopy);
  964. m_DelayRenderedFormats.Add(format);
  965. format.m_autoDeleteData = false; //owned by m_DelayRenderedFormats
  966. }
  967. BOOL bRet = FALSE;
  968. if(hData)
  969. {
  970. // if phGlobal is null, we can just give the allocated mem
  971. // else, our data must fit within the GlobalSize(*phGlobal)
  972. if(*phGlobal == 0)
  973. {
  974. *phGlobal = hData;
  975. }
  976. else
  977. {
  978. SIZE_T len = min(::GlobalSize(*phGlobal), ::GlobalSize(hData));
  979. if(len)
  980. {
  981. CopyToGlobalHH(*phGlobal, hData, len);
  982. }
  983. ::GlobalFree(hData);
  984. }
  985. bRet = TRUE;
  986. }
  987. bInHere = false;
  988. return bRet;
  989. }
  990. HGLOBAL COleClipSource::ConvertToFileDrop()
  991. {
  992. CString path = CGetSetOptions::GetPath(PATH_DRAG_FILES);
  993. CreateDirectory(path, NULL);
  994. CFileRecieve fileList;
  995. int dragId = CGetSetOptions::GetDragId();
  996. int origDragId = dragId;
  997. auto customDragName = g_Opt.GetTempDragFileName();
  998. if (customDragName != _T(""))
  999. {
  1000. dragId = 1;
  1001. }
  1002. for (int i = 0; i < m_ClipIDs.GetCount(); i++)
  1003. {
  1004. CClip fileClip;
  1005. fileClip.LoadFormats(m_ClipIDs[i]);
  1006. CClipFormat *unicodeText = fileClip.m_Formats.FindFormat(CF_UNICODETEXT);
  1007. if (unicodeText)
  1008. {
  1009. CString name = _T("text");
  1010. CString file;
  1011. if (customDragName != _T(""))
  1012. {
  1013. name = customDragName;
  1014. file.Format(_T("%s%s.txt"), path, name);
  1015. }
  1016. else
  1017. {
  1018. file.Format(_T("%s%s_%d.txt"), path, name, dragId++);
  1019. }
  1020. fileClip.WriteTextToFile(file, TRUE, FALSE, FALSE);
  1021. fileList.AddFile(file);
  1022. }
  1023. else
  1024. {
  1025. CClipFormat *asciiText = fileClip.m_Formats.FindFormat(CF_TEXT);
  1026. if (asciiText)
  1027. {
  1028. CString name = _T("text");
  1029. CString file;
  1030. if (customDragName != _T(""))
  1031. {
  1032. name = customDragName;
  1033. file.Format(_T("%s%s.txt"), path, name);
  1034. }
  1035. else
  1036. {
  1037. file.Format(_T("%s%s_%d.txt"), path, name, dragId++);
  1038. }
  1039. fileClip.WriteTextToFile(file, FALSE, TRUE, FALSE);
  1040. fileList.AddFile(file);
  1041. }
  1042. else
  1043. {
  1044. CClipFormat *png = NULL;
  1045. CClipFormat *bitmap = fileClip.m_Formats.FindFormat(CF_DIB);
  1046. if (bitmap == NULL)
  1047. {
  1048. png = fileClip.m_Formats.FindFormat(theApp.m_PNG_Format);
  1049. }
  1050. if (bitmap != NULL ||
  1051. png != NULL)
  1052. {
  1053. CString name = _T("image");
  1054. CString file;
  1055. if (customDragName != _T(""))
  1056. {
  1057. name = customDragName;
  1058. file.Format(_T("%s%s.png"), path, name);
  1059. }
  1060. else
  1061. {
  1062. file.Format(_T("%s%s_%d.png"), path, name, dragId++);
  1063. }
  1064. if (fileClip.WriteImageToFile(file))
  1065. {
  1066. fileList.AddFile(file);
  1067. }
  1068. }
  1069. }
  1070. }
  1071. }
  1072. if(customDragName == _T("") &&
  1073. dragId != origDragId)
  1074. {
  1075. CGetSetOptions::SetDragId(dragId);
  1076. }
  1077. HGLOBAL hData = fileList.CreateCF_HDROPBuffer();
  1078. return hData;
  1079. }
  1080. void COleClipSource::Slugify(CClip &clip)
  1081. {
  1082. IClipFormat *unicodeTextFormat = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
  1083. if (unicodeTextFormat != NULL)
  1084. {
  1085. CString cs(unicodeTextFormat->GetAsCString());
  1086. //free the old text we are going to replace it below with an upper case version
  1087. unicodeTextFormat->Free();
  1088. CString newString = slugify(cs.GetString()).c_str();
  1089. long len = newString.GetLength();
  1090. HGLOBAL hGlobal = NewGlobalP(newString.GetBuffer(), ((len + 1) * sizeof(wchar_t)));
  1091. unicodeTextFormat->Data(hGlobal);
  1092. }
  1093. }