OleClipSource.cpp 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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 "sqlite\unicode\unistr.h"
  14. #include "sqlite\unicode\uchar.h"
  15. /*------------------------------------------------------------------*\
  16. COleClipSource
  17. \*------------------------------------------------------------------*/
  18. //IMPLEMENT_DYNAMIC(COleClipSource, COleDataSource)
  19. COleClipSource::COleClipSource()
  20. {
  21. m_bLoadedFormats = false;
  22. m_convertToHDROPOnDelayRender = false;
  23. }
  24. COleClipSource::~COleClipSource()
  25. {
  26. }
  27. BOOL COleClipSource::DoDelayRender()
  28. {
  29. CClipTypes types;
  30. m_ClipIDs.GetTypes(types);
  31. bool foundHDrop = false;
  32. INT_PTR count = types.GetSize();
  33. for(int i=0; i < count; i++)
  34. {
  35. DelayRenderData(types[i]);
  36. if (types[i] == CF_HDROP)
  37. {
  38. foundHDrop = true;
  39. }
  40. }
  41. if (foundHDrop == false)
  42. {
  43. DelayRenderData(CF_HDROP);
  44. m_convertToHDROPOnDelayRender = true;
  45. }
  46. return count > 0;
  47. }
  48. BOOL COleClipSource::DoImmediateRender()
  49. {
  50. if(m_bLoadedFormats)
  51. return TRUE;
  52. m_bLoadedFormats = true;
  53. if(m_pasteOptions.m_pPasteFormats != NULL)
  54. {
  55. return PutFormatOnClipboard(m_pasteOptions.m_pPasteFormats) > 0;
  56. }
  57. INT_PTR count = m_ClipIDs.GetSize();
  58. if(count <= 0)
  59. return 0;
  60. CClip clip;
  61. if(count > 1)
  62. {
  63. CStringA SepA = CTextConvert::ConvertToChar(g_Opt.GetMultiPasteSeparator());
  64. CCF_TextAggregator CFText(SepA);
  65. if(m_ClipIDs.AggregateData(CFText, CF_TEXT, g_Opt.m_bMultiPasteReverse))
  66. {
  67. CClipFormat cf(CF_TEXT, CFText.GetHGlobal());
  68. clip.m_Formats.Add(cf);
  69. //clip.m_Formats now owns the global data
  70. cf.m_autoDeleteData = false;
  71. }
  72. CStringW SepW = CTextConvert::ConvertToUnicode(g_Opt.GetMultiPasteSeparator());
  73. CCF_UnicodeTextAggregator CFUnicodeText(SepW);
  74. if(m_ClipIDs.AggregateData(CFUnicodeText, CF_UNICODETEXT, g_Opt.m_bMultiPasteReverse))
  75. {
  76. CClipFormat cf(CF_UNICODETEXT, CFUnicodeText.GetHGlobal());
  77. clip.m_Formats.Add(cf);
  78. //clip.m_Formats now owns the global data
  79. cf.m_autoDeleteData = false;
  80. }
  81. if ((m_pasteOptions.LimitFormatsToText()) &&
  82. clip.m_Formats.GetCount() == 0)
  83. {
  84. CCF_HDropAggregator HDrop;
  85. if (m_ClipIDs.AggregateData(HDrop, CF_HDROP, g_Opt.m_bMultiPasteReverse))
  86. {
  87. CClipFormat cf(CF_UNICODETEXT, HDrop.GetHGlobalAsString());
  88. clip.m_Formats.Add(cf);
  89. //clip.m_Formats now owns the global data
  90. cf.m_autoDeleteData = false;
  91. }
  92. }
  93. else if (m_pasteOptions.LimitFormatsToText() == false)
  94. {
  95. CCF_HDropAggregator HDrop;
  96. if(m_ClipIDs.AggregateData(HDrop, CF_HDROP, g_Opt.m_bMultiPasteReverse))
  97. {
  98. CClipFormat cf(CF_HDROP, HDrop.GetHGlobal());
  99. clip.m_Formats.Add(cf);
  100. //clip.m_Formats now owns the global data
  101. cf.m_autoDeleteData = false;
  102. }
  103. CRichTextAggregator RichText(SepA);
  104. if(m_ClipIDs.AggregateData(RichText, theApp.m_RTFFormat, g_Opt.m_bMultiPasteReverse))
  105. {
  106. CClipFormat cf(theApp.m_RTFFormat, RichText.GetHGlobal());
  107. clip.m_Formats.Add(cf);
  108. //clip.m_Formats now owns the global data
  109. cf.m_autoDeleteData = false;
  110. }
  111. CHTMLFormatAggregator Html(SepA);
  112. if(m_ClipIDs.AggregateData(Html, theApp.m_HTML_Format, g_Opt.m_bMultiPasteReverse))
  113. {
  114. CClipFormat cf(theApp.m_HTML_Format, Html.GetHGlobal());
  115. clip.m_Formats.Add(cf);
  116. //clip.m_Formats now owns the global data
  117. cf.m_autoDeleteData = false;
  118. }
  119. }
  120. }
  121. if (count >= 1 && clip.m_Formats.GetCount() == 0)
  122. {
  123. clip.LoadFormats(m_ClipIDs[0], m_pasteOptions.LimitFormatsToText(), m_pasteOptions.IncludeRTFForTextOnly());
  124. }
  125. if (m_pasteOptions.LimitFormatsToText())
  126. {
  127. PlainTextFilter(clip);
  128. }
  129. if(m_pasteOptions.m_pasteUpperCase ||
  130. m_pasteOptions.m_pasteLowerCase)
  131. {
  132. DoUpperLowerCase(clip, m_pasteOptions.m_pasteUpperCase);
  133. }
  134. else if(m_pasteOptions.m_pasteCapitalize)
  135. {
  136. Capitalize(clip);
  137. }
  138. else if(m_pasteOptions.m_pasteSentenceCase)
  139. {
  140. SentenceCase(clip);
  141. }
  142. else if(m_pasteOptions.m_pasteRemoveLineFeeds)
  143. {
  144. RemoveLineFeeds(clip);
  145. }
  146. else if(m_pasteOptions.m_pasteAddOneLineFeed)
  147. {
  148. AddLineFeeds(clip, 1);
  149. }
  150. else if (m_pasteOptions.m_pasteAddTwoLineFeeds)
  151. {
  152. AddLineFeeds(clip, 2);
  153. }
  154. else if (m_pasteOptions.m_pasteTypoglycemia)
  155. {
  156. Typoglycemia(clip);
  157. }
  158. else if (m_pasteOptions.m_pasteAddingDateTime)
  159. {
  160. AddDateTime(clip);
  161. }
  162. return PutFormatOnClipboard(&clip.m_Formats) > 0;
  163. }
  164. void COleClipSource::DoUpperLowerCase(CClip &clip, bool upper)
  165. {
  166. IClipFormat *unicodeTextFormat = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
  167. if (unicodeTextFormat != NULL)
  168. {
  169. HGLOBAL data = unicodeTextFormat->Data();
  170. wchar_t * stringData = (wchar_t *) GlobalLock(data);
  171. int size = (int) GlobalSize(data);
  172. icu::UnicodeString s = stringData;
  173. GlobalUnlock(data);
  174. //free the old text we are going to replace it below with an upper case version
  175. unicodeTextFormat->Free();
  176. icu::UnicodeString val;
  177. if (upper)
  178. {
  179. val = s.toUpper();
  180. }
  181. else
  182. {
  183. val = s.toLower();
  184. }
  185. long lLen = val.length();
  186. HGLOBAL hGlobal = NewGlobalP((LPVOID)val.getTerminatedBuffer(), ((lLen+1) * sizeof(wchar_t)));
  187. val.releaseBuffer();
  188. unicodeTextFormat->Data(hGlobal);
  189. }
  190. IClipFormat *asciiTextFormat = clip.m_Formats.FindFormatEx(CF_TEXT);
  191. if (asciiTextFormat != NULL)
  192. {
  193. HGLOBAL data = asciiTextFormat->Data();
  194. char * stringData = (char *) GlobalLock(data);
  195. int size = (int) GlobalSize(data);
  196. CStringA cs(stringData);
  197. GlobalUnlock(data);
  198. //free the old text we are going to replace it below with an upper case version
  199. asciiTextFormat->Free();
  200. CString val;
  201. if (upper)
  202. {
  203. val = cs.MakeUpper();
  204. }
  205. else
  206. {
  207. val = cs.MakeLower();
  208. }
  209. long lLen = val.GetLength();
  210. HGLOBAL hGlobal = NewGlobalP(val.GetBuffer(lLen), lLen + sizeof(char));
  211. val.ReleaseBuffer();
  212. asciiTextFormat->Data(hGlobal);
  213. }
  214. }
  215. void COleClipSource::Capitalize(CClip &clip)
  216. {
  217. IClipFormat *unicodeTextFormat = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
  218. if (unicodeTextFormat != NULL)
  219. {
  220. HGLOBAL data = unicodeTextFormat->Data();
  221. wchar_t * stringData = (wchar_t *) GlobalLock(data);
  222. int size = (int) GlobalSize(data);
  223. CString cs(stringData);
  224. GlobalUnlock(data);
  225. //free the old text we are going to replace it below with an upper case version
  226. unicodeTextFormat->Free();
  227. icu::UnicodeString temp = cs;
  228. CString val = temp.toLower().getTerminatedBuffer();
  229. long len = val.GetLength();
  230. if (len > 0)
  231. {
  232. wchar_t * pText = val.GetBuffer();
  233. pText[0] = u_toupper(pText[0]);
  234. bool capitalize = false;
  235. for (int i = 1; i < len; i++)
  236. {
  237. wchar_t item = pText[i];
  238. if (item == ' ')
  239. {
  240. capitalize = true;
  241. }
  242. else if (capitalize)
  243. {
  244. pText[i] = u_toupper(item);
  245. capitalize = false;
  246. }
  247. }
  248. }
  249. val.ReleaseBuffer();
  250. HGLOBAL hGlobal = NewGlobalP(val.GetBuffer(), ((len + 1) * sizeof(wchar_t)));
  251. unicodeTextFormat->Data(hGlobal);
  252. }
  253. //my change
  254. //test
  255. //second test
  256. IClipFormat *asciiTextFormat = clip.m_Formats.FindFormatEx(CF_TEXT);
  257. if (asciiTextFormat != NULL)
  258. {
  259. HGLOBAL data = asciiTextFormat->Data();
  260. char * stringData = (char *) GlobalLock(data);
  261. int size = (int) GlobalSize(data);
  262. CStringA cs(stringData);
  263. GlobalUnlock(data);
  264. //free the old text we are going to replace it below with an upper case version
  265. asciiTextFormat->Free();
  266. CStringA val = cs.MakeLower();
  267. long len = val.GetLength();
  268. if (len > 0)
  269. {
  270. char * pText = val.GetBuffer();
  271. pText[0] = toupper(pText[0]);
  272. bool capitalize = false;
  273. for (int i = 1; i < len; i++)
  274. {
  275. wchar_t item = pText[i];
  276. if (item == ' ')
  277. {
  278. capitalize = true;
  279. }
  280. else if (capitalize)
  281. {
  282. pText[i] = toupper(item);
  283. capitalize = false;
  284. }
  285. }
  286. }
  287. val.ReleaseBuffer();
  288. HGLOBAL hGlobal = NewGlobalP(val.GetBuffer(), (len + 1));
  289. asciiTextFormat->Data(hGlobal);
  290. }
  291. }
  292. void COleClipSource::SentenceCase(CClip &clip)
  293. {
  294. IClipFormat *unicodeTextFormat = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
  295. if (unicodeTextFormat != NULL)
  296. {
  297. HGLOBAL data = unicodeTextFormat->Data();
  298. wchar_t * stringData = (wchar_t *) GlobalLock(data);
  299. int size = (int) GlobalSize(data);
  300. CString cs(stringData);
  301. GlobalUnlock(data);
  302. //free the old text we are going to replace it below with an upper case version
  303. unicodeTextFormat->Free();
  304. icu::UnicodeString temp = cs;
  305. CString val = temp.toLower().getTerminatedBuffer();
  306. long len = val.GetLength();
  307. if (len > 0)
  308. {
  309. wchar_t * pText = val.GetBuffer();
  310. pText[0] = u_toupper(pText[0]);
  311. bool capitalize = false;
  312. for (int i = 1; i < len; i++)
  313. {
  314. wchar_t item = pText[i];
  315. if (item == '.' ||
  316. item == '!' ||
  317. item == '?')
  318. {
  319. capitalize = true;
  320. }
  321. else if (capitalize && item != ' ')
  322. {
  323. pText[i] = u_toupper(item);
  324. capitalize = false;
  325. }
  326. }
  327. }
  328. val.ReleaseBuffer();
  329. HGLOBAL hGlobal = NewGlobalP(val.GetBuffer(), ((len + 1) * sizeof(wchar_t)));
  330. unicodeTextFormat->Data(hGlobal);
  331. }
  332. IClipFormat *asciiTextFormat = clip.m_Formats.FindFormatEx(CF_TEXT);
  333. if (asciiTextFormat != NULL)
  334. {
  335. HGLOBAL data = asciiTextFormat->Data();
  336. char * stringData = (char *) GlobalLock(data);
  337. int size = (int) GlobalSize(data);
  338. CStringA cs(stringData);
  339. GlobalUnlock(data);
  340. //free the old text we are going to replace it below with an upper case version
  341. asciiTextFormat->Free();
  342. CStringA val = cs.MakeLower();
  343. long len = val.GetLength();
  344. if (len > 0)
  345. {
  346. char * pText = val.GetBuffer();
  347. pText[0] = toupper(pText[0]);
  348. bool capitalize = false;
  349. for (int i = 1; i < len; i++)
  350. {
  351. wchar_t item = pText[i];
  352. if (item == '.' ||
  353. item == '!' ||
  354. item == '?')
  355. {
  356. capitalize = true;
  357. }
  358. else if (capitalize && item != ' ')
  359. {
  360. pText[i] = toupper(item);
  361. capitalize = false;
  362. }
  363. }
  364. }
  365. val.ReleaseBuffer();
  366. HGLOBAL hGlobal = NewGlobalP(val.GetBuffer(), (len + 1));
  367. asciiTextFormat->Data(hGlobal);
  368. }
  369. }
  370. void COleClipSource::PlainTextFilter(CClip &clip)
  371. {
  372. bool foundText = false;
  373. INT_PTR hDropIndex = -1;
  374. INT_PTR count = clip.m_Formats.GetCount();
  375. for (INT_PTR i = 0; i < count; i++)
  376. {
  377. CClipFormat *pCF = &clip.m_Formats.ElementAt(i);
  378. if (pCF->m_cfType == CF_TEXT ||
  379. pCF->m_cfType == CF_UNICODETEXT)
  380. {
  381. foundText = true;
  382. }
  383. else if (pCF->m_cfType == CF_HDROP)
  384. {
  385. hDropIndex = i;
  386. }
  387. }
  388. if (foundText &&
  389. hDropIndex > -1)
  390. {
  391. clip.m_Formats.RemoveAt(hDropIndex);
  392. }
  393. else if (foundText == false &&
  394. hDropIndex > -1)
  395. {
  396. CCF_HDropAggregator HDrop;
  397. if (m_ClipIDs.AggregateData(HDrop, CF_HDROP, g_Opt.m_bMultiPasteReverse))
  398. {
  399. clip.m_Formats.RemoveAt(hDropIndex);
  400. CClipFormat format(CF_UNICODETEXT, HDrop.GetHGlobalAsString());
  401. clip.m_Formats.Add(format);
  402. format.m_autoDeleteData = false; //owned by m_DelayRenderedFormats
  403. }
  404. }
  405. }
  406. void COleClipSource::RemoveLineFeeds(CClip &clip)
  407. {
  408. IClipFormat *pUnicodeText = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
  409. if (pUnicodeText != NULL)
  410. {
  411. wchar_t *stringData = (wchar_t *) GlobalLock(pUnicodeText->Data());
  412. if (stringData != NULL)
  413. {
  414. CStringW string(stringData);
  415. GlobalUnlock(pUnicodeText->Data());
  416. pUnicodeText->Free();
  417. int count = string.Replace(_T("\r\n"), _T(" "));
  418. count = string.Replace(_T("\r"), _T(" "));
  419. count = string.Replace(_T("\n"), _T(" "));
  420. HGLOBAL hGlobal = NewGlobalP(string.GetBuffer(), ((string.GetLength() + 1) * sizeof(wchar_t)));
  421. pUnicodeText->Data(hGlobal);
  422. }
  423. }
  424. IClipFormat *pAsciiText = clip.m_Formats.FindFormatEx(CF_TEXT);
  425. if (pAsciiText != NULL)
  426. {
  427. char *stringData = (char *) GlobalLock(pAsciiText->Data());
  428. if (stringData != NULL)
  429. {
  430. CStringA string(stringData);
  431. GlobalUnlock(pAsciiText->Data());
  432. pAsciiText->Free();
  433. int count = string.Replace("\r\n", " ");
  434. count = string.Replace("\r", " ");
  435. count = string.Replace("\n", " ");
  436. HGLOBAL hGlobal = NewGlobalP(string.GetBuffer(), ((string.GetLength() + 1)));
  437. pAsciiText->Data(hGlobal);
  438. }
  439. }
  440. IClipFormat *pRTFFormat = clip.m_Formats.FindFormatEx(theApp.m_RTFFormat);
  441. if (pRTFFormat != NULL)
  442. {
  443. char *stringData = (char *) GlobalLock(pRTFFormat->Data());
  444. if (stringData != NULL)
  445. {
  446. CStringA string(stringData);
  447. GlobalUnlock(pRTFFormat->Data());
  448. pRTFFormat->Free();
  449. int count = string.Replace("\\par\r\n", " ");
  450. int count2 = string.Replace("\\par ", " ");
  451. int count3 = string.Replace("\\line ", " ");
  452. HGLOBAL hGlobal = NewGlobalP(string.GetBuffer(), ((string.GetLength() + 1)));
  453. pRTFFormat->Data(hGlobal);
  454. }
  455. }
  456. }
  457. void COleClipSource::AddLineFeeds(CClip &clip, int count)
  458. {
  459. IClipFormat *pUnicodeText = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
  460. if (pUnicodeText != NULL)
  461. {
  462. wchar_t *stringData = (wchar_t *) GlobalLock(pUnicodeText->Data());
  463. if (stringData != NULL)
  464. {
  465. CStringW string(stringData);
  466. GlobalUnlock(pUnicodeText->Data());
  467. pUnicodeText->Free();
  468. for(int i = 0; i < count; i++)
  469. {
  470. string += _T("\r\n");
  471. }
  472. HGLOBAL hGlobal = NewGlobalP(string.GetBuffer(), ((string.GetLength() + 1) * sizeof(wchar_t)));
  473. pUnicodeText->Data(hGlobal);
  474. }
  475. }
  476. IClipFormat *pAsciiText = clip.m_Formats.FindFormatEx(CF_TEXT);
  477. if (pAsciiText != NULL)
  478. {
  479. char *stringData = (char *) GlobalLock(pAsciiText->Data());
  480. if (stringData != NULL)
  481. {
  482. CStringA string(stringData);
  483. GlobalUnlock(pAsciiText->Data());
  484. pAsciiText->Free();
  485. for (int i = 0; i < count; i++)
  486. {
  487. string += _T("\r\n");
  488. }
  489. HGLOBAL hGlobal = NewGlobalP(string.GetBuffer(), ((string.GetLength() + 1)));
  490. pAsciiText->Data(hGlobal);
  491. }
  492. }
  493. IClipFormat *pRTFFormat = clip.m_Formats.FindFormatEx(theApp.m_RTFFormat);
  494. if (pRTFFormat != NULL)
  495. {
  496. char *stringData = (char *) GlobalLock(pRTFFormat->Data());
  497. if (stringData != NULL)
  498. {
  499. CStringA string(stringData);
  500. GlobalUnlock(pRTFFormat->Data());
  501. pRTFFormat->Free();
  502. for (int i = 0; i < count; i++)
  503. {
  504. int pos = string.ReverseFind('}');
  505. if (pos >= 0)
  506. {
  507. int count = string.Insert(pos, "\\par\r\n");
  508. }
  509. }
  510. HGLOBAL hGlobal = NewGlobalP(string.GetBuffer(), ((string.GetLength() + 1)));
  511. pRTFFormat->Data(hGlobal);
  512. }
  513. }
  514. }
  515. void COleClipSource::AddDateTime(CClip &clip)
  516. {
  517. IClipFormat *pUnicodeText = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
  518. if (pUnicodeText != NULL)
  519. {
  520. wchar_t *stringData = (wchar_t *)GlobalLock(pUnicodeText->Data());
  521. if (stringData != NULL)
  522. {
  523. CStringW string(stringData);
  524. GlobalUnlock(pUnicodeText->Data());
  525. pUnicodeText->Free();
  526. string += _T("\r\n");
  527. COleDateTime now(COleDateTime::GetCurrentTime());
  528. string += now.Format();
  529. HGLOBAL hGlobal = NewGlobalP(string.GetBuffer(), ((string.GetLength() + 1) * sizeof(wchar_t)));
  530. pUnicodeText->Data(hGlobal);
  531. }
  532. }
  533. IClipFormat *pAsciiText = clip.m_Formats.FindFormatEx(CF_TEXT);
  534. if (pAsciiText != NULL)
  535. {
  536. char *stringData = (char *)GlobalLock(pAsciiText->Data());
  537. if (stringData != NULL)
  538. {
  539. CStringA string(stringData);
  540. GlobalUnlock(pAsciiText->Data());
  541. pAsciiText->Free();
  542. string += "\r\n\r\n";
  543. COleDateTime now(COleDateTime::GetCurrentTime());
  544. string += CTextConvert::UnicodeStringToMultiByte(now.Format());
  545. HGLOBAL hGlobal = NewGlobalP(string.GetBuffer(), ((string.GetLength() + 1)));
  546. pAsciiText->Data(hGlobal);
  547. }
  548. }
  549. IClipFormat *pRTFFormat = clip.m_Formats.FindFormatEx(theApp.m_RTFFormat);
  550. if (pRTFFormat != NULL)
  551. {
  552. char *stringData = (char *)GlobalLock(pRTFFormat->Data());
  553. if (stringData != NULL)
  554. {
  555. CStringA string(stringData);
  556. GlobalUnlock(pRTFFormat->Data());
  557. pRTFFormat->Free();
  558. int pos = string.ReverseFind('}');
  559. if (pos >= 0)
  560. {
  561. string += _T("\r\n\r\n");
  562. COleDateTime now(COleDateTime::GetCurrentTime());
  563. CStringA insert;
  564. insert.Format("\\par\r\n\\par\r\n%s", CTextConvert::UnicodeStringToMultiByte(now.Format()));
  565. int count = string.Insert(pos, insert);
  566. }
  567. HGLOBAL hGlobal = NewGlobalP(string.GetBuffer(), ((string.GetLength() + 1)));
  568. pRTFFormat->Data(hGlobal);
  569. }
  570. }
  571. }
  572. void COleClipSource::Typoglycemia(CClip &clip)
  573. {
  574. IClipFormat *unicodeTextFormat = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
  575. if (unicodeTextFormat != NULL)
  576. {
  577. HGLOBAL data = unicodeTextFormat->Data();
  578. wchar_t * stringData = (wchar_t *) GlobalLock(data);
  579. int size = (int) GlobalSize(data);
  580. CString cs(stringData);
  581. GlobalUnlock(data);
  582. //free the old text we are going to replace it below with an upper case version
  583. unicodeTextFormat->Free();
  584. CString newString;
  585. CTokenizer token(cs, _T(' '));
  586. CString word;
  587. while (token.Next(word))
  588. {
  589. if(word.GetLength() > 3)
  590. {
  591. int end = word.GetLength();
  592. for (int i = end-1; i >= 0; i--)
  593. {
  594. if(word[i] == _T('.') ||
  595. word[i] == _T('!') ||
  596. word[i] == _T('?'))
  597. {
  598. end--;
  599. }
  600. else
  601. {
  602. break;
  603. }
  604. }
  605. if (end > 3)
  606. {
  607. std::uniform_int_distribution<int> dist(1, end - 2);
  608. std::random_device rd;
  609. for (int i = 1; i < end - 1; i++)
  610. {
  611. int newPos = dist(rd);
  612. CString cs;
  613. cs.Format(_T("pos: %d, rnd: %d\r\n"), i, newPos);
  614. OutputDebugString(cs);
  615. TCHAR temp = word.GetAt(i);
  616. word.SetAt(i, word.GetAt(newPos));
  617. word.SetAt(newPos, temp);
  618. }
  619. }
  620. newString += word;
  621. }
  622. else
  623. {
  624. newString += word;
  625. }
  626. newString += _T(' ');
  627. }
  628. long len = newString.GetLength();
  629. HGLOBAL hGlobal = NewGlobalP(newString.GetBuffer(), ((len + 1) * sizeof(wchar_t)));
  630. unicodeTextFormat->Data(hGlobal);
  631. }
  632. }
  633. INT_PTR COleClipSource::PutFormatOnClipboard(CClipFormats *pFormats)
  634. {
  635. Log(_T("Start of put format on clipboard"));
  636. CClipFormat* pCF;
  637. INT_PTR count = pFormats->GetSize();
  638. bool bDelayedRenderCF_HDROP = false;
  639. INT_PTR i = 0;
  640. //see if the html format is in the list
  641. //if it is the list we will not paste CF_TEXT
  642. for(i = 0; i < count; i++)
  643. {
  644. pCF = &pFormats->ElementAt(i);
  645. if(pCF->m_cfType == theApp.m_RemoteCF_HDROP)
  646. {
  647. bDelayedRenderCF_HDROP = true;
  648. }
  649. }
  650. for(i = 0; i < count; i++)
  651. {
  652. pCF = &pFormats->ElementAt(i);
  653. if(bDelayedRenderCF_HDROP)
  654. {
  655. if(pCF->m_cfType == CF_HDROP)
  656. {
  657. LogSendRecieveInfo("Added delayed cf_hdrop to clipboard");
  658. DelayRenderData(pCF->m_cfType);
  659. }
  660. continue;
  661. }
  662. wchar_t * stringData = (wchar_t *) GlobalLock(pCF->m_hgData);
  663. int size = (int) GlobalSize(pCF->m_hgData);
  664. CString cs(stringData);
  665. GlobalUnlock(pCF->m_hgData);
  666. Log(StrF(_T("Setting clipboard type: %s to the clipboard"), GetFormatName(pCF->m_cfType)));
  667. CacheGlobalData(pCF->m_cfType, pCF->m_hgData);
  668. pCF->m_hgData = 0; // OLE owns it now
  669. }
  670. pFormats->RemoveAll();
  671. m_bLoadedFormats = true;
  672. Log(_T("End of put format on clipboard"));
  673. return count;
  674. }
  675. BOOL COleClipSource::OnRenderGlobalData(LPFORMATETC lpFormatEtc, HGLOBAL* phGlobal)
  676. {
  677. static bool bInHere = false;
  678. if(bInHere)
  679. {
  680. return FALSE;
  681. }
  682. bInHere = true;
  683. HGLOBAL hData = NULL;
  684. CClipFormat *pFind = m_DelayRenderedFormats.FindFormat(lpFormatEtc->cfFormat);
  685. if(pFind)
  686. {
  687. if(pFind->m_hgData)
  688. {
  689. hData = NewGlobalH(pFind->m_hgData, GlobalSize(pFind->m_hgData));
  690. }
  691. }
  692. else
  693. {
  694. LogSendRecieveInfo("Delayed Render, getting data from remote machine");
  695. CClip clip;
  696. if(m_ClipIDs.GetCount() > 0)
  697. {
  698. clip.LoadFormats(m_ClipIDs[0]);
  699. CClipFormat *pDittoDelayCF_HDROP = clip.m_Formats.FindFormat(theApp.m_RemoteCF_HDROP);
  700. CClipFormat *pCF_HDROP = clip.m_Formats.FindFormat(CF_HDROP);
  701. if(pDittoDelayCF_HDROP && pCF_HDROP)
  702. {
  703. CDittoCF_HDROP *pData = (CDittoCF_HDROP*)GlobalLock(pDittoDelayCF_HDROP->m_hgData);
  704. if(pData)
  705. {
  706. CString csComputerName;
  707. CString csIP;
  708. CTextConvert::ConvertFromUTF8(pData->m_cIP, csIP);
  709. CTextConvert::ConvertFromUTF8(pData->m_cComputerName, csComputerName);
  710. GlobalUnlock(pDittoDelayCF_HDROP->m_hgData);
  711. CClient cl;
  712. hData = cl.RequestCopiedFiles(*pCF_HDROP, csIP, csComputerName);
  713. }
  714. }
  715. else
  716. {
  717. hData = m_ClipIDs.Render(lpFormatEtc->cfFormat);
  718. if (m_convertToHDROPOnDelayRender &&
  719. hData == NULL &&
  720. lpFormatEtc->cfFormat == CF_HDROP)
  721. {
  722. hData = ConvertToFileDrop();
  723. }
  724. }
  725. }
  726. //Add to a cache of already rendered data
  727. //Windows seems to call this function multiple times
  728. //so only the first time do we need to go get the data
  729. HGLOBAL hCopy = NULL;
  730. if(hData)
  731. {
  732. hCopy = NewGlobalH(hData, GlobalSize(hData));
  733. }
  734. CClipFormat format(lpFormatEtc->cfFormat, hCopy);
  735. m_DelayRenderedFormats.Add(format);
  736. format.m_autoDeleteData = false; //owned by m_DelayRenderedFormats
  737. }
  738. BOOL bRet = FALSE;
  739. if(hData)
  740. {
  741. // if phGlobal is null, we can just give the allocated mem
  742. // else, our data must fit within the GlobalSize(*phGlobal)
  743. if(*phGlobal == 0)
  744. {
  745. *phGlobal = hData;
  746. }
  747. else
  748. {
  749. SIZE_T len = min(::GlobalSize(*phGlobal), ::GlobalSize(hData));
  750. if(len)
  751. {
  752. CopyToGlobalHH(*phGlobal, hData, len);
  753. }
  754. ::GlobalFree(hData);
  755. }
  756. bRet = TRUE;
  757. }
  758. bInHere = false;
  759. return bRet;
  760. }
  761. HGLOBAL COleClipSource::ConvertToFileDrop()
  762. {
  763. CString path = CGetSetOptions::GetPath(PATH_DRAG_FILES);
  764. CreateDirectory(path, NULL);
  765. CFileRecieve fileList;
  766. int dragId = CGetSetOptions::GetDragId();
  767. int origDragId = dragId;
  768. for (int i = 0; i < m_ClipIDs.GetCount(); i++)
  769. {
  770. CClip fileClip;
  771. fileClip.LoadFormats(m_ClipIDs[i]);
  772. CClipFormat *unicodeText = fileClip.m_Formats.FindFormat(CF_UNICODETEXT);
  773. if (unicodeText)
  774. {
  775. CString file;
  776. file.Format(_T("%stext_%d.txt"), path, dragId++);
  777. fileClip.WriteTextToFile(file, TRUE, FALSE, FALSE);
  778. fileList.AddFile(file);
  779. }
  780. else
  781. {
  782. CClipFormat *asciiText = fileClip.m_Formats.FindFormat(CF_TEXT);
  783. if (asciiText)
  784. {
  785. CString file;
  786. file.Format(_T("%stext_%d.txt"), path, dragId++);
  787. fileClip.WriteTextToFile(file, FALSE, TRUE, FALSE);
  788. fileList.AddFile(file);
  789. }
  790. else
  791. {
  792. CClipFormat *bitmap = fileClip.m_Formats.FindFormat(CF_DIB);
  793. if (bitmap)
  794. {
  795. CString file;
  796. file.Format(_T("%simage_%d.png"), path, dragId++);
  797. LPVOID pvData = GlobalLock(bitmap->m_hgData);
  798. ULONG size = (ULONG) GlobalSize(bitmap->m_hgData);
  799. WriteCF_DIBToFile(file, pvData, size);
  800. GlobalUnlock(bitmap->m_hgData);
  801. fileList.AddFile(file);
  802. }
  803. }
  804. }
  805. }
  806. if(dragId != origDragId)
  807. {
  808. CGetSetOptions::SetDragId(dragId);
  809. }
  810. HGLOBAL hData = fileList.CreateCF_HDROPBuffer();
  811. return hData;
  812. }