bump product version to 4.1.6.2
[LibreOffice.git] / sfx2 / source / doc / docfilt.cxx
blobd642f02ace722d769e5587ea8a47b0582db07a27
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #ifdef SOLARIS
21 #include <ctime>
22 #endif
24 #include <string>
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>
33 #include "fltfnc.hxx"
34 #include <sfx2/sfxuno.hxx>
35 #include <sfx2/objsh.hxx>
37 using namespace ::com::sun::star;
39 // STATIC DATA -----------------------------------------------------------
41 DBG_NAME(SfxFilter)
43 SfxFilter::SfxFilter( const OUString& rProvider, const OUString &rFilterName ) :
44 maFilterName(rFilterName),
45 maProvider(rProvider)
49 SfxFilter::SfxFilter( const OUString &rName,
50 const OUString &rWildCard,
51 SfxFilterFlags nType,
52 sal_uInt32 lFmt,
53 const OUString &rTypNm,
54 sal_uInt16 nIcon,
55 const OUString &rMimeType,
56 const OUString &rUsrDat,
57 const OUString &rServiceName ):
58 aWildCard(rWildCard, ';'),
59 aTypeName(rTypNm),
60 aUserData(rUsrDat),
61 aServiceName(rServiceName),
62 aMimeType(rMimeType),
63 maFilterName(rName),
64 aUIName(maFilterName),
65 nFormatType(nType),
66 nVersion(SOFFICE_FILEFORMAT_50),
67 lFormat(lFmt),
68 nDocIcon(nIcon)
70 OUString aExts = GetWildcard().getGlob();
71 OUString aShort, aLong;
72 OUString aRet;
73 sal_uInt16 nMaxLength = USHRT_MAX;
74 OUString aTest;
75 sal_uInt16 nPos = 0;
76 while (!(aRet = aExts.getToken(nPos++, ';')).isEmpty() )
78 aTest = aRet;
79 aTest = aTest.replaceFirst( "*." , "" );
80 if( aTest.getLength() <= nMaxLength )
82 if (!aShort.isEmpty())
83 aShort += ";";
84 aShort += aRet;
86 else
88 if (!aLong.isEmpty())
89 aLong += ";";
90 aLong += aRet;
93 if (!aShort.isEmpty() && !aLong.isEmpty())
95 aShort += ";";
96 aShort += aLong;
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
112 return maProvider;
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 ) ;
125 return aRet;
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 )
146 const char* pType=0;
147 if ( rStg.IsStream( OUString("WordDocument") ) )
149 if ( rStg.IsStream( OUString("0Table") ) || rStg.IsStream( OUString("1Table") ) )
150 pType = "writer_MS_Word_97";
151 else
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";
170 else
172 sal_Int32 nClipId = ((SotStorage&)rStg).GetFormat();
173 if ( nClipId )
175 const SfxFilter* pFilter = SfxFilterMatcher().GetFilter4ClipBoardId( nClipId );
176 if ( pFilter )
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;
189 const char* pType=0;
190 OUString aName;
191 if ( pFilterName )
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 );
198 if ( xProps.is() )
200 OUString aMediaType;
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 );
207 if ( nClipId )
209 SfxFilterFlags nMust = SFX_FILTER_IMPORT, nDont = SFX_FILTER_NOTINSTALLED;
210 if ( bTemplate )
211 // template filter was preselected, try to verify
212 nMust |= SFX_FILTER_TEMPLATEPATH;
213 else
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 );
226 if ( !pFilter )
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 );
232 if ( pFilter )
234 if ( pFilterName )
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!
244 OUString aRet;
245 if ( pType )
247 aRet = OUString::createFromAscii(pType);
248 if ( pFilterName )
249 *pFilterName = aMatcher.GetFilter4EA( aRet )->GetName();
252 return aRet;
255 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */