1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <sal/config.h>
24 #include <com/sun/star/embed/EmbedUpdateModes.hpp>
25 #include <com/sun/star/embed/EmbedStates.hpp>
26 #include <com/sun/star/lang/XComponent.hpp>
27 #include <com/sun/star/lang/DisposedException.hpp>
29 #include <cppuhelper/interfacecontainer.h>
31 #include <oleembobj.hxx>
32 #include <olepersist.hxx>
34 #include "ownview.hxx"
37 #include <olecomponent.hxx>
40 using namespace ::com::sun::star
;
44 OleEmbeddedObject::OleEmbeddedObject( const uno::Reference
< lang::XMultiServiceFactory
>& xFactory
,
45 const uno::Sequence
< sal_Int8
>& aClassID
,
46 const OUString
& aClassName
)
47 : m_pOleComponent( NULL
)
48 , m_pInterfaceContainer( NULL
)
49 , m_bReadOnly( false )
50 , m_bDisposed( false )
51 , m_nObjectState( -1 )
52 , m_nTargetState( -1 )
53 , m_nUpdateMode ( embed::EmbedUpdateModes::ALWAYS_UPDATE
)
54 , m_xFactory( xFactory
)
55 , m_aClassID( aClassID
)
56 , m_aClassName( aClassName
)
57 , m_bWaitSaveCompleted( false )
58 , m_bNewVisReplInStream( true )
59 , m_bStoreLoaded( false )
60 , m_bVisReplInitialized( false )
61 , m_bVisReplInStream( false )
62 , m_bStoreVisRepl( false )
64 , m_bHasCachedSize( false )
65 , m_nCachedAspect( 0 )
66 , m_bHasSizeToSet( false )
68 , m_bGotStatus( false )
70 , m_nStatusAspect( 0 )
72 , m_bFromClipboard( false )
73 , m_bTriedConversion( false )
78 // In case of loading from persistent entry the classID of the object
79 // will be retrieved from the entry, during construction it is unknown
80 OleEmbeddedObject::OleEmbeddedObject( const uno::Reference
< lang::XMultiServiceFactory
>& xFactory
, bool bLink
)
81 : m_pOleComponent( NULL
)
82 , m_pInterfaceContainer( NULL
)
83 , m_bReadOnly( false )
84 , m_bDisposed( false )
85 , m_nObjectState( -1 )
86 , m_nTargetState( -1 )
87 , m_nUpdateMode( embed::EmbedUpdateModes::ALWAYS_UPDATE
)
88 , m_xFactory( xFactory
)
89 , m_bWaitSaveCompleted( false )
90 , m_bNewVisReplInStream( true )
91 , m_bStoreLoaded( false )
92 , m_bVisReplInitialized( false )
93 , m_bVisReplInStream( false )
94 , m_bStoreVisRepl( false )
96 , m_bHasCachedSize( false )
97 , m_nCachedAspect( 0 )
98 , m_bHasSizeToSet( false )
100 , m_bGotStatus( false )
102 , m_nStatusAspect( 0 )
104 , m_bFromClipboard( false )
105 , m_bTriedConversion( false )
110 // this constructor let object be initialized from clipboard
111 OleEmbeddedObject::OleEmbeddedObject( const uno::Reference
< lang::XMultiServiceFactory
>& xFactory
)
112 : m_pOleComponent( NULL
)
113 , m_pInterfaceContainer( NULL
)
114 , m_bReadOnly( sal_False
)
115 , m_bDisposed( false )
116 , m_nObjectState( -1 )
117 , m_nTargetState( -1 )
118 , m_nUpdateMode( embed::EmbedUpdateModes::ALWAYS_UPDATE
)
119 , m_xFactory( xFactory
)
120 , m_bWaitSaveCompleted( sal_False
)
121 , m_bNewVisReplInStream( sal_True
)
122 , m_bStoreLoaded( sal_False
)
123 , m_bVisReplInitialized( sal_False
)
124 , m_bVisReplInStream( sal_False
)
125 , m_bStoreVisRepl( sal_False
)
126 , m_bIsLink( sal_False
)
127 , m_bHasCachedSize( sal_False
)
128 , m_nCachedAspect( 0 )
129 , m_bHasSizeToSet( sal_False
)
130 , m_nAspectToSet( 0 )
131 , m_bGotStatus( sal_False
)
133 , m_nStatusAspect( 0 )
135 , m_bFromClipboard( sal_True
)
136 , m_bTriedConversion( sal_False
)
141 OleEmbeddedObject::~OleEmbeddedObject()
143 OSL_ENSURE( !m_pInterfaceContainer
&& !m_pOleComponent
&& !m_xObjectStream
.is(),
144 "The object is not closed! DISASTER is possible!" );
146 if ( m_pOleComponent
|| m_pInterfaceContainer
|| m_xObjectStream
.is() )
148 // the component must be cleaned during closing
149 m_refCount
++; // to avoid crash
152 } catch( const uno::Exception
& ) {}
155 if ( !m_aTempURL
.isEmpty() )
156 KillFile_Impl( m_aTempURL
, m_xFactory
);
158 if ( !m_aTempDumpURL
.isEmpty() )
159 KillFile_Impl( m_aTempDumpURL
, m_xFactory
);
163 void OleEmbeddedObject::MakeEventListenerNotification_Impl( const OUString
& aEventName
)
165 if ( m_pInterfaceContainer
)
167 ::cppu::OInterfaceContainerHelper
* pContainer
=
168 m_pInterfaceContainer
->getContainer(
169 cppu::UnoType
<document::XEventListener
>::get());
170 if ( pContainer
!= NULL
)
172 document::EventObject
aEvent( static_cast< ::cppu::OWeakObject
* >( this ), aEventName
);
173 ::cppu::OInterfaceIteratorHelper
pIterator(*pContainer
);
174 while (pIterator
.hasMoreElements())
178 static_cast<document::XEventListener
*>(pIterator
.next())->notifyEvent( aEvent
);
180 catch( const uno::RuntimeException
& )
189 void OleEmbeddedObject::StateChangeNotification_Impl( sal_Bool bBeforeChange
, sal_Int32 nOldState
, sal_Int32 nNewState
)
191 if ( m_pInterfaceContainer
)
193 ::cppu::OInterfaceContainerHelper
* pContainer
= m_pInterfaceContainer
->getContainer(
194 cppu::UnoType
<embed::XStateChangeListener
>::get());
195 if ( pContainer
!= NULL
)
197 lang::EventObject
aSource( static_cast< ::cppu::OWeakObject
* >( this ) );
198 ::cppu::OInterfaceIteratorHelper
pIterator(*pContainer
);
200 while (pIterator
.hasMoreElements())
206 ((embed::XStateChangeListener
*)pIterator
.next())->changingState( aSource
, nOldState
, nNewState
);
208 catch( const uno::Exception
& )
210 // even if the listener complains ignore it for now
217 ((embed::XStateChangeListener
*)pIterator
.next())->stateChanged( aSource
, nOldState
, nNewState
);
219 catch( const uno::Exception
& )
221 // if anything happened it is problem of listener, ignore it
230 void OleEmbeddedObject::GetRidOfComponent()
233 if ( m_pOleComponent
)
235 if ( m_nObjectState
!= -1 && m_nObjectState
!= embed::EmbedStates::LOADED
)
238 m_pOleComponent
->removeCloseListener( m_xClosePreventer
);
241 m_pOleComponent
->close( sal_False
);
243 catch( const uno::Exception
& )
245 // TODO: there should be a special listener to wait for component closing
246 // and to notify object, may be object itself can be such a listener
247 m_pOleComponent
->addCloseListener( m_xClosePreventer
);
251 m_pOleComponent
->disconnectEmbeddedObject();
252 m_pOleComponent
->release();
253 m_pOleComponent
= NULL
;
259 void OleEmbeddedObject::Dispose()
261 if ( m_pInterfaceContainer
)
263 lang::EventObject
aSource( static_cast< ::cppu::OWeakObject
* >( this ) );
264 m_pInterfaceContainer
->disposeAndClear( aSource
);
265 delete m_pInterfaceContainer
;
266 m_pInterfaceContainer
= NULL
;
272 m_pOwnView
->release();
276 if ( m_pOleComponent
)
279 } catch( const uno::Exception
& )
282 throw; // TODO: there should be a special listener that will close object when
283 // component is finally closed
286 if ( m_xObjectStream
.is() )
288 uno::Reference
< lang::XComponent
> xComp( m_xObjectStream
, uno::UNO_QUERY
);
289 OSL_ENSURE( xComp
.is(), "Storage stream doesn't support XComponent!\n" );
295 } catch( const uno::Exception
& ) {}
297 m_xObjectStream
= uno::Reference
< io::XStream
>();
300 m_xParentStorage
= uno::Reference
< embed::XStorage
>();
306 uno::Sequence
< sal_Int8
> SAL_CALL
OleEmbeddedObject::getClassID()
307 throw ( uno::RuntimeException
, std::exception
)
309 // begin wrapping related part ====================
310 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
311 if ( xWrappedObject
.is() )
313 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
314 return xWrappedObject
->getClassID();
316 // end wrapping related part ====================
318 ::osl::MutexGuard
aGuard( m_aMutex
);
320 throw lang::DisposedException(); // TODO
326 OUString SAL_CALL
OleEmbeddedObject::getClassName()
327 throw ( uno::RuntimeException
, std::exception
)
329 // begin wrapping related part ====================
330 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
331 if ( xWrappedObject
.is() )
333 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
334 return xWrappedObject
->getClassName();
336 // end wrapping related part ====================
338 ::osl::MutexGuard
aGuard( m_aMutex
);
340 throw lang::DisposedException(); // TODO
346 void SAL_CALL
OleEmbeddedObject::setClassInfo(
347 const uno::Sequence
< sal_Int8
>& aClassID
, const OUString
& aClassName
)
348 throw ( lang::NoSupportException
,
349 uno::RuntimeException
, std::exception
)
351 // begin wrapping related part ====================
352 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
353 if ( xWrappedObject
.is() )
355 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
356 xWrappedObject
->setClassInfo( aClassID
, aClassName
);
359 // end wrapping related part ====================
361 // the object class info can not be changed explicitly
362 throw lang::NoSupportException(); //TODO:
366 uno::Reference
< util::XCloseable
> SAL_CALL
OleEmbeddedObject::getComponent()
367 throw ( uno::RuntimeException
, std::exception
)
369 // begin wrapping related part ====================
370 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
371 if ( xWrappedObject
.is() )
373 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
374 return xWrappedObject
->getComponent();
376 // end wrapping related part ====================
378 ::osl::MutexGuard
aGuard( m_aMutex
);
380 throw lang::DisposedException(); // TODO
382 if ( m_nObjectState
== -1 ) // || m_nObjectState == embed::EmbedStates::LOADED )
384 // the object is still not running
385 throw uno::RuntimeException( "The object is not loaded!",
386 static_cast< ::cppu::OWeakObject
* >(this) );
390 if (m_pOleComponent
!= 0)
392 return uno::Reference
< util::XCloseable
>( static_cast< ::cppu::OWeakObject
* >( m_pOleComponent
), uno::UNO_QUERY
);
396 assert(m_pOleComponent
== 0);
397 // TODO/LATER: Is it correct???
398 return uno::Reference
< util::XCloseable
>();
399 // throw uno::RuntimeException(); // TODO
403 void SAL_CALL
OleEmbeddedObject::addStateChangeListener( const uno::Reference
< embed::XStateChangeListener
>& xListener
)
404 throw ( uno::RuntimeException
, std::exception
)
406 // begin wrapping related part ====================
407 uno::Reference
< embed::XStateChangeBroadcaster
> xWrappedObject( m_xWrappedObject
, uno::UNO_QUERY
);
408 if ( xWrappedObject
.is() )
410 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
411 xWrappedObject
->addStateChangeListener( xListener
);
414 // end wrapping related part ====================
416 ::osl::MutexGuard
aGuard( m_aMutex
);
418 throw lang::DisposedException(); // TODO
420 if ( !m_pInterfaceContainer
)
421 m_pInterfaceContainer
= new ::cppu::OMultiTypeInterfaceContainerHelper( m_aMutex
);
423 m_pInterfaceContainer
->addInterface( cppu::UnoType
<embed::XStateChangeListener
>::get(),
428 void SAL_CALL
OleEmbeddedObject::removeStateChangeListener(
429 const uno::Reference
< embed::XStateChangeListener
>& xListener
)
430 throw (uno::RuntimeException
, std::exception
)
432 // begin wrapping related part ====================
433 uno::Reference
< embed::XStateChangeBroadcaster
> xWrappedObject( m_xWrappedObject
, uno::UNO_QUERY
);
434 if ( xWrappedObject
.is() )
436 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
437 xWrappedObject
->removeStateChangeListener( xListener
);
440 // end wrapping related part ====================
442 ::osl::MutexGuard
aGuard( m_aMutex
);
443 if ( m_pInterfaceContainer
)
444 m_pInterfaceContainer
->removeInterface( cppu::UnoType
<embed::XStateChangeListener
>::get(),
450 void SAL_CALL
OleEmbeddedObject::close( sal_Bool bDeliverOwnership
)
451 throw ( util::CloseVetoException
,
452 uno::RuntimeException
, std::exception
)
454 // begin wrapping related part ====================
455 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
456 if ( xWrappedObject
.is() )
458 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
459 xWrappedObject
->close( bDeliverOwnership
);
462 // end wrapping related part ====================
464 ::osl::MutexGuard
aGuard( m_aMutex
);
466 throw lang::DisposedException(); // TODO
468 uno::Reference
< uno::XInterface
> xSelfHold( static_cast< ::cppu::OWeakObject
* >( this ) );
469 lang::EventObject
aSource( static_cast< ::cppu::OWeakObject
* >( this ) );
471 if ( m_pInterfaceContainer
)
473 ::cppu::OInterfaceContainerHelper
* pContainer
=
474 m_pInterfaceContainer
->getContainer( cppu::UnoType
<util::XCloseListener
>::get());
475 if ( pContainer
!= NULL
)
477 ::cppu::OInterfaceIteratorHelper
pIterator(*pContainer
);
478 while (pIterator
.hasMoreElements())
482 static_cast<util::XCloseListener
*>(pIterator
.next())->queryClosing( aSource
, bDeliverOwnership
);
484 catch( const uno::RuntimeException
& )
491 pContainer
= m_pInterfaceContainer
->getContainer(
492 cppu::UnoType
<util::XCloseListener
>::get());
493 if ( pContainer
!= NULL
)
495 ::cppu::OInterfaceIteratorHelper
pCloseIterator(*pContainer
);
496 while (pCloseIterator
.hasMoreElements())
500 static_cast<util::XCloseListener
*>(pCloseIterator
.next())->notifyClosing( aSource
);
502 catch( const uno::RuntimeException
& )
504 pCloseIterator
.remove();
514 void SAL_CALL
OleEmbeddedObject::addCloseListener( const uno::Reference
< util::XCloseListener
>& xListener
)
515 throw ( uno::RuntimeException
, std::exception
)
517 // begin wrapping related part ====================
518 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
519 if ( xWrappedObject
.is() )
521 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
522 xWrappedObject
->addCloseListener( xListener
);
525 // end wrapping related part ====================
527 ::osl::MutexGuard
aGuard( m_aMutex
);
529 throw lang::DisposedException(); // TODO
531 if ( !m_pInterfaceContainer
)
532 m_pInterfaceContainer
= new ::cppu::OMultiTypeInterfaceContainerHelper( m_aMutex
);
534 m_pInterfaceContainer
->addInterface( cppu::UnoType
<util::XCloseListener
>::get(), xListener
);
538 void SAL_CALL
OleEmbeddedObject::removeCloseListener( const uno::Reference
< util::XCloseListener
>& xListener
)
539 throw (uno::RuntimeException
, std::exception
)
541 // begin wrapping related part ====================
542 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
543 if ( xWrappedObject
.is() )
545 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
546 xWrappedObject
->removeCloseListener( xListener
);
549 // end wrapping related part ====================
551 ::osl::MutexGuard
aGuard( m_aMutex
);
553 throw lang::DisposedException(); // TODO
555 if ( m_pInterfaceContainer
)
556 m_pInterfaceContainer
->removeInterface( cppu::UnoType
<util::XCloseListener
>::get(),
561 void SAL_CALL
OleEmbeddedObject::addEventListener( const uno::Reference
< document::XEventListener
>& xListener
)
562 throw ( uno::RuntimeException
, std::exception
)
564 // begin wrapping related part ====================
565 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
566 if ( xWrappedObject
.is() )
568 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
569 xWrappedObject
->addEventListener( xListener
);
572 // end wrapping related part ====================
574 ::osl::MutexGuard
aGuard( m_aMutex
);
576 throw lang::DisposedException(); // TODO
578 if ( !m_pInterfaceContainer
)
579 m_pInterfaceContainer
= new ::cppu::OMultiTypeInterfaceContainerHelper( m_aMutex
);
581 m_pInterfaceContainer
->addInterface( cppu::UnoType
<document::XEventListener
>::get(), xListener
);
585 void SAL_CALL
OleEmbeddedObject::removeEventListener(
586 const uno::Reference
< document::XEventListener
>& xListener
)
587 throw ( uno::RuntimeException
, std::exception
)
589 // begin wrapping related part ====================
590 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
591 if ( xWrappedObject
.is() )
593 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
594 xWrappedObject
->removeEventListener( xListener
);
597 // end wrapping related part ====================
599 ::osl::MutexGuard
aGuard( m_aMutex
);
601 throw lang::DisposedException(); // TODO
603 if ( m_pInterfaceContainer
)
604 m_pInterfaceContainer
->removeInterface( cppu::UnoType
<document::XEventListener
>::get(),
608 // XInplaceObject ( wrapper related implementation )
610 void SAL_CALL
OleEmbeddedObject::setObjectRectangles( const awt::Rectangle
& aPosRect
,
611 const awt::Rectangle
& aClipRect
)
612 throw ( embed::WrongStateException
,
614 uno::RuntimeException
, std::exception
)
616 // begin wrapping related part ====================
617 uno::Reference
< embed::XInplaceObject
> xWrappedObject( m_xWrappedObject
, uno::UNO_QUERY
);
618 if ( xWrappedObject
.is() )
620 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
621 xWrappedObject
->setObjectRectangles( aPosRect
, aClipRect
);
624 // end wrapping related part ====================
626 throw embed::WrongStateException();
630 void SAL_CALL
OleEmbeddedObject::enableModeless( sal_Bool bEnable
)
631 throw ( embed::WrongStateException
,
633 uno::RuntimeException
, std::exception
)
635 // begin wrapping related part ====================
636 uno::Reference
< embed::XInplaceObject
> xWrappedObject( m_xWrappedObject
, uno::UNO_QUERY
);
637 if ( xWrappedObject
.is() )
639 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
640 xWrappedObject
->enableModeless( bEnable
);
643 // end wrapping related part ====================
645 throw embed::WrongStateException();
649 void SAL_CALL
OleEmbeddedObject::translateAccelerators(
650 const uno::Sequence
< awt::KeyEvent
>& aKeys
)
651 throw ( embed::WrongStateException
,
652 uno::RuntimeException
, std::exception
)
654 // begin wrapping related part ====================
655 uno::Reference
< embed::XInplaceObject
> xWrappedObject( m_xWrappedObject
, uno::UNO_QUERY
);
656 if ( xWrappedObject
.is() )
658 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
659 xWrappedObject
->translateAccelerators( aKeys
);
662 // end wrapping related part ====================
668 com::sun::star::uno::Reference
< com::sun::star::uno::XInterface
> SAL_CALL
OleEmbeddedObject::getParent() throw (::com::sun::star::uno::RuntimeException
, std::exception
)
670 // begin wrapping related part ====================
671 uno::Reference
< container::XChild
> xWrappedObject( m_xWrappedObject
, uno::UNO_QUERY
);
672 if ( xWrappedObject
.is() )
674 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
675 return xWrappedObject
->getParent();
677 // end wrapping related part ====================
683 void SAL_CALL
OleEmbeddedObject::setParent( const com::sun::star::uno::Reference
< com::sun::star::uno::XInterface
>& xParent
) throw (::com::sun::star::lang::NoSupportException
, ::com::sun::star::uno::RuntimeException
, std::exception
)
685 // begin wrapping related part ====================
686 uno::Reference
< container::XChild
> xWrappedObject( m_xWrappedObject
, uno::UNO_QUERY
);
687 if ( xWrappedObject
.is() )
689 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
690 xWrappedObject
->setParent( xParent
);
693 // end wrapping related part ====================
698 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */