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 
50 const OUString TEXT_PLAIN_CHARSET ("text/plain;charset=");
51 const OUString HPNAME_OEM_ANSI_TEXT ("OEM/ANSI Text");
52 
53 const OUString HTML_FORMAT_NAME_WINDOWS ("HTML Format");
54 const OUString HTML_FORMAT_NAME_SOFFICE ("HTML (HyperText Markup Language)");
55 
57 {
58  m_XDataFormatTranslator = DataFormatTranslator::create( rxContext );
59 }
60 
61 CFormatEtc CDataFormatTranslator::getFormatEtcFromDataFlavor( const DataFlavor& aDataFlavor ) const
62 {
63  sal_Int32 cf = CF_INVALID;
64 
65  try
66  {
67  if( m_XDataFormatTranslator.is( ) )
68  {
69  Any aFormat = m_XDataFormatTranslator->getSystemDataTypeFromDataFlavor( aDataFlavor );
70 
71  if ( aFormat.hasValue( ) )
72  {
73  if ( aFormat.getValueType( ) == CPPUTYPE_SALINT32 )
74  {
75  aFormat >>= cf;
76  OSL_ENSURE( CF_INVALID != cf, "Invalid Clipboard format delivered" );
77  }
78  else if ( aFormat.getValueType( ) == CPPUTYPE_OUSTRING )
79  {
80  OUString aClipFmtName;
81  aFormat >>= aClipFmtName;
82 
83  OSL_ASSERT( aClipFmtName.getLength( ) );
84  cf = RegisterClipboardFormatW( o3tl::toW(aClipFmtName.getStr( )) );
85 
86  OSL_ENSURE( CF_INVALID != cf, "RegisterClipboardFormat failed" );
87  }
88  else
89  OSL_FAIL( "Wrong Any-Type detected" );
90  }
91  }
92  }
93  catch( ... )
94  {
95  OSL_FAIL( "Unexpected error" );
96  }
97 
98  return sal::static_int_cast<CFormatEtc>(getFormatEtcForClipformat( sal::static_int_cast<CLIPFORMAT>(cf) ));
99 }
100 
101 DataFlavor CDataFormatTranslator::getDataFlavorFromFormatEtc( const FORMATETC& aFormatEtc, LCID lcid ) const
102 {
103  DataFlavor aFlavor;
104 
105  try
106  {
107  CLIPFORMAT aClipformat = aFormatEtc.cfFormat;
108 
109  Any aAny;
110  aAny <<= static_cast< sal_Int32 >( aClipformat );
111 
112  if ( isOemOrAnsiTextFormat( aClipformat ) )
113  {
114  aFlavor.MimeType = TEXT_PLAIN_CHARSET;
115  aFlavor.MimeType += getTextCharsetFromLCID( lcid, aClipformat );
116 
117  aFlavor.HumanPresentableName = HPNAME_OEM_ANSI_TEXT;
118  aFlavor.DataType = CPPUTYPE_SEQSALINT8;
119  }
120  else if ( CF_INVALID != aClipformat )
121  {
122  if ( m_XDataFormatTranslator.is( ) )
123  {
124  aFlavor = m_XDataFormatTranslator->getDataFlavorFromSystemDataType( aAny );
125 
126  if ( !aFlavor.MimeType.getLength( ) )
127  {
128  // lookup of DataFlavor from clipboard format id
129  // failed, so we try to resolve via clipboard
130  // format name
131  OUString clipFormatName = getClipboardFormatName( aClipformat );
132 
133  // if we could not get a clipboard format name an
134  // error must have occurred or it is a standard
135  // clipboard format that we don't translate, e.g.
136  // CF_BITMAP (the office only uses CF_DIB)
137  if ( clipFormatName.getLength( ) )
138  {
139  aAny <<= clipFormatName;
140  aFlavor = m_XDataFormatTranslator->getDataFlavorFromSystemDataType( aAny );
141  }
142  }
143  }
144  }
145  }
146  catch( ... )
147  {
148  OSL_FAIL( "Unexpected error" );
149  }
150 
151  return aFlavor;
152 }
153 
155 {
156  // check parameter
157  if ( !aClipFmtName.getLength( ) )
158  return CFormatEtc( CF_INVALID );
159 
160  CLIPFORMAT cf = sal::static_int_cast<CLIPFORMAT>(RegisterClipboardFormatW( o3tl::toW(aClipFmtName.getStr( )) ));
161  return getFormatEtcForClipformat( cf );
162 }
163 
164 OUString CDataFormatTranslator::getClipboardFormatName( CLIPFORMAT aClipformat )
165 {
166  OSL_PRECOND( CF_INVALID != aClipformat, "Invalid clipboard format" );
167 
168  sal_Unicode wBuff[ MAX_CLIPFORMAT_NAME + 1 ]; // Null terminator isn't counted, apparently.
169  sal_Int32 nLen = GetClipboardFormatNameW( aClipformat, o3tl::toW(wBuff), MAX_CLIPFORMAT_NAME );
170 
171  return OUString( wBuff, nLen );
172 }
173 
175 {
176  CFormatEtc fetc( cf, TYMED_NULL, nullptr, DVASPECT_CONTENT );
177 
178  switch( cf )
179  {
180  case CF_METAFILEPICT:
181  fetc.setTymed( TYMED_MFPICT );
182  break;
183 
184  case CF_ENHMETAFILE:
185  fetc.setTymed( TYMED_ENHMF );
186  break;
187 
188  default:
189  fetc.setTymed( TYMED_HGLOBAL /*| TYMED_ISTREAM*/ );
190  }
191 
192  /*
193  hack: in order to paste urls copied by Internet Explorer
194  with "copy link" we set the lindex member to 0
195  but if we really want to support CFSTR_FILECONTENT and
196  the accompany format CFSTR_FILEDESCRIPTOR (FileGroupDescriptor)
197  the client of the clipboard service has to provide a id
198  of which FileContents it wants to paste
199  see MSDN: "Handling Shell Data Transfer Scenarios"
200  */
201  if ( cf == RegisterClipboardFormat( CFSTR_FILECONTENTS ) )
202  fetc.setLindex( 0 );
203 
204  return fetc;
205 }
206 
208 {
209  return ( (cf == CF_TEXT) || (cf == CF_OEMTEXT) );
210 }
211 
213 {
214  return ( cf == CF_UNICODETEXT );
215 }
216 
218 {
219  return ( isOemOrAnsiTextFormat( cf ) || isUnicodeTextFormat( cf ) );
220 }
221 
223 {
224  OUString clipFormatName = getClipboardFormatName( cf );
225  return ( clipFormatName == HTML_FORMAT_NAME_WINDOWS );
226 }
227 
229 {
230  OUString clipFormatName = getClipboardFormatName( cf );
231  return clipFormatName.equalsIgnoreAsciiCase( HTML_FORMAT_NAME_SOFFICE );
232 }
233 
234 OUString CDataFormatTranslator::getTextCharsetFromLCID( LCID lcid, CLIPFORMAT aClipformat )
235 {
236  OSL_ASSERT( isOemOrAnsiTextFormat( aClipformat ) );
237 
238  OUString charset;
239  if ( CF_TEXT == aClipformat )
240  {
241  charset = getMimeCharsetFromLocaleId(
242  lcid,
243  LOCALE_IDEFAULTANSICODEPAGE,
245  }
246  else if ( CF_OEMTEXT == aClipformat )
247  {
248  charset = getMimeCharsetFromLocaleId(
249  lcid,
250  LOCALE_IDEFAULTCODEPAGE,
252  }
253  else // CF_UNICODE
254  OSL_ASSERT( false );
255 
256  return charset;
257 }
258 
259 /* 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 TEXT_PLAIN_CHARSET("text/plain;charset=")
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
const OUString HTML_FORMAT_NAME_WINDOWS("HTML Format")
const OUString HTML_FORMAT_NAME_SOFFICE("HTML (HyperText Markup Language)")
const OUString HPNAME_OEM_ANSI_TEXT("OEM/ANSI Text")
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:25