148123e16SAndrew Rist /**************************************************************
2*c58c9863Smseidel  *
348123e16SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
448123e16SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
548123e16SAndrew Rist  * distributed with this work for additional information
648123e16SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
748123e16SAndrew Rist  * to you under the Apache License, Version 2.0 (the
848123e16SAndrew Rist  * "License"); you may not use this file except in compliance
948123e16SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*c58c9863Smseidel  *
1148123e16SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*c58c9863Smseidel  *
1348123e16SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1448123e16SAndrew Rist  * software distributed under the License is distributed on an
1548123e16SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1648123e16SAndrew Rist  * KIND, either express or implied.  See the License for the
1748123e16SAndrew Rist  * specific language governing permissions and limitations
1848123e16SAndrew Rist  * under the License.
19*c58c9863Smseidel  *
2048123e16SAndrew Rist  *************************************************************/
2148123e16SAndrew Rist 
2248123e16SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_dtrans.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir //------------------------------------------------------------------------
28cdf0e10cSrcweir // includes
29cdf0e10cSrcweir //------------------------------------------------------------------------
30cdf0e10cSrcweir #include "DataFmtTransl.hxx"
31cdf0e10cSrcweir #include <rtl/string.hxx>
32cdf0e10cSrcweir #include <osl/diagnose.h>
33cdf0e10cSrcweir #include <rtl/tencinfo.h>
34cdf0e10cSrcweir #include "..\misc\ImplHelper.hxx"
35cdf0e10cSrcweir #include "..\misc\WinClip.hxx"
36cdf0e10cSrcweir #include "MimeAttrib.hxx"
37cdf0e10cSrcweir #include "DTransHelper.hxx"
38cdf0e10cSrcweir #include <rtl/string.h>
39cdf0e10cSrcweir #include "Fetc.hxx"
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #if defined _MSC_VER
42cdf0e10cSrcweir #pragma warning(push,1)
43cdf0e10cSrcweir #pragma warning(disable:4917)
44cdf0e10cSrcweir #endif
45cdf0e10cSrcweir #include <windows.h>
46cdf0e10cSrcweir #if (_MSC_VER < 1300) && !defined(__MINGW32__)
47cdf0e10cSrcweir #include <olestd.h>
48cdf0e10cSrcweir #endif
49cdf0e10cSrcweir #include <shlobj.h>
50cdf0e10cSrcweir #if defined _MSC_VER
51cdf0e10cSrcweir #pragma warning(pop)
52cdf0e10cSrcweir #endif
53*c58c9863Smseidel 
54cdf0e10cSrcweir 
55cdf0e10cSrcweir //------------------------------------------------------------------------
56cdf0e10cSrcweir // namespace directives
57cdf0e10cSrcweir //------------------------------------------------------------------------
58cdf0e10cSrcweir 
59cdf0e10cSrcweir using namespace rtl;
60cdf0e10cSrcweir using namespace std;
61cdf0e10cSrcweir using namespace com::sun::star::uno;
62cdf0e10cSrcweir using namespace com::sun::star::datatransfer;
63cdf0e10cSrcweir using namespace com::sun::star::lang;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir //------------------------------------------------------------------------
66cdf0e10cSrcweir // const
67cdf0e10cSrcweir //------------------------------------------------------------------------
68cdf0e10cSrcweir 
69cdf0e10cSrcweir const Type       CPPUTYPE_SALINT32   = getCppuType((sal_Int32*)0);
70cdf0e10cSrcweir const Type       CPPUTYPE_SALINT8    = getCppuType((sal_Int8*)0);
71cdf0e10cSrcweir const Type       CPPUTYPE_OUSTRING   = getCppuType((OUString*)0);
72cdf0e10cSrcweir const Type       CPPUTYPE_SEQSALINT8 = getCppuType((Sequence< sal_Int8>*)0);
73cdf0e10cSrcweir const sal_Int32  MAX_CLIPFORMAT_NAME = 256;
74cdf0e10cSrcweir 
75cdf0e10cSrcweir const OUString TEXT_PLAIN_CHARSET   = OUString::createFromAscii( "text/plain;charset=" );
76cdf0e10cSrcweir const OUString HPNAME_OEM_ANSI_TEXT = OUString::createFromAscii( "OEM/ANSI Text" );
77cdf0e10cSrcweir 
78cdf0e10cSrcweir const OUString HTML_FORMAT_NAME_WINDOWS = OUString::createFromAscii( "HTML Format" );
79cdf0e10cSrcweir const OUString HTML_FORMAT_NAME_SOFFICE = OUString::createFromAscii( "HTML (HyperText Markup Language)" );
80cdf0e10cSrcweir 
81cdf0e10cSrcweir //------------------------------------------------------------------------
82*c58c9863Smseidel //
83cdf0e10cSrcweir //------------------------------------------------------------------------
84cdf0e10cSrcweir 
CDataFormatTranslator(const Reference<XMultiServiceFactory> & aServiceManager)85cdf0e10cSrcweir CDataFormatTranslator::CDataFormatTranslator( const Reference< XMultiServiceFactory >& aServiceManager ) :
86cdf0e10cSrcweir 	m_SrvMgr( aServiceManager )
87cdf0e10cSrcweir {
88cdf0e10cSrcweir 	m_XDataFormatTranslator = Reference< XDataFormatTranslator >(
89*c58c9863Smseidel 		m_SrvMgr->createInstance( OUString::createFromAscii( "com.sun.star.datatransfer.DataFormatTranslator" ) ), UNO_QUERY );
90cdf0e10cSrcweir }
91cdf0e10cSrcweir 
92cdf0e10cSrcweir //------------------------------------------------------------------------
93*c58c9863Smseidel //
94cdf0e10cSrcweir //------------------------------------------------------------------------
95cdf0e10cSrcweir 
getFormatEtcFromDataFlavor(const DataFlavor & aDataFlavor) const96cdf0e10cSrcweir CFormatEtc CDataFormatTranslator::getFormatEtcFromDataFlavor( const DataFlavor& aDataFlavor ) const
97*c58c9863Smseidel {
98cdf0e10cSrcweir 	sal_Int32 cf = CF_INVALID;
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 	try
101cdf0e10cSrcweir 	{
102*c58c9863Smseidel 		if( m_XDataFormatTranslator.is( ) )
103*c58c9863Smseidel 		{
104*c58c9863Smseidel 			Any aFormat = m_XDataFormatTranslator->getSystemDataTypeFromDataFlavor( aDataFlavor );
105*c58c9863Smseidel 
106*c58c9863Smseidel 			if ( aFormat.hasValue( ) )
107*c58c9863Smseidel 			{
108*c58c9863Smseidel 				if ( aFormat.getValueType( ) == CPPUTYPE_SALINT32 )
109*c58c9863Smseidel 				{
110*c58c9863Smseidel 					aFormat >>= cf;
111*c58c9863Smseidel 					OSL_ENSURE( CF_INVALID != cf, "Invalid Clipboard format delivered" );
112*c58c9863Smseidel 				}
113*c58c9863Smseidel 				else if ( aFormat.getValueType( ) == CPPUTYPE_OUSTRING )
114*c58c9863Smseidel 				{
115*c58c9863Smseidel 					OUString aClipFmtName;
116*c58c9863Smseidel 					aFormat >>= aClipFmtName;
117*c58c9863Smseidel 
118*c58c9863Smseidel 					OSL_ASSERT( aClipFmtName.getLength( ) );
119*c58c9863Smseidel 					cf = RegisterClipboardFormatW( reinterpret_cast<LPCWSTR>(aClipFmtName.getStr( )) );
120*c58c9863Smseidel 
121*c58c9863Smseidel 					OSL_ENSURE( CF_INVALID != cf, "RegisterClipboardFormat failed" );
122*c58c9863Smseidel 				}
123*c58c9863Smseidel 				else
124*c58c9863Smseidel 					OSL_ENSURE( sal_False, "Wrong Any-Type detected" );
125*c58c9863Smseidel 			}
126*c58c9863Smseidel 		}
127*c58c9863Smseidel 	}
128cdf0e10cSrcweir 	catch( ... )
129cdf0e10cSrcweir 	{
130cdf0e10cSrcweir 		OSL_ENSURE( sal_False, "Unexpected error" );
131cdf0e10cSrcweir 	}
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 	return sal::static_int_cast<CFormatEtc>(getFormatEtcForClipformat( sal::static_int_cast<CLIPFORMAT>(cf) ));
134cdf0e10cSrcweir }
135cdf0e10cSrcweir 
136cdf0e10cSrcweir //------------------------------------------------------------------------
137*c58c9863Smseidel //
138cdf0e10cSrcweir //------------------------------------------------------------------------
139cdf0e10cSrcweir 
getDataFlavorFromFormatEtc(const FORMATETC & aFormatEtc,LCID lcid) const140cdf0e10cSrcweir DataFlavor CDataFormatTranslator::getDataFlavorFromFormatEtc( const FORMATETC& aFormatEtc, LCID lcid ) const
141cdf0e10cSrcweir {
142cdf0e10cSrcweir 	DataFlavor aFlavor;
143*c58c9863Smseidel 
144cdf0e10cSrcweir 	try
145cdf0e10cSrcweir 	{
146cdf0e10cSrcweir 		CLIPFORMAT aClipformat = aFormatEtc.cfFormat;
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 		Any aAny;
149cdf0e10cSrcweir 		aAny <<= static_cast< sal_Int32 >( aClipformat );
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 		if ( isOemOrAnsiTextFormat( aClipformat ) )
152*c58c9863Smseidel 		{
153cdf0e10cSrcweir 			aFlavor.MimeType             = TEXT_PLAIN_CHARSET;
154cdf0e10cSrcweir 			aFlavor.MimeType            += getTextCharsetFromLCID( lcid, aClipformat );
155cdf0e10cSrcweir 
156cdf0e10cSrcweir 			aFlavor.HumanPresentableName = HPNAME_OEM_ANSI_TEXT;
157*c58c9863Smseidel 			aFlavor.DataType             = CPPUTYPE_SEQSALINT8;
158cdf0e10cSrcweir 		}
159cdf0e10cSrcweir 		else if ( CF_INVALID != aClipformat )
160cdf0e10cSrcweir 		{
161*c58c9863Smseidel 			if ( m_XDataFormatTranslator.is( ) )
162*c58c9863Smseidel 			{
163*c58c9863Smseidel 				aFlavor = m_XDataFormatTranslator->getDataFlavorFromSystemDataType( aAny );
164*c58c9863Smseidel 
165*c58c9863Smseidel 				if ( !aFlavor.MimeType.getLength( ) )
166*c58c9863Smseidel 				{
167*c58c9863Smseidel 					// lookup of DataFlavor from clipboard format id
168*c58c9863Smseidel 					// failed, so we try to resolve via clipboard
169*c58c9863Smseidel 					// format name
170*c58c9863Smseidel 					OUString clipFormatName = getClipboardFormatName( aClipformat );
171*c58c9863Smseidel 
172*c58c9863Smseidel 					// if we could not get a clipboard format name an
173*c58c9863Smseidel 					// error must have occurred or it is a standard
174*c58c9863Smseidel 					// clipboard format that we don't translate, e.g.
175*c58c9863Smseidel 					// CF_BITMAP (the office only uses CF_DIB)
176*c58c9863Smseidel 					if ( clipFormatName.getLength( ) )
177*c58c9863Smseidel 					{
178*c58c9863Smseidel 						aAny <<= clipFormatName;
179*c58c9863Smseidel 						aFlavor = m_XDataFormatTranslator->getDataFlavorFromSystemDataType( aAny );
180*c58c9863Smseidel 					}
181*c58c9863Smseidel 				}
182*c58c9863Smseidel 			}
183cdf0e10cSrcweir 		}
184cdf0e10cSrcweir 	}
185cdf0e10cSrcweir 	catch( ... )
186cdf0e10cSrcweir 	{
187cdf0e10cSrcweir 		OSL_ENSURE( sal_False, "Unexpected error" );
188cdf0e10cSrcweir 	}
189cdf0e10cSrcweir 
190cdf0e10cSrcweir 	return aFlavor;
191cdf0e10cSrcweir }
192cdf0e10cSrcweir 
193cdf0e10cSrcweir //------------------------------------------------------------------------
194*c58c9863Smseidel //
195cdf0e10cSrcweir //------------------------------------------------------------------------
196cdf0e10cSrcweir 
getFormatEtcForClipformatName(const OUString & aClipFmtName) const197cdf0e10cSrcweir CFormatEtc SAL_CALL CDataFormatTranslator::getFormatEtcForClipformatName( const OUString& aClipFmtName ) const
198cdf0e10cSrcweir {
199cdf0e10cSrcweir 	// check parameter
200cdf0e10cSrcweir 	if ( !aClipFmtName.getLength( ) )
201cdf0e10cSrcweir 		return CFormatEtc( CF_INVALID );
202cdf0e10cSrcweir 
203cdf0e10cSrcweir 	CLIPFORMAT cf = sal::static_int_cast<CLIPFORMAT>(RegisterClipboardFormatW( reinterpret_cast<LPCWSTR>(aClipFmtName.getStr( )) ));
204cdf0e10cSrcweir 	return getFormatEtcForClipformat( cf );
205cdf0e10cSrcweir }
206cdf0e10cSrcweir 
207cdf0e10cSrcweir //------------------------------------------------------------------------
208*c58c9863Smseidel //
209cdf0e10cSrcweir //------------------------------------------------------------------------
210cdf0e10cSrcweir 
getClipboardFormatName(CLIPFORMAT aClipformat) const211cdf0e10cSrcweir OUString CDataFormatTranslator::getClipboardFormatName( CLIPFORMAT aClipformat ) const
212cdf0e10cSrcweir {
213cdf0e10cSrcweir 	OSL_PRECOND( CF_INVALID != aClipformat, "Invalid clipboard format" );
214cdf0e10cSrcweir 
215cdf0e10cSrcweir 	sal_Unicode wBuff[ MAX_CLIPFORMAT_NAME ];
216cdf0e10cSrcweir 	sal_Int32   nLen = GetClipboardFormatNameW( aClipformat, reinterpret_cast<LPWSTR>(wBuff), MAX_CLIPFORMAT_NAME );
217cdf0e10cSrcweir 
218cdf0e10cSrcweir 	return OUString( wBuff, nLen );
219cdf0e10cSrcweir }
220cdf0e10cSrcweir 
221cdf0e10cSrcweir //------------------------------------------------------------------------
222*c58c9863Smseidel //
223cdf0e10cSrcweir //------------------------------------------------------------------------
224cdf0e10cSrcweir 
getFormatEtcForClipformat(CLIPFORMAT cf) const225cdf0e10cSrcweir CFormatEtc SAL_CALL CDataFormatTranslator::getFormatEtcForClipformat( CLIPFORMAT cf ) const
226cdf0e10cSrcweir {
227cdf0e10cSrcweir 	CFormatEtc fetc( cf, TYMED_NULL, NULL, DVASPECT_CONTENT );
228cdf0e10cSrcweir 
229cdf0e10cSrcweir 	switch( cf )
230cdf0e10cSrcweir 	{
231cdf0e10cSrcweir 	case CF_METAFILEPICT:
232cdf0e10cSrcweir 		fetc.setTymed( TYMED_MFPICT );
233cdf0e10cSrcweir 		break;
234cdf0e10cSrcweir 
235cdf0e10cSrcweir 	case CF_ENHMETAFILE:
236cdf0e10cSrcweir 		fetc.setTymed( TYMED_ENHMF );
237cdf0e10cSrcweir 		break;
238cdf0e10cSrcweir 
239cdf0e10cSrcweir 	default:
240cdf0e10cSrcweir 		fetc.setTymed( TYMED_HGLOBAL /*| TYMED_ISTREAM*/ );
241cdf0e10cSrcweir 	}
242cdf0e10cSrcweir 
243*c58c9863Smseidel 	/*
244*c58c9863Smseidel 		hack: in order to paste urls copied by Internet Explorer
245*c58c9863Smseidel 		with "copy link" we set the lindex member to 0
246*c58c9863Smseidel 		but if we really want to support CFSTR_FILECONTENT and
247*c58c9863Smseidel 		the accompany format CFSTR_FILEDESCRIPTOR (FileGroupDescriptor)
248*c58c9863Smseidel 		the client of the clipboard service has to provide a id
249*c58c9863Smseidel 		of which FileContents it wants to paste
250*c58c9863Smseidel 		see MSDN: "Handling Shell Data Transfer Scenarios"
251*c58c9863Smseidel 	*/
252*c58c9863Smseidel 	if ( cf == RegisterClipboardFormatA( CFSTR_FILECONTENTS ) )
253*c58c9863Smseidel 		fetc.setLindex( 0 );
254cdf0e10cSrcweir 
255cdf0e10cSrcweir 	return fetc;
256cdf0e10cSrcweir }
257cdf0e10cSrcweir 
258cdf0e10cSrcweir //------------------------------------------------------------------------
259*c58c9863Smseidel //
260cdf0e10cSrcweir //------------------------------------------------------------------------
261cdf0e10cSrcweir 
isOemOrAnsiTextFormat(CLIPFORMAT cf) const262cdf0e10cSrcweir sal_Bool SAL_CALL CDataFormatTranslator::isOemOrAnsiTextFormat( CLIPFORMAT cf ) const
263cdf0e10cSrcweir {
264cdf0e10cSrcweir 	return ( (cf == CF_TEXT) || (cf == CF_OEMTEXT) );
265cdf0e10cSrcweir }
266cdf0e10cSrcweir 
267cdf0e10cSrcweir //------------------------------------------------------------------------
268*c58c9863Smseidel //
269cdf0e10cSrcweir //------------------------------------------------------------------------
270cdf0e10cSrcweir 
isUnicodeTextFormat(CLIPFORMAT cf) const271cdf0e10cSrcweir sal_Bool SAL_CALL CDataFormatTranslator::isUnicodeTextFormat( CLIPFORMAT cf ) const
272cdf0e10cSrcweir {
273cdf0e10cSrcweir 	return ( cf == CF_UNICODETEXT );
274cdf0e10cSrcweir }
275cdf0e10cSrcweir 
276cdf0e10cSrcweir //------------------------------------------------------------------------
277*c58c9863Smseidel //
278cdf0e10cSrcweir //------------------------------------------------------------------------
279cdf0e10cSrcweir 
isTextFormat(CLIPFORMAT cf) const280*c58c9863Smseidel sal_Bool SAL_CALL CDataFormatTranslator::isTextFormat( CLIPFORMAT cf ) const
281cdf0e10cSrcweir {
282cdf0e10cSrcweir 	return ( isOemOrAnsiTextFormat( cf ) || isUnicodeTextFormat( cf ) );
283cdf0e10cSrcweir }
284cdf0e10cSrcweir 
285cdf0e10cSrcweir //------------------------------------------------------------------------
286*c58c9863Smseidel //
287cdf0e10cSrcweir //------------------------------------------------------------------------
288cdf0e10cSrcweir 
isHTMLFormat(CLIPFORMAT cf) const289cdf0e10cSrcweir sal_Bool SAL_CALL CDataFormatTranslator::isHTMLFormat( CLIPFORMAT cf ) const
290cdf0e10cSrcweir {
291cdf0e10cSrcweir 	OUString clipFormatName = getClipboardFormatName( cf );
292cdf0e10cSrcweir 	return ( clipFormatName == HTML_FORMAT_NAME_WINDOWS );
293cdf0e10cSrcweir }
294cdf0e10cSrcweir 
295cdf0e10cSrcweir //------------------------------------------------------------------------
296*c58c9863Smseidel //
297cdf0e10cSrcweir //------------------------------------------------------------------------
298cdf0e10cSrcweir 
isTextHtmlFormat(CLIPFORMAT cf) const299cdf0e10cSrcweir sal_Bool SAL_CALL CDataFormatTranslator::isTextHtmlFormat( CLIPFORMAT cf ) const
300cdf0e10cSrcweir {
301*c58c9863Smseidel 	OUString clipFormatName = getClipboardFormatName( cf );
302cdf0e10cSrcweir 	return ( clipFormatName.equalsIgnoreAsciiCase( HTML_FORMAT_NAME_SOFFICE ) );
303cdf0e10cSrcweir }
304cdf0e10cSrcweir 
305cdf0e10cSrcweir //------------------------------------------------------------------------
306*c58c9863Smseidel //
307cdf0e10cSrcweir //------------------------------------------------------------------------
308cdf0e10cSrcweir 
getTextCharsetFromLCID(LCID lcid,CLIPFORMAT aClipformat) const309cdf0e10cSrcweir OUString SAL_CALL CDataFormatTranslator::getTextCharsetFromLCID( LCID lcid, CLIPFORMAT aClipformat ) const
310cdf0e10cSrcweir {
311cdf0e10cSrcweir 	OSL_ASSERT( isOemOrAnsiTextFormat( aClipformat ) );
312cdf0e10cSrcweir 
313cdf0e10cSrcweir 	OUString charset;
314cdf0e10cSrcweir 	if ( CF_TEXT == aClipformat )
315*c58c9863Smseidel 	{
316*c58c9863Smseidel 		charset = getMimeCharsetFromLocaleId(
317*c58c9863Smseidel 					lcid,
318*c58c9863Smseidel 					LOCALE_IDEFAULTANSICODEPAGE,
319cdf0e10cSrcweir 					PRE_WINDOWS_CODEPAGE );
320cdf0e10cSrcweir 	}
321cdf0e10cSrcweir 	else if ( CF_OEMTEXT == aClipformat )
322*c58c9863Smseidel 	{
323*c58c9863Smseidel 		charset = getMimeCharsetFromLocaleId(
324*c58c9863Smseidel 					lcid,
325*c58c9863Smseidel 					LOCALE_IDEFAULTCODEPAGE,
326*c58c9863Smseidel 					PRE_OEM_CODEPAGE );
327cdf0e10cSrcweir 	}
328cdf0e10cSrcweir 	else // CF_UNICODE
329cdf0e10cSrcweir 		OSL_ASSERT( sal_False );
330*c58c9863Smseidel 
331cdf0e10cSrcweir 	return charset;
332cdf0e10cSrcweir }
333