1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_extensions.hxx"
26 #include "loadlisteneradapter.hxx"
27 #include <osl/diagnose.h>
28 #include <vos/ref.hxx>
29 
30 //.........................................................................
31 namespace bib
32 {
33 //.........................................................................
34 
35 	using namespace ::com::sun::star::uno;
36 	using namespace ::com::sun::star::lang;
37 	using namespace ::com::sun::star::form;
38 
39 	//=====================================================================
40 	//= OComponentListener
41 	//=====================================================================
42 	//---------------------------------------------------------------------
~OComponentListener()43 	OComponentListener::~OComponentListener()
44 	{
45 		{
46 			::osl::MutexGuard aGuard( m_rMutex );
47 			if ( m_pAdapter )
48 				m_pAdapter->dispose();
49 		}
50 	}
51 
52 	//---------------------------------------------------------------------
_disposing(const EventObject &)53 	void OComponentListener::_disposing( const EventObject& /*_rSource*/ ) throw( RuntimeException)
54 	{
55 		// nothing to do here, overrride if you're interested in
56 	}
57 
58 	//---------------------------------------------------------------------
setAdapter(OComponentAdapterBase * pAdapter)59 	void OComponentListener::setAdapter( OComponentAdapterBase* pAdapter )
60 	{
61 		{
62 			::osl::MutexGuard aGuard( m_rMutex );
63 			if ( m_pAdapter )
64 			{
65 				m_pAdapter->release();
66 				m_pAdapter = NULL;
67 			}
68 		}
69 
70 		if ( pAdapter )
71 		{
72 			::osl::MutexGuard aGuard( m_rMutex );
73 			m_pAdapter = pAdapter;
74 			m_pAdapter->acquire();
75 		}
76 	}
77 
78 	//=====================================================================
79 	//= OComponentAdapterBase
80 	//=====================================================================
81 	//---------------------------------------------------------------------
OComponentAdapterBase(const Reference<XComponent> & _rxComp,sal_Bool _bAutoRelease)82 	OComponentAdapterBase::OComponentAdapterBase( const Reference< XComponent >& _rxComp, sal_Bool _bAutoRelease )
83 		:m_xComponent( _rxComp )
84 		,m_pListener( NULL )
85 		,m_nLockCount( 0 )
86 		,m_bListening( sal_False )
87 		,m_bAutoRelease( _bAutoRelease )
88 	{
89 		OSL_ENSURE( m_xComponent.is(), "OComponentAdapterBase::OComponentAdapterBase: invalid component!" );
90 	}
91 
92 	//---------------------------------------------------------------------
Init(OComponentListener * _pListener)93 	void OComponentAdapterBase::Init( OComponentListener* _pListener )
94 	{
95 		OSL_ENSURE( !m_pListener, "OComponentAdapterBase::Init: already initialized!" );
96 		OSL_ENSURE( _pListener, "OComponentAdapterBase::Init: invalid listener!" );
97 
98 		m_pListener = _pListener;
99 		if ( m_pListener )
100 			m_pListener->setAdapter( this );
101 
102 		startComponentListening( );
103 		m_bListening = sal_True;
104 	}
105 
106 	//---------------------------------------------------------------------
~OComponentAdapterBase()107 	OComponentAdapterBase::~OComponentAdapterBase()
108 	{
109 	}
110 
111 	//---------------------------------------------------------------------
dispose()112 	void OComponentAdapterBase::dispose()
113 	{
114 		if ( m_bListening )
115 		{
116 			::vos::ORef< OComponentAdapterBase > xPreventDelete(this);
117 
118 			disposing();
119 
120 			m_pListener->setAdapter(NULL);
121 
122 			m_pListener = NULL;
123 			m_bListening = sal_False;
124 
125 			if (m_bAutoRelease)
126 				m_xComponent = NULL;
127 		}
128 	}
129 
130 	// XEventListener
131 
132 	//---------------------------------------------------------------------
disposing(const EventObject & _rSource)133 	void SAL_CALL OComponentAdapterBase::disposing( const EventObject& _rSource ) throw( RuntimeException )
134 	{
135 		if ( m_pListener )
136 		{
137 			 // tell the listener
138 			if ( !locked() )
139 				m_pListener->_disposing( _rSource );
140 
141 			// disconnect the listener
142 			if ( m_pListener )	// may have been reset whilest calling into _disposing
143 				m_pListener->setAdapter( NULL );
144 		}
145 
146 		m_pListener = NULL;
147 		m_bListening = sal_False;
148 
149 		if ( m_bAutoRelease )
150 			m_xComponent = NULL;
151 	}
152 
153 	//=====================================================================
154 	//= OLoadListenerAdapter
155 	//=====================================================================
156 	//---------------------------------------------------------------------
OLoadListenerAdapter(const Reference<XLoadable> & _rxLoadable,sal_Bool _bAutoRelease)157 	OLoadListenerAdapter::OLoadListenerAdapter( const Reference< XLoadable >& _rxLoadable, sal_Bool _bAutoRelease )
158 		:OComponentAdapterBase( Reference< XComponent >( _rxLoadable, UNO_QUERY ), _bAutoRelease )
159 	{
160 	}
161 
162 	//---------------------------------------------------------------------
startComponentListening()163 	void OLoadListenerAdapter::startComponentListening()
164 	{
165 		Reference< XLoadable > xLoadable( getComponent(), UNO_QUERY );
166 		OSL_ENSURE( xLoadable.is(), "OLoadListenerAdapter::OLoadListenerAdapter: invalid object!" );
167 		if ( xLoadable.is() )
168 			xLoadable->addLoadListener( this );
169 	}
170 
171 	//---------------------------------------------------------------------
acquire()172 	void SAL_CALL OLoadListenerAdapter::acquire(  ) throw ()
173 	{
174 		OLoadListenerAdapter_Base::acquire();
175 	}
176 
177 	//---------------------------------------------------------------------
release()178 	void SAL_CALL OLoadListenerAdapter::release(  ) throw ()
179 	{
180 		OLoadListenerAdapter_Base::release();
181 	}
182 
183 	//---------------------------------------------------------------------
disposing(const EventObject & _rSource)184 	void SAL_CALL OLoadListenerAdapter::disposing( const  EventObject& _rSource ) throw( RuntimeException)
185 	{
186 		OComponentAdapterBase::disposing( _rSource );
187 	}
188 
189 	//---------------------------------------------------------------------
disposing()190 	void OLoadListenerAdapter::disposing()
191 	{
192 		Reference< XLoadable > xLoadable( getComponent(), UNO_QUERY );
193 		if ( xLoadable.is() )
194 			xLoadable->removeLoadListener( this );
195 	}
196 
197 	//---------------------------------------------------------------------
loaded(const EventObject & _rEvent)198 	void SAL_CALL OLoadListenerAdapter::loaded( const EventObject& _rEvent ) throw (RuntimeException)
199 	{
200 		if ( !locked() && getLoadListener( ) )
201 			getLoadListener( )->_loaded( _rEvent );
202 	}
203 
204 	//---------------------------------------------------------------------
unloading(const EventObject & _rEvent)205 	void SAL_CALL OLoadListenerAdapter::unloading( const EventObject& _rEvent ) throw (RuntimeException)
206 	{
207 		if ( !locked() && getLoadListener( ) )
208 			getLoadListener( )->_unloading( _rEvent );
209 	}
210 
211 	//---------------------------------------------------------------------
unloaded(const EventObject & _rEvent)212 	void SAL_CALL OLoadListenerAdapter::unloaded( const EventObject& _rEvent ) throw (RuntimeException)
213 	{
214 		if ( !locked() && getLoadListener( ) )
215 			getLoadListener( )->_unloaded( _rEvent );
216 	}
217 
218 	//---------------------------------------------------------------------
reloading(const EventObject & _rEvent)219 	void SAL_CALL OLoadListenerAdapter::reloading( const EventObject& _rEvent ) throw (RuntimeException)
220 	{
221 		if ( !locked() && getLoadListener( ) )
222 			getLoadListener( )->_reloading( _rEvent );
223 	}
224 
225 	//---------------------------------------------------------------------
reloaded(const EventObject & _rEvent)226 	void SAL_CALL OLoadListenerAdapter::reloaded( const EventObject& _rEvent ) throw (RuntimeException)
227 	{
228 		if ( !locked() && getLoadListener( ) )
229 			getLoadListener( )->_reloaded( _rEvent );
230 	}
231 
232 //.........................................................................
233 }	// namespace bib
234 //.........................................................................
235 
236