1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 // INCLUDE ---------------------------------------------------------------
37 #include <sot/exchange.hxx>
38 #include <comphelper/processfactory.hxx>
39 #include <comphelper/string.hxx>
40 #include <com/sun/star/beans/XPropertySet.hpp>
41 #include <com/sun/star/container/XNameAccess.hpp>
43 #include <sfx2/docfac.hxx>
44 #include <sfx2/docfilt.hxx>
46 #include <sfx2/sfxuno.hxx>
47 #include <sfx2/objsh.hxx>
49 using namespace ::com::sun::star
;
51 // STATIC DATA -----------------------------------------------------------
55 SfxFilter::SfxFilter( const String
&rName
,
56 const String
&rWildCard
,
61 const String
&rMimeType
,
62 const String
&rUsrDat
,
63 const String
&rServiceName
):
64 aWildCard(rWildCard
, ';'),
70 aServiceName( rServiceName
),
71 aMimeType( rMimeType
),
74 String aExts
= GetWildcard().getGlob();
77 sal_uInt16 nMaxLength
= USHRT_MAX
;
80 while( ( aRet
= aExts
.GetToken( nPos
++, ';' ) ).Len() )
83 aTest
.SearchAndReplace( DEFINE_CONST_UNICODE( "*." ), String() );
84 if( aTest
.Len() <= nMaxLength
)
86 if( aShort
.Len() ) aShort
+= ';';
91 if( aLong
.Len() ) aLong
+= ';';
95 if( aShort
.Len() && aLong
.Len() )
100 aWildCard
.setGlob(aShort
);
102 nVersion
= SOFFICE_FILEFORMAT_50
;
103 aUIName
= aFilterName
;
106 SfxFilter::~SfxFilter()
110 String
SfxFilter::GetDefaultExtension() const
112 return comphelper::string::getToken(GetWildcard().getGlob(), 0, ';');
115 String
SfxFilter::GetSuffixes() const
117 String aRet
= GetWildcard().getGlob();
118 while( aRet
.SearchAndReplaceAscii( "*.", String() ) != STRING_NOTFOUND
) ;
119 while( aRet
.SearchAndReplace( ';', ',' ) != STRING_NOTFOUND
) ;
123 const SfxFilter
* SfxFilter::GetDefaultFilter( const String
& rName
)
125 return SfxFilterContainer::GetDefaultFilter_Impl( rName
);
128 const SfxFilter
* SfxFilter::GetDefaultFilterFromFactory( const String
& rFact
)
130 return GetDefaultFilter( SfxObjectShell::GetServiceNameFromFactory( rFact
) );
133 const SfxFilter
* SfxFilter::GetFilterByName( const String
& rName
)
135 SfxFilterMatcher aMatch
;
136 return aMatch
.GetFilter4FilterName( rName
, 0, 0 );
139 String
SfxFilter::GetTypeFromStorage( const SotStorage
& rStg
)
142 if ( rStg
.IsStream( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "WordDocument" ) ) ) )
144 if ( rStg
.IsStream( String::CreateFromAscii("0Table" ) ) || rStg
.IsStream( String::CreateFromAscii("1Table" ) ) )
145 pType
= "writer_MS_Word_97";
147 pType
= "writer_MS_Word_95";
149 else if ( rStg
.IsStream( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "Book" ) ) ) )
151 pType
= "calc_MS_Excel_95";
153 else if ( rStg
.IsStream( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "Workbook" ) ) ) )
155 pType
= "calc_MS_Excel_97";
157 else if ( rStg
.IsStream( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "PowerPoint Document" ) ) ) )
159 pType
= "impress_MS_PowerPoint_97";
161 else if ( rStg
.IsStream( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "Equation Native" ) ) ) )
163 pType
= "math_MathType_3x";
167 sal_Int32 nClipId
= ((SotStorage
&)rStg
).GetFormat();
170 const SfxFilter
* pFilter
= SfxFilterMatcher().GetFilter4ClipBoardId( nClipId
);
172 return pFilter
->GetTypeName();
176 return pType
? String::CreateFromAscii(pType
) : String();
179 String
SfxFilter::GetTypeFromStorage( const com::sun::star::uno::Reference
< com::sun::star::embed::XStorage
>& xStorage
, sal_Bool bTemplate
,
180 String
* pFilterName
)
181 throw ( beans::UnknownPropertyException
,
182 lang::WrappedTargetException
,
183 uno::RuntimeException
)
185 SfxFilterMatcher aMatcher
;
190 aName
= *pFilterName
;
191 pFilterName
->Erase();
194 com::sun::star::uno::Reference
< com::sun::star::beans::XPropertySet
> xProps( xStorage
, com::sun::star::uno::UNO_QUERY
);
197 ::rtl::OUString aMediaType
;
198 xProps
->getPropertyValue( ::rtl::OUString("MediaType") ) >>= aMediaType
;
199 if ( !aMediaType
.isEmpty() )
201 ::com::sun::star::datatransfer::DataFlavor aDataFlavor
;
202 aDataFlavor
.MimeType
= aMediaType
;
203 sal_uInt32 nClipId
= SotExchange::GetFormat( aDataFlavor
);
206 SfxFilterFlags nMust
= SFX_FILTER_IMPORT
, nDont
= SFX_FILTER_NOTINSTALLED
;
208 // template filter was preselected, try to verify
209 nMust
|= SFX_FILTER_TEMPLATEPATH
;
211 // template filters shouldn't be detected if not explicitly asked for
212 nDont
|= SFX_FILTER_TEMPLATEPATH
;
214 const SfxFilter
* pFilter
= 0;
216 // get preselected Filter if it matches the desired filter flags
217 pFilter
= aMatcher
.GetFilter4FilterName( aName
, nMust
, nDont
);
219 if ( !pFilter
|| pFilter
->GetFormat() != nClipId
)
221 // get filter from storage MediaType
222 pFilter
= aMatcher
.GetFilter4ClipBoardId( nClipId
, nMust
, nDont
);
224 // template filter is asked for , but there isn't one; so at least the "normal" format should be detected
225 // or storage *is* a template, but bTemplate is not set
226 pFilter
= aMatcher
.GetFilter4ClipBoardId( nClipId
);
232 *pFilterName
= pFilter
->GetName();
233 return pFilter
->GetTypeName();
239 //TODO: do it without SfxFilter
240 //TODO/LATER: don't yield FilterName, should be done in FWK!
244 aRet
= String::CreateFromAscii(pType
);
246 *pFilterName
= aMatcher
.GetFilter4EA( aRet
)->GetName();
252 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */