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 throw ( beans::UnknownPropertyException,
179 lang::WrappedTargetException,
180 uno::RuntimeException )
181 {
182 SfxFilterMatcher aMatcher;
183 const char* pType=0;
184 String aName;
185 if ( pFilterName )
186 {
187 aName = *pFilterName;
188 pFilterName->Erase();
189 }
190
191 com::sun::star::uno::Reference< com::sun::star::beans::XPropertySet > xProps( xStorage, com::sun::star::uno::UNO_QUERY );
192 if ( xProps.is() )
193 {
194 ::rtl::OUString aMediaType;
195 xProps->getPropertyValue( ::rtl::OUString::createFromAscii( "MediaType" ) ) >>= aMediaType;
196 if ( aMediaType.getLength() )
197 {
198 ::com::sun::star::datatransfer::DataFlavor aDataFlavor;
199 aDataFlavor.MimeType = aMediaType;
200 sal_uInt32 nClipId = SotExchange::GetFormat( aDataFlavor );
201 if ( nClipId )
202 {
203 SfxFilterFlags nMust = SFX_FILTER_IMPORT, nDont = SFX_FILTER_NOTINSTALLED;
204 if ( bTemplate )
205 // template filter was preselected, try to verify
206 nMust |= SFX_FILTER_TEMPLATEPATH;
207 else
208 // template filters shouldn't be detected if not explicitly asked for
209 nDont |= SFX_FILTER_TEMPLATEPATH;
210
211 const SfxFilter* pFilter = 0;
212 if ( aName.Len() )
213 // get preselected Filter if it matches the desired filter flags
214 pFilter = aMatcher.GetFilter4FilterName( aName, nMust, nDont );
215
216 if ( !pFilter || pFilter->GetFormat() != nClipId )
217 {
218 // get filter from storage MediaType
219 pFilter = aMatcher.GetFilter4ClipBoardId( nClipId, nMust, nDont );
220 if ( !pFilter )
221 // template filter is asked for , but there isn't one; so at least the "normal" format should be detected
222 // or storage *is* a template, but bTemplate is not set
223 pFilter = aMatcher.GetFilter4ClipBoardId( nClipId );
224 }
225
226 if ( pFilter )
227 {
228 if ( pFilterName )
229 *pFilterName = pFilter->GetName();
230 return pFilter->GetTypeName();
231 }
232 }
233 }
234 }
235
236 //TODO: do it without SfxFilter
237 //TODO/LATER: don't yield FilterName, should be done in FWK!
238 String aRet;
239 if ( pType )
240 {
241 aRet = String::CreateFromAscii(pType);
242 if ( pFilterName )
243 *pFilterName = aMatcher.GetFilter4EA( aRet )->GetName();
244 }
245
246 return aRet;
247 }
248