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 ---------------------------------------------------------------
37 // HACK: prevent conflict between STLPORT and Workshop headers on Solaris 8
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>
50 #include <sfx2/sfxuno.hxx>
51 #include <sfx2/objsh.hxx>
53 using namespace ::com::sun::star
;
55 // STATIC DATA -----------------------------------------------------------
59 SfxFilter::SfxFilter( const String
&rName
,
60 const String
&rWildCard
,
65 const String
&rMimeType
,
66 const String
&rUsrDat
,
67 const String
&rServiceName
):
68 aWildCard(rWildCard
, ';'),
74 aServiceName( rServiceName
),
75 aMimeType( rMimeType
),
78 String aExts
= GetWildcard()();
81 sal_uInt16 nMaxLength
= USHRT_MAX
;
84 while( ( aRet
= aExts
.GetToken( nPos
++, ';' ) ).Len() )
87 aTest
.SearchAndReplace( DEFINE_CONST_UNICODE( "*." ), String() );
88 if( aTest
.Len() <= nMaxLength
)
90 if( aShort
.Len() ) aShort
+= ';';
95 if( aLong
.Len() ) aLong
+= ';';
99 if( aShort
.Len() && aLong
.Len() )
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
) ;
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
)
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";
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";
171 sal_Int32 nClipId
= ((SotStorage
&)rStg
).GetFormat();
174 const SfxFilter
* pFilter
= SfxFilterMatcher().GetFilter4ClipBoardId( nClipId
);
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
;
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
);
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
);
210 SfxFilterFlags nMust
= SFX_FILTER_IMPORT
, nDont
= SFX_FILTER_NOTINSTALLED
;
212 // template filter was preselected, try to verify
213 nMust
|= SFX_FILTER_TEMPLATEPATH
;
215 // template filters shouldn't be detected if not explicitly asked for
216 nDont
|= SFX_FILTER_TEMPLATEPATH
;
218 const SfxFilter
* pFilter
= 0;
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
);
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
);
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!
248 aRet
= String::CreateFromAscii(pType
);
250 *pFilterName
= aMatcher
.GetFilter4EA( aRet
)->GetName();