Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "ModifyListenerHelper.hxx"
21 : #include "WeakListenerAdapter.hxx"
22 : #include "macros.hxx"
23 :
24 : #include <cppuhelper/interfacecontainer.hxx>
25 :
26 : #include <com/sun/star/frame/XModel.hpp>
27 :
28 : using namespace ::com::sun::star;
29 :
30 : using ::com::sun::star::uno::Reference;
31 : using ::com::sun::star::uno::Sequence;
32 :
33 : namespace
34 : {
35 :
36 211476 : void lcl_fireModifyEvent(
37 : ::cppu::OBroadcastHelper & rBroadcastHelper,
38 : const Reference< uno::XWeak > & xEventSource,
39 : const lang::EventObject * pEvent )
40 : {
41 : ::cppu::OInterfaceContainerHelper * pCntHlp = rBroadcastHelper.getContainer(
42 211476 : ::getCppuType( reinterpret_cast< Reference< util::XModifyListener > * >(0)));
43 211476 : if( pCntHlp )
44 : {
45 53690 : lang::EventObject aEventToSend;
46 53690 : if( pEvent )
47 53690 : aEventToSend = *pEvent;
48 : else
49 0 : aEventToSend.Source.set( xEventSource );
50 : OSL_ENSURE( aEventToSend.Source.is(), "Sending event without source" );
51 :
52 107380 : ::cppu::OInterfaceIteratorHelper aIt( *pCntHlp );
53 :
54 161065 : while( aIt.hasMoreElements())
55 : {
56 53685 : Reference< util::XModifyListener > xModListener( aIt.next(), uno::UNO_QUERY );
57 53685 : if( xModListener.is())
58 53685 : xModListener->modified( aEventToSend );
59 107375 : }
60 : }
61 211476 : }
62 :
63 55305 : struct lcl_weakReferenceToSame : public ::std::unary_function<
64 : ::std::pair<
65 : ::com::sun::star::uno::WeakReference< ::com::sun::star::util::XModifyListener >,
66 : ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifyListener > >,
67 : bool >
68 : {
69 18435 : lcl_weakReferenceToSame( const Reference< util::XModifyListener > & xModListener ) :
70 18435 : m_xHardRef( xModListener )
71 18435 : {}
72 :
73 18435 : bool operator() ( const argument_type & xElem )
74 : {
75 18435 : Reference< util::XModifyListener > xWeakAsHard( xElem.first );
76 18435 : if( xWeakAsHard.is())
77 18435 : return (xWeakAsHard == m_xHardRef);
78 0 : return false;
79 : }
80 :
81 : private:
82 : Reference< util::XModifyListener > m_xHardRef;
83 : };
84 :
85 : } // anonymous namespace
86 :
87 : namespace chart
88 : {
89 : namespace ModifyListenerHelper
90 : {
91 :
92 92621 : uno::Reference< util::XModifyListener > createModifyEventForwarder()
93 : {
94 92621 : return new ModifyEventForwarder();
95 : }
96 :
97 92658 : ModifyEventForwarder::ModifyEventForwarder() :
98 : ::cppu::WeakComponentImplHelper2<
99 : ::com::sun::star::util::XModifyBroadcaster,
100 : ::com::sun::star::util::XModifyListener >( m_aMutex ),
101 92658 : m_aModifyListeners( m_aMutex )
102 : {
103 92658 : }
104 :
105 211476 : void ModifyEventForwarder::FireEvent( const lang::EventObject & rEvent )
106 : {
107 211476 : lcl_fireModifyEvent( m_aModifyListeners, Reference< uno::XWeak >(), & rEvent );
108 211476 : }
109 :
110 21037 : void ModifyEventForwarder::AddListener( const Reference< util::XModifyListener >& aListener )
111 : {
112 : try
113 : {
114 21037 : Reference< util::XModifyListener > xListenerToAdd( aListener );
115 :
116 42074 : Reference< uno::XWeak > xWeak( aListener, uno::UNO_QUERY );
117 21037 : if( xWeak.is())
118 : {
119 : // remember the helper class for later remove
120 21037 : uno::WeakReference< util::XModifyListener > xWeakRef( aListener );
121 21037 : xListenerToAdd.set( new WeakModifyListenerAdapter( xWeakRef ));
122 21037 : m_aListenerMap.push_back( tListenerMap::value_type( xWeakRef, xListenerToAdd ));
123 : }
124 :
125 42074 : m_aModifyListeners.addListener( ::getCppuType( &xListenerToAdd ), xListenerToAdd );
126 : }
127 0 : catch( const uno::Exception & ex )
128 : {
129 : ASSERT_EXCEPTION( ex );
130 : }
131 21037 : }
132 :
133 18435 : void ModifyEventForwarder::RemoveListener( const Reference< util::XModifyListener >& aListener )
134 : {
135 : try
136 : {
137 : // look up fitting helper class that has been added
138 18435 : Reference< util::XModifyListener > xListenerToRemove( aListener );
139 : tListenerMap::iterator aIt(
140 18435 : ::std::find_if( m_aListenerMap.begin(), m_aListenerMap.end(), lcl_weakReferenceToSame( aListener )));
141 18435 : if( aIt != m_aListenerMap.end())
142 : {
143 18435 : xListenerToRemove.set( (*aIt).second );
144 : // map entry is no longer needed
145 18435 : m_aListenerMap.erase( aIt );
146 : }
147 :
148 18435 : m_aModifyListeners.removeListener( ::getCppuType( &aListener ), xListenerToRemove );
149 : }
150 0 : catch( const uno::Exception & ex )
151 : {
152 : ASSERT_EXCEPTION( ex );
153 : }
154 18435 : }
155 :
156 90334 : void ModifyEventForwarder::DisposeAndClear( const Reference< uno::XWeak > & xSource )
157 : {
158 : ::cppu::OInterfaceContainerHelper * pCntHlp = m_aModifyListeners.getContainer(
159 90334 : ::getCppuType( reinterpret_cast< Reference< util::XModifyListener > * >(0)));
160 90334 : if( pCntHlp )
161 18223 : pCntHlp->disposeAndClear( lang::EventObject( xSource ) );
162 90334 : }
163 :
164 : // ____ XModifyBroadcaster ____
165 21037 : void SAL_CALL ModifyEventForwarder::addModifyListener( const Reference< util::XModifyListener >& aListener )
166 : throw (uno::RuntimeException, std::exception)
167 : {
168 21037 : AddListener( aListener );
169 21037 : }
170 :
171 18435 : void SAL_CALL ModifyEventForwarder::removeModifyListener( const Reference< util::XModifyListener >& aListener )
172 : throw (uno::RuntimeException, std::exception)
173 : {
174 18435 : RemoveListener( aListener );
175 18435 : }
176 :
177 : // ____ XModifyListener ____
178 211476 : void SAL_CALL ModifyEventForwarder::modified( const lang::EventObject& aEvent )
179 : throw (uno::RuntimeException, std::exception)
180 : {
181 211476 : FireEvent( aEvent );
182 211476 : }
183 :
184 : // ____ XEventListener (base of XModifyListener) ____
185 9 : void SAL_CALL ModifyEventForwarder::disposing( const lang::EventObject& /* Source */ )
186 : throw (uno::RuntimeException, std::exception)
187 : {
188 : // nothing
189 9 : }
190 :
191 : // ____ WeakComponentImplHelperBase ____
192 90334 : void SAL_CALL ModifyEventForwarder::disposing()
193 : {
194 : // dispose was called at this
195 90334 : DisposeAndClear( this );
196 90334 : }
197 :
198 : } // namespace ModifyListenerHelper
199 : } // namespace chart
200 :
201 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|