1*b1cdbd2cSJim Jagielski /**************************************************************
2*b1cdbd2cSJim Jagielski *
3*b1cdbd2cSJim Jagielski * Licensed to the Apache Software Foundation (ASF) under one
4*b1cdbd2cSJim Jagielski * or more contributor license agreements. See the NOTICE file
5*b1cdbd2cSJim Jagielski * distributed with this work for additional information
6*b1cdbd2cSJim Jagielski * regarding copyright ownership. The ASF licenses this file
7*b1cdbd2cSJim Jagielski * to you under the Apache License, Version 2.0 (the
8*b1cdbd2cSJim Jagielski * "License"); you may not use this file except in compliance
9*b1cdbd2cSJim Jagielski * with the License. You may obtain a copy of the License at
10*b1cdbd2cSJim Jagielski *
11*b1cdbd2cSJim Jagielski * http://www.apache.org/licenses/LICENSE-2.0
12*b1cdbd2cSJim Jagielski *
13*b1cdbd2cSJim Jagielski * Unless required by applicable law or agreed to in writing,
14*b1cdbd2cSJim Jagielski * software distributed under the License is distributed on an
15*b1cdbd2cSJim Jagielski * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1cdbd2cSJim Jagielski * KIND, either express or implied. See the License for the
17*b1cdbd2cSJim Jagielski * specific language governing permissions and limitations
18*b1cdbd2cSJim Jagielski * under the License.
19*b1cdbd2cSJim Jagielski *
20*b1cdbd2cSJim Jagielski *************************************************************/
21*b1cdbd2cSJim Jagielski
22*b1cdbd2cSJim Jagielski
23*b1cdbd2cSJim Jagielski
24*b1cdbd2cSJim Jagielski // MARKER(update_precomp.py): autogen include statement, do not remove
25*b1cdbd2cSJim Jagielski #include "precompiled_dtrans.hxx"
26*b1cdbd2cSJim Jagielski
27*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
28*b1cdbd2cSJim Jagielski // includes
29*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
30*b1cdbd2cSJim Jagielski #include <sal/types.h>
31*b1cdbd2cSJim Jagielski #include <rtl/process.h>
32*b1cdbd2cSJim Jagielski
33*b1cdbd2cSJim Jagielski #ifndef _DOWRAPPERTRANSFERABLE_HXX_
34*b1cdbd2cSJim Jagielski #include "DOTransferable.hxx"
35*b1cdbd2cSJim Jagielski #endif
36*b1cdbd2cSJim Jagielski #include "..\misc\ImplHelper.hxx"
37*b1cdbd2cSJim Jagielski #include "..\misc\WinClip.hxx"
38*b1cdbd2cSJim Jagielski #include "DTransHelper.hxx"
39*b1cdbd2cSJim Jagielski #include "..\misc\ImplHelper.hxx"
40*b1cdbd2cSJim Jagielski #include "TxtCnvtHlp.hxx"
41*b1cdbd2cSJim Jagielski #include "MimeAttrib.hxx"
42*b1cdbd2cSJim Jagielski #include "FmtFilter.hxx"
43*b1cdbd2cSJim Jagielski #include "Fetc.hxx"
44*b1cdbd2cSJim Jagielski
45*b1cdbd2cSJim Jagielski
46*b1cdbd2cSJim Jagielski #if(_MSC_VER < 1300) && !defined(__MINGW32__)
47*b1cdbd2cSJim Jagielski #include <olestd.h>
48*b1cdbd2cSJim Jagielski #endif
49*b1cdbd2cSJim Jagielski
50*b1cdbd2cSJim Jagielski #define STR2(x) #x
51*b1cdbd2cSJim Jagielski #define STR(x) STR2(x)
52*b1cdbd2cSJim Jagielski #define PRAGMA_MSG( msg ) message( __FILE__ "(" STR(__LINE__) "): " #msg )
53*b1cdbd2cSJim Jagielski
54*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
55*b1cdbd2cSJim Jagielski // namespace directives
56*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
57*b1cdbd2cSJim Jagielski
58*b1cdbd2cSJim Jagielski using namespace rtl;
59*b1cdbd2cSJim Jagielski using namespace std;
60*b1cdbd2cSJim Jagielski using namespace osl;
61*b1cdbd2cSJim Jagielski using namespace cppu;
62*b1cdbd2cSJim Jagielski using namespace com::sun::star::uno;
63*b1cdbd2cSJim Jagielski using namespace com::sun::star::datatransfer;
64*b1cdbd2cSJim Jagielski using namespace com::sun::star::io;
65*b1cdbd2cSJim Jagielski using namespace com::sun::star::lang;
66*b1cdbd2cSJim Jagielski using namespace com::sun::star::container;
67*b1cdbd2cSJim Jagielski
68*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
69*b1cdbd2cSJim Jagielski //
70*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
71*b1cdbd2cSJim Jagielski
72*b1cdbd2cSJim Jagielski namespace
73*b1cdbd2cSJim Jagielski {
74*b1cdbd2cSJim Jagielski const Type CPPUTYPE_SEQINT8 = getCppuType( ( Sequence< sal_Int8 >* )0 );
75*b1cdbd2cSJim Jagielski const Type CPPUTYPE_OUSTRING = getCppuType( (OUString*)0 );
76*b1cdbd2cSJim Jagielski
77*b1cdbd2cSJim Jagielski inline
isValidFlavor(const DataFlavor & aFlavor)78*b1cdbd2cSJim Jagielski sal_Bool isValidFlavor( const DataFlavor& aFlavor )
79*b1cdbd2cSJim Jagielski {
80*b1cdbd2cSJim Jagielski return ( aFlavor.MimeType.getLength( ) &&
81*b1cdbd2cSJim Jagielski ( ( aFlavor.DataType == CPPUTYPE_SEQINT8 ) ||
82*b1cdbd2cSJim Jagielski ( aFlavor.DataType == CPPUTYPE_OUSTRING ) ) );
83*b1cdbd2cSJim Jagielski }
84*b1cdbd2cSJim Jagielski
85*b1cdbd2cSJim Jagielski } // end namespace
86*b1cdbd2cSJim Jagielski
87*b1cdbd2cSJim Jagielski
88*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
89*b1cdbd2cSJim Jagielski // ctor
90*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
91*b1cdbd2cSJim Jagielski
CDOTransferable(const Reference<XMultiServiceFactory> & ServiceManager,IDataObjectPtr rDataObject)92*b1cdbd2cSJim Jagielski CDOTransferable::CDOTransferable(
93*b1cdbd2cSJim Jagielski const Reference< XMultiServiceFactory >& ServiceManager, IDataObjectPtr rDataObject ) :
94*b1cdbd2cSJim Jagielski m_rDataObject( rDataObject ),
95*b1cdbd2cSJim Jagielski m_SrvMgr( ServiceManager ),
96*b1cdbd2cSJim Jagielski m_DataFormatTranslator( m_SrvMgr ),
97*b1cdbd2cSJim Jagielski m_bUnicodeRegistered( sal_False ),
98*b1cdbd2cSJim Jagielski m_TxtFormatOnClipboard( CF_INVALID )
99*b1cdbd2cSJim Jagielski {
100*b1cdbd2cSJim Jagielski }
101*b1cdbd2cSJim Jagielski
102*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
103*b1cdbd2cSJim Jagielski //
104*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
105*b1cdbd2cSJim Jagielski
getTransferData(const DataFlavor & aFlavor)106*b1cdbd2cSJim Jagielski Any SAL_CALL CDOTransferable::getTransferData( const DataFlavor& aFlavor )
107*b1cdbd2cSJim Jagielski throw( UnsupportedFlavorException, IOException, RuntimeException )
108*b1cdbd2cSJim Jagielski {
109*b1cdbd2cSJim Jagielski OSL_ASSERT( isValidFlavor( aFlavor ) );
110*b1cdbd2cSJim Jagielski
111*b1cdbd2cSJim Jagielski MutexGuard aGuard( m_aMutex );
112*b1cdbd2cSJim Jagielski
113*b1cdbd2cSJim Jagielski //------------------------------------------------
114*b1cdbd2cSJim Jagielski // convert dataflavor to formatetc
115*b1cdbd2cSJim Jagielski //------------------------------------------------
116*b1cdbd2cSJim Jagielski
117*b1cdbd2cSJim Jagielski CFormatEtc fetc = m_DataFormatTranslator.getFormatEtcFromDataFlavor( aFlavor );
118*b1cdbd2cSJim Jagielski OSL_ASSERT( CF_INVALID != fetc.getClipformat() );
119*b1cdbd2cSJim Jagielski
120*b1cdbd2cSJim Jagielski //------------------------------------------------
121*b1cdbd2cSJim Jagielski // get the data from clipboard in a byte stream
122*b1cdbd2cSJim Jagielski //------------------------------------------------
123*b1cdbd2cSJim Jagielski
124*b1cdbd2cSJim Jagielski ByteSequence_t clipDataStream;
125*b1cdbd2cSJim Jagielski
126*b1cdbd2cSJim Jagielski try
127*b1cdbd2cSJim Jagielski {
128*b1cdbd2cSJim Jagielski clipDataStream = getClipboardData( fetc );
129*b1cdbd2cSJim Jagielski }
130*b1cdbd2cSJim Jagielski catch( UnsupportedFlavorException& )
131*b1cdbd2cSJim Jagielski {
132*b1cdbd2cSJim Jagielski if ( m_DataFormatTranslator.isUnicodeTextFormat( fetc.getClipformat( ) ) &&
133*b1cdbd2cSJim Jagielski m_bUnicodeRegistered )
134*b1cdbd2cSJim Jagielski {
135*b1cdbd2cSJim Jagielski OUString aUnicodeText = synthesizeUnicodeText( );
136*b1cdbd2cSJim Jagielski Any aAny = makeAny( aUnicodeText );
137*b1cdbd2cSJim Jagielski return aAny;
138*b1cdbd2cSJim Jagielski }
139*b1cdbd2cSJim Jagielski // #124085# CF_DIBV5 should not be possible, but keep for reading from the
140*b1cdbd2cSJim Jagielski // clipboard for being on the safe side
141*b1cdbd2cSJim Jagielski else if(CF_DIBV5 == fetc.getClipformat())
142*b1cdbd2cSJim Jagielski {
143*b1cdbd2cSJim Jagielski // #123407# CF_DIBV5 has priority; if the try to fetch this failed,
144*b1cdbd2cSJim Jagielski // check CF_DIB availability as an alternative
145*b1cdbd2cSJim Jagielski fetc.setClipformat(CF_DIB);
146*b1cdbd2cSJim Jagielski
147*b1cdbd2cSJim Jagielski try
148*b1cdbd2cSJim Jagielski {
149*b1cdbd2cSJim Jagielski clipDataStream = getClipboardData( fetc );
150*b1cdbd2cSJim Jagielski }
151*b1cdbd2cSJim Jagielski catch( UnsupportedFlavorException& )
152*b1cdbd2cSJim Jagielski {
153*b1cdbd2cSJim Jagielski throw; // pass through, tried all possibilities
154*b1cdbd2cSJim Jagielski }
155*b1cdbd2cSJim Jagielski }
156*b1cdbd2cSJim Jagielski else
157*b1cdbd2cSJim Jagielski throw; // pass through exception
158*b1cdbd2cSJim Jagielski }
159*b1cdbd2cSJim Jagielski
160*b1cdbd2cSJim Jagielski //------------------------------------------------
161*b1cdbd2cSJim Jagielski // return the data as any
162*b1cdbd2cSJim Jagielski //------------------------------------------------
163*b1cdbd2cSJim Jagielski
164*b1cdbd2cSJim Jagielski return byteStreamToAny( clipDataStream, aFlavor.DataType );
165*b1cdbd2cSJim Jagielski }
166*b1cdbd2cSJim Jagielski
167*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
168*b1cdbd2cSJim Jagielski // getTransferDataFlavors
169*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
170*b1cdbd2cSJim Jagielski
getTransferDataFlavors()171*b1cdbd2cSJim Jagielski Sequence< DataFlavor > SAL_CALL CDOTransferable::getTransferDataFlavors( )
172*b1cdbd2cSJim Jagielski throw( RuntimeException )
173*b1cdbd2cSJim Jagielski {
174*b1cdbd2cSJim Jagielski return m_FlavorList;
175*b1cdbd2cSJim Jagielski }
176*b1cdbd2cSJim Jagielski
177*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
178*b1cdbd2cSJim Jagielski // isDataFlavorSupported
179*b1cdbd2cSJim Jagielski // returns true if we find a DataFlavor with the same MimeType and
180*b1cdbd2cSJim Jagielski // DataType
181*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
182*b1cdbd2cSJim Jagielski
isDataFlavorSupported(const DataFlavor & aFlavor)183*b1cdbd2cSJim Jagielski sal_Bool SAL_CALL CDOTransferable::isDataFlavorSupported( const DataFlavor& aFlavor )
184*b1cdbd2cSJim Jagielski throw( RuntimeException )
185*b1cdbd2cSJim Jagielski {
186*b1cdbd2cSJim Jagielski OSL_ASSERT( isValidFlavor( aFlavor ) );
187*b1cdbd2cSJim Jagielski
188*b1cdbd2cSJim Jagielski for ( sal_Int32 i = 0; i < m_FlavorList.getLength( ); i++ )
189*b1cdbd2cSJim Jagielski if ( compareDataFlavors( aFlavor, m_FlavorList[i] ) )
190*b1cdbd2cSJim Jagielski return sal_True;
191*b1cdbd2cSJim Jagielski
192*b1cdbd2cSJim Jagielski return sal_False;
193*b1cdbd2cSJim Jagielski }
194*b1cdbd2cSJim Jagielski
195*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
196*b1cdbd2cSJim Jagielski // helper function
197*b1cdbd2cSJim Jagielski // the list of datafalvors currently on the clipboard will be initialized
198*b1cdbd2cSJim Jagielski // only once; if the client of this Transferable will hold a reference
199*b1cdbd2cSJim Jagielski // to it und the underlying clipboard content changes, the client does
200*b1cdbd2cSJim Jagielski // possible operate on a invalid list
201*b1cdbd2cSJim Jagielski // if there is only text on the clipboard we will also offer unicode text
202*b1cdbd2cSJim Jagielski // an synthesize this format on the fly if requested, to accomplish this
203*b1cdbd2cSJim Jagielski // we save the first offered text format which we will later use for the
204*b1cdbd2cSJim Jagielski // conversion
205*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
206*b1cdbd2cSJim Jagielski
initFlavorList()207*b1cdbd2cSJim Jagielski void SAL_CALL CDOTransferable::initFlavorList( )
208*b1cdbd2cSJim Jagielski {
209*b1cdbd2cSJim Jagielski IEnumFORMATETCPtr pEnumFormatEtc;
210*b1cdbd2cSJim Jagielski HRESULT hr = m_rDataObject->EnumFormatEtc( DATADIR_GET, &pEnumFormatEtc );
211*b1cdbd2cSJim Jagielski if ( SUCCEEDED( hr ) )
212*b1cdbd2cSJim Jagielski {
213*b1cdbd2cSJim Jagielski pEnumFormatEtc->Reset( );
214*b1cdbd2cSJim Jagielski
215*b1cdbd2cSJim Jagielski FORMATETC fetc;
216*b1cdbd2cSJim Jagielski while ( S_FALSE != pEnumFormatEtc->Next( 1, &fetc, NULL ) )
217*b1cdbd2cSJim Jagielski {
218*b1cdbd2cSJim Jagielski // we use locales only to determine the
219*b1cdbd2cSJim Jagielski // charset if there is text on the cliboard
220*b1cdbd2cSJim Jagielski // we don't offer this format
221*b1cdbd2cSJim Jagielski if ( CF_LOCALE == fetc.cfFormat )
222*b1cdbd2cSJim Jagielski continue;
223*b1cdbd2cSJim Jagielski
224*b1cdbd2cSJim Jagielski DataFlavor aFlavor = formatEtcToDataFlavor( fetc );
225*b1cdbd2cSJim Jagielski
226*b1cdbd2cSJim Jagielski // if text or oemtext is offered we also pretend to have unicode text
227*b1cdbd2cSJim Jagielski if ( m_DataFormatTranslator.isOemOrAnsiTextFormat( fetc.cfFormat ) &&
228*b1cdbd2cSJim Jagielski !m_bUnicodeRegistered )
229*b1cdbd2cSJim Jagielski {
230*b1cdbd2cSJim Jagielski addSupportedFlavor( aFlavor );
231*b1cdbd2cSJim Jagielski
232*b1cdbd2cSJim Jagielski m_TxtFormatOnClipboard = fetc.cfFormat;
233*b1cdbd2cSJim Jagielski m_bUnicodeRegistered = sal_True;
234*b1cdbd2cSJim Jagielski
235*b1cdbd2cSJim Jagielski // register unicode text as accompany format
236*b1cdbd2cSJim Jagielski aFlavor = formatEtcToDataFlavor(
237*b1cdbd2cSJim Jagielski m_DataFormatTranslator.getFormatEtcForClipformat( CF_UNICODETEXT ) );
238*b1cdbd2cSJim Jagielski addSupportedFlavor( aFlavor );
239*b1cdbd2cSJim Jagielski }
240*b1cdbd2cSJim Jagielski else if ( (CF_UNICODETEXT == fetc.cfFormat) && !m_bUnicodeRegistered )
241*b1cdbd2cSJim Jagielski {
242*b1cdbd2cSJim Jagielski addSupportedFlavor( aFlavor );
243*b1cdbd2cSJim Jagielski m_bUnicodeRegistered = sal_True;
244*b1cdbd2cSJim Jagielski }
245*b1cdbd2cSJim Jagielski else
246*b1cdbd2cSJim Jagielski addSupportedFlavor( aFlavor );
247*b1cdbd2cSJim Jagielski
248*b1cdbd2cSJim Jagielski // see MSDN IEnumFORMATETC
249*b1cdbd2cSJim Jagielski CoTaskMemFree( fetc.ptd );
250*b1cdbd2cSJim Jagielski }
251*b1cdbd2cSJim Jagielski }
252*b1cdbd2cSJim Jagielski }
253*b1cdbd2cSJim Jagielski
254*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
255*b1cdbd2cSJim Jagielski //
256*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
257*b1cdbd2cSJim Jagielski
258*b1cdbd2cSJim Jagielski inline
addSupportedFlavor(const DataFlavor & aFlavor)259*b1cdbd2cSJim Jagielski void SAL_CALL CDOTransferable::addSupportedFlavor( const DataFlavor& aFlavor )
260*b1cdbd2cSJim Jagielski {
261*b1cdbd2cSJim Jagielski // we ignore all formats that couldn't be translated
262*b1cdbd2cSJim Jagielski if ( aFlavor.MimeType.getLength( ) )
263*b1cdbd2cSJim Jagielski {
264*b1cdbd2cSJim Jagielski OSL_ASSERT( isValidFlavor( aFlavor ) );
265*b1cdbd2cSJim Jagielski
266*b1cdbd2cSJim Jagielski m_FlavorList.realloc( m_FlavorList.getLength( ) + 1 );
267*b1cdbd2cSJim Jagielski m_FlavorList[m_FlavorList.getLength( ) - 1] = aFlavor;
268*b1cdbd2cSJim Jagielski }
269*b1cdbd2cSJim Jagielski }
270*b1cdbd2cSJim Jagielski
271*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
272*b1cdbd2cSJim Jagielski // helper function
273*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
274*b1cdbd2cSJim Jagielski
275*b1cdbd2cSJim Jagielski //inline
formatEtcToDataFlavor(const FORMATETC & aFormatEtc)276*b1cdbd2cSJim Jagielski DataFlavor SAL_CALL CDOTransferable::formatEtcToDataFlavor( const FORMATETC& aFormatEtc )
277*b1cdbd2cSJim Jagielski {
278*b1cdbd2cSJim Jagielski DataFlavor aFlavor;
279*b1cdbd2cSJim Jagielski LCID lcid = 0;
280*b1cdbd2cSJim Jagielski
281*b1cdbd2cSJim Jagielski // for non-unicode text format we must provid a locale to get
282*b1cdbd2cSJim Jagielski // the character-set of the text, if there is no locale on the
283*b1cdbd2cSJim Jagielski // clipboard we assume the text is in a charset appropriate for
284*b1cdbd2cSJim Jagielski // the current thread locale
285*b1cdbd2cSJim Jagielski if ( (CF_TEXT == aFormatEtc.cfFormat) || (CF_OEMTEXT == aFormatEtc.cfFormat) )
286*b1cdbd2cSJim Jagielski lcid = getLocaleFromClipboard( );
287*b1cdbd2cSJim Jagielski
288*b1cdbd2cSJim Jagielski return m_DataFormatTranslator.getDataFlavorFromFormatEtc( aFormatEtc, lcid );
289*b1cdbd2cSJim Jagielski }
290*b1cdbd2cSJim Jagielski
291*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
292*b1cdbd2cSJim Jagielski // returns the current locale on clipboard; if there is no locale on
293*b1cdbd2cSJim Jagielski // clipboard the function returns the current thread locale
294*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
295*b1cdbd2cSJim Jagielski
getLocaleFromClipboard()296*b1cdbd2cSJim Jagielski LCID SAL_CALL CDOTransferable::getLocaleFromClipboard( )
297*b1cdbd2cSJim Jagielski {
298*b1cdbd2cSJim Jagielski LCID lcid = GetThreadLocale( );
299*b1cdbd2cSJim Jagielski
300*b1cdbd2cSJim Jagielski try
301*b1cdbd2cSJim Jagielski {
302*b1cdbd2cSJim Jagielski CFormatEtc fetc = m_DataFormatTranslator.getFormatEtcForClipformat( CF_LOCALE );
303*b1cdbd2cSJim Jagielski ByteSequence_t aLCIDSeq = getClipboardData( fetc );
304*b1cdbd2cSJim Jagielski lcid = *(reinterpret_cast<LCID*>( aLCIDSeq.getArray( ) ) );
305*b1cdbd2cSJim Jagielski
306*b1cdbd2cSJim Jagielski // because of a Win95/98 Bug; there the high word
307*b1cdbd2cSJim Jagielski // of a locale has the same value as the
308*b1cdbd2cSJim Jagielski // low word e.g. 0x07040704 that's not right
309*b1cdbd2cSJim Jagielski // correct is 0x00000704
310*b1cdbd2cSJim Jagielski lcid &= 0x0000FFFF;
311*b1cdbd2cSJim Jagielski }
312*b1cdbd2cSJim Jagielski catch(...)
313*b1cdbd2cSJim Jagielski {
314*b1cdbd2cSJim Jagielski // we take the default locale
315*b1cdbd2cSJim Jagielski }
316*b1cdbd2cSJim Jagielski
317*b1cdbd2cSJim Jagielski return lcid;
318*b1cdbd2cSJim Jagielski }
319*b1cdbd2cSJim Jagielski
320*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
321*b1cdbd2cSJim Jagielski // i think it's not necessary to call ReleaseStgMedium
322*b1cdbd2cSJim Jagielski // in case of failures because nothing should have been
323*b1cdbd2cSJim Jagielski // allocated etc.
324*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
325*b1cdbd2cSJim Jagielski
getClipboardData(CFormatEtc & aFormatEtc)326*b1cdbd2cSJim Jagielski CDOTransferable::ByteSequence_t SAL_CALL CDOTransferable::getClipboardData( CFormatEtc& aFormatEtc )
327*b1cdbd2cSJim Jagielski {
328*b1cdbd2cSJim Jagielski STGMEDIUM stgmedium;
329*b1cdbd2cSJim Jagielski HRESULT hr = m_rDataObject->GetData( aFormatEtc, &stgmedium );
330*b1cdbd2cSJim Jagielski
331*b1cdbd2cSJim Jagielski // in case of failure to get a WMF metafile handle, try to get a memory block
332*b1cdbd2cSJim Jagielski if( FAILED( hr ) &&
333*b1cdbd2cSJim Jagielski ( CF_METAFILEPICT == aFormatEtc.getClipformat() ) &&
334*b1cdbd2cSJim Jagielski ( TYMED_MFPICT == aFormatEtc.getTymed() ) )
335*b1cdbd2cSJim Jagielski {
336*b1cdbd2cSJim Jagielski CFormatEtc aTempFormat( aFormatEtc );
337*b1cdbd2cSJim Jagielski aTempFormat.setTymed( TYMED_HGLOBAL );
338*b1cdbd2cSJim Jagielski hr = m_rDataObject->GetData( aTempFormat, &stgmedium );
339*b1cdbd2cSJim Jagielski }
340*b1cdbd2cSJim Jagielski
341*b1cdbd2cSJim Jagielski if ( FAILED( hr ) )
342*b1cdbd2cSJim Jagielski {
343*b1cdbd2cSJim Jagielski OSL_ASSERT( (hr != E_INVALIDARG) &&
344*b1cdbd2cSJim Jagielski (hr != DV_E_DVASPECT) &&
345*b1cdbd2cSJim Jagielski (hr != DV_E_LINDEX) &&
346*b1cdbd2cSJim Jagielski (hr != DV_E_TYMED) );
347*b1cdbd2cSJim Jagielski
348*b1cdbd2cSJim Jagielski if ( DV_E_FORMATETC == hr )
349*b1cdbd2cSJim Jagielski throw UnsupportedFlavorException( );
350*b1cdbd2cSJim Jagielski else if ( STG_E_MEDIUMFULL == hr )
351*b1cdbd2cSJim Jagielski throw IOException( );
352*b1cdbd2cSJim Jagielski else
353*b1cdbd2cSJim Jagielski throw RuntimeException( );
354*b1cdbd2cSJim Jagielski }
355*b1cdbd2cSJim Jagielski
356*b1cdbd2cSJim Jagielski ByteSequence_t byteStream;
357*b1cdbd2cSJim Jagielski
358*b1cdbd2cSJim Jagielski try
359*b1cdbd2cSJim Jagielski {
360*b1cdbd2cSJim Jagielski if ( CF_ENHMETAFILE == aFormatEtc.getClipformat() )
361*b1cdbd2cSJim Jagielski byteStream = WinENHMFPictToOOMFPict( stgmedium.hEnhMetaFile );
362*b1cdbd2cSJim Jagielski else if (CF_HDROP == aFormatEtc.getClipformat())
363*b1cdbd2cSJim Jagielski byteStream = CF_HDROPToFileList(stgmedium.hGlobal);
364*b1cdbd2cSJim Jagielski else if ( CF_BITMAP == aFormatEtc.getClipformat() )
365*b1cdbd2cSJim Jagielski {
366*b1cdbd2cSJim Jagielski byteStream = WinBITMAPToOOBMP(stgmedium.hBitmap);
367*b1cdbd2cSJim Jagielski if( aFormatEtc.getTymed() == TYMED_GDI &&
368*b1cdbd2cSJim Jagielski ! stgmedium.pUnkForRelease )
369*b1cdbd2cSJim Jagielski {
370*b1cdbd2cSJim Jagielski DeleteObject(stgmedium.hBitmap);
371*b1cdbd2cSJim Jagielski }
372*b1cdbd2cSJim Jagielski }
373*b1cdbd2cSJim Jagielski else
374*b1cdbd2cSJim Jagielski {
375*b1cdbd2cSJim Jagielski clipDataToByteStream( aFormatEtc.getClipformat( ), stgmedium, byteStream );
376*b1cdbd2cSJim Jagielski
377*b1cdbd2cSJim Jagielski // format conversion if necessary
378*b1cdbd2cSJim Jagielski // #124085# DIBV5 should not happen currently, but keep as a hint here
379*b1cdbd2cSJim Jagielski if(CF_DIBV5 == aFormatEtc.getClipformat() || CF_DIB == aFormatEtc.getClipformat())
380*b1cdbd2cSJim Jagielski {
381*b1cdbd2cSJim Jagielski byteStream = WinDIBToOOBMP(byteStream);
382*b1cdbd2cSJim Jagielski }
383*b1cdbd2cSJim Jagielski else if(CF_METAFILEPICT == aFormatEtc.getClipformat())
384*b1cdbd2cSJim Jagielski {
385*b1cdbd2cSJim Jagielski byteStream = WinMFPictToOOMFPict(byteStream);
386*b1cdbd2cSJim Jagielski }
387*b1cdbd2cSJim Jagielski }
388*b1cdbd2cSJim Jagielski
389*b1cdbd2cSJim Jagielski ReleaseStgMedium( &stgmedium );
390*b1cdbd2cSJim Jagielski }
391*b1cdbd2cSJim Jagielski catch( CStgTransferHelper::CStgTransferException& )
392*b1cdbd2cSJim Jagielski {
393*b1cdbd2cSJim Jagielski ReleaseStgMedium( &stgmedium );
394*b1cdbd2cSJim Jagielski throw IOException( );
395*b1cdbd2cSJim Jagielski }
396*b1cdbd2cSJim Jagielski
397*b1cdbd2cSJim Jagielski return byteStream;
398*b1cdbd2cSJim Jagielski }
399*b1cdbd2cSJim Jagielski
400*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
401*b1cdbd2cSJim Jagielski //
402*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
403*b1cdbd2cSJim Jagielski
synthesizeUnicodeText()404*b1cdbd2cSJim Jagielski OUString SAL_CALL CDOTransferable::synthesizeUnicodeText( )
405*b1cdbd2cSJim Jagielski {
406*b1cdbd2cSJim Jagielski ByteSequence_t aTextSequence;
407*b1cdbd2cSJim Jagielski CFormatEtc fetc;
408*b1cdbd2cSJim Jagielski LCID lcid = getLocaleFromClipboard( );
409*b1cdbd2cSJim Jagielski sal_uInt32 cpForTxtCnvt = 0;
410*b1cdbd2cSJim Jagielski
411*b1cdbd2cSJim Jagielski if ( CF_TEXT == m_TxtFormatOnClipboard )
412*b1cdbd2cSJim Jagielski {
413*b1cdbd2cSJim Jagielski fetc = m_DataFormatTranslator.getFormatEtcForClipformat( CF_TEXT );
414*b1cdbd2cSJim Jagielski aTextSequence = getClipboardData( fetc );
415*b1cdbd2cSJim Jagielski
416*b1cdbd2cSJim Jagielski // determine the codepage used for text conversion
417*b1cdbd2cSJim Jagielski cpForTxtCnvt = getWinCPFromLocaleId( lcid, LOCALE_IDEFAULTANSICODEPAGE ).toInt32( );
418*b1cdbd2cSJim Jagielski }
419*b1cdbd2cSJim Jagielski else if ( CF_OEMTEXT == m_TxtFormatOnClipboard )
420*b1cdbd2cSJim Jagielski {
421*b1cdbd2cSJim Jagielski fetc = m_DataFormatTranslator.getFormatEtcForClipformat( CF_OEMTEXT );
422*b1cdbd2cSJim Jagielski aTextSequence = getClipboardData( fetc );
423*b1cdbd2cSJim Jagielski
424*b1cdbd2cSJim Jagielski // determine the codepage used for text conversion
425*b1cdbd2cSJim Jagielski cpForTxtCnvt = getWinCPFromLocaleId( lcid, LOCALE_IDEFAULTCODEPAGE ).toInt32( );
426*b1cdbd2cSJim Jagielski }
427*b1cdbd2cSJim Jagielski else
428*b1cdbd2cSJim Jagielski OSL_ASSERT( sal_False );
429*b1cdbd2cSJim Jagielski
430*b1cdbd2cSJim Jagielski CStgTransferHelper stgTransferHelper;
431*b1cdbd2cSJim Jagielski
432*b1cdbd2cSJim Jagielski // convert the text
433*b1cdbd2cSJim Jagielski MultiByteToWideCharEx( cpForTxtCnvt,
434*b1cdbd2cSJim Jagielski reinterpret_cast<char*>( aTextSequence.getArray( ) ),
435*b1cdbd2cSJim Jagielski sal::static_int_cast<sal_uInt32>(-1), // Huh ?
436*b1cdbd2cSJim Jagielski stgTransferHelper,
437*b1cdbd2cSJim Jagielski sal_False);
438*b1cdbd2cSJim Jagielski
439*b1cdbd2cSJim Jagielski CRawHGlobalPtr ptrHGlob(stgTransferHelper);
440*b1cdbd2cSJim Jagielski sal_Unicode* pWChar = reinterpret_cast<sal_Unicode*>(ptrHGlob.GetMemPtr());
441*b1cdbd2cSJim Jagielski
442*b1cdbd2cSJim Jagielski return OUString(pWChar);
443*b1cdbd2cSJim Jagielski }
444*b1cdbd2cSJim Jagielski
445*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
446*b1cdbd2cSJim Jagielski //
447*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
448*b1cdbd2cSJim Jagielski
clipDataToByteStream(CLIPFORMAT cf,STGMEDIUM stgmedium,ByteSequence_t & aByteSequence)449*b1cdbd2cSJim Jagielski void CDOTransferable::clipDataToByteStream( CLIPFORMAT cf, STGMEDIUM stgmedium, ByteSequence_t& aByteSequence )
450*b1cdbd2cSJim Jagielski {
451*b1cdbd2cSJim Jagielski CStgTransferHelper memTransferHelper;
452*b1cdbd2cSJim Jagielski
453*b1cdbd2cSJim Jagielski switch( stgmedium.tymed )
454*b1cdbd2cSJim Jagielski {
455*b1cdbd2cSJim Jagielski case TYMED_HGLOBAL:
456*b1cdbd2cSJim Jagielski memTransferHelper.init( stgmedium.hGlobal );
457*b1cdbd2cSJim Jagielski break;
458*b1cdbd2cSJim Jagielski
459*b1cdbd2cSJim Jagielski case TYMED_MFPICT:
460*b1cdbd2cSJim Jagielski memTransferHelper.init( stgmedium.hMetaFilePict );
461*b1cdbd2cSJim Jagielski break;
462*b1cdbd2cSJim Jagielski
463*b1cdbd2cSJim Jagielski case TYMED_ENHMF:
464*b1cdbd2cSJim Jagielski memTransferHelper.init( stgmedium.hEnhMetaFile );
465*b1cdbd2cSJim Jagielski break;
466*b1cdbd2cSJim Jagielski
467*b1cdbd2cSJim Jagielski case TYMED_ISTREAM:
468*b1cdbd2cSJim Jagielski #ifdef _MSC_VER
469*b1cdbd2cSJim Jagielski #pragma PRAGMA_MSG( Has to be implemented )
470*b1cdbd2cSJim Jagielski #endif
471*b1cdbd2cSJim Jagielski break;
472*b1cdbd2cSJim Jagielski
473*b1cdbd2cSJim Jagielski default:
474*b1cdbd2cSJim Jagielski throw UnsupportedFlavorException( );
475*b1cdbd2cSJim Jagielski break;
476*b1cdbd2cSJim Jagielski }
477*b1cdbd2cSJim Jagielski
478*b1cdbd2cSJim Jagielski int nMemSize = memTransferHelper.memSize( cf );
479*b1cdbd2cSJim Jagielski aByteSequence.realloc( nMemSize );
480*b1cdbd2cSJim Jagielski memTransferHelper.read( aByteSequence.getArray( ), nMemSize );
481*b1cdbd2cSJim Jagielski }
482*b1cdbd2cSJim Jagielski
483*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
484*b1cdbd2cSJim Jagielski //
485*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
486*b1cdbd2cSJim Jagielski
487*b1cdbd2cSJim Jagielski inline
byteStreamToAny(ByteSequence_t & aByteStream,const Type & aRequestedDataType)488*b1cdbd2cSJim Jagielski Any CDOTransferable::byteStreamToAny( ByteSequence_t& aByteStream, const Type& aRequestedDataType )
489*b1cdbd2cSJim Jagielski {
490*b1cdbd2cSJim Jagielski Any aAny;
491*b1cdbd2cSJim Jagielski
492*b1cdbd2cSJim Jagielski if ( aRequestedDataType == CPPUTYPE_OUSTRING )
493*b1cdbd2cSJim Jagielski {
494*b1cdbd2cSJim Jagielski OUString str = byteStreamToOUString( aByteStream );
495*b1cdbd2cSJim Jagielski aAny = makeAny( str );
496*b1cdbd2cSJim Jagielski }
497*b1cdbd2cSJim Jagielski else
498*b1cdbd2cSJim Jagielski aAny = makeAny( aByteStream );
499*b1cdbd2cSJim Jagielski
500*b1cdbd2cSJim Jagielski return aAny;
501*b1cdbd2cSJim Jagielski }
502*b1cdbd2cSJim Jagielski
503*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
504*b1cdbd2cSJim Jagielski //
505*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
506*b1cdbd2cSJim Jagielski
507*b1cdbd2cSJim Jagielski inline
byteStreamToOUString(ByteSequence_t & aByteStream)508*b1cdbd2cSJim Jagielski OUString CDOTransferable::byteStreamToOUString( ByteSequence_t& aByteStream )
509*b1cdbd2cSJim Jagielski {
510*b1cdbd2cSJim Jagielski sal_Int32 nWChars;
511*b1cdbd2cSJim Jagielski sal_Int32 nMemSize = aByteStream.getLength( );
512*b1cdbd2cSJim Jagielski
513*b1cdbd2cSJim Jagielski // if there is a trailing L"\0" substract 1 from length
514*b1cdbd2cSJim Jagielski if ( 0 == aByteStream[ aByteStream.getLength( ) - 2 ] &&
515*b1cdbd2cSJim Jagielski 0 == aByteStream[ aByteStream.getLength( ) - 1 ] )
516*b1cdbd2cSJim Jagielski nWChars = static_cast< sal_Int32 >( nMemSize / sizeof( sal_Unicode ) ) - 1;
517*b1cdbd2cSJim Jagielski else
518*b1cdbd2cSJim Jagielski nWChars = static_cast< sal_Int32 >( nMemSize / sizeof( sal_Unicode ) );
519*b1cdbd2cSJim Jagielski
520*b1cdbd2cSJim Jagielski return OUString( reinterpret_cast< sal_Unicode* >( aByteStream.getArray( ) ), nWChars );
521*b1cdbd2cSJim Jagielski }
522*b1cdbd2cSJim Jagielski
523*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
524*b1cdbd2cSJim Jagielski //
525*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
526*b1cdbd2cSJim Jagielski
compareDataFlavors(const DataFlavor & lhs,const DataFlavor & rhs)527*b1cdbd2cSJim Jagielski sal_Bool SAL_CALL CDOTransferable::compareDataFlavors(
528*b1cdbd2cSJim Jagielski const DataFlavor& lhs, const DataFlavor& rhs )
529*b1cdbd2cSJim Jagielski {
530*b1cdbd2cSJim Jagielski if ( !m_rXMimeCntFactory.is( ) )
531*b1cdbd2cSJim Jagielski {
532*b1cdbd2cSJim Jagielski m_rXMimeCntFactory = Reference< XMimeContentTypeFactory >( m_SrvMgr->createInstance(
533*b1cdbd2cSJim Jagielski OUString::createFromAscii( "com.sun.star.datatransfer.MimeContentTypeFactory" ) ), UNO_QUERY );
534*b1cdbd2cSJim Jagielski }
535*b1cdbd2cSJim Jagielski OSL_ASSERT( m_rXMimeCntFactory.is( ) );
536*b1cdbd2cSJim Jagielski
537*b1cdbd2cSJim Jagielski sal_Bool bRet = sal_False;
538*b1cdbd2cSJim Jagielski
539*b1cdbd2cSJim Jagielski try
540*b1cdbd2cSJim Jagielski {
541*b1cdbd2cSJim Jagielski Reference< XMimeContentType > xLhs( m_rXMimeCntFactory->createMimeContentType( lhs.MimeType ) );
542*b1cdbd2cSJim Jagielski Reference< XMimeContentType > xRhs( m_rXMimeCntFactory->createMimeContentType( rhs.MimeType ) );
543*b1cdbd2cSJim Jagielski
544*b1cdbd2cSJim Jagielski if ( cmpFullMediaType( xLhs, xRhs ) )
545*b1cdbd2cSJim Jagielski {
546*b1cdbd2cSJim Jagielski bRet = cmpAllContentTypeParameter( xLhs, xRhs );
547*b1cdbd2cSJim Jagielski }
548*b1cdbd2cSJim Jagielski }
549*b1cdbd2cSJim Jagielski catch( IllegalArgumentException& )
550*b1cdbd2cSJim Jagielski {
551*b1cdbd2cSJim Jagielski OSL_ENSURE( sal_False, "Invalid content type detected" );
552*b1cdbd2cSJim Jagielski bRet = sal_False;
553*b1cdbd2cSJim Jagielski }
554*b1cdbd2cSJim Jagielski
555*b1cdbd2cSJim Jagielski return bRet;
556*b1cdbd2cSJim Jagielski }
557*b1cdbd2cSJim Jagielski
558*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
559*b1cdbd2cSJim Jagielski //
560*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
561*b1cdbd2cSJim Jagielski
cmpFullMediaType(const Reference<XMimeContentType> & xLhs,const Reference<XMimeContentType> & xRhs) const562*b1cdbd2cSJim Jagielski sal_Bool SAL_CALL CDOTransferable::cmpFullMediaType(
563*b1cdbd2cSJim Jagielski const Reference< XMimeContentType >& xLhs, const Reference< XMimeContentType >& xRhs ) const
564*b1cdbd2cSJim Jagielski {
565*b1cdbd2cSJim Jagielski return xLhs->getFullMediaType().equalsIgnoreAsciiCase( xRhs->getFullMediaType( ) );
566*b1cdbd2cSJim Jagielski }
567*b1cdbd2cSJim Jagielski
568*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
569*b1cdbd2cSJim Jagielski //
570*b1cdbd2cSJim Jagielski //------------------------------------------------------------------------
571*b1cdbd2cSJim Jagielski
cmpAllContentTypeParameter(const Reference<XMimeContentType> & xLhs,const Reference<XMimeContentType> & xRhs) const572*b1cdbd2cSJim Jagielski sal_Bool SAL_CALL CDOTransferable::cmpAllContentTypeParameter(
573*b1cdbd2cSJim Jagielski const Reference< XMimeContentType >& xLhs, const Reference< XMimeContentType >& xRhs ) const
574*b1cdbd2cSJim Jagielski {
575*b1cdbd2cSJim Jagielski Sequence< OUString > xLhsFlavors = xLhs->getParameters( );
576*b1cdbd2cSJim Jagielski Sequence< OUString > xRhsFlavors = xRhs->getParameters( );
577*b1cdbd2cSJim Jagielski sal_Bool bRet = sal_True;
578*b1cdbd2cSJim Jagielski
579*b1cdbd2cSJim Jagielski try
580*b1cdbd2cSJim Jagielski {
581*b1cdbd2cSJim Jagielski if ( xLhsFlavors.getLength( ) == xRhsFlavors.getLength( ) )
582*b1cdbd2cSJim Jagielski {
583*b1cdbd2cSJim Jagielski OUString pLhs;
584*b1cdbd2cSJim Jagielski OUString pRhs;
585*b1cdbd2cSJim Jagielski
586*b1cdbd2cSJim Jagielski for ( sal_Int32 i = 0; i < xLhsFlavors.getLength( ); i++ )
587*b1cdbd2cSJim Jagielski {
588*b1cdbd2cSJim Jagielski pLhs = xLhs->getParameterValue( xLhsFlavors[i] );
589*b1cdbd2cSJim Jagielski pRhs = xRhs->getParameterValue( xLhsFlavors[i] );
590*b1cdbd2cSJim Jagielski
591*b1cdbd2cSJim Jagielski if ( !pLhs.equalsIgnoreAsciiCase( pRhs ) )
592*b1cdbd2cSJim Jagielski {
593*b1cdbd2cSJim Jagielski bRet = sal_False;
594*b1cdbd2cSJim Jagielski break;
595*b1cdbd2cSJim Jagielski }
596*b1cdbd2cSJim Jagielski }
597*b1cdbd2cSJim Jagielski }
598*b1cdbd2cSJim Jagielski else
599*b1cdbd2cSJim Jagielski bRet = sal_False;
600*b1cdbd2cSJim Jagielski }
601*b1cdbd2cSJim Jagielski catch( NoSuchElementException& )
602*b1cdbd2cSJim Jagielski {
603*b1cdbd2cSJim Jagielski bRet = sal_False;
604*b1cdbd2cSJim Jagielski }
605*b1cdbd2cSJim Jagielski catch( IllegalArgumentException& )
606*b1cdbd2cSJim Jagielski {
607*b1cdbd2cSJim Jagielski bRet = sal_False;
608*b1cdbd2cSJim Jagielski }
609*b1cdbd2cSJim Jagielski
610*b1cdbd2cSJim Jagielski return bRet;
611*b1cdbd2cSJim Jagielski }
612*b1cdbd2cSJim Jagielski
getData(const Sequence<sal_Int8> & aProcessId)613*b1cdbd2cSJim Jagielski ::com::sun::star::uno::Any SAL_CALL CDOTransferable::getData( const Sequence< sal_Int8>& aProcessId )
614*b1cdbd2cSJim Jagielski throw (::com::sun::star::uno::RuntimeException)
615*b1cdbd2cSJim Jagielski {
616*b1cdbd2cSJim Jagielski Any retVal;
617*b1cdbd2cSJim Jagielski
618*b1cdbd2cSJim Jagielski sal_uInt8 * arProcCaller= (sal_uInt8*)(sal_Int8*) aProcessId.getConstArray();
619*b1cdbd2cSJim Jagielski sal_uInt8 arId[16];
620*b1cdbd2cSJim Jagielski rtl_getGlobalProcessId(arId);
621*b1cdbd2cSJim Jagielski if( ! memcmp( arId, arProcCaller,16))
622*b1cdbd2cSJim Jagielski {
623*b1cdbd2cSJim Jagielski if (m_rDataObject.is())
624*b1cdbd2cSJim Jagielski {
625*b1cdbd2cSJim Jagielski IDataObject* pObj= m_rDataObject.get();
626*b1cdbd2cSJim Jagielski pObj->AddRef();
627*b1cdbd2cSJim Jagielski retVal.setValue( &pObj, getCppuType((sal_uInt32*)0));
628*b1cdbd2cSJim Jagielski }
629*b1cdbd2cSJim Jagielski }
630*b1cdbd2cSJim Jagielski return retVal;
631*b1cdbd2cSJim Jagielski }
632*b1cdbd2cSJim Jagielski
633