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