LibreOffice Module dtrans (master)  1
DataFmtTransl.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include "DataFmtTransl.hxx"
21 #include <rtl/string.hxx>
22 #include <osl/diagnose.h>
23 #include <rtl/tencinfo.h>
24 #include "../misc/ImplHelper.hxx"
25 #include <WinClip.hxx>
26 #include "MimeAttrib.hxx"
27 #include "DTransHelper.hxx"
28 #include <rtl/string.h>
30 #include "Fetc.hxx"
31 #include <com/sun/star/datatransfer/DataFormatTranslator.hpp>
32 
33 #if !defined WIN32_LEAN_AND_MEAN
34 # define WIN32_LEAN_AND_MEAN
35 #endif
36 #include <windows.h>
37 #include <shlobj.h>
38 
39 using namespace std;
40 using namespace com::sun::star::uno;
41 using namespace com::sun::star::datatransfer;
42 using namespace com::sun::star::lang;
43 
48 const sal_Int32 MAX_CLIPFORMAT_NAME = 256;
49 
51 {
52  m_XDataFormatTranslator = DataFormatTranslator::create( rxContext );
53 }
54 
55 CFormatEtc CDataFormatTranslator::getFormatEtcFromDataFlavor( const DataFlavor& aDataFlavor ) const
56 {
57  sal_Int32 cf = CF_INVALID;
58 
59  try
60  {
61  if( m_XDataFormatTranslator.is( ) )
62  {
63  Any aFormat = m_XDataFormatTranslator->getSystemDataTypeFromDataFlavor( aDataFlavor );
64 
65  if ( aFormat.hasValue( ) )
66  {
67  if ( aFormat.getValueType( ) == CPPUTYPE_SALINT32 )
68  {
69  aFormat >>= cf;
70  OSL_ENSURE( CF_INVALID != cf, "Invalid Clipboard format delivered" );
71  }
72  else if ( aFormat.getValueType( ) == CPPUTYPE_OUSTRING )
73  {
74  OUString aClipFmtName;
75  aFormat >>= aClipFmtName;
76 
77  OSL_ASSERT( aClipFmtName.getLength( ) );
78  cf = RegisterClipboardFormatW( o3tl::toW(aClipFmtName.getStr( )) );
79 
80  OSL_ENSURE( CF_INVALID != cf, "RegisterClipboardFormat failed" );
81  }
82  else
83  OSL_FAIL( "Wrong Any-Type detected" );
84  }
85  }
86  }
87  catch( ... )
88  {
89  OSL_FAIL( "Unexpected error" );
90  }
91 
92  return sal::static_int_cast<CFormatEtc>(getFormatEtcForClipformat( sal::static_int_cast<CLIPFORMAT>(cf) ));
93 }
94 
95 DataFlavor CDataFormatTranslator::getDataFlavorFromFormatEtc( const FORMATETC& aFormatEtc, LCID lcid ) const
96 {
97  DataFlavor aFlavor;
98 
99  try
100  {
101  CLIPFORMAT aClipformat = aFormatEtc.cfFormat;
102 
103  Any aAny;
104  aAny <<= static_cast< sal_Int32 >( aClipformat );
105 
106  if ( isOemOrAnsiTextFormat( aClipformat ) )
107  {
108  aFlavor.MimeType = "text/plain;charset=";
109  aFlavor.MimeType += getTextCharsetFromLCID( lcid, aClipformat );
110 
111  aFlavor.HumanPresentableName = "OEM/ANSI Text";
112  aFlavor.DataType = CPPUTYPE_SEQSALINT8;
113  }
114  else if ( CF_INVALID != aClipformat )
115  {
116  if ( m_XDataFormatTranslator.is( ) )
117  {
118  aFlavor = m_XDataFormatTranslator->getDataFlavorFromSystemDataType( aAny );
119 
120  if ( !aFlavor.MimeType.getLength( ) )
121  {
122  // lookup of DataFlavor from clipboard format id
123  // failed, so we try to resolve via clipboard
124  // format name
125  OUString clipFormatName = getClipboardFormatName( aClipformat );
126 
127  // if we could not get a clipboard format name an
128  // error must have occurred or it is a standard
129  // clipboard format that we don't translate, e.g.
130  // CF_BITMAP (the office only uses CF_DIB)
131  if ( clipFormatName.getLength( ) )
132  {
133  aAny <<= clipFormatName;
134  aFlavor = m_XDataFormatTranslator->getDataFlavorFromSystemDataType( aAny );
135  }
136  }
137  }
138  }
139  }
140  catch( ... )
141  {
142  OSL_FAIL( "Unexpected error" );
143  }
144 
145  return aFlavor;
146 }
147 
149 {
150  // check parameter
151  if ( !aClipFmtName.getLength( ) )
152  return CFormatEtc( CF_INVALID );
153 
154  CLIPFORMAT cf = sal::static_int_cast<CLIPFORMAT>(RegisterClipboardFormatW( o3tl::toW(aClipFmtName.getStr( )) ));
155  return getFormatEtcForClipformat( cf );
156 }
157 
158 OUString CDataFormatTranslator::getClipboardFormatName( CLIPFORMAT aClipformat )
159 {
160  OSL_PRECOND( CF_INVALID != aClipformat, "Invalid clipboard format" );
161 
162  sal_Unicode wBuff[ MAX_CLIPFORMAT_NAME + 1 ]; // Null terminator isn't counted, apparently.
163  sal_Int32 nLen = GetClipboardFormatNameW( aClipformat, o3tl::toW(wBuff), MAX_CLIPFORMAT_NAME );
164 
165  return OUString( wBuff, nLen );
166 }
167 
169 {
170  CFormatEtc fetc( cf, TYMED_NULL, nullptr, DVASPECT_CONTENT );
171 
172  switch( cf )
173  {
174  case CF_METAFILEPICT:
175  fetc.setTymed( TYMED_MFPICT );
176  break;
177 
178  case CF_ENHMETAFILE:
179  fetc.setTymed( TYMED_ENHMF );
180  break;
181 
182  default:
183  fetc.setTymed( TYMED_HGLOBAL /*| TYMED_ISTREAM*/ );
184  }
185 
186  /*
187  hack: in order to paste urls copied by Internet Explorer
188  with "copy link" we set the lindex member to 0
189  but if we really want to support CFSTR_FILECONTENT and
190  the accompany format CFSTR_FILEDESCRIPTOR (FileGroupDescriptor)
191  the client of the clipboard service has to provide a id
192  of which FileContents it wants to paste
193  see MSDN: "Handling Shell Data Transfer Scenarios"
194  */
195  if ( cf == RegisterClipboardFormat( CFSTR_FILECONTENTS ) )
196  fetc.setLindex( 0 );
197 
198  return fetc;
199 }
200 
202 {
203  return ( (cf == CF_TEXT) || (cf == CF_OEMTEXT) );
204 }
205 
207 {
208  return ( cf == CF_UNICODETEXT );
209 }
210 
212 {
213  return ( isOemOrAnsiTextFormat( cf ) || isUnicodeTextFormat( cf ) );
214 }
215 
217 {
218  OUString clipFormatName = getClipboardFormatName( cf );
219  return ( clipFormatName == "HTML Format" );
220 }
221 
223 {
224  OUString clipFormatName = getClipboardFormatName( cf );
225  return clipFormatName.equalsIgnoreAsciiCase( "HTML (HyperText Markup Language)" );
226 }
227 
228 OUString CDataFormatTranslator::getTextCharsetFromLCID( LCID lcid, CLIPFORMAT aClipformat )
229 {
230  OSL_ASSERT( isOemOrAnsiTextFormat( aClipformat ) );
231 
232  OUString charset;
233  if ( CF_TEXT == aClipformat )
234  {
235  charset = getMimeCharsetFromLocaleId(
236  lcid,
237  LOCALE_IDEFAULTANSICODEPAGE,
239  }
240  else if ( CF_OEMTEXT == aClipformat )
241  {
242  charset = getMimeCharsetFromLocaleId(
243  lcid,
244  LOCALE_IDEFAULTCODEPAGE,
246  }
247  else // CF_UNICODE
248  OSL_ASSERT( false );
249 
250  return charset;
251 }
252 
253 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const Type CPPUTYPE_OUSTRING
Type
const Type CPPUTYPE_SALINT8
static bool isUnicodeTextFormat(CLIPFORMAT cf)
CFormatEtc getFormatEtcFromDataFlavor(const css::datatransfer::DataFlavor &aDataFlavor) const
sal_uInt16 sal_Unicode
css::datatransfer::DataFlavor getDataFlavorFromFormatEtc(const FORMATETC &aFormatEtc, LCID lcid=GetThreadLocale()) const
static bool isHTMLFormat(CLIPFORMAT cf)
static OUString getClipboardFormatName(CLIPFORMAT aClipformat)
static CFormatEtc getFormatEtcForClipformat(CLIPFORMAT cf)
OUString getMimeCharsetFromLocaleId(LCID lcid, LCTYPE lctype, const OUString &aPrefix)
Definition: ImplHelper.cxx:125
static bool isOemOrAnsiTextFormat(CLIPFORMAT cf)
const OUString PRE_WINDOWS_CODEPAGE("windows")
static CFormatEtc getFormatEtcForClipformatName(const OUString &aClipFmtName)
static bool isTextFormat(CLIPFORMAT cf)
static OUString getTextCharsetFromLCID(LCID lcid, CLIPFORMAT aClipformat)
const OUString PRE_OEM_CODEPAGE("cp")
css::uno::Type const & get()
CDataFormatTranslator(const css::uno::Reference< css::uno::XComponentContext > &rxContext)
const sal_Int32 MAX_CLIPFORMAT_NAME
static bool isTextHtmlFormat(CLIPFORMAT cf)
void setLindex(LONG lindex)
Definition: Fetc.cxx:151
const Type CPPUTYPE_SEQSALINT8
const Type CPPUTYPE_SALINT32
void setTymed(DWORD tymed)
Definition: Fetc.cxx:133
const sal_Int32 CF_INVALID
Definition: WinClip.hxx:24