OleClipSource.cpp 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  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. CString fileId = "";
  931. if (name.Replace(_T("#"), id) == 0 && m_ClipIDs.GetCount() > 0)
  932. {
  933. fileId = "_" + id;
  934. }
  935. file.Format(_T("%s%s%s.txt"), path, name, fileId);
  936. }
  937. else
  938. {
  939. file.Format(_T("%s%s_%d.txt"), path, name, dragId++);
  940. }
  941. fileClip.WriteTextToFile(file, TRUE, FALSE, FALSE);
  942. fileList.AddFile(file);
  943. }
  944. else
  945. {
  946. CClipFormat *asciiText = fileClip.m_Formats.FindFormat(CF_TEXT);
  947. if (asciiText)
  948. {
  949. CString name = _T("text");
  950. CString file;
  951. if (g_Opt.GetTempDragFileName() != _T(""))
  952. {
  953. name = g_Opt.GetTempDragFileName();
  954. CString id;
  955. id.Format(_T("%d"), dragId++);
  956. CString fileId = "";
  957. if (name.Replace(_T("#"), id) == 0 && m_ClipIDs.GetCount() > 0)
  958. {
  959. fileId = "_" + id;
  960. }
  961. file.Format(_T("%s%s%s.txt"), path, name, fileId);
  962. }
  963. else
  964. {
  965. file.Format(_T("%s%s_%d.txt"), path, name, dragId++);
  966. }
  967. fileClip.WriteTextToFile(file, FALSE, TRUE, FALSE);
  968. fileList.AddFile(file);
  969. }
  970. else
  971. {
  972. CClipFormat *png = NULL;
  973. CClipFormat *bitmap = fileClip.m_Formats.FindFormat(CF_DIB);
  974. if (bitmap == NULL)
  975. {
  976. png = fileClip.m_Formats.FindFormat(theApp.m_PNG_Format);
  977. }
  978. if (bitmap != NULL ||
  979. png != NULL)
  980. {
  981. CString name = _T("image");
  982. CString file;
  983. if (g_Opt.GetTempDragFileName() != _T(""))
  984. {
  985. name = g_Opt.GetTempDragFileName();
  986. CString id;
  987. id.Format(_T("%d"), dragId++);
  988. CString fileId = "";
  989. if (name.Replace(_T("#"), id) == 0 && m_ClipIDs.GetCount() > 0)
  990. {
  991. fileId = "_" + id;
  992. }
  993. file.Format(_T("%s%s%s.png"), path, name, fileId);
  994. }
  995. else
  996. {
  997. file.Format(_T("%s%s_%d.png"), path, name, dragId++);
  998. }
  999. if (fileClip.WriteImageToFile(file))
  1000. {
  1001. fileList.AddFile(file);
  1002. }
  1003. }
  1004. }
  1005. }
  1006. }
  1007. if(dragId != origDragId)
  1008. {
  1009. CGetSetOptions::SetDragId(dragId);
  1010. }
  1011. HGLOBAL hData = fileList.CreateCF_HDROPBuffer();
  1012. return hData;
  1013. }
  1014. void COleClipSource::Slugify(CClip &clip)
  1015. {
  1016. IClipFormat *unicodeTextFormat = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
  1017. if (unicodeTextFormat != NULL)
  1018. {
  1019. CString cs(unicodeTextFormat->GetAsCString());
  1020. //free the old text we are going to replace it below with an upper case version
  1021. unicodeTextFormat->Free();
  1022. CString newString = slugify(cs.GetString()).c_str();
  1023. long len = newString.GetLength();
  1024. HGLOBAL hGlobal = NewGlobalP(newString.GetBuffer(), ((len + 1) * sizeof(wchar_t)));
  1025. unicodeTextFormat->Data(hGlobal);
  1026. }
  1027. }