148123e16SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
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
1048123e16SAndrew Rist  *
1148123e16SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1248123e16SAndrew Rist  *
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.
1948123e16SAndrew Rist  *
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 <osl/diagnose.h>
31cdf0e10cSrcweir #include "FetcList.hxx"
32cdf0e10cSrcweir #include "Fetc.hxx"
33cdf0e10cSrcweir #include <com/sun/star/datatransfer/XMimeContentTypeFactory.hpp>
34cdf0e10cSrcweir #include <com/sun/star/datatransfer/XMimeContentType.hpp>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #ifndef _DATAFORMATTRANSLATOR_HXX_
37cdf0e10cSrcweir #include "DataFmtTransl.hxx"
38cdf0e10cSrcweir #endif
39cdf0e10cSrcweir #include "..\misc\ImplHelper.hxx"
40cdf0e10cSrcweir #include "..\misc\WinClip.hxx"
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #include <algorithm>
43cdf0e10cSrcweir 
44cdf0e10cSrcweir #include "MimeAttrib.hxx"
45cdf0e10cSrcweir 
46cdf0e10cSrcweir //------------------------------------------------------------------------
47cdf0e10cSrcweir // namespace directives
48cdf0e10cSrcweir //------------------------------------------------------------------------
49cdf0e10cSrcweir 
50cdf0e10cSrcweir using namespace com::sun::star::uno;
51cdf0e10cSrcweir using namespace com::sun::star::datatransfer;
52cdf0e10cSrcweir using namespace com::sun::star::lang;
53cdf0e10cSrcweir using namespace com::sun::star::container;
54cdf0e10cSrcweir using namespace rtl;
55cdf0e10cSrcweir using namespace std;
56cdf0e10cSrcweir 
57cdf0e10cSrcweir //------------------------------------------------------------------------
58cdf0e10cSrcweir //
59cdf0e10cSrcweir //------------------------------------------------------------------------
60cdf0e10cSrcweir 
61cdf0e10cSrcweir LCID       CFormatRegistrar::m_TxtLocale   = 0;
62cdf0e10cSrcweir sal_uInt32 CFormatRegistrar::m_TxtCodePage = GetACP( );
63cdf0e10cSrcweir 
64cdf0e10cSrcweir //------------------------------------------------------------------------
65cdf0e10cSrcweir //
66cdf0e10cSrcweir //------------------------------------------------------------------------
67cdf0e10cSrcweir 
CFormatEtcContainer()68cdf0e10cSrcweir CFormatEtcContainer::CFormatEtcContainer( )
69cdf0e10cSrcweir {
70cdf0e10cSrcweir 	m_EnumIterator = m_FormatMap.begin( );
71cdf0e10cSrcweir }
72cdf0e10cSrcweir 
73cdf0e10cSrcweir //------------------------------------------------------------------------
74cdf0e10cSrcweir //
75cdf0e10cSrcweir //------------------------------------------------------------------------
76cdf0e10cSrcweir 
addFormatEtc(const CFormatEtc & fetc)77cdf0e10cSrcweir void CFormatEtcContainer::addFormatEtc( const CFormatEtc& fetc )
78cdf0e10cSrcweir {
79cdf0e10cSrcweir 	m_FormatMap.push_back( CFormatEtc( fetc ) );
80cdf0e10cSrcweir }
81cdf0e10cSrcweir 
82cdf0e10cSrcweir //------------------------------------------------------------------------
83cdf0e10cSrcweir //
84cdf0e10cSrcweir //------------------------------------------------------------------------
85cdf0e10cSrcweir 
removeFormatEtc(const CFormatEtc & fetc)86cdf0e10cSrcweir void SAL_CALL CFormatEtcContainer::removeFormatEtc( const CFormatEtc& fetc )
87cdf0e10cSrcweir {
88cdf0e10cSrcweir 	FormatEtcMap_t::iterator iter =
89cdf0e10cSrcweir 		find( m_FormatMap.begin(), m_FormatMap.end(), fetc );
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 	if ( iter != m_FormatMap.end( ) )
92cdf0e10cSrcweir 		m_FormatMap.erase( iter );
93cdf0e10cSrcweir }
94cdf0e10cSrcweir 
95cdf0e10cSrcweir //------------------------------------------------------------------------
96cdf0e10cSrcweir //
97cdf0e10cSrcweir //------------------------------------------------------------------------
98cdf0e10cSrcweir 
removeAllFormatEtc()99cdf0e10cSrcweir void SAL_CALL CFormatEtcContainer::removeAllFormatEtc( )
100cdf0e10cSrcweir {
101cdf0e10cSrcweir 	m_FormatMap.clear( );
102cdf0e10cSrcweir }
103cdf0e10cSrcweir 
104cdf0e10cSrcweir //------------------------------------------------------------------------
105cdf0e10cSrcweir //
106cdf0e10cSrcweir //------------------------------------------------------------------------
107cdf0e10cSrcweir 
hasFormatEtc(const CFormatEtc & fetc) const108cdf0e10cSrcweir sal_Bool CFormatEtcContainer::hasFormatEtc( const CFormatEtc& fetc ) const
109cdf0e10cSrcweir {
110cdf0e10cSrcweir 	FormatEtcMap_t::const_iterator iter =
111cdf0e10cSrcweir 		find( m_FormatMap.begin(), m_FormatMap.end(), fetc );
112cdf0e10cSrcweir 
113cdf0e10cSrcweir 	return ( iter != m_FormatMap.end( ) );
114cdf0e10cSrcweir }
115cdf0e10cSrcweir 
116cdf0e10cSrcweir //------------------------------------------------------------------------
117cdf0e10cSrcweir //
118cdf0e10cSrcweir //------------------------------------------------------------------------
119cdf0e10cSrcweir 
hasElements() const120cdf0e10cSrcweir sal_Bool CFormatEtcContainer::hasElements( ) const
121cdf0e10cSrcweir {
122cdf0e10cSrcweir 	return ( m_FormatMap.size( ) > 0 );
123cdf0e10cSrcweir }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir //------------------------------------------------------------------------
126cdf0e10cSrcweir //
127cdf0e10cSrcweir //------------------------------------------------------------------------
128cdf0e10cSrcweir 
beginEnumFormatEtc()129cdf0e10cSrcweir void CFormatEtcContainer::beginEnumFormatEtc( )
130cdf0e10cSrcweir {
131cdf0e10cSrcweir 	m_EnumIterator = m_FormatMap.begin( );
132cdf0e10cSrcweir }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir //------------------------------------------------------------------------
135cdf0e10cSrcweir //
136cdf0e10cSrcweir //------------------------------------------------------------------------
137cdf0e10cSrcweir 
nextFormatEtc(LPFORMATETC lpFetc,sal_uInt32 aNum)138cdf0e10cSrcweir sal_uInt32 SAL_CALL CFormatEtcContainer::nextFormatEtc( LPFORMATETC lpFetc,
139cdf0e10cSrcweir 													    sal_uInt32 aNum )
140cdf0e10cSrcweir {
141cdf0e10cSrcweir 	OSL_ASSERT( lpFetc );
142cdf0e10cSrcweir 	OSL_ASSERT( !IsBadWritePtr( lpFetc, sizeof( FORMATETC ) * aNum ) );
143cdf0e10cSrcweir 
144cdf0e10cSrcweir 	sal_uInt32 nFetched = 0;
145cdf0e10cSrcweir 
146*69da7f00SHerbert Dürr 	for ( sal_uInt32 i = 0; i < aNum; i++, nFetched++, lpFetc++, ++m_EnumIterator )
147cdf0e10cSrcweir 	{
148*69da7f00SHerbert Dürr 		if ( m_EnumIterator == m_FormatMap.end() )
149*69da7f00SHerbert Dürr 			break;
150*69da7f00SHerbert Dürr 		CopyFormatEtc( lpFetc, *m_EnumIterator );
151cdf0e10cSrcweir 	}
152cdf0e10cSrcweir 
153cdf0e10cSrcweir 	return nFetched;
154cdf0e10cSrcweir }
155cdf0e10cSrcweir 
156cdf0e10cSrcweir 
157cdf0e10cSrcweir //------------------------------------------------------------------------
158cdf0e10cSrcweir //
159cdf0e10cSrcweir //------------------------------------------------------------------------
160cdf0e10cSrcweir 
skipFormatEtc(sal_uInt32 aNum)161cdf0e10cSrcweir sal_Bool SAL_CALL CFormatEtcContainer::skipFormatEtc( sal_uInt32 aNum )
162cdf0e10cSrcweir {
163cdf0e10cSrcweir 	FormatEtcMap_t::const_iterator iter_end = m_FormatMap.end( );
164cdf0e10cSrcweir 	for ( sal_uInt32 i = 0;
165cdf0e10cSrcweir 		  (i < aNum) && (m_EnumIterator != iter_end);
166cdf0e10cSrcweir 		  i++, ++m_EnumIterator )
167cdf0e10cSrcweir 		;/* intentionally left empty */
168cdf0e10cSrcweir 
169cdf0e10cSrcweir 	return ( m_EnumIterator != m_FormatMap.end( ) );
170cdf0e10cSrcweir }
171cdf0e10cSrcweir 
172cdf0e10cSrcweir 
173cdf0e10cSrcweir //#########################################################################
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 
176cdf0e10cSrcweir //------------------------------------------------------------------------
177cdf0e10cSrcweir //
178cdf0e10cSrcweir //------------------------------------------------------------------------
179cdf0e10cSrcweir 
CFormatRegistrar(const Reference<XMultiServiceFactory> & ServiceManager,const CDataFormatTranslator & aDataFormatTranslator)180cdf0e10cSrcweir CFormatRegistrar::CFormatRegistrar( const Reference< XMultiServiceFactory >& ServiceManager,
181cdf0e10cSrcweir 								    const CDataFormatTranslator& aDataFormatTranslator ) :
182cdf0e10cSrcweir 	m_DataFormatTranslator( aDataFormatTranslator ),
183cdf0e10cSrcweir 	m_bHasSynthesizedLocale( sal_False ),
184cdf0e10cSrcweir 	m_SrvMgr( ServiceManager )
185cdf0e10cSrcweir {
186cdf0e10cSrcweir }
187cdf0e10cSrcweir 
188cdf0e10cSrcweir // ----------------------------------------------------------------------------------------
189cdf0e10cSrcweir // this function converts all DataFlavors of the given FlavorList into
190cdf0e10cSrcweir // an appropriate FORMATETC structure, for some formats like unicodetext,
191cdf0e10cSrcweir // text and text/html we will offer an accompany format e.g.:
192cdf0e10cSrcweir //
193cdf0e10cSrcweir // DataFlavor				| Registered Clipformat		|	Registered accompany clipformat
194cdf0e10cSrcweir // -------------------------|---------------------------|-----------------------------------
195cdf0e10cSrcweir // text/plain;charset=ansi	| CF_TEXT					|	CF_UNICODETEXT
196cdf0e10cSrcweir //							|							|	CF_LOCALE (if charset != GetACP()
197cdf0e10cSrcweir //							|							|
198cdf0e10cSrcweir // text/plain;charset=oem	| CF_OEMTEXT				|	CF_UNICODETEXT
199cdf0e10cSrcweir //							|							|	CF_LOCALE (if charset != GetOEMCP()
200cdf0e10cSrcweir //							|							|
201cdf0e10cSrcweir // text/plain;charset=utf-16| CF_UNICODETEXT			|	CF_TEXT
202cdf0e10cSrcweir //							|							|
203cdf0e10cSrcweir // text/html				| HTML (Hypertext ...)		|	HTML Format
204cdf0e10cSrcweir //							|							|
205cdf0e10cSrcweir //
206cdf0e10cSrcweir // if some tries to register different text formats with different charsets the last
207cdf0e10cSrcweir // registered wins and the others are ignored
208cdf0e10cSrcweir // ----------------------------------------------------------------------------------------
209cdf0e10cSrcweir 
RegisterFormats(const Reference<XTransferable> & aXTransferable,CFormatEtcContainer & aFormatEtcContainer)210cdf0e10cSrcweir void SAL_CALL CFormatRegistrar::RegisterFormats(
211cdf0e10cSrcweir 	const Reference< XTransferable >& aXTransferable, CFormatEtcContainer& aFormatEtcContainer )
212cdf0e10cSrcweir {
213cdf0e10cSrcweir 	Sequence< DataFlavor > aFlavorList = aXTransferable->getTransferDataFlavors( );
214cdf0e10cSrcweir 	sal_Int32  nFlavors                = aFlavorList.getLength( );
215cdf0e10cSrcweir 	sal_Bool   bUnicodeRegistered      = sal_False;
216cdf0e10cSrcweir 	DataFlavor aFlavor;
217cdf0e10cSrcweir 
218cdf0e10cSrcweir 	for( sal_Int32 i = 0; i < nFlavors; i++ )
219cdf0e10cSrcweir 	{
220cdf0e10cSrcweir 		aFlavor = aFlavorList[i];
221cdf0e10cSrcweir 		CFormatEtc fetc = m_DataFormatTranslator.getFormatEtcFromDataFlavor( aFlavor );
222cdf0e10cSrcweir 
223cdf0e10cSrcweir 		// maybe an internal format so we ignore it
224cdf0e10cSrcweir 		if ( CF_INVALID == fetc.getClipformat( ) )
225cdf0e10cSrcweir 			continue;
226cdf0e10cSrcweir 
227cdf0e10cSrcweir 		if ( !needsToSynthesizeAccompanyFormats( fetc ) )
228cdf0e10cSrcweir 			aFormatEtcContainer.addFormatEtc( fetc );
229cdf0e10cSrcweir 		else
230cdf0e10cSrcweir 		{
231cdf0e10cSrcweir 			// if we haven't registered any text format up to now
232cdf0e10cSrcweir 			if ( m_DataFormatTranslator.isTextFormat( fetc.getClipformat() ) && !bUnicodeRegistered )
233cdf0e10cSrcweir 			{
234cdf0e10cSrcweir 				// if the transferable supports unicode text we ignore
235cdf0e10cSrcweir 				// any further text format the transferable offers
236cdf0e10cSrcweir 				// because we can create it from Unicode text in addition
237cdf0e10cSrcweir 				// we register CF_TEXT for non unicode clients
238cdf0e10cSrcweir 				if ( m_DataFormatTranslator.isUnicodeTextFormat( fetc.getClipformat() ) )
239cdf0e10cSrcweir 				{
240cdf0e10cSrcweir 					aFormatEtcContainer.addFormatEtc( fetc ); // add CF_UNICODE
241cdf0e10cSrcweir 					aFormatEtcContainer.addFormatEtc(
242cdf0e10cSrcweir 						m_DataFormatTranslator.getFormatEtcForClipformat( CF_TEXT ) ); // add CF_TEXT
243cdf0e10cSrcweir 					bUnicodeRegistered = sal_True;
244cdf0e10cSrcweir 				}
245cdf0e10cSrcweir 				else if ( !hasUnicodeFlavor( aXTransferable ) )
246cdf0e10cSrcweir 				{
247cdf0e10cSrcweir 					// we try to investigate the charset and make a valid
248cdf0e10cSrcweir 					// windows codepage from this charset the default
249cdf0e10cSrcweir 					// return value is the result of GetACP( )
250cdf0e10cSrcweir 					OUString charset = getCharsetFromDataFlavor( aFlavor );
251cdf0e10cSrcweir 					sal_uInt32 txtCP = getWinCPFromMimeCharset( charset );
252cdf0e10cSrcweir 
253cdf0e10cSrcweir 					// we try to get a Locale appropriate for this codepage
254cdf0e10cSrcweir 					if ( findLocaleForTextCodePage( ) )
255cdf0e10cSrcweir 					{
256cdf0e10cSrcweir 						m_TxtCodePage = txtCP;
257cdf0e10cSrcweir 
258cdf0e10cSrcweir 						aFormatEtcContainer.addFormatEtc(
259cdf0e10cSrcweir 							m_DataFormatTranslator.getFormatEtcForClipformat( CF_UNICODETEXT ) );
260cdf0e10cSrcweir 
261cdf0e10cSrcweir 						if ( !IsOEMCP( m_TxtCodePage ) )
262cdf0e10cSrcweir 							aFormatEtcContainer.addFormatEtc(
263cdf0e10cSrcweir 								m_DataFormatTranslator.getFormatEtcForClipformat( CF_TEXT ) );
264cdf0e10cSrcweir 						else
265cdf0e10cSrcweir 							aFormatEtcContainer.addFormatEtc(
266cdf0e10cSrcweir 								m_DataFormatTranslator.getFormatEtcForClipformat( CF_OEMTEXT ) );
267cdf0e10cSrcweir 
268cdf0e10cSrcweir 						aFormatEtcContainer.addFormatEtc(
269cdf0e10cSrcweir 							m_DataFormatTranslator.getFormatEtcForClipformat( CF_LOCALE ) );
270cdf0e10cSrcweir 
271cdf0e10cSrcweir 						// we save the flavor so it's easier when
272cdf0e10cSrcweir 						// queried for it in XTDataObject::GetData(...)
273cdf0e10cSrcweir 						m_RegisteredTextFlavor  = aFlavor;
274cdf0e10cSrcweir 						m_bHasSynthesizedLocale = sal_True;
275cdf0e10cSrcweir 					}
276cdf0e10cSrcweir 				}
277cdf0e10cSrcweir 			}
278cdf0e10cSrcweir 			else if ( m_DataFormatTranslator.isTextHtmlFormat( fetc.getClipformat( ) ) ) // Html (Hyper Text...)
279cdf0e10cSrcweir 			{
280cdf0e10cSrcweir 				// we add text/html ( HTML (HyperText Markup Language) )
281cdf0e10cSrcweir 				aFormatEtcContainer.addFormatEtc( fetc );
282cdf0e10cSrcweir 
283cdf0e10cSrcweir 				// and HTML Format
284cdf0e10cSrcweir 				OUString htmlFormat( OUString::createFromAscii( "HTML Format" ) );
285cdf0e10cSrcweir 				aFormatEtcContainer.addFormatEtc(
286cdf0e10cSrcweir 					m_DataFormatTranslator.getFormatEtcForClipformatName( htmlFormat ) );
287cdf0e10cSrcweir 			}
288cdf0e10cSrcweir 		}
289cdf0e10cSrcweir 	}
290cdf0e10cSrcweir }
291cdf0e10cSrcweir 
292cdf0e10cSrcweir //------------------------------------------------------------------------
293cdf0e10cSrcweir //
294cdf0e10cSrcweir //------------------------------------------------------------------------
295cdf0e10cSrcweir 
hasSynthesizedLocale() const296cdf0e10cSrcweir sal_Bool SAL_CALL CFormatRegistrar::hasSynthesizedLocale( ) const
297cdf0e10cSrcweir {
298cdf0e10cSrcweir 	return m_bHasSynthesizedLocale;
299cdf0e10cSrcweir }
300cdf0e10cSrcweir 
301cdf0e10cSrcweir //------------------------------------------------------------------------
302cdf0e10cSrcweir //
303cdf0e10cSrcweir //------------------------------------------------------------------------
304cdf0e10cSrcweir 
getSynthesizedLocale() const305cdf0e10cSrcweir LCID SAL_CALL CFormatRegistrar::getSynthesizedLocale( ) const
306cdf0e10cSrcweir {
307cdf0e10cSrcweir 	return m_TxtLocale;
308cdf0e10cSrcweir }
309cdf0e10cSrcweir 
310cdf0e10cSrcweir //------------------------------------------------------------------------
311cdf0e10cSrcweir //
312cdf0e10cSrcweir //------------------------------------------------------------------------
313cdf0e10cSrcweir 
getRegisteredTextCodePage() const314cdf0e10cSrcweir sal_uInt32 SAL_CALL CFormatRegistrar::getRegisteredTextCodePage( ) const
315cdf0e10cSrcweir {
316cdf0e10cSrcweir 	return m_TxtCodePage;
317cdf0e10cSrcweir }
318cdf0e10cSrcweir 
319cdf0e10cSrcweir //------------------------------------------------------------------------
320cdf0e10cSrcweir //
321cdf0e10cSrcweir //------------------------------------------------------------------------
322cdf0e10cSrcweir 
getRegisteredTextFlavor() const323cdf0e10cSrcweir DataFlavor SAL_CALL CFormatRegistrar::getRegisteredTextFlavor( ) const
324cdf0e10cSrcweir {
325cdf0e10cSrcweir 	return m_RegisteredTextFlavor;
326cdf0e10cSrcweir }
327cdf0e10cSrcweir 
328cdf0e10cSrcweir //------------------------------------------------------------------------
329cdf0e10cSrcweir //
330cdf0e10cSrcweir //------------------------------------------------------------------------
331cdf0e10cSrcweir 
isSynthesizeableFormat(const CFormatEtc & aFormatEtc) const332cdf0e10cSrcweir sal_Bool SAL_CALL CFormatRegistrar::isSynthesizeableFormat( const CFormatEtc& aFormatEtc ) const
333cdf0e10cSrcweir {
334cdf0e10cSrcweir 	return ( m_DataFormatTranslator.isOemOrAnsiTextFormat( aFormatEtc.getClipformat() ) ||
335cdf0e10cSrcweir 		     m_DataFormatTranslator.isUnicodeTextFormat( aFormatEtc.getClipformat() ) ||
336cdf0e10cSrcweir 			 m_DataFormatTranslator.isHTMLFormat( aFormatEtc.getClipformat() ) );
337cdf0e10cSrcweir }
338cdf0e10cSrcweir 
339cdf0e10cSrcweir //------------------------------------------------------------------------
340cdf0e10cSrcweir //
341cdf0e10cSrcweir //------------------------------------------------------------------------
342cdf0e10cSrcweir 
343cdf0e10cSrcweir inline
needsToSynthesizeAccompanyFormats(const CFormatEtc & aFormatEtc) const344cdf0e10cSrcweir sal_Bool SAL_CALL CFormatRegistrar::needsToSynthesizeAccompanyFormats( const CFormatEtc& aFormatEtc ) const
345cdf0e10cSrcweir {
346cdf0e10cSrcweir 	return ( m_DataFormatTranslator.isOemOrAnsiTextFormat( aFormatEtc.getClipformat() ) ||
347cdf0e10cSrcweir 		     m_DataFormatTranslator.isUnicodeTextFormat( aFormatEtc.getClipformat() ) ||
348cdf0e10cSrcweir 			 m_DataFormatTranslator.isTextHtmlFormat( aFormatEtc.getClipformat( ) ) );
349cdf0e10cSrcweir }
350cdf0e10cSrcweir 
351cdf0e10cSrcweir //------------------------------------------------------------------------
352cdf0e10cSrcweir //
353cdf0e10cSrcweir //------------------------------------------------------------------------
354cdf0e10cSrcweir 
getCharsetFromDataFlavor(const DataFlavor & aFlavor)355cdf0e10cSrcweir OUString SAL_CALL CFormatRegistrar::getCharsetFromDataFlavor( const DataFlavor& aFlavor )
356cdf0e10cSrcweir {
357cdf0e10cSrcweir 	OUString charset;
358cdf0e10cSrcweir 
359cdf0e10cSrcweir 	try
360cdf0e10cSrcweir 	{
361cdf0e10cSrcweir 		Reference< XMimeContentTypeFactory > xMimeFac(
362cdf0e10cSrcweir 			m_SrvMgr->createInstance( OUString::createFromAscii( \
363cdf0e10cSrcweir 				"com.sun.star.datatransfer.MimeContentTypeFactory" ) ), UNO_QUERY );
364cdf0e10cSrcweir 
365cdf0e10cSrcweir 		if( xMimeFac.is( ) )
366cdf0e10cSrcweir 		{
367cdf0e10cSrcweir 			Reference< XMimeContentType > xMimeType( xMimeFac->createMimeContentType( aFlavor.MimeType ) );
368cdf0e10cSrcweir 			if ( xMimeType->hasParameter( TEXTPLAIN_PARAM_CHARSET ) )
369cdf0e10cSrcweir 				charset = xMimeType->getParameterValue( TEXTPLAIN_PARAM_CHARSET );
370cdf0e10cSrcweir 			else
371cdf0e10cSrcweir 				charset = getMimeCharsetFromWinCP( GetACP( ), PRE_WINDOWS_CODEPAGE );
372cdf0e10cSrcweir 		}
373cdf0e10cSrcweir 	}
374cdf0e10cSrcweir 	catch(NoSuchElementException&)
375cdf0e10cSrcweir 	{
376cdf0e10cSrcweir 		OSL_ENSURE( sal_False, "Unexpected" );
377cdf0e10cSrcweir 	}
378cdf0e10cSrcweir 	catch(...)
379cdf0e10cSrcweir 	{
380cdf0e10cSrcweir 		OSL_ENSURE( sal_False, "Invalid data flavor" );
381cdf0e10cSrcweir 	}
382cdf0e10cSrcweir 
383cdf0e10cSrcweir 	return charset;
384cdf0e10cSrcweir }
385cdf0e10cSrcweir 
386cdf0e10cSrcweir //------------------------------------------------------------------------
387cdf0e10cSrcweir //
388cdf0e10cSrcweir //------------------------------------------------------------------------
389cdf0e10cSrcweir 
hasUnicodeFlavor(const Reference<XTransferable> & aXTransferable) const390cdf0e10cSrcweir sal_Bool SAL_CALL CFormatRegistrar::hasUnicodeFlavor( const Reference< XTransferable >& aXTransferable ) const
391cdf0e10cSrcweir {
392cdf0e10cSrcweir 	CFormatEtc fetc( CF_UNICODETEXT );
393cdf0e10cSrcweir 
394cdf0e10cSrcweir 	DataFlavor aFlavor =
395cdf0e10cSrcweir 		m_DataFormatTranslator.getDataFlavorFromFormatEtc( fetc );
396cdf0e10cSrcweir 
397cdf0e10cSrcweir 	return aXTransferable->isDataFlavorSupported( aFlavor );
398cdf0e10cSrcweir }
399cdf0e10cSrcweir 
400cdf0e10cSrcweir //------------------------------------------------------------------------
401cdf0e10cSrcweir //
402cdf0e10cSrcweir //------------------------------------------------------------------------
403cdf0e10cSrcweir 
404cdf0e10cSrcweir inline
isEqualCurrentSystemCodePage(sal_uInt32 aCodePage) const405cdf0e10cSrcweir sal_Bool CFormatRegistrar::isEqualCurrentSystemCodePage( sal_uInt32 aCodePage ) const
406cdf0e10cSrcweir {
407cdf0e10cSrcweir 	return ( (aCodePage == GetOEMCP()) || (aCodePage == GetACP()) );
408cdf0e10cSrcweir }
409cdf0e10cSrcweir 
410cdf0e10cSrcweir //------------------------------------------------------------------------
411cdf0e10cSrcweir //
412cdf0e10cSrcweir //------------------------------------------------------------------------
413cdf0e10cSrcweir 
findLocaleForTextCodePage()414cdf0e10cSrcweir sal_Bool SAL_CALL CFormatRegistrar::findLocaleForTextCodePage( )
415cdf0e10cSrcweir {
416cdf0e10cSrcweir 	m_TxtLocale = 0;
417cdf0e10cSrcweir 	EnumSystemLocalesA( CFormatRegistrar::EnumLocalesProc, LCID_INSTALLED );
418cdf0e10cSrcweir 	return ( IsValidLocale( m_TxtLocale, LCID_INSTALLED ) ) ? sal_True : sal_False;
419cdf0e10cSrcweir }
420cdf0e10cSrcweir 
421cdf0e10cSrcweir //------------------------------------------------------------------------
422cdf0e10cSrcweir //
423cdf0e10cSrcweir //------------------------------------------------------------------------
424cdf0e10cSrcweir 
isLocaleCodePage(LCID lcid,LCTYPE lctype,sal_uInt32 codepage)425cdf0e10cSrcweir sal_Bool SAL_CALL CFormatRegistrar::isLocaleCodePage( LCID lcid, LCTYPE lctype, sal_uInt32 codepage )
426cdf0e10cSrcweir {
427cdf0e10cSrcweir 	char  buff[6];
428cdf0e10cSrcweir 	sal_uInt32 localeCodePage;
429cdf0e10cSrcweir 
430cdf0e10cSrcweir 	OSL_ASSERT( IsValidLocale( lcid, LCID_INSTALLED ) );
431cdf0e10cSrcweir 
432cdf0e10cSrcweir 	// get the ansi codepage of the current locale
433cdf0e10cSrcweir 	GetLocaleInfoA( lcid, lctype, buff, sizeof( buff ) );
434cdf0e10cSrcweir 	localeCodePage = atol( buff );
435cdf0e10cSrcweir 
436cdf0e10cSrcweir 	return ( localeCodePage == codepage );
437cdf0e10cSrcweir }
438cdf0e10cSrcweir 
439cdf0e10cSrcweir //------------------------------------------------------------------------
440cdf0e10cSrcweir //
441cdf0e10cSrcweir //------------------------------------------------------------------------
442cdf0e10cSrcweir 
443cdf0e10cSrcweir inline
isLocaleOemCodePage(LCID lcid,sal_uInt32 codepage)444cdf0e10cSrcweir sal_Bool SAL_CALL CFormatRegistrar::isLocaleOemCodePage( LCID lcid, sal_uInt32 codepage )
445cdf0e10cSrcweir {
446cdf0e10cSrcweir 	return isLocaleCodePage( lcid, LOCALE_IDEFAULTCODEPAGE, codepage );
447cdf0e10cSrcweir }
448cdf0e10cSrcweir 
449cdf0e10cSrcweir //------------------------------------------------------------------------
450cdf0e10cSrcweir //
451cdf0e10cSrcweir //------------------------------------------------------------------------
452cdf0e10cSrcweir 
453cdf0e10cSrcweir inline
isLocaleAnsiCodePage(LCID lcid,sal_uInt32 codepage)454cdf0e10cSrcweir sal_Bool SAL_CALL CFormatRegistrar::isLocaleAnsiCodePage( LCID lcid, sal_uInt32 codepage )
455cdf0e10cSrcweir {
456cdf0e10cSrcweir 	return isLocaleCodePage( lcid, LOCALE_IDEFAULTANSICODEPAGE, codepage );
457cdf0e10cSrcweir }
458cdf0e10cSrcweir 
459cdf0e10cSrcweir //------------------------------------------------------------------------
460cdf0e10cSrcweir //
461cdf0e10cSrcweir //------------------------------------------------------------------------
462cdf0e10cSrcweir 
EnumLocalesProc(LPSTR lpLocaleStr)463cdf0e10cSrcweir BOOL CALLBACK CFormatRegistrar::EnumLocalesProc( LPSTR lpLocaleStr )
464cdf0e10cSrcweir {
465cdf0e10cSrcweir 	// the lpLocaleStr parametere is hexadecimal
466cdf0e10cSrcweir 	LCID lcid = strtol( lpLocaleStr, NULL, 16 );
467cdf0e10cSrcweir 
468cdf0e10cSrcweir 	if ( isLocaleAnsiCodePage( lcid, CFormatRegistrar::m_TxtCodePage ) ||
469cdf0e10cSrcweir 		 isLocaleOemCodePage( lcid, CFormatRegistrar::m_TxtCodePage ) )
470cdf0e10cSrcweir 	{
471cdf0e10cSrcweir 		CFormatRegistrar::m_TxtLocale = lcid;
472cdf0e10cSrcweir 		return sal_False; // stop enumerating
473cdf0e10cSrcweir 	}
474cdf0e10cSrcweir 
475cdf0e10cSrcweir 	return sal_True;
476cdf0e10cSrcweir }
477cdf0e10cSrcweir 
478