xref: /trunk/main/sfx2/source/doc/docfilt.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
1*d119d52dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*d119d52dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*d119d52dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*d119d52dSAndrew Rist  * distributed with this work for additional information
6*d119d52dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*d119d52dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*d119d52dSAndrew Rist  * "License"); you may not use this file except in compliance
9*d119d52dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*d119d52dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*d119d52dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*d119d52dSAndrew Rist  * software distributed under the License is distributed on an
15*d119d52dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*d119d52dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*d119d52dSAndrew Rist  * specific language governing permissions and limitations
18*d119d52dSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*d119d52dSAndrew Rist  *************************************************************/
21*d119d52dSAndrew Rist 
22*d119d52dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sfx2.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir // INCLUDE ---------------------------------------------------------------
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #ifdef SOLARIS
30cdf0e10cSrcweir // HACK: prevent conflict between STLPORT and Workshop headers on Solaris 8
31cdf0e10cSrcweir #include <ctime>
32cdf0e10cSrcweir #endif
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <string> // HACK: prevent conflict between STLPORT and Workshop headers
35cdf0e10cSrcweir #include <sot/exchange.hxx>
36cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
37cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
38cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #include <sfx2/docfac.hxx>
41cdf0e10cSrcweir #include <sfx2/docfilt.hxx>
42cdf0e10cSrcweir #include "fltfnc.hxx"
43cdf0e10cSrcweir #include <sfx2/sfxuno.hxx>
44cdf0e10cSrcweir #include <sfx2/objsh.hxx>
45cdf0e10cSrcweir 
46cdf0e10cSrcweir using namespace ::com::sun::star;
47cdf0e10cSrcweir 
48cdf0e10cSrcweir // STATIC DATA -----------------------------------------------------------
49cdf0e10cSrcweir 
DBG_NAME(SfxFilter)50cdf0e10cSrcweir DBG_NAME(SfxFilter)
51cdf0e10cSrcweir 
52cdf0e10cSrcweir SfxFilter::SfxFilter(  const String &rName,
53cdf0e10cSrcweir                        const String &rWildCard,
54cdf0e10cSrcweir                        SfxFilterFlags nType,
55cdf0e10cSrcweir                        sal_uInt32 lFmt,
56cdf0e10cSrcweir                        const String &rTypNm,
57cdf0e10cSrcweir                        sal_uInt16 nIcon,
58cdf0e10cSrcweir                        const String &rMimeType,
59cdf0e10cSrcweir                        const String &rUsrDat,
60cdf0e10cSrcweir                        const String &rServiceName ):
61cdf0e10cSrcweir     aWildCard(rWildCard, ';'),
62cdf0e10cSrcweir     lFormat(lFmt),
63cdf0e10cSrcweir     aTypeName(rTypNm),
64cdf0e10cSrcweir     aUserData(rUsrDat),
65cdf0e10cSrcweir     nFormatType(nType),
66cdf0e10cSrcweir     nDocIcon(nIcon),
67cdf0e10cSrcweir     aServiceName( rServiceName ),
68cdf0e10cSrcweir     aMimeType( rMimeType ),
69cdf0e10cSrcweir     aFilterName( rName )
70cdf0e10cSrcweir {
71cdf0e10cSrcweir     String aExts = GetWildcard()();
72cdf0e10cSrcweir     String aShort, aLong;
73cdf0e10cSrcweir     String aRet;
74cdf0e10cSrcweir     sal_uInt16 nMaxLength = USHRT_MAX;
75cdf0e10cSrcweir     String aTest;
76cdf0e10cSrcweir     sal_uInt16 nPos = 0;
77cdf0e10cSrcweir     while( ( aRet = aExts.GetToken( nPos++, ';' ) ).Len() )
78cdf0e10cSrcweir     {
79cdf0e10cSrcweir         aTest = aRet;
80cdf0e10cSrcweir         aTest.SearchAndReplace( DEFINE_CONST_UNICODE( "*." ), String() );
81cdf0e10cSrcweir         if( aTest.Len() <= nMaxLength )
82cdf0e10cSrcweir         {
83cdf0e10cSrcweir             if( aShort.Len() ) aShort += ';';
84cdf0e10cSrcweir             aShort += aRet;
85cdf0e10cSrcweir         }
86cdf0e10cSrcweir         else
87cdf0e10cSrcweir         {
88cdf0e10cSrcweir             if( aLong.Len() ) aLong += ';';
89cdf0e10cSrcweir             aLong += aRet;
90cdf0e10cSrcweir         }
91cdf0e10cSrcweir     }
92cdf0e10cSrcweir     if( aShort.Len() && aLong.Len() )
93cdf0e10cSrcweir     {
94cdf0e10cSrcweir         aShort += ';';
95cdf0e10cSrcweir         aShort += aLong;
96cdf0e10cSrcweir     }
97cdf0e10cSrcweir     aWildCard = aShort;
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     nVersion = SOFFICE_FILEFORMAT_50;
100cdf0e10cSrcweir     aUIName = aFilterName;
101cdf0e10cSrcweir }
102cdf0e10cSrcweir 
~SfxFilter()103cdf0e10cSrcweir SfxFilter::~SfxFilter()
104cdf0e10cSrcweir {
105cdf0e10cSrcweir }
106cdf0e10cSrcweir 
GetDefaultExtension() const107cdf0e10cSrcweir String SfxFilter::GetDefaultExtension() const
108cdf0e10cSrcweir {
109cdf0e10cSrcweir     return GetWildcard()().GetToken( 0, ';' );
110cdf0e10cSrcweir }
111cdf0e10cSrcweir 
GetSuffixes() const112cdf0e10cSrcweir String SfxFilter::GetSuffixes() const
113cdf0e10cSrcweir {
114cdf0e10cSrcweir     String aRet = GetWildcard()();
115cdf0e10cSrcweir     while( aRet.SearchAndReplaceAscii( "*.", String() ) != STRING_NOTFOUND ) ;
116cdf0e10cSrcweir     while( aRet.SearchAndReplace( ';', ',' ) != STRING_NOTFOUND ) ;
117cdf0e10cSrcweir     return aRet;
118cdf0e10cSrcweir }
119cdf0e10cSrcweir 
GetDefaultFilter(const String & rName)120cdf0e10cSrcweir const SfxFilter* SfxFilter::GetDefaultFilter( const String& rName )
121cdf0e10cSrcweir {
122cdf0e10cSrcweir     return SfxFilterContainer::GetDefaultFilter_Impl( rName );
123cdf0e10cSrcweir }
124cdf0e10cSrcweir 
GetDefaultFilterFromFactory(const String & rFact)125cdf0e10cSrcweir const SfxFilter* SfxFilter::GetDefaultFilterFromFactory( const String& rFact )
126cdf0e10cSrcweir {
127cdf0e10cSrcweir     return GetDefaultFilter( SfxObjectShell::GetServiceNameFromFactory( rFact ) );
128cdf0e10cSrcweir }
129cdf0e10cSrcweir 
GetFilterByName(const String & rName)130cdf0e10cSrcweir const SfxFilter* SfxFilter::GetFilterByName( const String& rName )
131cdf0e10cSrcweir {
132cdf0e10cSrcweir     SfxFilterMatcher aMatch;
133cdf0e10cSrcweir     return aMatch.GetFilter4FilterName( rName, 0, 0 );
134cdf0e10cSrcweir }
135cdf0e10cSrcweir 
GetTypeFromStorage(const SotStorage & rStg)136cdf0e10cSrcweir String SfxFilter::GetTypeFromStorage( const SotStorage& rStg )
137cdf0e10cSrcweir {
138cdf0e10cSrcweir     const char* pType=0;
139cdf0e10cSrcweir     if ( rStg.IsStream( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "WordDocument" ) ) ) )
140cdf0e10cSrcweir     {
141cdf0e10cSrcweir         if ( rStg.IsStream( String::CreateFromAscii("0Table" ) ) || rStg.IsStream( String::CreateFromAscii("1Table" ) ) )
142cdf0e10cSrcweir             pType = "writer_MS_Word_97";
143cdf0e10cSrcweir         else
144cdf0e10cSrcweir             pType = "writer_MS_Word_95";
145cdf0e10cSrcweir     }
146cdf0e10cSrcweir     else if ( rStg.IsStream( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "Book" ) ) ) )
147cdf0e10cSrcweir     {
148cdf0e10cSrcweir         pType = "calc_MS_Excel_95";
149cdf0e10cSrcweir     }
150cdf0e10cSrcweir     else if ( rStg.IsStream( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "Workbook" ) ) ) )
151cdf0e10cSrcweir     {
152cdf0e10cSrcweir         pType = "calc_MS_Excel_97";
153cdf0e10cSrcweir     }
154cdf0e10cSrcweir     else if ( rStg.IsStream( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "PowerPoint Document" ) ) ) )
155cdf0e10cSrcweir     {
156cdf0e10cSrcweir         pType = "impress_MS_PowerPoint_97";
157cdf0e10cSrcweir     }
158cdf0e10cSrcweir     else if ( rStg.IsStream( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "Equation Native" ) ) ) )
159cdf0e10cSrcweir     {
160cdf0e10cSrcweir         pType = "math_MathType_3x";
161cdf0e10cSrcweir     }
162cdf0e10cSrcweir     else
163cdf0e10cSrcweir     {
164cdf0e10cSrcweir         sal_Int32 nClipId = ((SotStorage&)rStg).GetFormat();
165cdf0e10cSrcweir         if ( nClipId )
166cdf0e10cSrcweir         {
167cdf0e10cSrcweir             const SfxFilter* pFilter = SfxFilterMatcher().GetFilter4ClipBoardId( nClipId );
168cdf0e10cSrcweir             if ( pFilter )
169cdf0e10cSrcweir                 return pFilter->GetTypeName();
170cdf0e10cSrcweir         }
171cdf0e10cSrcweir     }
172cdf0e10cSrcweir 
173cdf0e10cSrcweir     return pType ? String::CreateFromAscii(pType) : String();
174cdf0e10cSrcweir }
175cdf0e10cSrcweir 
GetTypeFromStorage(const com::sun::star::uno::Reference<com::sun::star::embed::XStorage> & xStorage,sal_Bool bTemplate,String * pFilterName)176cdf0e10cSrcweir String SfxFilter::GetTypeFromStorage( const com::sun::star::uno::Reference< com::sun::star::embed::XStorage >& xStorage, sal_Bool bTemplate,
177cdf0e10cSrcweir                                         String* pFilterName )
178cdf0e10cSrcweir         throw ( beans::UnknownPropertyException,
179cdf0e10cSrcweir                 lang::WrappedTargetException,
180cdf0e10cSrcweir                 uno::RuntimeException )
181cdf0e10cSrcweir {
182cdf0e10cSrcweir     SfxFilterMatcher aMatcher;
183cdf0e10cSrcweir     const char* pType=0;
184cdf0e10cSrcweir     String aName;
185cdf0e10cSrcweir     if ( pFilterName )
186cdf0e10cSrcweir     {
187cdf0e10cSrcweir         aName = *pFilterName;
188cdf0e10cSrcweir         pFilterName->Erase();
189cdf0e10cSrcweir     }
190cdf0e10cSrcweir 
191cdf0e10cSrcweir     com::sun::star::uno::Reference< com::sun::star::beans::XPropertySet > xProps( xStorage, com::sun::star::uno::UNO_QUERY );
192cdf0e10cSrcweir     if ( xProps.is() )
193cdf0e10cSrcweir     {
194cdf0e10cSrcweir         ::rtl::OUString aMediaType;
195cdf0e10cSrcweir         xProps->getPropertyValue( ::rtl::OUString::createFromAscii( "MediaType" ) ) >>= aMediaType;
196cdf0e10cSrcweir         if ( aMediaType.getLength() )
197cdf0e10cSrcweir         {
198cdf0e10cSrcweir             ::com::sun::star::datatransfer::DataFlavor aDataFlavor;
199cdf0e10cSrcweir             aDataFlavor.MimeType = aMediaType;
200cdf0e10cSrcweir             sal_uInt32 nClipId = SotExchange::GetFormat( aDataFlavor );
201cdf0e10cSrcweir             if ( nClipId )
202cdf0e10cSrcweir             {
203cdf0e10cSrcweir                 SfxFilterFlags nMust = SFX_FILTER_IMPORT, nDont = SFX_FILTER_NOTINSTALLED;
204cdf0e10cSrcweir                 if ( bTemplate )
205cdf0e10cSrcweir                     // template filter was preselected, try to verify
206cdf0e10cSrcweir                     nMust |= SFX_FILTER_TEMPLATEPATH;
207cdf0e10cSrcweir                 else
208cdf0e10cSrcweir                     // template filters shouldn't be detected if not explicitly asked for
209cdf0e10cSrcweir                     nDont |= SFX_FILTER_TEMPLATEPATH;
210cdf0e10cSrcweir 
211cdf0e10cSrcweir                 const SfxFilter* pFilter = 0;
212cdf0e10cSrcweir                 if ( aName.Len() )
213cdf0e10cSrcweir                     // get preselected Filter if it matches the desired filter flags
214cdf0e10cSrcweir                     pFilter = aMatcher.GetFilter4FilterName( aName, nMust, nDont );
215cdf0e10cSrcweir 
216cdf0e10cSrcweir                 if ( !pFilter || pFilter->GetFormat() != nClipId )
217cdf0e10cSrcweir                 {
218cdf0e10cSrcweir                     // get filter from storage MediaType
219cdf0e10cSrcweir                     pFilter = aMatcher.GetFilter4ClipBoardId( nClipId, nMust, nDont );
220cdf0e10cSrcweir                     if ( !pFilter )
221cdf0e10cSrcweir                         // template filter is asked for , but there isn't one; so at least the "normal" format should be detected
222cdf0e10cSrcweir                         // or storage *is* a template, but bTemplate is not set
223cdf0e10cSrcweir                         pFilter = aMatcher.GetFilter4ClipBoardId( nClipId );
224cdf0e10cSrcweir                 }
225cdf0e10cSrcweir 
226cdf0e10cSrcweir                 if ( pFilter )
227cdf0e10cSrcweir                 {
228cdf0e10cSrcweir                     if ( pFilterName )
229cdf0e10cSrcweir                         *pFilterName = pFilter->GetName();
230cdf0e10cSrcweir                     return pFilter->GetTypeName();
231cdf0e10cSrcweir                 }
232cdf0e10cSrcweir             }
233cdf0e10cSrcweir         }
234cdf0e10cSrcweir     }
235cdf0e10cSrcweir 
236cdf0e10cSrcweir     //TODO: do it without SfxFilter
237cdf0e10cSrcweir     //TODO/LATER: don't yield FilterName, should be done in FWK!
238cdf0e10cSrcweir     String aRet;
239cdf0e10cSrcweir     if ( pType )
240cdf0e10cSrcweir     {
241cdf0e10cSrcweir         aRet = String::CreateFromAscii(pType);
242cdf0e10cSrcweir         if ( pFilterName )
243cdf0e10cSrcweir             *pFilterName = aMatcher.GetFilter4EA( aRet )->GetName();
244cdf0e10cSrcweir     }
245cdf0e10cSrcweir 
246cdf0e10cSrcweir     return aRet;
247cdf0e10cSrcweir }
248