Update ooo320-m1
[ooovba.git] / sfx2 / source / doc / docfilt.cxx
blobd8b6d23b8d0377311dcbebe39ceca639af3a19a2
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: docfilt.cxx,v $
10 * $Revision: 1.23.142.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sfx2.hxx"
34 // INCLUDE ---------------------------------------------------------------
36 #ifdef SOLARIS
37 // HACK: prevent conflict between STLPORT and Workshop headers on Solaris 8
38 #include <ctime>
39 #endif
41 #include <string> // HACK: prevent conflict between STLPORT and Workshop headers
42 #include <sot/exchange.hxx>
43 #include <comphelper/processfactory.hxx>
44 #include <com/sun/star/beans/XPropertySet.hpp>
45 #include <com/sun/star/container/XNameAccess.hpp>
47 #include <sfx2/docfac.hxx>
48 #include <sfx2/docfilt.hxx>
49 #include "fltfnc.hxx"
50 #include <sfx2/sfxuno.hxx>
51 #include <sfx2/objsh.hxx>
53 using namespace ::com::sun::star;
55 namespace sfx2 {
57 // TODO #i105076# this should be in the filter configuration!!!
58 bool CheckMSPasswordCapabilityForExport( const String& rFilterName )
60 return
61 rFilterName.EqualsAscii( "MS Word 97" ) ||
62 rFilterName.EqualsAscii( "MS Word 97 Vorlage" ) ||
63 rFilterName.EqualsAscii( "MS Excel 97" ) ||
64 rFilterName.EqualsAscii( "MS Excel 97 Vorlage/Template" );
67 } // namespace sfx2
69 // STATIC DATA -----------------------------------------------------------
71 DBG_NAME(SfxFilter)
73 SfxFilter::SfxFilter( const String &rName,
74 const String &rWildCard,
75 SfxFilterFlags nType,
76 sal_uInt32 lFmt,
77 const String &rTypNm,
78 sal_uInt16 nIcon,
79 const String &rMimeType,
80 const String &rUsrDat,
81 const String &rServiceName ):
82 aWildCard(rWildCard, ';'),
83 lFormat(lFmt),
84 aTypeName(rTypNm),
85 aUserData(rUsrDat),
86 nFormatType(nType),
87 nDocIcon(nIcon),
88 aServiceName( rServiceName ),
89 aMimeType( rMimeType ),
90 aFilterName( rName )
92 String aExts = GetWildcard()();
93 String aShort, aLong;
94 String aRet;
95 sal_uInt16 nMaxLength = USHRT_MAX;
96 String aTest;
97 sal_uInt16 nPos = 0;
98 while( ( aRet = aExts.GetToken( nPos++, ';' ) ).Len() )
100 aTest = aRet;
101 aTest.SearchAndReplace( DEFINE_CONST_UNICODE( "*." ), String() );
102 if( aTest.Len() <= nMaxLength )
104 if( aShort.Len() ) aShort += ';';
105 aShort += aRet;
107 else
109 if( aLong.Len() ) aLong += ';';
110 aLong += aRet;
113 if( aShort.Len() && aLong.Len() )
115 aShort += ';';
116 aShort += aLong;
118 aWildCard = aShort;
120 nVersion = SOFFICE_FILEFORMAT_50;
121 aUIName = aFilterName;
124 SfxFilter::~SfxFilter()
128 String SfxFilter::GetDefaultExtension() const
130 return GetWildcard()().GetToken( 0, ';' );
133 String SfxFilter::GetSuffixes() const
135 String aRet = GetWildcard()();
136 while( aRet.SearchAndReplaceAscii( "*.", String() ) != STRING_NOTFOUND ) ;
137 while( aRet.SearchAndReplace( ';', ',' ) != STRING_NOTFOUND ) ;
138 return aRet;
141 const SfxFilter* SfxFilter::GetDefaultFilter( const String& rName )
143 return SfxFilterContainer::GetDefaultFilter_Impl( rName );
146 const SfxFilter* SfxFilter::GetDefaultFilterFromFactory( const String& rFact )
148 return GetDefaultFilter( SfxObjectShell::GetServiceNameFromFactory( rFact ) );
151 const SfxFilter* SfxFilter::GetFilterByName( const String& rName )
153 SfxFilterMatcher aMatch;
154 return aMatch.GetFilter4FilterName( rName, 0, 0 );
157 String SfxFilter::GetTypeFromStorage( const SotStorage& rStg )
159 const char* pType=0;
160 if ( rStg.IsStream( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "WordDocument" ) ) ) )
162 if ( rStg.IsStream( String::CreateFromAscii("0Table" ) ) || rStg.IsStream( String::CreateFromAscii("1Table" ) ) )
163 pType = "writer_MS_Word_97";
164 else
165 pType = "writer_MS_Word_95";
167 else if ( rStg.IsStream( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "Book" ) ) ) )
169 pType = "calc_MS_Excel_95";
171 else if ( rStg.IsStream( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "Workbook" ) ) ) )
173 pType = "calc_MS_Excel_97";
175 else if ( rStg.IsStream( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "PowerPoint Document" ) ) ) )
177 pType = "impress_MS_PowerPoint_97";
179 else if ( rStg.IsStream( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "Equation Native" ) ) ) )
181 pType = "math_MathType_3x";
183 else
185 sal_Int32 nClipId = ((SotStorage&)rStg).GetFormat();
186 if ( nClipId )
188 const SfxFilter* pFilter = SfxFilterMatcher().GetFilter4ClipBoardId( nClipId );
189 if ( pFilter )
190 return pFilter->GetTypeName();
194 return pType ? String::CreateFromAscii(pType) : String();
197 String SfxFilter::GetTypeFromStorage( const com::sun::star::uno::Reference< com::sun::star::embed::XStorage >& xStorage, BOOL bTemplate,
198 String* pFilterName )
199 throw ( beans::UnknownPropertyException,
200 lang::WrappedTargetException,
201 uno::RuntimeException )
203 SfxFilterMatcher aMatcher;
204 const char* pType=0;
205 String aName;
206 if ( pFilterName )
208 aName = *pFilterName;
209 pFilterName->Erase();
212 com::sun::star::uno::Reference< com::sun::star::beans::XPropertySet > xProps( xStorage, com::sun::star::uno::UNO_QUERY );
213 if ( xProps.is() )
215 ::rtl::OUString aMediaType;
216 xProps->getPropertyValue( ::rtl::OUString::createFromAscii( "MediaType" ) ) >>= aMediaType;
217 if ( aMediaType.getLength() )
219 ::com::sun::star::datatransfer::DataFlavor aDataFlavor;
220 aDataFlavor.MimeType = aMediaType;
221 sal_uInt32 nClipId = SotExchange::GetFormat( aDataFlavor );
222 if ( nClipId )
224 SfxFilterFlags nMust = SFX_FILTER_IMPORT, nDont = SFX_FILTER_NOTINSTALLED;
225 if ( bTemplate )
226 // template filter was preselected, try to verify
227 nMust |= SFX_FILTER_TEMPLATEPATH;
228 else
229 // template filters shouldn't be detected if not explicitly asked for
230 nDont |= SFX_FILTER_TEMPLATEPATH;
232 const SfxFilter* pFilter = 0;
233 if ( aName.Len() )
234 // get preselected Filter if it matches the desired filter flags
235 pFilter = aMatcher.GetFilter4FilterName( aName, nMust, nDont );
237 if ( !pFilter || pFilter->GetFormat() != nClipId )
239 // get filter from storage MediaType
240 pFilter = aMatcher.GetFilter4ClipBoardId( nClipId, nMust, nDont );
241 if ( !pFilter )
242 // template filter is asked for , but there isn't one; so at least the "normal" format should be detected
243 // or storage *is* a template, but bTemplate is not set
244 pFilter = aMatcher.GetFilter4ClipBoardId( nClipId );
247 if ( pFilter )
249 if ( pFilterName )
250 *pFilterName = pFilter->GetName();
251 return pFilter->GetTypeName();
257 //TODO: do it without SfxFilter
258 //TODO/LATER: don't yield FilterName, should be done in FWK!
259 String aRet;
260 if ( pType )
262 aRet = String::CreateFromAscii(pType);
263 if ( pFilterName )
264 *pFilterName = aMatcher.GetFilter4EA( aRet )->GetName();
267 return aRet;