| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588 |
- #include "stdafx.h"
- #include "CP_Main.h"
- #include "OleClipSource.h"
- #include "shared/TextConvert.h"
- #include "CF_HDropAggregator.h"
- #include "CF_UnicodeTextAggregator.h"
- #include "CF_TextAggregator.h"
- #include "richtextaggregator.h"
- #include "htmlformataggregator.h"
- /*------------------------------------------------------------------*\
- COleClipSource
- \*------------------------------------------------------------------*/
- //IMPLEMENT_DYNAMIC(COleClipSource, COleDataSource)
- COleClipSource::COleClipSource()
- {
- m_bLoadedFormats = false;
- }
- COleClipSource::~COleClipSource()
- {
-
- }
- BOOL COleClipSource::DoDelayRender()
- {
- CClipTypes types;
- m_ClipIDs.GetTypes(types);
-
- INT_PTR count = types.GetSize();
- for(int i=0; i < count; i++)
- {
- DelayRenderData(types[i]);
- }
- return count > 0;
- }
- #include "Client.h"
- BOOL COleClipSource::DoImmediateRender()
- {
- if(m_bLoadedFormats)
- return TRUE;
- m_bLoadedFormats = true;
- if(m_pasteOptions.m_pPasteFormats != NULL)
- {
- return PutFormatOnClipboard(m_pasteOptions.m_pPasteFormats) > 0;
- }
-
- INT_PTR count = m_ClipIDs.GetSize();
- if(count <= 0)
- return 0;
-
- CClip clip;
- if(count > 1)
- {
- CStringA SepA = CTextConvert::ConvertToChar(g_Opt.GetMultiPasteSeparator());
- CCF_TextAggregator CFText(SepA);
- if(m_ClipIDs.AggregateData(CFText, CF_TEXT, g_Opt.m_bMultiPasteReverse))
- {
- CClipFormat cf(CF_TEXT, CFText.GetHGlobal());
- clip.m_Formats.Add(cf);
- //clip.m_Formats now owns the global data
- cf.m_autoDeleteData = false;
- }
- CStringW SepW = CTextConvert::ConvertToUnicode(g_Opt.GetMultiPasteSeparator());
- CCF_UnicodeTextAggregator CFUnicodeText(SepW);
- if(m_ClipIDs.AggregateData(CFUnicodeText, CF_UNICODETEXT, g_Opt.m_bMultiPasteReverse))
- {
- CClipFormat cf(CF_UNICODETEXT, CFUnicodeText.GetHGlobal());
- clip.m_Formats.Add(cf);
- //clip.m_Formats now owns the global data
- cf.m_autoDeleteData = false;
- }
- if ((m_pasteOptions.LimitFormatsToText()) &&
- clip.m_Formats.GetCount() == 0)
- {
- CCF_HDropAggregator HDrop;
- if (m_ClipIDs.AggregateData(HDrop, CF_HDROP, g_Opt.m_bMultiPasteReverse))
- {
- CClipFormat cf(CF_UNICODETEXT, HDrop.GetHGlobalAsString());
- clip.m_Formats.Add(cf);
- //clip.m_Formats now owns the global data
- cf.m_autoDeleteData = false;
- }
- }
- else if (m_pasteOptions.LimitFormatsToText() == false)
- {
- CCF_HDropAggregator HDrop;
- if(m_ClipIDs.AggregateData(HDrop, CF_HDROP, g_Opt.m_bMultiPasteReverse))
- {
- CClipFormat cf(CF_HDROP, HDrop.GetHGlobal());
- clip.m_Formats.Add(cf);
- //clip.m_Formats now owns the global data
- cf.m_autoDeleteData = false;
- }
- CRichTextAggregator RichText(SepA);
- if(m_ClipIDs.AggregateData(RichText, theApp.m_RTFFormat, g_Opt.m_bMultiPasteReverse))
- {
- CClipFormat cf(theApp.m_RTFFormat, RichText.GetHGlobal());
- clip.m_Formats.Add(cf);
- //clip.m_Formats now owns the global data
- cf.m_autoDeleteData = false;
- }
- CHTMLFormatAggregator Html(SepA);
- if(m_ClipIDs.AggregateData(Html, theApp.m_HTML_Format, g_Opt.m_bMultiPasteReverse))
- {
- CClipFormat cf(theApp.m_HTML_Format, Html.GetHGlobal());
- clip.m_Formats.Add(cf);
- //clip.m_Formats now owns the global data
- cf.m_autoDeleteData = false;
- }
- }
- }
- if (count >= 1 && clip.m_Formats.GetCount() == 0)
- {
- clip.LoadFormats(m_ClipIDs[0], m_pasteOptions.LimitFormatsToText());
- }
- if (m_pasteOptions.LimitFormatsToText())
- {
- PlainTextFilter(clip);
- }
- if(m_pasteOptions.m_pasteUpperCase ||
- m_pasteOptions.m_pasteLowerCase)
- {
- DoUpperLowerCase(clip, m_pasteOptions.m_pasteUpperCase);
- }
- else if(m_pasteOptions.m_pasteCapitalize)
- {
- Capitalize(clip);
- }
- else if(m_pasteOptions.m_pasteSentenceCase)
- {
- SentenceCase(clip);
- }
- return PutFormatOnClipboard(&clip.m_Formats) > 0;
- }
- void COleClipSource::DoUpperLowerCase(CClip &clip, bool upper)
- {
- IClipFormat *unicodeTextFormat = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
- if (unicodeTextFormat != NULL)
- {
- HGLOBAL data = unicodeTextFormat->Data();
- wchar_t * stringData = (wchar_t *) GlobalLock(data);
- int size = (int) GlobalSize(data);
- CString cs(stringData, (size / sizeof(wchar_t)));
- GlobalUnlock(data);
- //free the old text we are going to replace it below with an upper case version
- unicodeTextFormat->Free();
- CString val;
- if (upper)
- {
- val = cs.MakeUpper();
- }
- else
- {
- val = cs.MakeLower();
- }
-
- long lLen = val.GetLength();
- HGLOBAL hGlobal = NewGlobalP(val.GetBuffer(), ((lLen+1) * sizeof(wchar_t)));
- val.ReleaseBuffer();
- unicodeTextFormat->Data(hGlobal);
- }
- IClipFormat *asciiTextFormat = clip.m_Formats.FindFormatEx(CF_TEXT);
- if (asciiTextFormat != NULL)
- {
- HGLOBAL data = asciiTextFormat->Data();
- char * stringData = (char *) GlobalLock(data);
- int size = (int) GlobalSize(data);
- CStringA cs(stringData, size);
- GlobalUnlock(data);
- //free the old text we are going to replace it below with an upper case version
- asciiTextFormat->Free();
-
- CString val;
- if (upper)
- {
- val = cs.MakeUpper();
- }
- else
- {
- val = cs.MakeLower();
- }
- long lLen = val.GetLength();
- HGLOBAL hGlobal = NewGlobalP(val.GetBuffer(lLen), lLen + sizeof(char));
- val.ReleaseBuffer();
- asciiTextFormat->Data(hGlobal);
- }
- }
- void COleClipSource::Capitalize(CClip &clip)
- {
- IClipFormat *unicodeTextFormat = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
- if (unicodeTextFormat != NULL)
- {
- HGLOBAL data = unicodeTextFormat->Data();
- wchar_t * stringData = (wchar_t *) GlobalLock(data);
- int size = (int) GlobalSize(data);
- CString cs(stringData, (size / sizeof(wchar_t)));
- GlobalUnlock(data);
- //free the old text we are going to replace it below with an upper case version
- unicodeTextFormat->Free();
- CString val = cs.MakeLower();
- long len = val.GetLength();
- if (len > 0)
- {
- wchar_t * pText = val.GetBuffer();
-
- pText[0] = toupper(pText[0]);
- bool capitalize = false;
-
- for (int i = 1; i < len; i++)
- {
- wchar_t item = pText[i];
- if (item == ' ')
- {
- capitalize = true;
- }
- else if (capitalize)
- {
- pText[i] = toupper(item);
- capitalize = false;
- }
- }
- }
-
- val.ReleaseBuffer();
- HGLOBAL hGlobal = NewGlobalP(val.GetBuffer(), ((len + 1) * sizeof(wchar_t)));
- unicodeTextFormat->Data(hGlobal);
- }
- //test
- IClipFormat *asciiTextFormat = clip.m_Formats.FindFormatEx(CF_TEXT);
- if (asciiTextFormat != NULL)
- {
- HGLOBAL data = asciiTextFormat->Data();
- char * stringData = (char *) GlobalLock(data);
- int size = (int) GlobalSize(data);
- CStringA cs(stringData, size);
- GlobalUnlock(data);
- //free the old text we are going to replace it below with an upper case version
- asciiTextFormat->Free();
- CStringA val = cs.MakeLower();
- long len = val.GetLength();
- if (len > 0)
- {
- char * pText = val.GetBuffer();
- pText[0] = toupper(pText[0]);
- bool capitalize = false;
- for (int i = 1; i < len; i++)
- {
- wchar_t item = pText[i];
- if (item == ' ')
- {
- capitalize = true;
- }
- else if (capitalize)
- {
- pText[i] = toupper(item);
- capitalize = false;
- }
- }
- }
- val.ReleaseBuffer();
- HGLOBAL hGlobal = NewGlobalP(val.GetBuffer(), (len + 1));
- asciiTextFormat->Data(hGlobal);
- }
- }
- void COleClipSource::SentenceCase(CClip &clip)
- {
- IClipFormat *unicodeTextFormat = clip.m_Formats.FindFormatEx(CF_UNICODETEXT);
- if (unicodeTextFormat != NULL)
- {
- HGLOBAL data = unicodeTextFormat->Data();
- wchar_t * stringData = (wchar_t *) GlobalLock(data);
- int size = (int) GlobalSize(data);
- CString cs(stringData, (size / sizeof(wchar_t)));
- GlobalUnlock(data);
- //free the old text we are going to replace it below with an upper case version
- unicodeTextFormat->Free();
- CString val = cs.MakeLower();
- long len = val.GetLength();
- if (len > 0)
- {
- wchar_t * pText = val.GetBuffer();
- pText[0] = toupper(pText[0]);
- bool capitalize = false;
- for (int i = 1; i < len; i++)
- {
- wchar_t item = pText[i];
- if (item == '.' ||
- item == '!' ||
- item == '?')
- {
- capitalize = true;
- }
- else if (capitalize && item != ' ')
- {
- pText[i] = toupper(item);
- capitalize = false;
- }
- }
- }
- val.ReleaseBuffer();
- HGLOBAL hGlobal = NewGlobalP(val.GetBuffer(), ((len + 1) * sizeof(wchar_t)));
- unicodeTextFormat->Data(hGlobal);
- }
- IClipFormat *asciiTextFormat = clip.m_Formats.FindFormatEx(CF_TEXT);
- if (asciiTextFormat != NULL)
- {
- HGLOBAL data = asciiTextFormat->Data();
- char * stringData = (char *) GlobalLock(data);
- int size = (int) GlobalSize(data);
- CStringA cs(stringData, size);
- GlobalUnlock(data);
- //free the old text we are going to replace it below with an upper case version
- asciiTextFormat->Free();
- CStringA val = cs.MakeLower();
- long len = val.GetLength();
- if (len > 0)
- {
- char * pText = val.GetBuffer();
- pText[0] = toupper(pText[0]);
- bool capitalize = false;
- for (int i = 1; i < len; i++)
- {
- wchar_t item = pText[i];
- if (item == '.' ||
- item == '!' ||
- item == '?')
- {
- capitalize = true;
- }
- else if (capitalize && item != ' ')
- {
- pText[i] = toupper(item);
- capitalize = false;
- }
- }
- }
- val.ReleaseBuffer();
- HGLOBAL hGlobal = NewGlobalP(val.GetBuffer(), (len + 1));
- asciiTextFormat->Data(hGlobal);
- }
- }
- void COleClipSource::PlainTextFilter(CClip &clip)
- {
- bool foundText = false;
- INT_PTR hDropIndex = -1;
- INT_PTR count = clip.m_Formats.GetCount();
- for (INT_PTR i = 0; i < count; i++)
- {
- CClipFormat *pCF = &clip.m_Formats.ElementAt(i);
- if (pCF->m_cfType == CF_TEXT ||
- pCF->m_cfType == CF_UNICODETEXT)
- {
- foundText = true;
- }
- else if (pCF->m_cfType == CF_HDROP)
- {
- hDropIndex = i;
- }
- }
- if (foundText &&
- hDropIndex > -1)
- {
- clip.m_Formats.RemoveAt(hDropIndex);
- }
- else if (foundText == false &&
- hDropIndex > -1)
- {
- CCF_HDropAggregator HDrop;
- if (m_ClipIDs.AggregateData(HDrop, CF_HDROP, g_Opt.m_bMultiPasteReverse))
- {
- clip.m_Formats.Add(CClipFormat(CF_UNICODETEXT, HDrop.GetHGlobalAsString()));
- }
- }
- }
- INT_PTR COleClipSource::PutFormatOnClipboard(CClipFormats *pFormats)
- {
- Log(_T("Start of put format on clipboard"));
- CClipFormat* pCF;
- INT_PTR count = pFormats->GetSize();
- bool bDelayedRenderCF_HDROP = false;
- INT_PTR i = 0;
- //see if the html format is in the list
- //if it is the list we will not paste CF_TEXT
- for(i = 0; i < count; i++)
- {
- pCF = &pFormats->ElementAt(i);
- if(pCF->m_cfType == theApp.m_RemoteCF_HDROP)
- {
- bDelayedRenderCF_HDROP = true;
- }
- }
- for(i = 0; i < count; i++)
- {
- pCF = &pFormats->ElementAt(i);
- if(bDelayedRenderCF_HDROP)
- {
- if(pCF->m_cfType == CF_HDROP)
- {
- LogSendRecieveInfo("Added delayed cf_hdrop to clipboard");
- DelayRenderData(pCF->m_cfType);
- }
- continue;
- }
- wchar_t * stringData = (wchar_t *) GlobalLock(pCF->m_hgData);
- int size = (int) GlobalSize(pCF->m_hgData);
- CString cs(stringData, (size / sizeof(wchar_t)));
- GlobalUnlock(pCF->m_hgData);
- Log(StrF(_T("Setting clipboard type: %s to the clipboard"), GetFormatName(pCF->m_cfType)));
- CacheGlobalData(pCF->m_cfType, pCF->m_hgData);
- pCF->m_hgData = 0; // OLE owns it now
- }
- pFormats->RemoveAll();
- m_bLoadedFormats = true;
- Log(_T("End of put format on clipboard"));
- return count;
- }
- BOOL COleClipSource::OnRenderGlobalData(LPFORMATETC lpFormatEtc, HGLOBAL* phGlobal)
- {
- static bool bInHere = false;
- if(bInHere)
- {
- return FALSE;
- }
- bInHere = true;
- HGLOBAL hData = NULL;
- CClipFormat *pFind = m_DelayRenderedFormats.FindFormat(lpFormatEtc->cfFormat);
- if(pFind)
- {
- if(pFind->m_hgData)
- {
- hData = NewGlobalH(pFind->m_hgData, GlobalSize(pFind->m_hgData));
- }
- }
- else
- {
- LogSendRecieveInfo("Delayed Render, getting data from remote machine");
- CClip clip;
- if(m_ClipIDs.GetCount() > 0)
- {
- clip.LoadFormats(m_ClipIDs[0]);
- CClipFormat *pDittoDelayCF_HDROP = clip.m_Formats.FindFormat(theApp.m_RemoteCF_HDROP);
- CClipFormat *pCF_HDROP = clip.m_Formats.FindFormat(CF_HDROP);
- if(pDittoDelayCF_HDROP && pCF_HDROP)
- {
- CDittoCF_HDROP *pData = (CDittoCF_HDROP*)GlobalLock(pDittoDelayCF_HDROP->m_hgData);
- if(pData)
- {
- CString csComputerName;
- CString csIP;
- CTextConvert::ConvertFromUTF8(pData->m_cIP, csIP);
- CTextConvert::ConvertFromUTF8(pData->m_cComputerName, csComputerName);
-
- GlobalUnlock(pDittoDelayCF_HDROP->m_hgData);
- CClient cl;
- hData = cl.RequestCopiedFiles(*pCF_HDROP, csIP, csComputerName);
- }
- }
- else
- {
- hData = m_ClipIDs.Render(lpFormatEtc->cfFormat);
- }
- }
- //Add to a cache of already rendered data
- //Windows seems to call this function multiple times
- //so only the first time do we need to go get the data
- HGLOBAL hCopy = NULL;
- if(hData)
- {
- hCopy = NewGlobalH(hData, GlobalSize(hData));
- }
- CClipFormat format(lpFormatEtc->cfFormat, hCopy);
- format.m_autoDeleteData = false; //owned by m_DelayRenderedFormats
- m_DelayRenderedFormats.Add(format);
- }
- BOOL bRet = FALSE;
- if(hData)
- {
- // if phGlobal is null, we can just give the allocated mem
- // else, our data must fit within the GlobalSize(*phGlobal)
- if(*phGlobal == 0)
- {
- *phGlobal = hData;
- }
- else
- {
- SIZE_T len = min(::GlobalSize(*phGlobal), ::GlobalSize(hData));
- if(len)
- {
- CopyToGlobalHH(*phGlobal, hData, len);
- }
- ::GlobalFree(hData);
- }
- bRet = TRUE;
- }
- bInHere = false;
- return bRet;
- }
|