OleClipSource.cpp 26 KB

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