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