Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : :
20 : :
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"
39 : :
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>
48 : :
49 : : #include <comphelper/interaction.hxx>
50 : : #include <comphelper/namedvaluecollection.hxx>
51 : : #include <comphelper/sequenceashashmap.hxx>
52 : : #include <cppuhelper/exc_hlp.hxx>
53 : : #include <framework/interaction.hxx>
54 : : #include <rtl/logfile.hxx>
55 : : #include <rtl/ustring.h>
56 : : #include <sot/storinfo.hxx>
57 : : #include <svtools/ehdl.hxx>
58 : : #include <svl/eitem.hxx>
59 : : #include <svl/itemset.hxx>
60 : : #include <unotools/moduleoptions.hxx>
61 : : #include <svtools/sfxecode.hxx>
62 : : #include <svl/stritem.hxx>
63 : : #include <toolkit/helper/vclunohelper.hxx>
64 : : #include <tools/diagnose_ex.h>
65 : : #include <ucbhelper/simpleinteractionrequest.hxx>
66 : : #include <osl/mutex.hxx>
67 : :
68 : : /** === begin UNO using === **/
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::util::XCloseable;
91 : : using ::com::sun::star::document::XViewDataSupplier;
92 : : using ::com::sun::star::container::XIndexAccess;
93 : : using ::com::sun::star::frame::XController2;
94 : : using ::com::sun::star::frame::XController;
95 : : using ::com::sun::star::frame::XModel2;
96 : : /** === end UNO using === **/
97 : :
98 : 1731 : SfxFrameLoader_Impl::SfxFrameLoader_Impl( const Reference< XMultiServiceFactory >& _rxFactory )
99 [ + - ]: 1731 : :m_aContext( _rxFactory )
100 : : {
101 : 1731 : }
102 : :
103 [ + - ]: 1731 : SfxFrameLoader_Impl::~SfxFrameLoader_Impl()
104 : : {
105 [ - + ]: 3462 : }
106 : :
107 : : // --------------------------------------------------------------------------------------------------------------------
108 : 0 : const SfxFilter* SfxFrameLoader_Impl::impl_detectFilterForURL( const ::rtl::OUString& sURL,
109 : : const ::comphelper::NamedValueCollection& i_rDescriptor, const SfxFilterMatcher& rMatcher ) const
110 : : {
111 : 0 : ::rtl::OUString sFilter;
112 : : try
113 : : {
114 [ # # ]: 0 : if ( sURL.isEmpty() )
115 : 0 : return 0;
116 : :
117 : : Reference< XTypeDetection > xDetect(
118 : : m_aContext.createComponent( "com.sun.star.document.TypeDetection" ),
119 [ # # ][ # # ]: 0 : UNO_QUERY_THROW);
120 : :
121 [ # # ]: 0 : ::comphelper::NamedValueCollection aNewArgs;
122 [ # # ]: 0 : aNewArgs.put( "URL", sURL );
123 : :
124 [ # # ][ # # ]: 0 : if ( i_rDescriptor.has( "InteractionHandler" ) )
125 [ # # ][ # # ]: 0 : aNewArgs.put( "InteractionHandler", i_rDescriptor.get( "InteractionHandler" ) );
126 [ # # ][ # # ]: 0 : if ( i_rDescriptor.has( "StatusIndicator" ) )
127 [ # # ][ # # ]: 0 : aNewArgs.put( "StatusIndicator", i_rDescriptor.get( "StatusIndicator" ) );
128 : :
129 [ # # ]: 0 : Sequence< PropertyValue > aQueryArgs( aNewArgs.getPropertyValues() );
130 [ # # ][ # # ]: 0 : ::rtl::OUString sType = xDetect->queryTypeByDescriptor( aQueryArgs, sal_True );
131 [ # # ]: 0 : if ( !sType.isEmpty() )
132 : : {
133 [ # # ][ # # ]: 0 : const SfxFilter* pFilter = rMatcher.GetFilter4EA( sType );
[ # # ]
134 [ # # ]: 0 : if ( pFilter )
135 [ # # ]: 0 : sFilter = pFilter->GetName();
136 [ # # ][ # # ]: 0 : }
137 : : }
138 [ # # # ]: 0 : catch ( const RuntimeException& )
139 : : {
140 : 0 : throw;
141 : : }
142 [ # # ]: 0 : catch( const Exception& )
143 : : {
144 : : DBG_UNHANDLED_EXCEPTION();
145 : 0 : sFilter = ::rtl::OUString();
146 : : }
147 : :
148 : 0 : const SfxFilter* pFilter = 0;
149 [ # # ]: 0 : if (!sFilter.isEmpty())
150 [ # # ][ # # ]: 0 : pFilter = rMatcher.GetFilter4FilterName(sFilter);
[ # # ]
151 : 0 : return pFilter;
152 : : }
153 : :
154 : : // --------------------------------------------------------------------------------------------------------------------
155 : 0 : const SfxFilter* SfxFrameLoader_Impl::impl_getFilterFromServiceName_nothrow( const ::rtl::OUString& i_rServiceName ) const
156 : : {
157 : : try
158 : : {
159 [ # # ]: 0 : ::comphelper::NamedValueCollection aQuery;
160 [ # # ]: 0 : aQuery.put( "DocumentService", i_rServiceName );
161 : :
162 : : const Reference< XContainerQuery > xQuery(
163 : : m_aContext.createComponent( "com.sun.star.document.FilterFactory" ),
164 [ # # ][ # # ]: 0 : UNO_QUERY_THROW );
165 : :
166 [ # # ][ # # ]: 0 : const SfxFilterMatcher& rMatcher = SFX_APP()->GetFilterMatcher();
167 : 0 : const SfxFilterFlags nMust = SFX_FILTER_IMPORT;
168 : 0 : const SfxFilterFlags nDont = SFX_FILTER_NOTINSTALLED;
169 : :
170 [ # # ]: 0 : Reference < XEnumeration > xEnum( xQuery->createSubSetEnumerationByProperties(
171 [ # # ][ # # ]: 0 : aQuery.getNamedValues() ), UNO_SET_THROW );
[ # # ][ # # ]
172 [ # # ][ # # ]: 0 : while ( xEnum->hasMoreElements() )
[ # # ]
173 : : {
174 [ # # ][ # # ]: 0 : ::comphelper::NamedValueCollection aType( xEnum->nextElement() );
[ # # ]
175 [ # # ]: 0 : ::rtl::OUString sFilterName = aType.getOrDefault( "Name", ::rtl::OUString() );
176 [ # # ]: 0 : if ( sFilterName.isEmpty() )
177 : 0 : continue;
178 : :
179 [ # # ][ # # ]: 0 : const SfxFilter* pFilter = rMatcher.GetFilter4FilterName( sFilterName );
[ # # ]
180 [ # # ]: 0 : if ( !pFilter )
181 : 0 : continue;
182 : :
183 : 0 : SfxFilterFlags nFlags = pFilter->GetFilterFlags();
184 [ # # ][ # # ]: 0 : if ( ( ( nFlags & nMust ) == nMust )
185 : : && ( ( nFlags & nDont ) == 0 )
186 : : )
187 : : {
188 : 0 : return pFilter;
189 : : }
190 [ # # # ]: 0 : }
[ # # ]
[ # # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
191 : : }
192 : 0 : catch( const Exception& )
193 : : {
194 : : DBG_UNHANDLED_EXCEPTION();
195 : : }
196 : 0 : return NULL;
197 : : }
198 : :
199 : : // --------------------------------------------------------------------------------------------------------------------
200 : 0 : ::rtl::OUString SfxFrameLoader_Impl::impl_askForFilter_nothrow( const Reference< XInteractionHandler >& i_rxHandler,
201 : : const ::rtl::OUString& i_rDocumentURL ) const
202 : : {
203 [ # # ][ # # ]: 0 : ENSURE_OR_THROW( i_rxHandler.is(), "invalid interaction handler" );
[ # # ][ # # ]
204 : :
205 : 0 : ::rtl::OUString sFilterName;
206 : : try
207 : : {
208 [ # # ]: 0 : ::framework::RequestFilterSelect aRequest( i_rDocumentURL );
209 [ # # ][ # # ]: 0 : i_rxHandler->handle( aRequest.GetRequest() );
[ # # ]
210 [ # # ][ # # ]: 0 : if( !aRequest.isAbort() )
211 [ # # ][ # # ]: 0 : sFilterName = aRequest.getFilter();
[ # # ]
212 : : }
213 [ # # ]: 0 : catch( const Exception& )
214 : : {
215 : : DBG_UNHANDLED_EXCEPTION();
216 : : }
217 : :
218 : 0 : return sFilterName;
219 : : }
220 : :
221 : : // --------------------------------------------------------------------------------------------------------------------
222 : : namespace
223 : : {
224 : 0 : sal_Bool lcl_getDispatchResult( const SfxPoolItem* _pResult )
225 : : {
226 [ # # ]: 0 : if ( !_pResult )
227 : 0 : return sal_False;
228 : :
229 : : // default must be set to true, because some return values
230 : : // cant be checked, but nonetheless indicate "success"!
231 : 0 : sal_Bool bSuccess = sal_True;
232 : :
233 : : // On the other side some special slots return a boolean state,
234 : : // which can be set to FALSE.
235 [ # # ][ # # ]: 0 : SfxBoolItem *pItem = PTR_CAST( SfxBoolItem, _pResult );
236 [ # # ]: 0 : if ( pItem )
237 : 0 : bSuccess = pItem->GetValue();
238 : :
239 : 0 : return bSuccess;
240 : : }
241 : : }
242 : :
243 : : // --------------------------------------------------------------------------------------------------------------------
244 : 0 : sal_Bool SfxFrameLoader_Impl::impl_createNewDocWithSlotParam( const sal_uInt16 _nSlotID, const Reference< XFrame >& i_rxFrame,
245 : : const bool i_bHidden )
246 : : {
247 [ # # ][ # # ]: 0 : SfxRequest aRequest( _nSlotID, SFX_CALLMODE_SYNCHRON, SFX_APP()->GetPool() );
248 [ # # ][ # # ]: 0 : aRequest.AppendItem( SfxUnoFrameItem( SID_FILLFRAME, i_rxFrame ) );
[ # # ]
249 [ # # ]: 0 : if ( i_bHidden )
250 [ # # ][ # # ]: 0 : aRequest.AppendItem( SfxBoolItem( SID_HIDDEN, sal_True ) );
[ # # ]
251 [ # # ][ # # ]: 0 : return lcl_getDispatchResult( SFX_APP()->ExecuteSlot( aRequest ) );
[ # # ][ # # ]
252 : : }
253 : :
254 : : // --------------------------------------------------------------------------------------------------------------------
255 : 614 : void SfxFrameLoader_Impl::impl_determineFilter( ::comphelper::NamedValueCollection& io_rDescriptor ) const
256 : : {
257 [ + - ]: 614 : const ::rtl::OUString sURL = io_rDescriptor.getOrDefault( "URL", ::rtl::OUString() );
258 [ + - ]: 614 : const ::rtl::OUString sTypeName = io_rDescriptor.getOrDefault( "TypeName", ::rtl::OUString() );
259 [ + - ]: 614 : const ::rtl::OUString sFilterName = io_rDescriptor.getOrDefault( "FilterName", ::rtl::OUString() );
260 [ + - ]: 614 : const ::rtl::OUString sServiceName = io_rDescriptor.getOrDefault( "DocumentService", ::rtl::OUString() );
261 : : const Reference< XInteractionHandler >
262 [ + - ]: 614 : xInteraction = io_rDescriptor.getOrDefault( "InteractionHandler", Reference< XInteractionHandler >() );
263 : :
264 [ + - ][ + - ]: 614 : const SfxFilterMatcher& rMatcher = SFX_APP()->GetFilterMatcher();
265 : 614 : const SfxFilter* pFilter = NULL;
266 : :
267 : : // get filter by its name directly ...
268 [ + - ]: 614 : if ( !sFilterName.isEmpty() )
269 [ + - ][ + - ]: 614 : pFilter = rMatcher.GetFilter4FilterName( sFilterName );
[ + - ]
270 : :
271 : : // or search the preferred filter for the detected type ...
272 [ - + ][ # # ]: 614 : if ( !pFilter && !sTypeName.isEmpty() )
[ - + ]
273 [ # # ][ # # ]: 0 : pFilter = rMatcher.GetFilter4EA( sTypeName );
[ # # ]
274 : :
275 : : // or use given document service for detection, too
276 [ - + ][ # # ]: 614 : if ( !pFilter && !sServiceName.isEmpty() )
[ - + ]
277 [ # # ]: 0 : pFilter = impl_getFilterFromServiceName_nothrow( sServiceName );
278 : :
279 : : // or use interaction to ask user for right filter.
280 [ - + ][ # # ]: 614 : if ( !pFilter && xInteraction.is() && !sURL.isEmpty() )
[ # # ][ - + ]
281 : : {
282 [ # # ]: 0 : ::rtl::OUString sSelectedFilter = impl_askForFilter_nothrow( xInteraction, sURL );
283 [ # # ]: 0 : if ( !sSelectedFilter.isEmpty() )
284 [ # # ][ # # ]: 0 : pFilter = rMatcher.GetFilter4FilterName( sSelectedFilter );
[ # # ]
285 : : }
286 : :
287 [ + - ]: 614 : if ( pFilter )
288 : : {
289 [ + - ][ + - ]: 614 : io_rDescriptor.put( "FilterName", ::rtl::OUString( pFilter->GetFilterName() ) );
290 : :
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 [ + - ][ - + ]: 614 : if ( pFilter->IsOwnTemplateFormat() && !io_rDescriptor.has( "AsTemplate" ) )
[ - + ][ + + ]
295 [ # # ]: 0 : io_rDescriptor.put( "AsTemplate", sal_Bool( sal_True ) );
296 : :
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 [ + - ]: 614 : io_rDescriptor.put( "DocumentService", ::rtl::OUString( pFilter->GetServiceName() ) );
300 : 614 : }
301 : 614 : }
302 : :
303 : : // --------------------------------------------------------------------------------------------------------------------
304 : 1727 : SfxObjectShellRef SfxFrameLoader_Impl::impl_findObjectShell( const Reference< XModel2 >& i_rxDocument ) const
305 : : {
306 [ + - ]: 5388 : for ( SfxObjectShell* pDoc = SfxObjectShell::GetFirst( NULL, sal_False ); pDoc; pDoc = SfxObjectShell::GetNext( *pDoc, NULL, sal_False ) )
307 : : {
308 [ + - ][ + + ]: 5388 : if ( i_rxDocument == pDoc->GetModel() )
309 : : {
310 : 1727 : return pDoc;
311 : : }
312 : : }
313 : :
314 : : OSL_FAIL( "SfxFrameLoader_Impl::impl_findObjectShell: model is not based on SfxObjectShell - wrong frame loader usage!" );
315 : 1727 : return NULL;
316 : : }
317 : :
318 : : // --------------------------------------------------------------------------------------------------------------------
319 : 1093 : bool SfxFrameLoader_Impl::impl_determineTemplateDocument( ::comphelper::NamedValueCollection& io_rDescriptor ) const
320 : : {
321 [ + - ]: 1093 : const ::rtl::OUString sTemplateRegioName = io_rDescriptor.getOrDefault( "TemplateRegionName", ::rtl::OUString() );
322 [ + - ]: 1093 : const ::rtl::OUString sTemplateName = io_rDescriptor.getOrDefault( "TemplateName", ::rtl::OUString() );
323 [ + - ]: 1093 : const ::rtl::OUString sServiceName = io_rDescriptor.getOrDefault( "DocumentService", ::rtl::OUString() );
324 [ + - ]: 1093 : const ::rtl::OUString sURL = io_rDescriptor.getOrDefault( "URL", ::rtl::OUString() );
325 : :
326 : : // determine the full URL of the template to use, if any
327 [ + - ]: 1093 : String sTemplateURL;
328 [ - + ][ # # ]: 1093 : if ( !sTemplateRegioName.isEmpty() && !sTemplateName.isEmpty() )
[ - + ]
329 : : {
330 [ # # ]: 0 : SfxDocumentTemplates aTmpFac;
331 [ # # ][ # # ]: 0 : aTmpFac.GetFull( sTemplateRegioName, sTemplateName, sTemplateURL );
[ # # ][ # # ]
[ # # ][ # # ]
332 : : }
333 : : else
334 : : {
335 [ - + ]: 1093 : if ( !sServiceName.isEmpty() )
336 [ # # ][ # # ]: 0 : sTemplateURL = SfxObjectFactory::GetStandardTemplate( sServiceName );
[ # # ][ # # ]
[ # # ]
337 : : else
338 [ + - ][ + - ]: 1093 : sTemplateURL = SfxObjectFactory::GetStandardTemplate( SfxObjectShell::GetServiceNameFromFactory( sURL ) );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
339 : : }
340 : :
341 [ - + ]: 1093 : if ( sTemplateURL.Len() > 0 )
342 : : {
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 [ # # ][ # # ]: 0 : const SfxFilter* pTemplateFilter = impl_detectFilterForURL( sTemplateURL, io_rDescriptor, SFX_APP()->GetFilterMatcher() );
[ # # ][ # # ]
346 [ # # ]: 0 : if ( pTemplateFilter )
347 : : {
348 : : // load the template document, but, well, "as template"
349 [ # # ][ # # ]: 0 : io_rDescriptor.put( "FilterName", ::rtl::OUString( pTemplateFilter->GetName() ) );
350 [ # # ][ # # ]: 0 : io_rDescriptor.put( "FileName", ::rtl::OUString( sTemplateURL ) );
351 [ # # ]: 0 : io_rDescriptor.put( "AsTemplate", sal_True );
352 : :
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 [ # # ]: 0 : io_rDescriptor.put( "DocumentService", ::rtl::OUString( pTemplateFilter->GetServiceName() ) );
357 : 0 : return true;
358 : : }
359 : : }
360 [ + - ]: 1093 : return false;
361 : : }
362 : :
363 : : // --------------------------------------------------------------------------------------------------------------------
364 : 1093 : sal_uInt16 SfxFrameLoader_Impl::impl_findSlotParam( const ::rtl::OUString& i_rFactoryURL ) const
365 : : {
366 : 1093 : ::rtl::OUString sSlotParam;
367 : 1093 : const sal_Int32 nParamPos = i_rFactoryURL.indexOf( '?' );
368 [ - + ]: 1093 : if ( nParamPos >= 0 )
369 : : {
370 : : // currently only the "slot" parameter is supported
371 : 0 : const sal_Int32 nSlotPos = i_rFactoryURL.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "slot=" ), nParamPos );
372 [ # # ]: 0 : if ( nSlotPos > 0 )
373 : 0 : sSlotParam = i_rFactoryURL.copy( nSlotPos + 5 );
374 : : }
375 : :
376 [ - + ]: 1093 : if ( !sSlotParam.isEmpty() )
377 : 0 : return sal_uInt16( sSlotParam.toInt32() );
378 : :
379 : 1093 : return 0;
380 : : }
381 : :
382 : : // --------------------------------------------------------------------------------------------------------------------
383 : 0 : void SfxFrameLoader_Impl::impl_handleCaughtError_nothrow( const Any& i_rCaughtError, const ::comphelper::NamedValueCollection& i_rDescriptor ) const
384 : : {
385 : : try
386 : : {
387 : : const Reference< XInteractionHandler > xInteraction =
388 [ # # ]: 0 : i_rDescriptor.getOrDefault( "InteractionHandler", Reference< XInteractionHandler >() );
389 [ # # ]: 0 : if ( !xInteraction.is() )
390 : 0 : return;
391 [ # # ]: 0 : ::rtl::Reference< ::comphelper::OInteractionRequest > pRequest( new ::comphelper::OInteractionRequest( i_rCaughtError ) );
392 [ # # ]: 0 : ::rtl::Reference< ::comphelper::OInteractionApprove > pApprove( new ::comphelper::OInteractionApprove );
393 [ # # ][ # # ]: 0 : pRequest->addContinuation( pApprove.get() );
[ # # ]
394 : :
395 [ # # ]: 0 : const Reference< XInteractionHandler2 > xHandler( xInteraction, UNO_QUERY );
396 : : #if OSL_DEBUG_LEVEL > 0
397 : : const sal_Bool bHandled =
398 : : #endif
399 [ # # ][ # # ]: 0 : xHandler.is() && xHandler->handleInteractionRequest( pRequest.get() );
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # # # ]
400 : :
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
407 : : }
408 : 0 : catch( const Exception& )
409 : : {
410 : : DBG_UNHANDLED_EXCEPTION();
411 : : }
412 : : }
413 : :
414 : : // --------------------------------------------------------------------------------------------------------------------
415 : 1113 : void SfxFrameLoader_Impl::impl_removeLoaderArguments( ::comphelper::NamedValueCollection& io_rDescriptor )
416 : : {
417 : : // remove the arguments which are for the loader only, and not for a call to attachResource
418 : 1113 : io_rDescriptor.remove( "StatusIndicator" );
419 : 1113 : io_rDescriptor.remove( "Model" );
420 : 1113 : }
421 : :
422 : : // --------------------------------------------------------------------------------------------------------------------
423 : 1727 : ::comphelper::NamedValueCollection SfxFrameLoader_Impl::impl_extractViewCreationArgs( ::comphelper::NamedValueCollection& io_rDescriptor )
424 : : {
425 : : const sal_Char* pKnownViewArgs[] = {
426 : : "JumpMark"
427 : 1727 : };
428 : :
429 [ + - ]: 1727 : ::comphelper::NamedValueCollection aViewArgs;
430 [ + + ]: 3454 : for ( size_t i=0; i < sizeof( pKnownViewArgs ) / sizeof( pKnownViewArgs[0] ); ++i )
431 : : {
432 [ + - ][ - + ]: 1727 : if ( io_rDescriptor.has( pKnownViewArgs[i] ) )
433 : : {
434 [ # # ][ # # ]: 0 : aViewArgs.put( pKnownViewArgs[i], io_rDescriptor.get( pKnownViewArgs[i] ) );
435 [ # # ]: 0 : io_rDescriptor.remove( pKnownViewArgs[i] );
436 : : }
437 : : }
438 : 1727 : return aViewArgs;
439 : : }
440 : :
441 : : // --------------------------------------------------------------------------------------------------------------------
442 : 1727 : sal_Int16 SfxFrameLoader_Impl::impl_determineEffectiveViewId_nothrow( const SfxObjectShell& i_rDocument, const ::comphelper::NamedValueCollection& i_rDescriptor )
443 : : {
444 [ + - ]: 1727 : sal_Int16 nViewId = i_rDescriptor.getOrDefault( "ViewId", sal_Int16( 0 ) );
445 : : try
446 : : {
447 [ + + ]: 1727 : if ( nViewId == 0 ) do
448 : : {
449 [ + - ][ + - ]: 1707 : Reference< XViewDataSupplier > xViewDataSupplier( i_rDocument.GetModel(), UNO_QUERY );
450 : 1707 : Reference< XIndexAccess > xViewData;
451 [ + - ]: 1707 : if ( xViewDataSupplier.is() )
452 [ + - ][ + - ]: 1707 : xViewData.set( xViewDataSupplier->getViewData() );
[ + - ]
453 : :
454 [ + + ][ + - ]: 1707 : if ( !xViewData.is() || ( xViewData->getCount() == 0 ) )
[ + - ][ - + ]
[ + + ]
455 : : // no view data stored together with the model
456 : : break;
457 : :
458 : : // obtain the ViewID from the view data
459 [ + - ]: 336 : Sequence< PropertyValue > aViewData;
460 [ + - ][ + - ]: 336 : if ( !( xViewData->getByIndex( 0 ) >>= aViewData ) )
[ + - ][ - + ]
461 : : break;
462 : :
463 [ + - ]: 336 : ::comphelper::NamedValueCollection aNamedViewData( aViewData );
464 [ + - ]: 336 : ::rtl::OUString sViewId = aNamedViewData.getOrDefault( "ViewId", ::rtl::OUString() );
465 [ + + ]: 336 : if ( sViewId.isEmpty() )
466 : : break;
467 : :
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.
470 : :
471 [ + - ][ + - ]: 91 : SfxViewFactory* pViewFactory = i_rDocument.GetFactory().GetViewFactoryByViewName( sViewId );
[ + - ][ + - ]
472 [ + + ]: 91 : if ( pViewFactory )
473 [ + + ][ + - ]: 1707 : nViewId = sal_Int16( pViewFactory->GetOrdinal() );
[ + + ][ + - ]
[ + + ][ + + ]
[ + + ][ # # ]
474 : : }
475 : : while ( false );
476 : : }
477 : 0 : catch( const Exception& )
478 : : {
479 : : DBG_UNHANDLED_EXCEPTION();
480 : : }
481 : :
482 [ + + ]: 1727 : if ( nViewId == 0 )
483 : 1617 : nViewId = i_rDocument.GetFactory().GetViewFactory( 0 ).GetOrdinal();
484 : 1727 : return nViewId;
485 : : }
486 : :
487 : : // --------------------------------------------------------------------------------------------------------------------
488 : 1727 : Reference< XController2 > SfxFrameLoader_Impl::impl_createDocumentView( const Reference< XModel2 >& i_rModel,
489 : : const Reference< XFrame >& i_rFrame, const ::comphelper::NamedValueCollection& i_rViewFactoryArgs,
490 : : const ::rtl::OUString& i_rViewName )
491 : : {
492 : : // let the model create a new controller
493 : 1727 : const Reference< XController2 > xController( i_rModel->createViewController(
494 : : i_rViewName,
495 : : i_rViewFactoryArgs.getPropertyValues(),
496 : : i_rFrame
497 [ + - ][ + - ]: 1727 : ), UNO_SET_THROW );
[ + - ]
498 : :
499 : : // introduce model/view/controller to each other
500 [ + - ][ + - ]: 1727 : xController->attachModel( i_rModel.get() );
[ + - ][ + - ]
501 [ + - ][ + - ]: 1727 : i_rModel->connectController( xController.get() );
[ + - ][ + - ]
502 [ + - ][ + - ]: 1727 : i_rFrame->setComponent( xController->getComponentWindow(), xController.get() );
[ + - ][ + - ]
[ + - ][ + - ]
503 [ + - ][ + - ]: 1727 : xController->attachFrame( i_rFrame );
504 [ + - ][ + - ]: 1727 : i_rModel->setCurrentController( xController.get() );
[ + - ][ + - ]
505 : :
506 : 1727 : return xController;
507 : : }
508 : :
509 : : // --------------------------------------------------------------------------------------------------------------------
510 : 1727 : sal_Bool SAL_CALL SfxFrameLoader_Impl::load( const Sequence< PropertyValue >& rArgs,
511 : : const Reference< XFrame >& _rTargetFrame )
512 : : throw( RuntimeException )
513 : : {
514 [ - + ][ # # ]: 1727 : ENSURE_OR_THROW( _rTargetFrame.is(), "illegal NULL frame" );
[ # # ][ # # ]
515 : :
516 [ + - ]: 1727 : SolarMutexGuard aGuard;
517 : :
518 : : RTL_LOGFILE_CONTEXT( aLog, "sfx2 (mb93783) ::SfxFrameLoader::load" );
519 : :
520 [ + - ]: 1727 : ::comphelper::NamedValueCollection aDescriptor( rArgs );
521 : :
522 : : // ensure the descriptor contains a referrer
523 [ + - ][ + + ]: 1727 : if ( !aDescriptor.has( "Referer" ) )
524 [ + - ]: 1709 : aDescriptor.put( "Referer", ::rtl::OUString() );
525 : :
526 : : // TODO: is this needed? finally, when loading is successfull, then there should be no need for this item,
527 : : // as the document can always obtain its frame. In particular, there might be situations where this frame
528 : : // is accessed, but already disposed: Imagine the user loading a document, opening a second view on it, and
529 : : // then closing the first view/frame.
530 [ + - ]: 1727 : aDescriptor.put( "Frame", _rTargetFrame );
531 : :
532 : : // did the caller already pass a model?
533 [ + - ]: 1727 : Reference< XModel2 > xModel = aDescriptor.getOrDefault( "Model", Reference< XModel2 >() );
534 : 1727 : const bool bExternalModel = xModel.is();
535 : :
536 : : // check for factory URLs to create a new doc, instead of loading one
537 [ + - ]: 1727 : const ::rtl::OUString sURL = aDescriptor.getOrDefault( "URL", ::rtl::OUString() );
538 : 1727 : const bool bIsFactoryURL = ( sURL.compareToAscii( RTL_CONSTASCII_STRINGPARAM( "private:factory/" ) ) == 0 );
539 : 1727 : bool bInitNewModel = bIsFactoryURL;
540 [ + - ][ + + ]: 1727 : if ( bIsFactoryURL && !bExternalModel )
541 : : {
542 : 1093 : const ::rtl::OUString sFactory = sURL.copy( sizeof( "private:factory/" ) -1 );
543 : : // special handling for some weird factory URLs a la private:factory/swriter?slot=21053
544 [ + - ]: 1093 : const sal_uInt16 nSlotParam = impl_findSlotParam( sFactory );
545 [ - + ]: 1093 : if ( nSlotParam != 0 )
546 : : {
547 [ # # ][ # # ]: 0 : return impl_createNewDocWithSlotParam( nSlotParam, _rTargetFrame, aDescriptor.getOrDefault( "Hidden", false ) );
548 : : }
549 : :
550 [ + - ]: 1093 : const bool bDescribesValidTemplate = impl_determineTemplateDocument( aDescriptor );
551 [ - + ]: 1093 : if ( bDescribesValidTemplate )
552 : : {
553 : : // if the media descriptor allowed us to determine a template document to create the new document
554 : : // from, then do not init a new document model from scratch (below), but instead load the
555 : : // template document
556 : 0 : bInitNewModel = false;
557 : : }
558 : : else
559 : : {
560 [ + - ][ + - ]: 1093 : const ::rtl::OUString sServiceName = SfxObjectShell::GetServiceNameFromFactory( sFactory );
[ + - ][ + - ]
[ + - ]
561 [ + - ]: 1093 : aDescriptor.put( "DocumentService", sServiceName );
562 [ + - ]: 1093 : }
563 : : }
564 : : else
565 : : {
566 : : // compatibility
567 [ + - ][ + - ]: 634 : aDescriptor.put( "FileName", aDescriptor.get( "URL" ) );
568 : : }
569 : :
570 : 1727 : sal_Bool bLoadSuccess = sal_False;
571 : : try
572 : : {
573 : : // extract view releant arguments from the loader args
574 [ + - ]: 1727 : ::comphelper::NamedValueCollection aViewCreationArgs( impl_extractViewCreationArgs( aDescriptor ) );
575 : :
576 : : // no model passed from outside? => create one from scratch
577 [ + + ]: 1727 : if ( !xModel.is() )
578 : : {
579 : : // beforehand, determine the filter to use, and update the descriptor with its information
580 [ + + ]: 1707 : if ( !bInitNewModel )
581 : : {
582 [ + - ]: 614 : impl_determineFilter( aDescriptor );
583 : : }
584 : :
585 : : // create the new doc
586 [ + - ]: 1707 : const ::rtl::OUString sServiceName = aDescriptor.getOrDefault( "DocumentService", ::rtl::OUString() );
587 [ + - ][ + - ]: 1707 : xModel.set( m_aContext.createComponent( sServiceName ), UNO_QUERY_THROW );
588 : :
589 : : // load resp. init it
590 [ + - ]: 1707 : const Reference< XLoadable > xLoadable( xModel, UNO_QUERY_THROW );
591 [ + + ]: 1707 : if ( bInitNewModel )
592 : : {
593 [ + - ][ + - ]: 1093 : xLoadable->initNew();
594 : :
595 [ + - ]: 1093 : impl_removeLoaderArguments( aDescriptor );
596 [ + - ][ + - ]: 1093 : xModel->attachResource( ::rtl::OUString(), aDescriptor.getPropertyValues() );
[ + - ][ + - ]
597 : : }
598 : : else
599 : : {
600 [ + - ][ + - ]: 614 : xLoadable->load( aDescriptor.getPropertyValues() );
[ + - ][ + - ]
601 : 1707 : }
602 : : }
603 : : else
604 : : {
605 : : // tell the doc its (current) load args.
606 [ + - ]: 20 : impl_removeLoaderArguments( aDescriptor );
607 [ + - ][ + - ]: 20 : xModel->attachResource( xModel->getURL(), aDescriptor.getPropertyValues() );
[ + - ][ + - ]
[ + - ][ + - ]
608 : : }
609 : :
610 : : // get the SfxObjectShell (still needed at the moment)
611 : : // SfxObjectShellRef is used here ( instead of ...Lock ) since the model is closed below if necessary
612 : : // SfxObjectShellLock would be even dangerous here, since the lifetime control should be done outside in case of success
613 [ + - ]: 1727 : const SfxObjectShellRef xDoc = impl_findObjectShell( xModel );
614 [ - + ][ # # ]: 1727 : ENSURE_OR_THROW( xDoc.Is(), "no SfxObjectShell for the given model" );
[ # # ][ # # ]
615 : :
616 : : // ensure the ID of the to-be-created view is in the descriptor, if possible
617 [ + - ]: 1727 : const sal_Int16 nViewId = impl_determineEffectiveViewId_nothrow( *xDoc, aDescriptor );
618 [ + - ][ + - ]: 1727 : const sal_Int16 nViewNo = xDoc->GetFactory().GetViewNo_Impl( nViewId, 0 );
619 [ + - ][ + - ]: 1727 : const ::rtl::OUString sViewName( xDoc->GetFactory().GetViewFactory( nViewNo ).GetAPIViewName() );
[ + - ][ + - ]
[ + - ]
620 : :
621 : : // plug the document into the frame
622 [ + - ]: 1727 : impl_createDocumentView( xModel, _rTargetFrame, aViewCreationArgs, sViewName );
623 [ + - ][ + - ]: 1727 : bLoadSuccess = sal_True;
624 : : }
625 [ # # # # ]: 0 : catch ( Exception& )
626 : : {
627 [ # # ]: 0 : const Any aError( ::cppu::getCaughtException() );
628 [ # # # # ]: 0 : if ( !aDescriptor.getOrDefault( "Silent", sal_False ) )
629 [ # # ]: 0 : impl_handleCaughtError_nothrow( aError, aDescriptor );
630 : : }
631 : :
632 : : // if loading was not successful, close the document
633 [ - + ][ # # ]: 1727 : if ( !bLoadSuccess && !bExternalModel )
634 : : {
635 : : try
636 : : {
637 [ # # ]: 0 : const Reference< XCloseable > xCloseable( xModel, UNO_QUERY_THROW );
638 [ # # ][ # # ]: 0 : xCloseable->close( sal_True );
[ # # ]
639 : : }
640 [ # # ]: 0 : catch ( Exception& )
641 : : {
642 : : DBG_UNHANDLED_EXCEPTION();
643 : : }
644 : : }
645 : :
646 [ + - ][ + - ]: 1727 : return bLoadSuccess;
647 : : }
648 : :
649 : 0 : void SfxFrameLoader_Impl::cancel() throw( RuntimeException )
650 : : {
651 : 0 : }
652 : :
653 [ + - ][ + - ]: 115 : SFX_IMPL_SINGLEFACTORY( SfxFrameLoader_Impl )
[ + - ]
654 : :
655 : : /* XServiceInfo */
656 : 2 : rtl::OUString SAL_CALL SfxFrameLoader_Impl::getImplementationName() throw( RuntimeException )
657 : : {
658 : 2 : return impl_getStaticImplementationName();
659 : : }
660 : : \
661 : : /* XServiceInfo */
662 : 0 : sal_Bool SAL_CALL SfxFrameLoader_Impl::supportsService( const rtl::OUString& sServiceName ) throw( RuntimeException )
663 : : {
664 [ # # ]: 0 : UNOSEQUENCE< rtl::OUString > seqServiceNames = getSupportedServiceNames();
665 : 0 : const rtl::OUString* pArray = seqServiceNames.getConstArray();
666 [ # # ]: 0 : for ( sal_Int32 nCounter=0; nCounter<seqServiceNames.getLength(); nCounter++ )
667 : : {
668 [ # # ]: 0 : if ( pArray[nCounter] == sServiceName )
669 : : {
670 : 0 : return sal_True ;
671 : : }
672 : : }
673 [ # # ]: 0 : return sal_False ;
674 : : }
675 : :
676 : : /* XServiceInfo */
677 : 0 : UNOSEQUENCE< rtl::OUString > SAL_CALL SfxFrameLoader_Impl::getSupportedServiceNames() throw( RuntimeException )
678 : : {
679 : 0 : return impl_getStaticSupportedServiceNames();
680 : : }
681 : :
682 : : /* Helper for XServiceInfo */
683 : 115 : UNOSEQUENCE< rtl::OUString > SfxFrameLoader_Impl::impl_getStaticSupportedServiceNames()
684 : : {
685 [ + - ][ + - ]: 115 : UNOMUTEXGUARD aGuard( UNOMUTEX::getGlobalMutex() );
686 [ + - ]: 115 : UNOSEQUENCE< rtl::OUString > seqServiceNames( 1 );
687 [ + - ]: 115 : seqServiceNames.getArray() [0] = ::rtl::OUString( "com.sun.star.frame.SynchronousFrameLoader" );
688 [ + - ]: 115 : return seqServiceNames ;
689 : : }
690 : :
691 : : /* Helper for XServiceInfo */
692 : 663 : rtl::OUString SfxFrameLoader_Impl::impl_getStaticImplementationName()
693 : : {
694 : 663 : return ::rtl::OUString( "com.sun.star.comp.office.FrameLoader" );
695 : : }
696 : :
697 : : /* Helper for registry */
698 : 1731 : UNOREFERENCE< UNOXINTERFACE > SAL_CALL SfxFrameLoader_Impl::impl_createInstance( const UNOREFERENCE< UNOXMULTISERVICEFACTORY >& xServiceManager ) throw( UNOEXCEPTION )
699 : : {
700 [ + - ]: 1731 : return UNOREFERENCE< UNOXINTERFACE >( *new SfxFrameLoader_Impl( xServiceManager ) );
701 : : }
702 : :
703 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|