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