lok: vcl: fix multiple floatwin removal case more robustly.
[LibreOffice.git] / embeddedobj / source / commonembedding / miscobj.cxx
blob912cf3a80611744ea4e627a1e7e6b5338a178e6f
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 .
20 #include <commonembobj.hxx>
21 #include <com/sun/star/embed/EmbedStates.hpp>
22 #include <com/sun/star/embed/EmbedVerbs.hpp>
23 #include <com/sun/star/embed/XStorage.hpp>
24 #include <com/sun/star/embed/EmbedUpdateModes.hpp>
25 #include <com/sun/star/embed/XInplaceClient.hpp>
26 #include <com/sun/star/lang/DisposedException.hpp>
27 #include <com/sun/star/lang/NoSupportException.hpp>
28 #include <com/sun/star/beans/NamedValue.hpp>
30 #include <cppuhelper/typeprovider.hxx>
31 #include <cppuhelper/queryinterface.hxx>
32 #include <cppuhelper/interfacecontainer.h>
33 #include <comphelper/mimeconfighelper.hxx>
35 #include <vcl/svapp.hxx>
37 #include <closepreventer.hxx>
38 #include <intercept.hxx>
39 #include "persistence.hxx"
41 using namespace ::com::sun::star;
44 OCommonEmbeddedObject::OCommonEmbeddedObject( const uno::Reference< uno::XComponentContext >& rxContext,
45 const uno::Sequence< beans::NamedValue >& aObjProps )
46 : m_pInterfaceContainer( nullptr )
47 , m_bReadOnly( false )
48 , m_bDisposed( false )
49 , m_bClosed( false )
50 , m_nObjectState( -1 )
51 , m_nTargetState( -1 )
52 , m_nUpdateMode ( embed::EmbedUpdateModes::ALWAYS_UPDATE )
53 , m_xContext( rxContext )
54 , m_nMiscStatus( 0 )
55 , m_bEmbeddedScriptSupport( true )
56 , m_bDocumentRecoverySupport( true )
57 , m_bWaitSaveCompleted( false )
58 , m_bIsLink( false )
59 , m_bLinkHasPassword( false )
60 , m_bHasClonedSize( false )
61 , m_nClonedMapUnit( 0 )
63 CommonInit_Impl( aObjProps );
67 OCommonEmbeddedObject::OCommonEmbeddedObject(
68 const uno::Reference< uno::XComponentContext >& rxContext,
69 const uno::Sequence< beans::NamedValue >& aObjProps,
70 const uno::Sequence< beans::PropertyValue >& aMediaDescr,
71 const uno::Sequence< beans::PropertyValue >& aObjectDescr )
72 : m_pInterfaceContainer( nullptr )
73 , m_bReadOnly( false )
74 , m_bDisposed( false )
75 , m_bClosed( false )
76 , m_nObjectState( embed::EmbedStates::LOADED )
77 , m_nTargetState( -1 )
78 , m_nUpdateMode ( embed::EmbedUpdateModes::ALWAYS_UPDATE )
79 , m_xContext( rxContext )
80 , m_nMiscStatus( 0 )
81 , m_bEmbeddedScriptSupport( true )
82 , m_bDocumentRecoverySupport( true )
83 , m_bWaitSaveCompleted( false )
84 , m_bIsLink( true )
85 , m_bLinkHasPassword( false )
86 , m_bHasClonedSize( false )
87 , m_nClonedMapUnit( 0 )
89 // linked object has no own persistence so it is in loaded state starting from creation
90 LinkInit_Impl( aObjProps, aMediaDescr, aObjectDescr );
94 void OCommonEmbeddedObject::CommonInit_Impl( const uno::Sequence< beans::NamedValue >& aObjectProps )
96 OSL_ENSURE( m_xContext.is(), "No ServiceFactory is provided!" );
97 if ( !m_xContext.is() )
98 throw uno::RuntimeException();
100 m_xDocHolder = new DocumentHolder( m_xContext, this );
102 // parse configuration entries
103 // TODO/LATER: in future UI names can be also provided here
104 for ( sal_Int32 nInd = 0; nInd < aObjectProps.getLength(); nInd++ )
106 if ( aObjectProps[nInd].Name == "ClassID" )
107 aObjectProps[nInd].Value >>= m_aClassID;
108 else if ( aObjectProps[nInd].Name == "ObjectDocumentServiceName" )
109 aObjectProps[nInd].Value >>= m_aDocServiceName;
110 else if ( aObjectProps[nInd].Name == "ObjectDocumentFilterName" )
111 aObjectProps[nInd].Value >>= m_aPresetFilterName;
112 else if ( aObjectProps[nInd].Name == "ObjectMiscStatus" )
113 aObjectProps[nInd].Value >>= m_nMiscStatus;
114 else if ( aObjectProps[nInd].Name == "ObjectVerbs" )
115 aObjectProps[nInd].Value >>= m_aObjectVerbs;
118 if ( m_aClassID.getLength() != 16 /*|| !m_aDocServiceName.getLength()*/ )
119 throw uno::RuntimeException(); // something goes really wrong
121 // accepted states
122 m_aAcceptedStates.realloc( NUM_SUPPORTED_STATES );
124 m_aAcceptedStates[0] = embed::EmbedStates::LOADED;
125 m_aAcceptedStates[1] = embed::EmbedStates::RUNNING;
126 m_aAcceptedStates[2] = embed::EmbedStates::INPLACE_ACTIVE;
127 m_aAcceptedStates[3] = embed::EmbedStates::UI_ACTIVE;
128 m_aAcceptedStates[4] = embed::EmbedStates::ACTIVE;
131 // intermediate states
132 // In the following table the first index points to starting state,
133 // the second one to the target state, and the sequence referenced by
134 // first two indexes contains intermediate states, that should be
135 // passed by object to reach the target state.
136 // If the sequence is empty that means that indirect switch from start
137 // state to the target state is forbidden, only if direct switch is possible
138 // the state can be reached.
140 m_pIntermediateStatesSeqs[0][2].realloc( 1 );
141 m_pIntermediateStatesSeqs[0][2][0] = embed::EmbedStates::RUNNING;
143 m_pIntermediateStatesSeqs[0][3].realloc( 2 );
144 m_pIntermediateStatesSeqs[0][3][0] = embed::EmbedStates::RUNNING;
145 m_pIntermediateStatesSeqs[0][3][1] = embed::EmbedStates::INPLACE_ACTIVE;
147 m_pIntermediateStatesSeqs[0][4].realloc( 1 );
148 m_pIntermediateStatesSeqs[0][4][0] = embed::EmbedStates::RUNNING;
150 m_pIntermediateStatesSeqs[1][3].realloc( 1 );
151 m_pIntermediateStatesSeqs[1][3][0] = embed::EmbedStates::INPLACE_ACTIVE;
153 m_pIntermediateStatesSeqs[2][0].realloc( 1 );
154 m_pIntermediateStatesSeqs[2][0][0] = embed::EmbedStates::RUNNING;
156 m_pIntermediateStatesSeqs[3][0].realloc( 2 );
157 m_pIntermediateStatesSeqs[3][0][0] = embed::EmbedStates::INPLACE_ACTIVE;
158 m_pIntermediateStatesSeqs[3][0][1] = embed::EmbedStates::RUNNING;
160 m_pIntermediateStatesSeqs[3][1].realloc( 1 );
161 m_pIntermediateStatesSeqs[3][1][0] = embed::EmbedStates::INPLACE_ACTIVE;
163 m_pIntermediateStatesSeqs[4][0].realloc( 1 );
164 m_pIntermediateStatesSeqs[4][0][0] = embed::EmbedStates::RUNNING;
166 // verbs table
167 sal_Int32 nVerbTableSize = 0;
168 for ( sal_Int32 nVerbInd = 0; nVerbInd < m_aObjectVerbs.getLength(); nVerbInd++ )
170 if ( m_aObjectVerbs[nVerbInd].VerbID == embed::EmbedVerbs::MS_OLEVERB_PRIMARY )
172 m_aVerbTable.realloc( ++nVerbTableSize );
173 m_aVerbTable[nVerbTableSize - 1].realloc( 2 );
174 m_aVerbTable[nVerbTableSize - 1][0] = m_aObjectVerbs[nVerbInd].VerbID;
175 m_aVerbTable[nVerbTableSize - 1][1] = embed::EmbedStates::UI_ACTIVE;
177 else if ( m_aObjectVerbs[nVerbInd].VerbID == embed::EmbedVerbs::MS_OLEVERB_SHOW )
179 m_aVerbTable.realloc( ++nVerbTableSize );
180 m_aVerbTable[nVerbTableSize - 1].realloc( 2 );
181 m_aVerbTable[nVerbTableSize - 1][0] = m_aObjectVerbs[nVerbInd].VerbID;
182 m_aVerbTable[nVerbTableSize - 1][1] = embed::EmbedStates::UI_ACTIVE;
184 else if ( m_aObjectVerbs[nVerbInd].VerbID == embed::EmbedVerbs::MS_OLEVERB_OPEN )
186 m_aVerbTable.realloc( ++nVerbTableSize );
187 m_aVerbTable[nVerbTableSize - 1].realloc( 2 );
188 m_aVerbTable[nVerbTableSize - 1][0] = m_aObjectVerbs[nVerbInd].VerbID;
189 m_aVerbTable[nVerbTableSize - 1][1] = embed::EmbedStates::ACTIVE;
191 else if ( m_aObjectVerbs[nVerbInd].VerbID == embed::EmbedVerbs::MS_OLEVERB_IPACTIVATE )
193 m_aVerbTable.realloc( ++nVerbTableSize );
194 m_aVerbTable[nVerbTableSize - 1].realloc( 2 );
195 m_aVerbTable[nVerbTableSize - 1][0] = m_aObjectVerbs[nVerbInd].VerbID;
196 m_aVerbTable[nVerbTableSize - 1][1] = embed::EmbedStates::INPLACE_ACTIVE;
198 else if ( m_aObjectVerbs[nVerbInd].VerbID == embed::EmbedVerbs::MS_OLEVERB_UIACTIVATE )
200 m_aVerbTable.realloc( ++nVerbTableSize );
201 m_aVerbTable[nVerbTableSize - 1].realloc( 2 );
202 m_aVerbTable[nVerbTableSize - 1][0] = m_aObjectVerbs[nVerbInd].VerbID;
203 m_aVerbTable[nVerbTableSize - 1][1] = embed::EmbedStates::UI_ACTIVE;
205 else if ( m_aObjectVerbs[nVerbInd].VerbID == embed::EmbedVerbs::MS_OLEVERB_HIDE )
207 m_aVerbTable.realloc( ++nVerbTableSize );
208 m_aVerbTable[nVerbTableSize - 1].realloc( 2 );
209 m_aVerbTable[nVerbTableSize - 1][0] = m_aObjectVerbs[nVerbInd].VerbID;
210 m_aVerbTable[nVerbTableSize - 1][1] = embed::EmbedStates::RUNNING;
216 void OCommonEmbeddedObject::LinkInit_Impl(
217 const uno::Sequence< beans::NamedValue >& aObjectProps,
218 const uno::Sequence< beans::PropertyValue >& aMediaDescr,
219 const uno::Sequence< beans::PropertyValue >& aObjectDescr )
221 // setPersistance has no effect on own links, so the complete initialization must be done here
223 for ( sal_Int32 nInd = 0; nInd < aMediaDescr.getLength(); nInd++ )
224 if ( aMediaDescr[nInd].Name == "URL" )
225 aMediaDescr[nInd].Value >>= m_aLinkURL;
226 else if ( aMediaDescr[nInd].Name == "FilterName" )
227 aMediaDescr[nInd].Value >>= m_aLinkFilterName;
229 OSL_ENSURE( m_aLinkURL.getLength() && m_aLinkFilterName.getLength(), "Filter and URL must be provided!" );
231 m_bReadOnly = true;
232 if ( m_aLinkFilterName.getLength() )
234 ::comphelper::MimeConfigurationHelper aHelper( m_xContext );
235 OUString aExportFilterName = aHelper.GetExportFilterFromImportFilter( m_aLinkFilterName );
236 m_bReadOnly = aExportFilterName != m_aLinkFilterName;
239 m_aDocMediaDescriptor = GetValuableArgs_Impl( aMediaDescr, false );
241 uno::Reference< frame::XDispatchProviderInterceptor > xDispatchInterceptor;
242 for ( sal_Int32 nObjInd = 0; nObjInd < aObjectDescr.getLength(); nObjInd++ )
243 if ( aObjectDescr[nObjInd].Name == "OutplaceDispatchInterceptor" )
245 aObjectDescr[nObjInd].Value >>= xDispatchInterceptor;
246 break;
248 else if ( aObjectDescr[nObjInd].Name == "Parent" )
250 aObjectDescr[nObjInd].Value >>= m_xParent;
253 CommonInit_Impl( aObjectProps );
255 if ( xDispatchInterceptor.is() )
256 m_xDocHolder->SetOutplaceDispatchInterceptor( xDispatchInterceptor );
260 OCommonEmbeddedObject::~OCommonEmbeddedObject()
262 if ( m_pInterfaceContainer || m_xDocHolder.is() )
264 m_refCount++;
265 try {
266 lang::EventObject aSource( static_cast< ::cppu::OWeakObject* >( this ) );
268 if ( m_pInterfaceContainer )
270 m_pInterfaceContainer->disposeAndClear( aSource );
272 delete m_pInterfaceContainer;
273 m_pInterfaceContainer = nullptr;
275 } catch( const uno::Exception& ) {}
277 try {
278 if ( m_xDocHolder.is() )
280 m_xDocHolder->CloseFrame();
281 try {
282 m_xDocHolder->CloseDocument( true, true );
283 } catch ( const uno::Exception& ) {}
284 m_xDocHolder->FreeOffice();
286 m_xDocHolder.clear();
288 } catch( const uno::Exception& ) {}
293 void OCommonEmbeddedObject::requestPositioning( const awt::Rectangle& aRect )
295 // the method is called in case object is inplace active and the object window was resized
297 OSL_ENSURE( m_xClientSite.is(), "The client site must be set for inplace active object!" );
298 if ( m_xClientSite.is() )
300 uno::Reference< embed::XInplaceClient > xInplaceClient( m_xClientSite, uno::UNO_QUERY );
302 OSL_ENSURE( xInplaceClient.is(), "The client site must support XInplaceClient to allow inplace activation!" );
303 if ( xInplaceClient.is() )
305 try {
306 xInplaceClient->changedPlacement( aRect );
308 catch( const uno::Exception& )
310 OSL_FAIL( "Exception on request to resize!" );
317 void OCommonEmbeddedObject::PostEvent_Impl( const OUString& aEventName )
319 if ( m_pInterfaceContainer )
321 ::cppu::OInterfaceContainerHelper* pIC = m_pInterfaceContainer->getContainer(
322 cppu::UnoType<document::XEventListener>::get());
323 if( pIC )
325 document::EventObject aEvent;
326 aEvent.EventName = aEventName;
327 aEvent.Source.set( static_cast< ::cppu::OWeakObject* >( this ) );
328 // For now all the events are sent as object events
329 // aEvent.Source = ( xSource.is() ? xSource
330 // : uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >( this ) ) );
331 ::cppu::OInterfaceIteratorHelper aIt( *pIC );
332 while( aIt.hasMoreElements() )
336 static_cast<document::XEventListener *>(aIt.next())->notifyEvent( aEvent );
338 catch( const uno::RuntimeException& )
340 aIt.remove();
343 // the listener could dispose the object.
344 if ( m_bDisposed )
345 return;
352 uno::Any SAL_CALL OCommonEmbeddedObject::queryInterface( const uno::Type& rType )
354 uno::Any aReturn;
356 if ( rType == cppu::UnoType<embed::XEmbeddedObject>::get() )
358 void * p = static_cast< embed::XEmbeddedObject * >( this );
359 return uno::Any( &p, rType );
361 else if (rType == cppu::UnoType<embed::XEmbedPersist2>::get())
363 void* p = static_cast<embed::XEmbedPersist2*>(this);
364 return uno::Any(&p, rType);
366 else
367 aReturn = ::cppu::queryInterface(
368 rType,
369 static_cast< embed::XInplaceObject* >( this ),
370 static_cast< embed::XVisualObject* >( this ),
371 static_cast< embed::XCommonEmbedPersist* >( static_cast< embed::XEmbedPersist* >( this ) ),
372 static_cast< embed::XEmbedPersist* >( this ),
373 static_cast< embed::XLinkageSupport* >( this ),
374 static_cast< embed::XStateChangeBroadcaster* >( this ),
375 static_cast< embed::XClassifiedObject* >( this ),
376 static_cast< embed::XComponentSupplier* >( this ),
377 static_cast< util::XCloseable* >( this ),
378 static_cast< container::XChild* >( this ),
379 static_cast< chart2::XDefaultSizeTransmitter* >( this ),
380 static_cast< document::XEventBroadcaster* >( this ) );
382 if ( aReturn.hasValue() )
383 return aReturn;
384 else
385 return ::cppu::OWeakObject::queryInterface( rType ) ;
390 void SAL_CALL OCommonEmbeddedObject::acquire()
391 throw()
393 ::cppu::OWeakObject::acquire() ;
397 void SAL_CALL OCommonEmbeddedObject::release()
398 throw()
400 ::cppu::OWeakObject::release() ;
404 uno::Sequence< sal_Int8 > SAL_CALL OCommonEmbeddedObject::getClassID()
406 if ( m_bDisposed )
407 throw lang::DisposedException();
409 return m_aClassID;
412 OUString SAL_CALL OCommonEmbeddedObject::getClassName()
414 if ( m_bDisposed )
415 throw lang::DisposedException();
417 return m_aClassName;
420 void SAL_CALL OCommonEmbeddedObject::setClassInfo(
421 const uno::Sequence< sal_Int8 >& /*aClassID*/, const OUString& /*aClassName*/ )
423 // the object class info can not be changed explicitly
424 throw lang::NoSupportException(); //TODO:
428 uno::Reference< util::XCloseable > SAL_CALL OCommonEmbeddedObject::getComponent()
430 SolarMutexGuard aGuard;
431 if ( m_bDisposed )
432 throw lang::DisposedException(); // TODO
434 // add an exception
435 if ( m_nObjectState == -1 )
437 // the object is still not loaded
438 throw uno::RuntimeException( "Can't store object without persistence!",
439 static_cast< ::cppu::OWeakObject* >(this) );
442 return uno::Reference< util::XCloseable >( m_xDocHolder->GetComponent(), uno::UNO_QUERY );
446 void SAL_CALL OCommonEmbeddedObject::addStateChangeListener( const uno::Reference< embed::XStateChangeListener >& xListener )
448 SolarMutexGuard aGuard;
449 if ( m_bDisposed )
450 throw lang::DisposedException(); // TODO
452 if ( !m_pInterfaceContainer )
453 m_pInterfaceContainer = new ::cppu::OMultiTypeInterfaceContainerHelper( m_aMutex );
455 m_pInterfaceContainer->addInterface( cppu::UnoType<embed::XStateChangeListener>::get(),
456 xListener );
460 void SAL_CALL OCommonEmbeddedObject::removeStateChangeListener(
461 const uno::Reference< embed::XStateChangeListener >& xListener )
463 SolarMutexGuard aGuard;
464 if ( m_pInterfaceContainer )
465 m_pInterfaceContainer->removeInterface( cppu::UnoType<embed::XStateChangeListener>::get(),
466 xListener );
470 void SAL_CALL OCommonEmbeddedObject::close( sal_Bool bDeliverOwnership )
472 SolarMutexGuard aGuard;
473 if ( m_bClosed )
474 throw lang::DisposedException(); // TODO
476 uno::Reference< uno::XInterface > xSelfHold( static_cast< ::cppu::OWeakObject* >( this ) );
477 lang::EventObject aSource( static_cast< ::cppu::OWeakObject* >( this ) );
479 if ( m_pInterfaceContainer )
481 ::cppu::OInterfaceContainerHelper* pContainer =
482 m_pInterfaceContainer->getContainer( cppu::UnoType<util::XCloseListener>::get());
483 if ( pContainer != nullptr )
485 ::cppu::OInterfaceIteratorHelper pIterator(*pContainer);
486 while (pIterator.hasMoreElements())
490 static_cast<util::XCloseListener*>(pIterator.next())->queryClosing( aSource, bDeliverOwnership );
492 catch( const uno::RuntimeException& )
494 pIterator.remove();
499 pContainer = m_pInterfaceContainer->getContainer(
500 cppu::UnoType<util::XCloseListener>::get());
501 if ( pContainer != nullptr )
503 ::cppu::OInterfaceIteratorHelper pCloseIterator(*pContainer);
504 while (pCloseIterator.hasMoreElements())
508 static_cast<util::XCloseListener*>(pCloseIterator.next())->notifyClosing( aSource );
510 catch( const uno::RuntimeException& )
512 pCloseIterator.remove();
517 m_pInterfaceContainer->disposeAndClear( aSource );
520 m_bDisposed = true; // the object is disposed now for outside
522 // it is possible that the document can not be closed, in this case if the argument is false
523 // the exception will be thrown otherwise in addition to exception the object must register itself
524 // as termination listener and listen for document events
526 if ( m_xDocHolder.is() )
528 m_xDocHolder->CloseFrame();
530 try {
531 m_xDocHolder->CloseDocument( bDeliverOwnership, bDeliverOwnership );
533 catch( const uno::Exception& )
535 if ( bDeliverOwnership )
537 m_xDocHolder.clear();
538 m_bClosed = true;
541 throw;
544 m_xDocHolder->FreeOffice();
546 m_xDocHolder.clear();
549 // TODO: for now the storage will be disposed by the object, but after the document
550 // will use the storage, the storage will be disposed by the document and recreated by the object
551 if ( m_xObjectStorage.is() )
553 uno::Reference< lang::XComponent > xComp( m_xObjectStorage, uno::UNO_QUERY );
554 OSL_ENSURE( xComp.is(), "Storage does not support XComponent!" );
556 if ( xComp.is() )
558 try {
559 xComp->dispose();
560 } catch ( const uno::Exception& ) {}
563 m_xObjectStorage.clear();
564 m_xRecoveryStorage.clear();
567 m_bClosed = true; // the closing succeeded
571 void SAL_CALL OCommonEmbeddedObject::addCloseListener( const uno::Reference< util::XCloseListener >& xListener )
573 SolarMutexGuard aGuard;
574 if ( m_bDisposed )
575 throw lang::DisposedException(); // TODO
577 if ( !m_pInterfaceContainer )
578 m_pInterfaceContainer = new ::cppu::OMultiTypeInterfaceContainerHelper(m_aMutex);
580 m_pInterfaceContainer->addInterface( cppu::UnoType<util::XCloseListener>::get(), xListener );
584 void SAL_CALL OCommonEmbeddedObject::removeCloseListener( const uno::Reference< util::XCloseListener >& xListener )
586 SolarMutexGuard aGuard;
587 if ( m_pInterfaceContainer )
588 m_pInterfaceContainer->removeInterface( cppu::UnoType<util::XCloseListener>::get(),
589 xListener );
593 void SAL_CALL OCommonEmbeddedObject::addEventListener( const uno::Reference< document::XEventListener >& xListener )
595 SolarMutexGuard aGuard;
596 if ( m_bDisposed )
597 throw lang::DisposedException(); // TODO
599 if ( !m_pInterfaceContainer )
600 m_pInterfaceContainer = new ::cppu::OMultiTypeInterfaceContainerHelper(m_aMutex);
602 m_pInterfaceContainer->addInterface( cppu::UnoType<document::XEventListener>::get(), xListener );
606 void SAL_CALL OCommonEmbeddedObject::removeEventListener( const uno::Reference< document::XEventListener >& xListener )
608 SolarMutexGuard aGuard;
609 if ( m_pInterfaceContainer )
610 m_pInterfaceContainer->removeInterface( cppu::UnoType<document::XEventListener>::get(),
611 xListener );
614 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */