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_chart2.hxx"
26
27 #include "CommandDispatch.hxx"
28 #include "CommonFunctors.hxx"
29 #include "macros.hxx"
30
31 #include <algorithm>
32 #include <functional>
33
34 using namespace ::com::sun::star;
35
36 using ::com::sun::star::uno::Reference;
37 using ::com::sun::star::uno::Sequence;
38 using ::rtl::OUString;
39
40 namespace
41 {
42 template< class Map >
43 struct lcl_DisposeAndClearAndDeleteMapElement :
44 public ::std::unary_function< typename Map::value_type, void >
45 {
lcl_DisposeAndClearAndDeleteMapElement__anoncd5966600111::lcl_DisposeAndClearAndDeleteMapElement46 lcl_DisposeAndClearAndDeleteMapElement( const Reference< uno::XInterface > & xEventSource ) :
47 m_aEvent( xEventSource )
48 {}
operator ()__anoncd5966600111::lcl_DisposeAndClearAndDeleteMapElement49 void operator() ( typename Map::value_type & rElement )
50 {
51 if( rElement.second )
52 {
53 rElement.second->disposeAndClear( m_aEvent );
54 delete rElement.second;
55 }
56 }
57 private:
58 lang::EventObject m_aEvent;
59 };
60
61 template< class Map >
lcl_DisposeAndClearAndDeleteAllMapElements(Map & rMap,const Reference<uno::XInterface> & xEventSource)62 void lcl_DisposeAndClearAndDeleteAllMapElements(
63 Map & rMap,
64 const Reference< uno::XInterface > & xEventSource )
65 {
66 ::std::for_each( rMap.begin(), rMap.end(),
67 lcl_DisposeAndClearAndDeleteMapElement< Map >( xEventSource ));
68 }
69
70 } // anonymous namespace
71
72 namespace chart
73 {
74
CommandDispatch(const Reference<uno::XComponentContext> & xContext)75 CommandDispatch::CommandDispatch(
76 const Reference< uno::XComponentContext > & xContext ) :
77 impl::CommandDispatch_Base( m_aMutex ),
78 m_xContext( xContext )
79 {
80 }
81
~CommandDispatch()82 CommandDispatch::~CommandDispatch()
83 {}
84
initialize()85 void CommandDispatch::initialize()
86 {}
87
88 // ____ WeakComponentImplHelperBase ____
89 /// is called when this is disposed
disposing()90 void SAL_CALL CommandDispatch::disposing()
91 {
92 lcl_DisposeAndClearAndDeleteAllMapElements( m_aListeners, static_cast< cppu::OWeakObject* >( this ));
93 m_aListeners.clear();
94 }
95
96 // ____ XDispatch ____
dispatch(const util::URL &,const Sequence<beans::PropertyValue> &)97 void SAL_CALL CommandDispatch::dispatch( const util::URL& /* URL */, const Sequence< beans::PropertyValue >& /* Arguments */ )
98 throw (uno::RuntimeException)
99 {}
100
addStatusListener(const Reference<frame::XStatusListener> & Control,const util::URL & URL)101 void SAL_CALL CommandDispatch::addStatusListener( const Reference< frame::XStatusListener >& Control, const util::URL& URL )
102 throw (uno::RuntimeException)
103 {
104 tListenerMap::iterator aIt( m_aListeners.find( URL.Complete ));
105 if( aIt == m_aListeners.end())
106 {
107 aIt = m_aListeners.insert(
108 m_aListeners.begin(),
109 tListenerMap::value_type( URL.Complete, new ::cppu::OInterfaceContainerHelper( m_aMutex )));
110 }
111 OSL_ASSERT( aIt != m_aListeners.end());
112
113 aIt->second->addInterface( Control );
114 fireStatusEvent( URL.Complete, Control );
115 }
116
removeStatusListener(const Reference<frame::XStatusListener> & Control,const util::URL & URL)117 void SAL_CALL CommandDispatch::removeStatusListener( const Reference< frame::XStatusListener >& Control, const util::URL& URL )
118 throw (uno::RuntimeException)
119 {
120 tListenerMap::iterator aIt( m_aListeners.find( URL.Complete ));
121 if( aIt != m_aListeners.end())
122 (*aIt).second->removeInterface( Control );
123 }
124
125 // ____ XModifyListener ____
modified(const lang::EventObject &)126 void SAL_CALL CommandDispatch::modified( const lang::EventObject& /* aEvent */ )
127 throw (uno::RuntimeException)
128 {
129 fireAllStatusEvents( 0 );
130 }
131
132 // ____ XEventListener (base of XModifyListener) ____
disposing(const lang::EventObject &)133 void SAL_CALL CommandDispatch::disposing( const lang::EventObject& /* Source */ )
134 throw (uno::RuntimeException)
135 {}
136
fireAllStatusEvents(const::com::sun::star::uno::Reference<::com::sun::star::frame::XStatusListener> & xSingleListener)137 void CommandDispatch::fireAllStatusEvents(
138 const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XStatusListener > & xSingleListener )
139 {
140 fireStatusEvent( OUString(), xSingleListener );
141 }
142
fireStatusEventForURL(const OUString & rURL,const uno::Any & rState,bool bEnabled,const Reference<frame::XStatusListener> & xSingleListener,const OUString & rFeatureDescriptor)143 void CommandDispatch::fireStatusEventForURL(
144 const OUString & rURL,
145 const uno::Any & rState,
146 bool bEnabled,
147 const Reference< frame::XStatusListener > & xSingleListener, /* = 0 */
148 const OUString & rFeatureDescriptor /* = OUString() */ )
149 {
150 // prepare event to send
151 util::URL aURL;
152 aURL.Complete = rURL;
153 if( !m_xURLTransformer.is())
154 {
155 m_xURLTransformer.set(
156 m_xContext->getServiceManager()->createInstanceWithContext(
157 C2U( "com.sun.star.util.URLTransformer" ),
158 m_xContext ),
159 uno::UNO_QUERY );
160 }
161 if( m_xURLTransformer.is())
162 m_xURLTransformer->parseStrict( aURL );
163
164 frame::FeatureStateEvent aEventToSend(
165 static_cast< cppu::OWeakObject* >( this ), // Source
166 aURL, // FeatureURL
167 rFeatureDescriptor, // FeatureDescriptor
168 bEnabled, // IsEnabled
169 false, // Requery
170 rState // State
171 );
172
173 // send event either to single listener or all registered ones
174 if( xSingleListener.is())
175 xSingleListener->statusChanged( aEventToSend );
176 else
177 {
178 tListenerMap::iterator aIt( m_aListeners.find( aURL.Complete ));
179 if( aIt != m_aListeners.end())
180 {
181 // ::cppu::OInterfaceContainerHelper * pCntHlp = rBHelper.getContainer(
182 // ::getCppuType( reinterpret_cast< Reference< frame::XStatusListener > * >(0)));
183 if( aIt->second )
184 {
185 ::cppu::OInterfaceIteratorHelper aIntfIt( *((*aIt).second) );
186
187 while( aIntfIt.hasMoreElements())
188 {
189 Reference< frame::XStatusListener > xListener( aIntfIt.next(), uno::UNO_QUERY );
190 try
191 {
192 if( xListener.is())
193 xListener->statusChanged( aEventToSend );
194 }
195 catch( const uno::Exception & ex )
196 {
197 ASSERT_EXCEPTION( ex );
198 }
199 }
200 }
201 }
202 }
203 }
204
205
206
207 } // namespace chart
208