xref: /AOO41X/main/sc/source/ui/unoobj/scdetect.cxx (revision fd9eb3f24fa0f922bd8d9734221d874f19411b0b)
1cdf0e10cSrcweir /*************************************************************************
2cdf0e10cSrcweir  *
3cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4cdf0e10cSrcweir  *
5cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6cdf0e10cSrcweir  *
7cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8cdf0e10cSrcweir  *
9cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10cdf0e10cSrcweir  *
11cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14cdf0e10cSrcweir  *
15cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20cdf0e10cSrcweir  *
21cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25cdf0e10cSrcweir  *
26cdf0e10cSrcweir  ************************************************************************/
27cdf0e10cSrcweir 
28cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29cdf0e10cSrcweir #include "precompiled_sc.hxx"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include "scdetect.hxx"
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <framework/interaction.hxx>
34cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
35cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
36cdf0e10cSrcweir #include <com/sun/star/frame/XFrame.hpp>
37cdf0e10cSrcweir #include <com/sun/star/frame/XModel.hpp>
38cdf0e10cSrcweir #include <com/sun/star/awt/XWindow.hpp>
39cdf0e10cSrcweir #include <com/sun/star/lang/XUnoTunnel.hpp>
40cdf0e10cSrcweir #ifndef _UNOTOOLS_PROCESSFACTORY_HXX
41cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
42cdf0e10cSrcweir #endif
43cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
44cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
45cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
46cdf0e10cSrcweir #include <com/sun/star/task/XInteractionHandler.hpp>
47cdf0e10cSrcweir #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
48cdf0e10cSrcweir #include <com/sun/star/ucb/CommandAbortedException.hpp>
49cdf0e10cSrcweir #include <com/sun/star/ucb/InteractiveAppException.hpp>
50cdf0e10cSrcweir #include <com/sun/star/ucb/XContent.hpp>
51cdf0e10cSrcweir #include <com/sun/star/packages/zip/ZipIOException.hpp>
52cdf0e10cSrcweir 
53cdf0e10cSrcweir 
54cdf0e10cSrcweir #include <framework/interaction.hxx>
55cdf0e10cSrcweir 
56cdf0e10cSrcweir #ifndef _TOOLKIT_UNOHLP_HXX
57cdf0e10cSrcweir #include <toolkit/helper/vclunohelper.hxx>
58cdf0e10cSrcweir #endif
59cdf0e10cSrcweir #include <ucbhelper/simpleinteractionrequest.hxx>
60cdf0e10cSrcweir 
61cdf0e10cSrcweir #include <svtools/parhtml.hxx>
62cdf0e10cSrcweir #include <rtl/ustring.h>
63cdf0e10cSrcweir #include <rtl/logfile.hxx>
64cdf0e10cSrcweir #include <svl/itemset.hxx>
65cdf0e10cSrcweir #include <vcl/window.hxx>
66cdf0e10cSrcweir #include <svl/eitem.hxx>
67cdf0e10cSrcweir #include <svl/stritem.hxx>
68cdf0e10cSrcweir #include <tools/urlobj.hxx>
69cdf0e10cSrcweir #include <vos/mutex.hxx>
70cdf0e10cSrcweir #include <svtools/sfxecode.hxx>
71cdf0e10cSrcweir #include <svtools/ehdl.hxx>
72cdf0e10cSrcweir #include <sot/storinfo.hxx>
73cdf0e10cSrcweir #include <vcl/svapp.hxx>
74cdf0e10cSrcweir #include <sfx2/sfxsids.hrc>
75cdf0e10cSrcweir #include <sfx2/request.hxx>
76cdf0e10cSrcweir #include <sfx2/docfile.hxx>
77cdf0e10cSrcweir #include <sfx2/docfilt.hxx>
78cdf0e10cSrcweir #include <sfx2/fcontnr.hxx>
79cdf0e10cSrcweir #include <sfx2/app.hxx>
80cdf0e10cSrcweir #include <sfx2/brokenpackageint.hxx>
81cdf0e10cSrcweir #include <sot/storage.hxx>
82cdf0e10cSrcweir 
83cdf0e10cSrcweir using namespace ::com::sun::star;
84cdf0e10cSrcweir using namespace ::com::sun::star::uno;
85cdf0e10cSrcweir using namespace ::com::sun::star::io;
86cdf0e10cSrcweir using namespace ::com::sun::star::frame;
87cdf0e10cSrcweir using namespace ::com::sun::star::task;
88cdf0e10cSrcweir using namespace ::com::sun::star::beans;
89cdf0e10cSrcweir using namespace ::com::sun::star::lang;
90cdf0e10cSrcweir using namespace ::com::sun::star::ucb;
91cdf0e10cSrcweir using ::rtl::OUString;
92cdf0e10cSrcweir 
93cdf0e10cSrcweir ScFilterDetect::ScFilterDetect( const REFERENCE < ::com::sun::star::lang::XMultiServiceFactory >& /* xFactory */ )
94cdf0e10cSrcweir {
95cdf0e10cSrcweir }
96cdf0e10cSrcweir 
97cdf0e10cSrcweir ScFilterDetect::~ScFilterDetect()
98cdf0e10cSrcweir {
99cdf0e10cSrcweir }
100cdf0e10cSrcweir 
101cdf0e10cSrcweir static const sal_Char __FAR_DATA pFilterSc50[]		= "StarCalc 5.0";
102cdf0e10cSrcweir static const sal_Char __FAR_DATA pFilterSc50Temp[]	= "StarCalc 5.0 Vorlage/Template";
103cdf0e10cSrcweir static const sal_Char __FAR_DATA pFilterSc40[]		= "StarCalc 4.0";
104cdf0e10cSrcweir static const sal_Char __FAR_DATA pFilterSc40Temp[]	= "StarCalc 4.0 Vorlage/Template";
105cdf0e10cSrcweir static const sal_Char __FAR_DATA pFilterSc30[]		= "StarCalc 3.0";
106cdf0e10cSrcweir static const sal_Char __FAR_DATA pFilterSc30Temp[]	= "StarCalc 3.0 Vorlage/Template";
107cdf0e10cSrcweir static const sal_Char __FAR_DATA pFilterSc10[]		= "StarCalc 1.0";
108cdf0e10cSrcweir static const sal_Char __FAR_DATA pFilterXML[]		= "StarOffice XML (Calc)";
109cdf0e10cSrcweir static const sal_Char __FAR_DATA pFilterAscii[]		= "Text - txt - csv (StarCalc)";
110cdf0e10cSrcweir static const sal_Char __FAR_DATA pFilterLotus[]		= "Lotus";
111cdf0e10cSrcweir static const sal_Char __FAR_DATA pFilterQPro6[]		= "Quattro Pro 6.0";
112cdf0e10cSrcweir static const sal_Char __FAR_DATA pFilterExcel4[]	= "MS Excel 4.0";
113cdf0e10cSrcweir static const sal_Char __FAR_DATA pFilterEx4Temp[]	= "MS Excel 4.0 Vorlage/Template";
114cdf0e10cSrcweir static const sal_Char __FAR_DATA pFilterExcel5[]	= "MS Excel 5.0/95";
115cdf0e10cSrcweir static const sal_Char __FAR_DATA pFilterEx5Temp[]	= "MS Excel 5.0/95 Vorlage/Template";
116cdf0e10cSrcweir static const sal_Char __FAR_DATA pFilterExcel95[]	= "MS Excel 95";
117cdf0e10cSrcweir static const sal_Char __FAR_DATA pFilterEx95Temp[]	= "MS Excel 95 Vorlage/Template";
118cdf0e10cSrcweir static const sal_Char __FAR_DATA pFilterExcel97[]	= "MS Excel 97";
119cdf0e10cSrcweir static const sal_Char __FAR_DATA pFilterEx97Temp[]	= "MS Excel 97 Vorlage/Template";
120*fd9eb3f2SEike Rathke static const sal_Char __FAR_DATA pFilterExcelXML[]  = "MS Excel 2003 XML";
121cdf0e10cSrcweir static const sal_Char __FAR_DATA pFilterDBase[]		= "dBase";
122cdf0e10cSrcweir static const sal_Char __FAR_DATA pFilterDif[]		= "DIF";
123cdf0e10cSrcweir static const sal_Char __FAR_DATA pFilterSylk[]		= "SYLK";
124cdf0e10cSrcweir static const sal_Char __FAR_DATA pFilterHtml[]		= "HTML (StarCalc)";
125cdf0e10cSrcweir static const sal_Char __FAR_DATA pFilterHtmlWeb[]	= "calc_HTML_WebQuery";
126cdf0e10cSrcweir static const sal_Char __FAR_DATA pFilterRtf[]		= "Rich Text Format (StarCalc)";
127cdf0e10cSrcweir 
128cdf0e10cSrcweir 
129cdf0e10cSrcweir static sal_Bool lcl_MayBeAscii( SvStream& rStream )
130cdf0e10cSrcweir {
131cdf0e10cSrcweir     // ASCII/CSV is considered possible if there are no null bytes, or a Byte
132cdf0e10cSrcweir     // Order Mark is present, or if, for Unicode UCS2/UTF-16, all null bytes
133cdf0e10cSrcweir     // are on either even or uneven byte positions.
134cdf0e10cSrcweir 
135cdf0e10cSrcweir 	rStream.Seek(STREAM_SEEK_TO_BEGIN);
136cdf0e10cSrcweir 
137cdf0e10cSrcweir     const size_t nBufSize = 2048;
138cdf0e10cSrcweir 	sal_uInt16 aBuffer[ nBufSize ];
139cdf0e10cSrcweir     sal_uInt8* pByte = reinterpret_cast<sal_uInt8*>(aBuffer);
140cdf0e10cSrcweir 	sal_uLong nBytesRead = rStream.Read( pByte, nBufSize*2);
141cdf0e10cSrcweir 
142cdf0e10cSrcweir 	if ( nBytesRead >= 2 && (aBuffer[0] == 0xfffe || aBuffer[0] == 0xfeff) )
143cdf0e10cSrcweir 	{
144cdf0e10cSrcweir         // Unicode BOM file may contain null bytes.
145cdf0e10cSrcweir 		return sal_True;
146cdf0e10cSrcweir 	}
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 	const sal_uInt16* p = aBuffer;
149cdf0e10cSrcweir     sal_uInt16 nMask = 0xffff;
150cdf0e10cSrcweir     nBytesRead /= 2;
151cdf0e10cSrcweir 	while( nBytesRead-- && nMask )
152cdf0e10cSrcweir     {
153cdf0e10cSrcweir         sal_uInt16 nVal = *p++ & nMask;
154cdf0e10cSrcweir         if (!(nVal & 0x00ff))
155cdf0e10cSrcweir             nMask &= 0xff00;
156cdf0e10cSrcweir         if (!(nVal & 0xff00))
157cdf0e10cSrcweir             nMask &= 0x00ff;
158cdf0e10cSrcweir     }
159cdf0e10cSrcweir 
160cdf0e10cSrcweir 	return nMask != 0;
161cdf0e10cSrcweir }
162cdf0e10cSrcweir 
163*fd9eb3f2SEike Rathke static const SfxFilter* lcl_DetectExcelXML( SvStream& rStream, SfxFilterMatcher& rMatcher )
164*fd9eb3f2SEike Rathke {
165*fd9eb3f2SEike Rathke     const SfxFilter* pFound = NULL;
166*fd9eb3f2SEike Rathke     rStream.Seek(STREAM_SEEK_TO_BEGIN);
167*fd9eb3f2SEike Rathke 
168*fd9eb3f2SEike Rathke     const size_t nBufSize = 4000;
169*fd9eb3f2SEike Rathke     sal_uInt8 aBuffer[ nBufSize ];
170*fd9eb3f2SEike Rathke     sal_uLong nBytesRead = rStream.Read( aBuffer, nBufSize );
171*fd9eb3f2SEike Rathke     sal_uLong nXMLStart = 0;
172*fd9eb3f2SEike Rathke 
173*fd9eb3f2SEike Rathke     // Skip UTF-8 BOM if present.
174*fd9eb3f2SEike Rathke     // No need to handle UTF-16 etc (also rejected in XMLFilterDetect).
175*fd9eb3f2SEike Rathke     if ( nBytesRead >= 3 && aBuffer[0] == 0xEF && aBuffer[1] == 0xBB && aBuffer[2] == 0xBF )
176*fd9eb3f2SEike Rathke         nXMLStart = 3;
177*fd9eb3f2SEike Rathke 
178*fd9eb3f2SEike Rathke     if ( nBytesRead >= nXMLStart + 5 && rtl_compareMemory( aBuffer+nXMLStart, "<?xml", 5 ) == 0 )
179*fd9eb3f2SEike Rathke     {
180*fd9eb3f2SEike Rathke         // Be consistent with XMLFilterDetect service: Check for presence of "Workbook" in XML file.
181*fd9eb3f2SEike Rathke 
182*fd9eb3f2SEike Rathke         rtl::OString aTryStr( "Workbook" );
183*fd9eb3f2SEike Rathke         rtl::OString aFileString(reinterpret_cast<const sal_Char*>(aBuffer), nBytesRead);
184*fd9eb3f2SEike Rathke 
185*fd9eb3f2SEike Rathke         if (aFileString.indexOf(aTryStr) >= 0)
186*fd9eb3f2SEike Rathke             pFound = rMatcher.GetFilter4FilterName( String::CreateFromAscii(pFilterExcelXML) );
187*fd9eb3f2SEike Rathke     }
188*fd9eb3f2SEike Rathke 
189*fd9eb3f2SEike Rathke     return pFound;
190*fd9eb3f2SEike Rathke }
191*fd9eb3f2SEike Rathke 
192cdf0e10cSrcweir static sal_Bool lcl_MayBeDBase( SvStream& rStream )
193cdf0e10cSrcweir {
194cdf0e10cSrcweir     // Look for dbf marker, see connectivity/source/inc/dbase/DTable.hxx
195cdf0e10cSrcweir     // DBFType for values.
196cdf0e10cSrcweir     const sal_uInt8 nValidMarks[] = {
197cdf0e10cSrcweir         0x03, 0x04, 0x05, 0x30, 0x43, 0xB3, 0x83, 0x8b, 0x8e, 0xf5 };
198cdf0e10cSrcweir     sal_uInt8 nMark;
199cdf0e10cSrcweir     rStream.Seek(STREAM_SEEK_TO_BEGIN);
200cdf0e10cSrcweir     rStream >> nMark;
201cdf0e10cSrcweir     bool bValidMark = false;
202cdf0e10cSrcweir     for (size_t i=0; i < sizeof(nValidMarks)/sizeof(nValidMarks[0]) && !bValidMark; ++i)
203cdf0e10cSrcweir     {
204cdf0e10cSrcweir         if (nValidMarks[i] == nMark)
205cdf0e10cSrcweir             bValidMark = true;
206cdf0e10cSrcweir     }
207cdf0e10cSrcweir     if ( !bValidMark )
208cdf0e10cSrcweir         return sal_False;
209cdf0e10cSrcweir 
210cdf0e10cSrcweir     const size_t nHeaderBlockSize = 32;
211cdf0e10cSrcweir     // Empty dbf is >= 32*2+1 bytes in size.
212cdf0e10cSrcweir     const size_t nEmptyDbf = nHeaderBlockSize * 2 + 1;
213cdf0e10cSrcweir 
214cdf0e10cSrcweir 	rStream.Seek(STREAM_SEEK_TO_END);
215cdf0e10cSrcweir 	sal_uLong nSize = rStream.Tell();
216cdf0e10cSrcweir 	if ( nSize < nEmptyDbf )
217cdf0e10cSrcweir 		return sal_False;
218cdf0e10cSrcweir 
219cdf0e10cSrcweir 	// length of header starts at 8
220cdf0e10cSrcweir 	rStream.Seek(8);
221cdf0e10cSrcweir 	sal_uInt16 nHeaderLen;
222cdf0e10cSrcweir 	rStream >> nHeaderLen;
223cdf0e10cSrcweir 
224cdf0e10cSrcweir 	if ( nHeaderLen < nEmptyDbf || nSize < nHeaderLen )
225cdf0e10cSrcweir 		return sal_False;
226cdf0e10cSrcweir 
227cdf0e10cSrcweir     // Last byte of header must be 0x0d, this is how it's specified.
228cdf0e10cSrcweir     // #i9581#,#i26407# but some applications don't follow the specification
229cdf0e10cSrcweir     // and pad the header with one byte 0x00 to reach an
230cdf0e10cSrcweir     // even boundary. Some (#i88577# ) even pad more or pad using a 0x1a ^Z
231cdf0e10cSrcweir     // control character (#i8857#). This results in:
232cdf0e10cSrcweir     // Last byte of header must be 0x0d on 32 bytes boundary.
233cdf0e10cSrcweir 	sal_uInt16 nBlocks = (nHeaderLen - 1) / nHeaderBlockSize;
234cdf0e10cSrcweir 	sal_uInt8 nEndFlag = 0;
235cdf0e10cSrcweir 	while ( nBlocks > 1 && nEndFlag != 0x0d ) {
236cdf0e10cSrcweir 		rStream.Seek( nBlocks-- * nHeaderBlockSize );
237cdf0e10cSrcweir 		rStream >> nEndFlag;
238cdf0e10cSrcweir 	}
239cdf0e10cSrcweir 
240cdf0e10cSrcweir 	return ( 0x0d == nEndFlag );
241cdf0e10cSrcweir }
242cdf0e10cSrcweir 
243cdf0e10cSrcweir #if 0
244cdf0e10cSrcweir static sal_Bool lcl_IsAnyXMLFilter( const SfxFilter* pFilter )
245cdf0e10cSrcweir {
246cdf0e10cSrcweir 	if ( !pFilter )
247cdf0e10cSrcweir 		return sal_False;
248cdf0e10cSrcweir 
249cdf0e10cSrcweir 	//	sal_True for XML file or template
250cdf0e10cSrcweir 	//	(template filter has no internal name -> allow configuration key names)
251cdf0e10cSrcweir 
252cdf0e10cSrcweir 	String aName(pFilter->GetFilterName());
253cdf0e10cSrcweir 	return aName.EqualsAscii(pFilterXML) ||
254cdf0e10cSrcweir 		   aName.EqualsAscii("calc_StarOffice_XML_Calc") ||
255cdf0e10cSrcweir 		   aName.EqualsAscii("calc_StarOffice_XML_Calc_Template");
256cdf0e10cSrcweir }
257cdf0e10cSrcweir #endif
258cdf0e10cSrcweir 
259cdf0e10cSrcweir ::rtl::OUString SAL_CALL ScFilterDetect::detect( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& lDescriptor ) throw( ::com::sun::star::uno::RuntimeException )
260cdf0e10cSrcweir {
261cdf0e10cSrcweir     REFERENCE< XInputStream > xStream;
262cdf0e10cSrcweir     REFERENCE< XContent > xContent;
263cdf0e10cSrcweir     REFERENCE< XInteractionHandler > xInteraction;
264cdf0e10cSrcweir     String aURL;
265cdf0e10cSrcweir 	::rtl::OUString sTemp;
266cdf0e10cSrcweir     String aTypeName;            // a name describing the type (from MediaDescriptor, usually from flat detection)
267cdf0e10cSrcweir     String aPreselectedFilterName;      // a name describing the filter to use (from MediaDescriptor, usually from UI action)
268cdf0e10cSrcweir 
269cdf0e10cSrcweir 	::rtl::OUString aDocumentTitle; // interesting only if set in this method
270cdf0e10cSrcweir 
271cdf0e10cSrcweir 	// opening as template is done when a parameter tells to do so and a template filter can be detected
272cdf0e10cSrcweir     // (otherwise no valid filter would be found) or if the detected filter is a template filter and
273cdf0e10cSrcweir 	// there is no parameter that forbids to open as template
274cdf0e10cSrcweir 	sal_Bool bOpenAsTemplate = sal_False;
275cdf0e10cSrcweir     sal_Bool bWasReadOnly = sal_False, bReadOnly = sal_False;
276cdf0e10cSrcweir 
277cdf0e10cSrcweir 	sal_Bool bRepairPackage = sal_False;
278cdf0e10cSrcweir 	sal_Bool bRepairAllowed = sal_False;
279cdf0e10cSrcweir 
280cdf0e10cSrcweir 	// now some parameters that can already be in the array, but may be overwritten or new inserted here
281cdf0e10cSrcweir 	// remember their indices in the case new values must be added to the array
282cdf0e10cSrcweir 	sal_Int32 nPropertyCount = lDescriptor.getLength();
283cdf0e10cSrcweir     sal_Int32 nIndexOfFilterName = -1;
284cdf0e10cSrcweir     sal_Int32 nIndexOfInputStream = -1;
285cdf0e10cSrcweir     sal_Int32 nIndexOfContent = -1;
286cdf0e10cSrcweir     sal_Int32 nIndexOfReadOnlyFlag = -1;
287cdf0e10cSrcweir     sal_Int32 nIndexOfTemplateFlag = -1;
288cdf0e10cSrcweir     sal_Int32 nIndexOfDocumentTitle = -1;
289cdf0e10cSrcweir     bool bFakeXLS = false;
290cdf0e10cSrcweir 
291cdf0e10cSrcweir     for( sal_Int32 nProperty=0; nProperty<nPropertyCount; ++nProperty )
292cdf0e10cSrcweir 	{
293cdf0e10cSrcweir         // extract properties
294cdf0e10cSrcweir         if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("URL")) )
295cdf0e10cSrcweir 		{
296cdf0e10cSrcweir 			lDescriptor[nProperty].Value >>= sTemp;
297cdf0e10cSrcweir 			aURL = sTemp;
298cdf0e10cSrcweir 		}
299cdf0e10cSrcweir         else if( !aURL.Len() && lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("FileName")) )
300cdf0e10cSrcweir 		{
301cdf0e10cSrcweir 			lDescriptor[nProperty].Value >>= sTemp;
302cdf0e10cSrcweir 			aURL = sTemp;
303cdf0e10cSrcweir 		}
304cdf0e10cSrcweir         else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("TypeName")) )
305cdf0e10cSrcweir 		{
306cdf0e10cSrcweir 			lDescriptor[nProperty].Value >>= sTemp;
307cdf0e10cSrcweir             aTypeName = sTemp;
308cdf0e10cSrcweir 		}
309cdf0e10cSrcweir         else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("FilterName")) )
310cdf0e10cSrcweir 		{
311cdf0e10cSrcweir 			lDescriptor[nProperty].Value >>= sTemp;
312cdf0e10cSrcweir             aPreselectedFilterName = sTemp;
313cdf0e10cSrcweir 
314cdf0e10cSrcweir             // if the preselected filter name is not correct, it must be erased after detection
315cdf0e10cSrcweir             // remember index of property to get access to it later
316cdf0e10cSrcweir             nIndexOfFilterName = nProperty;
317cdf0e10cSrcweir 		}
318cdf0e10cSrcweir         else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("InputStream")) )
319cdf0e10cSrcweir             nIndexOfInputStream = nProperty;
320cdf0e10cSrcweir         else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("ReadOnly")) )
321cdf0e10cSrcweir             nIndexOfReadOnlyFlag = nProperty;
322cdf0e10cSrcweir         else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("UCBContent")) )
323cdf0e10cSrcweir             nIndexOfContent = nProperty;
324cdf0e10cSrcweir         else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("AsTemplate")) )
325cdf0e10cSrcweir 		{
326cdf0e10cSrcweir 			lDescriptor[nProperty].Value >>= bOpenAsTemplate;
327cdf0e10cSrcweir             nIndexOfTemplateFlag = nProperty;
328cdf0e10cSrcweir 		}
329cdf0e10cSrcweir         else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("InteractionHandler")) )
330cdf0e10cSrcweir             lDescriptor[nProperty].Value >>= xInteraction;
331cdf0e10cSrcweir         else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("RepairPackage")) )
332cdf0e10cSrcweir             lDescriptor[nProperty].Value >>= bRepairPackage;
333cdf0e10cSrcweir         else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("DocumentTitle")) )
334cdf0e10cSrcweir             nIndexOfDocumentTitle = nProperty;
335cdf0e10cSrcweir 	}
336cdf0e10cSrcweir 
337cdf0e10cSrcweir     // can't check the type for external filters, so set the "dont" flag accordingly
338cdf0e10cSrcweir     ::vos::OGuard aGuard( Application::GetSolarMutex() );
339cdf0e10cSrcweir     //SfxFilterFlags nMust = SFX_FILTER_IMPORT, nDont = SFX_FILTER_NOTINSTALLED;
340cdf0e10cSrcweir 
341cdf0e10cSrcweir     SfxAllItemSet *pSet = new SfxAllItemSet( SFX_APP()->GetPool() );
342cdf0e10cSrcweir     TransformParameters( SID_OPENDOC, lDescriptor, *pSet );
343cdf0e10cSrcweir     SFX_ITEMSET_ARG( pSet, pItem, SfxBoolItem, SID_DOC_READONLY, sal_False );
344cdf0e10cSrcweir 
345cdf0e10cSrcweir     bWasReadOnly = pItem && pItem->GetValue();
346cdf0e10cSrcweir 
347cdf0e10cSrcweir 	const SfxFilter* pFilter = 0;
348cdf0e10cSrcweir 	String aPrefix = String::CreateFromAscii( "private:factory/" );
349cdf0e10cSrcweir 	if( aURL.Match( aPrefix ) == aPrefix.Len() )
350cdf0e10cSrcweir 	{
351cdf0e10cSrcweir 		String aPattern( aPrefix );
352cdf0e10cSrcweir 		aPattern += String::CreateFromAscii("scalc");
353cdf0e10cSrcweir 		if ( aURL.Match( aPattern ) >= aPattern.Len() )
354cdf0e10cSrcweir 			pFilter = SfxFilter::GetDefaultFilterFromFactory( aURL );
355cdf0e10cSrcweir 	}
356cdf0e10cSrcweir 	else
357cdf0e10cSrcweir 	{
358cdf0e10cSrcweir 		// container for Calc filters
359cdf0e10cSrcweir 		SfxFilterMatcher aMatcher( String::CreateFromAscii("scalc") );
360cdf0e10cSrcweir 		if ( aPreselectedFilterName.Len() )
361cdf0e10cSrcweir 			pFilter = SfxFilter::GetFilterByName( aPreselectedFilterName );
362cdf0e10cSrcweir 		else if( aTypeName.Len() )
363cdf0e10cSrcweir 			pFilter = aMatcher.GetFilter4EA( aTypeName );
364cdf0e10cSrcweir 
365cdf0e10cSrcweir 	    // ctor of SfxMedium uses owner transition of ItemSet
366cdf0e10cSrcweir 	    SfxMedium aMedium( aURL, bWasReadOnly ? STREAM_STD_READ : STREAM_STD_READWRITE, sal_False, NULL, pSet );
367cdf0e10cSrcweir 	    aMedium.UseInteractionHandler( sal_True );
368cdf0e10cSrcweir 
369cdf0e10cSrcweir 	    sal_Bool bIsStorage = aMedium.IsStorage();
370cdf0e10cSrcweir 	    if ( aMedium.GetErrorCode() == ERRCODE_NONE )
371cdf0e10cSrcweir 	    {
372cdf0e10cSrcweir 	        // remember input stream and content and put them into the descriptor later
373cdf0e10cSrcweir 			// should be done here since later the medium can switch to a version
374cdf0e10cSrcweir 	        xStream.set(aMedium.GetInputStream());
375cdf0e10cSrcweir 			xContent.set(aMedium.GetContent());
376cdf0e10cSrcweir         	bReadOnly = aMedium.IsReadOnly();
377cdf0e10cSrcweir 
378cdf0e10cSrcweir 	        // maybe that IsStorage() already created an error!
379cdf0e10cSrcweir 	        if ( bIsStorage )
380cdf0e10cSrcweir 			{
381cdf0e10cSrcweir                 uno::Reference < embed::XStorage > xStorage(aMedium.GetStorage( sal_False ));
382cdf0e10cSrcweir 				if ( aMedium.GetLastStorageCreationState() != ERRCODE_NONE )
383cdf0e10cSrcweir 				{
384cdf0e10cSrcweir 					// error during storage creation means _here_ that the medium
385cdf0e10cSrcweir 					// is broken, but we can not handle it in medium since unpossibility
386cdf0e10cSrcweir 					// to create a storage does not _always_ means that the medium is broken
387cdf0e10cSrcweir 					aMedium.SetError( aMedium.GetLastStorageCreationState(), ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ) );
388cdf0e10cSrcweir 					if ( xInteraction.is() )
389cdf0e10cSrcweir 					{
390cdf0e10cSrcweir 						OUString empty;
391cdf0e10cSrcweir 						try
392cdf0e10cSrcweir 						{
393cdf0e10cSrcweir 							InteractiveAppException xException( empty,
394cdf0e10cSrcweir 															REFERENCE< XInterface >(),
395cdf0e10cSrcweir 															InteractionClassification_ERROR,
396cdf0e10cSrcweir 															aMedium.GetError() );
397cdf0e10cSrcweir 
398cdf0e10cSrcweir                             REFERENCE< XInteractionRequest > xRequest(
399cdf0e10cSrcweir 								new ucbhelper::SimpleInteractionRequest( makeAny( xException ),
400cdf0e10cSrcweir 																 	 ucbhelper::CONTINUATION_APPROVE ) );
401cdf0e10cSrcweir 							xInteraction->handle( xRequest );
402cdf0e10cSrcweir 						}
403cdf0e10cSrcweir 						catch ( Exception & ) {};
404cdf0e10cSrcweir 					}
405cdf0e10cSrcweir 				}
406cdf0e10cSrcweir                 else if ( xStorage.is() )
407cdf0e10cSrcweir 				{
408cdf0e10cSrcweir 					try
409cdf0e10cSrcweir 					{
410cdf0e10cSrcweir                         String aFilterName;
411cdf0e10cSrcweir                         if ( pFilter )
412cdf0e10cSrcweir                             aFilterName = pFilter->GetName();
413cdf0e10cSrcweir                         aTypeName = SfxFilter::GetTypeFromStorage( xStorage, pFilter ? pFilter->IsOwnTemplateFormat() : sal_False, &aFilterName );
414cdf0e10cSrcweir 					}
415cdf0e10cSrcweir 					catch( lang::WrappedTargetException& aWrap )
416cdf0e10cSrcweir 					{
417cdf0e10cSrcweir 						packages::zip::ZipIOException aZipException;
418cdf0e10cSrcweir 
419cdf0e10cSrcweir 						// repairing is done only if this type is requested from outside
420cdf0e10cSrcweir 						if ( ( aWrap.TargetException >>= aZipException ) && aTypeName.Len() )
421cdf0e10cSrcweir 						{
422cdf0e10cSrcweir 							if ( xInteraction.is() )
423cdf0e10cSrcweir 							{
424cdf0e10cSrcweir 								// the package is broken one
425cdf0e10cSrcweir        							aDocumentTitle = aMedium.GetURLObject().getName(
426cdf0e10cSrcweir 															INetURLObject::LAST_SEGMENT,
427cdf0e10cSrcweir 															true,
428cdf0e10cSrcweir 															INetURLObject::DECODE_WITH_CHARSET );
429cdf0e10cSrcweir 
430cdf0e10cSrcweir 								if ( !bRepairPackage )
431cdf0e10cSrcweir 								{
432cdf0e10cSrcweir                                     // ask the user whether he wants to try to repair
433cdf0e10cSrcweir                                     RequestPackageReparation aRequest( aDocumentTitle );
434cdf0e10cSrcweir                                     xInteraction->handle( aRequest.GetRequest() );
435cdf0e10cSrcweir                                     bRepairAllowed = aRequest.isApproved();
436cdf0e10cSrcweir 								}
437cdf0e10cSrcweir 
438cdf0e10cSrcweir 								if ( !bRepairAllowed )
439cdf0e10cSrcweir 								{
440cdf0e10cSrcweir 									// repair either not allowed or not successful
441cdf0e10cSrcweir                                     NotifyBrokenPackage aNotifyRequest( aDocumentTitle );
442cdf0e10cSrcweir                                     xInteraction->handle( aNotifyRequest.GetRequest() );
443cdf0e10cSrcweir 								}
444cdf0e10cSrcweir 							}
445cdf0e10cSrcweir 
446cdf0e10cSrcweir 							if ( !bRepairAllowed )
447cdf0e10cSrcweir 								aTypeName.Erase();
448cdf0e10cSrcweir 						}
449cdf0e10cSrcweir 					}
450cdf0e10cSrcweir 					catch( uno::RuntimeException& )
451cdf0e10cSrcweir 					{
452cdf0e10cSrcweir 						throw;
453cdf0e10cSrcweir 					}
454cdf0e10cSrcweir 					catch( uno::Exception& )
455cdf0e10cSrcweir 					{
456cdf0e10cSrcweir 						aTypeName.Erase();
457cdf0e10cSrcweir 					}
458cdf0e10cSrcweir 
459cdf0e10cSrcweir                    	if ( aTypeName.Len() )
460cdf0e10cSrcweir                        	pFilter = SfxFilterMatcher( String::CreateFromAscii("scalc") ).GetFilter4EA( aTypeName );
461cdf0e10cSrcweir 
462cdf0e10cSrcweir 				}
463cdf0e10cSrcweir 			}
464cdf0e10cSrcweir 			else
465cdf0e10cSrcweir 			{
466cdf0e10cSrcweir                 bool bIsXLS = false;
467cdf0e10cSrcweir                 SvStream* pStream = aMedium.GetInStream();
468cdf0e10cSrcweir                 const SfxFilter* pPreselectedFilter = pFilter;
469cdf0e10cSrcweir                 if ( pPreselectedFilter && pPreselectedFilter->GetName().SearchAscii("Excel") != STRING_NOTFOUND )
470cdf0e10cSrcweir                     bIsXLS = true;
471cdf0e10cSrcweir                 pFilter = 0;
472cdf0e10cSrcweir                 if ( pStream )
473cdf0e10cSrcweir                 {
474cdf0e10cSrcweir                     SotStorageRef aStorage = new SotStorage ( pStream, sal_False );
475cdf0e10cSrcweir                     if ( !aStorage->GetError() )
476cdf0e10cSrcweir                     {
477cdf0e10cSrcweir                         // Excel-5: detect through contained streams
478cdf0e10cSrcweir                         // there are some "excel" formats from 3rd party vendors that need to be distinguished
479cdf0e10cSrcweir                         String aStreamName(RTL_CONSTASCII_STRINGPARAM("Workbook"));
480cdf0e10cSrcweir                         sal_Bool bExcel97Stream = ( aStorage->IsStream( aStreamName ) );
481cdf0e10cSrcweir 
482cdf0e10cSrcweir                         aStreamName = String(RTL_CONSTASCII_STRINGPARAM("Book"));
483cdf0e10cSrcweir                         sal_Bool bExcel5Stream = ( aStorage->IsStream( aStreamName ) );
484cdf0e10cSrcweir                         if ( bExcel97Stream || bExcel5Stream )
485cdf0e10cSrcweir                         {
486cdf0e10cSrcweir                             if ( bExcel97Stream )
487cdf0e10cSrcweir                             {
488cdf0e10cSrcweir                                 String aOldName;
489cdf0e10cSrcweir                                 sal_Bool bIsCalcFilter = sal_True;
490cdf0e10cSrcweir                                 if ( pPreselectedFilter )
491cdf0e10cSrcweir                                 {
492cdf0e10cSrcweir                                     // cross filter; now this should be a type detection only, not a filter detection
493cdf0e10cSrcweir                                     // we can simulate it by preserving the preselected filter if the type matches
494cdf0e10cSrcweir                                     // example: Excel filters for Writer
495cdf0e10cSrcweir                                     aOldName = pPreselectedFilter->GetFilterName();
496cdf0e10cSrcweir                                     bIsCalcFilter = pPreselectedFilter->GetServiceName().EqualsAscii("com.sun.star.sheet.SpreadsheetDocument");
497cdf0e10cSrcweir                                 }
498cdf0e10cSrcweir 
499cdf0e10cSrcweir                                 if ( aOldName.EqualsAscii(pFilterEx97Temp) || !bIsCalcFilter )
500cdf0e10cSrcweir                                 {
501cdf0e10cSrcweir                                     //  Excel 97 template selected -> keep selection
502cdf0e10cSrcweir                                 }
503cdf0e10cSrcweir                                 else if ( bExcel5Stream &&
504cdf0e10cSrcweir                                             ( aOldName.EqualsAscii(pFilterExcel5) || aOldName.EqualsAscii(pFilterEx5Temp) ||
505cdf0e10cSrcweir                                             aOldName.EqualsAscii(pFilterExcel95) || aOldName.EqualsAscii(pFilterEx95Temp) ) )
506cdf0e10cSrcweir                                 {
507cdf0e10cSrcweir                                     //  dual format file and Excel 5 selected -> keep selection
508cdf0e10cSrcweir                                 }
509cdf0e10cSrcweir                                 else
510cdf0e10cSrcweir                                 {
511cdf0e10cSrcweir                                     //  else use Excel 97 filter
512cdf0e10cSrcweir                                     pFilter = aMatcher.GetFilter4FilterName( String::CreateFromAscii(pFilterExcel97) );
513cdf0e10cSrcweir                                 }
514cdf0e10cSrcweir                             }
515cdf0e10cSrcweir                             else if ( bExcel5Stream )
516cdf0e10cSrcweir                             {
517cdf0e10cSrcweir                                 String aOldName;
518cdf0e10cSrcweir                                 sal_Bool bIsCalcFilter = sal_True;
519cdf0e10cSrcweir                                 if ( pPreselectedFilter )
520cdf0e10cSrcweir                                 {
521cdf0e10cSrcweir                                     // cross filter; now this should be a type detection only, not a filter detection
522cdf0e10cSrcweir                                     // we can simulate it by preserving the preselected filter if the type matches
523cdf0e10cSrcweir                                     // example: Excel filters for Writer
524cdf0e10cSrcweir                                     aOldName = pPreselectedFilter->GetFilterName();
525cdf0e10cSrcweir                                     bIsCalcFilter = pPreselectedFilter->GetServiceName().EqualsAscii("com.sun.star.sheet.SpreadsheetDocument");
526cdf0e10cSrcweir                                 }
527cdf0e10cSrcweir 
528cdf0e10cSrcweir                                 if ( aOldName.EqualsAscii(pFilterExcel95) || aOldName.EqualsAscii(pFilterEx95Temp) ||
529cdf0e10cSrcweir                                         aOldName.EqualsAscii(pFilterEx5Temp) || !bIsCalcFilter )
530cdf0e10cSrcweir                                 {
531cdf0e10cSrcweir                                     //  Excel 95 oder Vorlage (5 oder 95) eingestellt -> auch gut
532cdf0e10cSrcweir                                 }
533cdf0e10cSrcweir                                 else if ( aOldName.EqualsAscii(pFilterEx97Temp) )
534cdf0e10cSrcweir                                 {
535cdf0e10cSrcweir                                     // #101923# auto detection has found template -> return Excel5 template
536cdf0e10cSrcweir                                     pFilter = aMatcher.GetFilter4FilterName( String::CreateFromAscii(pFilterEx5Temp) );
537cdf0e10cSrcweir                                 }
538cdf0e10cSrcweir                                 else
539cdf0e10cSrcweir                                 {
540cdf0e10cSrcweir                                     //  sonst wird als Excel 5-Datei erkannt
541cdf0e10cSrcweir                                     pFilter = aMatcher.GetFilter4FilterName( String::CreateFromAscii(pFilterExcel5) );
542cdf0e10cSrcweir                                 }
543cdf0e10cSrcweir                             }
544cdf0e10cSrcweir                         }
545cdf0e10cSrcweir                     }
546cdf0e10cSrcweir                     else
547cdf0e10cSrcweir                     {
548cdf0e10cSrcweir                         SvStream &rStr = *pStream;
549cdf0e10cSrcweir 
550cdf0e10cSrcweir                         // Tabelle mit Suchmustern
551cdf0e10cSrcweir                         // Bedeutung der Sequenzen
552cdf0e10cSrcweir                         // 0x00??: genau Byte 0x?? muss an dieser Stelle stehen
553cdf0e10cSrcweir                         // 0x0100: ein Byte ueberlesen (don't care)
554cdf0e10cSrcweir                         // 0x02nn: ein Byte aus 0xnn Alternativen folgt
555cdf0e10cSrcweir                         // 0x8000: Erkennung abgeschlossen
556cdf0e10cSrcweir                         //
557cdf0e10cSrcweir 
558cdf0e10cSrcweir         #define M_DC        0x0100
559cdf0e10cSrcweir         #define M_ALT(ANZ)  (0x0200+(ANZ))
560cdf0e10cSrcweir         #define M_ENDE      0x8000
561cdf0e10cSrcweir 
562cdf0e10cSrcweir                         static const sal_uInt16 pLotus[] =      // Lotus 1/1A/2
563cdf0e10cSrcweir                             { 0x0000, 0x0000, 0x0002, 0x0000,
564cdf0e10cSrcweir                             M_ALT(2), 0x0004, 0x0006,
565cdf0e10cSrcweir                             0x0004, M_ENDE };
566cdf0e10cSrcweir 
567cdf0e10cSrcweir                         static const sal_uInt16 pLotusNew[] =   // Lotus >= 9.7
568cdf0e10cSrcweir                             { 0x0000, 0x0000, M_DC, 0x0000,     // Rec# + Len (0x1a)
569cdf0e10cSrcweir                               M_ALT(3), 0x0003, 0x0004, 0x0005, // File Revision Code 97->ME
570cdf0e10cSrcweir                               0x0010, 0x0004, 0x0000, 0x0000,
571cdf0e10cSrcweir                               M_ENDE };
572cdf0e10cSrcweir 
573cdf0e10cSrcweir                         static const sal_uInt16 pExcel1[] =     // Excel BIFF2, BIFF3, BIFF4
574cdf0e10cSrcweir                             {   0x09,                                   // lobyte of BOF rec ID (0x0009, 0x0209, 0x0409)
575cdf0e10cSrcweir                                 M_ALT(3), 0x00, 0x02, 0x04,             // hibyte of BOF rec ID (0x0009, 0x0209, 0x0409)
576cdf0e10cSrcweir                                 M_ALT(3), 4, 6, 8,                      // lobyte of BOF rec size (4, 6, 8, 16)
577cdf0e10cSrcweir                                 0x00,                                   // hibyte of BOF rec size (4, 6, 8, 16)
578cdf0e10cSrcweir                                 M_DC, M_DC,                             // any version
579cdf0e10cSrcweir                                 M_ALT(3), 0x10, 0x20, 0x40,             // lobyte of data type (0x0010, 0x0020, 0x0040)
580cdf0e10cSrcweir                                 0x00,                                   // hibyte of data type (0x0010, 0x0020, 0x0040)
581cdf0e10cSrcweir                                 M_ENDE };
582cdf0e10cSrcweir 
583cdf0e10cSrcweir                         static const sal_uInt16 pExcel2[] =     // Excel BIFF4 Workspace
584cdf0e10cSrcweir                             {   0x09,                                   // lobyte of BOF rec ID (0x0409)
585cdf0e10cSrcweir                                 0x04,                                   // hibyte of BOF rec ID (0x0409)
586cdf0e10cSrcweir                                 M_ALT(3), 4, 6, 8,                      // lobyte of BOF rec size (4, 6, 8, 16)
587cdf0e10cSrcweir                                 0x00,                                   // hibyte of BOF rec size (4, 6, 8, 16)
588cdf0e10cSrcweir                                 M_DC, M_DC,                             // any version
589cdf0e10cSrcweir                                 0x00,                                   // lobyte of data type (0x0100)
590cdf0e10cSrcweir                                 0x01,                                   // hibyte of data type (0x0100)
591cdf0e10cSrcweir                                 M_ENDE };
592cdf0e10cSrcweir 
593cdf0e10cSrcweir                         static const sal_uInt16 pExcel3[] =     // #i23425# Excel BIFF5, BIFF7, BIFF8 (simple book stream)
594cdf0e10cSrcweir                             {   0x09,                                   // lobyte of BOF rec ID (0x0809)
595cdf0e10cSrcweir                                 0x08,                                   // hibyte of BOF rec ID (0x0809)
596cdf0e10cSrcweir                                 M_ALT(4), 4, 6, 8, 16,                  // lobyte of BOF rec size
597cdf0e10cSrcweir                                 0x00,                                   // hibyte of BOF rec size
598cdf0e10cSrcweir                                 M_DC, M_DC,                             // any version
599cdf0e10cSrcweir                                 M_ALT(5), 0x05, 0x06, 0x10, 0x20, 0x40, // lobyte of data type
600cdf0e10cSrcweir                                 0x00,                                   // hibyte of data type
601cdf0e10cSrcweir                                 M_ENDE };
602cdf0e10cSrcweir 
603cdf0e10cSrcweir                         static const sal_uInt16 pSc10[] =       // StarCalc 1.0 Dokumente
604cdf0e10cSrcweir                             { 'B', 'l', 'a', 'i', 's', 'e', '-', 'T', 'a', 'b', 'e', 'l', 'l',
605cdf0e10cSrcweir                             'e', 0x000A, 0x000D, 0x0000,    // Sc10CopyRight[16]
606cdf0e10cSrcweir                             M_DC, M_DC, M_DC, M_DC, M_DC, M_DC, M_DC, M_DC, M_DC, M_DC, M_DC,
607cdf0e10cSrcweir                             M_DC, M_DC,                   // Sc10CopyRight[29]
608cdf0e10cSrcweir                             M_ALT(2), 0x0065, 0x0066,     // Versionsnummer 101 oder 102
609cdf0e10cSrcweir                             0x0000,
610cdf0e10cSrcweir                             M_ENDE };
611cdf0e10cSrcweir 
612cdf0e10cSrcweir                         static const sal_uInt16 pLotus2[] =     // Lotus >3
613cdf0e10cSrcweir                             { 0x0000, 0x0000, 0x001A, 0x0000,   // Rec# + Len (26)
614cdf0e10cSrcweir                             M_ALT(2), 0x0000, 0x0002,         // File Revision Code
615cdf0e10cSrcweir                             0x0010,
616cdf0e10cSrcweir                             0x0004, 0x0000,                   // File Revision Subcode
617cdf0e10cSrcweir                             M_ENDE };
618cdf0e10cSrcweir 
619cdf0e10cSrcweir                         static const sal_uInt16 pQPro[] =
620cdf0e10cSrcweir                                { 0x0000, 0x0000, 0x0002, 0x0000,
621cdf0e10cSrcweir                                  M_ALT(4), 0x0001, 0x0002, // WB1, WB2
622cdf0e10cSrcweir                                  0x0006, 0x0007,           // QPro 6/7 (?)
623cdf0e10cSrcweir                                  0x0010,
624cdf0e10cSrcweir                                  M_ENDE };
625cdf0e10cSrcweir 
626cdf0e10cSrcweir                         static const sal_uInt16 pDIF1[] =       // DIF mit CR-LF
627cdf0e10cSrcweir                             {
628cdf0e10cSrcweir                             'T', 'A', 'B', 'L', 'E',
629cdf0e10cSrcweir                             M_DC, M_DC,
630cdf0e10cSrcweir                             '0', ',', '1',
631cdf0e10cSrcweir                             M_DC, M_DC,
632cdf0e10cSrcweir                             '\"',
633cdf0e10cSrcweir                             M_ENDE };
634cdf0e10cSrcweir 
635cdf0e10cSrcweir                         static const sal_uInt16 pDIF2[] =       // DIF mit CR oder LF
636cdf0e10cSrcweir                             {
637cdf0e10cSrcweir                             'T', 'A', 'B', 'L', 'E',
638cdf0e10cSrcweir                             M_DC,
639cdf0e10cSrcweir                             '0', ',', '1',
640cdf0e10cSrcweir                             M_DC,
641cdf0e10cSrcweir                             '\"',
642cdf0e10cSrcweir                             M_ENDE };
643cdf0e10cSrcweir 
644cdf0e10cSrcweir                         static const sal_uInt16 pSylk[] =       // Sylk
645cdf0e10cSrcweir                             {
646cdf0e10cSrcweir                             'I', 'D', ';',
647cdf0e10cSrcweir                             M_ALT(3), 'P', 'N', 'E',        // 'P' plus undocumented Excel extensions 'N' and 'E'
648cdf0e10cSrcweir                             M_ENDE };
649cdf0e10cSrcweir 
650cdf0e10cSrcweir                         static const sal_uInt16 *ppFilterPatterns[] =      // Arrays mit Suchmustern
651cdf0e10cSrcweir                             {
652cdf0e10cSrcweir                             pLotus,
653cdf0e10cSrcweir                             pExcel1,
654cdf0e10cSrcweir                             pExcel2,
655cdf0e10cSrcweir                             pExcel3,
656cdf0e10cSrcweir                             pSc10,
657cdf0e10cSrcweir                             pDIF1,
658cdf0e10cSrcweir                             pDIF2,
659cdf0e10cSrcweir                             pSylk,
660cdf0e10cSrcweir                             pLotusNew,
661cdf0e10cSrcweir                             pLotus2,
662cdf0e10cSrcweir                             pQPro
663cdf0e10cSrcweir                             };
664cdf0e10cSrcweir                         const sal_uInt16 nFilterCount = sizeof(ppFilterPatterns) / sizeof(ppFilterPatterns[0]);
665cdf0e10cSrcweir 
666cdf0e10cSrcweir                         static const sal_Char* const pFilterName[] =     // zugehoerige Filter
667cdf0e10cSrcweir                             {
668cdf0e10cSrcweir                             pFilterLotus,
669cdf0e10cSrcweir                             pFilterExcel4,
670cdf0e10cSrcweir                             pFilterExcel4,
671cdf0e10cSrcweir                             pFilterExcel4,
672cdf0e10cSrcweir                             pFilterSc10,
673cdf0e10cSrcweir                             pFilterDif,
674cdf0e10cSrcweir                             pFilterDif,
675cdf0e10cSrcweir                             pFilterSylk,
676cdf0e10cSrcweir                             pFilterLotus,
677cdf0e10cSrcweir                             pFilterLotus,
678cdf0e10cSrcweir                             pFilterQPro6
679cdf0e10cSrcweir                             };
680cdf0e10cSrcweir 
681cdf0e10cSrcweir                         // const sal_uInt16 nByteMask = 0xFF;
682cdf0e10cSrcweir 
683cdf0e10cSrcweir                         // suchen Sie jetzt!
684cdf0e10cSrcweir                         // ... realisiert ueber 'Mustererkennung'
685cdf0e10cSrcweir 
686cdf0e10cSrcweir                         sal_uInt8            nAkt;
687cdf0e10cSrcweir                         sal_Bool            bSync;          // Datei und Muster stimmen ueberein
688cdf0e10cSrcweir                         sal_uInt16          nFilter;        // Zaehler ueber alle Filter
689cdf0e10cSrcweir                         const sal_uInt16    *pSearch;       // aktuelles Musterwort
690cdf0e10cSrcweir 
691cdf0e10cSrcweir                         for ( nFilter = 0 ; nFilter < nFilterCount ; nFilter++ )
692cdf0e10cSrcweir                         {
693cdf0e10cSrcweir                             rStr.Seek( 0 ); // am Anfang war alles Uebel...
694cdf0e10cSrcweir                             rStr >> nAkt;
695cdf0e10cSrcweir                             pSearch = ppFilterPatterns[ nFilter ];
696cdf0e10cSrcweir                             bSync = sal_True;
697cdf0e10cSrcweir                             while( !rStr.IsEof() && bSync )
698cdf0e10cSrcweir                             {
699cdf0e10cSrcweir                                 register sal_uInt16 nMuster = *pSearch;
700cdf0e10cSrcweir 
701cdf0e10cSrcweir                                 if( nMuster < 0x0100 )
702cdf0e10cSrcweir                                 { //                                direkter Byte-Vergleich
703cdf0e10cSrcweir                                     if( ( sal_uInt8 ) nMuster != nAkt )
704cdf0e10cSrcweir                                         bSync = sal_False;
705cdf0e10cSrcweir                                 }
706cdf0e10cSrcweir                                 else if( nMuster & M_DC )
707cdf0e10cSrcweir                                 { //                                             don't care
708cdf0e10cSrcweir                                 }
709cdf0e10cSrcweir                                 else if( nMuster & M_ALT(0) )
710cdf0e10cSrcweir                                 { //                                      alternative Bytes
711cdf0e10cSrcweir                                     sal_uInt8 nAnzAlt = ( sal_uInt8 ) nMuster;
712cdf0e10cSrcweir                                     bSync = sal_False;          // zunaechst unsynchron
713cdf0e10cSrcweir                                     while( nAnzAlt > 0 )
714cdf0e10cSrcweir                                     {
715cdf0e10cSrcweir                                         pSearch++;
716cdf0e10cSrcweir                                         if( ( sal_uInt8 ) *pSearch == nAkt )
717cdf0e10cSrcweir                                             bSync = sal_True;   // jetzt erst Synchronisierung
718cdf0e10cSrcweir                                         nAnzAlt--;
719cdf0e10cSrcweir                                     }
720cdf0e10cSrcweir                                 }
721cdf0e10cSrcweir                                 else if( nMuster & M_ENDE )
722cdf0e10cSrcweir                                 { //                                        Format detected
723cdf0e10cSrcweir                                     if ( pFilterName[nFilter] == pFilterExcel4 && pPreselectedFilter &&
724cdf0e10cSrcweir                                         ( (pPreselectedFilter)->GetFilterName().EqualsAscii(pFilterEx4Temp) || pPreselectedFilter->GetTypeName().EqualsAscii("calc_MS_Excel_40") ) )
725cdf0e10cSrcweir                                     {
726cdf0e10cSrcweir                                         //  Excel 4 erkannt, Excel 4 Vorlage eingestellt -> auch gut
727cdf0e10cSrcweir                                         // oder Excel 4 Filter anderer Applikation (simulated type detection!)
728cdf0e10cSrcweir                                     }
729cdf0e10cSrcweir                                     else
730cdf0e10cSrcweir                                     {   // gefundenen Filter einstellen
731cdf0e10cSrcweir                                         pFilter = aMatcher.GetFilter4FilterName( String::CreateFromAscii(pFilterName[ nFilter ]) );
732cdf0e10cSrcweir                                     }
733cdf0e10cSrcweir                                     bSync = sal_False;              // leave inner loop
734cdf0e10cSrcweir                                     nFilter = nFilterCount;     // leave outer loop
735cdf0e10cSrcweir                                 }
736cdf0e10cSrcweir                                 else
737cdf0e10cSrcweir                                 { //                                         Tabellenfehler
738cdf0e10cSrcweir                                     DBG_ERROR( "-ScApplication::DetectFilter(): Fehler in Mustertabelle");
739cdf0e10cSrcweir                                 }
740cdf0e10cSrcweir 
741cdf0e10cSrcweir                                 pSearch++;
742cdf0e10cSrcweir                                 rStr >> nAkt;
743cdf0e10cSrcweir                             }
744cdf0e10cSrcweir                         }
745cdf0e10cSrcweir 
746cdf0e10cSrcweir                         if ( pPreselectedFilter && !pFilter )
747cdf0e10cSrcweir                         {
748cdf0e10cSrcweir                             // further checks for filters only if they are preselected: ASCII, HTML, RTF, DBase
749cdf0e10cSrcweir                             // without the preselection other filters (Writer) take precedence
750cdf0e10cSrcweir                             // DBase can't be detected reliably, so it also needs preselection
751cdf0e10cSrcweir                             bool bMaybeText = lcl_MayBeAscii( rStr );
752cdf0e10cSrcweir                             if ( pPreselectedFilter->GetFilterName().EqualsAscii(pFilterAscii) && bMaybeText )
753cdf0e10cSrcweir                             {
754cdf0e10cSrcweir                                 // Text filter is accepted if preselected
755cdf0e10cSrcweir                                 pFilter = pPreselectedFilter;
756cdf0e10cSrcweir                             }
757cdf0e10cSrcweir                             else
758cdf0e10cSrcweir                             {
759cdf0e10cSrcweir                                 // get file header
760cdf0e10cSrcweir                                 rStr.Seek( 0 );
761cdf0e10cSrcweir                                 const int nTrySize = 80;
762cdf0e10cSrcweir                                 ByteString aHeader;
763cdf0e10cSrcweir                                 for ( int j = 0; j < nTrySize && !rStr.IsEof(); j++ )
764cdf0e10cSrcweir                                 {
765cdf0e10cSrcweir                                     sal_Char c;
766cdf0e10cSrcweir                                     rStr >> c;
767cdf0e10cSrcweir                                     aHeader += c;
768cdf0e10cSrcweir                                 }
769cdf0e10cSrcweir                                 aHeader += '\0';
770cdf0e10cSrcweir 
771cdf0e10cSrcweir                                 if ( HTMLParser::IsHTMLFormat( aHeader.GetBuffer() ) )
772cdf0e10cSrcweir                                 {
773cdf0e10cSrcweir                                     // test for HTML
774cdf0e10cSrcweir                                     if ( pPreselectedFilter->GetName().EqualsAscii(pFilterHtml) )
775cdf0e10cSrcweir                                     {
776cdf0e10cSrcweir                                         pFilter = pPreselectedFilter;
777cdf0e10cSrcweir                                     }
778cdf0e10cSrcweir                                     else
779cdf0e10cSrcweir                                     {
780cdf0e10cSrcweir                                         pFilter = aMatcher.GetFilter4FilterName( String::CreateFromAscii(pFilterHtmlWeb) );
781cdf0e10cSrcweir                                         if ( bIsXLS )
782cdf0e10cSrcweir                                             bFakeXLS = true;
783cdf0e10cSrcweir                                     }
784cdf0e10cSrcweir                                 }
785cdf0e10cSrcweir                                 else if ( bIsXLS && bMaybeText )
786cdf0e10cSrcweir                                 {
787*fd9eb3f2SEike Rathke                                     // Detect Excel 2003 XML here only if XLS was preselected.
788*fd9eb3f2SEike Rathke                                     // The configured detection for Excel 2003 XML is still in XMLFilterDetect.
789*fd9eb3f2SEike Rathke                                     pFilter = lcl_DetectExcelXML( rStr, aMatcher );
790*fd9eb3f2SEike Rathke                                     if (!pFilter)
791cdf0e10cSrcweir                                         pFilter = aMatcher.GetFilter4FilterName( String::CreateFromAscii(pFilterAscii) );
792cdf0e10cSrcweir                                     bFakeXLS = true;
793cdf0e10cSrcweir                                 }
794cdf0e10cSrcweir                                 else if ( aHeader.CompareTo( "{\\rtf", 5 ) == COMPARE_EQUAL )
795cdf0e10cSrcweir                                 {
796cdf0e10cSrcweir                                     // test for RTF
797cdf0e10cSrcweir                                     pFilter = aMatcher.GetFilter4FilterName( String::CreateFromAscii(pFilterRtf) );
798cdf0e10cSrcweir                                 }
799cdf0e10cSrcweir                                 else if ( pPreselectedFilter->GetName().EqualsAscii(pFilterDBase) && lcl_MayBeDBase( rStr ) )
800cdf0e10cSrcweir                                     pFilter = pPreselectedFilter;
801cdf0e10cSrcweir                             }
802cdf0e10cSrcweir                         }
803cdf0e10cSrcweir                     }
804cdf0e10cSrcweir 				}
805cdf0e10cSrcweir 			}
806cdf0e10cSrcweir 		}
807cdf0e10cSrcweir 	}
808cdf0e10cSrcweir 
809cdf0e10cSrcweir     if ( nIndexOfInputStream == -1 && xStream.is() )
810cdf0e10cSrcweir     {
811cdf0e10cSrcweir         // if input stream wasn't part of the descriptor, now it should be, otherwise the content would be opend twice
812cdf0e10cSrcweir         lDescriptor.realloc( nPropertyCount + 1 );
813cdf0e10cSrcweir         lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("InputStream");
814cdf0e10cSrcweir         lDescriptor[nPropertyCount].Value <<= xStream;
815cdf0e10cSrcweir         nPropertyCount++;
816cdf0e10cSrcweir     }
817cdf0e10cSrcweir 
818cdf0e10cSrcweir     if ( nIndexOfContent == -1 && xContent.is() )
819cdf0e10cSrcweir     {
820cdf0e10cSrcweir         // if input stream wasn't part of the descriptor, now it should be, otherwise the content would be opend twice
821cdf0e10cSrcweir         lDescriptor.realloc( nPropertyCount + 1 );
822cdf0e10cSrcweir         lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("UCBContent");
823cdf0e10cSrcweir         lDescriptor[nPropertyCount].Value <<= xContent;
824cdf0e10cSrcweir         nPropertyCount++;
825cdf0e10cSrcweir     }
826cdf0e10cSrcweir 
827cdf0e10cSrcweir     if ( bReadOnly != bWasReadOnly )
828cdf0e10cSrcweir     {
829cdf0e10cSrcweir         if ( nIndexOfReadOnlyFlag == -1 )
830cdf0e10cSrcweir         {
831cdf0e10cSrcweir             lDescriptor.realloc( nPropertyCount + 1 );
832cdf0e10cSrcweir             lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("ReadOnly");
833cdf0e10cSrcweir             lDescriptor[nPropertyCount].Value <<= bReadOnly;
834cdf0e10cSrcweir             nPropertyCount++;
835cdf0e10cSrcweir         }
836cdf0e10cSrcweir         else
837cdf0e10cSrcweir             lDescriptor[nIndexOfReadOnlyFlag].Value <<= bReadOnly;
838cdf0e10cSrcweir     }
839cdf0e10cSrcweir 
840cdf0e10cSrcweir 	if ( !bRepairPackage && bRepairAllowed )
841cdf0e10cSrcweir 	{
842cdf0e10cSrcweir         lDescriptor.realloc( nPropertyCount + 1 );
843cdf0e10cSrcweir         lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("RepairPackage");
844cdf0e10cSrcweir         lDescriptor[nPropertyCount].Value <<= bRepairAllowed;
845cdf0e10cSrcweir         nPropertyCount++;
846cdf0e10cSrcweir 
847cdf0e10cSrcweir 		bOpenAsTemplate = sal_True;
848cdf0e10cSrcweir 
849cdf0e10cSrcweir 		// TODO/LATER: set progress bar that should be used
850cdf0e10cSrcweir 	}
851cdf0e10cSrcweir 
852cdf0e10cSrcweir 	if ( bOpenAsTemplate )
853cdf0e10cSrcweir 	{
854cdf0e10cSrcweir 		if ( nIndexOfTemplateFlag == -1 )
855cdf0e10cSrcweir 		{
856cdf0e10cSrcweir         	lDescriptor.realloc( nPropertyCount + 1 );
857cdf0e10cSrcweir         	lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("AsTemplate");
858cdf0e10cSrcweir         	lDescriptor[nPropertyCount].Value <<= bOpenAsTemplate;
859cdf0e10cSrcweir         	nPropertyCount++;
860cdf0e10cSrcweir 		}
861cdf0e10cSrcweir 		else
862cdf0e10cSrcweir         	lDescriptor[nIndexOfTemplateFlag].Value <<= bOpenAsTemplate;
863cdf0e10cSrcweir 	}
864cdf0e10cSrcweir 
865cdf0e10cSrcweir 	if ( aDocumentTitle.getLength() )
866cdf0e10cSrcweir 	{
867cdf0e10cSrcweir 		// the title was set here
868cdf0e10cSrcweir 		if ( nIndexOfDocumentTitle == -1 )
869cdf0e10cSrcweir 		{
870cdf0e10cSrcweir         	lDescriptor.realloc( nPropertyCount + 1 );
871cdf0e10cSrcweir         	lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("DocumentTitle");
872cdf0e10cSrcweir         	lDescriptor[nPropertyCount].Value <<= aDocumentTitle;
873cdf0e10cSrcweir         	nPropertyCount++;
874cdf0e10cSrcweir 		}
875cdf0e10cSrcweir 		else
876cdf0e10cSrcweir         	lDescriptor[nIndexOfDocumentTitle].Value <<= aDocumentTitle;
877cdf0e10cSrcweir 	}
878cdf0e10cSrcweir 
879cdf0e10cSrcweir     if ( bFakeXLS )
880cdf0e10cSrcweir     {
881cdf0e10cSrcweir         if ( nIndexOfFilterName == -1 )
882cdf0e10cSrcweir         {
883cdf0e10cSrcweir             lDescriptor.realloc( nPropertyCount + 1 );
884cdf0e10cSrcweir             lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("FilterName");
885cdf0e10cSrcweir             lDescriptor[nPropertyCount].Value <<= rtl::OUString(pFilter->GetName());
886cdf0e10cSrcweir             nPropertyCount++;
887cdf0e10cSrcweir         }
888cdf0e10cSrcweir         else
889cdf0e10cSrcweir             lDescriptor[nIndexOfFilterName].Value <<= rtl::OUString(pFilter->GetName());
890cdf0e10cSrcweir     }
891cdf0e10cSrcweir 
892cdf0e10cSrcweir     if ( pFilter )
893cdf0e10cSrcweir         aTypeName = pFilter->GetTypeName();
894cdf0e10cSrcweir     else
895cdf0e10cSrcweir         aTypeName.Erase();
896cdf0e10cSrcweir     return aTypeName;
897cdf0e10cSrcweir }
898cdf0e10cSrcweir 
899cdf0e10cSrcweir SFX_IMPL_SINGLEFACTORY( ScFilterDetect )
900cdf0e10cSrcweir 
901cdf0e10cSrcweir /* XServiceInfo */
902cdf0e10cSrcweir UNOOUSTRING SAL_CALL ScFilterDetect::getImplementationName() throw( UNORUNTIMEEXCEPTION )
903cdf0e10cSrcweir {
904cdf0e10cSrcweir     return impl_getStaticImplementationName();
905cdf0e10cSrcweir }
906cdf0e10cSrcweir                                                                                                                                 \
907cdf0e10cSrcweir /* XServiceInfo */
908cdf0e10cSrcweir sal_Bool SAL_CALL ScFilterDetect::supportsService( const UNOOUSTRING& sServiceName ) throw( UNORUNTIMEEXCEPTION )
909cdf0e10cSrcweir {
910cdf0e10cSrcweir     UNOSEQUENCE< UNOOUSTRING >  seqServiceNames(getSupportedServiceNames());
911cdf0e10cSrcweir     const UNOOUSTRING*          pArray          =   seqServiceNames.getConstArray();
912cdf0e10cSrcweir     for ( sal_Int32 nCounter=0; nCounter<seqServiceNames.getLength(); nCounter++ )
913cdf0e10cSrcweir     {
914cdf0e10cSrcweir         if ( pArray[nCounter] == sServiceName )
915cdf0e10cSrcweir         {
916cdf0e10cSrcweir             return sal_True ;
917cdf0e10cSrcweir         }
918cdf0e10cSrcweir     }
919cdf0e10cSrcweir     return sal_False ;
920cdf0e10cSrcweir }
921cdf0e10cSrcweir 
922cdf0e10cSrcweir /* XServiceInfo */
923cdf0e10cSrcweir UNOSEQUENCE< UNOOUSTRING > SAL_CALL ScFilterDetect::getSupportedServiceNames() throw( UNORUNTIMEEXCEPTION )
924cdf0e10cSrcweir {
925cdf0e10cSrcweir     return impl_getStaticSupportedServiceNames();
926cdf0e10cSrcweir }
927cdf0e10cSrcweir 
928cdf0e10cSrcweir /* Helper for XServiceInfo */
929cdf0e10cSrcweir UNOSEQUENCE< UNOOUSTRING > ScFilterDetect::impl_getStaticSupportedServiceNames()
930cdf0e10cSrcweir {
931cdf0e10cSrcweir     UNOMUTEXGUARD aGuard( UNOMUTEX::getGlobalMutex() );
932cdf0e10cSrcweir     UNOSEQUENCE< UNOOUSTRING > seqServiceNames( 1 );
933cdf0e10cSrcweir     seqServiceNames.getArray() [0] = UNOOUSTRING::createFromAscii( "com.sun.star.frame.ExtendedTypeDetection"  );
934cdf0e10cSrcweir     return seqServiceNames ;
935cdf0e10cSrcweir }
936cdf0e10cSrcweir 
937cdf0e10cSrcweir /* Helper for XServiceInfo */
938cdf0e10cSrcweir UNOOUSTRING ScFilterDetect::impl_getStaticImplementationName()
939cdf0e10cSrcweir {
940cdf0e10cSrcweir     return UNOOUSTRING::createFromAscii( "com.sun.star.comp.calc.FormatDetector" );
941cdf0e10cSrcweir }
942cdf0e10cSrcweir 
943cdf0e10cSrcweir /* Helper for registry */
944cdf0e10cSrcweir UNOREFERENCE< UNOXINTERFACE > SAL_CALL ScFilterDetect::impl_createInstance( const UNOREFERENCE< UNOXMULTISERVICEFACTORY >& xServiceManager ) throw( UNOEXCEPTION )
945cdf0e10cSrcweir {
946cdf0e10cSrcweir     return UNOREFERENCE< UNOXINTERFACE >( *new ScFilterDetect( xServiceManager ) );
947cdf0e10cSrcweir }
948cdf0e10cSrcweir 
949