Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / sfx2 / source / doc / docfilt.cxx
blobc4d11d3f8370dbd6a9b34c0ad4cd6dd6d0285ab0
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 ---------------------------------------------------------------
32 #ifdef SOLARIS
33 #include <ctime>
34 #endif
36 #include <string>
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>
45 #include "fltfnc.hxx"
46 #include <sfx2/sfxuno.hxx>
47 #include <sfx2/objsh.hxx>
49 using namespace ::com::sun::star;
51 // STATIC DATA -----------------------------------------------------------
53 DBG_NAME(SfxFilter)
55 SfxFilter::SfxFilter( const String &rName,
56 const String &rWildCard,
57 SfxFilterFlags nType,
58 sal_uInt32 lFmt,
59 const String &rTypNm,
60 sal_uInt16 nIcon,
61 const String &rMimeType,
62 const String &rUsrDat,
63 const String &rServiceName ):
64 aWildCard(rWildCard, ';'),
65 lFormat(lFmt),
66 aTypeName(rTypNm),
67 aUserData(rUsrDat),
68 nFormatType(nType),
69 nDocIcon(nIcon),
70 aServiceName( rServiceName ),
71 aMimeType( rMimeType ),
72 aFilterName( rName )
74 String aExts = GetWildcard().getGlob();
75 String aShort, aLong;
76 String aRet;
77 sal_uInt16 nMaxLength = USHRT_MAX;
78 String aTest;
79 sal_uInt16 nPos = 0;
80 while( ( aRet = aExts.GetToken( nPos++, ';' ) ).Len() )
82 aTest = aRet;
83 aTest.SearchAndReplace( DEFINE_CONST_UNICODE( "*." ), String() );
84 if( aTest.Len() <= nMaxLength )
86 if( aShort.Len() ) aShort += ';';
87 aShort += aRet;
89 else
91 if( aLong.Len() ) aLong += ';';
92 aLong += aRet;
95 if( aShort.Len() && aLong.Len() )
97 aShort += ';';
98 aShort += aLong;
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 ) ;
120 return aRet;
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 )
141 const char* pType=0;
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";
146 else
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";
165 else
167 sal_Int32 nClipId = ((SotStorage&)rStg).GetFormat();
168 if ( nClipId )
170 const SfxFilter* pFilter = SfxFilterMatcher().GetFilter4ClipBoardId( nClipId );
171 if ( pFilter )
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;
186 const char* pType=0;
187 String aName;
188 if ( pFilterName )
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 );
195 if ( xProps.is() )
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 );
204 if ( nClipId )
206 SfxFilterFlags nMust = SFX_FILTER_IMPORT, nDont = SFX_FILTER_NOTINSTALLED;
207 if ( bTemplate )
208 // template filter was preselected, try to verify
209 nMust |= SFX_FILTER_TEMPLATEPATH;
210 else
211 // template filters shouldn't be detected if not explicitly asked for
212 nDont |= SFX_FILTER_TEMPLATEPATH;
214 const SfxFilter* pFilter = 0;
215 if ( aName.Len() )
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 );
223 if ( !pFilter )
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 );
229 if ( pFilter )
231 if ( pFilterName )
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!
241 String aRet;
242 if ( pType )
244 aRet = String::CreateFromAscii(pType);
245 if ( pFilterName )
246 *pFilterName = aMatcher.GetFilter4EA( aRet )->GetName();
249 return aRet;
252 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */