OleClipSource.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  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. 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::UnicodeToAnsi(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::UnicodeToAnsi(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::UnicodeToAnsi(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 = CTextConvert::Utf8ToUnicode(src);
  660. CString unicodeMd5 = CTextConvert::Utf8ToUnicode(md5String);
  661. Log(StrF(_T("Saving file contents from Ditto, original file: %s, size: %d, md5: %s"), unicodeFilePath, dataSize, unicodeMd5));
  662. if (md5String == originalMd5)
  663. {
  664. using namespace nsPath;
  665. CPath path(unicodeFilePath);
  666. CString fileName = path.GetName();
  667. CString newFilePath = CGetSetOptions::GetPath(PATH_DRAG_FILES);
  668. newFilePath += fileName;
  669. CFile f;
  670. if (f.Open(newFilePath, CFile::modeWrite | CFile::modeCreate))
  671. {
  672. f.Write(stringData, dataSize);
  673. f.Close();
  674. savedFile = true;
  675. hDrpData.AddFile(newFilePath);
  676. }
  677. else
  678. {
  679. Log(StrF(_T("Error saving file: %s"), unicodeFilePath));
  680. }
  681. }
  682. else
  683. {
  684. Log(StrF(_T("MD5 ERROR, file: %s, original md5: %s, calc md5: %s"), unicodeFilePath, originalMd5, md5String));
  685. }
  686. }
  687. }
  688. else if (pCF->m_cfType == CF_HDROP)
  689. {
  690. hDropIndex = i;
  691. }
  692. }
  693. if (savedFile)
  694. {
  695. if (hDropIndex >= 0)
  696. {
  697. clip.m_Formats.RemoveAt(hDropIndex);
  698. }
  699. CClipFormat cf(CF_HDROP, hDrpData.CreateCF_HDROPBuffer());
  700. clip.m_Formats.Add(cf);
  701. //clip.m_Formats now owns the global data
  702. cf.m_autoDeleteData = false;
  703. }
  704. }
  705. void COleClipSource::Typoglycemia(CClip &clip)
  706. {
  707. IClipFormat *unicodeTextFormat = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
  708. if (unicodeTextFormat != NULL)
  709. {
  710. CString cs(unicodeTextFormat->GetAsCString());
  711. //free the old text we are going to replace it below with an upper case version
  712. unicodeTextFormat->Free();
  713. CString newString;
  714. CTokenizer token(cs, _T(' '));
  715. CString word;
  716. while (token.Next(word))
  717. {
  718. if(word.GetLength() > 3)
  719. {
  720. int end = word.GetLength();
  721. for (int i = end-1; i >= 0; i--)
  722. {
  723. if(word[i] == _T('.') ||
  724. word[i] == _T('!') ||
  725. word[i] == _T('?'))
  726. {
  727. end--;
  728. }
  729. else
  730. {
  731. break;
  732. }
  733. }
  734. if (end > 3)
  735. {
  736. std::uniform_int_distribution<int> dist(1, end - 2);
  737. std::random_device rd;
  738. for (int i = 1; i < end - 1; i++)
  739. {
  740. int newPos = dist(rd);
  741. CString cs;
  742. cs.Format(_T("pos: %d, rnd: %d\r\n"), i, newPos);
  743. OutputDebugString(cs);
  744. TCHAR temp = word.GetAt(i);
  745. word.SetAt(i, word.GetAt(newPos));
  746. word.SetAt(newPos, temp);
  747. }
  748. }
  749. newString += word;
  750. }
  751. else
  752. {
  753. newString += word;
  754. }
  755. newString += _T(' ');
  756. }
  757. long len = newString.GetLength();
  758. HGLOBAL hGlobal = NewGlobalP(newString.GetBuffer(), ((len + 1) * sizeof(wchar_t)));
  759. unicodeTextFormat->Data(hGlobal);
  760. }
  761. }
  762. INT_PTR COleClipSource::PutFormatOnClipboard(CClipFormats *pFormats)
  763. {
  764. Log(_T("Start of put format on clipboard"));
  765. CClipFormat* pCF;
  766. INT_PTR count = pFormats->GetSize();
  767. bool bDelayedRenderCF_HDROP = false;
  768. bool dittoFileData = false;
  769. INT_PTR i = 0;
  770. //see if the html format is in the list
  771. //if it is the list we will not paste CF_TEXT
  772. for(i = 0; i < count; i++)
  773. {
  774. pCF = &pFormats->ElementAt(i);
  775. if(pCF->m_cfType == theApp.m_RemoteCF_HDROP)
  776. {
  777. bDelayedRenderCF_HDROP = true;
  778. }
  779. if (pCF->m_cfType == theApp.m_DittoFileData)
  780. {
  781. dittoFileData = true;
  782. //save file data
  783. //adjust hdrop
  784. }
  785. }
  786. for(i = 0; i < count; i++)
  787. {
  788. pCF = &pFormats->ElementAt(i);
  789. if(bDelayedRenderCF_HDROP)
  790. {
  791. if(pCF->m_cfType == CF_HDROP)
  792. {
  793. LogSendRecieveInfo("Added delayed cf_hdrop to clipboard");
  794. DelayRenderData(pCF->m_cfType);
  795. }
  796. continue;
  797. }
  798. Log(StrF(_T("Setting clipboard type: %s to the clipboard"), GetFormatName(pCF->m_cfType)));
  799. CacheGlobalData(pCF->m_cfType, pCF->m_hgData);
  800. pCF->m_hgData = 0; // OLE owns it now
  801. }
  802. pFormats->RemoveAll();
  803. m_bLoadedFormats = true;
  804. Log(_T("End of put format on clipboard"));
  805. return count;
  806. }
  807. BOOL COleClipSource::OnRenderGlobalData(LPFORMATETC lpFormatEtc, HGLOBAL* phGlobal)
  808. {
  809. static bool bInHere = false;
  810. if(bInHere)
  811. {
  812. return FALSE;
  813. }
  814. bInHere = true;
  815. HGLOBAL hData = NULL;
  816. CClipFormat *pFind = m_DelayRenderedFormats.FindFormat(lpFormatEtc->cfFormat);
  817. if(pFind)
  818. {
  819. if(pFind->m_hgData)
  820. {
  821. hData = NewGlobalH(pFind->m_hgData, GlobalSize(pFind->m_hgData));
  822. }
  823. }
  824. else
  825. {
  826. if (m_pasteOptions.m_delayRenderLockout > 0 &&
  827. (GetTickCount() - m_pasteOptions.m_delayRenderLockout) < (DWORD)CGetSetOptions::GetDelayRenderLockout())
  828. {
  829. bInHere = false;
  830. return false;
  831. }
  832. LogSendRecieveInfo("Delayed Render, getting data from remote machine");
  833. CClip clip;
  834. if(m_ClipIDs.GetCount() > 0)
  835. {
  836. clip.LoadFormats(m_ClipIDs[0]);
  837. CClipFormat *pDittoDelayCF_HDROP = clip.m_Formats.FindFormat(theApp.m_RemoteCF_HDROP);
  838. CClipFormat *pCF_HDROP = clip.m_Formats.FindFormat(CF_HDROP);
  839. if(pDittoDelayCF_HDROP && pCF_HDROP)
  840. {
  841. CDittoCF_HDROP *pData = (CDittoCF_HDROP*)GlobalLock(pDittoDelayCF_HDROP->m_hgData);
  842. if(pData)
  843. {
  844. CString csComputerName;
  845. CString csIP;
  846. csIP = CTextConvert::Utf8ToUnicode(pData->m_cIP);
  847. csComputerName = CTextConvert::Utf8ToUnicode(pData->m_cComputerName);
  848. GlobalUnlock(pDittoDelayCF_HDROP->m_hgData);
  849. CString ipPort = csIP;
  850. if (pData->respondPort > 0)
  851. {
  852. ipPort.Format(_T("%s:%d"), csIP, pData->respondPort);
  853. }
  854. CString namePort = csComputerName;
  855. if (pData->respondPort > 0)
  856. {
  857. namePort.Format(_T("%s:%d"), csComputerName, pData->respondPort);
  858. }
  859. CClient cl;
  860. hData = cl.RequestCopiedFiles(*pCF_HDROP, ipPort, namePort);
  861. }
  862. }
  863. else
  864. {
  865. hData = m_ClipIDs.Render(lpFormatEtc->cfFormat);
  866. if (m_convertToHDROPOnDelayRender &&
  867. hData == NULL &&
  868. lpFormatEtc->cfFormat == CF_HDROP)
  869. {
  870. hData = ConvertToFileDrop();
  871. }
  872. }
  873. }
  874. //Add to a cache of already rendered data
  875. //Windows seems to call this function multiple times
  876. //so only the first time do we need to go get the data
  877. HGLOBAL hCopy = NULL;
  878. if(hData)
  879. {
  880. hCopy = NewGlobalH(hData, GlobalSize(hData));
  881. }
  882. CClipFormat format(lpFormatEtc->cfFormat, hCopy);
  883. m_DelayRenderedFormats.Add(format);
  884. format.m_autoDeleteData = false; //owned by m_DelayRenderedFormats
  885. }
  886. BOOL bRet = FALSE;
  887. if(hData)
  888. {
  889. // if phGlobal is null, we can just give the allocated mem
  890. // else, our data must fit within the GlobalSize(*phGlobal)
  891. if(*phGlobal == 0)
  892. {
  893. *phGlobal = hData;
  894. }
  895. else
  896. {
  897. SIZE_T len = min(::GlobalSize(*phGlobal), ::GlobalSize(hData));
  898. if(len)
  899. {
  900. CopyToGlobalHH(*phGlobal, hData, len);
  901. }
  902. ::GlobalFree(hData);
  903. }
  904. bRet = TRUE;
  905. }
  906. bInHere = false;
  907. return bRet;
  908. }
  909. HGLOBAL COleClipSource::ConvertToFileDrop()
  910. {
  911. CString path = CGetSetOptions::GetPath(PATH_DRAG_FILES);
  912. CreateDirectory(path, NULL);
  913. CFileRecieve fileList;
  914. int dragId = CGetSetOptions::GetDragId();
  915. int origDragId = dragId;
  916. for (int i = 0; i < m_ClipIDs.GetCount(); i++)
  917. {
  918. CClip fileClip;
  919. fileClip.LoadFormats(m_ClipIDs[i]);
  920. CClipFormat *unicodeText = fileClip.m_Formats.FindFormat(CF_UNICODETEXT);
  921. if (unicodeText)
  922. {
  923. CString name = _T("text");
  924. CString file;
  925. if (g_Opt.GetTempDragFileName() != _T(""))
  926. {
  927. name = g_Opt.GetTempDragFileName();
  928. CString id;
  929. id.Format(_T("%d"), dragId++);
  930. name.Replace(_T("#"), id);
  931. file.Format(_T("%s%s.txt"), path, name);
  932. }
  933. else
  934. {
  935. file.Format(_T("%s%s_%d.txt"), path, name, dragId++);
  936. }
  937. fileClip.WriteTextToFile(file, TRUE, FALSE, FALSE);
  938. fileList.AddFile(file);
  939. }
  940. else
  941. {
  942. CClipFormat *asciiText = fileClip.m_Formats.FindFormat(CF_TEXT);
  943. if (asciiText)
  944. {
  945. CString name = _T("text");
  946. CString file;
  947. if (g_Opt.GetTempDragFileName() != _T(""))
  948. {
  949. name = g_Opt.GetTempDragFileName();
  950. CString id;
  951. id.Format(_T("%d"), dragId++);
  952. name.Replace(_T("#"), id);
  953. file.Format(_T("%s%s.txt"), path, name);
  954. }
  955. else
  956. {
  957. file.Format(_T("%s%s_%d.txt"), path, name, dragId++);
  958. }
  959. fileClip.WriteTextToFile(file, FALSE, TRUE, FALSE);
  960. fileList.AddFile(file);
  961. }
  962. else
  963. {
  964. CClipFormat *png = NULL;
  965. CClipFormat *bitmap = fileClip.m_Formats.FindFormat(CF_DIB);
  966. if (bitmap == NULL)
  967. {
  968. png = fileClip.m_Formats.FindFormat(theApp.m_PNG_Format);
  969. }
  970. if (bitmap != NULL ||
  971. png != NULL)
  972. {
  973. CString name = _T("image");
  974. CString file;
  975. if (g_Opt.GetTempDragFileName() != _T(""))
  976. {
  977. name = g_Opt.GetTempDragFileName();
  978. CString id;
  979. id.Format(_T("%d"), dragId++);
  980. name.Replace(_T("#"), id);
  981. file.Format(_T("%s%s.png"), path, name);
  982. }
  983. else
  984. {
  985. file.Format(_T("%s%s_%d.png"), path, name, dragId++);
  986. }
  987. if (fileClip.WriteImageToFile(file))
  988. {
  989. fileList.AddFile(file);
  990. }
  991. }
  992. }
  993. }
  994. }
  995. if(dragId != origDragId)
  996. {
  997. CGetSetOptions::SetDragId(dragId);
  998. }
  999. HGLOBAL hData = fileList.CreateCF_HDROPBuffer();
  1000. return hData;
  1001. }
  1002. void COleClipSource::Slugify(CClip &clip)
  1003. {
  1004. IClipFormat *unicodeTextFormat = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
  1005. if (unicodeTextFormat != NULL)
  1006. {
  1007. CString cs(unicodeTextFormat->GetAsCString());
  1008. //free the old text we are going to replace it below with an upper case version
  1009. unicodeTextFormat->Free();
  1010. CString newString = slugify(cs.GetString()).c_str();
  1011. long len = newString.GetLength();
  1012. HGLOBAL hGlobal = NewGlobalP(newString.GetBuffer(), ((len + 1) * sizeof(wchar_t)));
  1013. unicodeTextFormat->Data(hGlobal);
  1014. }
  1015. }