merge the formfield patch from ooo-build
[ooovba.git] / sd / source / ui / unoidl / sddetect.cxx
blob7ffbf7585addeb6582a68cd44af2a86b58c2ce31
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: sddetect.cxx,v $
10 * $Revision: 1.12 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sd.hxx"
34 #include "sddetect.hxx"
36 #include <framework/interaction.hxx>
37 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
38 #include <com/sun/star/beans/PropertyValue.hpp>
39 #include <com/sun/star/frame/XFrame.hpp>
40 #include <com/sun/star/frame/XModel.hpp>
41 #include <com/sun/star/awt/XWindow.hpp>
42 #include <com/sun/star/lang/XUnoTunnel.hpp>
43 #ifndef _UNOTOOLS_PROCESSFACTORY_HXX
44 #include <comphelper/processfactory.hxx>
45 #endif
46 #include <com/sun/star/beans/PropertyValue.hpp>
47 #include <com/sun/star/container/XNameAccess.hpp>
48 #include <com/sun/star/io/XInputStream.hpp>
49 #include <com/sun/star/task/XInteractionHandler.hpp>
50 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
51 #include <com/sun/star/ucb/CommandAbortedException.hpp>
52 #include <com/sun/star/ucb/InteractiveAppException.hpp>
53 #include <com/sun/star/ucb/XContent.hpp>
54 #include <com/sun/star/packages/zip/ZipIOException.hpp>
57 #include <framework/interaction.hxx>
59 #ifndef _TOOLKIT_UNOHLP_HXX
60 #include <toolkit/helper/vclunohelper.hxx>
61 #endif
62 #include <ucbhelper/simpleinteractionrequest.hxx>
64 #include <rtl/ustring.h>
65 #include <rtl/logfile.hxx>
66 #include <svtools/itemset.hxx>
67 #include <vcl/window.hxx>
68 #include <svtools/eitem.hxx>
69 #include <svtools/stritem.hxx>
70 #include <tools/urlobj.hxx>
71 #include <vos/mutex.hxx>
72 #include <svtools/sfxecode.hxx>
73 #include <svtools/ehdl.hxx>
74 #include <sot/storinfo.hxx>
75 #include <vcl/svapp.hxx>
76 #include <sfx2/app.hxx>
77 #include <sfx2/sfxsids.hrc>
78 #include <sfx2/request.hxx>
79 #include <sfx2/docfile.hxx>
80 #include <sfx2/docfilt.hxx>
81 #include <sfx2/fcontnr.hxx>
82 #include <sfx2/brokenpackageint.hxx>
83 #include <svx/impgrf.hxx>
84 #include <svtools/FilterConfigItem.hxx>
85 #include <sot/storage.hxx>
86 #include <svtools/moduleoptions.hxx>
87 #include <com/sun/star/util/XArchiver.hpp>
88 #include <comphelper/processfactory.hxx>
90 #include "strmname.h"
92 using namespace ::com::sun::star;
93 using namespace ::com::sun::star::uno;
94 using namespace ::com::sun::star::io;
95 using namespace ::com::sun::star::frame;
96 using namespace ::com::sun::star::task;
97 using namespace ::com::sun::star::beans;
98 using namespace ::com::sun::star::lang;
99 using namespace ::com::sun::star::ucb;
100 using namespace ::rtl;
102 SdFilterDetect::SdFilterDetect( const REFERENCE < ::com::sun::star::lang::XMultiServiceFactory >& )
106 SdFilterDetect::~SdFilterDetect()
110 ::rtl::OUString SAL_CALL SdFilterDetect::detect( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& lDescriptor ) throw( ::com::sun::star::uno::RuntimeException )
112 REFERENCE< XInputStream > xStream;
113 REFERENCE< XContent > xContent;
114 REFERENCE< XInteractionHandler > xInteraction;
115 String aURL;
116 ::rtl::OUString sTemp;
117 String aTypeName; // a name describing the type (from MediaDescriptor, usually from flat detection)
118 String aPreselectedFilterName; // a name describing the filter to use (from MediaDescriptor, usually from UI action)
120 ::rtl::OUString aDocumentTitle; // interesting only if set in this method
122 // opening as template is done when a parameter tells to do so and a template filter can be detected
123 // (otherwise no valid filter would be found) or if the detected filter is a template filter and
124 // there is no parameter that forbids to open as template
125 sal_Bool bOpenAsTemplate = sal_False;
126 sal_Bool bWasReadOnly = sal_False, bReadOnly = sal_False;
128 sal_Bool bRepairPackage = sal_False;
129 sal_Bool bRepairAllowed = sal_False;
131 // now some parameters that can already be in the array, but may be overwritten or new inserted here
132 // remember their indices in the case new values must be added to the array
133 sal_Int32 nPropertyCount = lDescriptor.getLength();
134 sal_Int32 nIndexOfFilterName = -1;
135 sal_Int32 nIndexOfInputStream = -1;
136 sal_Int32 nIndexOfContent = -1;
137 sal_Int32 nIndexOfReadOnlyFlag = -1;
138 sal_Int32 nIndexOfTemplateFlag = -1;
139 sal_Int32 nIndexOfDocumentTitle = -1;
141 for( sal_Int32 nProperty=0; nProperty<nPropertyCount; ++nProperty )
143 // extract properties
144 if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("URL")) )
146 lDescriptor[nProperty].Value >>= sTemp;
147 aURL = sTemp;
149 else if( !aURL.Len() && lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("FileName")) )
151 lDescriptor[nProperty].Value >>= sTemp;
152 aURL = sTemp;
154 else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("TypeName")) )
156 lDescriptor[nProperty].Value >>= sTemp;
157 aTypeName = sTemp;
159 else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("FilterName")) )
161 lDescriptor[nProperty].Value >>= sTemp;
162 aPreselectedFilterName = sTemp;
164 // if the preselected filter name is not correct, it must be erased after detection
165 // remember index of property to get access to it later
166 nIndexOfFilterName = nProperty;
168 else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("InputStream")) )
169 nIndexOfInputStream = nProperty;
170 else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("ReadOnly")) )
171 nIndexOfReadOnlyFlag = nProperty;
172 else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("UCBContent")) )
173 nIndexOfContent = nProperty;
174 else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("AsTemplate")) )
176 lDescriptor[nProperty].Value >>= bOpenAsTemplate;
177 nIndexOfTemplateFlag = nProperty;
179 else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("InteractionHandler")) )
180 lDescriptor[nProperty].Value >>= xInteraction;
181 else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("RapairPackage")) )
182 lDescriptor[nProperty].Value >>= bRepairPackage;
183 else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("DocumentTitle")) )
184 nIndexOfDocumentTitle = nProperty;
187 // can't check the type for external filters, so set the "dont" flag accordingly
188 ::vos::OGuard aGuard( Application::GetSolarMutex() );
189 //SfxFilterFlags nMust = SFX_FILTER_IMPORT, nDont = SFX_FILTER_NOTINSTALLED;
191 SfxApplication* pApp = SFX_APP();
192 SfxAllItemSet *pSet = new SfxAllItemSet( pApp->GetPool() );
193 TransformParameters( SID_OPENDOC, lDescriptor, *pSet );
194 SFX_ITEMSET_ARG( pSet, pItem, SfxBoolItem, SID_DOC_READONLY, FALSE );
196 bWasReadOnly = pItem && pItem->GetValue();
198 const SfxFilter* pFilter = 0;
199 String aFilterName;
200 String aPrefix = String::CreateFromAscii( "private:factory/" );
201 if( aURL.Match( aPrefix ) == aPrefix.Len() )
203 if( SvtModuleOptions().IsImpress() )
205 String aPattern( aPrefix );
206 aPattern += String::CreateFromAscii("simpress");
207 if ( aURL.Match( aPattern ) >= aPattern.Len() )
208 pFilter = SfxFilter::GetDefaultFilterFromFactory( aURL );
211 if( !pFilter && SvtModuleOptions().IsDraw() )
213 String aPattern( aPrefix );
214 aPattern += String::CreateFromAscii("sdraw");
215 if ( aURL.Match( aPattern ) >= aPattern.Len() )
216 pFilter = SfxFilter::GetDefaultFilterFromFactory( aURL );
219 else
221 // ctor of SfxMedium uses owner transition of ItemSet
222 SfxMedium aMedium( aURL, bWasReadOnly ? STREAM_STD_READ : STREAM_STD_READWRITE, FALSE, NULL, pSet );
223 aMedium.UseInteractionHandler( TRUE );
224 if ( aPreselectedFilterName.Len() )
225 pFilter = SfxFilter::GetFilterByName( aPreselectedFilterName );
226 else if( aTypeName.Len() )
228 SfxFilterMatcher aMatch;
229 pFilter = aMatch.GetFilter4EA( aTypeName );
232 if ( aMedium.GetErrorCode() == ERRCODE_NONE )
234 // remember input stream and content and put them into the descriptor later
235 // should be done here since later the medium can switch to a version
236 xStream = aMedium.GetInputStream();
237 xContent = aMedium.GetContent();
238 bReadOnly = aMedium.IsReadOnly();
239 BOOL bIsStorage = aMedium.IsStorage();
241 if (aMedium.GetError() == SVSTREAM_OK)
243 if ( bIsStorage )
245 // PowerPoint needs to be detected via StreamName, all other storage based formats are our own and can
246 // be detected by the ClipboardId, so except for the PPT filter all filters must have a ClipboardId set
247 uno::Reference < embed::XStorage > xStorage = aMedium.GetStorage( sal_False );
249 //TODO/LATER: move error handling to central place! (maybe even complete own filters)
250 if ( aMedium.GetLastStorageCreationState() != ERRCODE_NONE )
252 // error during storage creation means _here_ that the medium
253 // is broken, but we can not handle it in medium since unpossibility
254 // to create a storage does not _always_ means that the medium is broken
255 aMedium.SetError( aMedium.GetLastStorageCreationState(), ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ) );
256 if ( xInteraction.is() )
258 OUString empty;
261 InteractiveAppException xException( empty,
262 REFERENCE< XInterface >(),
263 InteractionClassification_ERROR,
264 aMedium.GetError() );
266 REFERENCE< XInteractionRequest > xRequest(
267 new ucbhelper::SimpleInteractionRequest( makeAny( xException ),
268 ucbhelper::CONTINUATION_APPROVE ) );
269 xInteraction->handle( xRequest );
271 catch ( Exception & ) {};
274 else
276 if ( pFilter && !pFilter->GetFormat() )
277 // preselected Filter has no ClipboardId -> doesn't match (see comment above)
278 pFilter = 0;
280 // the storage must be checked even if filter is already found, since it is deep type detection
281 // the storage can be corrupted and it will be detected here
284 String sFilterName;
285 if ( pFilter )
286 sFilterName = pFilter->GetName();
287 aTypeName = SfxFilter::GetTypeFromStorage( xStorage, pFilter ? pFilter->IsOwnTemplateFormat() : FALSE, &sFilterName );
289 catch( lang::WrappedTargetException& aWrap )
291 packages::zip::ZipIOException aZipException;
292 if ( ( aWrap.TargetException >>= aZipException ) && aTypeName.Len() )
294 if ( xInteraction.is() )
296 // the package is broken one
297 aDocumentTitle = aMedium.GetURLObject().getName(
298 INetURLObject::LAST_SEGMENT,
299 true,
300 INetURLObject::DECODE_WITH_CHARSET );
302 if ( !bRepairPackage )
304 // ask the user whether he wants to try to repair
305 RequestPackageReparation* pRequest = new RequestPackageReparation( aDocumentTitle );
306 uno::Reference< task::XInteractionRequest > xRequest ( pRequest );
308 xInteraction->handle( xRequest );
310 bRepairAllowed = pRequest->isApproved();
313 if ( !bRepairAllowed )
315 // repair either not allowed or not successful
316 NotifyBrokenPackage* pNotifyRequest = new NotifyBrokenPackage( aDocumentTitle );
317 uno::Reference< task::XInteractionRequest > xRequest ( pNotifyRequest );
318 xInteraction->handle( xRequest );
322 if ( !bRepairAllowed )
324 aTypeName.Erase();
325 pFilter = 0;
329 catch( uno::RuntimeException& )
331 throw;
333 catch( uno::Exception& )
335 aTypeName.Erase();
336 pFilter = 0;
339 if ( !pFilter && aTypeName.Len() )
341 //TODO/LATER: using this method impress is always preferred if no flat detecion has been made
342 // this should been discussed!
343 if ( SvtModuleOptions().IsImpress() )
344 pFilter = SfxFilterMatcher( String::CreateFromAscii("simpress") ).GetFilter4EA( aTypeName );
345 else if ( SvtModuleOptions().IsDraw() )
346 pFilter = SfxFilterMatcher( String::CreateFromAscii("sdraw") ).GetFilter4EA( aTypeName );
350 else
352 SvStream* pStm = aMedium.GetInStream();
353 if ( !pStm )
355 pFilter = 0;
357 else
359 SotStorageRef aStorage = new SotStorage ( pStm, FALSE );
360 if ( !aStorage->GetError() )
362 String aStreamName = UniString::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "PowerPoint Document" ) );
363 if ( aStorage->IsStream( aStreamName ) && SvtModuleOptions().IsImpress() )
365 String aFileName(aMedium.GetName());
366 aFileName.ToUpperAscii();
368 if( aFileName.SearchAscii( ".POT" ) != STRING_NOTFOUND )
369 pFilter = SfxFilter::GetFilterByName( pFilterPowerPoint97Template );
370 else if( aFileName.SearchAscii( ".PPS" ) != STRING_NOTFOUND )
371 pFilter = SfxFilter::GetFilterByName( pFilterPowerPoint97AutoPlay );
372 else
373 pFilter = SfxFilter::GetFilterByName( pFilterPowerPoint97);
376 else
378 // Vektorgraphik?
379 pStm->Seek( STREAM_SEEK_TO_BEGIN );
381 const String aFileName( aMedium.GetURLObject().GetMainURL( INetURLObject::NO_DECODE ) );
382 GraphicDescriptor aDesc( *pStm, &aFileName );
383 GraphicFilter* pGrfFilter = GetGrfFilter();
384 if( !aDesc.Detect( FALSE ) )
386 pFilter = 0;
387 if( SvtModuleOptions().IsImpress() )
389 INetURLObject aCheckURL( aFileName );
390 if( aCheckURL.getExtension().equalsIgnoreAsciiCaseAscii( "cgm" ) )
392 sal_uInt8 n8;
393 pStm->Seek( STREAM_SEEK_TO_BEGIN );
394 *pStm >> n8;
395 if ( ( n8 & 0xf0 ) == 0 ) // we are supporting binary cgm format only, so
396 { // this is a small test to exclude cgm text
397 const String aName = UniString::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "CGM - Computer Graphics Metafile" ) );
398 SfxFilterMatcher aMatch( String::CreateFromAscii("simpress") );
399 pFilter = aMatch.GetFilter4FilterName( aName );
404 else
406 String aShortName( aDesc.GetImportFormatShortName( aDesc.GetFileFormat() ) );
407 const String aName( pGrfFilter->GetImportFormatTypeName( pGrfFilter->GetImportFormatNumberForShortName( aShortName ) ) );
409 if ( pFilter && aShortName.EqualsIgnoreCaseAscii( "PCD" ) ) // there is a multiple pcd selection possible
411 sal_Int32 nBase = 2; // default Base0
412 String aFilterTypeName( pFilter->GetRealTypeName() );
413 if ( aFilterTypeName.CompareToAscii( "pcd_Photo_CD_Base4" ) == COMPARE_EQUAL )
414 nBase = 1;
415 else if ( aFilterTypeName.CompareToAscii( "pcd_Photo_CD_Base16" ) == COMPARE_EQUAL )
416 nBase = 0;
417 String aFilterConfigPath( RTL_CONSTASCII_USTRINGPARAM( "Office.Common/Filter/Graphic/Import/PCD" ) );
418 FilterConfigItem aFilterConfigItem( aFilterConfigPath );
419 aFilterConfigItem.WriteInt32( String( RTL_CONSTASCII_USTRINGPARAM( "Resolution" ) ), nBase );
422 SfxFilterMatcher aMatch( String::CreateFromAscii("sdraw") );
423 pFilter = aMatch.GetFilter4FilterName( aName );
432 if ( nIndexOfInputStream == -1 && xStream.is() )
434 // if input stream wasn't part of the descriptor, now it should be, otherwise the content would be opend twice
435 lDescriptor.realloc( nPropertyCount + 1 );
436 lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("InputStream");
437 lDescriptor[nPropertyCount].Value <<= xStream;
438 nPropertyCount++;
441 if ( nIndexOfContent == -1 && xContent.is() )
443 // if input stream wasn't part of the descriptor, now it should be, otherwise the content would be opend twice
444 lDescriptor.realloc( nPropertyCount + 1 );
445 lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("UCBContent");
446 lDescriptor[nPropertyCount].Value <<= xContent;
447 nPropertyCount++;
450 if ( bReadOnly != bWasReadOnly )
452 if ( nIndexOfReadOnlyFlag == -1 )
454 lDescriptor.realloc( nPropertyCount + 1 );
455 lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("ReadOnly");
456 lDescriptor[nPropertyCount].Value <<= bReadOnly;
457 nPropertyCount++;
459 else
460 lDescriptor[nIndexOfReadOnlyFlag].Value <<= bReadOnly;
463 if ( !bRepairPackage && bRepairAllowed )
465 lDescriptor.realloc( nPropertyCount + 1 );
466 lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("RepairPackage");
467 lDescriptor[nPropertyCount].Value <<= bRepairAllowed;
468 nPropertyCount++;
470 bOpenAsTemplate = sal_True;
472 // TODO/LATER: set progress bar that should be used
475 if ( bOpenAsTemplate )
477 if ( nIndexOfTemplateFlag == -1 )
479 lDescriptor.realloc( nPropertyCount + 1 );
480 lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("AsTemplate");
481 lDescriptor[nPropertyCount].Value <<= bOpenAsTemplate;
482 nPropertyCount++;
484 else
485 lDescriptor[nIndexOfTemplateFlag].Value <<= bOpenAsTemplate;
488 if ( aDocumentTitle.getLength() )
490 // the title was set here
491 if ( nIndexOfDocumentTitle == -1 )
493 lDescriptor.realloc( nPropertyCount + 1 );
494 lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("DocumentTitle");
495 lDescriptor[nPropertyCount].Value <<= aDocumentTitle;
496 nPropertyCount++;
498 else
499 lDescriptor[nIndexOfDocumentTitle].Value <<= aDocumentTitle;
502 if ( pFilter )
503 aTypeName = pFilter->GetTypeName();
504 else
505 aTypeName.Erase();
507 return aTypeName;
510 SFX_IMPL_SINGLEFACTORY( SdFilterDetect )
512 /* XServiceInfo */
513 UNOOUSTRING SAL_CALL SdFilterDetect::getImplementationName() throw( UNORUNTIMEEXCEPTION )
515 return impl_getStaticImplementationName();
518 /* XServiceInfo */
519 sal_Bool SAL_CALL SdFilterDetect::supportsService( const UNOOUSTRING& sServiceName ) throw( UNORUNTIMEEXCEPTION )
521 UNOSEQUENCE< UNOOUSTRING > seqServiceNames = getSupportedServiceNames();
522 const UNOOUSTRING* pArray = seqServiceNames.getConstArray();
523 for ( sal_Int32 nCounter=0; nCounter<seqServiceNames.getLength(); nCounter++ )
525 if ( pArray[nCounter] == sServiceName )
527 return sal_True ;
530 return sal_False ;
533 /* XServiceInfo */
534 UNOSEQUENCE< UNOOUSTRING > SAL_CALL SdFilterDetect::getSupportedServiceNames() throw( UNORUNTIMEEXCEPTION )
536 return impl_getStaticSupportedServiceNames();
539 /* Helper for XServiceInfo */
540 UNOSEQUENCE< UNOOUSTRING > SdFilterDetect::impl_getStaticSupportedServiceNames()
542 UNOMUTEXGUARD aGuard( UNOMUTEX::getGlobalMutex() );
543 UNOSEQUENCE< UNOOUSTRING > seqServiceNames( 1 );
544 seqServiceNames.getArray() [0] = UNOOUSTRING::createFromAscii( "com.sun.star.frame.ExtendedTypeDetection" );
545 return seqServiceNames ;
548 /* Helper for XServiceInfo */
549 UNOOUSTRING SdFilterDetect::impl_getStaticImplementationName()
551 return UNOOUSTRING::createFromAscii( "com.sun.star.comp.draw.FormatDetector" );
554 /* Helper for registry */
555 UNOREFERENCE< UNOXINTERFACE > SAL_CALL SdFilterDetect::impl_createInstance( const UNOREFERENCE< UNOXMULTISERVICEFACTORY >& xServiceManager ) throw( UNOEXCEPTION )
557 return UNOREFERENCE< UNOXINTERFACE >( *new SdFilterDetect( xServiceManager ) );