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_comphelper.hxx"
26 #include "comphelper/containermultiplexer.hxx"
27 #include "comphelper/uno3.hxx"
28 #include <osl/diagnose.h>
29 //.........................................................................
30 namespace comphelper
31 {
32 //.........................................................................
33 
34 	using namespace ::com::sun::star::uno;
35 	using namespace ::com::sun::star::lang;
36 	using namespace ::com::sun::star::container;
37 
38 	//=====================================================================
39 	//= OContainerListener
40 	//=====================================================================
41 	//---------------------------------------------------------------------
OContainerListener(::osl::Mutex & _rMutex)42 	OContainerListener::OContainerListener(::osl::Mutex& _rMutex)
43 		:m_pAdapter(NULL)
44 		,m_rMutex(_rMutex)
45 	{
46 	}
47 
48 	//---------------------------------------------------------------------
~OContainerListener()49 	OContainerListener::~OContainerListener()
50 	{
51 		if (m_pAdapter)
52 		{
53 			m_pAdapter->dispose();
54 			m_pAdapter = NULL;
55 		}
56 	}
57 
58 	//---------------------------------------------------------------------
_elementInserted(const ContainerEvent &)59 	void OContainerListener::_elementInserted( const ContainerEvent& /*_rEvent*/ ) throw(RuntimeException)
60 	{
61 	}
62 
63 	//---------------------------------------------------------------------
_elementRemoved(const ContainerEvent &)64 	void OContainerListener::_elementRemoved( const ContainerEvent& ) throw(RuntimeException)
65 	{
66 	}
67 
68 	//---------------------------------------------------------------------
_elementReplaced(const ContainerEvent &)69 	void OContainerListener::_elementReplaced( const ContainerEvent& /*_rEvent*/ ) throw(RuntimeException)
70 	{
71 	}
72 
73 	//---------------------------------------------------------------------
_disposing(const EventObject &)74 	void OContainerListener::_disposing(const EventObject& ) throw( RuntimeException)
75 	{
76 	}
77 
78 	//------------------------------------------------------------------
setAdapter(OContainerListenerAdapter * pAdapter)79 	void OContainerListener::setAdapter(OContainerListenerAdapter* pAdapter)
80 	{
81 		if (m_pAdapter)
82 		{
83 			::osl::MutexGuard aGuard(m_rMutex);
84 			m_pAdapter->release();
85 			m_pAdapter = NULL;
86 		}
87 
88 		if (pAdapter)
89 		{
90 			::osl::MutexGuard aGuard(m_rMutex);
91 			m_pAdapter = pAdapter;
92 			m_pAdapter->acquire();
93 		}
94 	}
95 
96 	//=====================================================================
97 	//= OContainerListenerAdapter
98 	//=====================================================================
99 	//---------------------------------------------------------------------
OContainerListenerAdapter(OContainerListener * _pListener,const Reference<XContainer> & _rxContainer)100 	OContainerListenerAdapter::OContainerListenerAdapter(OContainerListener* _pListener,
101 			const  Reference< XContainer >& _rxContainer)
102 		:m_xContainer(_rxContainer)
103 		,m_pListener(_pListener)
104 		,m_nLockCount(0)
105 	{
106 		if (m_pListener)
107 			m_pListener->setAdapter(this);
108 
109 		::comphelper::increment(m_refCount);
110 		try
111 		{
112 			m_xContainer->addContainerListener(this);
113 		}
114 		catch(const Exception&)
115 		{
116 			OSL_ENSURE(0,"Exceptiopn catched!");
117 		}
118 		::comphelper::decrement(m_refCount);
119 	}
120 
121 	//---------------------------------------------------------------------
~OContainerListenerAdapter()122 	OContainerListenerAdapter::~OContainerListenerAdapter()
123 	{
124 	}
125 
126 	//------------------------------------------------------------------
lock()127 	void OContainerListenerAdapter::lock()
128 	{
129 		++m_nLockCount;
130 	}
131 
132 	//------------------------------------------------------------------
unlock()133 	void OContainerListenerAdapter::unlock()
134 	{
135 		--m_nLockCount;
136 	}
137 
138 	//------------------------------------------------------------------
dispose()139 	void OContainerListenerAdapter::dispose()
140 	{
141 		if (m_xContainer.is())
142 		{
143 			try
144 			{
145 				Reference< XContainerListener > xPreventDelete(this);
146 				m_xContainer->removeContainerListener(xPreventDelete);
147 				m_pListener->setAdapter(NULL);
148 			}
149 			catch(const Exception&)
150 			{
151 				OSL_ENSURE(0,"Exception catched!");
152 			}
153 			m_xContainer = NULL;
154 			m_pListener = NULL;
155 		}
156 	}
157 
158 	//------------------------------------------------------------------
disposing(const EventObject & _rSource)159 	void SAL_CALL OContainerListenerAdapter::disposing( const  EventObject& _rSource) throw(RuntimeException)
160 	{
161 		if (m_pListener)
162 		{
163 			 // tell the listener
164 			if (!locked())
165 				m_pListener->_disposing(_rSource);
166 			// disconnect the listener
167             if ( m_pListener )
168 			    m_pListener->setAdapter(NULL);
169 		}
170 
171 		m_xContainer = NULL;
172 		m_pListener = NULL;
173 	}
174 
175 	//------------------------------------------------------------------
elementInserted(const ContainerEvent & _rEvent)176 	void SAL_CALL OContainerListenerAdapter::elementInserted( const ContainerEvent& _rEvent ) throw(RuntimeException)
177 	{
178 		if (m_pListener && !locked())
179 			m_pListener->_elementInserted(_rEvent);
180 	}
181 
182 	//------------------------------------------------------------------
elementRemoved(const ContainerEvent & _rEvent)183 	void SAL_CALL OContainerListenerAdapter::elementRemoved( const ContainerEvent& _rEvent ) throw(RuntimeException)
184 	{
185 		if (m_pListener && !locked())
186 			m_pListener->_elementRemoved(_rEvent);
187 	}
188 
189 	//------------------------------------------------------------------
elementReplaced(const ContainerEvent & _rEvent)190 	void SAL_CALL OContainerListenerAdapter::elementReplaced( const ContainerEvent& _rEvent ) throw(RuntimeException)
191 	{
192 		if (m_pListener && !locked())
193 			m_pListener->_elementReplaced(_rEvent);
194 	}
195 
196 //.........................................................................
197 }	// namespace comphelper
198 //.........................................................................
199 
200