xref: /trunk/main/comphelper/source/container/containermultiplexer.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
30 #include "comphelper/containermultiplexer.hxx"
31 #include "comphelper/uno3.hxx"
32 #include <osl/diagnose.h>
33 //.........................................................................
34 namespace comphelper
35 {
36 //.........................................................................
37 
38     using namespace ::com::sun::star::uno;
39     using namespace ::com::sun::star::lang;
40     using namespace ::com::sun::star::container;
41 
42     //=====================================================================
43     //= OContainerListener
44     //=====================================================================
45     //---------------------------------------------------------------------
46     OContainerListener::OContainerListener(::osl::Mutex& _rMutex)
47         :m_pAdapter(NULL)
48         ,m_rMutex(_rMutex)
49     {
50     }
51 
52     //---------------------------------------------------------------------
53     OContainerListener::~OContainerListener()
54     {
55         if (m_pAdapter)
56         {
57             m_pAdapter->dispose();
58             m_pAdapter = NULL;
59         }
60     }
61 
62     //---------------------------------------------------------------------
63     void OContainerListener::_elementInserted( const ContainerEvent& /*_rEvent*/ ) throw(RuntimeException)
64     {
65     }
66 
67     //---------------------------------------------------------------------
68     void OContainerListener::_elementRemoved( const ContainerEvent& ) throw(RuntimeException)
69     {
70     }
71 
72     //---------------------------------------------------------------------
73     void OContainerListener::_elementReplaced( const ContainerEvent& /*_rEvent*/ ) throw(RuntimeException)
74     {
75     }
76 
77     //---------------------------------------------------------------------
78     void OContainerListener::_disposing(const EventObject& ) throw( RuntimeException)
79     {
80     }
81 
82     //------------------------------------------------------------------
83     void OContainerListener::setAdapter(OContainerListenerAdapter* pAdapter)
84     {
85         if (m_pAdapter)
86         {
87             ::osl::MutexGuard aGuard(m_rMutex);
88             m_pAdapter->release();
89             m_pAdapter = NULL;
90         }
91 
92         if (pAdapter)
93         {
94             ::osl::MutexGuard aGuard(m_rMutex);
95             m_pAdapter = pAdapter;
96             m_pAdapter->acquire();
97         }
98     }
99 
100     //=====================================================================
101     //= OContainerListenerAdapter
102     //=====================================================================
103     //---------------------------------------------------------------------
104     OContainerListenerAdapter::OContainerListenerAdapter(OContainerListener* _pListener,
105             const  Reference< XContainer >& _rxContainer)
106         :m_xContainer(_rxContainer)
107         ,m_pListener(_pListener)
108         ,m_nLockCount(0)
109     {
110         if (m_pListener)
111             m_pListener->setAdapter(this);
112 
113         ::comphelper::increment(m_refCount);
114         try
115         {
116             m_xContainer->addContainerListener(this);
117         }
118         catch(const Exception&)
119         {
120             OSL_ENSURE(0,"Exceptiopn catched!");
121         }
122         ::comphelper::decrement(m_refCount);
123     }
124 
125     //---------------------------------------------------------------------
126     OContainerListenerAdapter::~OContainerListenerAdapter()
127     {
128     }
129 
130     //------------------------------------------------------------------
131     void OContainerListenerAdapter::lock()
132     {
133         ++m_nLockCount;
134     }
135 
136     //------------------------------------------------------------------
137     void OContainerListenerAdapter::unlock()
138     {
139         --m_nLockCount;
140     }
141 
142     //------------------------------------------------------------------
143     void OContainerListenerAdapter::dispose()
144     {
145         if (m_xContainer.is())
146         {
147             try
148             {
149                 Reference< XContainerListener > xPreventDelete(this);
150                 m_xContainer->removeContainerListener(xPreventDelete);
151                 m_pListener->setAdapter(NULL);
152             }
153             catch(const Exception&)
154             {
155                 OSL_ENSURE(0,"Exception catched!");
156             }
157             m_xContainer = NULL;
158             m_pListener = NULL;
159         }
160     }
161 
162     //------------------------------------------------------------------
163     void SAL_CALL OContainerListenerAdapter::disposing( const  EventObject& _rSource) throw(RuntimeException)
164     {
165         if (m_pListener)
166         {
167              // tell the listener
168             if (!locked())
169                 m_pListener->_disposing(_rSource);
170             // disconnect the listener
171             if ( m_pListener )
172                 m_pListener->setAdapter(NULL);
173         }
174 
175         m_xContainer = NULL;
176         m_pListener = NULL;
177     }
178 
179     //------------------------------------------------------------------
180     void SAL_CALL OContainerListenerAdapter::elementInserted( const ContainerEvent& _rEvent ) throw(RuntimeException)
181     {
182         if (m_pListener && !locked())
183             m_pListener->_elementInserted(_rEvent);
184     }
185 
186     //------------------------------------------------------------------
187     void SAL_CALL OContainerListenerAdapter::elementRemoved( const ContainerEvent& _rEvent ) throw(RuntimeException)
188     {
189         if (m_pListener && !locked())
190             m_pListener->_elementRemoved(_rEvent);
191     }
192 
193     //------------------------------------------------------------------
194     void SAL_CALL OContainerListenerAdapter::elementReplaced( const ContainerEvent& _rEvent ) throw(RuntimeException)
195     {
196         if (m_pListener && !locked())
197             m_pListener->_elementReplaced(_rEvent);
198     }
199 
200 //.........................................................................
201 }   // namespace comphelper
202 //.........................................................................
203 
204