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