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