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