OleClipSource.cpp 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  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::ConvertToChar(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 = CTextConvert::ConvertToUnicode(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. SaveDittoFileDataToFile(clip);
  177. if (m_pasteOptions.m_pasteScriptGuid != _T(""))
  178. {
  179. for (auto & element : g_Opt.m_pasteScripts.m_list)
  180. {
  181. if (element.m_guid == m_pasteOptions.m_pasteScriptGuid)
  182. {
  183. try
  184. {
  185. Log(StrF(_T("Start of paste script name: %s, script: %s"), element.m_name, element.m_script));
  186. ChaiScriptOnCopy onPaste;
  187. CDittoChaiScript clipData(&clip, "", "");
  188. if (onPaste.ProcessScript(clipData, (LPCSTR)CTextConvert::ConvertToChar(element.m_script)) == false)
  189. {
  190. 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));
  191. return FALSE;
  192. }
  193. Log(StrF(_T("End of paste script name: %s, returned true, last Error: %s"), element.m_name, onPaste.m_lastError));
  194. }
  195. catch (CException *ex)
  196. {
  197. TCHAR szCause[255];
  198. ex->GetErrorMessage(szCause, 255);
  199. CString cs;
  200. cs.Format(_T("chai script paste exception: %s"), szCause);
  201. Log(cs);
  202. }
  203. catch (...)
  204. {
  205. Log(_T("chai script paste exception 2"));
  206. }
  207. break;
  208. }
  209. }
  210. }
  211. return PutFormatOnClipboard(&clip.m_Formats) > 0;
  212. }
  213. void COleClipSource::DoUpperLowerCase(CClip &clip, bool upper)
  214. {
  215. IClipFormat *unicodeTextFormat = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
  216. if (unicodeTextFormat != NULL)
  217. {
  218. CString cs = unicodeTextFormat->GetAsCString();
  219. //free the old text we are going to replace it below with an upper case version
  220. unicodeTextFormat->Free();
  221. CString val;
  222. if (upper)
  223. {
  224. val = theApp.m_icuString.ToUpperStringEx(cs);
  225. }
  226. else
  227. {
  228. val = theApp.m_icuString.ToLowerStringEx(cs);
  229. }
  230. long lLen = val.GetLength();
  231. HGLOBAL hGlobal = NewGlobalP(val.GetBuffer(lLen), ((lLen+1) * sizeof(wchar_t)));
  232. val.ReleaseBuffer();
  233. unicodeTextFormat->Data(hGlobal);
  234. }
  235. IClipFormat *asciiTextFormat = clip.m_Formats.FindFormatEx(CF_TEXT);
  236. if (asciiTextFormat != NULL)
  237. {
  238. CStringA cs(asciiTextFormat->GetAsCStringA());
  239. //free the old text we are going to replace it below with an upper case version
  240. asciiTextFormat->Free();
  241. CString val;
  242. if (upper)
  243. {
  244. val = cs.MakeUpper();
  245. }
  246. else
  247. {
  248. val = cs.MakeLower();
  249. }
  250. long lLen = val.GetLength();
  251. HGLOBAL hGlobal = NewGlobalP(val.GetBuffer(lLen), lLen + sizeof(char));
  252. val.ReleaseBuffer();
  253. asciiTextFormat->Data(hGlobal);
  254. }
  255. }
  256. void COleClipSource::InvertCase(CClip &clip)
  257. {
  258. IClipFormat *unicodeTextFormat = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
  259. if (unicodeTextFormat != NULL)
  260. {
  261. CString cs(unicodeTextFormat->GetAsCString());
  262. //free the old text we are going to replace it below with an upper case version
  263. unicodeTextFormat->Free();
  264. const int len = cs.GetLength();
  265. if (len > 0)
  266. {
  267. wchar_t* pText = cs.GetBuffer();
  268. for (int i = 0; i < len; i++)
  269. {
  270. wchar_t item = pText[i];
  271. if (theApp.m_icuString.IsUpperEx(item))
  272. {
  273. pText[i] = theApp.m_icuString.ToLowerEx(item);
  274. }
  275. else
  276. {
  277. pText[i] = theApp.m_icuString.ToUpperEx(item);
  278. }
  279. }
  280. }
  281. cs.ReleaseBuffer();
  282. HGLOBAL hGlobal = NewGlobalP(cs.GetBuffer(), ((len + 1) * sizeof(wchar_t)));
  283. unicodeTextFormat->Data(hGlobal);
  284. }
  285. IClipFormat *asciiTextFormat = clip.m_Formats.FindFormatEx(CF_TEXT);
  286. if (asciiTextFormat != NULL)
  287. {
  288. CStringA cs(asciiTextFormat->GetAsCStringA());
  289. //free the old text we are going to replace it below with an upper case version
  290. asciiTextFormat->Free();
  291. long len = cs.GetLength();
  292. if (len > 0)
  293. {
  294. char * pText = cs.GetBuffer();
  295. for (int i = 0; i < len; i++)
  296. {
  297. char item = pText[i];
  298. if (::isupper(item))
  299. {
  300. pText[i] = ::tolower(item);
  301. }
  302. else
  303. {
  304. pText[i] = ::toupper(item);
  305. }
  306. }
  307. }
  308. cs.ReleaseBuffer();
  309. HGLOBAL hGlobal = NewGlobalP(cs.GetBuffer(), (len + 1));
  310. asciiTextFormat->Data(hGlobal);
  311. }
  312. }
  313. void COleClipSource::Capitalize(CClip &clip)
  314. {
  315. IClipFormat *unicodeTextFormat = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
  316. if (unicodeTextFormat != NULL)
  317. {
  318. CString cs(unicodeTextFormat->GetAsCString());
  319. //free the old text we are going to replace it below with an upper case version
  320. unicodeTextFormat->Free();
  321. CString val = theApp.m_icuString.ToLowerStringEx(cs);
  322. long len = val.GetLength();
  323. if (len > 0)
  324. {
  325. wchar_t * pText = val.GetBuffer();
  326. pText[0] = theApp.m_icuString.ToUpperEx(pText[0]);
  327. bool capitalize = false;
  328. for (int i = 1; i < len; i++)
  329. {
  330. wchar_t item = pText[i];
  331. if (item == ' ')
  332. {
  333. capitalize = true;
  334. }
  335. else if (capitalize)
  336. {
  337. pText[i] = theApp.m_icuString.ToUpperEx(item);
  338. capitalize = false;
  339. }
  340. }
  341. }
  342. val.ReleaseBuffer();
  343. HGLOBAL hGlobal = NewGlobalP(val.GetBuffer(), ((len + 1) * sizeof(wchar_t)));
  344. unicodeTextFormat->Data(hGlobal);
  345. }
  346. //my change
  347. //test
  348. //second test
  349. IClipFormat *asciiTextFormat = clip.m_Formats.FindFormatEx(CF_TEXT);
  350. if (asciiTextFormat != NULL)
  351. {
  352. CStringA cs(asciiTextFormat->GetAsCStringA());
  353. //free the old text we are going to replace it below with an upper case version
  354. asciiTextFormat->Free();
  355. CStringA val = cs.MakeLower();
  356. long len = val.GetLength();
  357. if (len > 0)
  358. {
  359. char * pText = val.GetBuffer();
  360. pText[0] = toupper(pText[0]);
  361. bool capitalize = false;
  362. for (int i = 1; i < len; i++)
  363. {
  364. char item = pText[i];
  365. if (item == ' ')
  366. {
  367. capitalize = true;
  368. }
  369. else if (capitalize)
  370. {
  371. pText[i] = toupper(item);
  372. capitalize = false;
  373. }
  374. }
  375. }
  376. val.ReleaseBuffer();
  377. HGLOBAL hGlobal = NewGlobalP(val.GetBuffer(), (len + 1));
  378. asciiTextFormat->Data(hGlobal);
  379. }
  380. }
  381. void COleClipSource::SentenceCase(CClip &clip)
  382. {
  383. IClipFormat *unicodeTextFormat = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
  384. if (unicodeTextFormat != NULL)
  385. {
  386. CString cs(unicodeTextFormat->GetAsCString());
  387. //free the old text we are going to replace it below with an upper case version
  388. unicodeTextFormat->Free();
  389. CString val = theApp.m_icuString.ToLowerStringEx(cs);;
  390. long len = val.GetLength();
  391. if (len > 0)
  392. {
  393. wchar_t * pText = val.GetBuffer();
  394. pText[0] = theApp.m_icuString.ToUpperEx(pText[0]);
  395. bool capitalize = false;
  396. for (int i = 1; i < len; i++)
  397. {
  398. wchar_t item = pText[i];
  399. if (item == '.' ||
  400. item == '!' ||
  401. item == '?')
  402. {
  403. capitalize = true;
  404. }
  405. else if (capitalize && item != ' ')
  406. {
  407. pText[i] = theApp.m_icuString.ToUpperEx(item);
  408. capitalize = false;
  409. }
  410. }
  411. }
  412. val.ReleaseBuffer();
  413. HGLOBAL hGlobal = NewGlobalP(val.GetBuffer(), ((len + 1) * sizeof(wchar_t)));
  414. unicodeTextFormat->Data(hGlobal);
  415. }
  416. IClipFormat *asciiTextFormat = clip.m_Formats.FindFormatEx(CF_TEXT);
  417. if (asciiTextFormat != NULL)
  418. {
  419. CStringA cs(asciiTextFormat->GetAsCStringA());
  420. //free the old text we are going to replace it below with an upper case version
  421. asciiTextFormat->Free();
  422. CStringA val = cs.MakeLower();
  423. long len = val.GetLength();
  424. if (len > 0)
  425. {
  426. char * pText = val.GetBuffer();
  427. pText[0] = toupper(pText[0]);
  428. bool capitalize = false;
  429. for (int i = 1; i < len; i++)
  430. {
  431. char item = pText[i];
  432. if (item == '.' ||
  433. item == '!' ||
  434. item == '?')
  435. {
  436. capitalize = true;
  437. }
  438. else if (capitalize && item != ' ')
  439. {
  440. pText[i] = toupper(item);
  441. capitalize = false;
  442. }
  443. }
  444. }
  445. val.ReleaseBuffer();
  446. HGLOBAL hGlobal = NewGlobalP(val.GetBuffer(), (len + 1));
  447. asciiTextFormat->Data(hGlobal);
  448. }
  449. }
  450. void COleClipSource::PlainTextFilter(CClip &clip)
  451. {
  452. bool foundText = false;
  453. INT_PTR hDropIndex = -1;
  454. INT_PTR count = clip.m_Formats.GetCount();
  455. for (INT_PTR i = 0; i < count; i++)
  456. {
  457. CClipFormat *pCF = &clip.m_Formats.ElementAt(i);
  458. if (pCF->m_cfType == CF_TEXT ||
  459. pCF->m_cfType == CF_UNICODETEXT)
  460. {
  461. foundText = true;
  462. }
  463. else if (pCF->m_cfType == CF_HDROP)
  464. {
  465. hDropIndex = i;
  466. }
  467. }
  468. if (foundText &&
  469. hDropIndex > -1)
  470. {
  471. clip.m_Formats.RemoveAt(hDropIndex);
  472. }
  473. else if (foundText == false &&
  474. hDropIndex > -1)
  475. {
  476. CCF_HDropAggregator HDrop;
  477. if (m_ClipIDs.AggregateData(HDrop, CF_HDROP, g_Opt.m_bMultiPasteReverse, m_pasteOptions.LimitFormatsToText()))
  478. {
  479. clip.m_Formats.RemoveAt(hDropIndex);
  480. CClipFormat format(CF_UNICODETEXT, HDrop.GetHGlobalAsString());
  481. clip.m_Formats.Add(format);
  482. format.m_autoDeleteData = false; //owned by m_DelayRenderedFormats
  483. }
  484. }
  485. }
  486. void COleClipSource::RemoveLineFeeds(CClip &clip)
  487. {
  488. IClipFormat *pUnicodeText = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
  489. if (pUnicodeText != NULL)
  490. {
  491. CStringW string(pUnicodeText->GetAsCString());
  492. pUnicodeText->Free();
  493. int count = string.Replace(_T("\r\n"), _T(" "));
  494. count = string.Replace(_T("\r"), _T(" "));
  495. count = string.Replace(_T("\n"), _T(" "));
  496. HGLOBAL hGlobal = NewGlobalP(string.GetBuffer(), ((string.GetLength() + 1) * sizeof(wchar_t)));
  497. pUnicodeText->Data(hGlobal);
  498. }
  499. IClipFormat *pAsciiText = clip.m_Formats.FindFormatEx(CF_TEXT);
  500. if (pAsciiText != NULL)
  501. {
  502. CStringA string(pAsciiText->GetAsCStringA());
  503. pAsciiText->Free();
  504. int count = string.Replace("\r\n", " ");
  505. count = string.Replace("\r", " ");
  506. count = string.Replace("\n", " ");
  507. HGLOBAL hGlobal = NewGlobalP(string.GetBuffer(), ((string.GetLength() + 1)));
  508. pAsciiText->Data(hGlobal);
  509. }
  510. IClipFormat *pRTFFormat = clip.m_Formats.FindFormatEx(theApp.m_RTFFormat);
  511. if (pRTFFormat != NULL)
  512. {
  513. CStringA string(pRTFFormat->GetAsCStringA());
  514. pRTFFormat->Free();
  515. int count = string.Replace("\\par\r\n", " ");
  516. int count2 = string.Replace("\\par ", " ");
  517. int count3 = string.Replace("\\line ", " ");
  518. HGLOBAL hGlobal = NewGlobalP(string.GetBuffer(), ((string.GetLength() + 1)));
  519. pRTFFormat->Data(hGlobal);
  520. }
  521. }
  522. void COleClipSource::AddLineFeeds(CClip &clip, int count)
  523. {
  524. IClipFormat *pUnicodeText = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
  525. if (pUnicodeText != NULL)
  526. {
  527. CStringW string(pUnicodeText->GetAsCString());
  528. pUnicodeText->Free();
  529. for(int i = 0; i < count; i++)
  530. {
  531. string += _T("\r\n");
  532. }
  533. HGLOBAL hGlobal = NewGlobalP(string.GetBuffer(), ((string.GetLength() + 1) * sizeof(wchar_t)));
  534. pUnicodeText->Data(hGlobal);
  535. }
  536. IClipFormat *pAsciiText = clip.m_Formats.FindFormatEx(CF_TEXT);
  537. if (pAsciiText != NULL)
  538. {
  539. CStringA string(pAsciiText->GetAsCStringA());
  540. pAsciiText->Free();
  541. for (int i = 0; i < count; i++)
  542. {
  543. string += "\r\n";
  544. }
  545. HGLOBAL hGlobal = NewGlobalP(string.GetBuffer(), ((string.GetLength() + 1)));
  546. pAsciiText->Data(hGlobal);
  547. }
  548. IClipFormat *pRTFFormat = clip.m_Formats.FindFormatEx(theApp.m_RTFFormat);
  549. if (pRTFFormat != NULL)
  550. {
  551. CStringA string(pRTFFormat->GetAsCStringA());
  552. pRTFFormat->Free();
  553. for (int i = 0; i < count; i++)
  554. {
  555. int pos = string.ReverseFind('}');
  556. if (pos >= 0)
  557. {
  558. int count = string.Insert(pos, "\\par\r\n");
  559. }
  560. }
  561. HGLOBAL hGlobal = NewGlobalP(string.GetBuffer(), ((string.GetLength() + 1)));
  562. pRTFFormat->Data(hGlobal);
  563. }
  564. }
  565. void COleClipSource::AddDateTime(CClip &clip)
  566. {
  567. IClipFormat *pUnicodeText = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
  568. if (pUnicodeText != NULL)
  569. {
  570. CStringW string(pUnicodeText->GetAsCString());
  571. pUnicodeText->Free();
  572. string += _T("\r\n");
  573. COleDateTime now(COleDateTime::GetCurrentTime());
  574. string += now.Format();
  575. HGLOBAL hGlobal = NewGlobalP(string.GetBuffer(), ((string.GetLength() + 1) * sizeof(wchar_t)));
  576. pUnicodeText->Data(hGlobal);
  577. }
  578. IClipFormat *pAsciiText = clip.m_Formats.FindFormatEx(CF_TEXT);
  579. if (pAsciiText != NULL)
  580. {
  581. CStringA string(pAsciiText->GetAsCStringA());
  582. pAsciiText->Free();
  583. string += "\r\n\r\n";
  584. COleDateTime now(COleDateTime::GetCurrentTime());
  585. string += CTextConvert::UnicodeStringToMultiByte(now.Format());
  586. HGLOBAL hGlobal = NewGlobalP(string.GetBuffer(), ((string.GetLength() + 1)));
  587. pAsciiText->Data(hGlobal);
  588. }
  589. IClipFormat *pRTFFormat = clip.m_Formats.FindFormatEx(theApp.m_RTFFormat);
  590. if (pRTFFormat != NULL)
  591. {
  592. CStringA string(pRTFFormat->GetAsCStringA());
  593. pRTFFormat->Free();
  594. int pos = string.ReverseFind('}');
  595. if (pos >= 0)
  596. {
  597. string += _T("\r\n\r\n");
  598. COleDateTime now(COleDateTime::GetCurrentTime());
  599. CStringA insert;
  600. insert.Format("\\par\r\n\\par\r\n%s", CTextConvert::UnicodeStringToMultiByte(now.Format()));
  601. int count = string.Insert(pos, insert);
  602. }
  603. HGLOBAL hGlobal = NewGlobalP(string.GetBuffer(), ((string.GetLength() + 1)));
  604. pRTFFormat->Data(hGlobal);
  605. }
  606. }
  607. void COleClipSource::TrimWhiteSpace(CClip &clip)
  608. {
  609. IClipFormat *pUnicodeText = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
  610. if (pUnicodeText != NULL)
  611. {
  612. CStringW string(pUnicodeText->GetAsCString());
  613. pUnicodeText->Free();
  614. string = string.Trim();
  615. string = string.Trim(_T("\t"));
  616. string = string.Trim(_T("\r"));
  617. string = string.Trim(_T("\n"));
  618. HGLOBAL hGlobal = NewGlobalP(string.GetBuffer(), ((string.GetLength() + 1) * sizeof(wchar_t)));
  619. pUnicodeText->Data(hGlobal);
  620. }
  621. IClipFormat *pAsciiText = clip.m_Formats.FindFormatEx(CF_TEXT);
  622. if (pAsciiText != NULL)
  623. {
  624. CStringA string(pAsciiText->GetAsCStringA());
  625. pAsciiText->Free();
  626. string = string.Trim();
  627. string = string.Trim("\t");
  628. string = string.Trim("\r");
  629. string = string.Trim("\n");
  630. HGLOBAL hGlobal = NewGlobalP(string.GetBuffer(), ((string.GetLength() + 1)));
  631. pAsciiText->Data(hGlobal);
  632. }
  633. }
  634. void COleClipSource::SaveDittoFileDataToFile(CClip &clip)
  635. {
  636. CFileRecieve hDrpData;
  637. CClipFormat* pCF;
  638. int hDropIndex = -1;
  639. bool savedFile = false;
  640. INT_PTR count = clip.m_Formats.GetSize();
  641. for (int i = 0; i < count; i++)
  642. {
  643. pCF = &clip.m_Formats.ElementAt(i);
  644. if (pCF->m_cfType == theApp.m_DittoFileData)
  645. {
  646. IClipFormat *dittoFileData = &clip.m_Formats.ElementAt(i);
  647. if (dittoFileData != NULL)
  648. {
  649. HGLOBAL data = dittoFileData->Data();
  650. char * stringData = (char *)GlobalLock(data);
  651. //original source is store in the first string ending in the null terminator
  652. CStringA src(stringData);
  653. stringData += src.GetLength() + 1;
  654. CStringA originalMd5(stringData);
  655. stringData += originalMd5.GetLength() + 1;
  656. int dataSize = (int)GlobalSize(data) - (src.GetLength() + 1) - (originalMd5.GetLength() + 1);
  657. CMd5 calcMd5;
  658. CStringA md5String = calcMd5.CalcMD5FromString(stringData, dataSize);
  659. CString unicodeFilePath;
  660. CTextConvert::ConvertFromUTF8(src, unicodeFilePath);
  661. CString unicodeMd5;
  662. CTextConvert::ConvertFromUTF8(md5String, unicodeMd5);
  663. Log(StrF(_T("Saving file contents from Ditto, original file: %s, size: %d, md5: %s"), unicodeFilePath, dataSize, unicodeMd5));
  664. if (md5String == originalMd5)
  665. {
  666. using namespace nsPath;
  667. CPath path(unicodeFilePath);
  668. CString fileName = path.GetName();
  669. CString newFilePath = CGetSetOptions::GetPath(PATH_DRAG_FILES);
  670. newFilePath += fileName;
  671. CFile f;
  672. if (f.Open(newFilePath, CFile::modeWrite | CFile::modeCreate))
  673. {
  674. f.Write(stringData, dataSize);
  675. f.Close();
  676. savedFile = true;
  677. hDrpData.AddFile(newFilePath);
  678. }
  679. else
  680. {
  681. Log(StrF(_T("Error saving file: %s"), unicodeFilePath));
  682. }
  683. }
  684. else
  685. {
  686. Log(StrF(_T("MD5 ERROR, file: %s, original md5: %s, calc md5: %s"), unicodeFilePath, originalMd5, md5String));
  687. }
  688. }
  689. }
  690. else if (pCF->m_cfType == CF_HDROP)
  691. {
  692. hDropIndex = i;
  693. }
  694. }
  695. if (savedFile)
  696. {
  697. if (hDropIndex >= 0)
  698. {
  699. clip.m_Formats.RemoveAt(hDropIndex);
  700. }
  701. CClipFormat cf(CF_HDROP, hDrpData.CreateCF_HDROPBuffer());
  702. clip.m_Formats.Add(cf);
  703. //clip.m_Formats now owns the global data
  704. cf.m_autoDeleteData = false;
  705. }
  706. }
  707. void COleClipSource::Typoglycemia(CClip &clip)
  708. {
  709. IClipFormat *unicodeTextFormat = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
  710. if (unicodeTextFormat != NULL)
  711. {
  712. CString cs(unicodeTextFormat->GetAsCString());
  713. //free the old text we are going to replace it below with an upper case version
  714. unicodeTextFormat->Free();
  715. CString newString;
  716. CTokenizer token(cs, _T(' '));
  717. CString word;
  718. while (token.Next(word))
  719. {
  720. if(word.GetLength() > 3)
  721. {
  722. int end = word.GetLength();
  723. for (int i = end-1; i >= 0; i--)
  724. {
  725. if(word[i] == _T('.') ||
  726. word[i] == _T('!') ||
  727. word[i] == _T('?'))
  728. {
  729. end--;
  730. }
  731. else
  732. {
  733. break;
  734. }
  735. }
  736. if (end > 3)
  737. {
  738. std::uniform_int_distribution<int> dist(1, end - 2);
  739. std::random_device rd;
  740. for (int i = 1; i < end - 1; i++)
  741. {
  742. int newPos = dist(rd);
  743. CString cs;
  744. cs.Format(_T("pos: %d, rnd: %d\r\n"), i, newPos);
  745. OutputDebugString(cs);
  746. TCHAR temp = word.GetAt(i);
  747. word.SetAt(i, word.GetAt(newPos));
  748. word.SetAt(newPos, temp);
  749. }
  750. }
  751. newString += word;
  752. }
  753. else
  754. {
  755. newString += word;
  756. }
  757. newString += _T(' ');
  758. }
  759. long len = newString.GetLength();
  760. HGLOBAL hGlobal = NewGlobalP(newString.GetBuffer(), ((len + 1) * sizeof(wchar_t)));
  761. unicodeTextFormat->Data(hGlobal);
  762. }
  763. }
  764. INT_PTR COleClipSource::PutFormatOnClipboard(CClipFormats *pFormats)
  765. {
  766. Log(_T("Start of put format on clipboard"));
  767. CClipFormat* pCF;
  768. INT_PTR count = pFormats->GetSize();
  769. bool bDelayedRenderCF_HDROP = false;
  770. bool dittoFileData = false;
  771. INT_PTR i = 0;
  772. //see if the html format is in the list
  773. //if it is the list we will not paste CF_TEXT
  774. for(i = 0; i < count; i++)
  775. {
  776. pCF = &pFormats->ElementAt(i);
  777. if(pCF->m_cfType == theApp.m_RemoteCF_HDROP)
  778. {
  779. bDelayedRenderCF_HDROP = true;
  780. }
  781. if (pCF->m_cfType == theApp.m_DittoFileData)
  782. {
  783. dittoFileData = true;
  784. //save file data
  785. //adjust hdrop
  786. }
  787. }
  788. for(i = 0; i < count; i++)
  789. {
  790. pCF = &pFormats->ElementAt(i);
  791. if(bDelayedRenderCF_HDROP)
  792. {
  793. if(pCF->m_cfType == CF_HDROP)
  794. {
  795. LogSendRecieveInfo("Added delayed cf_hdrop to clipboard");
  796. DelayRenderData(pCF->m_cfType);
  797. }
  798. continue;
  799. }
  800. Log(StrF(_T("Setting clipboard type: %s to the clipboard"), GetFormatName(pCF->m_cfType)));
  801. CacheGlobalData(pCF->m_cfType, pCF->m_hgData);
  802. pCF->m_hgData = 0; // OLE owns it now
  803. }
  804. pFormats->RemoveAll();
  805. m_bLoadedFormats = true;
  806. Log(_T("End of put format on clipboard"));
  807. return count;
  808. }
  809. BOOL COleClipSource::OnRenderGlobalData(LPFORMATETC lpFormatEtc, HGLOBAL* phGlobal)
  810. {
  811. static bool bInHere = false;
  812. if(bInHere)
  813. {
  814. return FALSE;
  815. }
  816. bInHere = true;
  817. HGLOBAL hData = NULL;
  818. CClipFormat *pFind = m_DelayRenderedFormats.FindFormat(lpFormatEtc->cfFormat);
  819. if(pFind)
  820. {
  821. if(pFind->m_hgData)
  822. {
  823. hData = NewGlobalH(pFind->m_hgData, GlobalSize(pFind->m_hgData));
  824. }
  825. }
  826. else
  827. {
  828. if (m_pasteOptions.m_delayRenderLockout > 0 &&
  829. (GetTickCount() - m_pasteOptions.m_delayRenderLockout) < (DWORD)CGetSetOptions::GetDelayRenderLockout())
  830. {
  831. bInHere = false;
  832. return false;
  833. }
  834. LogSendRecieveInfo("Delayed Render, getting data from remote machine");
  835. CClip clip;
  836. if(m_ClipIDs.GetCount() > 0)
  837. {
  838. clip.LoadFormats(m_ClipIDs[0]);
  839. CClipFormat *pDittoDelayCF_HDROP = clip.m_Formats.FindFormat(theApp.m_RemoteCF_HDROP);
  840. CClipFormat *pCF_HDROP = clip.m_Formats.FindFormat(CF_HDROP);
  841. if(pDittoDelayCF_HDROP && pCF_HDROP)
  842. {
  843. CDittoCF_HDROP *pData = (CDittoCF_HDROP*)GlobalLock(pDittoDelayCF_HDROP->m_hgData);
  844. if(pData)
  845. {
  846. CString csComputerName;
  847. CString csIP;
  848. CTextConvert::ConvertFromUTF8(pData->m_cIP, csIP);
  849. CTextConvert::ConvertFromUTF8(pData->m_cComputerName, csComputerName);
  850. GlobalUnlock(pDittoDelayCF_HDROP->m_hgData);
  851. CString ipPort = csIP;
  852. if (pData->respondPort > 0)
  853. {
  854. ipPort.Format(_T("%s:%d"), csIP, pData->respondPort);
  855. }
  856. CString namePort = csComputerName;
  857. if (pData->respondPort > 0)
  858. {
  859. namePort.Format(_T("%s:%d"), csComputerName, pData->respondPort);
  860. }
  861. CClient cl;
  862. hData = cl.RequestCopiedFiles(*pCF_HDROP, ipPort, namePort);
  863. }
  864. }
  865. else
  866. {
  867. hData = m_ClipIDs.Render(lpFormatEtc->cfFormat);
  868. if (m_convertToHDROPOnDelayRender &&
  869. hData == NULL &&
  870. lpFormatEtc->cfFormat == CF_HDROP)
  871. {
  872. hData = ConvertToFileDrop();
  873. }
  874. }
  875. }
  876. //Add to a cache of already rendered data
  877. //Windows seems to call this function multiple times
  878. //so only the first time do we need to go get the data
  879. HGLOBAL hCopy = NULL;
  880. if(hData)
  881. {
  882. hCopy = NewGlobalH(hData, GlobalSize(hData));
  883. }
  884. CClipFormat format(lpFormatEtc->cfFormat, hCopy);
  885. m_DelayRenderedFormats.Add(format);
  886. format.m_autoDeleteData = false; //owned by m_DelayRenderedFormats
  887. }
  888. BOOL bRet = FALSE;
  889. if(hData)
  890. {
  891. // if phGlobal is null, we can just give the allocated mem
  892. // else, our data must fit within the GlobalSize(*phGlobal)
  893. if(*phGlobal == 0)
  894. {
  895. *phGlobal = hData;
  896. }
  897. else
  898. {
  899. SIZE_T len = min(::GlobalSize(*phGlobal), ::GlobalSize(hData));
  900. if(len)
  901. {
  902. CopyToGlobalHH(*phGlobal, hData, len);
  903. }
  904. ::GlobalFree(hData);
  905. }
  906. bRet = TRUE;
  907. }
  908. bInHere = false;
  909. return bRet;
  910. }
  911. HGLOBAL COleClipSource::ConvertToFileDrop()
  912. {
  913. CString path = CGetSetOptions::GetPath(PATH_DRAG_FILES);
  914. CreateDirectory(path, NULL);
  915. CFileRecieve fileList;
  916. int dragId = CGetSetOptions::GetDragId();
  917. int origDragId = dragId;
  918. for (int i = 0; i < m_ClipIDs.GetCount(); i++)
  919. {
  920. CClip fileClip;
  921. fileClip.LoadFormats(m_ClipIDs[i]);
  922. CClipFormat *unicodeText = fileClip.m_Formats.FindFormat(CF_UNICODETEXT);
  923. if (unicodeText)
  924. {
  925. CString file;
  926. file.Format(_T("%stext_%d.txt"), path, dragId++);
  927. fileClip.WriteTextToFile(file, TRUE, FALSE, FALSE);
  928. fileList.AddFile(file);
  929. }
  930. else
  931. {
  932. CClipFormat *asciiText = fileClip.m_Formats.FindFormat(CF_TEXT);
  933. if (asciiText)
  934. {
  935. CString file;
  936. file.Format(_T("%stext_%d.txt"), path, dragId++);
  937. fileClip.WriteTextToFile(file, FALSE, TRUE, FALSE);
  938. fileList.AddFile(file);
  939. }
  940. else
  941. {
  942. CClipFormat *png = NULL;
  943. CClipFormat *bitmap = fileClip.m_Formats.FindFormat(CF_DIB);
  944. if (bitmap == NULL)
  945. {
  946. png = fileClip.m_Formats.FindFormat(theApp.m_PNG_Format);
  947. }
  948. if(bitmap != NULL ||
  949. png != NULL)
  950. {
  951. CString file;
  952. file.Format(_T("%simage_%d.png"), path, dragId++);
  953. if (fileClip.WriteImageToFile(file))
  954. {
  955. fileList.AddFile(file);
  956. }
  957. }
  958. }
  959. }
  960. }
  961. if(dragId != origDragId)
  962. {
  963. CGetSetOptions::SetDragId(dragId);
  964. }
  965. HGLOBAL hData = fileList.CreateCF_HDROPBuffer();
  966. return hData;
  967. }
  968. void COleClipSource::Slugify(CClip &clip)
  969. {
  970. IClipFormat *unicodeTextFormat = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
  971. if (unicodeTextFormat != NULL)
  972. {
  973. CString cs(unicodeTextFormat->GetAsCString());
  974. //free the old text we are going to replace it below with an upper case version
  975. unicodeTextFormat->Free();
  976. CString newString = slugify(cs.GetString()).c_str();
  977. long len = newString.GetLength();
  978. HGLOBAL hGlobal = NewGlobalP(newString.GetBuffer(), ((len + 1) * sizeof(wchar_t)));
  979. unicodeTextFormat->Data(hGlobal);
  980. }
  981. }