1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
25 #include <sot/exchange.hxx>
26 #include <comphelper/processfactory.hxx>
27 #include <comphelper/string.hxx>
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/container/XNameAccess.hpp>
31 #include <sfx2/docfac.hxx>
32 #include <sfx2/docfilt.hxx>
34 #include <sfx2/sfxuno.hxx>
35 #include <sfx2/objsh.hxx>
37 using namespace ::com::sun::star
;
39 // STATIC DATA -----------------------------------------------------------
43 SfxFilter::SfxFilter( const OUString
& rProvider
, const OUString
&rFilterName
) :
44 maFilterName(rFilterName
),
49 SfxFilter::SfxFilter( const OUString
&rName
,
50 const OUString
&rWildCard
,
53 const OUString
&rTypNm
,
55 const OUString
&rMimeType
,
56 const OUString
&rUsrDat
,
57 const OUString
&rServiceName
):
58 aWildCard(rWildCard
, ';'),
61 aServiceName(rServiceName
),
64 aUIName(maFilterName
),
66 nVersion(SOFFICE_FILEFORMAT_50
),
70 OUString aExts
= GetWildcard().getGlob();
71 OUString aShort
, aLong
;
73 sal_uInt16 nMaxLength
= USHRT_MAX
;
76 while (!(aRet
= aExts
.getToken(nPos
++, ';')).isEmpty() )
79 aTest
= aTest
.replaceFirst( "*." , "" );
80 if( aTest
.getLength() <= nMaxLength
)
82 if (!aShort
.isEmpty())
93 if (!aShort
.isEmpty() && !aLong
.isEmpty())
98 aWildCard
.setGlob(aShort
);
101 SfxFilter::~SfxFilter()
105 OUString
SfxFilter::GetDefaultExtension() const
107 return comphelper::string::getToken(GetWildcard().getGlob(), 0, ';');
110 const OUString
& SfxFilter::GetProviderName() const
115 void SfxFilter::SetURLPattern( const OUString
& rStr
)
117 aPattern
= rStr
.toAsciiLowerCase();
120 OUString
SfxFilter::GetSuffixes() const
122 String aRet
= GetWildcard().getGlob();
123 while( aRet
.SearchAndReplaceAscii( "*.", String() ) != STRING_NOTFOUND
) ;
124 while( aRet
.SearchAndReplace( ';', ',' ) != STRING_NOTFOUND
) ;
128 const SfxFilter
* SfxFilter::GetDefaultFilter( const String
& rName
)
130 return SfxFilterContainer::GetDefaultFilter_Impl( rName
);
133 const SfxFilter
* SfxFilter::GetDefaultFilterFromFactory( const String
& rFact
)
135 return GetDefaultFilter( SfxObjectShell::GetServiceNameFromFactory( rFact
) );
138 const SfxFilter
* SfxFilter::GetFilterByName( const String
& rName
)
140 SfxFilterMatcher aMatch
;
141 return aMatch
.GetFilter4FilterName( rName
, 0, 0 );
144 OUString
SfxFilter::GetTypeFromStorage( const SotStorage
& rStg
)
147 if ( rStg
.IsStream( OUString("WordDocument") ) )
149 if ( rStg
.IsStream( OUString("0Table") ) || rStg
.IsStream( OUString("1Table") ) )
150 pType
= "writer_MS_Word_97";
152 pType
= "writer_MS_Word_95";
154 else if ( rStg
.IsStream( OUString("Book") ) )
156 pType
= "calc_MS_Excel_95";
158 else if ( rStg
.IsStream( OUString("Workbook" ) ) )
160 pType
= "calc_MS_Excel_97";
162 else if ( rStg
.IsStream( OUString("PowerPoint Document") ) )
164 pType
= "impress_MS_PowerPoint_97";
166 else if ( rStg
.IsStream( OUString("Equation Native") ) )
168 pType
= "math_MathType_3x";
172 sal_Int32 nClipId
= ((SotStorage
&)rStg
).GetFormat();
175 const SfxFilter
* pFilter
= SfxFilterMatcher().GetFilter4ClipBoardId( nClipId
);
177 return pFilter
->GetTypeName();
181 return pType
? OUString::createFromAscii(pType
) : OUString();
184 OUString
SfxFilter::GetTypeFromStorage(
185 const uno::Reference
<embed::XStorage
>& xStorage
, bool bTemplate
, OUString
* pFilterName
)
186 throw ( beans::UnknownPropertyException
, lang::WrappedTargetException
, uno::RuntimeException
)
188 SfxFilterMatcher aMatcher
;
193 aName
= *pFilterName
;
194 *pFilterName
= OUString();
197 com::sun::star::uno::Reference
< com::sun::star::beans::XPropertySet
> xProps( xStorage
, com::sun::star::uno::UNO_QUERY
);
201 xProps
->getPropertyValue("MediaType") >>= aMediaType
;
202 if ( !aMediaType
.isEmpty() )
204 ::com::sun::star::datatransfer::DataFlavor aDataFlavor
;
205 aDataFlavor
.MimeType
= aMediaType
;
206 sal_uInt32 nClipId
= SotExchange::GetFormat( aDataFlavor
);
209 SfxFilterFlags nMust
= SFX_FILTER_IMPORT
, nDont
= SFX_FILTER_NOTINSTALLED
;
211 // template filter was preselected, try to verify
212 nMust
|= SFX_FILTER_TEMPLATEPATH
;
214 // template filters shouldn't be detected if not explicitly asked for
215 nDont
|= SFX_FILTER_TEMPLATEPATH
;
217 const SfxFilter
* pFilter
= 0;
218 if (!aName
.isEmpty())
219 // get preselected Filter if it matches the desired filter flags
220 pFilter
= aMatcher
.GetFilter4FilterName( aName
, nMust
, nDont
);
222 if ( !pFilter
|| pFilter
->GetFormat() != nClipId
)
224 // get filter from storage MediaType
225 pFilter
= aMatcher
.GetFilter4ClipBoardId( nClipId
, nMust
, nDont
);
227 // template filter is asked for , but there isn't one; so at least the "normal" format should be detected
228 // or storage *is* a template, but bTemplate is not set
229 pFilter
= aMatcher
.GetFilter4ClipBoardId( nClipId
);
235 *pFilterName
= pFilter
->GetName();
236 return pFilter
->GetTypeName();
242 //TODO: do it without SfxFilter
243 //TODO/LATER: don't yield FilterName, should be done in FWK!
247 aRet
= OUString::createFromAscii(pType
);
249 *pFilterName
= aMatcher
.GetFilter4EA( aRet
)->GetName();
255 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */