xref: /trunk/main/sfx2/source/doc/docfilt.cxx (revision cdf0e10c)
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_sfx2.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir // INCLUDE ---------------------------------------------------------------
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #ifdef SOLARIS
34*cdf0e10cSrcweir // HACK: prevent conflict between STLPORT and Workshop headers on Solaris 8
35*cdf0e10cSrcweir #include <ctime>
36*cdf0e10cSrcweir #endif
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #include <string> // HACK: prevent conflict between STLPORT and Workshop headers
39*cdf0e10cSrcweir #include <sot/exchange.hxx>
40*cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
41*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir #include <sfx2/docfac.hxx>
45*cdf0e10cSrcweir #include <sfx2/docfilt.hxx>
46*cdf0e10cSrcweir #include "fltfnc.hxx"
47*cdf0e10cSrcweir #include <sfx2/sfxuno.hxx>
48*cdf0e10cSrcweir #include <sfx2/objsh.hxx>
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir using namespace ::com::sun::star;
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir // STATIC DATA -----------------------------------------------------------
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir DBG_NAME(SfxFilter)
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir SfxFilter::SfxFilter(  const String &rName,
57*cdf0e10cSrcweir 					   const String &rWildCard,
58*cdf0e10cSrcweir 					   SfxFilterFlags nType,
59*cdf0e10cSrcweir 					   sal_uInt32 lFmt,
60*cdf0e10cSrcweir 					   const String &rTypNm,
61*cdf0e10cSrcweir 					   sal_uInt16 nIcon,
62*cdf0e10cSrcweir 					   const String &rMimeType,
63*cdf0e10cSrcweir 					   const String &rUsrDat,
64*cdf0e10cSrcweir 					   const String &rServiceName ):
65*cdf0e10cSrcweir 	aWildCard(rWildCard, ';'),
66*cdf0e10cSrcweir 	lFormat(lFmt),
67*cdf0e10cSrcweir 	aTypeName(rTypNm),
68*cdf0e10cSrcweir 	aUserData(rUsrDat),
69*cdf0e10cSrcweir 	nFormatType(nType),
70*cdf0e10cSrcweir 	nDocIcon(nIcon),
71*cdf0e10cSrcweir 	aServiceName( rServiceName ),
72*cdf0e10cSrcweir 	aMimeType( rMimeType ),
73*cdf0e10cSrcweir 	aFilterName( rName )
74*cdf0e10cSrcweir {
75*cdf0e10cSrcweir 	String aExts = GetWildcard()();
76*cdf0e10cSrcweir 	String aShort, aLong;
77*cdf0e10cSrcweir 	String aRet;
78*cdf0e10cSrcweir     sal_uInt16 nMaxLength = USHRT_MAX;
79*cdf0e10cSrcweir 	String aTest;
80*cdf0e10cSrcweir 	sal_uInt16 nPos = 0;
81*cdf0e10cSrcweir 	while( ( aRet = aExts.GetToken( nPos++, ';' ) ).Len() )
82*cdf0e10cSrcweir 	{
83*cdf0e10cSrcweir 		aTest = aRet;
84*cdf0e10cSrcweir 		aTest.SearchAndReplace( DEFINE_CONST_UNICODE( "*." ), String() );
85*cdf0e10cSrcweir 		if( aTest.Len() <= nMaxLength )
86*cdf0e10cSrcweir 		{
87*cdf0e10cSrcweir 			if( aShort.Len() ) aShort += ';';
88*cdf0e10cSrcweir 			aShort += aRet;
89*cdf0e10cSrcweir 		}
90*cdf0e10cSrcweir 		else
91*cdf0e10cSrcweir 		{
92*cdf0e10cSrcweir 			if( aLong.Len() ) aLong += ';';
93*cdf0e10cSrcweir 			aLong += aRet;
94*cdf0e10cSrcweir 		}
95*cdf0e10cSrcweir 	}
96*cdf0e10cSrcweir 	if( aShort.Len() && aLong.Len() )
97*cdf0e10cSrcweir 	{
98*cdf0e10cSrcweir 		aShort += ';';
99*cdf0e10cSrcweir 		aShort += aLong;
100*cdf0e10cSrcweir 	}
101*cdf0e10cSrcweir 	aWildCard = aShort;
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir     nVersion = SOFFICE_FILEFORMAT_50;
104*cdf0e10cSrcweir 	aUIName = aFilterName;
105*cdf0e10cSrcweir }
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir SfxFilter::~SfxFilter()
108*cdf0e10cSrcweir {
109*cdf0e10cSrcweir }
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir String SfxFilter::GetDefaultExtension() const
112*cdf0e10cSrcweir {
113*cdf0e10cSrcweir 	return GetWildcard()().GetToken( 0, ';' );
114*cdf0e10cSrcweir }
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir String SfxFilter::GetSuffixes() const
117*cdf0e10cSrcweir {
118*cdf0e10cSrcweir 	String aRet = GetWildcard()();
119*cdf0e10cSrcweir 	while( aRet.SearchAndReplaceAscii( "*.", String() ) != STRING_NOTFOUND ) ;
120*cdf0e10cSrcweir 	while( aRet.SearchAndReplace( ';', ',' ) != STRING_NOTFOUND ) ;
121*cdf0e10cSrcweir 	return aRet;
122*cdf0e10cSrcweir }
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir const SfxFilter* SfxFilter::GetDefaultFilter( const String& rName )
125*cdf0e10cSrcweir {
126*cdf0e10cSrcweir 	return SfxFilterContainer::GetDefaultFilter_Impl( rName );
127*cdf0e10cSrcweir }
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir const SfxFilter* SfxFilter::GetDefaultFilterFromFactory( const String& rFact )
130*cdf0e10cSrcweir {
131*cdf0e10cSrcweir 	return GetDefaultFilter( SfxObjectShell::GetServiceNameFromFactory( rFact ) );
132*cdf0e10cSrcweir }
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir const SfxFilter* SfxFilter::GetFilterByName( const String& rName )
135*cdf0e10cSrcweir {
136*cdf0e10cSrcweir 	SfxFilterMatcher aMatch;
137*cdf0e10cSrcweir 	return aMatch.GetFilter4FilterName( rName, 0, 0 );
138*cdf0e10cSrcweir }
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir String SfxFilter::GetTypeFromStorage( const SotStorage& rStg )
141*cdf0e10cSrcweir {
142*cdf0e10cSrcweir 	const char* pType=0;
143*cdf0e10cSrcweir 	if ( rStg.IsStream( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "WordDocument" ) ) ) )
144*cdf0e10cSrcweir 	{
145*cdf0e10cSrcweir 		if ( rStg.IsStream( String::CreateFromAscii("0Table" ) ) || rStg.IsStream( String::CreateFromAscii("1Table" ) ) )
146*cdf0e10cSrcweir 			pType = "writer_MS_Word_97";
147*cdf0e10cSrcweir 		else
148*cdf0e10cSrcweir 		    pType = "writer_MS_Word_95";
149*cdf0e10cSrcweir 	}
150*cdf0e10cSrcweir 	else if ( rStg.IsStream( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "Book" ) ) ) )
151*cdf0e10cSrcweir 	{
152*cdf0e10cSrcweir 		pType = "calc_MS_Excel_95";
153*cdf0e10cSrcweir 	}
154*cdf0e10cSrcweir 	else if ( rStg.IsStream( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "Workbook" ) ) ) )
155*cdf0e10cSrcweir 	{
156*cdf0e10cSrcweir 		pType = "calc_MS_Excel_97";
157*cdf0e10cSrcweir 	}
158*cdf0e10cSrcweir 	else if ( rStg.IsStream( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "PowerPoint Document" ) ) ) )
159*cdf0e10cSrcweir 	{
160*cdf0e10cSrcweir 		pType = "impress_MS_PowerPoint_97";
161*cdf0e10cSrcweir 	}
162*cdf0e10cSrcweir 	else if ( rStg.IsStream( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "Equation Native" ) ) ) )
163*cdf0e10cSrcweir 	{
164*cdf0e10cSrcweir 		pType = "math_MathType_3x";
165*cdf0e10cSrcweir 	}
166*cdf0e10cSrcweir 	else
167*cdf0e10cSrcweir 	{
168*cdf0e10cSrcweir 		sal_Int32 nClipId = ((SotStorage&)rStg).GetFormat();
169*cdf0e10cSrcweir 		if ( nClipId )
170*cdf0e10cSrcweir 		{
171*cdf0e10cSrcweir             const SfxFilter* pFilter = SfxFilterMatcher().GetFilter4ClipBoardId( nClipId );
172*cdf0e10cSrcweir 			if ( pFilter )
173*cdf0e10cSrcweir 				return pFilter->GetTypeName();
174*cdf0e10cSrcweir 		}
175*cdf0e10cSrcweir 	}
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir 	return pType ? String::CreateFromAscii(pType) : String();
178*cdf0e10cSrcweir }
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir String SfxFilter::GetTypeFromStorage( const com::sun::star::uno::Reference< com::sun::star::embed::XStorage >& xStorage, sal_Bool bTemplate,
181*cdf0e10cSrcweir 										String* pFilterName )
182*cdf0e10cSrcweir 		throw ( beans::UnknownPropertyException,
183*cdf0e10cSrcweir 				lang::WrappedTargetException,
184*cdf0e10cSrcweir 				uno::RuntimeException )
185*cdf0e10cSrcweir {
186*cdf0e10cSrcweir     SfxFilterMatcher aMatcher;
187*cdf0e10cSrcweir 	const char* pType=0;
188*cdf0e10cSrcweir     String aName;
189*cdf0e10cSrcweir     if ( pFilterName )
190*cdf0e10cSrcweir     {
191*cdf0e10cSrcweir         aName = *pFilterName;
192*cdf0e10cSrcweir         pFilterName->Erase();
193*cdf0e10cSrcweir     }
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir 	com::sun::star::uno::Reference< com::sun::star::beans::XPropertySet > xProps( xStorage, com::sun::star::uno::UNO_QUERY );
196*cdf0e10cSrcweir     if ( xProps.is() )
197*cdf0e10cSrcweir     {
198*cdf0e10cSrcweir         ::rtl::OUString aMediaType;
199*cdf0e10cSrcweir         xProps->getPropertyValue( ::rtl::OUString::createFromAscii( "MediaType" ) ) >>= aMediaType;
200*cdf0e10cSrcweir         if ( aMediaType.getLength() )
201*cdf0e10cSrcweir         {
202*cdf0e10cSrcweir             ::com::sun::star::datatransfer::DataFlavor aDataFlavor;
203*cdf0e10cSrcweir             aDataFlavor.MimeType = aMediaType;
204*cdf0e10cSrcweir             sal_uInt32 nClipId = SotExchange::GetFormat( aDataFlavor );
205*cdf0e10cSrcweir             if ( nClipId )
206*cdf0e10cSrcweir             {
207*cdf0e10cSrcweir                 SfxFilterFlags nMust = SFX_FILTER_IMPORT, nDont = SFX_FILTER_NOTINSTALLED;
208*cdf0e10cSrcweir                 if ( bTemplate )
209*cdf0e10cSrcweir                     // template filter was preselected, try to verify
210*cdf0e10cSrcweir                     nMust |= SFX_FILTER_TEMPLATEPATH;
211*cdf0e10cSrcweir                 else
212*cdf0e10cSrcweir                     // template filters shouldn't be detected if not explicitly asked for
213*cdf0e10cSrcweir                     nDont |= SFX_FILTER_TEMPLATEPATH;
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir                 const SfxFilter* pFilter = 0;
216*cdf0e10cSrcweir                 if ( aName.Len() )
217*cdf0e10cSrcweir                     // get preselected Filter if it matches the desired filter flags
218*cdf0e10cSrcweir                     pFilter = aMatcher.GetFilter4FilterName( aName, nMust, nDont );
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir                 if ( !pFilter || pFilter->GetFormat() != nClipId )
221*cdf0e10cSrcweir                 {
222*cdf0e10cSrcweir                     // get filter from storage MediaType
223*cdf0e10cSrcweir                     pFilter = aMatcher.GetFilter4ClipBoardId( nClipId, nMust, nDont );
224*cdf0e10cSrcweir                     if ( !pFilter )
225*cdf0e10cSrcweir                         // template filter is asked for , but there isn't one; so at least the "normal" format should be detected
226*cdf0e10cSrcweir                         // or storage *is* a template, but bTemplate is not set
227*cdf0e10cSrcweir                         pFilter = aMatcher.GetFilter4ClipBoardId( nClipId );
228*cdf0e10cSrcweir                 }
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir                 if ( pFilter )
231*cdf0e10cSrcweir                 {
232*cdf0e10cSrcweir                     if ( pFilterName )
233*cdf0e10cSrcweir                         *pFilterName = pFilter->GetName();
234*cdf0e10cSrcweir                     return pFilter->GetTypeName();
235*cdf0e10cSrcweir                 }
236*cdf0e10cSrcweir             }
237*cdf0e10cSrcweir         }
238*cdf0e10cSrcweir     }
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir     //TODO: do it without SfxFilter
241*cdf0e10cSrcweir     //TODO/LATER: don't yield FilterName, should be done in FWK!
242*cdf0e10cSrcweir     String aRet;
243*cdf0e10cSrcweir     if ( pType )
244*cdf0e10cSrcweir     {
245*cdf0e10cSrcweir         aRet = String::CreateFromAscii(pType);
246*cdf0e10cSrcweir         if ( pFilterName )
247*cdf0e10cSrcweir             *pFilterName = aMatcher.GetFilter4EA( aRet )->GetName();
248*cdf0e10cSrcweir     }
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir     return aRet;
251*cdf0e10cSrcweir }
252