xref: /AOO41X/main/sw/source/ui/uno/swdetect.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_sw.hxx"
30*cdf0e10cSrcweir #include "swdetect.hxx"
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include <framework/interaction.hxx>
33*cdf0e10cSrcweir #include <com/sun/star/frame/XFrame.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/frame/XModel.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/lang/XUnoTunnel.hpp>
36*cdf0e10cSrcweir #ifndef _UNOTOOLS_PROCESSFACTORY_HXX
37*cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
38*cdf0e10cSrcweir #endif
39*cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/task/XInteractionHandler.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
43*cdf0e10cSrcweir #include <com/sun/star/ucb/CommandAbortedException.hpp>
44*cdf0e10cSrcweir #include <com/sun/star/ucb/InteractiveAppException.hpp>
45*cdf0e10cSrcweir #include <com/sun/star/ucb/XContent.hpp>
46*cdf0e10cSrcweir #include <com/sun/star/packages/zip/ZipIOException.hpp>
47*cdf0e10cSrcweir #ifndef _TOOLKIT_UNOHLP_HXX
48*cdf0e10cSrcweir #include <toolkit/helper/vclunohelper.hxx>
49*cdf0e10cSrcweir #endif
50*cdf0e10cSrcweir #include <ucbhelper/simpleinteractionrequest.hxx>
51*cdf0e10cSrcweir #include <rtl/ustring.h>
52*cdf0e10cSrcweir #include <rtl/logfile.hxx>
53*cdf0e10cSrcweir #include <svl/itemset.hxx>
54*cdf0e10cSrcweir #include <vcl/window.hxx>
55*cdf0e10cSrcweir #include <svl/eitem.hxx>
56*cdf0e10cSrcweir #include <svl/stritem.hxx>
57*cdf0e10cSrcweir #include <tools/urlobj.hxx>
58*cdf0e10cSrcweir #include <vos/mutex.hxx>
59*cdf0e10cSrcweir #include <svtools/sfxecode.hxx>
60*cdf0e10cSrcweir #include <svtools/ehdl.hxx>
61*cdf0e10cSrcweir #include <sot/storinfo.hxx>
62*cdf0e10cSrcweir #include <vcl/svapp.hxx>
63*cdf0e10cSrcweir #include <sfx2/app.hxx>
64*cdf0e10cSrcweir #include <sfx2/sfxsids.hrc>
65*cdf0e10cSrcweir #include <sfx2/request.hxx>
66*cdf0e10cSrcweir #include <sfx2/docfile.hxx>
67*cdf0e10cSrcweir #include <sfx2/docfilt.hxx>
68*cdf0e10cSrcweir #include <sfx2/fcontnr.hxx>
69*cdf0e10cSrcweir #include <sfx2/brokenpackageint.hxx>
70*cdf0e10cSrcweir #include <svtools/FilterConfigItem.hxx>
71*cdf0e10cSrcweir #include <unotools/moduleoptions.hxx>
72*cdf0e10cSrcweir #include <com/sun/star/util/XArchiver.hpp>
73*cdf0e10cSrcweir #include <comphelper/ihwrapnofilter.hxx>
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir #include <swdll.hxx>
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir using namespace ::com::sun::star;
78*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
79*cdf0e10cSrcweir using namespace ::com::sun::star::io;
80*cdf0e10cSrcweir using namespace ::com::sun::star::frame;
81*cdf0e10cSrcweir using namespace ::com::sun::star::task;
82*cdf0e10cSrcweir using namespace ::com::sun::star::beans;
83*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
84*cdf0e10cSrcweir using namespace ::com::sun::star::ucb;
85*cdf0e10cSrcweir using ::rtl::OUString;
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir SwFilterDetect::SwFilterDetect( const REFERENCE < lang::XMultiServiceFactory >& /*xFactory*/ )
88*cdf0e10cSrcweir {
89*cdf0e10cSrcweir }
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir SwFilterDetect::~SwFilterDetect()
92*cdf0e10cSrcweir {
93*cdf0e10cSrcweir }
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir ::rtl::OUString SAL_CALL SwFilterDetect::detect( uno::Sequence< beans::PropertyValue >& lDescriptor ) throw( uno::RuntimeException )
96*cdf0e10cSrcweir {
97*cdf0e10cSrcweir     REFERENCE< XInputStream > xStream;
98*cdf0e10cSrcweir     REFERENCE< XContent > xContent;
99*cdf0e10cSrcweir     REFERENCE< XInteractionHandler > xInteraction;
100*cdf0e10cSrcweir     String aURL;
101*cdf0e10cSrcweir 	::rtl::OUString sTemp;
102*cdf0e10cSrcweir     String aTypeName;            // a name describing the type (from MediaDescriptor, usually from flat detection)
103*cdf0e10cSrcweir     String aPreselectedFilterName;      // a name describing the filter to use (from MediaDescriptor, usually from UI action)
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir 	::rtl::OUString aDocumentTitle; // interesting only if set in this method
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir 	// opening as template is done when a parameter tells to do so and a template filter can be detected
108*cdf0e10cSrcweir     // (otherwise no valid filter would be found) or if the detected filter is a template filter and
109*cdf0e10cSrcweir 	// there is no parameter that forbids to open as template
110*cdf0e10cSrcweir 	sal_Bool bOpenAsTemplate = sal_False;
111*cdf0e10cSrcweir     sal_Bool bWasReadOnly = sal_False, bReadOnly = sal_False;
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir 	sal_Bool bRepairPackage = sal_False;
114*cdf0e10cSrcweir 	sal_Bool bRepairAllowed = sal_False;
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir 	// now some parameters that can already be in the array, but may be overwritten or new inserted here
117*cdf0e10cSrcweir 	// remember their indices in the case new values must be added to the array
118*cdf0e10cSrcweir 	sal_Int32 nPropertyCount = lDescriptor.getLength();
119*cdf0e10cSrcweir     sal_Int32 nIndexOfFilterName = -1;
120*cdf0e10cSrcweir     sal_Int32 nIndexOfInputStream = -1;
121*cdf0e10cSrcweir     sal_Int32 nIndexOfContent = -1;
122*cdf0e10cSrcweir     sal_Int32 nIndexOfReadOnlyFlag = -1;
123*cdf0e10cSrcweir     sal_Int32 nIndexOfTemplateFlag = -1;
124*cdf0e10cSrcweir     sal_Int32 nIndexOfDocumentTitle = -1;
125*cdf0e10cSrcweir     sal_Int32 nIndexOfInteractionHandler = -1;
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir     for( sal_Int32 nProperty=0; nProperty<nPropertyCount; ++nProperty )
128*cdf0e10cSrcweir 	{
129*cdf0e10cSrcweir         // extract properties
130*cdf0e10cSrcweir         if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("URL")) )
131*cdf0e10cSrcweir 		{
132*cdf0e10cSrcweir 			lDescriptor[nProperty].Value >>= sTemp;
133*cdf0e10cSrcweir 			aURL = sTemp;
134*cdf0e10cSrcweir 		}
135*cdf0e10cSrcweir         else if( !aURL.Len() && lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("FileName")) )
136*cdf0e10cSrcweir 		{
137*cdf0e10cSrcweir 			lDescriptor[nProperty].Value >>= sTemp;
138*cdf0e10cSrcweir 			aURL = sTemp;
139*cdf0e10cSrcweir 		}
140*cdf0e10cSrcweir         else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("TypeName")) )
141*cdf0e10cSrcweir 		{
142*cdf0e10cSrcweir 			lDescriptor[nProperty].Value >>= sTemp;
143*cdf0e10cSrcweir             aTypeName = sTemp;
144*cdf0e10cSrcweir 		}
145*cdf0e10cSrcweir         else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("FilterName")) )
146*cdf0e10cSrcweir 		{
147*cdf0e10cSrcweir 			lDescriptor[nProperty].Value >>= sTemp;
148*cdf0e10cSrcweir             aPreselectedFilterName = sTemp;
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir             // if the preselected filter name is not correct, it must be erased after detection
151*cdf0e10cSrcweir             // remember index of property to get access to it later
152*cdf0e10cSrcweir             nIndexOfFilterName = nProperty;
153*cdf0e10cSrcweir 		}
154*cdf0e10cSrcweir         else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("InputStream")) )
155*cdf0e10cSrcweir             nIndexOfInputStream = nProperty;
156*cdf0e10cSrcweir         else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("ReadOnly")) )
157*cdf0e10cSrcweir             nIndexOfReadOnlyFlag = nProperty;
158*cdf0e10cSrcweir         else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("UCBContent")) )
159*cdf0e10cSrcweir             nIndexOfContent = nProperty;
160*cdf0e10cSrcweir         else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("AsTemplate")) )
161*cdf0e10cSrcweir 		{
162*cdf0e10cSrcweir 			lDescriptor[nProperty].Value >>= bOpenAsTemplate;
163*cdf0e10cSrcweir             nIndexOfTemplateFlag = nProperty;
164*cdf0e10cSrcweir 		}
165*cdf0e10cSrcweir         else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("InteractionHandler")) )
166*cdf0e10cSrcweir         {
167*cdf0e10cSrcweir             lDescriptor[nProperty].Value >>= xInteraction;
168*cdf0e10cSrcweir             nIndexOfInteractionHandler = nProperty;
169*cdf0e10cSrcweir         }
170*cdf0e10cSrcweir         else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("RepairPackage")) )
171*cdf0e10cSrcweir             lDescriptor[nProperty].Value >>= bRepairPackage;
172*cdf0e10cSrcweir         else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("DocumentTitle")) )
173*cdf0e10cSrcweir             nIndexOfDocumentTitle = nProperty;
174*cdf0e10cSrcweir 	}
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir     // can't check the type for external filters, so set the "dont" flag accordingly
177*cdf0e10cSrcweir     ::vos::OGuard aGuard( Application::GetSolarMutex() );
178*cdf0e10cSrcweir     //SfxFilterFlags nMust = SFX_FILTER_IMPORT, nDont = SFX_FILTER_NOTINSTALLED;
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir     SfxApplication* pApp = SFX_APP();
181*cdf0e10cSrcweir     SfxAllItemSet *pSet = new SfxAllItemSet( pApp->GetPool() );
182*cdf0e10cSrcweir     TransformParameters( SID_OPENDOC, lDescriptor, *pSet );
183*cdf0e10cSrcweir     SFX_ITEMSET_ARG( pSet, pItem, SfxBoolItem, SID_DOC_READONLY, sal_False );
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir     bWasReadOnly = pItem && pItem->GetValue();
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir 	const SfxFilter* pFilter = 0;
188*cdf0e10cSrcweir 	String aPrefix = String::CreateFromAscii( "private:factory/" );
189*cdf0e10cSrcweir 	if( aURL.Match( aPrefix ) == aPrefix.Len() )
190*cdf0e10cSrcweir 	{
191*cdf0e10cSrcweir 		if( SvtModuleOptions().IsWriter() )
192*cdf0e10cSrcweir 		{
193*cdf0e10cSrcweir 			String aPattern( aPrefix );
194*cdf0e10cSrcweir 			aPattern += String::CreateFromAscii("swriter");
195*cdf0e10cSrcweir 			if ( aURL.Match( aPattern ) >= aPattern.Len() )
196*cdf0e10cSrcweir 				//pFilter = SfxFilter::GetDefaultFilterFromFactory( aURL );
197*cdf0e10cSrcweir 				return aTypeName;
198*cdf0e10cSrcweir 		}
199*cdf0e10cSrcweir 	}
200*cdf0e10cSrcweir 	else
201*cdf0e10cSrcweir 	{
202*cdf0e10cSrcweir 	    // ctor of SfxMedium uses owner transition of ItemSet
203*cdf0e10cSrcweir 	    SfxMedium aMedium( aURL, bWasReadOnly ? STREAM_STD_READ : STREAM_STD_READWRITE, sal_False, NULL, pSet );
204*cdf0e10cSrcweir 	    aMedium.UseInteractionHandler( sal_True );
205*cdf0e10cSrcweir 	    if ( aMedium.GetErrorCode() == ERRCODE_NONE )
206*cdf0e10cSrcweir 	    {
207*cdf0e10cSrcweir 	        // remember input stream and content and put them into the descriptor later
208*cdf0e10cSrcweir 			// should be done here since later the medium can switch to a version
209*cdf0e10cSrcweir 	        xStream = aMedium.GetInputStream();
210*cdf0e10cSrcweir 			xContent = aMedium.GetContent();
211*cdf0e10cSrcweir             bReadOnly = aMedium.IsReadOnly();
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir         	sal_Bool bIsStorage = aMedium.IsStorage();
214*cdf0e10cSrcweir             if ( bIsStorage )
215*cdf0e10cSrcweir 			{
216*cdf0e10cSrcweir                 uno::Reference< embed::XStorage > xStorage = aMedium.GetStorage( sal_False );
217*cdf0e10cSrcweir 				if ( aMedium.GetLastStorageCreationState() != ERRCODE_NONE )
218*cdf0e10cSrcweir 				{
219*cdf0e10cSrcweir 					// error during storage creation means _here_ that the medium
220*cdf0e10cSrcweir                     // is broken, but we can not handle it in medium since impossibility
221*cdf0e10cSrcweir 					// to create a storage does not _always_ means that the medium is broken
222*cdf0e10cSrcweir 					aMedium.SetError( aMedium.GetLastStorageCreationState(), ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ) );
223*cdf0e10cSrcweir 					if ( xInteraction.is() )
224*cdf0e10cSrcweir 					{
225*cdf0e10cSrcweir 						OUString empty;
226*cdf0e10cSrcweir 						try
227*cdf0e10cSrcweir 						{
228*cdf0e10cSrcweir 							InteractiveAppException xException( empty,
229*cdf0e10cSrcweir 															REFERENCE< XInterface >(),
230*cdf0e10cSrcweir 															InteractionClassification_ERROR,
231*cdf0e10cSrcweir 															aMedium.GetError() );
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir 							REFERENCE< XInteractionRequest > xRequest(
234*cdf0e10cSrcweir 								new ucbhelper::SimpleInteractionRequest( makeAny( xException ),
235*cdf0e10cSrcweir 																 	 ucbhelper::CONTINUATION_APPROVE ) );
236*cdf0e10cSrcweir 							xInteraction->handle( xRequest );
237*cdf0e10cSrcweir 						}
238*cdf0e10cSrcweir 						catch ( Exception & ) {};
239*cdf0e10cSrcweir 					}
240*cdf0e10cSrcweir 				}
241*cdf0e10cSrcweir 				else
242*cdf0e10cSrcweir 				{
243*cdf0e10cSrcweir 					DBG_ASSERT( xStorage.is(), "At this point storage must exist!" );
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir 					try
246*cdf0e10cSrcweir 					{
247*cdf0e10cSrcweir                         const SfxFilter* pPreFilter = aPreselectedFilterName.Len() ?
248*cdf0e10cSrcweir                                 SfxFilterMatcher().GetFilter4FilterName( aPreselectedFilterName ) : aTypeName.Len() ?
249*cdf0e10cSrcweir                                 SfxFilterMatcher(String::CreateFromAscii("swriter")).GetFilter4EA( aTypeName ) : 0;
250*cdf0e10cSrcweir                         if (!pPreFilter)
251*cdf0e10cSrcweir                             pPreFilter = SfxFilterMatcher(String::CreateFromAscii("sweb")).GetFilter4EA( aTypeName );
252*cdf0e10cSrcweir                         String aFilterName;
253*cdf0e10cSrcweir                         if ( pPreFilter )
254*cdf0e10cSrcweir                         {
255*cdf0e10cSrcweir                             aFilterName = pPreFilter->GetName();
256*cdf0e10cSrcweir                             aTypeName = pPreFilter->GetTypeName();
257*cdf0e10cSrcweir                         }
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir                         aTypeName = SfxFilter::GetTypeFromStorage( xStorage, pPreFilter ? pPreFilter->IsOwnTemplateFormat() : sal_False, &aFilterName );
260*cdf0e10cSrcweir 					}
261*cdf0e10cSrcweir 					catch( lang::WrappedTargetException& aWrap )
262*cdf0e10cSrcweir 					{
263*cdf0e10cSrcweir 						packages::zip::ZipIOException aZipException;
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir 						// repairing is done only if this type is requested from outside
266*cdf0e10cSrcweir                         // we don't do any type detection on broken packages (f.e. because it might be impossible), so any requested
267*cdf0e10cSrcweir                         // type will be accepted if the user allows to repair the file
268*cdf0e10cSrcweir                         if ( ( aWrap.TargetException >>= aZipException ) && ( aTypeName.Len() || aPreselectedFilterName.Len() ) )
269*cdf0e10cSrcweir 						{
270*cdf0e10cSrcweir 							if ( xInteraction.is() )
271*cdf0e10cSrcweir 							{
272*cdf0e10cSrcweir                                 // the package is a broken one
273*cdf0e10cSrcweir        							aDocumentTitle = aMedium.GetURLObject().getName(
274*cdf0e10cSrcweir 															INetURLObject::LAST_SEGMENT,
275*cdf0e10cSrcweir 															true,
276*cdf0e10cSrcweir 															INetURLObject::DECODE_WITH_CHARSET );
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir 								if ( !bRepairPackage )
279*cdf0e10cSrcweir 								{
280*cdf0e10cSrcweir                                     // ask the user whether he wants to try to repair
281*cdf0e10cSrcweir                                     RequestPackageReparation aRequest( aDocumentTitle );
282*cdf0e10cSrcweir                                     xInteraction->handle( aRequest.GetRequest() );
283*cdf0e10cSrcweir                                     bRepairAllowed = aRequest.isApproved();
284*cdf0e10cSrcweir 								}
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir 								if ( !bRepairAllowed )
287*cdf0e10cSrcweir 								{
288*cdf0e10cSrcweir 									// repair either not allowed or not successful
289*cdf0e10cSrcweir 									// repair either not allowed or not successful
290*cdf0e10cSrcweir                                     NotifyBrokenPackage aNotifyRequest( aDocumentTitle );
291*cdf0e10cSrcweir                                     xInteraction->handle( aNotifyRequest.GetRequest() );
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir                                     rtl::Reference< ::comphelper::OIHWrapNoFilterDialog > xHandler = new ::comphelper::OIHWrapNoFilterDialog( xInteraction );
294*cdf0e10cSrcweir                                     if ( nIndexOfInteractionHandler != -1 )
295*cdf0e10cSrcweir                                         lDescriptor[nIndexOfInteractionHandler].Value <<= uno::Reference< XInteractionHandler >( static_cast< task::XInteractionHandler* >( xHandler.get() ) );
296*cdf0e10cSrcweir 
297*cdf0e10cSrcweir 									aMedium.SetError( ERRCODE_ABORT, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ) );
298*cdf0e10cSrcweir 								}
299*cdf0e10cSrcweir 							}
300*cdf0e10cSrcweir 							else
301*cdf0e10cSrcweir                                 // no interaction, error handling as usual
302*cdf0e10cSrcweir 								aMedium.SetError( ERRCODE_IO_BROKENPACKAGE, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ) );
303*cdf0e10cSrcweir 
304*cdf0e10cSrcweir 							if ( !bRepairAllowed )
305*cdf0e10cSrcweir 							{
306*cdf0e10cSrcweir 								aTypeName.Erase();
307*cdf0e10cSrcweir 								aPreselectedFilterName.Erase();
308*cdf0e10cSrcweir 							}
309*cdf0e10cSrcweir 						}
310*cdf0e10cSrcweir 					}
311*cdf0e10cSrcweir 					catch( uno::RuntimeException& )
312*cdf0e10cSrcweir 					{
313*cdf0e10cSrcweir 						throw;
314*cdf0e10cSrcweir 					}
315*cdf0e10cSrcweir 					catch( uno::Exception& )
316*cdf0e10cSrcweir 					{
317*cdf0e10cSrcweir 						aTypeName.Erase();
318*cdf0e10cSrcweir 						aPreselectedFilterName.Erase();
319*cdf0e10cSrcweir 					}
320*cdf0e10cSrcweir 				}
321*cdf0e10cSrcweir 			}
322*cdf0e10cSrcweir             else
323*cdf0e10cSrcweir             {
324*cdf0e10cSrcweir                 aMedium.GetInStream();
325*cdf0e10cSrcweir                 if ( aMedium.GetErrorCode() == ERRCODE_NONE )
326*cdf0e10cSrcweir                 {
327*cdf0e10cSrcweir                     if ( aPreselectedFilterName.Len() )
328*cdf0e10cSrcweir                         pFilter = SfxFilter::GetFilterByName( aPreselectedFilterName );
329*cdf0e10cSrcweir                     else
330*cdf0e10cSrcweir                         pFilter = SfxFilterMatcher().GetFilter4EA( aTypeName );
331*cdf0e10cSrcweir 
332*cdf0e10cSrcweir                     sal_Bool bTestWriter = !pFilter || pFilter->GetServiceName().EqualsAscii("com.sun.star.text.TextDocument") ||
333*cdf0e10cSrcweir                         pFilter->GetServiceName().EqualsAscii("com.sun.star.text.WebDocument");
334*cdf0e10cSrcweir                     sal_Bool bTestGlobal = !pFilter || pFilter->GetServiceName().EqualsAscii("com.sun.star.text.GlobalDocument");
335*cdf0e10cSrcweir 
336*cdf0e10cSrcweir                     const SfxFilter* pOrigFilter = NULL;
337*cdf0e10cSrcweir                     if ( !bTestWriter && !bTestGlobal && pFilter )
338*cdf0e10cSrcweir                     {
339*cdf0e10cSrcweir                         // cross filter; now this should be a type detection only, not a filter detection
340*cdf0e10cSrcweir                         // we can simulate it by preserving the preselected filter if the type matches
341*cdf0e10cSrcweir                         // example: HTML filter for Calc
342*cdf0e10cSrcweir                         pOrigFilter = pFilter;
343*cdf0e10cSrcweir                         pFilter = SfxFilterMatcher().GetFilter4EA( pFilter->GetTypeName() );
344*cdf0e10cSrcweir                         bTestWriter = sal_True;
345*cdf0e10cSrcweir                     }
346*cdf0e10cSrcweir 
347*cdf0e10cSrcweir                     sal_uLong nErr = ERRCODE_NONE;
348*cdf0e10cSrcweir                     if ( pFilter || bTestWriter )
349*cdf0e10cSrcweir                         nErr = DetectFilter( aMedium, &pFilter );
350*cdf0e10cSrcweir                     if ( nErr != ERRCODE_NONE )
351*cdf0e10cSrcweir                         pFilter = NULL;
352*cdf0e10cSrcweir                     else if ( pOrigFilter && pFilter && pFilter->GetTypeName() == pOrigFilter->GetTypeName() )
353*cdf0e10cSrcweir                         // cross filter, see above
354*cdf0e10cSrcweir                         pFilter = pOrigFilter;
355*cdf0e10cSrcweir                 }
356*cdf0e10cSrcweir 
357*cdf0e10cSrcweir                 if ( pFilter )
358*cdf0e10cSrcweir                     aTypeName = pFilter->GetTypeName();
359*cdf0e10cSrcweir                 else
360*cdf0e10cSrcweir                     aTypeName.Erase();
361*cdf0e10cSrcweir             }
362*cdf0e10cSrcweir 		}
363*cdf0e10cSrcweir 	}
364*cdf0e10cSrcweir 
365*cdf0e10cSrcweir     if ( nIndexOfInputStream == -1 && xStream.is() )
366*cdf0e10cSrcweir     {
367*cdf0e10cSrcweir         // if input stream wasn't part of the descriptor, now it should be, otherwise the content would be opend twice
368*cdf0e10cSrcweir         lDescriptor.realloc( nPropertyCount + 1 );
369*cdf0e10cSrcweir         lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("InputStream");
370*cdf0e10cSrcweir         lDescriptor[nPropertyCount].Value <<= xStream;
371*cdf0e10cSrcweir         nPropertyCount++;
372*cdf0e10cSrcweir     }
373*cdf0e10cSrcweir 
374*cdf0e10cSrcweir     if ( nIndexOfContent == -1 && xContent.is() )
375*cdf0e10cSrcweir     {
376*cdf0e10cSrcweir         // if input stream wasn't part of the descriptor, now it should be, otherwise the content would be opend twice
377*cdf0e10cSrcweir         lDescriptor.realloc( nPropertyCount + 1 );
378*cdf0e10cSrcweir         lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("UCBContent");
379*cdf0e10cSrcweir         lDescriptor[nPropertyCount].Value <<= xContent;
380*cdf0e10cSrcweir         nPropertyCount++;
381*cdf0e10cSrcweir     }
382*cdf0e10cSrcweir 
383*cdf0e10cSrcweir     if ( bReadOnly != bWasReadOnly )
384*cdf0e10cSrcweir     {
385*cdf0e10cSrcweir         if ( nIndexOfReadOnlyFlag == -1 )
386*cdf0e10cSrcweir         {
387*cdf0e10cSrcweir             lDescriptor.realloc( nPropertyCount + 1 );
388*cdf0e10cSrcweir             lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("ReadOnly");
389*cdf0e10cSrcweir             lDescriptor[nPropertyCount].Value <<= bReadOnly;
390*cdf0e10cSrcweir             nPropertyCount++;
391*cdf0e10cSrcweir         }
392*cdf0e10cSrcweir         else
393*cdf0e10cSrcweir             lDescriptor[nIndexOfReadOnlyFlag].Value <<= bReadOnly;
394*cdf0e10cSrcweir     }
395*cdf0e10cSrcweir 
396*cdf0e10cSrcweir 	if ( !bRepairPackage && bRepairAllowed )
397*cdf0e10cSrcweir 	{
398*cdf0e10cSrcweir         lDescriptor.realloc( nPropertyCount + 1 );
399*cdf0e10cSrcweir         lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("RepairPackage");
400*cdf0e10cSrcweir         lDescriptor[nPropertyCount].Value <<= bRepairAllowed;
401*cdf0e10cSrcweir         nPropertyCount++;
402*cdf0e10cSrcweir 		bOpenAsTemplate = sal_True;
403*cdf0e10cSrcweir 		// TODO/LATER: set progress bar that should be used
404*cdf0e10cSrcweir 	}
405*cdf0e10cSrcweir 
406*cdf0e10cSrcweir 	if ( bOpenAsTemplate )
407*cdf0e10cSrcweir 	{
408*cdf0e10cSrcweir 		if ( nIndexOfTemplateFlag == -1 )
409*cdf0e10cSrcweir 		{
410*cdf0e10cSrcweir         	lDescriptor.realloc( nPropertyCount + 1 );
411*cdf0e10cSrcweir         	lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("AsTemplate");
412*cdf0e10cSrcweir         	lDescriptor[nPropertyCount].Value <<= bOpenAsTemplate;
413*cdf0e10cSrcweir         	nPropertyCount++;
414*cdf0e10cSrcweir 		}
415*cdf0e10cSrcweir 		else
416*cdf0e10cSrcweir         	lDescriptor[nIndexOfTemplateFlag].Value <<= bOpenAsTemplate;
417*cdf0e10cSrcweir 	}
418*cdf0e10cSrcweir 
419*cdf0e10cSrcweir 	if ( aDocumentTitle.getLength() )
420*cdf0e10cSrcweir 	{
421*cdf0e10cSrcweir 		// the title was set here
422*cdf0e10cSrcweir 		if ( nIndexOfDocumentTitle == -1 )
423*cdf0e10cSrcweir 		{
424*cdf0e10cSrcweir         	lDescriptor.realloc( nPropertyCount + 1 );
425*cdf0e10cSrcweir         	lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("DocumentTitle");
426*cdf0e10cSrcweir         	lDescriptor[nPropertyCount].Value <<= aDocumentTitle;
427*cdf0e10cSrcweir         	nPropertyCount++;
428*cdf0e10cSrcweir 		}
429*cdf0e10cSrcweir 		else
430*cdf0e10cSrcweir         	lDescriptor[nIndexOfDocumentTitle].Value <<= aDocumentTitle;
431*cdf0e10cSrcweir 	}
432*cdf0e10cSrcweir 
433*cdf0e10cSrcweir 
434*cdf0e10cSrcweir     return aTypeName;
435*cdf0e10cSrcweir }
436*cdf0e10cSrcweir 
437*cdf0e10cSrcweir SFX_IMPL_SINGLEFACTORY( SwFilterDetect )
438*cdf0e10cSrcweir 
439*cdf0e10cSrcweir /* XServiceInfo */
440*cdf0e10cSrcweir UNOOUSTRING SAL_CALL SwFilterDetect::getImplementationName() throw( UNORUNTIMEEXCEPTION )
441*cdf0e10cSrcweir {
442*cdf0e10cSrcweir     return impl_getStaticImplementationName();
443*cdf0e10cSrcweir }
444*cdf0e10cSrcweir                                                                                                                                 \
445*cdf0e10cSrcweir /* XServiceInfo */
446*cdf0e10cSrcweir sal_Bool SAL_CALL SwFilterDetect::supportsService( const UNOOUSTRING& sServiceName ) throw( UNORUNTIMEEXCEPTION )
447*cdf0e10cSrcweir {
448*cdf0e10cSrcweir     UNOSEQUENCE< UNOOUSTRING >  seqServiceNames =   getSupportedServiceNames();
449*cdf0e10cSrcweir     const UNOOUSTRING*          pArray          =   seqServiceNames.getConstArray();
450*cdf0e10cSrcweir     for ( sal_Int32 nCounter=0; nCounter<seqServiceNames.getLength(); nCounter++ )
451*cdf0e10cSrcweir     {
452*cdf0e10cSrcweir         if ( pArray[nCounter] == sServiceName )
453*cdf0e10cSrcweir         {
454*cdf0e10cSrcweir             return sal_True ;
455*cdf0e10cSrcweir         }
456*cdf0e10cSrcweir     }
457*cdf0e10cSrcweir     return sal_False ;
458*cdf0e10cSrcweir }
459*cdf0e10cSrcweir 
460*cdf0e10cSrcweir /* XServiceInfo */
461*cdf0e10cSrcweir UNOSEQUENCE< UNOOUSTRING > SAL_CALL SwFilterDetect::getSupportedServiceNames() throw( UNORUNTIMEEXCEPTION )
462*cdf0e10cSrcweir {
463*cdf0e10cSrcweir     return impl_getStaticSupportedServiceNames();
464*cdf0e10cSrcweir }
465*cdf0e10cSrcweir 
466*cdf0e10cSrcweir /* Helper for XServiceInfo */
467*cdf0e10cSrcweir UNOSEQUENCE< UNOOUSTRING > SwFilterDetect::impl_getStaticSupportedServiceNames()
468*cdf0e10cSrcweir {
469*cdf0e10cSrcweir     UNOMUTEXGUARD aGuard( UNOMUTEX::getGlobalMutex() );
470*cdf0e10cSrcweir     UNOSEQUENCE< UNOOUSTRING > seqServiceNames( 3 );
471*cdf0e10cSrcweir     seqServiceNames.getArray() [0] = UNOOUSTRING::createFromAscii( "com.sun.star.frame.ExtendedTypeDetection"  );
472*cdf0e10cSrcweir     seqServiceNames.getArray() [1] = UNOOUSTRING::createFromAscii( "com.sun.star.text.FormatDetector"  );
473*cdf0e10cSrcweir     seqServiceNames.getArray() [2] = UNOOUSTRING::createFromAscii( "com.sun.star.text.W4WFormatDetector"  );
474*cdf0e10cSrcweir     return seqServiceNames ;
475*cdf0e10cSrcweir }
476*cdf0e10cSrcweir 
477*cdf0e10cSrcweir /* Helper for XServiceInfo */
478*cdf0e10cSrcweir UNOOUSTRING SwFilterDetect::impl_getStaticImplementationName()
479*cdf0e10cSrcweir {
480*cdf0e10cSrcweir     return UNOOUSTRING::createFromAscii( "com.sun.star.comp.writer.FormatDetector" );
481*cdf0e10cSrcweir }
482*cdf0e10cSrcweir 
483*cdf0e10cSrcweir /* Helper for registry */
484*cdf0e10cSrcweir UNOREFERENCE< UNOXINTERFACE > SAL_CALL SwFilterDetect::impl_createInstance( const UNOREFERENCE< UNOXMULTISERVICEFACTORY >& xServiceManager ) throw( UNOEXCEPTION )
485*cdf0e10cSrcweir {
486*cdf0e10cSrcweir     return UNOREFERENCE< UNOXINTERFACE >( *new SwFilterDetect( xServiceManager ) );
487*cdf0e10cSrcweir }
488*cdf0e10cSrcweir 
489