update dev300-m58
[ooovba.git] / sfx2 / source / doc / docfilt.cxx
blob6ca20b3622795b70282ba36350830f95ab1799b5
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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 // STATIC DATA -----------------------------------------------------------
57 DBG_NAME(SfxFilter)
59 SfxFilter::SfxFilter( const String &rName,
60 const String &rWildCard,
61 SfxFilterFlags nType,
62 sal_uInt32 lFmt,
63 const String &rTypNm,
64 sal_uInt16 nIcon,
65 const String &rMimeType,
66 const String &rUsrDat,
67 const String &rServiceName ):
68 aWildCard(rWildCard, ';'),
69 lFormat(lFmt),
70 aTypeName(rTypNm),
71 aUserData(rUsrDat),
72 nFormatType(nType),
73 nDocIcon(nIcon),
74 aServiceName( rServiceName ),
75 aMimeType( rMimeType ),
76 aFilterName( rName )
78 String aExts = GetWildcard()();
79 String aShort, aLong;
80 String aRet;
81 sal_uInt16 nMaxLength = USHRT_MAX;
82 String aTest;
83 sal_uInt16 nPos = 0;
84 while( ( aRet = aExts.GetToken( nPos++, ';' ) ).Len() )
86 aTest = aRet;
87 aTest.SearchAndReplace( DEFINE_CONST_UNICODE( "*." ), String() );
88 if( aTest.Len() <= nMaxLength )
90 if( aShort.Len() ) aShort += ';';
91 aShort += aRet;
93 else
95 if( aLong.Len() ) aLong += ';';
96 aLong += aRet;
99 if( aShort.Len() && aLong.Len() )
101 aShort += ';';
102 aShort += aLong;
104 aWildCard = aShort;
106 nVersion = SOFFICE_FILEFORMAT_50;
107 aUIName = aFilterName;
110 SfxFilter::~SfxFilter()
114 String SfxFilter::GetDefaultExtension() const
116 return GetWildcard()().GetToken( 0, ';' );
119 String SfxFilter::GetSuffixes() const
121 String aRet = GetWildcard()();
122 while( aRet.SearchAndReplaceAscii( "*.", String() ) != STRING_NOTFOUND ) ;
123 while( aRet.SearchAndReplace( ';', ',' ) != STRING_NOTFOUND ) ;
124 return aRet;
127 const SfxFilter* SfxFilter::GetDefaultFilter( const String& rName )
129 return SfxFilterContainer::GetDefaultFilter_Impl( rName );
132 const SfxFilter* SfxFilter::GetDefaultFilterFromFactory( const String& rFact )
134 return GetDefaultFilter( SfxObjectShell::GetServiceNameFromFactory( rFact ) );
137 const SfxFilter* SfxFilter::GetFilterByName( const String& rName )
139 SfxFilterMatcher aMatch;
140 return aMatch.GetFilter4FilterName( rName, 0, 0 );
143 String SfxFilter::GetTypeFromStorage( const SotStorage& rStg )
145 const char* pType=0;
146 if ( rStg.IsStream( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "WordDocument" ) ) ) )
148 if ( rStg.IsStream( String::CreateFromAscii("0Table" ) ) || rStg.IsStream( String::CreateFromAscii("1Table" ) ) )
149 pType = "writer_MS_Word_97";
150 else
151 pType = "writer_MS_Word_95";
153 else if ( rStg.IsStream( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "Book" ) ) ) )
155 pType = "calc_MS_Excel_95";
157 else if ( rStg.IsStream( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "Workbook" ) ) ) )
159 pType = "calc_MS_Excel_97";
161 else if ( rStg.IsStream( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "PowerPoint Document" ) ) ) )
163 pType = "impress_MS_PowerPoint_97";
165 else if ( rStg.IsStream( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "Equation Native" ) ) ) )
167 pType = "math_MathType_3x";
169 else
171 sal_Int32 nClipId = ((SotStorage&)rStg).GetFormat();
172 if ( nClipId )
174 const SfxFilter* pFilter = SfxFilterMatcher().GetFilter4ClipBoardId( nClipId );
175 if ( pFilter )
176 return pFilter->GetTypeName();
180 return pType ? String::CreateFromAscii(pType) : String();
183 String SfxFilter::GetTypeFromStorage( const com::sun::star::uno::Reference< com::sun::star::embed::XStorage >& xStorage, BOOL bTemplate,
184 String* pFilterName )
185 throw ( beans::UnknownPropertyException,
186 lang::WrappedTargetException,
187 uno::RuntimeException )
189 SfxFilterMatcher aMatcher;
190 const char* pType=0;
191 String aName;
192 if ( pFilterName )
194 aName = *pFilterName;
195 pFilterName->Erase();
198 com::sun::star::uno::Reference< com::sun::star::beans::XPropertySet > xProps( xStorage, com::sun::star::uno::UNO_QUERY );
199 if ( xProps.is() )
201 ::rtl::OUString aMediaType;
202 xProps->getPropertyValue( ::rtl::OUString::createFromAscii( "MediaType" ) ) >>= aMediaType;
203 if ( aMediaType.getLength() )
205 ::com::sun::star::datatransfer::DataFlavor aDataFlavor;
206 aDataFlavor.MimeType = aMediaType;
207 sal_uInt32 nClipId = SotExchange::GetFormat( aDataFlavor );
208 if ( nClipId )
210 SfxFilterFlags nMust = SFX_FILTER_IMPORT, nDont = SFX_FILTER_NOTINSTALLED;
211 if ( bTemplate )
212 // template filter was preselected, try to verify
213 nMust |= SFX_FILTER_TEMPLATEPATH;
214 else
215 // template filters shouldn't be detected if not explicitly asked for
216 nDont |= SFX_FILTER_TEMPLATEPATH;
218 const SfxFilter* pFilter = 0;
219 if ( aName.Len() )
220 // get preselected Filter if it matches the desired filter flags
221 pFilter = aMatcher.GetFilter4FilterName( aName, nMust, nDont );
223 if ( !pFilter || pFilter->GetFormat() != nClipId )
225 // get filter from storage MediaType
226 pFilter = aMatcher.GetFilter4ClipBoardId( nClipId, nMust, nDont );
227 if ( !pFilter )
228 // template filter is asked for , but there isn't one; so at least the "normal" format should be detected
229 // or storage *is* a template, but bTemplate is not set
230 pFilter = aMatcher.GetFilter4ClipBoardId( nClipId );
233 if ( pFilter )
235 if ( pFilterName )
236 *pFilterName = pFilter->GetName();
237 return pFilter->GetTypeName();
243 //TODO: do it without SfxFilter
244 //TODO/LATER: don't yield FilterName, should be done in FWK!
245 String aRet;
246 if ( pType )
248 aRet = String::CreateFromAscii(pType);
249 if ( pFilterName )
250 *pFilterName = aMatcher.GetFilter4EA( aRet )->GetName();
253 return aRet;