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 <com/sun/star/embed/EmbedUpdateModes.hpp>
21 #include <com/sun/star/embed/EmbedStates.hpp>
22 #include <com/sun/star/lang/XComponent.hpp>
23 #include <com/sun/star/lang/DisposedException.hpp>
25 #include <cppuhelper/interfacecontainer.h>
27 #include <oleembobj.hxx>
28 #include <olecomponent.hxx>
30 #include "ownview.hxx"
32 using namespace ::com::sun::star
;
34 sal_Bool
KillFile_Impl( const OUString
& aURL
, const uno::Reference
< lang::XMultiServiceFactory
>& xFactory
);
37 //------------------------------------------------------
38 OleEmbeddedObject::OleEmbeddedObject( const uno::Reference
< lang::XMultiServiceFactory
>& xFactory
,
39 const uno::Sequence
< sal_Int8
>& aClassID
,
40 const OUString
& aClassName
)
41 : m_pOleComponent( NULL
)
42 , m_pInterfaceContainer( NULL
)
43 , m_bReadOnly( sal_False
)
44 , m_bDisposed( sal_False
)
45 , m_nObjectState( -1 )
46 , m_nTargetState( -1 )
47 , m_nUpdateMode ( embed::EmbedUpdateModes::ALWAYS_UPDATE
)
48 , m_xFactory( xFactory
)
49 , m_aClassID( aClassID
)
50 , m_aClassName( aClassName
)
51 , m_bWaitSaveCompleted( sal_False
)
52 , m_bNewVisReplInStream( sal_True
)
53 , m_bStoreLoaded( sal_False
)
54 , m_bVisReplInitialized( sal_False
)
55 , m_bVisReplInStream( sal_False
)
56 , m_bStoreVisRepl( sal_False
)
57 , m_bIsLink( sal_False
)
58 , m_bHasCachedSize( sal_False
)
59 , m_nCachedAspect( 0 )
60 , m_bHasSizeToSet( sal_False
)
62 , m_bGotStatus( sal_False
)
64 , m_nStatusAspect( 0 )
66 , m_bFromClipboard( sal_False
)
67 , m_bTriedConversion( sal_False
)
71 //------------------------------------------------------
72 // In case of loading from persistent entry the classID of the object
73 // will be retrieved from the entry, during construction it is unknown
74 OleEmbeddedObject::OleEmbeddedObject( const uno::Reference
< lang::XMultiServiceFactory
>& xFactory
, sal_Bool bLink
)
75 : m_pOleComponent( NULL
)
76 , m_pInterfaceContainer( NULL
)
77 , m_bReadOnly( sal_False
)
78 , m_bDisposed( sal_False
)
79 , m_nObjectState( -1 )
80 , m_nTargetState( -1 )
81 , m_nUpdateMode( embed::EmbedUpdateModes::ALWAYS_UPDATE
)
82 , m_xFactory( xFactory
)
83 , m_bWaitSaveCompleted( sal_False
)
84 , m_bNewVisReplInStream( sal_True
)
85 , m_bStoreLoaded( sal_False
)
86 , m_bVisReplInitialized( sal_False
)
87 , m_bVisReplInStream( sal_False
)
88 , m_bStoreVisRepl( sal_False
)
90 , m_bHasCachedSize( sal_False
)
91 , m_nCachedAspect( 0 )
92 , m_bHasSizeToSet( sal_False
)
94 , m_bGotStatus( sal_False
)
96 , m_nStatusAspect( 0 )
98 , m_bFromClipboard( sal_False
)
99 , m_bTriedConversion( sal_False
)
103 //------------------------------------------------------
104 // this constructor let object be initialized from clipboard
105 OleEmbeddedObject::OleEmbeddedObject( const uno::Reference
< lang::XMultiServiceFactory
>& xFactory
)
106 : m_pOleComponent( NULL
)
107 , m_pInterfaceContainer( NULL
)
108 , m_bReadOnly( sal_False
)
109 , m_bDisposed( sal_False
)
110 , m_nObjectState( -1 )
111 , m_nTargetState( -1 )
112 , m_nUpdateMode( embed::EmbedUpdateModes::ALWAYS_UPDATE
)
113 , m_xFactory( xFactory
)
114 , m_bWaitSaveCompleted( sal_False
)
115 , m_bNewVisReplInStream( sal_True
)
116 , m_bStoreLoaded( sal_False
)
117 , m_bVisReplInitialized( sal_False
)
118 , m_bVisReplInStream( sal_False
)
119 , m_bStoreVisRepl( sal_False
)
120 , m_bIsLink( sal_False
)
121 , m_bHasCachedSize( sal_False
)
122 , m_nCachedAspect( 0 )
123 , m_bHasSizeToSet( sal_False
)
124 , m_nAspectToSet( 0 )
125 , m_bGotStatus( sal_False
)
127 , m_nStatusAspect( 0 )
129 , m_bFromClipboard( sal_True
)
130 , m_bTriedConversion( sal_False
)
134 //------------------------------------------------------
135 OleEmbeddedObject::~OleEmbeddedObject()
137 OSL_ENSURE( !m_pInterfaceContainer
&& !m_pOleComponent
&& !m_xObjectStream
.is(),
138 "The object is not closed! DISASTER is possible!" );
140 if ( m_pOleComponent
|| m_pInterfaceContainer
|| m_xObjectStream
.is() )
142 // the component must be cleaned during closing
143 m_refCount
++; // to avoid crash
146 } catch( const uno::Exception
& ) {}
149 if ( !m_aTempURL
.isEmpty() )
150 KillFile_Impl( m_aTempURL
, m_xFactory
);
152 if ( !m_aTempDumpURL
.isEmpty() )
153 KillFile_Impl( m_aTempDumpURL
, m_xFactory
);
156 //------------------------------------------------------
157 void OleEmbeddedObject::MakeEventListenerNotification_Impl( const OUString
& aEventName
)
159 if ( m_pInterfaceContainer
)
161 ::cppu::OInterfaceContainerHelper
* pContainer
=
162 m_pInterfaceContainer
->getContainer(
163 ::getCppuType( ( const uno::Reference
< document::XEventListener
>*) NULL
) );
164 if ( pContainer
!= NULL
)
166 document::EventObject
aEvent( static_cast< ::cppu::OWeakObject
* >( this ), aEventName
);
167 ::cppu::OInterfaceIteratorHelper
pIterator(*pContainer
);
168 while (pIterator
.hasMoreElements())
172 ((document::XEventListener
*)pIterator
.next())->notifyEvent( aEvent
);
174 catch( const uno::RuntimeException
& )
182 //----------------------------------------------
183 void OleEmbeddedObject::StateChangeNotification_Impl( sal_Bool bBeforeChange
, sal_Int32 nOldState
, sal_Int32 nNewState
)
185 if ( m_pInterfaceContainer
)
187 ::cppu::OInterfaceContainerHelper
* pContainer
= m_pInterfaceContainer
->getContainer(
188 ::getCppuType( ( const uno::Reference
< embed::XStateChangeListener
>*) NULL
) );
189 if ( pContainer
!= NULL
)
191 lang::EventObject
aSource( static_cast< ::cppu::OWeakObject
* >( this ) );
192 ::cppu::OInterfaceIteratorHelper
pIterator(*pContainer
);
194 while (pIterator
.hasMoreElements())
200 ((embed::XStateChangeListener
*)pIterator
.next())->changingState( aSource
, nOldState
, nNewState
);
202 catch( const uno::Exception
& )
204 // even if the listener complains ignore it for now
211 ((embed::XStateChangeListener
*)pIterator
.next())->stateChanged( aSource
, nOldState
, nNewState
);
213 catch( const uno::Exception
& )
215 // if anything happened it is problem of listener, ignore it
223 //------------------------------------------------------
224 void OleEmbeddedObject::GetRidOfComponent()
227 if ( m_pOleComponent
)
229 if ( m_nObjectState
!= -1 && m_nObjectState
!= embed::EmbedStates::LOADED
)
232 m_pOleComponent
->removeCloseListener( m_xClosePreventer
);
235 m_pOleComponent
->close( sal_False
);
237 catch( const uno::Exception
& )
239 // TODO: there should be a special listener to wait for component closing
240 // and to notify object, may be object itself can be such a listener
241 m_pOleComponent
->addCloseListener( m_xClosePreventer
);
245 m_pOleComponent
->disconnectEmbeddedObject();
246 m_pOleComponent
->release();
247 m_pOleComponent
= NULL
;
252 //------------------------------------------------------
253 void OleEmbeddedObject::Dispose()
255 if ( m_pInterfaceContainer
)
257 lang::EventObject
aSource( static_cast< ::cppu::OWeakObject
* >( this ) );
258 m_pInterfaceContainer
->disposeAndClear( aSource
);
259 delete m_pInterfaceContainer
;
260 m_pInterfaceContainer
= NULL
;
266 m_pOwnView
->release();
270 if ( m_pOleComponent
)
273 } catch( const uno::Exception
& )
276 throw; // TODO: there should be a special listener that will close object when
277 // component is finally closed
280 if ( m_xObjectStream
.is() )
282 uno::Reference
< lang::XComponent
> xComp( m_xObjectStream
, uno::UNO_QUERY
);
283 OSL_ENSURE( xComp
.is(), "Storage stream doesn't support XComponent!\n" );
289 } catch( const uno::Exception
& ) {}
291 m_xObjectStream
= uno::Reference
< io::XStream
>();
294 m_xParentStorage
= uno::Reference
< embed::XStorage
>();
299 //------------------------------------------------------
300 uno::Sequence
< sal_Int8
> SAL_CALL
OleEmbeddedObject::getClassID()
301 throw ( uno::RuntimeException
)
303 // begin wrapping related part ====================
304 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
305 if ( xWrappedObject
.is() )
307 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
308 return xWrappedObject
->getClassID();
310 // end wrapping related part ====================
312 ::osl::MutexGuard
aGuard( m_aMutex
);
314 throw lang::DisposedException(); // TODO
319 //------------------------------------------------------
320 OUString SAL_CALL
OleEmbeddedObject::getClassName()
321 throw ( uno::RuntimeException
)
323 // begin wrapping related part ====================
324 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
325 if ( xWrappedObject
.is() )
327 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
328 return xWrappedObject
->getClassName();
330 // end wrapping related part ====================
332 ::osl::MutexGuard
aGuard( m_aMutex
);
334 throw lang::DisposedException(); // TODO
339 //------------------------------------------------------
340 void SAL_CALL
OleEmbeddedObject::setClassInfo(
341 const uno::Sequence
< sal_Int8
>& aClassID
, const OUString
& aClassName
)
342 throw ( lang::NoSupportException
,
343 uno::RuntimeException
)
345 // begin wrapping related part ====================
346 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
347 if ( xWrappedObject
.is() )
349 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
350 xWrappedObject
->setClassInfo( aClassID
, aClassName
);
353 // end wrapping related part ====================
355 // the object class info can not be changed explicitly
356 throw lang::NoSupportException(); //TODO:
359 //------------------------------------------------------
360 uno::Reference
< util::XCloseable
> SAL_CALL
OleEmbeddedObject::getComponent()
361 throw ( uno::RuntimeException
)
363 // begin wrapping related part ====================
364 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
365 if ( xWrappedObject
.is() )
367 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
368 return xWrappedObject
->getComponent();
370 // end wrapping related part ====================
372 ::osl::MutexGuard
aGuard( m_aMutex
);
374 throw lang::DisposedException(); // TODO
376 if ( m_nObjectState
== -1 ) // || m_nObjectState == embed::EmbedStates::LOADED )
378 // the object is still not running
379 throw embed::WrongStateException( OUString( "The object is not loaded!\n" ),
380 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ) );
383 if ( !m_pOleComponent
)
385 // TODO/LATER: Is it correct???
386 return uno::Reference
< util::XCloseable
>();
387 // throw uno::RuntimeException(); // TODO
390 return uno::Reference
< util::XCloseable
>( static_cast< ::cppu::OWeakObject
* >( m_pOleComponent
), uno::UNO_QUERY
);
393 //----------------------------------------------
394 void SAL_CALL
OleEmbeddedObject::addStateChangeListener( const uno::Reference
< embed::XStateChangeListener
>& xListener
)
395 throw ( uno::RuntimeException
)
397 // begin wrapping related part ====================
398 uno::Reference
< embed::XStateChangeBroadcaster
> xWrappedObject( m_xWrappedObject
, uno::UNO_QUERY
);
399 if ( xWrappedObject
.is() )
401 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
402 xWrappedObject
->addStateChangeListener( xListener
);
405 // end wrapping related part ====================
407 ::osl::MutexGuard
aGuard( m_aMutex
);
409 throw lang::DisposedException(); // TODO
411 if ( !m_pInterfaceContainer
)
412 m_pInterfaceContainer
= new ::cppu::OMultiTypeInterfaceContainerHelper( m_aMutex
);
414 m_pInterfaceContainer
->addInterface( ::getCppuType( (const uno::Reference
< embed::XStateChangeListener
>*)0 ),
418 //----------------------------------------------
419 void SAL_CALL
OleEmbeddedObject::removeStateChangeListener(
420 const uno::Reference
< embed::XStateChangeListener
>& xListener
)
421 throw (uno::RuntimeException
)
423 // begin wrapping related part ====================
424 uno::Reference
< embed::XStateChangeBroadcaster
> xWrappedObject( m_xWrappedObject
, uno::UNO_QUERY
);
425 if ( xWrappedObject
.is() )
427 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
428 xWrappedObject
->removeStateChangeListener( xListener
);
431 // end wrapping related part ====================
433 ::osl::MutexGuard
aGuard( m_aMutex
);
434 if ( m_pInterfaceContainer
)
435 m_pInterfaceContainer
->removeInterface( ::getCppuType( (const uno::Reference
< embed::XStateChangeListener
>*)0 ),
440 //----------------------------------------------
441 void SAL_CALL
OleEmbeddedObject::close( sal_Bool bDeliverOwnership
)
442 throw ( util::CloseVetoException
,
443 uno::RuntimeException
)
445 // begin wrapping related part ====================
446 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
447 if ( xWrappedObject
.is() )
449 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
450 xWrappedObject
->close( bDeliverOwnership
);
453 // end wrapping related part ====================
455 ::osl::MutexGuard
aGuard( m_aMutex
);
457 throw lang::DisposedException(); // TODO
459 uno::Reference
< uno::XInterface
> xSelfHold( static_cast< ::cppu::OWeakObject
* >( this ) );
460 lang::EventObject
aSource( static_cast< ::cppu::OWeakObject
* >( this ) );
462 if ( m_pInterfaceContainer
)
464 ::cppu::OInterfaceContainerHelper
* pContainer
=
465 m_pInterfaceContainer
->getContainer( ::getCppuType( ( const uno::Reference
< util::XCloseListener
>*) NULL
) );
466 if ( pContainer
!= NULL
)
468 ::cppu::OInterfaceIteratorHelper
pIterator(*pContainer
);
469 while (pIterator
.hasMoreElements())
473 ((util::XCloseListener
*)pIterator
.next())->queryClosing( aSource
, bDeliverOwnership
);
475 catch( const uno::RuntimeException
& )
482 pContainer
= m_pInterfaceContainer
->getContainer(
483 ::getCppuType( ( const uno::Reference
< util::XCloseListener
>*) NULL
) );
484 if ( pContainer
!= NULL
)
486 ::cppu::OInterfaceIteratorHelper
pCloseIterator(*pContainer
);
487 while (pCloseIterator
.hasMoreElements())
491 ((util::XCloseListener
*)pCloseIterator
.next())->notifyClosing( aSource
);
493 catch( const uno::RuntimeException
& )
495 pCloseIterator
.remove();
504 //----------------------------------------------
505 void SAL_CALL
OleEmbeddedObject::addCloseListener( const uno::Reference
< util::XCloseListener
>& xListener
)
506 throw ( uno::RuntimeException
)
508 // begin wrapping related part ====================
509 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
510 if ( xWrappedObject
.is() )
512 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
513 xWrappedObject
->addCloseListener( xListener
);
516 // end wrapping related part ====================
518 ::osl::MutexGuard
aGuard( m_aMutex
);
520 throw lang::DisposedException(); // TODO
522 if ( !m_pInterfaceContainer
)
523 m_pInterfaceContainer
= new ::cppu::OMultiTypeInterfaceContainerHelper( m_aMutex
);
525 m_pInterfaceContainer
->addInterface( ::getCppuType( (const uno::Reference
< util::XCloseListener
>*)0 ), xListener
);
528 //----------------------------------------------
529 void SAL_CALL
OleEmbeddedObject::removeCloseListener( const uno::Reference
< util::XCloseListener
>& xListener
)
530 throw (uno::RuntimeException
)
532 // begin wrapping related part ====================
533 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
534 if ( xWrappedObject
.is() )
536 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
537 xWrappedObject
->removeCloseListener( xListener
);
540 // end wrapping related part ====================
542 ::osl::MutexGuard
aGuard( m_aMutex
);
544 throw lang::DisposedException(); // TODO
546 if ( m_pInterfaceContainer
)
547 m_pInterfaceContainer
->removeInterface( ::getCppuType( (const uno::Reference
< util::XCloseListener
>*)0 ),
551 //------------------------------------------------------
552 void SAL_CALL
OleEmbeddedObject::addEventListener( const uno::Reference
< document::XEventListener
>& xListener
)
553 throw ( uno::RuntimeException
)
555 // begin wrapping related part ====================
556 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
557 if ( xWrappedObject
.is() )
559 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
560 xWrappedObject
->addEventListener( xListener
);
563 // end wrapping related part ====================
565 ::osl::MutexGuard
aGuard( m_aMutex
);
567 throw lang::DisposedException(); // TODO
569 if ( !m_pInterfaceContainer
)
570 m_pInterfaceContainer
= new ::cppu::OMultiTypeInterfaceContainerHelper( m_aMutex
);
572 m_pInterfaceContainer
->addInterface( ::getCppuType( (const uno::Reference
< document::XEventListener
>*)0 ), xListener
);
575 //------------------------------------------------------
576 void SAL_CALL
OleEmbeddedObject::removeEventListener(
577 const uno::Reference
< document::XEventListener
>& xListener
)
578 throw ( uno::RuntimeException
)
580 // begin wrapping related part ====================
581 uno::Reference
< embed::XEmbeddedObject
> xWrappedObject
= m_xWrappedObject
;
582 if ( xWrappedObject
.is() )
584 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
585 xWrappedObject
->removeEventListener( xListener
);
588 // end wrapping related part ====================
590 ::osl::MutexGuard
aGuard( m_aMutex
);
592 throw lang::DisposedException(); // TODO
594 if ( m_pInterfaceContainer
)
595 m_pInterfaceContainer
->removeInterface( ::getCppuType( (const uno::Reference
< document::XEventListener
>*)0 ),
599 // XInplaceObject ( wrapper related implementation )
600 //------------------------------------------------------
601 void SAL_CALL
OleEmbeddedObject::setObjectRectangles( const awt::Rectangle
& aPosRect
,
602 const awt::Rectangle
& aClipRect
)
603 throw ( embed::WrongStateException
,
605 uno::RuntimeException
)
607 // begin wrapping related part ====================
608 uno::Reference
< embed::XInplaceObject
> xWrappedObject( m_xWrappedObject
, uno::UNO_QUERY
);
609 if ( xWrappedObject
.is() )
611 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
612 xWrappedObject
->setObjectRectangles( aPosRect
, aClipRect
);
615 // end wrapping related part ====================
617 throw embed::WrongStateException();
620 //------------------------------------------------------
621 void SAL_CALL
OleEmbeddedObject::enableModeless( sal_Bool bEnable
)
622 throw ( embed::WrongStateException
,
624 uno::RuntimeException
)
626 // begin wrapping related part ====================
627 uno::Reference
< embed::XInplaceObject
> xWrappedObject( m_xWrappedObject
, uno::UNO_QUERY
);
628 if ( xWrappedObject
.is() )
630 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
631 xWrappedObject
->enableModeless( bEnable
);
634 // end wrapping related part ====================
636 throw embed::WrongStateException();
639 //------------------------------------------------------
640 void SAL_CALL
OleEmbeddedObject::translateAccelerators(
641 const uno::Sequence
< awt::KeyEvent
>& aKeys
)
642 throw ( embed::WrongStateException
,
643 uno::RuntimeException
)
645 // begin wrapping related part ====================
646 uno::Reference
< embed::XInplaceObject
> xWrappedObject( m_xWrappedObject
, uno::UNO_QUERY
);
647 if ( xWrappedObject
.is() )
649 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
650 xWrappedObject
->translateAccelerators( aKeys
);
653 // end wrapping related part ====================
658 //------------------------------------------------------
659 com::sun::star::uno::Reference
< com::sun::star::uno::XInterface
> SAL_CALL
OleEmbeddedObject::getParent() throw (::com::sun::star::uno::RuntimeException
)
661 // begin wrapping related part ====================
662 uno::Reference
< container::XChild
> xWrappedObject( m_xWrappedObject
, uno::UNO_QUERY
);
663 if ( xWrappedObject
.is() )
665 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
666 return xWrappedObject
->getParent();
668 // end wrapping related part ====================
673 //------------------------------------------------------
674 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
)
676 // begin wrapping related part ====================
677 uno::Reference
< container::XChild
> xWrappedObject( m_xWrappedObject
, uno::UNO_QUERY
);
678 if ( xWrappedObject
.is() )
680 // the object was converted to OOo embedded object, the current implementation is now only a wrapper
681 xWrappedObject
->setParent( xParent
);
684 // end wrapping related part ====================
689 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */