1*dde7d3faSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*dde7d3faSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*dde7d3faSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*dde7d3faSAndrew Rist  * distributed with this work for additional information
6*dde7d3faSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*dde7d3faSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*dde7d3faSAndrew Rist  * "License"); you may not use this file except in compliance
9*dde7d3faSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*dde7d3faSAndrew Rist  *
11*dde7d3faSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*dde7d3faSAndrew Rist  *
13*dde7d3faSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*dde7d3faSAndrew Rist  * software distributed under the License is distributed on an
15*dde7d3faSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*dde7d3faSAndrew Rist  * KIND, either express or implied.  See the License for the
17*dde7d3faSAndrew Rist  * specific language governing permissions and limitations
18*dde7d3faSAndrew Rist  * under the License.
19*dde7d3faSAndrew Rist  *
20*dde7d3faSAndrew Rist  *************************************************************/
21*dde7d3faSAndrew Rist 
22*dde7d3faSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_comphelper.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "comphelper_module.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <com/sun/star/util/XCloseBroadcaster.hpp>
30cdf0e10cSrcweir #include <com/sun/star/util/XCloseable.hpp>
31cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp>
32cdf0e10cSrcweir #include <com/sun/star/lang/IllegalArgumentException.hpp>
33cdf0e10cSrcweir #include <com/sun/star/frame/XDesktop.hpp>
34cdf0e10cSrcweir #include <com/sun/star/frame/DoubleInitializationException.hpp>
35cdf0e10cSrcweir #include <com/sun/star/frame/DoubleInitializationException.hpp>
36cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #include "instancelocker.hxx"
39cdf0e10cSrcweir 
40cdf0e10cSrcweir using namespace ::com::sun::star;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir 
43cdf0e10cSrcweir // ====================================================================
44cdf0e10cSrcweir // OInstanceLocker
45cdf0e10cSrcweir // ====================================================================
46cdf0e10cSrcweir 
47cdf0e10cSrcweir // --------------------------------------------------------
OInstanceLocker(const uno::Reference<uno::XComponentContext> & xContext)48cdf0e10cSrcweir OInstanceLocker::OInstanceLocker( const uno::Reference< uno::XComponentContext >& xContext )
49cdf0e10cSrcweir : m_xContext( xContext )
50cdf0e10cSrcweir , m_pLockListener( NULL )
51cdf0e10cSrcweir , m_pListenersContainer( NULL )
52cdf0e10cSrcweir , m_bDisposed( sal_False )
53cdf0e10cSrcweir , m_bInitialized( sal_False )
54cdf0e10cSrcweir {
55cdf0e10cSrcweir }
56cdf0e10cSrcweir 
57cdf0e10cSrcweir // --------------------------------------------------------
~OInstanceLocker()58cdf0e10cSrcweir OInstanceLocker::~OInstanceLocker()
59cdf0e10cSrcweir {
60cdf0e10cSrcweir 	if ( !m_bDisposed )
61cdf0e10cSrcweir 	{
62cdf0e10cSrcweir 		m_refCount++; // to call dispose
63cdf0e10cSrcweir 		try {
64cdf0e10cSrcweir 			dispose();
65cdf0e10cSrcweir 		}
66cdf0e10cSrcweir 		catch ( uno::RuntimeException& )
67cdf0e10cSrcweir 		{}
68cdf0e10cSrcweir 	}
69cdf0e10cSrcweir 
70cdf0e10cSrcweir 	if ( m_pListenersContainer )
71cdf0e10cSrcweir 	{
72cdf0e10cSrcweir 		delete m_pListenersContainer;
73cdf0e10cSrcweir 		m_pListenersContainer = NULL;
74cdf0e10cSrcweir 	}
75cdf0e10cSrcweir }
76cdf0e10cSrcweir 
77cdf0e10cSrcweir // XComponent
78cdf0e10cSrcweir // --------------------------------------------------------
dispose()79cdf0e10cSrcweir void SAL_CALL OInstanceLocker::dispose()
80cdf0e10cSrcweir 	throw (uno::RuntimeException)
81cdf0e10cSrcweir {
82cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 	if ( m_bDisposed )
85cdf0e10cSrcweir 		throw lang::DisposedException();
86cdf0e10cSrcweir 
87cdf0e10cSrcweir    	lang::EventObject aSource( static_cast< ::cppu::OWeakObject* >(this) );
88cdf0e10cSrcweir 	if ( m_pListenersContainer )
89cdf0e10cSrcweir 		m_pListenersContainer->disposeAndClear( aSource );
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 	if ( m_xLockListener.is() )
92cdf0e10cSrcweir 	{
93cdf0e10cSrcweir 		if ( m_pLockListener )
94cdf0e10cSrcweir 		{
95cdf0e10cSrcweir 			m_pLockListener->Dispose();
96cdf0e10cSrcweir 			m_pLockListener = NULL;
97cdf0e10cSrcweir 		}
98cdf0e10cSrcweir 		m_xLockListener = uno::Reference< uno::XInterface >();
99cdf0e10cSrcweir 	}
100cdf0e10cSrcweir 
101cdf0e10cSrcweir 	m_bDisposed = sal_True;
102cdf0e10cSrcweir }
103cdf0e10cSrcweir 
104cdf0e10cSrcweir // --------------------------------------------------------
addEventListener(const uno::Reference<lang::XEventListener> & xListener)105cdf0e10cSrcweir void SAL_CALL OInstanceLocker::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
106cdf0e10cSrcweir 	throw (uno::RuntimeException)
107cdf0e10cSrcweir {
108cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
109cdf0e10cSrcweir 	if ( m_bDisposed )
110cdf0e10cSrcweir 		throw lang::DisposedException(); // TODO
111cdf0e10cSrcweir 
112cdf0e10cSrcweir 	if ( !m_pListenersContainer )
113cdf0e10cSrcweir 		m_pListenersContainer = new ::cppu::OInterfaceContainerHelper( m_aMutex );
114cdf0e10cSrcweir 
115cdf0e10cSrcweir 	m_pListenersContainer->addInterface( xListener );
116cdf0e10cSrcweir }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir // --------------------------------------------------------
removeEventListener(const uno::Reference<lang::XEventListener> & xListener)119cdf0e10cSrcweir void SAL_CALL OInstanceLocker::removeEventListener( const uno::Reference< lang::XEventListener >& xListener )
120cdf0e10cSrcweir 	throw (uno::RuntimeException)
121cdf0e10cSrcweir {
122cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
123cdf0e10cSrcweir 	if ( m_pListenersContainer )
124cdf0e10cSrcweir 		m_pListenersContainer->removeInterface( xListener );
125cdf0e10cSrcweir }
126cdf0e10cSrcweir 
127cdf0e10cSrcweir // XInitialization
128cdf0e10cSrcweir // --------------------------------------------------------
initialize(const uno::Sequence<uno::Any> & aArguments)129cdf0e10cSrcweir void SAL_CALL OInstanceLocker::initialize( const uno::Sequence< uno::Any >& aArguments )
130cdf0e10cSrcweir 	throw (uno::Exception, uno::RuntimeException)
131cdf0e10cSrcweir {
132cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
133cdf0e10cSrcweir 	if ( m_bInitialized )
134cdf0e10cSrcweir 		throw frame::DoubleInitializationException();
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 	if ( m_bDisposed )
137cdf0e10cSrcweir 		throw lang::DisposedException(); // TODO
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 	if ( !m_refCount )
140cdf0e10cSrcweir 		throw uno::RuntimeException(); // the object must be refcounted already!
141cdf0e10cSrcweir 
142cdf0e10cSrcweir 	uno::Reference< uno::XInterface > xInstance;
143cdf0e10cSrcweir 	uno::Reference< embed::XActionsApproval > xApproval;
144cdf0e10cSrcweir 	sal_Int32 nModes = 0;
145cdf0e10cSrcweir 
146cdf0e10cSrcweir 	try
147cdf0e10cSrcweir 	{
148cdf0e10cSrcweir 		sal_Int32 nLen = aArguments.getLength();
149cdf0e10cSrcweir 		if ( nLen < 2 || nLen > 3 )
150cdf0e10cSrcweir 			throw lang::IllegalArgumentException(
151cdf0e10cSrcweir 							::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Wrong count of parameters!" ) ),
152cdf0e10cSrcweir 							uno::Reference< uno::XInterface >(),
153cdf0e10cSrcweir 							0 );
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 		if ( !( aArguments[0] >>= xInstance ) || !xInstance.is() )
156cdf0e10cSrcweir 			throw lang::IllegalArgumentException(
157cdf0e10cSrcweir 					::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Nonempty reference is expected as the first argument!" ) ),
158cdf0e10cSrcweir 					uno::Reference< uno::XInterface >(),
159cdf0e10cSrcweir 					0 );
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 		if (
162cdf0e10cSrcweir             !( aArguments[1] >>= nModes ) ||
163cdf0e10cSrcweir             (
164cdf0e10cSrcweir               !( nModes & embed::Actions::PREVENT_CLOSE ) &&
165cdf0e10cSrcweir               !( nModes & embed::Actions::PREVENT_TERMINATION )
166cdf0e10cSrcweir             )
167cdf0e10cSrcweir            )
168cdf0e10cSrcweir         {
169cdf0e10cSrcweir 			throw lang::IllegalArgumentException(
170cdf0e10cSrcweir 					::rtl::OUString(
171cdf0e10cSrcweir 							RTL_CONSTASCII_USTRINGPARAM("The correct modes set is expected as the second argument!" ) ),
172cdf0e10cSrcweir 					uno::Reference< uno::XInterface >(),
173cdf0e10cSrcweir 					0 );
174cdf0e10cSrcweir         }
175cdf0e10cSrcweir 
176cdf0e10cSrcweir 		if ( nLen == 3 && !( aArguments[2] >>= xApproval ) )
177cdf0e10cSrcweir 			throw lang::IllegalArgumentException(
178cdf0e10cSrcweir 					::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("If the third argument is provided, it must be XActionsApproval implementation!" ) ),
179cdf0e10cSrcweir 					uno::Reference< uno::XInterface >(),
180cdf0e10cSrcweir 					0 );
181cdf0e10cSrcweir 
182cdf0e10cSrcweir 		m_pLockListener = new OLockListener( uno::Reference< lang::XComponent > ( static_cast< lang::XComponent* >( this ) ),
183cdf0e10cSrcweir 											xInstance,
184cdf0e10cSrcweir 											nModes,
185cdf0e10cSrcweir 											xApproval );
186cdf0e10cSrcweir 		m_xLockListener = uno::Reference< uno::XInterface >( static_cast< OWeakObject* >( m_pLockListener ) );
187cdf0e10cSrcweir 		m_pLockListener->Init();
188cdf0e10cSrcweir 	}
189cdf0e10cSrcweir 	catch( uno::Exception& )
190cdf0e10cSrcweir 	{
191cdf0e10cSrcweir 		dispose();
192cdf0e10cSrcweir 		throw;
193cdf0e10cSrcweir 	}
194cdf0e10cSrcweir 
195cdf0e10cSrcweir 	m_bInitialized = sal_True;
196cdf0e10cSrcweir }
197cdf0e10cSrcweir 
198cdf0e10cSrcweir 
199cdf0e10cSrcweir // XServiceInfo
200cdf0e10cSrcweir // --------------------------------------------------------
getImplementationName()201cdf0e10cSrcweir ::rtl::OUString SAL_CALL OInstanceLocker::getImplementationName(  )
202cdf0e10cSrcweir 	throw (uno::RuntimeException)
203cdf0e10cSrcweir {
204cdf0e10cSrcweir 	return getImplementationName_static();
205cdf0e10cSrcweir }
206cdf0e10cSrcweir 
207cdf0e10cSrcweir // --------------------------------------------------------
supportsService(const::rtl::OUString & ServiceName)208cdf0e10cSrcweir ::sal_Bool SAL_CALL OInstanceLocker::supportsService( const ::rtl::OUString& ServiceName )
209cdf0e10cSrcweir 	throw (uno::RuntimeException)
210cdf0e10cSrcweir {
211cdf0e10cSrcweir 	uno::Sequence< ::rtl::OUString > aSeq = getSupportedServiceNames();
212cdf0e10cSrcweir 
213cdf0e10cSrcweir 	for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ )
214cdf0e10cSrcweir     	if ( ServiceName.compareTo( aSeq[nInd] ) == 0 )
215cdf0e10cSrcweir         	return sal_True;
216cdf0e10cSrcweir 
217cdf0e10cSrcweir 	return sal_False;
218cdf0e10cSrcweir }
219cdf0e10cSrcweir 
220cdf0e10cSrcweir // --------------------------------------------------------
getSupportedServiceNames()221cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > SAL_CALL OInstanceLocker::getSupportedServiceNames()
222cdf0e10cSrcweir 	throw (uno::RuntimeException)
223cdf0e10cSrcweir {
224cdf0e10cSrcweir 	return getSupportedServiceNames_static();
225cdf0e10cSrcweir }
226cdf0e10cSrcweir 
227cdf0e10cSrcweir // Static methods
228cdf0e10cSrcweir // --------------------------------------------------------
getSupportedServiceNames_static()229cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > SAL_CALL OInstanceLocker::getSupportedServiceNames_static()
230cdf0e10cSrcweir {
231cdf0e10cSrcweir     const rtl::OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.embed.InstanceLocker" ) );
232cdf0e10cSrcweir     return uno::Sequence< rtl::OUString >( &aServiceName, 1 );
233cdf0e10cSrcweir }
234cdf0e10cSrcweir 
235cdf0e10cSrcweir // --------------------------------------------------------
getImplementationName_static()236cdf0e10cSrcweir ::rtl::OUString SAL_CALL OInstanceLocker::getImplementationName_static()
237cdf0e10cSrcweir {
238cdf0e10cSrcweir     return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.embed.InstanceLocker" ) );
239cdf0e10cSrcweir }
240cdf0e10cSrcweir 
241cdf0e10cSrcweir // --------------------------------------------------------
Create(const uno::Reference<uno::XComponentContext> & rxContext)242cdf0e10cSrcweir uno::Reference< uno::XInterface > SAL_CALL OInstanceLocker::Create(
243cdf0e10cSrcweir 								const uno::Reference< uno::XComponentContext >& rxContext )
244cdf0e10cSrcweir {
245cdf0e10cSrcweir     return static_cast< cppu::OWeakObject * >( new OInstanceLocker( rxContext ) );
246cdf0e10cSrcweir }
247cdf0e10cSrcweir 
248cdf0e10cSrcweir 
249cdf0e10cSrcweir 
250cdf0e10cSrcweir // ====================================================================
251cdf0e10cSrcweir // OLockListener
252cdf0e10cSrcweir // ====================================================================
253cdf0e10cSrcweir 
254cdf0e10cSrcweir // --------------------------------------------------------
OLockListener(const uno::WeakReference<lang::XComponent> & xWrapper,const uno::Reference<uno::XInterface> & xInstance,sal_Int32 nMode,const uno::Reference<embed::XActionsApproval> xApproval)255cdf0e10cSrcweir OLockListener::OLockListener( const uno::WeakReference< lang::XComponent >& xWrapper,
256cdf0e10cSrcweir 					const uno::Reference< uno::XInterface >& xInstance,
257cdf0e10cSrcweir 					sal_Int32 nMode,
258cdf0e10cSrcweir 					const uno::Reference< embed::XActionsApproval > xApproval )
259cdf0e10cSrcweir : m_xInstance( xInstance )
260cdf0e10cSrcweir , m_xApproval( xApproval )
261cdf0e10cSrcweir , m_xWrapper( xWrapper )
262cdf0e10cSrcweir , m_bDisposed( sal_False )
263cdf0e10cSrcweir , m_bInitialized( sal_False )
264cdf0e10cSrcweir , m_nMode( nMode )
265cdf0e10cSrcweir {
266cdf0e10cSrcweir }
267cdf0e10cSrcweir 
268cdf0e10cSrcweir // --------------------------------------------------------
~OLockListener()269cdf0e10cSrcweir OLockListener::~OLockListener()
270cdf0e10cSrcweir {
271cdf0e10cSrcweir }
272cdf0e10cSrcweir 
273cdf0e10cSrcweir // --------------------------------------------------------
Dispose()274cdf0e10cSrcweir void OLockListener::Dispose()
275cdf0e10cSrcweir {
276cdf0e10cSrcweir 	::osl::ResettableMutexGuard aGuard( m_aMutex );
277cdf0e10cSrcweir 
278cdf0e10cSrcweir 	if ( m_bDisposed )
279cdf0e10cSrcweir 		return;
280cdf0e10cSrcweir 
281cdf0e10cSrcweir 	if ( m_nMode & embed::Actions::PREVENT_CLOSE )
282cdf0e10cSrcweir 	{
283cdf0e10cSrcweir 		try
284cdf0e10cSrcweir 		{
285cdf0e10cSrcweir 			uno::Reference< util::XCloseBroadcaster > xCloseBroadcaster( m_xInstance, uno::UNO_QUERY );
286cdf0e10cSrcweir 			if ( xCloseBroadcaster.is() )
287cdf0e10cSrcweir 				xCloseBroadcaster->removeCloseListener( static_cast< util::XCloseListener* >( this ) );
288cdf0e10cSrcweir 
289cdf0e10cSrcweir 			uno::Reference< util::XCloseable > xCloseable( m_xInstance, uno::UNO_QUERY );
290cdf0e10cSrcweir 			if ( xCloseable.is() )
291cdf0e10cSrcweir 				xCloseable->close( sal_True );
292cdf0e10cSrcweir 		}
293cdf0e10cSrcweir 		catch( uno::Exception& )
294cdf0e10cSrcweir 		{}
295cdf0e10cSrcweir 	}
296cdf0e10cSrcweir 
297cdf0e10cSrcweir 	if ( m_nMode & embed::Actions::PREVENT_TERMINATION )
298cdf0e10cSrcweir 	{
299cdf0e10cSrcweir 		try
300cdf0e10cSrcweir 		{
301cdf0e10cSrcweir 			uno::Reference< frame::XDesktop > xDesktop( m_xInstance, uno::UNO_QUERY_THROW );
302cdf0e10cSrcweir 			xDesktop->removeTerminateListener( static_cast< frame::XTerminateListener* >( this ) );
303cdf0e10cSrcweir 		}
304cdf0e10cSrcweir 		catch( uno::Exception& )
305cdf0e10cSrcweir 		{}
306cdf0e10cSrcweir 	}
307cdf0e10cSrcweir 
308cdf0e10cSrcweir 	m_xInstance = uno::Reference< uno::XInterface >();
309cdf0e10cSrcweir 	m_bDisposed = sal_True;
310cdf0e10cSrcweir }
311cdf0e10cSrcweir 
312cdf0e10cSrcweir // XEventListener
313cdf0e10cSrcweir // --------------------------------------------------------
disposing(const lang::EventObject & aEvent)314cdf0e10cSrcweir void SAL_CALL OLockListener::disposing( const lang::EventObject& aEvent )
315cdf0e10cSrcweir 	throw (uno::RuntimeException)
316cdf0e10cSrcweir {
317cdf0e10cSrcweir 	::osl::ResettableMutexGuard aGuard( m_aMutex );
318cdf0e10cSrcweir 
319cdf0e10cSrcweir 	// object is disposed
320cdf0e10cSrcweir 	if ( aEvent.Source == m_xInstance )
321cdf0e10cSrcweir 	{
322cdf0e10cSrcweir 		// the object does not listen for anything any more
323cdf0e10cSrcweir 		m_nMode = 0;
324cdf0e10cSrcweir 
325cdf0e10cSrcweir 		// dispose the wrapper;
326cdf0e10cSrcweir 		uno::Reference< lang::XComponent > xComponent( m_xWrapper.get(), uno::UNO_QUERY );
327cdf0e10cSrcweir 		aGuard.clear();
328cdf0e10cSrcweir 		if ( xComponent.is() )
329cdf0e10cSrcweir 		{
330cdf0e10cSrcweir 			try { xComponent->dispose(); }
331cdf0e10cSrcweir 			catch( uno::Exception& ){}
332cdf0e10cSrcweir 		}
333cdf0e10cSrcweir 	}
334cdf0e10cSrcweir }
335cdf0e10cSrcweir 
336cdf0e10cSrcweir 
337cdf0e10cSrcweir // XCloseListener
338cdf0e10cSrcweir // --------------------------------------------------------
queryClosing(const lang::EventObject & aEvent,sal_Bool)339cdf0e10cSrcweir void SAL_CALL OLockListener::queryClosing( const lang::EventObject& aEvent, sal_Bool )
340cdf0e10cSrcweir 	throw (util::CloseVetoException, uno::RuntimeException)
341cdf0e10cSrcweir {
342cdf0e10cSrcweir 	// GetsOwnership parameter is always ignored, the user of the service must close the object always
343cdf0e10cSrcweir 	::osl::ResettableMutexGuard aGuard( m_aMutex );
344cdf0e10cSrcweir 	if ( !m_bDisposed && aEvent.Source == m_xInstance && ( m_nMode & embed::Actions::PREVENT_CLOSE ) )
345cdf0e10cSrcweir 	{
346cdf0e10cSrcweir 		try
347cdf0e10cSrcweir 		{
348cdf0e10cSrcweir 			uno::Reference< embed::XActionsApproval > xApprove = m_xApproval;
349cdf0e10cSrcweir 
350cdf0e10cSrcweir 			// unlock the mutex here
351cdf0e10cSrcweir 			aGuard.clear();
352cdf0e10cSrcweir 
353cdf0e10cSrcweir 			if ( xApprove.is() && xApprove->approveAction( embed::Actions::PREVENT_CLOSE ) )
354cdf0e10cSrcweir 				throw util::CloseVetoException();
355cdf0e10cSrcweir 		}
356cdf0e10cSrcweir 		catch( util::CloseVetoException& )
357cdf0e10cSrcweir 		{
358cdf0e10cSrcweir 			// rethrow this exception
359cdf0e10cSrcweir 			throw;
360cdf0e10cSrcweir 		}
361cdf0e10cSrcweir 		catch( uno::Exception& )
362cdf0e10cSrcweir 		{
363cdf0e10cSrcweir 			// no action should be done
364cdf0e10cSrcweir 		}
365cdf0e10cSrcweir 	}
366cdf0e10cSrcweir }
367cdf0e10cSrcweir 
368cdf0e10cSrcweir // --------------------------------------------------------
notifyClosing(const lang::EventObject & aEvent)369cdf0e10cSrcweir void SAL_CALL OLockListener::notifyClosing( const lang::EventObject& aEvent )
370cdf0e10cSrcweir 	throw (uno::RuntimeException)
371cdf0e10cSrcweir {
372cdf0e10cSrcweir 	::osl::ResettableMutexGuard aGuard( m_aMutex );
373cdf0e10cSrcweir 
374cdf0e10cSrcweir 	// object is closed, no reason to listen
375cdf0e10cSrcweir 	if ( aEvent.Source == m_xInstance )
376cdf0e10cSrcweir 	{
377cdf0e10cSrcweir 		uno::Reference< util::XCloseBroadcaster > xCloseBroadcaster( aEvent.Source, uno::UNO_QUERY );
378cdf0e10cSrcweir 		if ( xCloseBroadcaster.is() )
379cdf0e10cSrcweir 		{
380cdf0e10cSrcweir 			xCloseBroadcaster->removeCloseListener( static_cast< util::XCloseListener* >( this ) );
381cdf0e10cSrcweir 			m_nMode &= ~embed::Actions::PREVENT_CLOSE;
382cdf0e10cSrcweir 			if ( !m_nMode )
383cdf0e10cSrcweir 			{
384cdf0e10cSrcweir 				// dispose the wrapper;
385cdf0e10cSrcweir 				uno::Reference< lang::XComponent > xComponent( m_xWrapper.get(), uno::UNO_QUERY );
386cdf0e10cSrcweir 				aGuard.clear();
387cdf0e10cSrcweir 				if ( xComponent.is() )
388cdf0e10cSrcweir 				{
389cdf0e10cSrcweir 					try { xComponent->dispose(); }
390cdf0e10cSrcweir 					catch( uno::Exception& ){}
391cdf0e10cSrcweir 				}
392cdf0e10cSrcweir 			}
393cdf0e10cSrcweir 		}
394cdf0e10cSrcweir 	}
395cdf0e10cSrcweir }
396cdf0e10cSrcweir 
397cdf0e10cSrcweir 
398cdf0e10cSrcweir // XTerminateListener
399cdf0e10cSrcweir // --------------------------------------------------------
queryTermination(const lang::EventObject & aEvent)400cdf0e10cSrcweir void SAL_CALL OLockListener::queryTermination( const lang::EventObject& aEvent )
401cdf0e10cSrcweir 	throw (frame::TerminationVetoException, uno::RuntimeException)
402cdf0e10cSrcweir {
403cdf0e10cSrcweir 	::osl::ResettableMutexGuard aGuard( m_aMutex );
404cdf0e10cSrcweir 	if ( aEvent.Source == m_xInstance && ( m_nMode & embed::Actions::PREVENT_TERMINATION ) )
405cdf0e10cSrcweir 	{
406cdf0e10cSrcweir 		try
407cdf0e10cSrcweir 		{
408cdf0e10cSrcweir 			uno::Reference< embed::XActionsApproval > xApprove = m_xApproval;
409cdf0e10cSrcweir 
410cdf0e10cSrcweir 			// unlock the mutex here
411cdf0e10cSrcweir 			aGuard.clear();
412cdf0e10cSrcweir 
413cdf0e10cSrcweir 			if ( xApprove.is() && xApprove->approveAction( embed::Actions::PREVENT_TERMINATION ) )
414cdf0e10cSrcweir 				throw frame::TerminationVetoException();
415cdf0e10cSrcweir 		}
416cdf0e10cSrcweir 		catch( frame::TerminationVetoException& )
417cdf0e10cSrcweir 		{
418cdf0e10cSrcweir 			// rethrow this exception
419cdf0e10cSrcweir 			throw;
420cdf0e10cSrcweir 		}
421cdf0e10cSrcweir 		catch( uno::Exception& )
422cdf0e10cSrcweir 		{
423cdf0e10cSrcweir 			// no action should be done
424cdf0e10cSrcweir 		}
425cdf0e10cSrcweir 	}
426cdf0e10cSrcweir }
427cdf0e10cSrcweir 
428cdf0e10cSrcweir // --------------------------------------------------------
notifyTermination(const lang::EventObject & aEvent)429cdf0e10cSrcweir void SAL_CALL OLockListener::notifyTermination( const lang::EventObject& aEvent )
430cdf0e10cSrcweir 	throw (uno::RuntimeException)
431cdf0e10cSrcweir {
432cdf0e10cSrcweir 	::osl::ResettableMutexGuard aGuard( m_aMutex );
433cdf0e10cSrcweir 
434cdf0e10cSrcweir 	// object is terminated, no reason to listen
435cdf0e10cSrcweir 	if ( aEvent.Source == m_xInstance )
436cdf0e10cSrcweir 	{
437cdf0e10cSrcweir 		uno::Reference< frame::XDesktop > xDesktop( aEvent.Source, uno::UNO_QUERY );
438cdf0e10cSrcweir 		if ( xDesktop.is() )
439cdf0e10cSrcweir 		{
440cdf0e10cSrcweir 			try
441cdf0e10cSrcweir 			{
442cdf0e10cSrcweir 				xDesktop->removeTerminateListener( static_cast< frame::XTerminateListener* >( this ) );
443cdf0e10cSrcweir 				m_nMode &= ~embed::Actions::PREVENT_TERMINATION;
444cdf0e10cSrcweir 				if ( !m_nMode )
445cdf0e10cSrcweir 				{
446cdf0e10cSrcweir 					// dispose the wrapper;
447cdf0e10cSrcweir 					uno::Reference< lang::XComponent > xComponent( m_xWrapper.get(), uno::UNO_QUERY );
448cdf0e10cSrcweir 					aGuard.clear();
449cdf0e10cSrcweir 					if ( xComponent.is() )
450cdf0e10cSrcweir 					{
451cdf0e10cSrcweir 						try { xComponent->dispose(); }
452cdf0e10cSrcweir 						catch( uno::Exception& ){}
453cdf0e10cSrcweir 					}
454cdf0e10cSrcweir 				}
455cdf0e10cSrcweir 			}
456cdf0e10cSrcweir 			catch( uno::Exception& )
457cdf0e10cSrcweir 			{}
458cdf0e10cSrcweir 		}
459cdf0e10cSrcweir 	}
460cdf0e10cSrcweir }
461cdf0e10cSrcweir 
462cdf0e10cSrcweir 
463cdf0e10cSrcweir // XInitialization
464cdf0e10cSrcweir // --------------------------------------------------------
Init()465cdf0e10cSrcweir sal_Bool OLockListener::Init()
466cdf0e10cSrcweir {
467cdf0e10cSrcweir 	::osl::ResettableMutexGuard aGuard( m_aMutex );
468cdf0e10cSrcweir 
469cdf0e10cSrcweir 	if ( m_bDisposed || m_bInitialized )
470cdf0e10cSrcweir 		return sal_False;
471cdf0e10cSrcweir 
472cdf0e10cSrcweir 	try
473cdf0e10cSrcweir 	{
474cdf0e10cSrcweir 		if ( m_nMode & embed::Actions::PREVENT_CLOSE )
475cdf0e10cSrcweir 		{
476cdf0e10cSrcweir 			uno::Reference< util::XCloseBroadcaster > xCloseBroadcaster( m_xInstance, uno::UNO_QUERY_THROW );
477cdf0e10cSrcweir 			xCloseBroadcaster->addCloseListener( static_cast< util::XCloseListener* >( this ) );
478cdf0e10cSrcweir 		}
479cdf0e10cSrcweir 
480cdf0e10cSrcweir 		if ( m_nMode & embed::Actions::PREVENT_TERMINATION )
481cdf0e10cSrcweir 		{
482cdf0e10cSrcweir 			uno::Reference< frame::XDesktop > xDesktop( m_xInstance, uno::UNO_QUERY_THROW );
483cdf0e10cSrcweir 			xDesktop->addTerminateListener( static_cast< frame::XTerminateListener* >( this ) );
484cdf0e10cSrcweir 		}
485cdf0e10cSrcweir 	}
486cdf0e10cSrcweir 	catch( uno::Exception& )
487cdf0e10cSrcweir 	{
488cdf0e10cSrcweir 		// dispose the wrapper;
489cdf0e10cSrcweir 		uno::Reference< lang::XComponent > xComponent( m_xWrapper.get(), uno::UNO_QUERY );
490cdf0e10cSrcweir 		aGuard.clear();
491cdf0e10cSrcweir 		if ( xComponent.is() )
492cdf0e10cSrcweir 		{
493cdf0e10cSrcweir 			try { xComponent->dispose(); }
494cdf0e10cSrcweir 			catch( uno::Exception& ){}
495cdf0e10cSrcweir 		}
496cdf0e10cSrcweir 
497cdf0e10cSrcweir 		throw;
498cdf0e10cSrcweir 	}
499cdf0e10cSrcweir 
500cdf0e10cSrcweir 	m_bInitialized = sal_True;
501cdf0e10cSrcweir 
502cdf0e10cSrcweir 	return sal_True;
503cdf0e10cSrcweir }
504cdf0e10cSrcweir 
createRegistryInfo_OInstanceLocker()505cdf0e10cSrcweir void createRegistryInfo_OInstanceLocker()
506cdf0e10cSrcweir {
507cdf0e10cSrcweir     static ::comphelper::module::OAutoRegistration< OInstanceLocker > aAutoRegistration;
508cdf0e10cSrcweir }
509