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