1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_dtrans.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir //------------------------------------------------------------------------ 32*cdf0e10cSrcweir // includes 33*cdf0e10cSrcweir //------------------------------------------------------------------------ 34*cdf0e10cSrcweir #include "DataFmtTransl.hxx" 35*cdf0e10cSrcweir #include <rtl/string.hxx> 36*cdf0e10cSrcweir #include <osl/diagnose.h> 37*cdf0e10cSrcweir #include <rtl/tencinfo.h> 38*cdf0e10cSrcweir #include "..\misc\ImplHelper.hxx" 39*cdf0e10cSrcweir #include "..\misc\WinClip.hxx" 40*cdf0e10cSrcweir #include "MimeAttrib.hxx" 41*cdf0e10cSrcweir #include "DTransHelper.hxx" 42*cdf0e10cSrcweir #include <rtl/string.h> 43*cdf0e10cSrcweir #include "Fetc.hxx" 44*cdf0e10cSrcweir 45*cdf0e10cSrcweir #if defined _MSC_VER 46*cdf0e10cSrcweir #pragma warning(push,1) 47*cdf0e10cSrcweir #pragma warning(disable:4917) 48*cdf0e10cSrcweir #endif 49*cdf0e10cSrcweir #include <windows.h> 50*cdf0e10cSrcweir #if (_MSC_VER < 1300) && !defined(__MINGW32__) 51*cdf0e10cSrcweir #include <olestd.h> 52*cdf0e10cSrcweir #endif 53*cdf0e10cSrcweir #include <shlobj.h> 54*cdf0e10cSrcweir #if defined _MSC_VER 55*cdf0e10cSrcweir #pragma warning(pop) 56*cdf0e10cSrcweir #endif 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir //------------------------------------------------------------------------ 60*cdf0e10cSrcweir // namespace directives 61*cdf0e10cSrcweir //------------------------------------------------------------------------ 62*cdf0e10cSrcweir 63*cdf0e10cSrcweir using namespace rtl; 64*cdf0e10cSrcweir using namespace std; 65*cdf0e10cSrcweir using namespace com::sun::star::uno; 66*cdf0e10cSrcweir using namespace com::sun::star::datatransfer; 67*cdf0e10cSrcweir using namespace com::sun::star::lang; 68*cdf0e10cSrcweir 69*cdf0e10cSrcweir //------------------------------------------------------------------------ 70*cdf0e10cSrcweir // const 71*cdf0e10cSrcweir //------------------------------------------------------------------------ 72*cdf0e10cSrcweir 73*cdf0e10cSrcweir const Type CPPUTYPE_SALINT32 = getCppuType((sal_Int32*)0); 74*cdf0e10cSrcweir const Type CPPUTYPE_SALINT8 = getCppuType((sal_Int8*)0); 75*cdf0e10cSrcweir const Type CPPUTYPE_OUSTRING = getCppuType((OUString*)0); 76*cdf0e10cSrcweir const Type CPPUTYPE_SEQSALINT8 = getCppuType((Sequence< sal_Int8>*)0); 77*cdf0e10cSrcweir const sal_Int32 MAX_CLIPFORMAT_NAME = 256; 78*cdf0e10cSrcweir 79*cdf0e10cSrcweir const OUString TEXT_PLAIN_CHARSET = OUString::createFromAscii( "text/plain;charset=" ); 80*cdf0e10cSrcweir const OUString HPNAME_OEM_ANSI_TEXT = OUString::createFromAscii( "OEM/ANSI Text" ); 81*cdf0e10cSrcweir 82*cdf0e10cSrcweir const OUString HTML_FORMAT_NAME_WINDOWS = OUString::createFromAscii( "HTML Format" ); 83*cdf0e10cSrcweir const OUString HTML_FORMAT_NAME_SOFFICE = OUString::createFromAscii( "HTML (HyperText Markup Language)" ); 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir //------------------------------------------------------------------------ 86*cdf0e10cSrcweir // 87*cdf0e10cSrcweir //------------------------------------------------------------------------ 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir CDataFormatTranslator::CDataFormatTranslator( const Reference< XMultiServiceFactory >& aServiceManager ) : 90*cdf0e10cSrcweir m_SrvMgr( aServiceManager ) 91*cdf0e10cSrcweir { 92*cdf0e10cSrcweir m_XDataFormatTranslator = Reference< XDataFormatTranslator >( 93*cdf0e10cSrcweir m_SrvMgr->createInstance( OUString::createFromAscii( "com.sun.star.datatransfer.DataFormatTranslator" ) ), UNO_QUERY ); 94*cdf0e10cSrcweir } 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir //------------------------------------------------------------------------ 97*cdf0e10cSrcweir // 98*cdf0e10cSrcweir //------------------------------------------------------------------------ 99*cdf0e10cSrcweir 100*cdf0e10cSrcweir CFormatEtc CDataFormatTranslator::getFormatEtcFromDataFlavor( const DataFlavor& aDataFlavor ) const 101*cdf0e10cSrcweir { 102*cdf0e10cSrcweir sal_Int32 cf = CF_INVALID; 103*cdf0e10cSrcweir 104*cdf0e10cSrcweir try 105*cdf0e10cSrcweir { 106*cdf0e10cSrcweir if( m_XDataFormatTranslator.is( ) ) 107*cdf0e10cSrcweir { 108*cdf0e10cSrcweir Any aFormat = m_XDataFormatTranslator->getSystemDataTypeFromDataFlavor( aDataFlavor ); 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir if ( aFormat.hasValue( ) ) 111*cdf0e10cSrcweir { 112*cdf0e10cSrcweir if ( aFormat.getValueType( ) == CPPUTYPE_SALINT32 ) 113*cdf0e10cSrcweir { 114*cdf0e10cSrcweir aFormat >>= cf; 115*cdf0e10cSrcweir OSL_ENSURE( CF_INVALID != cf, "Invalid Clipboard format delivered" ); 116*cdf0e10cSrcweir } 117*cdf0e10cSrcweir else if ( aFormat.getValueType( ) == CPPUTYPE_OUSTRING ) 118*cdf0e10cSrcweir { 119*cdf0e10cSrcweir OUString aClipFmtName; 120*cdf0e10cSrcweir aFormat >>= aClipFmtName; 121*cdf0e10cSrcweir 122*cdf0e10cSrcweir OSL_ASSERT( aClipFmtName.getLength( ) ); 123*cdf0e10cSrcweir cf = RegisterClipboardFormatW( reinterpret_cast<LPCWSTR>(aClipFmtName.getStr( )) ); 124*cdf0e10cSrcweir 125*cdf0e10cSrcweir OSL_ENSURE( CF_INVALID != cf, "RegisterClipboardFormat failed" ); 126*cdf0e10cSrcweir } 127*cdf0e10cSrcweir else 128*cdf0e10cSrcweir OSL_ENSURE( sal_False, "Wrong Any-Type detected" ); 129*cdf0e10cSrcweir } 130*cdf0e10cSrcweir } 131*cdf0e10cSrcweir } 132*cdf0e10cSrcweir catch( ... ) 133*cdf0e10cSrcweir { 134*cdf0e10cSrcweir OSL_ENSURE( sal_False, "Unexpected error" ); 135*cdf0e10cSrcweir } 136*cdf0e10cSrcweir 137*cdf0e10cSrcweir return sal::static_int_cast<CFormatEtc>(getFormatEtcForClipformat( sal::static_int_cast<CLIPFORMAT>(cf) )); 138*cdf0e10cSrcweir } 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir //------------------------------------------------------------------------ 141*cdf0e10cSrcweir // 142*cdf0e10cSrcweir //------------------------------------------------------------------------ 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir DataFlavor CDataFormatTranslator::getDataFlavorFromFormatEtc( const FORMATETC& aFormatEtc, LCID lcid ) const 145*cdf0e10cSrcweir { 146*cdf0e10cSrcweir DataFlavor aFlavor; 147*cdf0e10cSrcweir 148*cdf0e10cSrcweir try 149*cdf0e10cSrcweir { 150*cdf0e10cSrcweir CLIPFORMAT aClipformat = aFormatEtc.cfFormat; 151*cdf0e10cSrcweir 152*cdf0e10cSrcweir Any aAny; 153*cdf0e10cSrcweir aAny <<= static_cast< sal_Int32 >( aClipformat ); 154*cdf0e10cSrcweir 155*cdf0e10cSrcweir if ( isOemOrAnsiTextFormat( aClipformat ) ) 156*cdf0e10cSrcweir { 157*cdf0e10cSrcweir aFlavor.MimeType = TEXT_PLAIN_CHARSET; 158*cdf0e10cSrcweir aFlavor.MimeType += getTextCharsetFromLCID( lcid, aClipformat ); 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir aFlavor.HumanPresentableName = HPNAME_OEM_ANSI_TEXT; 161*cdf0e10cSrcweir aFlavor.DataType = CPPUTYPE_SEQSALINT8; 162*cdf0e10cSrcweir } 163*cdf0e10cSrcweir else if ( CF_INVALID != aClipformat ) 164*cdf0e10cSrcweir { 165*cdf0e10cSrcweir if ( m_XDataFormatTranslator.is( ) ) 166*cdf0e10cSrcweir { 167*cdf0e10cSrcweir aFlavor = m_XDataFormatTranslator->getDataFlavorFromSystemDataType( aAny ); 168*cdf0e10cSrcweir 169*cdf0e10cSrcweir if ( !aFlavor.MimeType.getLength( ) ) 170*cdf0e10cSrcweir { 171*cdf0e10cSrcweir // lookup of DataFlavor from clipboard format id 172*cdf0e10cSrcweir // failed, so we try to resolve via clipboard 173*cdf0e10cSrcweir // format name 174*cdf0e10cSrcweir OUString clipFormatName = getClipboardFormatName( aClipformat ); 175*cdf0e10cSrcweir 176*cdf0e10cSrcweir // if we could not get a clipboard format name an 177*cdf0e10cSrcweir // error must have occured or it is a standard 178*cdf0e10cSrcweir // clipboard format that we don't translate, e.g. 179*cdf0e10cSrcweir // CF_BITMAP (the office only uses CF_DIB) 180*cdf0e10cSrcweir if ( clipFormatName.getLength( ) ) 181*cdf0e10cSrcweir { 182*cdf0e10cSrcweir aAny <<= clipFormatName; 183*cdf0e10cSrcweir aFlavor = m_XDataFormatTranslator->getDataFlavorFromSystemDataType( aAny ); 184*cdf0e10cSrcweir } 185*cdf0e10cSrcweir } 186*cdf0e10cSrcweir } 187*cdf0e10cSrcweir } 188*cdf0e10cSrcweir } 189*cdf0e10cSrcweir catch( ... ) 190*cdf0e10cSrcweir { 191*cdf0e10cSrcweir OSL_ENSURE( sal_False, "Unexpected error" ); 192*cdf0e10cSrcweir } 193*cdf0e10cSrcweir 194*cdf0e10cSrcweir return aFlavor; 195*cdf0e10cSrcweir } 196*cdf0e10cSrcweir 197*cdf0e10cSrcweir //------------------------------------------------------------------------ 198*cdf0e10cSrcweir // 199*cdf0e10cSrcweir //------------------------------------------------------------------------ 200*cdf0e10cSrcweir 201*cdf0e10cSrcweir CFormatEtc SAL_CALL CDataFormatTranslator::getFormatEtcForClipformatName( const OUString& aClipFmtName ) const 202*cdf0e10cSrcweir { 203*cdf0e10cSrcweir // check parameter 204*cdf0e10cSrcweir if ( !aClipFmtName.getLength( ) ) 205*cdf0e10cSrcweir return CFormatEtc( CF_INVALID ); 206*cdf0e10cSrcweir 207*cdf0e10cSrcweir CLIPFORMAT cf = sal::static_int_cast<CLIPFORMAT>(RegisterClipboardFormatW( reinterpret_cast<LPCWSTR>(aClipFmtName.getStr( )) )); 208*cdf0e10cSrcweir return getFormatEtcForClipformat( cf ); 209*cdf0e10cSrcweir } 210*cdf0e10cSrcweir 211*cdf0e10cSrcweir //------------------------------------------------------------------------ 212*cdf0e10cSrcweir // 213*cdf0e10cSrcweir //------------------------------------------------------------------------ 214*cdf0e10cSrcweir 215*cdf0e10cSrcweir OUString CDataFormatTranslator::getClipboardFormatName( CLIPFORMAT aClipformat ) const 216*cdf0e10cSrcweir { 217*cdf0e10cSrcweir OSL_PRECOND( CF_INVALID != aClipformat, "Invalid clipboard format" ); 218*cdf0e10cSrcweir 219*cdf0e10cSrcweir sal_Unicode wBuff[ MAX_CLIPFORMAT_NAME ]; 220*cdf0e10cSrcweir sal_Int32 nLen = GetClipboardFormatNameW( aClipformat, reinterpret_cast<LPWSTR>(wBuff), MAX_CLIPFORMAT_NAME ); 221*cdf0e10cSrcweir 222*cdf0e10cSrcweir return OUString( wBuff, nLen ); 223*cdf0e10cSrcweir } 224*cdf0e10cSrcweir 225*cdf0e10cSrcweir //------------------------------------------------------------------------ 226*cdf0e10cSrcweir // 227*cdf0e10cSrcweir //------------------------------------------------------------------------ 228*cdf0e10cSrcweir 229*cdf0e10cSrcweir CFormatEtc SAL_CALL CDataFormatTranslator::getFormatEtcForClipformat( CLIPFORMAT cf ) const 230*cdf0e10cSrcweir { 231*cdf0e10cSrcweir CFormatEtc fetc( cf, TYMED_NULL, NULL, DVASPECT_CONTENT ); 232*cdf0e10cSrcweir 233*cdf0e10cSrcweir switch( cf ) 234*cdf0e10cSrcweir { 235*cdf0e10cSrcweir case CF_METAFILEPICT: 236*cdf0e10cSrcweir fetc.setTymed( TYMED_MFPICT ); 237*cdf0e10cSrcweir break; 238*cdf0e10cSrcweir 239*cdf0e10cSrcweir case CF_ENHMETAFILE: 240*cdf0e10cSrcweir fetc.setTymed( TYMED_ENHMF ); 241*cdf0e10cSrcweir break; 242*cdf0e10cSrcweir 243*cdf0e10cSrcweir default: 244*cdf0e10cSrcweir fetc.setTymed( TYMED_HGLOBAL /*| TYMED_ISTREAM*/ ); 245*cdf0e10cSrcweir } 246*cdf0e10cSrcweir 247*cdf0e10cSrcweir /* 248*cdf0e10cSrcweir hack: in order to paste urls copied by Internet Explorer 249*cdf0e10cSrcweir with "copy link" we set the lindex member to 0 250*cdf0e10cSrcweir but if we really want to support CFSTR_FILECONTENT and 251*cdf0e10cSrcweir the accompany format CFSTR_FILEDESCRIPTOR (FileGroupDescriptor) 252*cdf0e10cSrcweir the client of the clipboard service has to provide a id 253*cdf0e10cSrcweir of which FileContents it wants to paste 254*cdf0e10cSrcweir see MSDN: "Handling Shell Data Transfer Scenarios" 255*cdf0e10cSrcweir */ 256*cdf0e10cSrcweir if ( cf == RegisterClipboardFormatA( CFSTR_FILECONTENTS ) ) 257*cdf0e10cSrcweir fetc.setLindex( 0 ); 258*cdf0e10cSrcweir 259*cdf0e10cSrcweir return fetc; 260*cdf0e10cSrcweir } 261*cdf0e10cSrcweir 262*cdf0e10cSrcweir //------------------------------------------------------------------------ 263*cdf0e10cSrcweir // 264*cdf0e10cSrcweir //------------------------------------------------------------------------ 265*cdf0e10cSrcweir 266*cdf0e10cSrcweir sal_Bool SAL_CALL CDataFormatTranslator::isOemOrAnsiTextFormat( CLIPFORMAT cf ) const 267*cdf0e10cSrcweir { 268*cdf0e10cSrcweir return ( (cf == CF_TEXT) || (cf == CF_OEMTEXT) ); 269*cdf0e10cSrcweir } 270*cdf0e10cSrcweir 271*cdf0e10cSrcweir //------------------------------------------------------------------------ 272*cdf0e10cSrcweir // 273*cdf0e10cSrcweir //------------------------------------------------------------------------ 274*cdf0e10cSrcweir 275*cdf0e10cSrcweir sal_Bool SAL_CALL CDataFormatTranslator::isUnicodeTextFormat( CLIPFORMAT cf ) const 276*cdf0e10cSrcweir { 277*cdf0e10cSrcweir return ( cf == CF_UNICODETEXT ); 278*cdf0e10cSrcweir } 279*cdf0e10cSrcweir 280*cdf0e10cSrcweir //------------------------------------------------------------------------ 281*cdf0e10cSrcweir // 282*cdf0e10cSrcweir //------------------------------------------------------------------------ 283*cdf0e10cSrcweir 284*cdf0e10cSrcweir sal_Bool SAL_CALL CDataFormatTranslator::isTextFormat( CLIPFORMAT cf ) const 285*cdf0e10cSrcweir { 286*cdf0e10cSrcweir return ( isOemOrAnsiTextFormat( cf ) || isUnicodeTextFormat( cf ) ); 287*cdf0e10cSrcweir } 288*cdf0e10cSrcweir 289*cdf0e10cSrcweir //------------------------------------------------------------------------ 290*cdf0e10cSrcweir // 291*cdf0e10cSrcweir //------------------------------------------------------------------------ 292*cdf0e10cSrcweir 293*cdf0e10cSrcweir sal_Bool SAL_CALL CDataFormatTranslator::isHTMLFormat( CLIPFORMAT cf ) const 294*cdf0e10cSrcweir { 295*cdf0e10cSrcweir OUString clipFormatName = getClipboardFormatName( cf ); 296*cdf0e10cSrcweir return ( clipFormatName == HTML_FORMAT_NAME_WINDOWS ); 297*cdf0e10cSrcweir } 298*cdf0e10cSrcweir 299*cdf0e10cSrcweir //------------------------------------------------------------------------ 300*cdf0e10cSrcweir // 301*cdf0e10cSrcweir //------------------------------------------------------------------------ 302*cdf0e10cSrcweir 303*cdf0e10cSrcweir sal_Bool SAL_CALL CDataFormatTranslator::isTextHtmlFormat( CLIPFORMAT cf ) const 304*cdf0e10cSrcweir { 305*cdf0e10cSrcweir OUString clipFormatName = getClipboardFormatName( cf ); 306*cdf0e10cSrcweir return ( clipFormatName.equalsIgnoreAsciiCase( HTML_FORMAT_NAME_SOFFICE ) ); 307*cdf0e10cSrcweir } 308*cdf0e10cSrcweir 309*cdf0e10cSrcweir //------------------------------------------------------------------------ 310*cdf0e10cSrcweir // 311*cdf0e10cSrcweir //------------------------------------------------------------------------ 312*cdf0e10cSrcweir 313*cdf0e10cSrcweir OUString SAL_CALL CDataFormatTranslator::getTextCharsetFromLCID( LCID lcid, CLIPFORMAT aClipformat ) const 314*cdf0e10cSrcweir { 315*cdf0e10cSrcweir OSL_ASSERT( isOemOrAnsiTextFormat( aClipformat ) ); 316*cdf0e10cSrcweir 317*cdf0e10cSrcweir OUString charset; 318*cdf0e10cSrcweir if ( CF_TEXT == aClipformat ) 319*cdf0e10cSrcweir { 320*cdf0e10cSrcweir charset = getMimeCharsetFromLocaleId( 321*cdf0e10cSrcweir lcid, 322*cdf0e10cSrcweir LOCALE_IDEFAULTANSICODEPAGE, 323*cdf0e10cSrcweir PRE_WINDOWS_CODEPAGE ); 324*cdf0e10cSrcweir } 325*cdf0e10cSrcweir else if ( CF_OEMTEXT == aClipformat ) 326*cdf0e10cSrcweir { 327*cdf0e10cSrcweir charset = getMimeCharsetFromLocaleId( 328*cdf0e10cSrcweir lcid, 329*cdf0e10cSrcweir LOCALE_IDEFAULTCODEPAGE, 330*cdf0e10cSrcweir PRE_OEM_CODEPAGE ); 331*cdf0e10cSrcweir } 332*cdf0e10cSrcweir else // CF_UNICODE 333*cdf0e10cSrcweir OSL_ASSERT( sal_False ); 334*cdf0e10cSrcweir 335*cdf0e10cSrcweir return charset; 336*cdf0e10cSrcweir } 337