Bump version to 4.1-6
[LibreOffice.git] / sfx2 / source / view / frmload.cxx
blob03f242bd8b3c2a23ee606f36c87a9d97b990b249
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <sal/macros.h>
22 #include "frmload.hxx"
23 #include "objshimp.hxx"
24 #include "sfx2/app.hxx"
25 #include "sfx2/dispatch.hxx"
26 #include "sfx2/docfac.hxx"
27 #include "sfx2/docfile.hxx"
28 #include "sfx2/docfilt.hxx"
29 #include "sfx2/doctempl.hxx"
30 #include "sfx2/fcontnr.hxx"
31 #include "sfx2/frame.hxx"
32 #include "sfx2/request.hxx"
33 #include "sfx2/sfx.hrc"
34 #include "sfx2/sfxsids.hrc"
35 #include "sfx2/sfxuno.hxx"
36 #include "sfx2/viewfrm.hxx"
37 #include "sfx2/viewsh.hxx"
38 #include "sfx2/viewfac.hxx"
40 #include <com/sun/star/container/XContainerQuery.hpp>
41 #include <com/sun/star/document/XTypeDetection.hpp>
42 #include <com/sun/star/frame/XFrame.hpp>
43 #include <com/sun/star/frame/XLoadable.hpp>
44 #include <com/sun/star/frame/XModel.hpp>
45 #include <com/sun/star/task/XInteractionHandler2.hpp>
46 #include <com/sun/star/document/XViewDataSupplier.hpp>
47 #include <com/sun/star/container/XIndexAccess.hpp>
49 #include <comphelper/interaction.hxx>
50 #include <comphelper/namedvaluecollection.hxx>
51 #include <comphelper/sequenceashashmap.hxx>
52 #include <comphelper/processfactory.hxx>
53 #include <cppuhelper/exc_hlp.hxx>
54 #include <framework/interaction.hxx>
55 #include <rtl/logfile.hxx>
56 #include <rtl/ustring.h>
57 #include <sot/storinfo.hxx>
58 #include <svtools/ehdl.hxx>
59 #include <svl/eitem.hxx>
60 #include <svl/itemset.hxx>
61 #include <unotools/moduleoptions.hxx>
62 #include <svtools/sfxecode.hxx>
63 #include <svl/stritem.hxx>
64 #include <toolkit/helper/vclunohelper.hxx>
65 #include <tools/diagnose_ex.h>
66 #include <ucbhelper/simpleinteractionrequest.hxx>
67 #include <osl/mutex.hxx>
69 using ::com::sun::star::beans::PropertyValue;
70 using ::com::sun::star::container::XContainerQuery;
71 using ::com::sun::star::container::XEnumeration;
72 using ::com::sun::star::document::XTypeDetection;
73 using ::com::sun::star::frame::XFrame;
74 using ::com::sun::star::frame::XLoadable;
75 using ::com::sun::star::frame::XModel;
76 using ::com::sun::star::lang::XMultiServiceFactory;
77 using ::com::sun::star::task::XInteractionHandler;
78 using ::com::sun::star::task::XInteractionHandler2;
79 using ::com::sun::star::task::XInteractionRequest;
80 using ::com::sun::star::task::XStatusIndicator;
81 using ::com::sun::star::uno::Any;
82 using ::com::sun::star::uno::Exception;
83 using ::com::sun::star::uno::Reference;
84 using ::com::sun::star::uno::RuntimeException;
85 using ::com::sun::star::uno::Sequence;
86 using ::com::sun::star::uno::UNO_QUERY;
87 using ::com::sun::star::uno::UNO_QUERY_THROW;
88 using ::com::sun::star::uno::UNO_SET_THROW;
89 using ::com::sun::star::uno::makeAny;
90 using ::com::sun::star::uno::XComponentContext;
91 using ::com::sun::star::util::XCloseable;
92 using ::com::sun::star::document::XViewDataSupplier;
93 using ::com::sun::star::container::XIndexAccess;
94 using ::com::sun::star::frame::XController2;
95 using ::com::sun::star::frame::XController;
96 using ::com::sun::star::frame::XModel2;
98 SfxFrameLoader_Impl::SfxFrameLoader_Impl( const Reference< XComponentContext >& _rxContext )
99 :m_aContext( _rxContext )
103 SfxFrameLoader_Impl::~SfxFrameLoader_Impl()
107 // --------------------------------------------------------------------------------------------------------------------
108 const SfxFilter* SfxFrameLoader_Impl::impl_detectFilterForURL( const OUString& sURL,
109 const ::comphelper::NamedValueCollection& i_rDescriptor, const SfxFilterMatcher& rMatcher ) const
111 OUString sFilter;
114 if ( sURL.isEmpty() )
115 return 0;
117 Reference< XTypeDetection > xDetect(
118 m_aContext->getServiceManager()->createInstanceWithContext("com.sun.star.document.TypeDetection", m_aContext),
119 UNO_QUERY_THROW);
121 ::comphelper::NamedValueCollection aNewArgs;
122 aNewArgs.put( "URL", sURL );
124 if ( i_rDescriptor.has( "InteractionHandler" ) )
125 aNewArgs.put( "InteractionHandler", i_rDescriptor.get( "InteractionHandler" ) );
126 if ( i_rDescriptor.has( "StatusIndicator" ) )
127 aNewArgs.put( "StatusIndicator", i_rDescriptor.get( "StatusIndicator" ) );
129 Sequence< PropertyValue > aQueryArgs( aNewArgs.getPropertyValues() );
130 OUString sType = xDetect->queryTypeByDescriptor( aQueryArgs, sal_True );
131 if ( !sType.isEmpty() )
133 const SfxFilter* pFilter = rMatcher.GetFilter4EA( sType );
134 if ( pFilter )
135 sFilter = pFilter->GetName();
138 catch ( const RuntimeException& )
140 throw;
142 catch( const Exception& )
144 DBG_UNHANDLED_EXCEPTION();
145 sFilter = OUString();
148 const SfxFilter* pFilter = 0;
149 if (!sFilter.isEmpty())
150 pFilter = rMatcher.GetFilter4FilterName(sFilter);
151 return pFilter;
154 // --------------------------------------------------------------------------------------------------------------------
155 const SfxFilter* SfxFrameLoader_Impl::impl_getFilterFromServiceName_nothrow( const OUString& i_rServiceName ) const
159 ::comphelper::NamedValueCollection aQuery;
160 aQuery.put( "DocumentService", i_rServiceName );
162 const Reference< XContainerQuery > xQuery(
163 m_aContext->getServiceManager()->createInstanceWithContext("com.sun.star.document.FilterFactory", m_aContext),
164 UNO_QUERY_THROW );
166 const SfxFilterMatcher& rMatcher = SFX_APP()->GetFilterMatcher();
167 const SfxFilterFlags nMust = SFX_FILTER_IMPORT;
168 const SfxFilterFlags nDont = SFX_FILTER_NOTINSTALLED;
170 Reference < XEnumeration > xEnum( xQuery->createSubSetEnumerationByProperties(
171 aQuery.getNamedValues() ), UNO_SET_THROW );
172 while ( xEnum->hasMoreElements() )
174 ::comphelper::NamedValueCollection aType( xEnum->nextElement() );
175 OUString sFilterName = aType.getOrDefault( "Name", OUString() );
176 if ( sFilterName.isEmpty() )
177 continue;
179 const SfxFilter* pFilter = rMatcher.GetFilter4FilterName( sFilterName );
180 if ( !pFilter )
181 continue;
183 SfxFilterFlags nFlags = pFilter->GetFilterFlags();
184 if ( ( ( nFlags & nMust ) == nMust )
185 && ( ( nFlags & nDont ) == 0 )
188 return pFilter;
192 catch( const Exception& )
194 DBG_UNHANDLED_EXCEPTION();
196 return NULL;
199 // --------------------------------------------------------------------------------------------------------------------
200 OUString SfxFrameLoader_Impl::impl_askForFilter_nothrow( const Reference< XInteractionHandler >& i_rxHandler,
201 const OUString& i_rDocumentURL ) const
203 ENSURE_OR_THROW( i_rxHandler.is(), "invalid interaction handler" );
205 OUString sFilterName;
208 ::framework::RequestFilterSelect aRequest( i_rDocumentURL );
209 i_rxHandler->handle( aRequest.GetRequest() );
210 if( !aRequest.isAbort() )
211 sFilterName = aRequest.getFilter();
213 catch( const Exception& )
215 DBG_UNHANDLED_EXCEPTION();
218 return sFilterName;
221 // --------------------------------------------------------------------------------------------------------------------
222 namespace
224 sal_Bool lcl_getDispatchResult( const SfxPoolItem* _pResult )
226 if ( !_pResult )
227 return sal_False;
229 // default must be set to true, because some return values
230 // cant be checked, but nonetheless indicate "success"!
231 sal_Bool bSuccess = sal_True;
233 // On the other side some special slots return a boolean state,
234 // which can be set to FALSE.
235 SfxBoolItem *pItem = PTR_CAST( SfxBoolItem, _pResult );
236 if ( pItem )
237 bSuccess = pItem->GetValue();
239 return bSuccess;
243 // --------------------------------------------------------------------------------------------------------------------
244 sal_Bool SfxFrameLoader_Impl::impl_createNewDocWithSlotParam( const sal_uInt16 _nSlotID, const Reference< XFrame >& i_rxFrame,
245 const bool i_bHidden )
247 SfxRequest aRequest( _nSlotID, SFX_CALLMODE_SYNCHRON, SFX_APP()->GetPool() );
248 aRequest.AppendItem( SfxUnoFrameItem( SID_FILLFRAME, i_rxFrame ) );
249 if ( i_bHidden )
250 aRequest.AppendItem( SfxBoolItem( SID_HIDDEN, sal_True ) );
251 return lcl_getDispatchResult( SFX_APP()->ExecuteSlot( aRequest ) );
254 // --------------------------------------------------------------------------------------------------------------------
255 void SfxFrameLoader_Impl::impl_determineFilter( ::comphelper::NamedValueCollection& io_rDescriptor ) const
257 const OUString sURL = io_rDescriptor.getOrDefault( "URL", OUString() );
258 const OUString sTypeName = io_rDescriptor.getOrDefault( "TypeName", OUString() );
259 const OUString sFilterName = io_rDescriptor.getOrDefault( "FilterName", OUString() );
260 const OUString sServiceName = io_rDescriptor.getOrDefault( "DocumentService", OUString() );
261 const Reference< XInteractionHandler >
262 xInteraction = io_rDescriptor.getOrDefault( "InteractionHandler", Reference< XInteractionHandler >() );
264 const SfxFilterMatcher& rMatcher = SFX_APP()->GetFilterMatcher();
265 const SfxFilter* pFilter = NULL;
267 // get filter by its name directly ...
268 if ( !sFilterName.isEmpty() )
269 pFilter = rMatcher.GetFilter4FilterName( sFilterName );
271 // or search the preferred filter for the detected type ...
272 if ( !pFilter && !sTypeName.isEmpty() )
273 pFilter = rMatcher.GetFilter4EA( sTypeName );
275 // or use given document service for detection, too
276 if ( !pFilter && !sServiceName.isEmpty() )
277 pFilter = impl_getFilterFromServiceName_nothrow( sServiceName );
279 // or use interaction to ask user for right filter.
280 if ( !pFilter && xInteraction.is() && !sURL.isEmpty() )
282 OUString sSelectedFilter = impl_askForFilter_nothrow( xInteraction, sURL );
283 if ( !sSelectedFilter.isEmpty() )
284 pFilter = rMatcher.GetFilter4FilterName( sSelectedFilter );
287 if ( pFilter )
289 io_rDescriptor.put( "FilterName", OUString( pFilter->GetFilterName() ) );
291 // If detected filter indicates using of an own template format
292 // add property "AsTemplate" to descriptor. But suppress this step
293 // if such property already exists.
294 if ( pFilter->IsOwnTemplateFormat() && !io_rDescriptor.has( "AsTemplate" ) )
295 io_rDescriptor.put( "AsTemplate", sal_Bool( sal_True ) );
297 // The DocumentService property will finally be used to determine the document type to create, so
298 // override it with the service name as indicated by the found filter.
299 io_rDescriptor.put( "DocumentService", OUString( pFilter->GetServiceName() ) );
303 // --------------------------------------------------------------------------------------------------------------------
304 SfxObjectShellRef SfxFrameLoader_Impl::impl_findObjectShell( const Reference< XModel2 >& i_rxDocument ) const
306 for ( SfxObjectShell* pDoc = SfxObjectShell::GetFirst( NULL, sal_False ); pDoc; pDoc = SfxObjectShell::GetNext( *pDoc, NULL, sal_False ) )
308 if ( i_rxDocument == pDoc->GetModel() )
310 return pDoc;
314 OSL_FAIL( "SfxFrameLoader_Impl::impl_findObjectShell: model is not based on SfxObjectShell - wrong frame loader usage!" );
315 return NULL;
318 // --------------------------------------------------------------------------------------------------------------------
319 bool SfxFrameLoader_Impl::impl_determineTemplateDocument( ::comphelper::NamedValueCollection& io_rDescriptor ) const
321 const OUString sTemplateRegioName = io_rDescriptor.getOrDefault( "TemplateRegionName", OUString() );
322 const OUString sTemplateName = io_rDescriptor.getOrDefault( "TemplateName", OUString() );
323 const OUString sServiceName = io_rDescriptor.getOrDefault( "DocumentService", OUString() );
324 const OUString sURL = io_rDescriptor.getOrDefault( "URL", OUString() );
326 // determine the full URL of the template to use, if any
327 String sTemplateURL;
328 if ( !sTemplateRegioName.isEmpty() && !sTemplateName.isEmpty() )
330 SfxDocumentTemplates aTmpFac;
331 aTmpFac.GetFull( sTemplateRegioName, sTemplateName, sTemplateURL );
333 else
335 if ( !sServiceName.isEmpty() )
336 sTemplateURL = SfxObjectFactory::GetStandardTemplate( sServiceName );
337 else
338 sTemplateURL = SfxObjectFactory::GetStandardTemplate( SfxObjectShell::GetServiceNameFromFactory( sURL ) );
341 if ( sTemplateURL.Len() > 0 )
343 // detect the filter for the template. Might still be NULL (if the template is broken, or does not
344 // exist, or some such), but this is handled by our caller the same way as if no template/URL was present.
345 const SfxFilter* pTemplateFilter = impl_detectFilterForURL( sTemplateURL, io_rDescriptor, SFX_APP()->GetFilterMatcher() );
346 if ( pTemplateFilter )
348 // load the template document, but, well, "as template"
349 io_rDescriptor.put( "FilterName", OUString( pTemplateFilter->GetName() ) );
350 io_rDescriptor.put( "FileName", OUString( sTemplateURL ) );
351 io_rDescriptor.put( "AsTemplate", sal_True );
353 // #i21583#
354 // the DocumentService property will finally be used to create the document. Thus, override any possibly
355 // present value with the document service of the template.
356 io_rDescriptor.put( "DocumentService", OUString( pTemplateFilter->GetServiceName() ) );
357 return true;
360 return false;
363 // --------------------------------------------------------------------------------------------------------------------
364 sal_uInt16 SfxFrameLoader_Impl::impl_findSlotParam( const OUString& i_rFactoryURL ) const
366 OUString sSlotParam;
367 const sal_Int32 nParamPos = i_rFactoryURL.indexOf( '?' );
368 if ( nParamPos >= 0 )
370 // currently only the "slot" parameter is supported
371 const sal_Int32 nSlotPos = i_rFactoryURL.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "slot=" ), nParamPos );
372 if ( nSlotPos > 0 )
373 sSlotParam = i_rFactoryURL.copy( nSlotPos + 5 );
376 if ( !sSlotParam.isEmpty() )
377 return sal_uInt16( sSlotParam.toInt32() );
379 return 0;
382 // --------------------------------------------------------------------------------------------------------------------
383 void SfxFrameLoader_Impl::impl_handleCaughtError_nothrow( const Any& i_rCaughtError, const ::comphelper::NamedValueCollection& i_rDescriptor ) const
387 const Reference< XInteractionHandler > xInteraction =
388 i_rDescriptor.getOrDefault( "InteractionHandler", Reference< XInteractionHandler >() );
389 if ( !xInteraction.is() )
390 return;
391 ::rtl::Reference< ::comphelper::OInteractionRequest > pRequest( new ::comphelper::OInteractionRequest( i_rCaughtError ) );
392 ::rtl::Reference< ::comphelper::OInteractionApprove > pApprove( new ::comphelper::OInteractionApprove );
393 pRequest->addContinuation( pApprove.get() );
395 const Reference< XInteractionHandler2 > xHandler( xInteraction, UNO_QUERY );
396 #if OSL_DEBUG_LEVEL > 0
397 const sal_Bool bHandled =
398 #endif
399 xHandler.is() && xHandler->handleInteractionRequest( pRequest.get() );
401 #if OSL_DEBUG_LEVEL > 0
402 if ( !bHandled )
403 // the interaction handler couldn't deal with this error
404 // => report it as assertion, at least (done in the DBG_UNHANDLED_EXCEPTION below)
405 ::cppu::throwException( i_rCaughtError );
406 #endif
408 catch( const Exception& )
410 DBG_UNHANDLED_EXCEPTION();
414 // --------------------------------------------------------------------------------------------------------------------
415 void SfxFrameLoader_Impl::impl_removeLoaderArguments( ::comphelper::NamedValueCollection& io_rDescriptor )
417 // remove the arguments which are for the loader only, and not for a call to attachResource
418 io_rDescriptor.remove( "StatusIndicator" );
419 io_rDescriptor.remove( "Model" );
422 // --------------------------------------------------------------------------------------------------------------------
423 ::comphelper::NamedValueCollection SfxFrameLoader_Impl::impl_extractViewCreationArgs( ::comphelper::NamedValueCollection& io_rDescriptor )
425 const sal_Char* pKnownViewArgs[] = {
426 "JumpMark"
429 ::comphelper::NamedValueCollection aViewArgs;
430 for ( size_t i=0; i < sizeof( pKnownViewArgs ) / sizeof( pKnownViewArgs[0] ); ++i )
432 if ( io_rDescriptor.has( pKnownViewArgs[i] ) )
434 aViewArgs.put( pKnownViewArgs[i], io_rDescriptor.get( pKnownViewArgs[i] ) );
435 io_rDescriptor.remove( pKnownViewArgs[i] );
438 return aViewArgs;
441 // --------------------------------------------------------------------------------------------------------------------
442 sal_Int16 SfxFrameLoader_Impl::impl_determineEffectiveViewId_nothrow( const SfxObjectShell& i_rDocument, const ::comphelper::NamedValueCollection& i_rDescriptor )
444 sal_Int16 nViewId = i_rDescriptor.getOrDefault( "ViewId", sal_Int16( 0 ) );
447 if ( nViewId == 0 ) do
449 Reference< XViewDataSupplier > xViewDataSupplier( i_rDocument.GetModel(), UNO_QUERY );
450 Reference< XIndexAccess > xViewData;
451 if ( xViewDataSupplier.is() )
452 xViewData.set( xViewDataSupplier->getViewData() );
454 if ( !xViewData.is() || ( xViewData->getCount() == 0 ) )
455 // no view data stored together with the model
456 break;
458 // obtain the ViewID from the view data
459 Sequence< PropertyValue > aViewData;
460 if ( !( xViewData->getByIndex( 0 ) >>= aViewData ) )
461 break;
463 ::comphelper::NamedValueCollection aNamedViewData( aViewData );
464 OUString sViewId = aNamedViewData.getOrDefault( "ViewId", OUString() );
465 if ( sViewId.isEmpty() )
466 break;
468 // somewhat weird convention here ... in the view data, the ViewId is a string, effectively describing
469 // a view name. In the document load descriptor, the ViewId is in fact the numeric ID.
471 SfxViewFactory* pViewFactory = i_rDocument.GetFactory().GetViewFactoryByViewName( sViewId );
472 if ( pViewFactory )
473 nViewId = sal_Int16( pViewFactory->GetOrdinal() );
475 while ( false );
477 catch( const Exception& )
479 DBG_UNHANDLED_EXCEPTION();
482 if ( nViewId == 0 )
483 nViewId = i_rDocument.GetFactory().GetViewFactory( 0 ).GetOrdinal();
484 return nViewId;
487 // --------------------------------------------------------------------------------------------------------------------
488 Reference< XController2 > SfxFrameLoader_Impl::impl_createDocumentView( const Reference< XModel2 >& i_rModel,
489 const Reference< XFrame >& i_rFrame, const ::comphelper::NamedValueCollection& i_rViewFactoryArgs,
490 const OUString& i_rViewName )
492 // let the model create a new controller
493 const Reference< XController2 > xController( i_rModel->createViewController(
494 i_rViewName,
495 i_rViewFactoryArgs.getPropertyValues(),
496 i_rFrame
497 ), UNO_SET_THROW );
499 // introduce model/view/controller to each other
500 xController->attachModel( i_rModel.get() );
501 i_rModel->connectController( xController.get() );
502 i_rFrame->setComponent( xController->getComponentWindow(), xController.get() );
503 xController->attachFrame( i_rFrame );
504 i_rModel->setCurrentController( xController.get() );
506 return xController;
509 // --------------------------------------------------------------------------------------------------------------------
510 sal_Bool SAL_CALL SfxFrameLoader_Impl::load( const Sequence< PropertyValue >& rArgs,
511 const Reference< XFrame >& _rTargetFrame )
512 throw( RuntimeException )
514 ENSURE_OR_THROW( _rTargetFrame.is(), "illegal NULL frame" );
516 SolarMutexGuard aGuard;
518 RTL_LOGFILE_CONTEXT( aLog, "sfx2 (mb93783) ::SfxFrameLoader::load" );
520 ::comphelper::NamedValueCollection aDescriptor( rArgs );
522 // ensure the descriptor contains a referrer
523 if ( !aDescriptor.has( "Referer" ) )
524 aDescriptor.put( "Referer", OUString() );
526 // did the caller already pass a model?
527 Reference< XModel2 > xModel = aDescriptor.getOrDefault( "Model", Reference< XModel2 >() );
528 const bool bExternalModel = xModel.is();
530 // check for factory URLs to create a new doc, instead of loading one
531 const OUString sURL = aDescriptor.getOrDefault( "URL", OUString() );
532 const bool bIsFactoryURL = sURL.startsWith( "private:factory/" );
533 bool bInitNewModel = bIsFactoryURL;
534 if ( bIsFactoryURL && !bExternalModel )
536 const OUString sFactory = sURL.copy( sizeof( "private:factory/" ) -1 );
537 // special handling for some weird factory URLs a la private:factory/swriter?slot=21053
538 const sal_uInt16 nSlotParam = impl_findSlotParam( sFactory );
539 if ( nSlotParam != 0 )
541 return impl_createNewDocWithSlotParam( nSlotParam, _rTargetFrame, aDescriptor.getOrDefault( "Hidden", false ) );
544 const bool bDescribesValidTemplate = impl_determineTemplateDocument( aDescriptor );
545 if ( bDescribesValidTemplate )
547 // if the media descriptor allowed us to determine a template document to create the new document
548 // from, then do not init a new document model from scratch (below), but instead load the
549 // template document
550 bInitNewModel = false;
552 else
554 const OUString sServiceName = SfxObjectShell::GetServiceNameFromFactory( sFactory );
555 aDescriptor.put( "DocumentService", sServiceName );
558 else
560 // compatibility
561 aDescriptor.put( "FileName", aDescriptor.get( "URL" ) );
564 sal_Bool bLoadSuccess = sal_False;
567 // extract view releant arguments from the loader args
568 ::comphelper::NamedValueCollection aViewCreationArgs( impl_extractViewCreationArgs( aDescriptor ) );
570 // no model passed from outside? => create one from scratch
571 if ( !xModel.is() )
573 bool bInternalFilter = aDescriptor.getOrDefault<OUString>("FilterProvider", OUString()).isEmpty();
575 if (bInternalFilter && !bInitNewModel)
577 // Ensure that the current SfxFilter instance is loaded before
578 // going further. We don't need to do this for external
579 // filter providers.
580 impl_determineFilter(aDescriptor);
583 // create the new doc
584 const OUString sServiceName = aDescriptor.getOrDefault( "DocumentService", OUString() );
585 xModel.set( m_aContext->getServiceManager()->createInstanceWithContext(sServiceName, m_aContext), UNO_QUERY_THROW );
587 // load resp. init it
588 const Reference< XLoadable > xLoadable( xModel, UNO_QUERY_THROW );
589 if ( bInitNewModel )
591 xLoadable->initNew();
593 impl_removeLoaderArguments( aDescriptor );
594 xModel->attachResource( OUString(), aDescriptor.getPropertyValues() );
596 else
598 xLoadable->load( aDescriptor.getPropertyValues() );
601 else
603 // tell the doc its (current) load args.
604 impl_removeLoaderArguments( aDescriptor );
605 xModel->attachResource( xModel->getURL(), aDescriptor.getPropertyValues() );
608 // get the SfxObjectShell (still needed at the moment)
609 // SfxObjectShellRef is used here ( instead of ...Lock ) since the model is closed below if necessary
610 // SfxObjectShellLock would be even dangerous here, since the lifetime control should be done outside in case of success
611 const SfxObjectShellRef xDoc = impl_findObjectShell( xModel );
612 ENSURE_OR_THROW( xDoc.Is(), "no SfxObjectShell for the given model" );
614 // ensure the ID of the to-be-created view is in the descriptor, if possible
615 const sal_Int16 nViewId = impl_determineEffectiveViewId_nothrow( *xDoc, aDescriptor );
616 const sal_Int16 nViewNo = xDoc->GetFactory().GetViewNo_Impl( nViewId, 0 );
617 const OUString sViewName( xDoc->GetFactory().GetViewFactory( nViewNo ).GetAPIViewName() );
619 // plug the document into the frame
620 impl_createDocumentView( xModel, _rTargetFrame, aViewCreationArgs, sViewName );
621 bLoadSuccess = sal_True;
623 catch ( Exception& )
625 const Any aError( ::cppu::getCaughtException() );
626 if ( !aDescriptor.getOrDefault( "Silent", sal_False ) )
627 impl_handleCaughtError_nothrow( aError, aDescriptor );
630 // if loading was not successful, close the document
631 if ( !bLoadSuccess && !bExternalModel )
635 const Reference< XCloseable > xCloseable( xModel, UNO_QUERY_THROW );
636 xCloseable->close( sal_True );
638 catch ( Exception& )
640 DBG_UNHANDLED_EXCEPTION();
644 return bLoadSuccess;
647 void SfxFrameLoader_Impl::cancel() throw( RuntimeException )
651 SFX_IMPL_SINGLEFACTORY( SfxFrameLoader_Impl )
653 /* XServiceInfo */
654 OUString SAL_CALL SfxFrameLoader_Impl::getImplementationName() throw( RuntimeException )
656 return impl_getStaticImplementationName();
659 /* XServiceInfo */
660 sal_Bool SAL_CALL SfxFrameLoader_Impl::supportsService( const OUString& sServiceName ) throw( RuntimeException )
662 Sequence< OUString > seqServiceNames = getSupportedServiceNames();
663 const OUString* pArray = seqServiceNames.getConstArray();
664 for ( sal_Int32 nCounter=0; nCounter<seqServiceNames.getLength(); nCounter++ )
666 if ( pArray[nCounter] == sServiceName )
668 return sal_True ;
671 return sal_False ;
674 /* XServiceInfo */
675 Sequence< OUString > SAL_CALL SfxFrameLoader_Impl::getSupportedServiceNames() throw( RuntimeException )
677 return impl_getStaticSupportedServiceNames();
680 /* Helper for XServiceInfo */
681 Sequence< OUString > SfxFrameLoader_Impl::impl_getStaticSupportedServiceNames()
683 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
684 Sequence< OUString > seqServiceNames( 1 );
685 seqServiceNames.getArray() [0] = OUString( "com.sun.star.frame.SynchronousFrameLoader" );
686 return seqServiceNames ;
689 /* Helper for XServiceInfo */
690 OUString SfxFrameLoader_Impl::impl_getStaticImplementationName()
692 return OUString( "com.sun.star.comp.office.FrameLoader" );
695 /* Helper for registry */
696 Reference< css::uno::XInterface > SAL_CALL SfxFrameLoader_Impl::impl_createInstance( const Reference< XMultiServiceFactory >& xServiceManager ) throw( Exception )
698 return Reference< css::uno::XInterface >( *new SfxFrameLoader_Impl( comphelper::getComponentContext(xServiceManager) ) );
701 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */